diff --git a/inc/admin-layouts.php b/inc/admin-layouts.php index 246d6a10..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,20 +103,19 @@ public function get_local_layouts() { } foreach ( $files as $file ) { + $valid_file_type = false; if ( function_exists( 'mime_content_type' ) ) { - // get file mime type $mime_type = mime_content_type( $file ); + $valid_file_type = strpos( $mime_type, '/json' ) !== false; - // Valid if text files. - - // Valid if text or json file. - $valid_file_type = strpos( $mime_type, '/json' ) || strpos( $mime_type, 'text/' ) > -1; + 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 { - // If `mime_content_type` isn't available, just check file extension. - $ext = pathinfo( $file, PATHINFO_EXTENSION ); - - // skip files which don't have a `.json` extension. - $valid_file_type = ! empty( $ext ) && $ext === 'json'; + // Can't check MIME. Check extension. + $valid_file_type = self::check_file_ext( $file ); } if ( ! $valid_file_type ) { @@ -120,28 +130,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; } } } 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.