Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 7e778e8

Browse files
author
Soft79
committed
Minor performance improvement
1 parent 39e28a8 commit 7e778e8

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

includes/wpm-formatting-functions.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ function wpm_strip_protocol( $url ) {
187187
*/
188188
if ( ! function_exists( 'isJSON' ) ) {
189189
function isJSON( $string ) {
190-
return is_string( $string ) && is_array( json_decode( $string, true ) ) && ( json_last_error() == JSON_ERROR_NONE ) ? true : false;
190+
return is_string( $string )
191+
&& strlen( $string ) >= 2
192+
&& $string[0] === '{'
193+
&& is_array( json_decode( $string, true ) );
191194
}
192195
}

includes/wpm-translation-functions.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,16 @@ function wpm_translate_value( $value, $language = '' ) {
154154
* @return array|mixed|string
155155
*/
156156
function wpm_string_to_ml_array( $string ) {
157-
if ( ! is_string( $string ) || $string === '' || is_serialized_string( $string ) || isJSON( $string ) ) {
157+
if ( ! is_string( $string ) || strlen( $string ) === 0 || is_serialized_string( $string ) || isJSON( $string ) ) {
158158
return $string;
159159
}
160160

161+
//Improves speed
162+
if ( ! str_contains( $string, '[:' ) ) return $string;
163+
161164
$string = htmlspecialchars_decode( $string );
162165
$blocks = preg_split( '#\[:([a-z-]*)\]#im', $string, - 1, PREG_SPLIT_DELIM_CAPTURE );
163166

164-
if ( empty( $blocks ) ) {
165-
return $string;
166-
}
167-
168167
$languages = wpm_get_lang_option();
169168
$result = array_fill_keys( array_keys( $languages ), '' );
170169
$language = count( $blocks ) === 1 ? wpm_get_default_language() : '';

0 commit comments

Comments
 (0)