Skip to content

Commit

Permalink
Merge branch 'release/2.29.21'
Browse files Browse the repository at this point in the history
  • Loading branch information
Misplon committed Sep 17, 2024
2 parents 039cbeb + 2eb5964 commit b66eb7d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
63 changes: 37 additions & 26 deletions inc/admin-layouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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;
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit b66eb7d

Please sign in to comment.