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

Commit 7d42820

Browse files
committed
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
1 parent feedc68 commit 7d42820

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

src/Hyyan/WPI/Emails.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@ public function translateCommonString( $email_string ) {
267267
return $email_string;
268268
}
269269
if ( is_admin() ) {
270-
$screen = get_current_screen();
271-
if ( $screen && $screen->id == 'shop_order' ) {
272-
global $post;
273-
if ( $post ) {
274-
$order_locale = pll_get_post_language( $post->ID );
275-
return pll_translate_string( $email_string, $order_locale );
276-
}
277-
}
270+
//#435 allow the possibility of other screens sending email, including where current_screen is not defined
271+
//$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
272+
//if ( $screen && $screen->id == 'shop_order' ) {
273+
global $post;
274+
if ( $post ) {
275+
$locale = pll_get_post_language( $post->ID );
276+
return pll_translate_string( $email_string, $locale );
277+
}
278+
//}
278279
}
279280
$trans = pll__( $email_string );
280281
if ( $trans ) {

src/Hyyan/WPI/Endpoints.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ public function fixMyAccountLinkInMenus(array $items = array())
250250
*/
251251
public function showFlashMessages()
252252
{
253-
$screen = get_current_screen();
253+
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
254254
/*
255255
* this only gets shown once before being dismissed so show only in the relevant place
256256
*/
257-
if ( $screen->id == 'woocommerce_page_wc-settings' && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'advanced' ) {
257+
if ( $screen && $screen->id == 'woocommerce_page_wc-settings' && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] == 'advanced' ) {
258258
FlashMessages::add(
259259
MessagesInterface::MSG_ENDPOINTS_TRANSLATION, Plugin::getView( 'Messages/endpointsTranslations' )
260260
);

src/Hyyan/WPI/Order.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ public function correctGetOrderQuery($query, $args)
142142
public function limitPolylangFeaturesForOrders()
143143
{
144144
add_action('current_screen', function () {
145-
$screen = get_current_screen();
145+
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
146146

147-
if ($screen->post_type === 'shop_order') {
147+
if ( $screen && $screen->post_type === 'shop_order' ) {
148148
add_action('admin_print_scripts', function () {
149149
$jsID = 'order-translations-buttons';
150150
$code = '$(".pll_icon_add,#post-translations").fadeOut()';

src/Hyyan/WPI/Product/Meta.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ public function saveQuickEdit(\WC_Product $product)
133133
public function syncProductsMeta()
134134
{
135135
//change proposed Teemu Suoranta 3/Nov
136-
$currentScreen = get_current_screen();
137-
if ($currentScreen->post_type !== 'product') {
136+
$currentScreen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
137+
if ( $currentScreen && $currentScreen->post_type !== 'product' ) {
138138
return false;
139139
}
140140

src/Hyyan/WPI/Product/Variable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ public function handleVariableLimitation()
384384
public function shouldDisableLangSwitcher()
385385
{
386386
add_action('current_screen', function () {
387-
$screen = get_current_screen();
388-
if ($screen->id !== 'settings_page_mlang') {
387+
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
388+
if ($screen && $screen->id !== 'settings_page_mlang') {
389389
return false;
390390
}
391391

src/Hyyan/WPI/Shipping.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function __construct()
4848
*/
4949
public function disableSettings()
5050
{
51-
$currentScreen = get_current_screen();
52-
if ($currentScreen->id !== 'settings_page_hyyan-wpi') {
51+
$currentScreen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
52+
if ($currentScreen && $currentScreen->id !== 'settings_page_hyyan-wpi') {
5353
return false;
5454
}
5555

src/Hyyan/WPI/Tools/FlashMessages.php

-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,5 @@ private static function getMessages()
137137
$messages = array();
138138
}
139139
return $messages;
140-
return get_option(static::getOptionName(), array());
141140
}
142141
}

0 commit comments

Comments
 (0)