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

Commit b48f7d6

Browse files
committed
Merge branch 'release/2.2.2'
2 parents 12d57bb + a0c1c19 commit b48f7d6

14 files changed

+98
-28
lines changed

includes/admin/class-wpm-admin-assets.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ public function admin_scripts() {
107107

108108
$admin_html_tags = apply_filters( 'wpm_admin_html_tags', $config['admin_html_tags'] );
109109

110-
if ( isset( $admin_html_tags[ $screen_id ] ) && null !== $admin_html_tags[ $screen_id ] ) {
110+
if ( ! empty( $admin_html_tags[ $screen_id ] ) ) {
111111
wp_enqueue_script( 'wpm_translator' );
112112
$js_code = '';
113-
foreach ( $admin_html_tags[ $screen_id ] as $attr => $selector ) {
113+
foreach ( ( array ) $admin_html_tags[ $screen_id ] as $attr => $selector ) {
114114
$js_code .= '$( "' . implode( ', ', $selector ) . '" ).each( function () {';
115115
if ( 'text' === $attr ) {
116116
$js_code .= '$(this).text(wpm_translator.translate_string($(this).text()));';

includes/class-wp-multilang.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class WP_Multilang {
2121
*
2222
* @var string
2323
*/
24-
public $version = '2.2.1';
24+
public $version = '2.2.2';
2525

2626
/**
2727
* The single instance of the class.
@@ -178,6 +178,7 @@ public function init() {
178178
new WPM_Widgets();
179179
new WPM_Users();
180180
new WPM_Comments();
181+
new WPM_Shortcodes();
181182

182183
if ( $this->is_request( 'admin' ) ) {
183184
new Admin\WPM_Admin();

includes/class-wpm-ajax.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static function set_default_language_for_object( $object, $object_config
222222
break;
223223
}
224224

225-
if ( json_decode( $content ) ) {
225+
if ( isJSON( $content ) ) {
226226
$content = json_decode( $content, true );
227227
$object->$key = wp_json_encode( wpm_set_new_value( $content, wpm_translate_value( $content, $lang ), $object_config[ $key ], $lang ) );
228228
break;

includes/class-wpm-config.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ public static function load_theme_config() {
106106
*/
107107
public static function parse_config_files() {
108108

109-
foreach ( self::$config_files as $file ) {
109+
$config_files = apply_filters( 'wpm_json_files', self::$config_files );
110+
111+
foreach ( $config_files as $name => $file ) {
112+
113+
$file = apply_filters( "wpm_{$name}_json_file", $file );
114+
110115
if ( $file && is_readable( $file ) ) {
111116
$config = json_decode( file_get_contents( $file ), true );
112117

includes/class-wpm-setup.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,11 @@ public function load_integrations() {
546546

547547
$active_plugins = wp_cache_get( 'active_plugins', 'wpm' );
548548

549-
if ( is_array( $active_plugins) ) {
549+
if ( is_array( $active_plugins ) ) {
550550
foreach ( $active_plugins as $plugin ) {
551-
if ( isset( $integrations[ $plugin ] ) && ! empty( $integrations[ $plugin ] ) ) {
552-
new $integrations[ $plugin ]();
551+
if ( ! empty( $integrations[ $plugin ] ) ) {
552+
$integration = apply_filters( "wpm_{$plugin}_integration", $integrations[ $plugin ] );
553+
new $integration();
553554
}
554555
}
555556
}

includes/class-wpm-shortcodes.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace WPM\Includes;
3+
4+
if ( ! defined( 'ABSPATH' ) ) {
5+
exit; // Exit if accessed directly
6+
}
7+
8+
/**
9+
* ShortCodes
10+
*
11+
* Class WPM_Shortcodes
12+
* @package WPM\Includes
13+
*/
14+
class WPM_Shortcodes {
15+
16+
/**
17+
* WPM_Shortcodes constructor.
18+
*/
19+
public function __construct() {
20+
add_shortcode( 'wpm_lang_switcher', array( $this, 'language_switcher' ) );
21+
}
22+
23+
/**
24+
* Language switcher html
25+
*
26+
* @param $atts
27+
*
28+
* @return string
29+
*/
30+
public function language_switcher( $atts ) {
31+
32+
$atts = shortcode_atts( array(
33+
'type' => 'list',
34+
'show' => 'both'
35+
), $atts );
36+
37+
return wpm_get_language_switcher( $atts['type'], $atts['show'] );
38+
}
39+
}

includes/integrations/class-wpm-gutenberg.php

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function add_language_switcher() {
4242

4343
add_action( 'admin_print_footer_scripts', 'wpm_admin_language_switcher_customizer' );
4444
wpm_enqueue_js( "
45+
if (wp.api === undefined) {
46+
return;
47+
}
48+
4549
wp.api.init().then( function() {
4650
if ($('#wpm-language-switcher').length === 0) {
4751
var language_switcher = wp.template( 'wpm-ls-customizer' );

includes/integrations/class-wpm-pbso.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function translate_value( $meta_value ) {
4343
foreach ( $meta_value['widgets'] as $key => $widget ) {
4444
if ( isset( $widget['frames'] ) ) {
4545
foreach ( $widget['frames'] as $_key => $frame ) {
46-
if ( isset( $frame['content'] ) && is_string( $frame['content'] ) && json_decode( $frame['content'] ) ) {
46+
if ( ! empty( $frame['content'] ) && isJSON( $frame['content'] ) ) {
4747
$frame['content'] = json_decode( $frame['content'], true );
4848
if ( is_array( $frame['content'] ) && isset( $frame['content']['widgets'] ) ) {
4949
$meta_value['widgets'][ $key ]['frames'][ $_key ]['content'] = $this->translate_value( $frame['content'] );
@@ -68,7 +68,7 @@ public function transform_value( $meta_value ) {
6868
foreach ( $meta_value['widgets'] as $key => $widget ) {
6969
if ( isset( $widget['frames'] ) ) {
7070
foreach ( $widget['frames'] as $_key => $frame ) {
71-
if ( isset( $frame['content'] ) && json_decode( $frame['content'] ) ) {
71+
if ( ! empty( $frame['content'] ) && isJSON( $frame['content'] ) ) {
7272
$frame['content'] = json_decode( $frame['content'], true );
7373
if ( is_array( $frame['content'] ) && isset( $frame['content']['widgets'] ) ) {
7474
$meta_value['widgets'][ $key ]['frames'][ $_key ]['content'] = $this->transform_value( $frame['content'] );
@@ -98,9 +98,9 @@ public function filter_old_value( $old_value, $meta_value ) {
9898
foreach ( $old_value['widgets'] as $_widget ) {
9999
if ( $widget['panels_info']['widget_id'] === $_widget['panels_info']['widget_id'] ) {
100100
$new_old_value['widgets'][ $key ] = $_widget;
101-
if ( isset( $widget['frames'] ) ) {
101+
if ( ! empty( $widget['frames'] ) ) {
102102
foreach ( $widget['frames'] as $_key => $frame ) {
103-
if ( is_array( $frame['content'] ) && isset( $frame['content'], $frame['content']['widgets'] ) ) {
103+
if ( is_array( $frame['content'] ) && isset( $frame['content']['widgets'] ) ) {
104104
foreach ( $_widget['frames'] as $_frame ) {
105105
$_frame['content'] = json_decode( $_frame['content'], true );
106106
$new_old_value['widgets'][ $key ]['frames'][ $_key ]['content'] = $this->filter_old_value( $_frame['content'], $frame['content'] );
@@ -151,7 +151,7 @@ public function add_recursive_config( $config, $meta_value ) {
151151
foreach ( $meta_value['widgets'] as $widget ) {
152152
if ( isset( $widget['frames'] ) ) {
153153
foreach ( $widget['frames'] as $frame ) {
154-
if ( isset( $frame['content'] ) && is_string( $frame['content'] ) && json_decode( $frame['content'] ) ) {
154+
if ( ! empty( $frame['content'] ) && isJSON( $frame['content'] ) ) {
155155
$frame['content'] = json_decode( $frame['content'], true );
156156
if ( isset( $frame['content']['widgets'] ) ) {
157157
$config['widgets']['wpm_each']['frames']['wpm_each']['content'] = $this->add_recursive_config( $config, $frame['content'] );

includes/wpm-core-functions.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ function wpm_attribute_escape( $string ) {
240240
if ( 'GET' === $_SERVER['REQUEST_METHOD'] ) {
241241
$string = wp_specialchars_decode( $string, ENT_QUOTES );
242242

243-
if ( $array = json_decode( $string, true ) ) {
243+
if ( isJSON( $string ) ) {
244+
$array = json_decode( $string, true );
244245
$array = wpm_translate_value( $array );
245246
$string = wp_json_encode( $array );
246247
} else {

includes/wpm-formatting-functions.php

+9
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,12 @@ function wpm_strip_protocol( $url ) {
181181
// strip the protical
182182
return preg_replace( '#^https?://#i', '', $url );
183183
}
184+
185+
/**
186+
* Check if is JSON string
187+
*/
188+
if ( ! function_exists( 'isJSON' ) ) {
189+
function isJSON( $string ) {
190+
return is_string( $string ) && is_array( json_decode( $string, true ) ) && ( json_last_error() == JSON_ERROR_NONE ) ? true : false;
191+
}
192+
}

includes/wpm-translation-functions.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function wpm_translate_value( $value, $language = '' ) {
155155
*/
156156
function wpm_string_to_ml_array( $string ) {
157157

158-
if ( ! is_string( $string ) || is_serialized_string( $string ) || json_decode( $string ) ) {
158+
if ( ! is_string( $string ) || is_serialized_string( $string ) || isJSON( $string ) ) {
159159
return $string;
160160
}
161161

@@ -337,7 +337,7 @@ function wpm_set_language_value( $localize_array, $value, $config = array(), $la
337337
$new_value = $localize_array;
338338
$new_value[ $lang ] = $value;
339339
} else {
340-
if ( json_decode( $value ) || is_serialized_string( $value ) ) {
340+
if ( isJSON( $value ) || is_serialized_string( $value ) ) {
341341
$new_value = $value;
342342
} else {
343343
$result = array();
@@ -383,7 +383,7 @@ function wpm_translate_object( $object, $lang = '' ) {
383383
break;
384384
}
385385

386-
if ( json_decode( $content ) ) {
386+
if ( isJSON( $content ) ) {
387387
$object->$key = wp_json_encode( wpm_translate_value( json_decode( $content, true ), $lang ) );
388388
break;
389389
}
@@ -505,11 +505,11 @@ function wpm_is_ml_array( $array ) {
505505
*/
506506
function wpm_is_ml_string( $string ) {
507507

508-
if ( ! is_string( $string ) || is_serialized_string( $string ) || json_decode( $string ) ) {
508+
if ( ! is_string( $string ) || is_serialized_string( $string ) || isJSON( $string ) ) {
509509
return false;
510510
}
511511

512-
return preg_match( '#^(<!--:[a-z-]+-->|\[:[a-z-]+\]|\{:[a-z-]+\}).+#ism', $string );
512+
return preg_match( '#(<!--:[a-z-]+-->|\[:[a-z-]+\]|\{:[a-z-]+\}).?#ism', $string );
513513
}
514514

515515
/**
@@ -553,7 +553,7 @@ function wpm_set_new_value( $old_value, $new_value, $config = array(), $lang = '
553553
return $new_value;
554554
}
555555

556-
if ( is_serialized_string( $old_value ) || ( is_string( $old_value ) && json_decode( $old_value ) ) ) {
556+
if ( is_serialized_string( $old_value ) || isJSON( $old_value ) ) {
557557
return $old_value;
558558
}
559559

readme.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Contributors: valexar
44
Tags: localization, multilanguage, multilingual, translation, multilang
55
Requires at least: 4.7
66
Tested up to: 4.9
7-
Stable tag: 2.2.1
7+
Stable tag: 2.2.2
88
Requires PHP: 5.6+
99
License: GPLv3
1010
License URI: https://www.gnu.org/licenses/gpl-3.0.html
@@ -110,6 +110,8 @@ Function accepts two parameters:
110110
$type - 'list', 'dropdown', 'select'. Default - 'list'.
111111
$show - 'flag', 'name', 'both'. Default - 'both'.
112112

113+
Or using the shortcode `wpm_lang_switcher`. It accept two not necessary parameters 'type' and 'show'.
114+
113115
Available features for translation:
114116

115117
`wpm_translate_url ($url, $language = '');` - translate url
@@ -200,6 +202,14 @@ If you have opened several browser tabs for editing this post in different langu
200202

201203
== Changelog ==
202204

205+
= 2.2.2 =
206+
- added shortcode for display language switcher
207+
- add filters for set json config files for integration
208+
- fixed checking for ml strings
209+
- fixed compatibility with Gutenberg
210+
- fixed checking for JSON strings
211+
- fixed regex for ml strings
212+
203213
= 2.2.1 =
204214
- fixed escaping text. Have error on some sites.
205215
- fixed ACF PRO menu integration. Have error on ACFv4.

uninstall.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
$meta_value = serialize( wpm_translate_value( unserialize( $meta_value ), $default_language ) );
9494
}
9595

96-
if ( json_decode( $meta_value ) ) {
96+
if ( isJSON( $meta_value ) ) {
9797
$meta_value = wp_json_encode( wpm_translate_value( json_decode( $meta_value, true ), $default_language ) );
9898
}
9999

@@ -118,7 +118,7 @@
118118
$meta_value = serialize( wpm_translate_value( unserialize( $meta_value ), $default_language ) );
119119
}
120120

121-
if ( json_decode( $meta_value ) ) {
121+
if ( isJSON( $meta_value ) ) {
122122
$meta_value = wp_json_encode( wpm_translate_value( json_decode( $meta_value, true ), $default_language ) );
123123
}
124124

@@ -143,7 +143,7 @@
143143
$meta_value = serialize( wpm_translate_value( unserialize( $meta_value ), $default_language ) );
144144
}
145145

146-
if ( json_decode( $meta_value ) ) {
146+
if ( isJSON( $meta_value ) ) {
147147
$meta_value = wp_json_encode( wpm_translate_value( json_decode( $meta_value, true ), $default_language ) );
148148
}
149149

@@ -167,7 +167,7 @@
167167
$meta_value = serialize( wpm_translate_value( unserialize( $meta_value ), $default_language ) );
168168
}
169169

170-
if ( json_decode( $meta_value ) ) {
170+
if ( isJSON( $meta_value ) ) {
171171
$meta_value = wp_json_encode( wpm_translate_value( json_decode( $meta_value, true ), $default_language ) );
172172
}
173173

@@ -191,7 +191,7 @@
191191
$option_value = serialize( wpm_translate_value( unserialize( $option_value ), $default_language ) );
192192
}
193193

194-
if ( json_decode( $option_value ) ) {
194+
if ( isJSON( $option_value ) ) {
195195
$option_value = wp_json_encode( wpm_translate_value( json_decode( $option_value, true ), $default_language ) );
196196
}
197197

@@ -215,7 +215,7 @@
215215
$meta_value = serialize( wpm_translate_value( unserialize( $meta_value ), $default_language ) );
216216
}
217217

218-
if ( json_decode( $meta_value ) ) {
218+
if ( isJSON( $meta_value ) ) {
219219
$meta_value = wp_json_encode( wpm_translate_value( json_decode( $meta_value, true ), $default_language ) );
220220
}
221221

wp-multilang.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010
* Text Domain: wp-multilang
1111
* Domain Path: /languages
12-
* Version: 2.2.1
12+
* Version: 2.2.2
1313
* Copyright: © 2017-2018 Valentyn Riaboshtan
1414
*
1515
* @package WPM

0 commit comments

Comments
 (0)