Completed
Push — update/trascription-dev-branch ( e410a3...6eafef )
by
unknown
94:40 queued 85:56
created

anchor-fm.php ➔ process_anchor_params()   F

Complexity

Conditions 19
Paths 243

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 243
nop 0
dl 0
loc 69
rs 3.1458
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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_Podcast_Helper;
15
16
const FEATURE_NAME = 'anchor-fm';
17
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
18
19
if ( ! class_exists( 'Jetpack_Podcast_Helper' ) ) {
20
	\jetpack_require_lib( 'class-jetpack-podcast-helper' );
21
}
22
23
/**
24
 * Registers Anchor.fm integration for the block editor.
25
 */
26
function register_block() {
27
	Blocks::jetpack_register_block( BLOCK_NAME );
28
29
	// Register post_meta for connecting Anchor podcasts with posts.
30
	register_post_meta(
31
		'post',
32
		'jetpack_anchor_podcast',
33
		array(
34
			'show_in_rest' => true,
35
			'single'       => true,
36
			'type'         => 'string',
37
		)
38
	);
39
	register_post_meta(
40
		'post',
41
		'jetpack_anchor_episode',
42
		array(
43
			'show_in_rest' => true,
44
			'single'       => true,
45
			'type'         => 'string',
46
		)
47
	);
48
49
	register_post_meta(
50
		'post',
51
		'jetpack_anchor_track',
52
		array(
53
			'show_in_rest' => true,
54
			'single'       => true,
55
			'type'         => 'string',
56
		)
57
	);
58
59
	register_post_meta(
60
		'post',
61
		'jetpack_anchor_spotify_show',
62
		array(
63
			'show_in_rest' => true,
64
			'single'       => true,
65
			'type'         => 'string',
66
		)
67
	);
68
}
69
70
/**
71
 * Checks URL params to determine the Anchor integration action to perform.
72
 */
73
function process_anchor_params() {
74
	if (
75
		! function_exists( 'get_current_screen' )
76
		|| is_null( \get_current_screen() )
77
	) {
78
		return;
79
	}
80
81
	$current_screen = \get_current_screen();
82
	// TODO: Replace `$current_screen->is_block_editor()` with `wp_should_load_block_editor_scripts_and_styles()` that is introduced in WP 5.6.
83
	if ( method_exists( $current_screen, 'is_block_editor' ) && ! $current_screen->is_block_editor() ) {
84
		// Return early if we are not in the block editor.
85
		return;
86
	}
87
88
	$post = get_post();
89
	if ( ! $post || ! $post->ID ) {
90
		return;
91
	}
92
93
	// phpcs:disable WordPress.Security.NonceVerification.Recommended
94
	$podcast_id       = isset( $_GET['anchor_podcast'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_podcast'] ) ) : null;
95
	$episode_id       = isset( $_GET['anchor_episode'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_episode'] ) ) : null;
96
	$spotify_show_url = isset( $_GET['spotify_show_url'] ) ? esc_url_raw( wp_unslash( $_GET['spotify_show_url'] ) ) : null;
97
	// phpcs:enable WordPress.Security.NonceVerification.Recommended
98
99
	$data = array();
100
101
	if ( ! empty( $podcast_id ) ) {
102
		$feed           = 'https://anchor.fm/s/' . $podcast_id . '/podcast/rss';
103
		$podcast_helper = new Jetpack_Podcast_Helper( $feed );
104
		$rss            = $podcast_helper->load_feed();
105
		if ( ! \is_wp_error( $rss ) ) {
106
			$data['podcastId'] = $podcast_id;
107
			update_post_meta( $post->ID, 'jetpack_anchor_podcast', $podcast_id );
108
109
			if ( ! empty( $episode_id ) ) {
110
				$track = $podcast_helper->get_track_data( $episode_id );
111
				if ( ! \is_wp_error( $track ) ) {
112
					$data['episodeId'] = $episode_id;
113
					$data['track']     = $track;
114
					update_post_meta( $post->ID, 'jetpack_anchor_episode', $episode_id );
115
					update_post_meta( $post->ID, 'jetpack_anchor_track', json_encode( $track ) );
116
				}
117
			}
118
		}
119
	}
120
121
	if ( ! empty( $spotify_show_url ) ) {
122
		$data['spotifyShowUrl'] = $spotify_show_url;
123
		if ( get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) !== $spotify_show_url ) {
124
			update_post_meta( $post->ID, 'jetpack_anchor_spotify_show', $spotify_show_url );
125
			$data['action'] = 'insert-spotify-badge';
126
			$data['image']  = Assets::staticize_subdomain( 'https://wordpress.com/i/spotify-badge.svg' );
127
		}
128
	}
129
130
	// Display an outbound link after publishing a post (only to English-speaking users since Anchor
131
	// is English only).
132
	if (
133
		'post' === get_post_type() &&
134
		! get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) &&
135
		0 === strpos( get_user_locale(), 'en' )
136
	) {
137
		$data['action'] = 'show-post-publish-outbound-link';
138
	}
139
140
	wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_AnchorFm', $data );
141
}
142
143
add_action( 'init', __NAMESPACE__ . '\register_block' );
144
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\process_anchor_params' );
145