| 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.1.6'; |
| 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 |
} |
| 114 |
|