Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
fixes #434 page checks duplicating pages and other language switching…
Browse files Browse the repository at this point in the history
… issues

Issue with page checks uncovered various other historic issues in the
language detection and switching, addressed in this commit
  • Loading branch information
Jon007 committed Jun 30, 2019
1 parent 7d42820 commit 8c54fb2
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 105 deletions.
23 changes: 20 additions & 3 deletions src/Hyyan/WPI/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function get_default_setting( $string_type ) {
public function __construct()
{
if ('on' === Settings::getOption('emails', Features::getID(), 'on')) {
add_filter( 'plugin_locale', array( $this, 'correctLocal' ), 999 );

//Note: correct locale was performing language switching functions which should not really be performed on every call to this filter - language now switched more fully on first translation call and better detection now done in translateCommonString
//add_filter( 'plugin_locale', array( $this, 'correctLocal' ), 999 );
// Register WooCommerce email subjects and headings in polylang strings translations table
$this->registerEmailStringsForTranslation(); // called only after all plugins are loaded
// Translate WooCommerce email subjects and headings to the order language
Expand Down Expand Up @@ -274,8 +274,25 @@ public function translateCommonString( $email_string ) {
if ( $post ) {
$locale = pll_get_post_language( $post->ID );
return pll_translate_string( $email_string, $locale );
} else { //moved from correctLocal
$ID = false;
if ( ! isset( $_REQUEST[ 'ipn_track_id' ] ) ) {
$search = array( 'post', 'post_ID', 'pll_post_id', 'order_id' );

foreach ( $search as $value ) {
if ( isset( $_REQUEST[ $value ] ) ) {
$ID = esc_attr( $_REQUEST[ $value ] );
break;
}
}
} else {
$ID = $this->getOrderIDFromIPNRequest();
}
if ( $ID ) {
$locale = pll_get_post_language( $ID );
return pll_translate_string( $email_string, $locale );
}
}
//}
}
$trans = pll__( $email_string );
if ( $trans ) {
Expand Down
226 changes: 153 additions & 73 deletions src/Hyyan/WPI/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function __construct()
//skipping ajax
} else {
$wcpagecheck_passed = get_option( 'wpi_wcpagecheck_passed' );
if ( Settings::getOption( 'checkpages', Features::getID(), 0 ) || get_option( 'wpi_wcpagecheck_passed' ) == '0' ) {
add_action( 'current_screen', array( __CLASS__, 'wpi_ensure_woocommerce_pages_translated' ) );
$check_pages = Settings::getOption( 'checkpages', Features::getID(), 0 );
if ( ($check_pages && $check_pages != 'off') || ! ($wcpagecheck_passed) ) {
add_action( 'current_screen', array( __CLASS__, 'wpi_ensure_woocommerce_pages_translated' ) );
}
}
}
Expand Down Expand Up @@ -97,11 +98,7 @@ public function activate()
add_filter('plugin_action_links_woo-poly-integration/__init__.php', function ($links) {
$baseURL = is_multisite() ? get_admin_url() : admin_url();
$settingsLinks = array(
'<a href="'
. $baseURL
. 'options-general.php?page=hyyan-wpi">'
. __('Settings', ' woo-poly-integration')
. '</a>',
static::settingsLinkHTML(),
'<a target="_blank" href="https://github.com/hyyan/woo-poly-integration/wiki">'
. __('Docs', 'woo-poly-integration')
. '</a>',
Expand All @@ -121,6 +118,19 @@ public function activate()
$this->registerCore();
}

/*
* make settings page link easily available from multiple messages
*/
public static function settingsLinkHTML()
{
$baseURL = is_multisite() ? get_admin_url() : admin_url();
return '<a href="'
. $baseURL
. 'options-general.php?page=hyyan-wpi">'
. __( 'Settings', ' woo-poly-integration' )
. '</a>';
}

/**
* Check if the plugin can be activated.
*
Expand Down Expand Up @@ -306,23 +316,52 @@ public static function wpi_ensure_woocommerce_pages_translated() {
$page_types = array( 'cart', 'checkout', 'myaccount', 'shop' );
$pages = array();
$warnings = array();
$failure = false;
//only create pages if the setting is on, otherwise only warnings will be shown
$create_pages = Settings::getOption( 'checkpages', Features::getID(), 0 );
if ( $create_pages && $create_pages == 'off' ) {
$create_pages = false;
}

//just in case, get and ensure we are in the default locale
//get status of current language environment
$default_lang = pll_default_language();
$default_locale = pll_default_language( 'locale' );
$start_locale = get_locale();
if ( $default_locale != $start_locale ) {
$start_locale = ( is_admin() ) ? get_user_locale() : get_locale();
//important: in admin mode and 'Show all language' there is no polylang current language
$pll_start_locale = pll_current_language( 'locale' );

/*
* important, we must be in the base language before doing the check
* because otherwise the posts will be pll filtered into other language
* and appear to be missing if not translated
*/
if ( $pll_start_locale ) {
if ( $default_locale != $pll_start_locale ) {
Utilities::switchLocale( $default_locale );
}
} elseif ( $default_locale != $start_locale ) {
Utilities::switchLocale( $default_locale );
}

//get the current id of each woocommerce page
/*
* get the current id of each woocommerce page in the base language
* using the native woocommerce function to fill any missing page
*/
foreach ( $page_types as $page_type ) {
$pageid = wc_get_page_id( $page_type );
if ( $pageid == -1 || ! get_post( $pageid ) ) {
//if any of the pages is missing, rerun the woocommerce page creation
//which will just fill in any missing page
\WC_Install::create_pages();
$pageid = wc_get_page_id( $page_type );
if ( $create_pages ) {
//if any of the pages is missing, rerun the woocommerce page creation
//which will just fill in any missing page
\WC_Install::create_pages();
$pageid = wc_get_page_id( $page_type );
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
__( '%1$s page in base language %2$s was not found and was created using woocommerce create_pages() as page <a href="%3$s">%4$s</a>', 'woo-poly-integration' ), $page_type, $default_locale, edit_post_link( $pageid, 'link' ), $pageid );
} else {
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
__( '%1$s page in language %2$s was not found and must be created for the shop to work: this will be done automatically if Check WooCommerce Pages option is enabled in %3$s. Translations for this page may also be missing.', 'woo-poly-integration' ), $page_type, $default_locale, static::settingsLinkHTML() );
$failure = true;
}
}
$pages[ $page_type ] = $pageid;
}
Expand All @@ -334,16 +373,20 @@ public static function wpi_ensure_woocommerce_pages_translated() {

//for each page, check all the translations and fill in and link where necessary
foreach ( $pages as $page_type => $orig_page_id ) {
$changed = false;
$orig_page = get_post( $orig_page_id );
if ( $orig_page ) {
$orig_postlocale = pll_get_post_language( $orig_page_id, 'locale' );
$orig_postlang = pll_get_post_language( $orig_page_id, 'slug' );
//default pages may not have language set correctly
if ( ! $orig_postlocale || ($orig_postlocale != $default_locale) ) {
if ( ! $orig_postlocale ) {
$orig_postlocale = $default_locale;
pll_set_post_language( $orig_page_id, $default_lang );
$orig_postlang = $default_lang;
pll_set_post_language( $orig_page_id, $orig_postlang );
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
__( '%1$s page did not have language - language set to %2$s on page <a href="%3$s">%4$s</a>', 'woo-poly-integration' ), $page_type, $default_locale, edit_post_link( $orig_page_id, 'link' ), $orig_page_id );
}
$translations[ $default_lang ] = $orig_page_id;
$changed = false;
$translations[ $orig_postlang ] = $orig_page_id;
foreach ( $langs as $langId => $langLocale ) {
$translation_id = $orig_page_id;
$langSlug = $lang_slugs[ $langId ];
Expand All @@ -356,67 +399,92 @@ public static function wpi_ensure_woocommerce_pages_translated() {
// and there is no translation in target language
$translation_id = pll_get_post( $orig_page_id, $langLocale );
if ( $translation_id == 0 || $translation_id == $orig_page_id ) {

//then create new post in target language
$isNewPost = true;
Utilities::switchLocale( $langLocale );

//default to copy source page
$post_name = $orig_page->post_name;
$post_title = $orig_page->post_title;
$post_content = $orig_page->post_content;

//ideally, get correct translation
switch ( $page_type ) {
case 'shop':
$post_name = _x( 'shop', 'Page slug', 'woocommerce' );
$post_title = _x( 'Shop', 'Page title', 'woocommerce' );
$post_content = '';
break;
case 'cart':
$post_name = _x( 'cart', 'Page slug', 'woocommerce' );
$post_title = _x( 'Cart', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']<!-- /wp:shortcode -->';
break;
case 'checkout':
$post_name = _x( 'checkout', 'Page slug', 'woocommerce' );
$post_title = _x( 'Checkout', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']<!-- /wp:shortcode -->';
break;
case 'myaccount':
$post_name = _x( 'my-account', 'Page slug', 'woocommerce' );
$post_title = _x( 'My account', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->';
break;
if ( $create_pages ) {
//then create new post in target language
$isNewPost = true;
Utilities::switchLocale( $langLocale );

//default to copy source page
$post_name = $orig_page->post_name;
$post_title = $orig_page->post_title;
$post_content = $orig_page->post_content;

//ideally, get correct translation
switch ( $page_type ) {
case 'shop':
$post_name = _x( 'shop', 'Page slug', 'woocommerce' );
$post_title = _x( 'Shop', 'Page title', 'woocommerce' );
$post_content = '';
break;
case 'cart':
$post_name = _x( 'cart', 'Page slug', 'woocommerce' );
$post_title = _x( 'Cart', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']<!-- /wp:shortcode -->';
break;
case 'checkout':
$post_name = _x( 'checkout', 'Page slug', 'woocommerce' );
$post_title = _x( 'Checkout', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']<!-- /wp:shortcode -->';
break;
case 'myaccount':
$post_name = _x( 'my-account', 'Page slug', 'woocommerce' );
$post_title = _x( 'My account', 'Page title', 'woocommerce' );
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->';
break;
}


$page_data = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $post_name,
'post_title' => $post_title,
'post_content' => $post_content,
//'post_parent' => $post_parent,
'comment_status' => 'closed',
);
$translation_id = wp_insert_post( $page_data );
}
//if there now is a translation is where there was not before, creation must have been successful
if ( $translation_id ) {
pll_set_post_language( $translation_id, $langSlug );
$changed = true;
$warnings[ $page_type . '::' . $langLocale ] = sprintf(
__( '%1$s page in language %2$s was not found and was created as page <a href="%3$s">%4$s</a>', 'woo-poly-integration' ), $page_type, $langLocale, get_edit_post_link( $translation_id, 'link' ), $translation_id );
} else {
$warnings[ $page_type . '::' . $langLocale ] = sprintf(
__( '%1$s page in language %2$s was not found and must be created for the shop to work in this language: this will be done automatically if Check WooCommerce Pages option is enabled in %3$s.', 'woo-poly-integration' ), $page_type, $langLocale, static::settingsLinkHTML() );
$failure = true;
}


$page_data = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_author' => 1,
'post_name' => $post_name,
'post_title' => $post_title,
'post_content' => $post_content,
//'post_parent' => $post_parent,
'comment_status' => 'closed',
);
$translation_id = wp_insert_post( $page_data );

pll_set_post_language( $translation_id, $langSlug );
$changed = true;
}
//always add the existing translations back into the translations array
$translations [ $langSlug ] = $translation_id;
if ( $translation_id ) {
$translations [ $langSlug ] = $translation_id;
}
}
//if this woocommerce page is an existing post, check post status
if ( $translation_id && ! $isNewPost ) {
$thisPost = get_post( $translation_id );
if ( $thisPost ) {
$postStatus = $thisPost->post_status;
if ( $postStatus != 'publish' ) {
$baseURL = is_multisite() ? get_admin_url() : admin_url();
if ( $postStatus == 'trash' ) {
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
__( '%1$s page in language %2$s has been deleted, please check the <a href="%3$s">trash</a>, and restore page %4$s', 'woo-poly-integration' ), $page_type, $langLocale, $baseURL . 'edit.php?post_status=trash&post_type=page&lang=' . $langSlug, $translation_id );
} else {
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
__( '%1$s page in language %2$s is in status %3$s and needs to be published for the shop to work properly, check page <a href="%4$s">%5$s</a>', 'woo-poly-integration' ), $page_type, $langLocale, $postStatus, get_edit_post_link( $translation_id, 'link' ), $translation_id );
}
$failure = true;
}
} else {
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
__( '%1$s page in language %2$s is in status %3$s and needs to be published for the shop to work properly, check page id %4$s', 'woo-poly-integration' ), $page_type, $langLocale, $postStatus, $translation_id );
__( '%1$s page in language %2$s was linked in polylang but cannot be found, link will be removed to missing page %3$s', 'woo-poly-integration' ), $page_type, $langLocale, $translation_id );
unset( $translations[ $langSlug ] );
$failure = true;
$changed = true;
}
}
}
Expand All @@ -425,21 +493,33 @@ public static function wpi_ensure_woocommerce_pages_translated() {
pll_save_post_translations( $translations );
}
}
}

/*
* update result of page checks
*/
if ( $warnings ) {
FlashMessages::add(
'pagechecks', implode( '<br/>', $warnings )
, array( 'updated' ), true
);
update_option( 'wpi_wcpagecheck_passed', false );
} else {
FlashMessages::remove( 'pagechecks' );
update_option( 'wpi_wcpagecheck_passed', true );
}
if ( $failure ) {
update_option( 'wpi_wcpagecheck_passed', '0' );
} else {
update_option( 'wpi_wcpagecheck_passed', '1' );
}

/*
* check current locale and reset it if changed
*/
$locale = get_locale();
if ( $locale != $start_locale ) {
Utilities::switchLocale( $start_locale );
Utilities::switch_wp_locale( $start_locale );
}
if ( $locale != $pll_start_locale ) {
Utilities::switch_pll_locale( $pll_start_locale );
}
}

Expand Down
Loading

0 comments on commit 8c54fb2

Please sign in to comment.