class.jetpack-amp-support.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * Manages compatibility with the amp-wp plugin
  4. *
  5. * @see https://github.com/Automattic/amp-wp
  6. */
  7. class Jetpack_AMP_Support {
  8. static function init() {
  9. if ( ! self::is_amp_request() ) {
  10. return;
  11. }
  12. // enable stats
  13. if ( Jetpack::is_module_active( 'stats' ) ) {
  14. add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
  15. }
  16. // carousel
  17. add_filter( 'jp_carousel_maybe_disable', '__return_true' );
  18. // sharing
  19. add_filter( 'sharing_enqueue_scripts', '__return_false' );
  20. add_filter( 'jetpack_sharing_counts', '__return_false' );
  21. add_filter( 'sharing_js', '__return_false' );
  22. add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
  23. // disable lazy images
  24. add_filter( 'lazyload_is_enabled', '__return_false' );
  25. // disable imploding CSS
  26. add_filter( 'jetpack_implode_frontend_css', '__return_false' );
  27. // enforce freedom mode for videopress
  28. add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
  29. // include Jetpack og tags when rendering native AMP head
  30. add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
  31. // Post rendering changes for legacy AMP
  32. add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
  33. // Add post template metadata for legacy AMP
  34. add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
  35. }
  36. static function admin_init() {
  37. // disable Likes metabox for post editor if AMP canonical disabled
  38. add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
  39. }
  40. static function init_filter_jetpack_widgets() {
  41. if ( ! self::is_amp_request() ) {
  42. return;
  43. }
  44. // widgets
  45. add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) );
  46. }
  47. static function is_amp_canonical() {
  48. return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
  49. }
  50. static function is_amp_request() {
  51. // can't use is_amp_endpoint() since it's not ready early enough in init.
  52. // is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished
  53. // "Conditional query tags do not work before the query is run"
  54. $is_amp_request =
  55. defined( 'AMP__VERSION' )
  56. &&
  57. ! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin
  58. &&
  59. function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists
  60. &&
  61. (
  62. amp_is_canonical()
  63. ||
  64. isset( $_GET[ amp_get_slug() ] )
  65. ||
  66. ( version_compare( AMP__VERSION, '1.0', '<' ) && self::has_amp_suffix() ) // after AMP 1.0, the amp suffix will no longer be supported
  67. );
  68. /**
  69. * Returns true if the current request should return valid AMP content.
  70. *
  71. * @since 6.2.0
  72. *
  73. * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
  74. */
  75. return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
  76. }
  77. static function has_amp_suffix() {
  78. $request_path = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
  79. return $request_path && preg_match( '#/amp/?$#i', $request_path );
  80. }
  81. static function filter_available_widgets( $widgets ) {
  82. if ( self::is_amp_request() ) {
  83. $widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) );
  84. }
  85. return $widgets;
  86. }
  87. static function is_supported_widget( $widget_path ) {
  88. return substr( $widget_path, -14 ) !== '/milestone.php';
  89. }
  90. static function amp_disable_the_content_filters() {
  91. if ( defined( 'WPCOM') && WPCOM ) {
  92. add_filter( 'videopress_show_2015_player', '__return_true' );
  93. add_filter( 'protected_embeds_use_form_post', '__return_false' );
  94. remove_filter( 'the_title', 'widont' );
  95. }
  96. remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
  97. remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
  98. }
  99. /**
  100. * Add Jetpack stats pixel.
  101. *
  102. * @since 6.2.1
  103. */
  104. static function add_stats_pixel() {
  105. if ( ! has_action( 'wp_footer', 'stats_footer' ) ) {
  106. return;
  107. }
  108. stats_render_amp_footer( stats_build_view_data() );
  109. }
  110. /**
  111. * Add publisher and image metadata to legacy AMP post.
  112. *
  113. * @since 6.2.0
  114. *
  115. * @param array $metadata Metadata array.
  116. * @param WP_Post $post Post.
  117. * @return array Modified metadata array.
  118. */
  119. static function amp_post_template_metadata( $metadata, $post ) {
  120. if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
  121. $metadata = self::add_site_icon_to_metadata( $metadata );
  122. }
  123. if ( ! isset( $metadata['image'] ) ) {
  124. $metadata = self::add_image_to_metadata( $metadata, $post );
  125. }
  126. return $metadata;
  127. }
  128. /**
  129. * Add blavatar to legacy AMP post metadata.
  130. *
  131. * @since 6.2.0
  132. *
  133. * @param array $metadata Metadata.
  134. * @return array Metadata.
  135. */
  136. static function add_site_icon_to_metadata( $metadata ) {
  137. $size = 60;
  138. if ( function_exists( 'blavatar_domain' ) ) {
  139. $metadata['publisher']['logo'] = array(
  140. '@type' => 'ImageObject',
  141. 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
  142. 'width' => $size,
  143. 'height' => $size,
  144. );
  145. } else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) {
  146. $metadata['publisher']['logo'] = array(
  147. '@type' => 'ImageObject',
  148. 'url' => $site_icon_url,
  149. 'width' => $size,
  150. 'height' => $size,
  151. );
  152. }
  153. return $metadata;
  154. }
  155. /**
  156. * Add image to legacy AMP post metadata.
  157. *
  158. * @since 6.2.0
  159. *
  160. * @param array $metadata Metadata.
  161. * @param WP_Post $post Post.
  162. * @return array Metadata.
  163. */
  164. static function add_image_to_metadata( $metadata, $post ) {
  165. $image = Jetpack_PostImages::get_image( $post->ID, array(
  166. 'fallback_to_avatars' => true,
  167. 'avatar_size' => 200,
  168. // AMP already attempts these.
  169. 'from_thumbnail' => false,
  170. 'from_attachment' => false,
  171. ) );
  172. if ( empty( $image ) ) {
  173. return self::add_fallback_image_to_metadata( $metadata );
  174. }
  175. if ( ! isset( $image['src_width'] ) ) {
  176. $dimensions = self::extract_image_dimensions_from_getimagesize( array(
  177. $image['src'] => false,
  178. ) );
  179. if ( false !== $dimensions[ $image['src'] ] ) {
  180. $image['src_width'] = $dimensions['width'];
  181. $image['src_height'] = $dimensions['height'];
  182. }
  183. }
  184. $metadata['image'] = array(
  185. '@type' => 'ImageObject',
  186. 'url' => $image['src'],
  187. );
  188. if ( isset( $image['src_width'] ) ) {
  189. $metadata['image']['width'] = $image['src_width'];
  190. }
  191. if ( isset( $image['src_width'] ) ) {
  192. $metadata['image']['height'] = $image['src_height'];
  193. }
  194. return $metadata;
  195. }
  196. /**
  197. * Add fallback image to legacy AMP post metadata.
  198. *
  199. * @since 6.2.0
  200. *
  201. * @param array $metadata Metadata.
  202. * @return array Metadata.
  203. */
  204. static function add_fallback_image_to_metadata( $metadata ) {
  205. /** This filter is documented in functions.opengraph.php */
  206. $default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
  207. $metadata['image'] = array(
  208. '@type' => 'ImageObject',
  209. 'url' => self::staticize_subdomain( $default_image ),
  210. 'width' => 200,
  211. 'height' => 200,
  212. );
  213. return $metadata;
  214. }
  215. static function staticize_subdomain( $domain ) {
  216. // deal with WPCOM vs Jetpack
  217. if ( function_exists( 'staticize_subdomain' ) ) {
  218. return staticize_subdomain( $domain );
  219. } else {
  220. return Jetpack::staticize_subdomain( $domain );
  221. }
  222. }
  223. /**
  224. * Extract image dimensions via wpcom/imagesize, only on WPCOM
  225. *
  226. * @since 6.2.0
  227. *
  228. * @param array $dimensions Dimensions.
  229. * @return array Dimensions.
  230. */
  231. static function extract_image_dimensions_from_getimagesize( $dimensions ) {
  232. if ( ! ( defined('WPCOM') && WPCOM && function_exists( 'require_lib' ) ) ) {
  233. return $dimensions;
  234. }
  235. require_lib( 'wpcom/imagesize' );
  236. foreach ( $dimensions as $url => $value ) {
  237. if ( is_array( $value ) ) {
  238. continue;
  239. }
  240. $result = wpcom_getimagesize( $url );
  241. if ( is_array( $result ) ) {
  242. $dimensions[ $url ] = array(
  243. 'width' => $result[0],
  244. 'height' => $result[1],
  245. );
  246. }
  247. }
  248. return $dimensions;
  249. }
  250. static function amp_post_jetpack_og_tags() {
  251. Jetpack::init()->check_open_graph();
  252. if ( function_exists( 'jetpack_og_tags' ) ) {
  253. jetpack_og_tags();
  254. }
  255. }
  256. static function videopress_enable_freedom_mode( $options ) {
  257. $options['freedom'] = true;
  258. return $options;
  259. }
  260. static function render_sharing_html( $markup, $sharing_enabled ) {
  261. remove_action( 'wp_footer', 'sharing_add_footer' );
  262. if ( empty( $sharing_enabled ) ) {
  263. return $markup;
  264. }
  265. $supported_services = array(
  266. 'facebook' => array(
  267. /** This filter is documented in modules/sharedaddy/sharing-sources.php */
  268. 'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ),
  269. ),
  270. 'twitter' => array(),
  271. 'pinterest' => array(),
  272. 'whatsapp' => array(),
  273. 'google-plus-1' => array(
  274. 'type' => 'gplus',
  275. ),
  276. 'tumblr' => array(),
  277. 'linkedin' => array(),
  278. );
  279. $sharing_links = array();
  280. foreach ( $sharing_enabled['visible'] as $id => $service ) {
  281. if ( ! isset( $supported_services[ $id ] ) ) {
  282. $sharing_links[] = "<!-- not supported: $id -->";
  283. continue;
  284. }
  285. $args = array_merge(
  286. array(
  287. 'type' => $id,
  288. ),
  289. $supported_services[ $id ]
  290. );
  291. $sharing_link = '<amp-social-share';
  292. foreach ( $args as $key => $value ) {
  293. $sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) );
  294. }
  295. $sharing_link .= '></amp-social-share>';
  296. $sharing_links[] = $sharing_link;
  297. }
  298. return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup );
  299. }
  300. }
  301. add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
  302. add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 );
  303. // this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must
  304. // take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module,
  305. // or preventing it from loading in the first place (better for performance)
  306. add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 );