Skip to content

Commit

Permalink
Add workshop post type and register some meta keys for it
Browse files Browse the repository at this point in the history
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
coreymckrill authored Aug 6, 2020
2 parents 21657ec + 0e1d650 commit cc475a1
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 53 deletions.
8 changes: 4 additions & 4 deletions wp-content/plugins/wporg-learn/inc/class-lesson-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function lesson_duration_taxonomy() {
'show_tagcloud' => false,
'show_in_rest' => true,
);
register_taxonomy( 'duration', array( 'lesson-plan', 'wporg_workshop' ), $args );
register_taxonomy( 'duration', array( 'lesson-plan' ), $args );

}

Expand Down Expand Up @@ -139,7 +139,7 @@ public static function lesson_level_taxonomy() {
'show_tagcloud' => false,
'show_in_rest' => true,
);
register_taxonomy( 'level', array( 'lesson-plan', 'wporg_workshop' ), $args );
register_taxonomy( 'level', array( 'lesson-plan' ), $args );

}

Expand Down Expand Up @@ -180,7 +180,7 @@ public static function lesson_audience_taxonomy() {
'show_tagcloud' => false,
'show_in_rest' => true,
);
register_taxonomy( 'audience', array( 'lesson-plan', 'wporg_workshop' ), $args );
register_taxonomy( 'audience', array( 'lesson-plan' ), $args );

}

Expand Down Expand Up @@ -221,7 +221,7 @@ public static function lesson_instruction_type_taxonomy() {
'show_tagcloud' => false,
'show_in_rest' => true,
);
register_taxonomy( 'instruction_type', array( 'lesson-plan', 'wporg_workshop' ), $args );
register_taxonomy( 'instruction_type', array( 'lesson-plan' ), $args );

}

Expand Down
2 changes: 1 addition & 1 deletion wp-content/plugins/wporg-learn/inc/class-workshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function lesson_workshop_taxonomy() {
'show_in_rest' => true,
);

register_taxonomy( 'lesson_group', array( 'wporg_workshop', 'lesson-plan' ), $args );
register_taxonomy( 'lesson_group', array( 'lesson-plan' ), $args );
}

/**
Expand Down
179 changes: 179 additions & 0 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
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 );
}
}
6 changes: 6 additions & 0 deletions wp-content/plugins/wporg-learn/inc/post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

defined( 'WPINC' ) || die();

/**
* Register all post types.
*/
function register() {
register_workshop();
}

/**
* Register a Workshop post type.
*/
function register_workshop() {
$labels = array(
'name' => _x( 'Workshops', 'Post Type General Name', 'wporg_learn' ),
Expand Down
51 changes: 51 additions & 0 deletions wp-content/plugins/wporg-learn/views/metabox-workshop-details.php
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>
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>
4 changes: 4 additions & 0 deletions wp-content/plugins/wporg-learn/wporg-learn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_once dirname( __FILE__ ) . '/inc/class-lesson-plan.php';
require_once dirname( __FILE__ ) . '/inc/class-workshop.php';
require_once dirname( __FILE__ ) . '/inc/post-type.php';
require_once dirname( __FILE__ ) . '/inc/post-meta.php';

/**
* Registry of actions and filters
Expand All @@ -37,6 +38,9 @@


add_action( 'init', 'WPORG_Learn\Post_Types\register' );
add_action( 'init', 'WPORG_Learn\Post_Meta\register' );
add_action( 'add_meta_boxes', 'WPORG_Learn\Post_Meta\add_workshop_metaboxes' );
add_action( 'save_post_wporg_workshop', 'WPORG_Learn\Post_Meta\save_workshop_metabox_fields', 10, 2 );
add_action( 'init', array( 'WPORG_Learn\Workshop', 'lesson_workshop_taxonomy' ) );
add_action( 'init', array( 'WPORG_Learn\Workshop', 'workshop_topics_taxonomy' ) );
add_filter('query_vars', 'add_category');
Expand Down
46 changes: 0 additions & 46 deletions wp-content/themes/wporg-learn/single-workshop.php

This file was deleted.

4 changes: 2 additions & 2 deletions wp-content/themes/wporg-learn/single-wporg_workshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package WPBBP
*/

get_header();
get_header();
?>

<main id="main" class="site-main page-full-width" role="main">
Expand All @@ -17,4 +17,4 @@
</main><!-- #main -->

<?php
get_footer();
get_footer();

0 comments on commit cc475a1

Please sign in to comment.