| @@ -1,8 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Pinterest Block. |
| 4 | 4 | * |
| 5 | + * Note: this block is no longer available to be added to new posts. | |
| 6 | + * It is only kept available for existing posts with Pinterest blocks. | |
| 7 | + * You can still embed Pinterest content using the embed method provided by WordPress itself. | |
| 8 | + * | |
| 5 | 9 | * @since 8.0.0 |
| 6 | 10 | * |
| 7 | 11 | * @package automattic/jetpack |
| 8 | 12 | */ |
| @@ -9,13 +13,16 @@ | ||
| 9 | 13 | |
| 10 | 14 | namespace Automattic\Jetpack\Extensions\Pinterest; |
| 11 | 15 | |
| 12 | 16 | use Automattic\Jetpack\Blocks; |
| 17 | +use Automattic\Jetpack\Status\Request; | |
| 13 | 18 | use WP_Error; |
| 14 | 19 | |
| 15 | -const FEATURE_NAME = 'pinterest'; | |
| 16 | -const BLOCK_NAME = 'jetpack/' . FEATURE_NAME; | |
| 17 | -const URL_PATTERN = '#^https?://(?:www\.)?(?:[a-z]{2}\.)?pinterest\.[a-z.]+/pin/(?P<pin_id>[^/]+)/?#i'; // Taken from AMP plugin, originally from Jetpack. | |
| 20 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | + exit( 0 ); | |
| 22 | +} | |
| 23 | + | |
| 24 | +const URL_PATTERN = '#^https?://(?:www\.)?(?:[a-z]{2}\.)?pinterest\.[a-z.]+/pin/(?P<pin_id>[^/]+)/?#i'; // Taken from AMP plugin, originally from Jetpack. | |
| 18 | 25 | // This is the validate Pinterest URLs, converted from URL_REGEX in extensions/blocks/pinterest/index.js. |
| 19 | 26 | const PINTEREST_URL_REGEX = '/^https?:\/\/(?:www\.)?(?:[a-z]{2}\.)?(?:pinterest\.[a-z.]+|pin\.it)\/([^\/]+)(\/[^\/]+)?/i'; |
| 20 | 27 | // This looks for matches in /foo/ of https://www.pinterest.ca/foo/. |
| 21 | 28 | const REMAINING_URL_PATH_REGEX = '/^\/([^\/]+)\/?$/'; |
| @@ -25,9 +32,9 @@ | ||
| 25 | 32 | /** |
| 26 | 33 | * Determines the Pinterest embed type from the URL. |
| 27 | 34 | * |
| 28 | 35 | * @param string $url the URL to check. |
| 29 | - * @returns {string} The pin type. Empty string if it isn't a valid Pinterest URL. | |
| 36 | + * @return string The pin type. Empty string if it isn't a valid Pinterest URL. | |
| 30 | 37 | */ |
| 31 | 38 | function pin_type( $url ) { |
| 32 | 39 | if ( null === $url || ! preg_match( PINTEREST_URL_REGEX, $url ) ) { |
| 33 | 40 | return ''; |
| @@ -38,9 +45,9 @@ | ||
| 38 | 45 | if ( ! $path ) { |
| 39 | 46 | return ''; |
| 40 | 47 | } |
| 41 | 48 | |
| 42 | - if ( substr( $path, 0, 5 ) === '/pin/' ) { | |
| 49 | + if ( str_starts_with( $path, '/pin/' ) ) { | |
| 43 | 50 | return 'embedPin'; |
| 44 | 51 | } |
| 45 | 52 | |
| 46 | 53 | if ( preg_match( REMAINING_URL_PATH_REGEX, $path ) ) { |
| @@ -60,9 +67,9 @@ | ||
| 60 | 67 | * registration if we need to. |
| 61 | 68 | */ |
| 62 | 69 | function register_block() { |
| 63 | 70 | Blocks::jetpack_register_block( |
| 64 | - BLOCK_NAME, | |
| 71 | + __DIR__, | |
| 65 | 72 | array( 'render_callback' => __NAMESPACE__ . '\load_assets' ) |
| 66 | 73 | ); |
| 67 | 74 | } |
| 68 | 75 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| @@ -139,10 +146,10 @@ | ||
| 139 | 146 | } |
| 140 | 147 | |
| 141 | 148 | if ( is_array( $info ) ) { |
| 142 | 149 | $image = $info['images']['237x']; |
| 143 | - $title = isset( $info['rich_metadata']['title'] ) ? $info['rich_metadata']['title'] : null; | |
| 144 | - $description = isset( $info['rich_metadata']['description'] ) ? $info['rich_metadata']['description'] : null; | |
| 150 | + $title = $info['rich_metadata']['title'] ?? null; | |
| 151 | + $description = $info['rich_metadata']['description'] ?? null; | |
| 145 | 152 | |
| 146 | 153 | // This placeholder will appear while waiting for the amp-pinterest component to initialize (or if it fails to initialize due to JS being disabled). |
| 147 | 154 | $placeholder = sprintf( |
| 148 | 155 | // The AMP_Img_Sanitizer will convert his to <amp-img> while also supplying `noscript > img` as fallback when JS is disabled. |
| @@ -217,11 +224,12 @@ | ||
| 217 | 224 | * |
| 218 | 225 | * @return string |
| 219 | 226 | */ |
| 220 | 227 | function load_assets( $attr, $content ) { |
| 221 | - if ( ! jetpack_is_frontend() ) { | |
| 228 | + if ( ! Request::is_frontend() ) { | |
| 222 | 229 | return $content; |
| 223 | 230 | } |
| 231 | + $attr['url'] ??= ''; | |
| 224 | 232 | if ( Blocks::is_amp_request() ) { |
| 225 | 233 | return render_amp_pin( $attr ); |
| 226 | 234 | } else { |
| 227 | 235 | $url = $attr['url']; |
| @@ -237,9 +245,9 @@ | ||
| 237 | 245 | <div class="%1$s"> |
| 238 | 246 | <a data-pin-do="%2$s" href="%3$s"></a> |
| 239 | 247 | </div> |
| 240 | 248 | ', |
| 241 | - esc_attr( Blocks::classes( FEATURE_NAME, $attr ) ), | |
| 249 | + esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), | |
| 242 | 250 | esc_attr( $type ), |
| 243 | 251 | esc_url( $url ) |
| 244 | 252 | ); |
| 245 | 253 | } |