= 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' );