Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

pinterest.php ➔ pinterest_embed_handler()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 3
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
1
<?php
2
/**
3
 * Pinterest embeds
4
 *
5
 * Based on "Board Widget" example here: http://business.pinterest.com/widget-builder/#code
6
 *
7
 * Example URL: https://pinterest.com/pin/129056345550241149/
8
 * Second Example URL: https://uk.pinterest.com/annsawesomepins/travel/
9
 *
10
 * @package Jetpack
11
 */
12
13
wp_embed_register_handler(
14
	'pinterest',
15
	'#'
16
	. 'https?://'
17
	. '(?:www\.)?'
18
	. '(?:[a-z]{2}\.)?'
19
	. 'pinterest\.[a-z.]+/'
20
	. '([^/]+)'
21
	. '(/[^/]+)?'
22
	. '#',
23
	'pinterest_embed_handler'
24
);
25
26
/**
27
 * Callback to modify output of embedded Pinterest posts.
28
 *
29
 * @param array $matches Regex partial matches against the URL passed.
30
 * @param array $attr    Attributes received in embed response.
31
 * @param array $url     Requested URL to be embedded.
32
 */
33
function pinterest_embed_handler( $matches, $attr, $url ) {
34
	// Pinterest's JS handles making the embed.
35
	$script_src = '//assets.pinterest.com/js/pinit.js';
36
37
	wp_enqueue_script( 'pinterest-embed', $script_src, array(), JETPACK__VERSION, true );
38
39
	$path = wp_parse_url( $url, PHP_URL_PATH );
0 ignored issues
show
Unused Code introduced by
The call to wp_parse_url() has too many arguments starting with PHP_URL_PATH.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
40
	if ( 0 === strpos( $path, '/pin/' ) ) {
41
		$embed_type = 'embedPin';
42
	} elseif ( preg_match( '#^/([^/]+)/?$#', $path ) ) {
43
		$embed_type = 'embedUser';
44
	} elseif ( preg_match( '#^/([^/]+)/([^/]+)/?$#', $path ) ) {
45
		$embed_type = 'embedBoard';
46
	} else {
47
		if ( current_user_can( 'edit_posts' ) ) {
48
			return __( 'Sorry, that Pinterest URL was not recognized.', 'jetpack' );
49
		}
50
		return;
51
	}
52
53
	$return = sprintf( '<a data-pin-do="%s" href="%s"></a>', esc_attr( $embed_type ), esc_url( $url ) );
54
55
	// If we're generating an embed view for the WordPress Admin via ajax.
56
	if ( doing_action( 'wp_ajax_parse-embed' ) ) {
57
		$return .= sprintf(
58
			'<script src="%s"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
59
			esc_url( $script_src )
60
		);
61
	}
62
63
	return $return;
64
}
65