Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.86% |
29 / 35 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Podcast | |
82.86% |
29 / 35 |
|
60.00% |
3 / 5 |
17.29 | |
0.00% |
0 / 1 |
| limit_episode_query | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| limit_episode_where | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| init | |
75.00% |
15 / 20 |
|
0.00% |
0 / 1 |
6.56 | |||
| is_posts_to_podcast_enabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| is_user_connected | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Main loader for the Jetpack Podcast package. |
| 4 | * |
| 5 | * @package automattic/jetpack-podcast |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Podcast; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | use Automattic\Jetpack\Podcast\Feed\Customize_Feed; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | |
| 14 | /** |
| 15 | * Loads the Jetpack Podcast package. |
| 16 | */ |
| 17 | class Podcast { |
| 18 | |
| 19 | const PACKAGE_VERSION = '2.2.8'; |
| 20 | |
| 21 | /** |
| 22 | * Whether the class has been initialized. |
| 23 | * |
| 24 | * @var bool |
| 25 | */ |
| 26 | private static $initialized = false; |
| 27 | |
| 28 | /** |
| 29 | * Limit a posts REST query for the podcast category to posts with an audio |
| 30 | * or podcast episode block. |
| 31 | * |
| 32 | * @param array $args WP_Query args. |
| 33 | * @param \WP_REST_Request $request REST request. |
| 34 | * @return array |
| 35 | */ |
| 36 | public static function limit_episode_query( $args, $request ) { |
| 37 | $requested = array_map( 'intval', (array) $request->get_param( 'categories' ) ); |
| 38 | if ( 1 !== count( $requested ) || 0 === $requested[0] || Customize_Feed::resolve_category_id() !== $requested[0] ) { |
| 39 | return $args; |
| 40 | } |
| 41 | |
| 42 | $args['jetpack_podcast_episodes'] = true; |
| 43 | return $args; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Restrict a flagged query to posts whose content has an audio or podcast episode block. |
| 48 | * |
| 49 | * @param string $where WHERE clause. |
| 50 | * @param \WP_Query $query Query. |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function limit_episode_where( $where, $query ) { |
| 54 | if ( ! $query->get( 'jetpack_podcast_episodes' ) ) { |
| 55 | return $where; |
| 56 | } |
| 57 | |
| 58 | global $wpdb; |
| 59 | $audio = '%' . $wpdb->esc_like( '<!-- wp:audio ' ) . '%'; |
| 60 | $episode = '%' . $wpdb->esc_like( '<!-- wp:jetpack/podcast-episode ' ) . '%'; |
| 61 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared below. |
| 62 | return $where . $wpdb->prepare( " AND ( {$wpdb->posts}.post_content LIKE %s OR {$wpdb->posts}.post_content LIKE %s )", $audio, $episode ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Initialize the package. Loads unconditionally; callers decide whether to. |
| 67 | */ |
| 68 | public static function init() { |
| 69 | if ( self::$initialized ) { |
| 70 | return; |
| 71 | } |
| 72 | self::$initialized = true; |
| 73 | |
| 74 | Podcast_Episode_Block::register_hooks(); |
| 75 | |
| 76 | Podcast_Stats_Endpoint::init(); |
| 77 | Podcast_Distribution_Endpoint::init(); |
| 78 | Podcast_Settings_Endpoint::init(); |
| 79 | |
| 80 | Settings::register(); |
| 81 | |
| 82 | // Wire the RSS feed customizations (`<itunes:*>` + `<podcast:*>` tags, |
| 83 | // stats-tracked enclosure URLs) for the configured podcast category. |
| 84 | Customize_Feed::init(); |
| 85 | |
| 86 | add_filter( 'rest_post_query', array( __CLASS__, 'limit_episode_query' ), 10, 2 ); |
| 87 | add_filter( 'posts_where', array( __CLASS__, 'limit_episode_where' ), 10, 2 ); |
| 88 | |
| 89 | Tracks::init(); |
| 90 | |
| 91 | Admin_Page::init(); |
| 92 | |
| 93 | if ( is_admin() ) { |
| 94 | New_Episode_Prefill::init(); |
| 95 | } |
| 96 | |
| 97 | $host = new Host(); |
| 98 | if ( $host->is_wpcom_simple() || $host->is_woa_site() ) { |
| 99 | // Register the local REST routes before request-local rollout gates. |
| 100 | // Requests from public-api.wordpress.com may not satisfy those gates, |
| 101 | // but the wpcom/v2 routes still need to exist so permission and |
| 102 | // callback checks can handle the request. |
| 103 | Posts_To_Podcast_Endpoint::init(); |
| 104 | |
| 105 | // Posts to Podcast lives behind its own filter so the Create AI |
| 106 | // Podcast page can ship independently of the rest of the package. |
| 107 | // |
| 108 | // Note: no `is_admin()` guard here. The submenu must also register when |
| 109 | // Calypso builds its nav via the `wpcom/v2/admin-menu` REST endpoint, |
| 110 | // which fires `admin_menu` by loading `wp-admin/menu.php` but runs as a |
| 111 | // REST request where `is_admin()` is false. The hooks `init()` wires |
| 112 | // (`admin_menu`, `enqueue_block_editor_assets`) self-gate, so this is a |
| 113 | // no-op on non-admin/non-editor requests. |
| 114 | if ( self::is_posts_to_podcast_enabled() ) { |
| 115 | Create_AI_Podcast_Page::init(); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Whether the Posts to Podcast feature (Create AI Podcast page + REST |
| 122 | * proxy) is enabled for the current request. |
| 123 | * |
| 124 | * Defaults to true for connected WordPress.com users, and can be flipped |
| 125 | * globally via the `jetpack_posts_to_podcast` filter. |
| 126 | */ |
| 127 | public static function is_posts_to_podcast_enabled() { |
| 128 | /** |
| 129 | * Master switch for the Posts to Podcast (Create AI Podcast) feature. |
| 130 | * |
| 131 | * @since 0.1.0 |
| 132 | * |
| 133 | * @param bool $enabled Whether to enable Posts to Podcast. |
| 134 | */ |
| 135 | $enabled = self::is_user_connected( get_current_user_id() ); |
| 136 | |
| 137 | return (bool) apply_filters( 'jetpack_posts_to_podcast', $enabled ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Whether a user is connected to WordPress.com. |
| 142 | * |
| 143 | * @param int $user_id User ID. |
| 144 | * @return bool |
| 145 | */ |
| 146 | private static function is_user_connected( $user_id ) { |
| 147 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id ); |
| 152 | } |
| 153 | } |