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

Commit 8c54fb2

Browse files
committed
fixes #434 page checks duplicating pages and other language switching issues
Issue with page checks uncovered various other historic issues in the language detection and switching, addressed in this commit
1 parent 7d42820 commit 8c54fb2

File tree

3 files changed

+260
-105
lines changed

3 files changed

+260
-105
lines changed

src/Hyyan/WPI/Emails.php

+20-3
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function get_default_setting( $string_type ) {
8080
public function __construct()
8181
{
8282
if ('on' === Settings::getOption('emails', Features::getID(), 'on')) {
83-
add_filter( 'plugin_locale', array( $this, 'correctLocal' ), 999 );
84-
83+
//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
84+
//add_filter( 'plugin_locale', array( $this, 'correctLocal' ), 999 );
8585
// Register WooCommerce email subjects and headings in polylang strings translations table
8686
$this->registerEmailStringsForTranslation(); // called only after all plugins are loaded
8787
// Translate WooCommerce email subjects and headings to the order language
@@ -274,8 +274,25 @@ public function translateCommonString( $email_string ) {
274274
if ( $post ) {
275275
$locale = pll_get_post_language( $post->ID );
276276
return pll_translate_string( $email_string, $locale );
277+
} else { //moved from correctLocal
278+
$ID = false;
279+
if ( ! isset( $_REQUEST[ 'ipn_track_id' ] ) ) {
280+
$search = array( 'post', 'post_ID', 'pll_post_id', 'order_id' );
281+
282+
foreach ( $search as $value ) {
283+
if ( isset( $_REQUEST[ $value ] ) ) {
284+
$ID = esc_attr( $_REQUEST[ $value ] );
285+
break;
286+
}
287+
}
288+
} else {
289+
$ID = $this->getOrderIDFromIPNRequest();
290+
}
291+
if ( $ID ) {
292+
$locale = pll_get_post_language( $ID );
293+
return pll_translate_string( $email_string, $locale );
294+
}
277295
}
278-
//}
279296
}
280297
$trans = pll__( $email_string );
281298
if ( $trans ) {

src/Hyyan/WPI/Plugin.php

+153-73
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function __construct()
4545
//skipping ajax
4646
} else {
4747
$wcpagecheck_passed = get_option( 'wpi_wcpagecheck_passed' );
48-
if ( Settings::getOption( 'checkpages', Features::getID(), 0 ) || get_option( 'wpi_wcpagecheck_passed' ) == '0' ) {
49-
add_action( 'current_screen', array( __CLASS__, 'wpi_ensure_woocommerce_pages_translated' ) );
48+
$check_pages = Settings::getOption( 'checkpages', Features::getID(), 0 );
49+
if ( ($check_pages && $check_pages != 'off') || ! ($wcpagecheck_passed) ) {
50+
add_action( 'current_screen', array( __CLASS__, 'wpi_ensure_woocommerce_pages_translated' ) );
5051
}
5152
}
5253
}
@@ -97,11 +98,7 @@ public function activate()
9798
add_filter('plugin_action_links_woo-poly-integration/__init__.php', function ($links) {
9899
$baseURL = is_multisite() ? get_admin_url() : admin_url();
99100
$settingsLinks = array(
100-
'<a href="'
101-
. $baseURL
102-
. 'options-general.php?page=hyyan-wpi">'
103-
. __('Settings', ' woo-poly-integration')
104-
. '</a>',
101+
static::settingsLinkHTML(),
105102
'<a target="_blank" href="https://github.com/hyyan/woo-poly-integration/wiki">'
106103
. __('Docs', 'woo-poly-integration')
107104
. '</a>',
@@ -121,6 +118,19 @@ public function activate()
121118
$this->registerCore();
122119
}
123120

121+
/*
122+
* make settings page link easily available from multiple messages
123+
*/
124+
public static function settingsLinkHTML()
125+
{
126+
$baseURL = is_multisite() ? get_admin_url() : admin_url();
127+
return '<a href="'
128+
. $baseURL
129+
. 'options-general.php?page=hyyan-wpi">'
130+
. __( 'Settings', ' woo-poly-integration' )
131+
. '</a>';
132+
}
133+
124134
/**
125135
* Check if the plugin can be activated.
126136
*
@@ -306,23 +316,52 @@ public static function wpi_ensure_woocommerce_pages_translated() {
306316
$page_types = array( 'cart', 'checkout', 'myaccount', 'shop' );
307317
$pages = array();
308318
$warnings = array();
319+
$failure = false;
320+
//only create pages if the setting is on, otherwise only warnings will be shown
321+
$create_pages = Settings::getOption( 'checkpages', Features::getID(), 0 );
322+
if ( $create_pages && $create_pages == 'off' ) {
323+
$create_pages = false;
324+
}
309325

310-
//just in case, get and ensure we are in the default locale
326+
//get status of current language environment
311327
$default_lang = pll_default_language();
312328
$default_locale = pll_default_language( 'locale' );
313-
$start_locale = get_locale();
314-
if ( $default_locale != $start_locale ) {
329+
$start_locale = ( is_admin() ) ? get_user_locale() : get_locale();
330+
//important: in admin mode and 'Show all language' there is no polylang current language
331+
$pll_start_locale = pll_current_language( 'locale' );
332+
333+
/*
334+
* important, we must be in the base language before doing the check
335+
* because otherwise the posts will be pll filtered into other language
336+
* and appear to be missing if not translated
337+
*/
338+
if ( $pll_start_locale ) {
339+
if ( $default_locale != $pll_start_locale ) {
340+
Utilities::switchLocale( $default_locale );
341+
}
342+
} elseif ( $default_locale != $start_locale ) {
315343
Utilities::switchLocale( $default_locale );
316344
}
317345

318-
//get the current id of each woocommerce page
346+
/*
347+
* get the current id of each woocommerce page in the base language
348+
* using the native woocommerce function to fill any missing page
349+
*/
319350
foreach ( $page_types as $page_type ) {
320351
$pageid = wc_get_page_id( $page_type );
321352
if ( $pageid == -1 || ! get_post( $pageid ) ) {
322-
//if any of the pages is missing, rerun the woocommerce page creation
323-
//which will just fill in any missing page
324-
\WC_Install::create_pages();
325-
$pageid = wc_get_page_id( $page_type );
353+
if ( $create_pages ) {
354+
//if any of the pages is missing, rerun the woocommerce page creation
355+
//which will just fill in any missing page
356+
\WC_Install::create_pages();
357+
$pageid = wc_get_page_id( $page_type );
358+
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
359+
__( '%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 );
360+
} else {
361+
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
362+
__( '%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() );
363+
$failure = true;
364+
}
326365
}
327366
$pages[ $page_type ] = $pageid;
328367
}
@@ -334,16 +373,20 @@ public static function wpi_ensure_woocommerce_pages_translated() {
334373

335374
//for each page, check all the translations and fill in and link where necessary
336375
foreach ( $pages as $page_type => $orig_page_id ) {
376+
$changed = false;
337377
$orig_page = get_post( $orig_page_id );
338378
if ( $orig_page ) {
339379
$orig_postlocale = pll_get_post_language( $orig_page_id, 'locale' );
380+
$orig_postlang = pll_get_post_language( $orig_page_id, 'slug' );
340381
//default pages may not have language set correctly
341-
if ( ! $orig_postlocale || ($orig_postlocale != $default_locale) ) {
382+
if ( ! $orig_postlocale ) {
342383
$orig_postlocale = $default_locale;
343-
pll_set_post_language( $orig_page_id, $default_lang );
384+
$orig_postlang = $default_lang;
385+
pll_set_post_language( $orig_page_id, $orig_postlang );
386+
$warnings[ $page_type . '::' . $default_locale ] = sprintf(
387+
__( '%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 );
344388
}
345-
$translations[ $default_lang ] = $orig_page_id;
346-
$changed = false;
389+
$translations[ $orig_postlang ] = $orig_page_id;
347390
foreach ( $langs as $langId => $langLocale ) {
348391
$translation_id = $orig_page_id;
349392
$langSlug = $lang_slugs[ $langId ];
@@ -356,67 +399,92 @@ public static function wpi_ensure_woocommerce_pages_translated() {
356399
// and there is no translation in target language
357400
$translation_id = pll_get_post( $orig_page_id, $langLocale );
358401
if ( $translation_id == 0 || $translation_id == $orig_page_id ) {
359-
360-
//then create new post in target language
361-
$isNewPost = true;
362-
Utilities::switchLocale( $langLocale );
363-
364-
//default to copy source page
365-
$post_name = $orig_page->post_name;
366-
$post_title = $orig_page->post_title;
367-
$post_content = $orig_page->post_content;
368-
369-
//ideally, get correct translation
370-
switch ( $page_type ) {
371-
case 'shop':
372-
$post_name = _x( 'shop', 'Page slug', 'woocommerce' );
373-
$post_title = _x( 'Shop', 'Page title', 'woocommerce' );
374-
$post_content = '';
375-
break;
376-
case 'cart':
377-
$post_name = _x( 'cart', 'Page slug', 'woocommerce' );
378-
$post_title = _x( 'Cart', 'Page title', 'woocommerce' );
379-
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']<!-- /wp:shortcode -->';
380-
break;
381-
case 'checkout':
382-
$post_name = _x( 'checkout', 'Page slug', 'woocommerce' );
383-
$post_title = _x( 'Checkout', 'Page title', 'woocommerce' );
384-
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']<!-- /wp:shortcode -->';
385-
break;
386-
case 'myaccount':
387-
$post_name = _x( 'my-account', 'Page slug', 'woocommerce' );
388-
$post_title = _x( 'My account', 'Page title', 'woocommerce' );
389-
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->';
390-
break;
402+
if ( $create_pages ) {
403+
//then create new post in target language
404+
$isNewPost = true;
405+
Utilities::switchLocale( $langLocale );
406+
407+
//default to copy source page
408+
$post_name = $orig_page->post_name;
409+
$post_title = $orig_page->post_title;
410+
$post_content = $orig_page->post_content;
411+
412+
//ideally, get correct translation
413+
switch ( $page_type ) {
414+
case 'shop':
415+
$post_name = _x( 'shop', 'Page slug', 'woocommerce' );
416+
$post_title = _x( 'Shop', 'Page title', 'woocommerce' );
417+
$post_content = '';
418+
break;
419+
case 'cart':
420+
$post_name = _x( 'cart', 'Page slug', 'woocommerce' );
421+
$post_title = _x( 'Cart', 'Page title', 'woocommerce' );
422+
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']<!-- /wp:shortcode -->';
423+
break;
424+
case 'checkout':
425+
$post_name = _x( 'checkout', 'Page slug', 'woocommerce' );
426+
$post_title = _x( 'Checkout', 'Page title', 'woocommerce' );
427+
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']<!-- /wp:shortcode -->';
428+
break;
429+
case 'myaccount':
430+
$post_name = _x( 'my-account', 'Page slug', 'woocommerce' );
431+
$post_title = _x( 'My account', 'Page title', 'woocommerce' );
432+
$post_content = '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->';
433+
break;
434+
}
435+
436+
437+
$page_data = array(
438+
'post_status' => 'publish',
439+
'post_type' => 'page',
440+
'post_author' => 1,
441+
'post_name' => $post_name,
442+
'post_title' => $post_title,
443+
'post_content' => $post_content,
444+
//'post_parent' => $post_parent,
445+
'comment_status' => 'closed',
446+
);
447+
$translation_id = wp_insert_post( $page_data );
448+
}
449+
//if there now is a translation is where there was not before, creation must have been successful
450+
if ( $translation_id ) {
451+
pll_set_post_language( $translation_id, $langSlug );
452+
$changed = true;
453+
$warnings[ $page_type . '::' . $langLocale ] = sprintf(
454+
__( '%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 );
455+
} else {
456+
$warnings[ $page_type . '::' . $langLocale ] = sprintf(
457+
__( '%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() );
458+
$failure = true;
391459
}
392-
393-
394-
$page_data = array(
395-
'post_status' => 'publish',
396-
'post_type' => 'page',
397-
'post_author' => 1,
398-
'post_name' => $post_name,
399-
'post_title' => $post_title,
400-
'post_content' => $post_content,
401-
//'post_parent' => $post_parent,
402-
'comment_status' => 'closed',
403-
);
404-
$translation_id = wp_insert_post( $page_data );
405-
406-
pll_set_post_language( $translation_id, $langSlug );
407-
$changed = true;
408460
}
409461
//always add the existing translations back into the translations array
410-
$translations [ $langSlug ] = $translation_id;
462+
if ( $translation_id ) {
463+
$translations [ $langSlug ] = $translation_id;
464+
}
411465
}
412466
//if this woocommerce page is an existing post, check post status
413467
if ( $translation_id && ! $isNewPost ) {
414468
$thisPost = get_post( $translation_id );
415469
if ( $thisPost ) {
416470
$postStatus = $thisPost->post_status;
417471
if ( $postStatus != 'publish' ) {
472+
$baseURL = is_multisite() ? get_admin_url() : admin_url();
473+
if ( $postStatus == 'trash' ) {
474+
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
475+
__( '%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 );
476+
} else {
477+
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
478+
__( '%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 );
479+
}
480+
$failure = true;
481+
}
482+
} else {
418483
$warnings[ $page_type . '::' . $langSlug ] = sprintf(
419-
__( '%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 );
484+
__( '%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 );
485+
unset( $translations[ $langSlug ] );
486+
$failure = true;
487+
$changed = true;
420488
}
421489
}
422490
}
@@ -425,21 +493,33 @@ public static function wpi_ensure_woocommerce_pages_translated() {
425493
pll_save_post_translations( $translations );
426494
}
427495
}
428-
}
429496

497+
/*
498+
* update result of page checks
499+
*/
430500
if ( $warnings ) {
431501
FlashMessages::add(
432502
'pagechecks', implode( '<br/>', $warnings )
433503
, array( 'updated' ), true
434504
);
435-
update_option( 'wpi_wcpagecheck_passed', false );
436505
} else {
437506
FlashMessages::remove( 'pagechecks' );
438-
update_option( 'wpi_wcpagecheck_passed', true );
439507
}
508+
if ( $failure ) {
509+
update_option( 'wpi_wcpagecheck_passed', '0' );
510+
} else {
511+
update_option( 'wpi_wcpagecheck_passed', '1' );
512+
}
513+
514+
/*
515+
* check current locale and reset it if changed
516+
*/
440517
$locale = get_locale();
441518
if ( $locale != $start_locale ) {
442-
Utilities::switchLocale( $start_locale );
519+
Utilities::switch_wp_locale( $start_locale );
520+
}
521+
if ( $locale != $pll_start_locale ) {
522+
Utilities::switch_pll_locale( $pll_start_locale );
443523
}
444524
}
445525

0 commit comments

Comments
 (0)