Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.91% covered (warning)
73.91%
17 / 23
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Podcast
73.91% covered (warning)
73.91%
17 / 23
33.33% covered (danger)
33.33%
1 / 3
11.78
0.00% covered (danger)
0.00%
0 / 1
 init
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
6.77
 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 = '1.3.1';
20
21    /**
22     * Whether the class has been initialized.
23     *
24     * @var bool
25     */
26    private static $initialized = false;
27
28    /**
29     * Initialize the package. Loads unconditionally; callers decide whether to.
30     */
31    public static function init() {
32        if ( self::$initialized ) {
33            return;
34        }
35        self::$initialized = true;
36
37        Podcast_Episode_Block::register_hooks();
38
39        Podcast_Stats_Endpoint::init();
40        Podcast_Distribution_Endpoint::init();
41        Podcast_Settings_Endpoint::init();
42
43        Settings::register();
44
45        // Wire the RSS feed customizations (`<itunes:*>` + `<podcast:*>` tags,
46        // stats-tracked enclosure URLs) for the configured podcast category.
47        Customize_Feed::init();
48
49        Tracks::init();
50
51        Admin_Page::init();
52
53        if ( is_admin() ) {
54            New_Episode_Prefill::init();
55        }
56
57        $host = new Host();
58        if ( $host->is_wpcom_simple() || $host->is_woa_site() ) {
59            // Register the local REST routes before request-local rollout gates.
60            // Requests from public-api.wordpress.com may not satisfy those gates,
61            // but the wpcom/v2 routes still need to exist so permission and
62            // callback checks can handle the request.
63            Posts_To_Podcast_Endpoint::init();
64
65            // Posts to Podcast lives behind its own filter so the Create AI
66            // Podcast page can ship independently of the rest of the package.
67            //
68            // Note: no `is_admin()` guard here. The submenu must also register when
69            // Calypso builds its nav via the `wpcom/v2/admin-menu` REST endpoint,
70            // which fires `admin_menu` by loading `wp-admin/menu.php` but runs as a
71            // REST request where `is_admin()` is false. The hooks `init()` wires
72            // (`admin_menu`, `enqueue_block_editor_assets`) self-gate, so this is a
73            // no-op on non-admin/non-editor requests.
74            if ( self::is_posts_to_podcast_enabled() ) {
75                Create_AI_Podcast_Page::init();
76            }
77        }
78    }
79
80    /**
81     * Whether the Posts to Podcast feature (Create AI Podcast page + REST
82     * proxy) is enabled for the current request.
83     *
84     * Defaults to true for connected WordPress.com users, and can be flipped
85     * globally via the `jetpack_posts_to_podcast` filter.
86     */
87    public static function is_posts_to_podcast_enabled() {
88        /**
89         * Master switch for the Posts to Podcast (Create AI Podcast) feature.
90         *
91         * @since 0.1.0
92         *
93         * @param bool $enabled Whether to enable Posts to Podcast.
94         */
95        $enabled = self::is_user_connected( get_current_user_id() );
96
97        return (bool) apply_filters( 'jetpack_posts_to_podcast', $enabled );
98    }
99
100    /**
101     * Whether a user is connected to WordPress.com.
102     *
103     * @param int $user_id User ID.
104     * @return bool
105     */
106    private static function is_user_connected( $user_id ) {
107        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
108            return true;
109        }
110
111        return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id );
112    }
113}