feat: add CHRNX editorial metadata
This commit is contained in:
10 files changed
+1127
-1
No files matched your search
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* Podcast post metadata.
|
||||
*
|
||||
* @package CHRNX
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sanitize an episode number.
|
||||
*
|
||||
* @param mixed $value Submitted value.
|
||||
* @return int Nonnegative episode number.
|
||||
*/
|
||||
function chrnx_sanitize_episode_number( $value ) {
|
||||
return max( 0, intval( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a duration display string.
|
||||
*
|
||||
* @param mixed $value Submitted value.
|
||||
* @return string Valid duration or an empty string.
|
||||
*/
|
||||
function chrnx_sanitize_episode_duration( $value ) {
|
||||
if ( ! is_string( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = trim( $value );
|
||||
$sanitized = sanitize_text_field( $value );
|
||||
if ( $value !== $sanitized || ! preg_match( '/^\d+:[0-5]\d(?::[0-5]\d)?$/', $sanitized ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize participant names while preserving their order.
|
||||
*
|
||||
* @param mixed $value Submitted value.
|
||||
* @return string[] Sanitized participant names.
|
||||
*/
|
||||
function chrnx_sanitize_episode_participants( $value ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$participants = array();
|
||||
foreach ( $value as $participant ) {
|
||||
if ( ! is_scalar( $participant ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = trim( sanitize_text_field( (string) $participant ) );
|
||||
if ( '' !== $name ) {
|
||||
$participants[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
return $participants;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a canonical episode URL and require HTTPS.
|
||||
*
|
||||
* @param mixed $value Submitted value.
|
||||
* @return string Valid HTTPS URL or an empty string.
|
||||
*/
|
||||
function chrnx_sanitize_canonical_episode_url( $value ) {
|
||||
if ( ! is_string( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = trim( $value );
|
||||
if ( ! filter_var( $value, FILTER_VALIDATE_URL ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$url = esc_url_raw( $value, array( 'https' ) );
|
||||
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
|
||||
$host = wp_parse_url( $url, PHP_URL_HOST );
|
||||
|
||||
return 'https' === $scheme && ! empty( $host ) ? $url : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Require permission to edit the post owning podcast metadata.
|
||||
*
|
||||
* @param bool $allowed Previously calculated permission.
|
||||
* @param string $meta_key Metadata key.
|
||||
* @param int $post_id Post ID.
|
||||
* @return bool Whether the current user can edit the post.
|
||||
*/
|
||||
function chrnx_auth_podcast_meta( $allowed, $meta_key, $post_id ) {
|
||||
return current_user_can( 'edit_post', $post_id );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register podcast metadata for posts.
|
||||
*/
|
||||
function chrnx_register_podcast_meta() {
|
||||
$definitions = array(
|
||||
'chrnx_episode_number' => array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'chrnx_sanitize_episode_number',
|
||||
'schema' => array(
|
||||
'type' => 'integer',
|
||||
'minimum' => 0,
|
||||
),
|
||||
),
|
||||
'chrnx_episode_duration' => array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'chrnx_sanitize_episode_duration',
|
||||
'schema' => array( 'type' => 'string' ),
|
||||
),
|
||||
'chrnx_episode_participants' => array(
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => 'chrnx_sanitize_episode_participants',
|
||||
'schema' => array(
|
||||
'type' => 'array',
|
||||
'items' => array( 'type' => 'string' ),
|
||||
),
|
||||
),
|
||||
'canonical_episode_url' => array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'chrnx_sanitize_canonical_episode_url',
|
||||
'schema' => array(
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $definitions as $meta_key => $definition ) {
|
||||
register_post_meta(
|
||||
'post',
|
||||
$meta_key,
|
||||
array(
|
||||
'type' => $definition['type'],
|
||||
'single' => true,
|
||||
'show_in_rest' => array( 'schema' => $definition['schema'] ),
|
||||
'sanitize_callback' => $definition['sanitize_callback'],
|
||||
'auth_callback' => 'chrnx_auth_podcast_meta',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
add_action( 'init', 'chrnx_register_podcast_meta' );
|
||||
Reference in new issue
Block a user