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