From 86a32d3dfc9b3bb0f514a7bce02350005fe835e4 Mon Sep 17 00:00:00 2001 From: Alex S <17275120+AlexGStapleton@users.noreply.github.com> Date: Wed, 11 Sep 2024 00:58:56 +1200 Subject: [PATCH 1/4] Prebuilt Local Layouts: Resolve Fatal `PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string in inc\admin-layouts.php:131` --- inc/admin-layouts.php | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/inc/admin-layouts.php b/inc/admin-layouts.php index 246d6a10..3db056ec 100644 --- a/inc/admin-layouts.php +++ b/inc/admin-layouts.php @@ -120,28 +120,29 @@ public function get_local_layouts() { continue; } - // json decode - $panels_data = json_decode( $file_contents, true ); + $panels_data = $this->decode_panels_data( $file_contents ); - if ( ! empty( $panels_data ) ) { - // get file name by stripping out folder path and .json extension - $file_name = str_replace( array( $folder . '/', '.json' ), '', $file ); + if ( empty( $panels_data ) ) { + continue; + } - // get name: check for id or name else use filename - $panels_data['id'] = sanitize_title_with_dashes( $this->get_layout_id( $panels_data, $file_name ) ); + // get file name by stripping out folder path and .json extension + $file_name = str_replace( array( $folder . '/', '.json' ), '', $file ); - if ( empty( $panels_data['name'] ) ) { - $panels_data['name'] = $file_name; - } + // get name: check for id or name else use filename + $panels_data['id'] = sanitize_title_with_dashes( $this->get_layout_id( $panels_data, $file_name ) ); - $panels_data['name'] = sanitize_text_field( $panels_data['name'] ); + if ( empty( $panels_data['name'] ) ) { + $panels_data['name'] = $file_name; + } - // get screenshot: check for screenshot prop else try use image file with same filename. - $panels_data['screenshot'] = $this->get_layout_file_screenshot( $panels_data, $folder, $file_name ); + $panels_data['name'] = sanitize_text_field( $panels_data['name'] ); - // set item on layouts array - $layouts[ $panels_data['id'] ] = $panels_data; - } + // get screenshot: check for screenshot prop else try use image file with same filename. + $panels_data['screenshot'] = $this->get_layout_file_screenshot( $panels_data, $folder, $file_name ); + + // set item on layouts array + $layouts[ $panels_data['id'] ] = $panels_data; } } } From 4388e3f525f277b424f22a366f14c561586cf745 Mon Sep 17 00:00:00 2001 From: Alex S <17275120+AlexGStapleton@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:27:34 +1200 Subject: [PATCH 2/4] Local Layouts: Restrict layouts to just JSON files. --- inc/admin-layouts.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/inc/admin-layouts.php b/inc/admin-layouts.php index 3db056ec..bfd1f9a4 100644 --- a/inc/admin-layouts.php +++ b/inc/admin-layouts.php @@ -92,19 +92,13 @@ public function get_local_layouts() { } foreach ( $files as $file ) { + // Ensure file is a JSON file. if ( function_exists( 'mime_content_type' ) ) { - // get file mime type $mime_type = mime_content_type( $file ); - - // Valid if text files. - - // Valid if text or json file. - $valid_file_type = strpos( $mime_type, '/json' ) || strpos( $mime_type, 'text/' ) > -1; + $valid_file_type = strpos( $mime_type, '/json' ) !== false; } else { - // If `mime_content_type` isn't available, just check file extension. + // Can't check MIME. Check extension. $ext = pathinfo( $file, PATHINFO_EXTENSION ); - - // skip files which don't have a `.json` extension. $valid_file_type = ! empty( $ext ) && $ext === 'json'; } From 1f5e07edae389aaa54bb43158603d3bbc8723766 Mon Sep 17 00:00:00 2001 From: Alex S <17275120+AlexGStapleton@users.noreply.github.com> Date: Mon, 16 Sep 2024 01:36:07 +1200 Subject: [PATCH 3/4] Local Prebuilt Layouts: If Text File, Check Extension --- inc/admin-layouts.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/inc/admin-layouts.php b/inc/admin-layouts.php index bfd1f9a4..07203991 100644 --- a/inc/admin-layouts.php +++ b/inc/admin-layouts.php @@ -63,6 +63,17 @@ public function get_directories() { return $directories; } + /** + * Determines if file has a JSON extension. + * + * @param string $file File path. + * @return bool True if JSON, false otherwise. + */ + private static function check_file_ext( $file ) { + $ext = pathinfo( $file, PATHINFO_EXTENSION ); + return ! empty( $ext ) && $ext === 'json'; + } + /** * Looks through local folders in the active theme and any others filtered in by theme and plugins, to find JSON * prebuilt layouts. @@ -92,14 +103,19 @@ public function get_local_layouts() { } foreach ( $files as $file ) { - // Ensure file is a JSON file. + $valid_file_type = false; if ( function_exists( 'mime_content_type' ) ) { $mime_type = mime_content_type( $file ); $valid_file_type = strpos( $mime_type, '/json' ) !== false; + + if ( ! $valid_file_type ) { + // It could have the wrong MIME type. Check for text/plain, and if it is, check the extension. + $valid_file_type = strpos( $mime_type, 'text/plain' ) !== false && + self::check_file_ext( $file ); + } } else { // Can't check MIME. Check extension. - $ext = pathinfo( $file, PATHINFO_EXTENSION ); - $valid_file_type = ! empty( $ext ) && $ext === 'json'; + $valid_file_type = self::check_file_ext( $file ); } if ( ! $valid_file_type ) { From 2eb59643488d37aedd15afa8d2a5028639981e0d Mon Sep 17 00:00:00 2001 From: Andrew Misplon Date: Tue, 17 Sep 2024 22:44:58 +0100 Subject: [PATCH 4/4] Changelog update --- readme.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.txt b/readme.txt index 4e1980a7..a5b20577 100644 --- a/readme.txt +++ b/readme.txt @@ -121,6 +121,10 @@ SiteOrigin offers a single premium plugin that enhances and extends Page Builder == Changelog == += 2.29.21 – 17 September 2024 = +* Prebuilt Local Layouts: Resolved a potential error. +* Prebuilt Local Layouts: Restricted layout files to JSON. + = 2.29.20 – 08 August 2024 = * Rank Math: Resolved content analysis issue when multiple blocks in use.