-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workshop post type and register some meta keys for it
Registers a new Workshop post type (different than the original workshop post type from the training team repo, which has been temporarily disabled until we can rethink how to integrate it). Also registers several post meta keys and adds a crude UI to the block editor for managing those keys, via meta boxes. Eventually those meta boxes should be replaced with proper block editor controls of some kind.
- Loading branch information
Showing
9 changed files
with
262 additions
and
53 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
namespace WPOrg_Learn\Post_Meta; | ||
|
||
use DateTime, DateInterval; | ||
use WP_Post; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Register all post meta keys. | ||
*/ | ||
function register() { | ||
register_workshop_meta(); | ||
} | ||
|
||
/** | ||
* Register post meta keys for workshops. | ||
*/ | ||
function register_workshop_meta() { | ||
$post_type = 'wporg_workshop'; | ||
|
||
register_post_meta( | ||
$post_type, | ||
'duration', | ||
array( | ||
'description' => __( 'The duration in seconds of the workshop. Should be converted to a human readable string for display.', 'wporg_learn' ), | ||
'type' => 'integer', | ||
'single' => true, | ||
'sanitize_callback' => 'absint', | ||
'show_in_rest' => true, | ||
) | ||
); | ||
|
||
register_post_meta( | ||
$post_type, | ||
'facilitator_wporg_username', | ||
array( | ||
'description' => __( 'The WordPress.org user name of a facilitator for this workshop.', 'wporg_learn' ), | ||
'type' => 'string', | ||
'single' => false, | ||
'sanitize_callback' => 'sanitize_user', | ||
'show_in_rest' => true, | ||
) | ||
); | ||
|
||
register_post_meta( | ||
$post_type, | ||
'video_language', | ||
array( | ||
'description' => __( 'The language that the workshop is presented in.', 'wporg_learn' ), | ||
'type' => 'string', | ||
'single' => true, | ||
'sanitize_callback' => '', // todo | ||
'show_in_rest' => true, | ||
) | ||
); | ||
|
||
register_post_meta( | ||
$post_type, | ||
'video_caption_language', | ||
array( | ||
'description' => __( 'A language for which captions are available for the workshop video.', 'wporg_learn' ), | ||
'type' => 'string', | ||
'single' => false, | ||
'sanitize_callback' => '', // todo | ||
'show_in_rest' => true, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get the duration of a workshop in a specified format. | ||
* | ||
* @param WP_Post $workshop The workshop post to get the duration for. | ||
* @param string $format Optional. The format to return the duration in. 'raw', 'interval', or 'string'. | ||
* Default 'raw'. | ||
* | ||
* @return int|DateInterval|string | ||
*/ | ||
function get_workshop_duration( WP_Post $workshop, $format = 'raw' ) { | ||
$raw_duration = $workshop->duration ?: 0; | ||
$interval = date_diff( new DateTime( '@0' ), new DateTime( "@$raw_duration" ) ); // The '@' ignores timezone. | ||
$return = null; | ||
|
||
switch ( $format ) { | ||
case 'interval': | ||
$return = $interval; | ||
break; | ||
case 'string': | ||
$return = human_readable_duration( $interval->format( 'HH:ii:ss' ) ); | ||
break; | ||
case 'raw': | ||
default: | ||
$return = $raw_duration; | ||
break; | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
/** | ||
* Add meta boxes to the Edit Workshop screen. | ||
* | ||
* Todo these should be replaced with block editor panels. | ||
*/ | ||
function add_workshop_metaboxes() { | ||
add_meta_box( | ||
'workshop-details', | ||
__( 'Workshop Details', 'wporg_learn' ), | ||
__NAMESPACE__ . '\render_metabox_workshop_details', | ||
'wporg_workshop', | ||
'side' | ||
); | ||
|
||
add_meta_box( | ||
'workshop-facilitators', | ||
__( 'Facilitators', 'wporg_learn' ), | ||
__NAMESPACE__ . '\render_metabox_workshop_facilitators', | ||
'wporg_workshop', | ||
'side' | ||
); | ||
} | ||
|
||
/** | ||
* Render the Workshop Details meta box. | ||
* | ||
* @param WP_Post $post | ||
*/ | ||
function render_metabox_workshop_details( WP_Post $post ) { | ||
$duration_interval = get_workshop_duration( $post, 'interval' ); | ||
$captions = get_post_meta( $post->ID, 'video_caption_language' ) ?: array(); | ||
|
||
require dirname( dirname( __FILE__ ) ) . '/views/metabox-workshop-details.php'; | ||
} | ||
|
||
/** | ||
* Render the Facilitators meta box. | ||
* | ||
* @param WP_Post $post | ||
*/ | ||
function render_metabox_workshop_facilitators( WP_Post $post ) { | ||
$facilitators = get_post_meta( $post->ID, 'facilitator_wporg_username' ) ?: array(); | ||
|
||
require dirname( dirname( __FILE__ ) ) . '/views/metabox-workshop-facilitators.php'; | ||
} | ||
|
||
/** | ||
* Update the post meta values from the meta box fields when the post is saved. | ||
* | ||
* @param int $post_id | ||
* @param WP_Post $post | ||
*/ | ||
function save_workshop_metabox_fields( $post_id, WP_Post $post ) { | ||
if ( wp_is_post_revision( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { | ||
return; | ||
} | ||
|
||
$duration = filter_input( INPUT_POST, 'duration', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY ); | ||
$duration = $duration['h'] * HOUR_IN_SECONDS + $duration['m'] * MINUTE_IN_SECONDS; | ||
update_post_meta( $post_id, 'duration', $duration ); | ||
|
||
$facilitator_wporg_username = filter_input( INPUT_POST, 'facilitator-wporg-username' ); | ||
$usernames = array_map( 'trim', explode( ',', $facilitator_wporg_username ) ); | ||
delete_post_meta( $post_id, 'facilitator_wporg_username' ); | ||
foreach( $usernames as $username ) { | ||
add_post_meta( $post_id, 'facilitator_wporg_username', $username ); | ||
} | ||
|
||
$video_language = filter_input( INPUT_POST, 'video-language' ); | ||
update_post_meta( $post_id, 'video_language', $video_language ); | ||
|
||
$video_caption_language = filter_input( INPUT_POST, 'video-caption-language' ); | ||
$captions = array_map( 'trim', explode( ',', $video_caption_language ) ); | ||
delete_post_meta( $post_id, 'video_caption_language' ); | ||
foreach( $captions as $caption ) { | ||
add_post_meta( $post_id, 'video_caption_language', $caption ); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
wp-content/plugins/wporg-learn/views/metabox-workshop-details.php
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 | ||
/** @var WP_Post $post */ | ||
/** @var DateInterval $duration_interval */ | ||
/** @var array $captions */ | ||
?> | ||
|
||
<p> | ||
<label><?php _e( 'Duration', 'wporg_learn' ); ?></label><br /> | ||
<label for="workshop-duration-hours"> | ||
<input | ||
id="workshop-duration-hours" | ||
name="duration[h]" | ||
class="tiny-text" | ||
type="number" | ||
value="<?php echo absint( $duration_interval->h ); ?>" | ||
max="23" | ||
/> | ||
<?php _e( 'hours', 'wporg_learn' ); ?> | ||
</label> | ||
<label for="workshop-duration-minutes"> | ||
<input | ||
id="workshop-duration-minutes" | ||
name="duration[m]" | ||
class="tiny-text" | ||
type="number" | ||
value="<?php echo absint( $duration_interval->i ); ?>" | ||
max="59" | ||
/> | ||
<?php _e( 'minutes', 'wporg_learn' ); ?> | ||
</label> | ||
</p> | ||
|
||
<?php // todo Change this to a select dropdown with locale values. ?> | ||
<p> | ||
<label for="workshop-video-language"><?php _e( 'Language', 'wporg_learn' ); ?></label> | ||
<input | ||
id="workshop-video-language" | ||
name="video-language" | ||
type="text" | ||
value="<?php echo sanitize_text_field( $post->video_language ); ?>" | ||
/> | ||
</p> | ||
|
||
<?php // todo Change this to a multiselect dropdown with locale values. ?> | ||
<p> | ||
<label for="workshop-video-caption-language"><?php _e( 'Captions', 'wporg_learn' ); ?></label> | ||
<textarea id="workshop-video-caption-language" name="video-caption-language"><?php echo sanitize_textarea_field( implode( ', ', $captions ) ); ?></textarea> | ||
<span class="help"> | ||
<?php _e( 'Separate multiple languages with a comma.', 'wporg_learn' ); ?> | ||
</span> | ||
</p> |
15 changes: 15 additions & 0 deletions
15
wp-content/plugins/wporg-learn/views/metabox-workshop-facilitators.php
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,15 @@ | ||
<?php | ||
/** @var WP_Post $post */ | ||
/** @var array $facilitators */ | ||
?> | ||
|
||
<?php // todo Change this to a multiselect dropdown that validates wporg usernames. ?> | ||
<p> | ||
<label for="workshop-facilitator-wporg-username"><?php _e( 'WordPress.org User Names', 'wporg_learn' ); ?></label> | ||
<textarea id="workshop-facilitator-wporg-username" name="facilitator-wporg-username"><?php | ||
echo sanitize_textarea_field( implode( ', ', $facilitators ) ); | ||
?></textarea> | ||
<span class="help"> | ||
<?php _e( 'Separate multiple facilitator user names with a comma.', 'wporg_learn' ); ?> | ||
</span> | ||
</p> |
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 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