| @@ -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,10 +13,15 @@ | ||
| 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 | |
| 20 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | + exit( 0 ); | |
| 22 | +} | |
| 23 | + | |
| 15 | 24 | const URL_PATTERN = '#^https?://(?:www\.)?(?:[a-z]{2}\.)?pinterest\.[a-z.]+/pin/(?P<pin_id>[^/]+)/?#i'; // Taken from AMP plugin, originally from Jetpack. |
| 16 | 25 | // This is the validate Pinterest URLs, converted from URL_REGEX in extensions/blocks/pinterest/index.js. |
| 17 | 26 | const PINTEREST_URL_REGEX = '/^https?:\/\/(?:www\.)?(?:[a-z]{2}\.)?(?:pinterest\.[a-z.]+|pin\.it)\/([^\/]+)(\/[^\/]+)?/i'; |
| 18 | 27 | // This looks for matches in /foo/ of https://www.pinterest.ca/foo/. |
| @@ -23,9 +32,9 @@ | ||
| 23 | 32 | /** |
| 24 | 33 | * Determines the Pinterest embed type from the URL. |
| 25 | 34 | * |
| 26 | 35 | * @param string $url the URL to check. |
| 27 | - * @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. | |
| 28 | 37 | */ |
| 29 | 38 | function pin_type( $url ) { |
| 30 | 39 | if ( null === $url || ! preg_match( PINTEREST_URL_REGEX, $url ) ) { |
| 31 | 40 | return ''; |
| @@ -137,10 +146,10 @@ | ||
| 137 | 146 | } |
| 138 | 147 | |
| 139 | 148 | if ( is_array( $info ) ) { |
| 140 | 149 | $image = $info['images']['237x']; |
| 141 | - $title = isset( $info['rich_metadata']['title'] ) ? $info['rich_metadata']['title'] : null; | |
| 142 | - $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; | |
| 143 | 152 | |
| 144 | 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). |
| 145 | 154 | $placeholder = sprintf( |
| 146 | 155 | // The AMP_Img_Sanitizer will convert his to <amp-img> while also supplying `noscript > img` as fallback when JS is disabled. |
| @@ -215,11 +224,12 @@ | ||
| 215 | 224 | * |
| 216 | 225 | * @return string |
| 217 | 226 | */ |
| 218 | 227 | function load_assets( $attr, $content ) { |
| 219 | - if ( ! jetpack_is_frontend() ) { | |
| 228 | + if ( ! Request::is_frontend() ) { | |
| 220 | 229 | return $content; |
| 221 | 230 | } |
| 231 | + $attr['url'] ??= ''; | |
| 222 | 232 | if ( Blocks::is_amp_request() ) { |
| 223 | 233 | return render_amp_pin( $attr ); |
| 224 | 234 | } else { |
| 225 | 235 | $url = $attr['url']; |