> Platform key to label and icon. */ function chrnx_get_show_platforms() { $release = chrnx_get_release_platforms(); return array( 'spotify' => $release['spotify'], 'apple' => array( 'label' => __( 'Apple Podcasts', 'chrnx' ), 'icon' => $release['apple']['icon'], ), 'rss' => array( 'label' => __( 'RSS feed', 'chrnx' ), 'icon' => '', ), ); } /** * Return the show term meta keys and their sanitizers. * * @return array Meta key to sanitize callback. */ function chrnx_get_show_meta_definitions() { $definitions = array(); foreach ( array_keys( chrnx_get_show_platforms() ) as $platform ) { $definitions[ 'chrnx_show_url_' . $platform ] = 'chrnx_sanitize_https_url'; } return $definitions; } /** * Register show term metadata. */ function chrnx_register_show_meta() { foreach ( chrnx_get_show_meta_definitions() as $meta_key => $sanitize ) { register_term_meta( 'chrnx_show', $meta_key, array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => $sanitize, 'auth_callback' => 'chrnx_auth_release_meta', ) ); } } add_action( 'init', 'chrnx_register_show_meta' ); /** * Return the stored listening links for one show. * * @param int $term_id Show term ID. * @return array Platform key to URL, omitting empty ones. */ function chrnx_get_show_links( $term_id ) { $links = array(); foreach ( array_keys( chrnx_get_show_platforms() ) as $platform ) { $url = chrnx_sanitize_https_url( get_term_meta( $term_id, 'chrnx_show_url_' . $platform, true ) ); if ( '' !== $url ) { $links[ $platform ] = $url; } } return $links; } /** * Render the show metadata fields on the term edit screen. * * @param WP_Term $term Show term. */ function chrnx_render_show_edit_fields( $term ) { foreach ( chrnx_get_show_platforms() as $platform => $definition ) { chrnx_print_release_field( 'chrnx_show_url_' . $platform, /* translators: %s: listening platform name. */ sprintf( __( '%s URL', 'chrnx' ), $definition['label'] ), (string) get_term_meta( $term->term_id, 'chrnx_show_url_' . $platform, true ), 'url', __( 'HTTPS only. Leave empty to hide this icon.', 'chrnx' ) ); } } add_action( 'chrnx_show_edit_form_fields', 'chrnx_render_show_edit_fields' ); /** * Persist show term metadata from the term edit screen. * * @param int $term_id Show term ID. */ function chrnx_save_show_meta( $term_id ) { if ( ! chrnx_auth_release_meta() ) { return; } // Core supplies and verifies the add/update term nonce on these screens. $nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'update-tag_' . $term_id ) && ! wp_verify_nonce( $nonce, 'add-tag' ) ) { return; } foreach ( chrnx_get_show_meta_definitions() as $meta_key => $sanitize ) { if ( ! isset( $_POST[ $meta_key ] ) ) { continue; } $value = call_user_func( $sanitize, wp_unslash( $_POST[ $meta_key ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by the mapped callback. if ( '' === $value ) { delete_term_meta( $term_id, $meta_key ); continue; } update_term_meta( $term_id, $meta_key, $value ); } } add_action( 'edited_chrnx_show', 'chrnx_save_show_meta' ); add_action( 'created_chrnx_show', 'chrnx_save_show_meta' );