diff --git a/assets/css/components.css b/assets/css/components.css index 349b93e..e6379c7 100644 --- a/assets/css/components.css +++ b/assets/css/components.css @@ -1856,3 +1856,25 @@ html[data-theme="light"] .chrnx-music-landing .chrnx-character-stage { grid-template-columns: minmax(0, 1fr); } } + +/* Where a show can be heard, above its latest episode. */ +.chrnx-show-links { + display: flex; + flex-wrap: wrap; + gap: 0.5rem 1rem; + align-items: center; + margin-block: 1.5rem; +} + +.chrnx-show-links__label { + margin: 0; + color: var(--chrnx-muted); + font-family: var(--wp--preset--font-family--metadata); + font-size: var(--wp--preset--font-size--small); + letter-spacing: 0.08em; + text-transform: uppercase; +} + +.chrnx-show-links .chrnx-platform-links { + margin: 0; +} diff --git a/blocks/show-links/block.json b/blocks/show-links/block.json new file mode 100644 index 0000000..28fba72 --- /dev/null +++ b/blocks/show-links/block.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "apiVersion": 3, + "name": "chrnx/show-links", + "version": "0.2.0", + "title": "Show Links", + "category": "theme", + "description": "Where a podcast show can be listened to, rendered only when a URL exists.", + "textdomain": "chrnx", + "attributes": { + "show": { "type": "string", "default": "" } + }, + "supports": { "html": false }, + "render": "file:./render.php" +} diff --git a/blocks/show-links/render.php b/blocks/show-links/render.php new file mode 100644 index 0000000..bc44f29 --- /dev/null +++ b/blocks/show-links/render.php @@ -0,0 +1,23 @@ +taxonomy ) { + return; +} + +$chrnx_links = chrnx_get_show_links( $chrnx_term->term_id ); +if ( empty( $chrnx_links ) ) { + return; +} +?> +
'chrnx-show-links' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> + + +
diff --git a/functions.php b/functions.php index cf3ee9c..79cb46a 100644 --- a/functions.php +++ b/functions.php @@ -2,7 +2,7 @@ define( 'CHRNX_VERSION', '0.2.0' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -foreach ( array( 'setup', 'assets', 'taxonomies', 'editor', 'podcast-meta', 'project-meta', 'release-meta', 'track-meta', 'settings', 'queries', 'media', 'blocks', 'theme-preference' ) as $module ) { +foreach ( array( 'setup', 'assets', 'taxonomies', 'editor', 'podcast-meta', 'project-meta', 'release-meta', 'show-meta', 'track-meta', 'settings', 'queries', 'media', 'blocks', 'theme-preference' ) as $module ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $path = get_theme_file_path( "inc/{$module}.php" ); if ( file_exists( $path ) ) { diff --git a/inc/blocks.php b/inc/blocks.php index 517d53b..2f84323 100644 --- a/inc/blocks.php +++ b/inc/blocks.php @@ -232,7 +232,7 @@ function chrnx_get_editorial_card_markup( $post_id, $heading_level = 3, $class_n /** Register all block metadata directories that exist. */ function chrnx_register_editorial_blocks() { - $blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link', 'selected-work', 'release-list', 'release-detail', 'track-meta', 'subscribe-form' ); + $blocks = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta', 'theme-toggle', 'site-brand', 'mobile-menu-toggle', 'footer-content', 'header-tools', 'reading-time', 'journal-link', 'selected-work', 'release-list', 'release-detail', 'track-meta', 'subscribe-form', 'show-links' ); foreach ( $blocks as $block ) { $name = 'chrnx/' . $block; if ( WP_Block_Type_Registry::get_instance()->is_registered( $name ) ) { diff --git a/inc/release-meta.php b/inc/release-meta.php index a6e25af..7e08eb6 100644 --- a/inc/release-meta.php +++ b/inc/release-meta.php @@ -112,12 +112,13 @@ function chrnx_get_release_links( $term_id ) { /** * Render a row of platform links, or nothing when none are stored. * - * @param array $links Platform key to URL. - * @param string $class_name Wrapper class. + * @param array $links Platform key to URL. + * @param string $class_name Wrapper class. + * @param array|null $platforms Platform definitions, defaulting to releases. * @return string Escaped markup or an empty string. */ -function chrnx_get_platform_links_markup( array $links, $class_name = 'chrnx-platform-links' ) { - $platforms = chrnx_get_release_platforms(); +function chrnx_get_platform_links_markup( array $links, $class_name = 'chrnx-platform-links', $platforms = null ) { + $platforms = is_array( $platforms ) ? $platforms : chrnx_get_release_platforms(); $items = ''; foreach ( $links as $platform => $url ) { diff --git a/inc/show-meta.php b/inc/show-meta.php new file mode 100644 index 0000000..25d1e9b --- /dev/null +++ b/inc/show-meta.php @@ -0,0 +1,129 @@ +> 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' ); diff --git a/patterns/podcast-landing.php b/patterns/podcast-landing.php index 869a819..74231fd 100644 --- a/patterns/podcast-landing.php +++ b/patterns/podcast-landing.php @@ -23,6 +23,7 @@
+