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