-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from WordPress/update/movie-runtime-with-post-…
…meta Move movie release date and runtime to block bindings.
- Loading branch information
Showing
12 changed files
with
133 additions
and
91 deletions.
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,51 @@ | ||
<?php | ||
/** | ||
* Registers movies block variations for the 'core/paragraph' block. | ||
* | ||
* @param array $variations An array of block variations. | ||
* @param WP_Block_Type $block_type The block type object. | ||
* | ||
* @return array The updated array of block variations. | ||
*/ | ||
function wpmovies_block_type_variations( $variations, $block_type ) { | ||
if ( 'core/paragraph' === $block_type->name ) { | ||
$variations[] = array( | ||
'name' => 'wpmovies/movie-runtime', | ||
'title' => 'WP Movies - Movie Runtime', | ||
'icon' => 'clock', | ||
'attributes' => array( | ||
'metadata' => array( | ||
'bindings' => array( | ||
'content' => array( | ||
'source' => 'core/post-meta', | ||
'args' => array( | ||
'key' => 'wpmovies_runtime', | ||
), | ||
), | ||
), | ||
), | ||
), | ||
'isActive' => array( 'metadata.bindings.content' ), | ||
); | ||
$variations[] = array( | ||
'name' => 'wpmovies/movie-release-date', | ||
'title' => 'WP Movies - Movie Release Date', | ||
'icon' => 'calendar', | ||
'attributes' => array( | ||
'metadata' => array( | ||
'bindings' => array( | ||
'content' => array( | ||
'source' => 'core/post-meta', | ||
'args' => array( | ||
'key' => 'wpmovies_release_date', | ||
), | ||
), | ||
), | ||
), | ||
), | ||
'isActive' => array( 'metadata.bindings.content' ), | ||
); | ||
} | ||
return $variations; | ||
} | ||
add_filter( 'get_block_type_variations', 'wpmovies_block_type_variations', 10, 2 ); |
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,71 @@ | ||
<?php | ||
/** | ||
* Registers the custom meta field 'wpmovies_runtime' for movie posts. | ||
* | ||
* This function registers a meta field named 'wpmovies_runtime' for the 'movies' | ||
* post type. The meta field is configured to be shown in the REST API, is a single | ||
* value, and has a default value of '1h 36m'. The meta field is of type 'string'. | ||
* | ||
* @return void | ||
*/ | ||
function wp_movies_register_meta() { | ||
register_meta( | ||
'post', | ||
'wpmovies_runtime', | ||
array( | ||
'show_in_rest' => true, | ||
'single' => true, | ||
'type' => 'string', | ||
'object_subtype' => 'movies', | ||
'default' => '1h 36m', | ||
'label' => 'Movie Runtime', | ||
) | ||
); | ||
|
||
register_meta( | ||
'post', | ||
'wpmovies_release_date', | ||
array( | ||
'show_in_rest' => true, | ||
'single' => true, | ||
'type' => 'string', | ||
'object_subtype' => 'movies', | ||
'default' => '14th March 1972', | ||
'label' => 'Movie Release Date', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'wp_movies_register_meta' ); | ||
|
||
|
||
/** | ||
* Renders the runtime of a movie in hours and minutes. | ||
* | ||
* This function takes the runtime value in minutes and converts it to a | ||
* human-readable format of hours and minutes. If the runtime is not greater | ||
* than zero, it returns 'N/A'. | ||
* | ||
* @param mixed $value The original runtime value. | ||
* @param string $source_name The source of the meta data. This function only processes | ||
* values from 'core/post-meta'. | ||
* @param array $source_args Additional arguments passed to the function. | ||
* | ||
* @return string The formatted runtime in 'Xh Ym' format or 'N/A' if the runtime is not valid. | ||
*/ | ||
function wp_movies_render_movie_data( $value, $source_name, $source_args ) { | ||
if ( 'core/post-meta' !== $source_name ) { | ||
return $value; | ||
} | ||
switch ( $source_args['key'] ) { | ||
case 'wpmovies_runtime': | ||
$runtime_minutes = intval( $value ); | ||
$value = $runtime_minutes > 0 ? intdiv( $runtime_minutes, 60 ) . 'h ' . ( $runtime_minutes % 60 ) . 'm' : __( 'N/A', 'wp-movies-demo' ); | ||
break; | ||
case 'wpmovies_release_date': | ||
$value = gmdate( 'jS F Y', strtotime( $value ) ); | ||
break; | ||
} | ||
return $value; | ||
}; | ||
|
||
add_filter( 'block_bindings_source_value', 'wp_movies_render_movie_data', 10, 3 ); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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