Completed
Push — issue/2347-pinterest-shortcode ( b0c3d8 )
by George
19:21 queued 05:34
created

pinterest.php ➔ pinterest_embed_handler()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 3
dl 0
loc 19
rs 8.8571
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
8
// Example URL: http://pinterest.com/pinterest/pin-pets/
9
// Second Example URL: https://uk.pinterest.com/annsawesomepins/travel/
10
wp_embed_register_handler( 'pinterest', '#https?://(?:www\.)?(?:[a-z]{2}\.)?pinterest\.com/([^/]+)(/[^/]+)?#', 'pinterest_embed_handler' );
11
12
function pinterest_embed_handler( $matches, $attr, $url ) {
0 ignored issues
show
Unused Code introduced by
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
13
	// Pinterest's JS handles making the embed
14
	wp_enqueue_script( 'pinterest-embed', '//assets.pinterest.com/js/pinit.js', array(), false, true );
15
16
	$path = parse_url( $url, PHP_URL_PATH );
17
	if ( 0 === strpos( $path, '/pin/' ) ) {
18
		$embed_type = 'embedPin';
19
	} elseif ( preg_match( '#^/([^/]+)/?$#', $path ) ) {
20
		$embed_type = 'embedUser';
21
	} elseif ( preg_match( '#^/([^/]+)/([^/]+)/?$#', $path ) ) {
22
		$embed_type = 'embedBoard';
23
	} else {
24
		if ( current_user_can( 'edit_posts' ) )
25
			return __( 'Sorry, that Pinterest URL was not recognized.', 'jetpack' );
26
		return;
27
	}
28
29
	return sprintf( '<a data-pin-do="%s" href="%s"></a>', esc_attr( $embed_type ), esc_url( $url ) );
30
}
31