-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parsing and validation for block.json files #2
Merged
tellyworth
merged 8 commits into
experiment/block-checker
from
add/block-json-validator
Jun 17, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
12b90ad
Add block-json directory and files
coreymckrill ed5c3f2
Add context to parser error messages
coreymckrill dfad00a
Tweaks to validator
coreymckrill f89618b
Add missing result
coreymckrill 61a0b27
Add pattern matching to string validator
coreymckrill 9d7074c
Include index when reporting array item errors
coreymckrill ebaa1b8
Use block parser/validator in the plugin importer
coreymckrill 8e9bbe3
Category will become optional.
tellyworth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "my-plugin/notice", | ||
"title": "Notice", | ||
"category": "text", | ||
"parent": [ "core/group" ], | ||
"icon": "star", | ||
"description": "Shows warning, error or success notices ...", | ||
"keywords": [ "alert", "message" ], | ||
"textdomain": "my-plugin", | ||
"attributes": { | ||
"message": { | ||
"type": "string", | ||
"source": "html", | ||
"selector": ".message" | ||
} | ||
}, | ||
"supports": { | ||
"align": true, | ||
"lightBlockWrapper": true | ||
}, | ||
"styles": [ | ||
{ "name": "default", "label": "Default", "isDefault": true }, | ||
{ "name": "other", "label": "Other" } | ||
], | ||
"example": { | ||
"attributes": { | ||
"message": "This is a notice!" | ||
} | ||
}, | ||
"editorScript": "build/editor.js", | ||
"script": "build/main.js", | ||
"editorStyle": "build/editor.css", | ||
"style": "build/style.css" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are going to be added two more fields:
Tracking ticket: https://core.trac.wordpress.org/ticket/49927 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
namespace WordPressdotorg\Plugin_Directory\Block_JSON; | ||
|
||
use WP_Error; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Class Parser | ||
* | ||
* @package WordPressdotorg\Plugin_Directory\Block_JSON | ||
*/ | ||
class Parser { | ||
/** | ||
* A location resource must be pointing to a file named this. | ||
* | ||
* @const string | ||
*/ | ||
const REQUIRED_FILENAME = 'block.json'; | ||
|
||
/** | ||
* Parse the JSON content of a given resource into a PHP object. | ||
* | ||
* @param array $resource An associative array with one item where the key specifies the type of resource and the | ||
* value represents the resource as a string. Valid types are `url`, `file`, and `content`. | ||
* In this case, the value of `content` is expected to be a raw string of JSON, while the | ||
* other two are expected to be the location of a file containing the JSON. | ||
* | ||
* @return object|WP_Error An object if the parse was successful, otherwise a WP_Error. | ||
*/ | ||
public static function parse( array $resource ) { | ||
reset( $resource ); | ||
$type = key( $resource ); | ||
$handle = current( $resource ); | ||
|
||
switch ( $type ) { | ||
case 'url': | ||
$content = self::extract_content_from_url( $handle ); | ||
break; | ||
case 'file': | ||
$content = self::extract_content_from_file( $handle ); | ||
break; | ||
case 'content': | ||
$content = $handle; | ||
break; | ||
default: | ||
$content = new WP_Error( | ||
'no_valid_resource', | ||
__( 'No valid resource type was given.', 'wporg-plugins' ) | ||
); | ||
break; | ||
} | ||
|
||
if ( is_wp_error( $content ) ) { | ||
return $content; | ||
} | ||
|
||
return self::parse_content( $content ); | ||
} | ||
|
||
/** | ||
* Get the contents of a block.json file via an HTTP request to a URL. | ||
* | ||
* @param string $url | ||
* | ||
* @return string|WP_Error | ||
*/ | ||
protected static function extract_content_from_url( $url ) { | ||
$url = esc_url_raw( $url ); | ||
|
||
$filename_length = strlen( self::REQUIRED_FILENAME ); | ||
if ( strtolower( substr( $url, - $filename_length ) ) !== self::REQUIRED_FILENAME ) { | ||
return new WP_Error( | ||
'resource_url_invalid', | ||
sprintf( | ||
/* translators: %s: file name */ | ||
__( 'URL must end in %s!', 'wporg-plugins' ), | ||
'<code>' . self::REQUIRED_FILENAME . '</code>' | ||
) | ||
); | ||
} | ||
|
||
$response = wp_safe_remote_get( $url ); | ||
$response_code = wp_remote_retrieve_response_code( $response ); | ||
|
||
if ( is_wp_error( $response ) ) { | ||
return $response; | ||
} elseif ( 200 !== $response_code ) { | ||
return new WP_Error( | ||
'resource_url_unexpected_response', | ||
__( 'URL returned an unexpected status code.', 'wporg-plugins' ), | ||
array( | ||
'status' => $response_code, | ||
) | ||
); | ||
} | ||
|
||
return wp_remote_retrieve_body( $response ); | ||
} | ||
|
||
/** | ||
* Get the contents of a block.json file via a path in the filesystem. | ||
* | ||
* @param string $file | ||
* | ||
* @return string|WP_Error | ||
*/ | ||
protected static function extract_content_from_file( $file ) { | ||
$filename_length = strlen( self::REQUIRED_FILENAME ); | ||
if ( strtolower( substr( $file, - $filename_length ) ) !== self::REQUIRED_FILENAME ) { | ||
return new WP_Error( | ||
'resource_file_invalid', | ||
sprintf( | ||
/* translators: %s: file name */ | ||
__( 'File must be named %s!', 'wporg-plugins' ), | ||
'<code>' . self::REQUIRED_FILENAME . '</code>' | ||
) | ||
); | ||
} | ||
|
||
if ( ! is_readable( $file ) ) { | ||
return new WP_Error( | ||
'resource_file_unreadable', | ||
__( 'The file could not be read.', 'wporg-plugins' ), | ||
array( | ||
'file' => $file, | ||
) | ||
); | ||
} | ||
|
||
$content = file_get_contents( $file ); | ||
|
||
if ( false === $content ) { | ||
return new WP_Error( | ||
'resource_file_failed_retrieval', | ||
__( 'Could not get the contents of the file.', 'wporg-plugins' ), | ||
array( | ||
'file' => $file, | ||
) | ||
); | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Parse a JSON string into an object, and handle parsing errors. | ||
* | ||
* @param string $content | ||
* | ||
* @return object|WP_Error | ||
*/ | ||
protected static function parse_content( $content ) { | ||
$parsed = json_decode( $content ); | ||
$error = json_last_error_msg(); | ||
|
||
// TODO Once we are on PHP 7.3 we can use the JSON_THROW_ON_ERROR option and catch an exception here. | ||
if ( 'No error' !== $error ) { | ||
return new WP_Error( | ||
'json_parse_error', | ||
sprintf( | ||
__( 'JSON Parser: %s', 'wporg-plugins' ), | ||
esc_html( $error ) | ||
), | ||
array( | ||
'error_code' => json_last_error(), | ||
) | ||
); | ||
} | ||
|
||
return $parsed; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up with a more nuanced way of defining assets, for example:
https://github.com/WordPress/gutenberg/blob/master/docs/rfc/block-registration.md#editor-script
Full description here:
https://github.com/WordPress/gutenberg/blob/master/docs/rfc/block-registration.md#wpdefinedasset
Long story short, developers can provide a relative path as before or a script/style handle. For the Block Directory, you could say that file is the only option.