Skip to content

Commit

Permalink
Merge pull request #76 from WordPress/update/movie-runtime-with-post-…
Browse files Browse the repository at this point in the history
…meta

Move movie release date and runtime to block bindings.
  • Loading branch information
cbravobernal authored Oct 11, 2024
2 parents 81c1d37 + f3f36ca commit 9240db0
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 91 deletions.
51 changes: 51 additions & 0 deletions lib/custom-block-variations.php
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 );
71 changes: 71 additions & 0 deletions lib/custom-post-meta.php
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 );
14 changes: 0 additions & 14 deletions src/blocks/non-interactive/movie-release-date/block.json

This file was deleted.

7 changes: 0 additions & 7 deletions src/blocks/non-interactive/movie-release-date/edit.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/blocks/non-interactive/movie-release-date/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/blocks/non-interactive/movie-release-date/render.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/blocks/non-interactive/movie-runtime/block.json

This file was deleted.

7 changes: 0 additions & 7 deletions src/blocks/non-interactive/movie-runtime/edit.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/blocks/non-interactive/movie-runtime/index.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/blocks/non-interactive/movie-runtime/render.php

This file was deleted.

11 changes: 8 additions & 3 deletions wp-movies-theme/templates/single-movies.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@
>
<!-- wp:wpmovies/movie-score /-->

<!-- wp:wpmovies/movie-release-date /-->

<!-- wp:wpmovies/movie-runtime /-->

<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"wpmovies_release_date"}}}}} -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"wpmovies_runtime"}}}}} -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:wpmovies/movie-genres /-->
</div>
Expand Down
4 changes: 3 additions & 1 deletion wpmovies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: WP Movies
* Version: 0.1.37
* Requires at least: 6.5
* Requires at least: 6.7
* Requires PHP: 7.0
* Description: Plugin that demoes the usage of the Interactivity API.
* Author: WordPress Team
Expand All @@ -15,6 +15,8 @@
require_once __DIR__ . '/lib/custom-post-types.php';
require_once __DIR__ . '/lib/custom-taxonomies.php';
require_once __DIR__ . '/lib/custom-query-block.php';
require_once __DIR__ . '/lib/custom-post-meta.php';
require_once __DIR__ . '/lib/custom-block-variations.php';

add_action( 'init', 'auto_register_block_types' );

Expand Down

0 comments on commit 9240db0

Please sign in to comment.