Нет описания

class.jetpack-amp-support.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. use Automattic\Jetpack\Assets;
  3. use Automattic\Jetpack\Sync\Functions;
  4. /**
  5. * Manages compatibility with the amp-wp plugin
  6. *
  7. * @see https://github.com/Automattic/amp-wp
  8. */
  9. class Jetpack_AMP_Support {
  10. /**
  11. * Apply custom AMP changes on the front-end.
  12. */
  13. public static function init() {
  14. // Add Stats tracking pixel on Jetpack sites when the Stats module is active.
  15. if (
  16. Jetpack::is_module_active( 'stats' )
  17. && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
  18. ) {
  19. add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
  20. }
  21. /**
  22. * Remove this during the init hook in case users have enabled it during
  23. * the after_setup_theme hook, which triggers before init.
  24. */
  25. remove_theme_support( 'jetpack-devicepx' );
  26. // Sharing.
  27. add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
  28. add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) );
  29. add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) );
  30. // Sharing for Reader mode.
  31. if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) {
  32. add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' );
  33. }
  34. add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 );
  35. // enforce freedom mode for videopress.
  36. add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
  37. // include Jetpack og tags when rendering native AMP head.
  38. add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
  39. // Post rendering changes for legacy AMP.
  40. add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
  41. // Disable Comment Likes.
  42. add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
  43. // Transitional mode AMP should not have comment likes.
  44. add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );
  45. // Remove the Likes button from the admin bar.
  46. add_filter( 'jetpack_admin_bar_likes_enabled', array( 'Jetpack_AMP_Support', 'disable_likes_admin_bar' ) );
  47. // Add post template metadata for legacy AMP.
  48. add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
  49. // Filter photon image args for AMP Stories.
  50. add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );
  51. // Sync the amp-options.
  52. add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) );
  53. }
  54. /**
  55. * Disable the Comment Likes feature on AMP views.
  56. *
  57. * @param bool $enabled Should comment likes be enabled.
  58. */
  59. public static function comment_likes_enabled( $enabled ) {
  60. return $enabled && ! self::is_amp_request();
  61. }
  62. /**
  63. * Apply custom AMP changes in wp-admin.
  64. */
  65. public static function admin_init() {
  66. // disable Likes metabox for post editor if AMP canonical disabled.
  67. add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
  68. }
  69. /**
  70. * Is the page in AMP 'canonical mode'.
  71. * Used when themes register support for AMP with `add_theme_support( 'amp' )`.
  72. *
  73. * @return bool is_amp_canonical
  74. */
  75. public static function is_amp_canonical() {
  76. return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
  77. }
  78. /**
  79. * Is AMP available for this request
  80. * This returns false for admin, CLI requests etc.
  81. *
  82. * @return bool is_amp_available
  83. */
  84. public static function is_amp_available() {
  85. return ( function_exists( 'amp_is_available' ) && amp_is_available() );
  86. }
  87. /**
  88. * Does the page return AMP content.
  89. *
  90. * @return bool $is_amp_request Are we on am AMP view.
  91. */
  92. public static function is_amp_request() {
  93. $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
  94. /**
  95. * Returns true if the current request should return valid AMP content.
  96. *
  97. * @since 6.2.0
  98. *
  99. * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
  100. */
  101. return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
  102. }
  103. /**
  104. * Remove content filters added by Jetpack.
  105. */
  106. public static function amp_disable_the_content_filters() {
  107. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  108. add_filter( 'videopress_show_2015_player', '__return_true' );
  109. add_filter( 'protected_embeds_use_form_post', '__return_false' );
  110. remove_filter( 'the_title', 'widont' );
  111. }
  112. remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
  113. remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
  114. }
  115. /**
  116. * Do not add comment likes on AMP requests.
  117. *
  118. * @param string $content Post content.
  119. */
  120. public static function disable_comment_likes_before_the_content( $content ) {
  121. if ( self::is_amp_request() ) {
  122. remove_filter( 'comment_text', 'comment_like_button', 12, 2 );
  123. }
  124. return $content;
  125. }
  126. /**
  127. * Do not display the Likes' Admin bar on AMP requests.
  128. *
  129. * @param bool $is_admin_bar_button_visible Should the Like button be visible in the Admin bar. Default to true.
  130. */
  131. public static function disable_likes_admin_bar( $is_admin_bar_button_visible ) {
  132. if ( self::is_amp_request() ) {
  133. return false;
  134. }
  135. return $is_admin_bar_button_visible;
  136. }
  137. /**
  138. * Add Jetpack stats pixel.
  139. *
  140. * @since 6.2.1
  141. */
  142. public static function add_stats_pixel() {
  143. if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
  144. return;
  145. }
  146. stats_render_amp_footer( stats_build_view_data() );
  147. }
  148. /**
  149. * Add publisher and image metadata to legacy AMP post.
  150. *
  151. * @since 6.2.0
  152. *
  153. * @param array $metadata Metadata array.
  154. * @param WP_Post $post Post.
  155. * @return array Modified metadata array.
  156. */
  157. public static function amp_post_template_metadata( $metadata, $post ) {
  158. if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
  159. $metadata = self::add_site_icon_to_metadata( $metadata );
  160. }
  161. if ( ! isset( $metadata['image'] ) && ! empty( $post ) ) {
  162. $metadata = self::add_image_to_metadata( $metadata, $post );
  163. }
  164. return $metadata;
  165. }
  166. /**
  167. * Add blavatar to legacy AMP post metadata.
  168. *
  169. * @since 6.2.0
  170. *
  171. * @param array $metadata Metadata.
  172. *
  173. * @return array Metadata.
  174. */
  175. private static function add_site_icon_to_metadata( $metadata ) {
  176. $size = 60;
  177. $site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : '';
  178. if ( function_exists( 'blavatar_domain' ) ) {
  179. $metadata['publisher']['logo'] = array(
  180. '@type' => 'ImageObject',
  181. 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
  182. 'width' => $size,
  183. 'height' => $size,
  184. );
  185. } elseif ( $site_icon_url ) {
  186. $metadata['publisher']['logo'] = array(
  187. '@type' => 'ImageObject',
  188. 'url' => $site_icon_url,
  189. 'width' => $size,
  190. 'height' => $size,
  191. );
  192. }
  193. return $metadata;
  194. }
  195. /**
  196. * Add image to legacy AMP post metadata.
  197. *
  198. * @since 6.2.0
  199. *
  200. * @param array $metadata Metadata.
  201. * @param WP_Post $post Post.
  202. * @return array Metadata.
  203. */
  204. private static function add_image_to_metadata( $metadata, $post ) {
  205. $image = Jetpack_PostImages::get_image(
  206. $post->ID,
  207. array(
  208. 'fallback_to_avatars' => true,
  209. 'avatar_size' => 200,
  210. // AMP already attempts these.
  211. 'from_thumbnail' => false,
  212. 'from_attachment' => false,
  213. )
  214. );
  215. if ( empty( $image ) ) {
  216. return self::add_fallback_image_to_metadata( $metadata );
  217. }
  218. if ( ! isset( $image['src_width'] ) ) {
  219. $dimensions = self::extract_image_dimensions_from_getimagesize(
  220. array(
  221. $image['src'] => false,
  222. )
  223. );
  224. if ( false !== $dimensions[ $image['src'] ] ) {
  225. $image['src_width'] = $dimensions['width'];
  226. $image['src_height'] = $dimensions['height'];
  227. }
  228. }
  229. $metadata['image'] = array(
  230. '@type' => 'ImageObject',
  231. 'url' => $image['src'],
  232. );
  233. if ( isset( $image['src_width'] ) ) {
  234. $metadata['image']['width'] = $image['src_width'];
  235. }
  236. if ( isset( $image['src_width'] ) ) {
  237. $metadata['image']['height'] = $image['src_height'];
  238. }
  239. return $metadata;
  240. }
  241. /**
  242. * Add fallback image to legacy AMP post metadata.
  243. *
  244. * @since 6.2.0
  245. *
  246. * @param array $metadata Metadata.
  247. * @return array Metadata.
  248. */
  249. private static function add_fallback_image_to_metadata( $metadata ) {
  250. /** This filter is documented in functions.opengraph.php */
  251. $default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
  252. $metadata['image'] = array(
  253. '@type' => 'ImageObject',
  254. 'url' => self::staticize_subdomain( $default_image ),
  255. 'width' => 200,
  256. 'height' => 200,
  257. );
  258. return $metadata;
  259. }
  260. /**
  261. * Return static WordPress.com domain to use to load resources from WordPress.com.
  262. *
  263. * @param string $domain Asset URL.
  264. */
  265. private static function staticize_subdomain( $domain ) {
  266. // deal with WPCOM vs Jetpack.
  267. if ( function_exists( 'staticize_subdomain' ) ) {
  268. return staticize_subdomain( $domain );
  269. } else {
  270. return Assets::staticize_subdomain( $domain );
  271. }
  272. }
  273. /**
  274. * Extract image dimensions via wpcom/imagesize, only on WPCOM
  275. *
  276. * @since 6.2.0
  277. *
  278. * @param array $dimensions Dimensions.
  279. * @return array Dimensions.
  280. */
  281. private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
  282. if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'jetpack_require_lib' ) ) ) {
  283. return $dimensions;
  284. }
  285. jetpack_require_lib( 'wpcom/imagesize' );
  286. foreach ( $dimensions as $url => $value ) {
  287. if ( is_array( $value ) ) {
  288. continue;
  289. }
  290. $result = wpcom_getimagesize( $url );
  291. if ( is_array( $result ) ) {
  292. $dimensions[ $url ] = array(
  293. 'width' => $result[0],
  294. 'height' => $result[1],
  295. );
  296. }
  297. }
  298. return $dimensions;
  299. }
  300. /**
  301. * Display Open Graph Meta tags in AMP views.
  302. */
  303. public static function amp_post_jetpack_og_tags() {
  304. if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
  305. Jetpack::init()->check_open_graph();
  306. }
  307. if ( function_exists( 'jetpack_og_tags' ) ) {
  308. jetpack_og_tags();
  309. }
  310. }
  311. /**
  312. * Force Freedom mode in VideoPress.
  313. *
  314. * @param array $options Array of VideoPress shortcode options.
  315. */
  316. public static function videopress_enable_freedom_mode( $options ) {
  317. if ( self::is_amp_request() ) {
  318. $options['freedom'] = true;
  319. }
  320. return $options;
  321. }
  322. /**
  323. * Display custom markup for the sharing buttons when in an AMP view.
  324. *
  325. * @param string $markup Content markup of the Jetpack sharing links.
  326. * @param array $sharing_enabled Array of Sharing Services currently enabled.
  327. */
  328. public static function render_sharing_html( $markup, $sharing_enabled ) {
  329. global $post;
  330. if ( empty( $post ) ) {
  331. return '';
  332. }
  333. if ( ! self::is_amp_request() ) {
  334. return $markup;
  335. }
  336. remove_action( 'wp_footer', 'sharing_add_footer' );
  337. if ( empty( $sharing_enabled ) ) {
  338. return $markup;
  339. }
  340. $sharing_links = array();
  341. foreach ( $sharing_enabled['visible'] as $service ) {
  342. $sharing_link = $service->get_amp_display( $post );
  343. if ( ! empty( $sharing_link ) ) {
  344. $sharing_links[] = $sharing_link;
  345. }
  346. }
  347. // Replace the existing unordered list with AMP sharing buttons.
  348. $markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup );
  349. // Remove any lingering share-end list items.
  350. $markup = str_replace( '<li class="share-end"></li>', '', $markup );
  351. return $markup;
  352. }
  353. /**
  354. * Tells Jetpack not to enqueue CSS for share buttons.
  355. *
  356. * @param bool $enqueue Whether or not to enqueue.
  357. * @return bool Whether or not to enqueue.
  358. */
  359. public static function amp_disable_sharedaddy_css( $enqueue ) {
  360. if ( self::is_amp_request() ) {
  361. $enqueue = false;
  362. }
  363. return $enqueue;
  364. }
  365. /**
  366. * Enqueues the AMP specific sharing styles for the sharing icons.
  367. */
  368. public static function amp_enqueue_sharing_css() {
  369. if ( self::is_amp_request() ) {
  370. wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( __DIR__ ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION );
  371. }
  372. }
  373. /**
  374. * For the AMP Reader mode template, include styles that we need.
  375. */
  376. public static function amp_reader_sharing_css() {
  377. // If sharing is not enabled, we should not proceed to render the CSS.
  378. if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) | ! defined( 'JETPACK_SOCIAL_LOGOS_URL' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) {
  379. return;
  380. }
  381. /*
  382. * We'll need to output the full contents of the 2 files
  383. * in the head on AMP views. We can't rely on regular enqueues here.
  384. * @todo As of AMP plugin v1.5, you can actually rely on regular enqueues thanks to https://github.com/ampproject/amp-wp/pull/4299. Once WPCOM upgrades AMP, then this method can be eliminated.
  385. *
  386. * phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
  387. * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
  388. */
  389. $css = file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' );
  390. $css = preg_replace( '#(?<=url\(")(?=social-logos\.)#', JETPACK_SOCIAL_LOGOS_URL, $css ); // Make sure font files get their absolute paths.
  391. echo $css;
  392. echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' );
  393. /*
  394. * phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
  395. * phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
  396. */
  397. }
  398. /**
  399. * Ensure proper Photon image dimensions for AMP Stories.
  400. *
  401. * @param array $args Array of Photon Arguments.
  402. * @param array $details {
  403. * Array of image details.
  404. *
  405. * @type string $tag Image tag (Image HTML output).
  406. * @type string $src Image URL.
  407. * @type string $src_orig Original Image URL.
  408. * @type int|false $width Image width.
  409. * @type int|false $height Image height.
  410. * @type int|false $width_orig Original image width before constrained by content_width.
  411. * @type int|false $height_orig Original Image height before constrained by content_width.
  412. * @type string $transform_orig Original transform before constrained by content_width.
  413. * }
  414. * @return array Args.
  415. */
  416. public static function filter_photon_post_image_args_for_stories( $args, $details ) {
  417. if ( ! is_singular( 'amp_story' ) ) {
  418. return $args;
  419. }
  420. // Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
  421. if ( false !== strpos( $details['width_orig'], '%' ) || false !== strpos( $details['height_orig'], '%' ) ) {
  422. return $args;
  423. }
  424. $max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
  425. $transform = $details['transform_orig'];
  426. $width = $details['width_orig'];
  427. $height = $details['height_orig'];
  428. // If height is available, constrain to $max_height.
  429. if ( false !== $height ) {
  430. if ( $height > $max_height && false !== $height ) {
  431. $width = ( $max_height * $width ) / $height;
  432. $height = $max_height;
  433. } elseif ( $height > $max_height ) {
  434. $height = $max_height;
  435. }
  436. }
  437. /*
  438. * Set a height if none is found.
  439. * If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
  440. */
  441. if ( false === $height ) {
  442. $height = $max_height;
  443. if ( false !== $width ) {
  444. $transform = 'fit';
  445. }
  446. }
  447. // Build array of Photon args and expose to filter before passing to Photon URL function.
  448. $args = array();
  449. if ( false !== $width && false !== $height ) {
  450. $args[ $transform ] = $width . ',' . $height;
  451. } elseif ( false !== $width ) {
  452. $args['w'] = $width;
  453. } elseif ( false !== $height ) {
  454. $args['h'] = $height;
  455. }
  456. return $args;
  457. }
  458. /**
  459. * Adds amp-options to the list of options to sync, if AMP is available
  460. *
  461. * @param array $options_safelist Safelist of options to sync.
  462. *
  463. * @return array Updated options safelist
  464. */
  465. public static function filter_jetpack_options_safelist( $options_safelist ) {
  466. if ( function_exists( 'is_amp_endpoint' ) ) {
  467. $options_safelist[] = 'amp-options';
  468. }
  469. return $options_safelist;
  470. }
  471. }
  472. add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
  473. add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );