From 7d428208ff3b3a2472ce42f5a2259604ce2128c8 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Sat, 29 Jun 2019 09:44:15 +0100 Subject: [PATCH] fixes #435 get_current_screen() not available in some admin screens 1. added defensive check to each occurrence of get_current_screen 2. removed get_current_screen test from common email string translation to allow common strings to be translated when email sent from other screens --- src/Hyyan/WPI/Emails.php | 17 +++++++++-------- src/Hyyan/WPI/Endpoints.php | 4 ++-- src/Hyyan/WPI/Order.php | 4 ++-- src/Hyyan/WPI/Product/Meta.php | 4 ++-- src/Hyyan/WPI/Product/Variable.php | 4 ++-- src/Hyyan/WPI/Shipping.php | 4 ++-- src/Hyyan/WPI/Tools/FlashMessages.php | 1 - 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Hyyan/WPI/Emails.php b/src/Hyyan/WPI/Emails.php index 817ce3e..b4c5e4c 100644 --- a/src/Hyyan/WPI/Emails.php +++ b/src/Hyyan/WPI/Emails.php @@ -267,14 +267,15 @@ public function translateCommonString( $email_string ) { return $email_string; } if ( is_admin() ) { - $screen = get_current_screen(); - if ( $screen && $screen->id == 'shop_order' ) { - global $post; - if ( $post ) { - $order_locale = pll_get_post_language( $post->ID ); - return pll_translate_string( $email_string, $order_locale ); - } - } + //#435 allow the possibility of other screens sending email, including where current_screen is not defined + //$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; + //if ( $screen && $screen->id == 'shop_order' ) { + global $post; + if ( $post ) { + $locale = pll_get_post_language( $post->ID ); + return pll_translate_string( $email_string, $locale ); + } + //} } $trans = pll__( $email_string ); if ( $trans ) { diff --git a/src/Hyyan/WPI/Endpoints.php b/src/Hyyan/WPI/Endpoints.php index 7580541..5231a64 100644 --- a/src/Hyyan/WPI/Endpoints.php +++ b/src/Hyyan/WPI/Endpoints.php @@ -250,11 +250,11 @@ public function fixMyAccountLinkInMenus(array $items = array()) */ public function showFlashMessages() { - $screen = get_current_screen(); + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; /* * this only gets shown once before being dismissed so show only in the relevant place */ - if ( $screen->id == 'woocommerce_page_wc-settings' && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'advanced' ) { + if ( $screen && $screen->id == 'woocommerce_page_wc-settings' && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'advanced' ) { FlashMessages::add( MessagesInterface::MSG_ENDPOINTS_TRANSLATION, Plugin::getView( 'Messages/endpointsTranslations' ) ); diff --git a/src/Hyyan/WPI/Order.php b/src/Hyyan/WPI/Order.php index 9b8dd18..83ea354 100644 --- a/src/Hyyan/WPI/Order.php +++ b/src/Hyyan/WPI/Order.php @@ -142,9 +142,9 @@ public function correctGetOrderQuery($query, $args) public function limitPolylangFeaturesForOrders() { add_action('current_screen', function () { - $screen = get_current_screen(); + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; - if ($screen->post_type === 'shop_order') { + if ( $screen && $screen->post_type === 'shop_order' ) { add_action('admin_print_scripts', function () { $jsID = 'order-translations-buttons'; $code = '$(".pll_icon_add,#post-translations").fadeOut()'; diff --git a/src/Hyyan/WPI/Product/Meta.php b/src/Hyyan/WPI/Product/Meta.php index dfdaa7a..aa5258e 100644 --- a/src/Hyyan/WPI/Product/Meta.php +++ b/src/Hyyan/WPI/Product/Meta.php @@ -133,8 +133,8 @@ public function saveQuickEdit(\WC_Product $product) public function syncProductsMeta() { //change proposed Teemu Suoranta 3/Nov - $currentScreen = get_current_screen(); - if ($currentScreen->post_type !== 'product') { + $currentScreen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; + if ( $currentScreen && $currentScreen->post_type !== 'product' ) { return false; } diff --git a/src/Hyyan/WPI/Product/Variable.php b/src/Hyyan/WPI/Product/Variable.php index 0b200cc..e157fa7 100644 --- a/src/Hyyan/WPI/Product/Variable.php +++ b/src/Hyyan/WPI/Product/Variable.php @@ -384,8 +384,8 @@ public function handleVariableLimitation() public function shouldDisableLangSwitcher() { add_action('current_screen', function () { - $screen = get_current_screen(); - if ($screen->id !== 'settings_page_mlang') { + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; + if ($screen && $screen->id !== 'settings_page_mlang') { return false; } diff --git a/src/Hyyan/WPI/Shipping.php b/src/Hyyan/WPI/Shipping.php index 07ff780..dcb2933 100644 --- a/src/Hyyan/WPI/Shipping.php +++ b/src/Hyyan/WPI/Shipping.php @@ -48,8 +48,8 @@ public function __construct() */ public function disableSettings() { - $currentScreen = get_current_screen(); - if ($currentScreen->id !== 'settings_page_hyyan-wpi') { + $currentScreen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; + if ($currentScreen && $currentScreen->id !== 'settings_page_hyyan-wpi') { return false; } diff --git a/src/Hyyan/WPI/Tools/FlashMessages.php b/src/Hyyan/WPI/Tools/FlashMessages.php index 830e15b..15dc722 100644 --- a/src/Hyyan/WPI/Tools/FlashMessages.php +++ b/src/Hyyan/WPI/Tools/FlashMessages.php @@ -137,6 +137,5 @@ private static function getMessages() $messages = array(); } return $messages; - return get_option(static::getOptionName(), array()); } }