Skip to content

Commit afe0472

Browse files
authored
Polyfill json_validate that is not defined in PHP <8.3 (#37)
* Polyfill json_validate()
1 parent 85b4572 commit afe0472

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

classes/class-plugin-install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static function do_directory_request( $args = array(), $type = 'plugins'
156156
if ( $e !== 200 ) {
157157
$result['error'] = $response['response']['message'] . '.';
158158
$result['code'] = $response['response']['code'];
159-
if ( ! isset( $response['body'] ) || ! json_validate( $response['body'] ) ) {
159+
if ( ! isset( $response['body'] ) || ! self::json_validate( $response['body'] ) ) {
160160
return $result;
161161
}
162162
$api_message = json_decode( $response['body'], true );

classes/class-theme-install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static function do_directory_request( $args = array(), $type = 'themes' )
149149
if ( $e !== 200 ) {
150150
$result['error'] = $response['response']['message'] . '.';
151151
$result['code'] = $response['response']['code'];
152-
if ( ! isset( $response['body'] ) || ! json_validate( $response['body'] ) ) {
152+
if ( ! isset( $response['body'] ) || ! self::json_validate( $response['body'] ) ) {
153153
return $result;
154154
}
155155
$api_message = json_decode( $response['body'], true );

classes/trait-helpers.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
trait Helpers {
66

7-
87
/**
98
* Get all substrings within text that are found between two other, specified strings
109
*
@@ -32,4 +31,16 @@ private function get_markdown_contents( $str, $startDelimiter, $endDelimiter ) {
3231

3332
return $contents;
3433
}
34+
35+
/**
36+
* Polyfill for json_validate
37+
* The function is defined only in PHP 8 >= 8.3.0
38+
*/
39+
private static function json_validate( $json ) {
40+
if ( function_exists( 'json_validate' ) ) {
41+
return json_validate( $json );
42+
}
43+
return json_decode( $json ) !== null;
44+
}
45+
3546
}

classicpress-directory-integration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* -----------------------------------------------------------------------------
55
* Plugin Name: ClassicPress Directory Integration
66
* Description: Install and update plugins and themes from ClassicPress directory.
7-
* Version: 1.1.3
7+
* Version: 1.1.4
88
* Author: ClassicPress Contributors
99
* Author URI: https://www.classicpress.net
1010
* Plugin URI: https://www.classicpress.net

0 commit comments

Comments
 (0)