anchor-fm.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Anchor.fm integration. |
| 4 | * |
| 5 | * @since 9.3.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\AnchorFm; |
| 11 | |
| 12 | use Automattic\Jetpack\Assets; |
| 13 | use Automattic\Jetpack\Blocks; |
| 14 | use Jetpack_Podcast_Helper; |
| 15 | |
| 16 | const FEATURE_NAME = 'anchor-fm'; |
| 17 | const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
| 18 | |
| 19 | if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) { |
| 20 | require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-podcast-helper.php'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Registers Anchor.fm integration for the block editor. |
| 25 | */ |
| 26 | function register_extension() { |
| 27 | Blocks::jetpack_register_block( BLOCK_NAME ); |
| 28 | |
| 29 | // Register post_meta for connecting Anchor podcasts with posts. |
| 30 | register_post_meta( |
| 31 | 'post', |
| 32 | 'jetpack_anchor_podcast', |
| 33 | array( |
| 34 | 'show_in_rest' => true, |
| 35 | 'single' => true, |
| 36 | 'type' => 'string', |
| 37 | ) |
| 38 | ); |
| 39 | register_post_meta( |
| 40 | 'post', |
| 41 | 'jetpack_anchor_episode', |
| 42 | array( |
| 43 | 'show_in_rest' => true, |
| 44 | 'single' => true, |
| 45 | 'type' => 'string', |
| 46 | ) |
| 47 | ); |
| 48 | register_post_meta( |
| 49 | 'post', |
| 50 | 'jetpack_anchor_spotify_show', |
| 51 | array( |
| 52 | 'show_in_rest' => true, |
| 53 | 'single' => true, |
| 54 | 'type' => 'string', |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Checks URL params to determine the Anchor integration action to perform. |
| 61 | */ |
| 62 | function process_anchor_params() { |
| 63 | if ( |
| 64 | ! function_exists( 'get_current_screen' ) |
| 65 | || \get_current_screen() === null |
| 66 | ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Return early if we are not in the block editor. |
| 71 | if ( ! wp_should_load_block_editor_scripts_and_styles() ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | $post = get_post(); |
| 76 | if ( ! $post || ! $post->ID ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 81 | $podcast_id = isset( $_GET['anchor_podcast'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_podcast'] ) ) : null; |
| 82 | $episode_id = isset( $_GET['anchor_episode'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_episode'] ) ) : null; |
| 83 | $spotify_url = isset( $_GET['spotify_url'] ) ? esc_url_raw( wp_unslash( $_GET['spotify_url'] ) ) : null; |
| 84 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 85 | |
| 86 | $data = array( |
| 87 | 'actions' => array(), |
| 88 | ); |
| 89 | |
| 90 | // add / update Spotify Badge URL. |
| 91 | $valid_spotify_url = \Jetpack_Gutenberg::validate_block_embed_url( $spotify_url, array( 'open.spotify.com' ) ); |
| 92 | if ( $valid_spotify_url ) { |
| 93 | $data['spotifyShowUrl'] = $valid_spotify_url; |
| 94 | if ( get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) !== $valid_spotify_url ) { |
| 95 | update_post_meta( $post->ID, 'jetpack_anchor_spotify_show', $valid_spotify_url ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ( ! empty( $podcast_id ) ) { |
| 100 | $feed = 'https://anchor.fm/s/' . $podcast_id . '/podcast/rss'; |
| 101 | $podcast_helper = new Jetpack_Podcast_Helper( $feed ); |
| 102 | $rss = $podcast_helper->load_feed(); |
| 103 | if ( ! \is_wp_error( $rss ) ) { |
| 104 | update_post_meta( $post->ID, 'jetpack_anchor_podcast', $podcast_id ); |
| 105 | |
| 106 | // If we haven't got an episode ID, try and get the latest episode. |
| 107 | if ( empty( $episode_id ) && $rss->get_item_quantity() ) { |
| 108 | $latest_episode = $rss->get_item( 0 ); |
| 109 | if ( $latest_episode ) { |
| 110 | $episode_id = $latest_episode->get_id(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if ( ! empty( $episode_id ) ) { |
| 115 | $track = $podcast_helper->get_track_data( $episode_id, true ); |
| 116 | if ( ! \is_wp_error( $track ) ) { |
| 117 | update_post_meta( $post->ID, 'jetpack_anchor_episode', $track['guid'] ); |
| 118 | |
| 119 | if ( 'post-new.php' === $GLOBALS['pagenow'] ) { |
| 120 | $data['actions'][] = array( |
| 121 | 'set-episode-title', |
| 122 | array( |
| 123 | 'title' => $track['title'], |
| 124 | ), |
| 125 | ); |
| 126 | |
| 127 | $self_links = $rss->get_links( 'self' ); |
| 128 | $cover = $rss->get_image_url(); |
| 129 | |
| 130 | // Add insert basic template action. |
| 131 | $data['actions'][] = array( |
| 132 | 'insert-episode-template', |
| 133 | array( |
| 134 | 'feedUrl' => ! empty( $self_links ) ? esc_url_raw( $self_links[0] ) : $feed, |
| 135 | 'coverImage' => ! empty( $cover ) ? esc_url( $cover ) : null, |
| 136 | 'episodeTrack' => $track, |
| 137 | 'spotifyImageUrl' => Assets::staticize_subdomain( 'https://wordpress.com/i/spotify-badge.svg' ), |
| 138 | 'spotifyShowUrl' => esc_url_raw( $valid_spotify_url ), |
| 139 | ), |
| 140 | ); |
| 141 | } |
| 142 | } else { |
| 143 | $retry_url = add_query_arg( |
| 144 | array( |
| 145 | 'anchor_episode' => $episode_id, |
| 146 | 'anchor_podcast' => $podcast_id, |
| 147 | 'spotify_url' => $valid_spotify_url ? rawurlencode( $spotify_url ) : false, |
| 148 | ), |
| 149 | admin_url( 'post-new.php' ) |
| 150 | ); |
| 151 | $data['actions'][] = array( |
| 152 | 'create-episode-error-notice', |
| 153 | array( |
| 154 | 'retry_url' => esc_url_raw( $retry_url ), |
| 155 | ), |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Add Spotify Badge template action. |
| 163 | if ( |
| 164 | $valid_spotify_url && ( |
| 165 | 'post-new.php' !== $GLOBALS['pagenow'] // Delegate badge insertion to podcast template. |
| 166 | ) |
| 167 | ) { |
| 168 | $data['actions'][] = array( |
| 169 | 'insert-spotify-badge', |
| 170 | array( |
| 171 | 'spotifyImageUrl' => Assets::staticize_subdomain( 'https://wordpress.com/i/spotify-badge.svg' ), |
| 172 | 'spotifyShowUrl' => esc_url_raw( $valid_spotify_url ), |
| 173 | ), |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | // Display an outbound link after publishing a post (only to English-speaking users since Anchor |
| 178 | // is English only). This is only displayed if the blog is connected to an Anchor podcast. |
| 179 | $blog_connected_to_anchor = function_exists( 'get_blog_option' ) && false !== get_blog_option( get_current_blog_id(), 'anchor_podcast' ); |
| 180 | |
| 181 | /** |
| 182 | * Allows disabling the Anchor convert to audio prompt. |
| 183 | * |
| 184 | * @since 12.1 |
| 185 | * |
| 186 | * @param bool $is_anchor_enabled Whether the prompt is enabled or not. |
| 187 | */ |
| 188 | $is_anchor_enabled = apply_filters( 'jetpack_is_anchor_enabled', true ); |
| 189 | |
| 190 | if ( |
| 191 | $is_anchor_enabled && |
| 192 | 'post' === get_post_type() && |
| 193 | ! get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) && |
| 194 | 0 === strpos( get_user_locale(), 'en' ) && |
| 195 | $blog_connected_to_anchor |
| 196 | ) { |
| 197 | $data['actions'][] = 'show-post-publish-outbound-link'; |
| 198 | } |
| 199 | |
| 200 | wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_AnchorFm', $data ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Function to check if the Anchor feature is enabled. This is used in the |
| 205 | * filter below. |
| 206 | * |
| 207 | * @param bool $is_anchor_enabled filter input value. |
| 208 | * @return bool whether the feature is enabled or not. |
| 209 | */ |
| 210 | function is_anchor_enabled( $is_anchor_enabled ) { |
| 211 | if ( ! $is_anchor_enabled ) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | return time() < strtotime( '2023-05-11 00:00:00 UTC' ); |
| 216 | } |
| 217 | |
| 218 | add_filter( |
| 219 | 'jetpack_is_anchor_enabled', |
| 220 | __NAMESPACE__ . '\is_anchor_enabled' |
| 221 | ); |
| 222 | |
| 223 | add_action( 'init', __NAMESPACE__ . '\register_extension' ); |
| 224 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\process_anchor_params' ); |
| 225 |