157 lines
4.0 KiB
PHP
157 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* Podcast post metadata.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
define( 'CHRNX_MAX_EPISODE_PARTICIPANTS', 50 );
|
|
define( 'CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH', 100 );
|
|
|
|
/**
|
|
* 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[] Up to 50 sanitized names of up to 100 Unicode characters each.
|
|
*/
|
|
function chrnx_sanitize_episode_participants( $value ) {
|
|
if ( ! is_array( $value ) ) {
|
|
return array();
|
|
}
|
|
|
|
$participants = array();
|
|
foreach ( $value as $participant ) {
|
|
if ( count( $participants ) >= CHRNX_MAX_EPISODE_PARTICIPANTS ) {
|
|
break;
|
|
}
|
|
|
|
if ( ! is_scalar( $participant ) ) {
|
|
continue;
|
|
}
|
|
|
|
$name = trim( sanitize_text_field( (string) $participant ) );
|
|
$name = mb_substr( $name, 0, CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH, 'UTF-8' );
|
|
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 '';
|
|
}
|
|
|
|
$url = esc_url_raw( trim( $value ) );
|
|
$scheme = wp_parse_url( $url, PHP_URL_SCHEME );
|
|
$host = wp_parse_url( $url, PHP_URL_HOST );
|
|
|
|
return ! empty( $url ) && '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',
|
|
'maxItems' => CHRNX_MAX_EPISODE_PARTICIPANTS,
|
|
'items' => array(
|
|
'type' => 'string',
|
|
'maxLength' => CHRNX_MAX_EPISODE_PARTICIPANT_LENGTH,
|
|
),
|
|
),
|
|
),
|
|
'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' );
|