1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Anchor.fm integration. |
4
|
|
|
* |
5
|
|
|
* @since 9.3.0 |
6
|
|
|
* |
7
|
|
|
* @package Jetpack |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Automattic\Jetpack\Extensions\AnchorFm; |
11
|
|
|
|
12
|
|
|
use Automattic\Jetpack\Assets; |
13
|
|
|
use Automattic\Jetpack\Blocks; |
14
|
|
|
use Jetpack_Gutenberg; |
15
|
|
|
use Jetpack_Podcast_Helper; |
16
|
|
|
|
17
|
|
|
const FEATURE_NAME = 'anchor-fm'; |
18
|
|
|
const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; |
19
|
|
|
|
20
|
|
|
if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) { |
21
|
|
|
\jetpack_require_lib( 'class-jetpack-podcast-helper' ); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Determine if the Anchor integration extension for the block editor is available. |
26
|
|
|
* |
27
|
|
|
* @return boolean Whether the extension is available |
28
|
|
|
*/ |
29
|
|
|
function is_extension_available() { |
30
|
|
|
/** |
31
|
|
|
* Filter the availability of the Anchor integration extension for the block editor. |
32
|
|
|
* |
33
|
|
|
* @since 9.3.0 |
34
|
|
|
* |
35
|
|
|
* @param boolean $is_available Whether the extension is available. |
36
|
|
|
*/ |
37
|
|
|
return apply_filters( 'jetpack_anchor_integration_availability', false ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the availability of the Anchor integration extension for the block editor. |
42
|
|
|
*/ |
43
|
|
|
function set_extension_availability() { |
44
|
|
|
if ( is_extension_available() ) { |
45
|
|
|
Jetpack_Gutenberg::set_extension_available( BLOCK_NAME ); |
46
|
|
|
} else { |
47
|
|
|
Jetpack_Gutenberg::set_extension_unavailable( BLOCK_NAME, 'Not supported' ); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Registers Anchor.fm integration for the block editor. |
53
|
|
|
*/ |
54
|
|
|
function register_extension() { |
55
|
|
|
if ( ! is_extension_available() ) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
Blocks::jetpack_register_block( BLOCK_NAME ); |
60
|
|
|
|
61
|
|
|
// Register post_meta for connecting Anchor podcasts with posts. |
62
|
|
|
register_post_meta( |
63
|
|
|
'post', |
64
|
|
|
'jetpack_anchor_podcast', |
65
|
|
|
array( |
66
|
|
|
'show_in_rest' => true, |
67
|
|
|
'single' => true, |
68
|
|
|
'type' => 'string', |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
register_post_meta( |
72
|
|
|
'post', |
73
|
|
|
'jetpack_anchor_episode', |
74
|
|
|
array( |
75
|
|
|
'show_in_rest' => true, |
76
|
|
|
'single' => true, |
77
|
|
|
'type' => 'string', |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
register_post_meta( |
81
|
|
|
'post', |
82
|
|
|
'jetpack_anchor_spotify_show', |
83
|
|
|
array( |
84
|
|
|
'show_in_rest' => true, |
85
|
|
|
'single' => true, |
86
|
|
|
'type' => 'string', |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Checks URL params to determine the Anchor integration action to perform. |
93
|
|
|
*/ |
94
|
|
|
function process_anchor_params() { |
95
|
|
|
if ( ! is_extension_available() ) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ( |
100
|
|
|
! function_exists( 'get_current_screen' ) |
101
|
|
|
|| is_null( \get_current_screen() ) |
102
|
|
|
) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$current_screen = \get_current_screen(); |
107
|
|
|
// TODO: Replace `$current_screen->is_block_editor()` with `wp_should_load_block_editor_scripts_and_styles()` that is introduced in WP 5.6. |
108
|
|
|
if ( method_exists( $current_screen, 'is_block_editor' ) && ! $current_screen->is_block_editor() ) { |
109
|
|
|
// Return early if we are not in the block editor. |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$post = get_post(); |
114
|
|
|
if ( ! $post || ! $post->ID ) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Recommended |
119
|
|
|
$podcast_id = isset( $_GET['anchor_podcast'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_podcast'] ) ) : null; |
120
|
|
|
$episode_id = isset( $_GET['anchor_episode'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_episode'] ) ) : null; |
121
|
|
|
$spotify_show_url = isset( $_GET['spotify_show_url'] ) ? esc_url_raw( wp_unslash( $_GET['spotify_show_url'] ) ) : null; |
122
|
|
|
// phpcs:enable WordPress.Security.NonceVerification.Recommended |
123
|
|
|
|
124
|
|
|
$data = array( |
125
|
|
|
'actions' => array(), |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
if ( ! empty( $podcast_id ) ) { |
129
|
|
|
$feed = 'https://anchor.fm/s/' . $podcast_id . '/podcast/rss'; |
130
|
|
|
$podcast_helper = new Jetpack_Podcast_Helper( $feed ); |
131
|
|
|
$rss = $podcast_helper->load_feed(); |
132
|
|
|
if ( ! \is_wp_error( $rss ) ) { |
133
|
|
|
update_post_meta( $post->ID, 'jetpack_anchor_podcast', $podcast_id ); |
134
|
|
|
|
135
|
|
|
if ( ! empty( $episode_id ) ) { |
136
|
|
|
$track = $podcast_helper->get_track_data( $episode_id ); |
137
|
|
|
if ( ! \is_wp_error( $track ) ) { |
138
|
|
|
update_post_meta( $post->ID, 'jetpack_anchor_episode', $episode_id ); |
139
|
|
|
|
140
|
|
|
if ( 'post-new.php' === $GLOBALS['pagenow'] ) { |
141
|
|
|
$data['actions'][] = array( |
142
|
|
|
'set-episode-title', |
143
|
|
|
array( |
144
|
|
|
'title' => $track['title'], |
145
|
|
|
), |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ( ! empty( $spotify_show_url ) ) { |
154
|
|
|
$data['spotifyShowUrl'] = $spotify_show_url; |
155
|
|
|
if ( get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) !== $spotify_show_url ) { |
156
|
|
|
update_post_meta( $post->ID, 'jetpack_anchor_spotify_show', $spotify_show_url ); |
157
|
|
|
$data['actions'][] = array( |
158
|
|
|
'insert-spotify-badge', |
159
|
|
|
array( |
160
|
|
|
'image' => Assets::staticize_subdomain( 'https://wordpress.com/i/spotify-badge.svg' ), |
161
|
|
|
'url' => $spotify_show_url, |
162
|
|
|
), |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Display an outbound link after publishing a post (only to English-speaking users since Anchor |
168
|
|
|
// is English only). |
169
|
|
|
if ( |
170
|
|
|
'post' === get_post_type() && |
171
|
|
|
! get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) && |
172
|
|
|
0 === strpos( get_user_locale(), 'en' ) |
173
|
|
|
) { |
174
|
|
|
$data['actions'][] = 'show-post-publish-outbound-link'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_AnchorFm', $data ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
add_action( 'init', __NAMESPACE__ . '\register_extension' ); |
181
|
|
|
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\process_anchor_params' ); |
182
|
|
|
add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\set_extension_availability' ); |
183
|
|
|
|