Completed
Push — try/anchor-fm-add-badge-in-bac... ( d86d4c )
by
unknown
94:26 queued 85:50
created

anchor-fm.php ➔ process_anchor_params()   D

Complexity

Conditions 18
Paths 99

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
nc 99
nop 0
dl 0
loc 67
rs 4.8666
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_extension() {
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
	register_post_meta(
49
		'post',
50
		'jetpack_anchor_spotify_show',
51
		array(
52
			'show_in_rest' => true,
53
			'single'       => true,
54
			'type'         => 'string',
55
		)
56
	);
57
58
	// phpcs:disable WordPress.Security.NonceVerification.Recommended
59
	$podcast_id       = isset( $_GET['anchor_podcast'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_podcast'] ) ) : null;
0 ignored issues
show
Unused Code introduced by
$podcast_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
	$episode_id       = isset( $_GET['anchor_episode'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_episode'] ) ) : null;
0 ignored issues
show
Unused Code introduced by
$episode_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
	$spotify_show_url = isset( $_GET['spotify_show_url'] ) ? esc_url_raw( wp_unslash( $_GET['spotify_show_url'] ) ) : null;
62
	// phpcs:enable WordPress.Security.NonceVerification.Recommended
63
64
	$template_blocks = array();
65
66
	if ( ! empty( $spotify_show_url ) ) {
67
		$data['spotifyShowUrl'] = $spotify_show_url;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
68
69
			array_push( $template_blocks,
70
				array( 'core/image', array(
71
					'url'             => Assets::staticize_subdomain( 'https://wordpress.com/i/spotify-badge.svg' ),
72
					'linkDestination' => 'none',
73
					'href'            => $spotify_show_url,
74
					'align'           => 'center',
75
					'width'           => 165,
76
					'height'          => 40,
77
					'className'       => 'is-spotify-podcast-badge',
78
				)
79
			) );
80
	}
81
	
82
	$post_type_object = get_post_type_object( 'post' );
83
	$post_type_object->template = $template_blocks;
84
}
85
86
/**
87
 * Checks URL params to determine the Anchor integration action to perform.
88
 */
89
function process_anchor_params() {
90
	if (
91
		! function_exists( 'get_current_screen' )
92
		|| is_null( \get_current_screen() )
93
	) {
94
		return;
95
	}
96
97
	$current_screen = \get_current_screen();
98
	// TODO: Replace `$current_screen->is_block_editor()` with `wp_should_load_block_editor_scripts_and_styles()` that is introduced in WP 5.6.
99
	if ( method_exists( $current_screen, 'is_block_editor' ) && ! $current_screen->is_block_editor() ) {
100
		// Return early if we are not in the block editor.
101
		return;
102
	}
103
104
	$post = get_post();
105
	if ( ! $post || ! $post->ID ) {
106
		return;
107
	}
108
109
	// phpcs:disable WordPress.Security.NonceVerification.Recommended
110
	$podcast_id       = isset( $_GET['anchor_podcast'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_podcast'] ) ) : null;
111
	$episode_id       = isset( $_GET['anchor_episode'] ) ? sanitize_text_field( wp_unslash( $_GET['anchor_episode'] ) ) : null;
112
	$spotify_show_url = isset( $_GET['spotify_show_url'] ) ? esc_url_raw( wp_unslash( $_GET['spotify_show_url'] ) ) : null;
0 ignored issues
show
Unused Code introduced by
$spotify_show_url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
	// phpcs:enable WordPress.Security.NonceVerification.Recommended
114
115
	$data = array(
116
		'actions' => array(),
117
	);
118
119
	if ( ! empty( $podcast_id ) ) {
120
		$feed           = 'https://anchor.fm/s/' . $podcast_id . '/podcast/rss';
121
		$podcast_helper = new Jetpack_Podcast_Helper( $feed );
122
		$rss            = $podcast_helper->load_feed();
123
		if ( ! \is_wp_error( $rss ) ) {
124
			update_post_meta( $post->ID, 'jetpack_anchor_podcast', $podcast_id );
125
126
			if ( ! empty( $episode_id ) ) {
127
				$track = $podcast_helper->get_track_data( $episode_id );
128
				if ( ! \is_wp_error( $track ) ) {
129
					update_post_meta( $post->ID, 'jetpack_anchor_episode', $episode_id );
130
131
					if ( 'post-new.php' === $GLOBALS['pagenow'] ) {
132
						$data['actions'][] = array(
133
							'set-episode-title',
134
							array(
135
								'title' => $track['title'],
136
							),
137
						);
138
					}
139
				}
140
			}
141
		}
142
	}
143
144
	// Display an outbound link after publishing a post (only to English-speaking users since Anchor
145
	// is English only).
146
	if (
147
		'post' === get_post_type() &&
148
		! get_post_meta( $post->ID, 'jetpack_anchor_spotify_show', true ) &&
149
		0 === strpos( get_user_locale(), 'en' )
150
	) {
151
		$data['actions'][] = 'show-post-publish-outbound-link';
152
	}
153
154
	wp_localize_script( 'jetpack-blocks-editor', 'Jetpack_AnchorFm', $data );
155
}
156
157
add_action( 'init', __NAMESPACE__ . '\register_extension' );
158
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\process_anchor_params' );
159