Jetpack_Photon

The Jetpack by WordPress.com Jetpack Photon class.

Defined (1)

The class is defined in the following location(s).

/class.photon.php  
  1. class Jetpack_Photon { 
  2. /** 
  3. * Class variables 
  4. */ 
  5. // Oh look, a singleton 
  6. private static $__instance = null; 
  7.  
  8. // Allowed extensions must match http://code.trac.wordpress.org/browser/photon/index.php#L31 
  9. protected static $extensions = array( 
  10. 'gif',  
  11. 'jpg',  
  12. 'jpeg',  
  13. 'png' 
  14. ); 
  15.  
  16. // Don't access this directly. Instead, use self::image_sizes() so it's actually populated with something. 
  17. protected static $image_sizes = null; 
  18.  
  19. /** 
  20. * Singleton implementation 
  21. * @return object 
  22. */ 
  23. public static function instance() { 
  24. if ( ! is_a( self::$__instance, 'Jetpack_Photon' ) ) { 
  25. self::$__instance = new Jetpack_Photon; 
  26. self::$__instance->setup(); 
  27.  
  28. return self::$__instance; 
  29.  
  30. /** 
  31. * Silence is golden. 
  32. */ 
  33. private function __construct() {} 
  34.  
  35. /** 
  36. * Register actions and filters, but only if basic Photon functions are available. 
  37. * The basic functions are found in ./functions.photon.php. 
  38. * @uses add_action, add_filter 
  39. * @return null 
  40. */ 
  41. private function setup() { 
  42. // Display warning if site is private 
  43. add_action( 'jetpack_activate_module_photon', array( $this, 'action_jetpack_activate_module_photon' ) ); 
  44.  
  45. if ( ! function_exists( 'jetpack_photon_url' ) ) 
  46. return; 
  47.  
  48. // Images in post content and galleries 
  49. add_filter( 'the_content', array( __CLASS__, 'filter_the_content' ), 999999 ); 
  50. add_filter( 'get_post_galleries', array( __CLASS__, 'filter_the_galleries' ), 999999 ); 
  51.  
  52. // Core image retrieval 
  53. add_filter( 'image_downsize', array( $this, 'filter_image_downsize' ), 10, 3 ); 
  54.  
  55. // Responsive image srcset substitution 
  56. add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_array' ) ); 
  57.  
  58. // Helpers for maniuplated images 
  59. add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ), 9 ); 
  60.  
  61. /** 
  62. * Check if site is private and warn user if it is 
  63. * @uses Jetpack::check_privacy 
  64. * @action jetpack_activate_module_photon 
  65. * @return null 
  66. */ 
  67. public function action_jetpack_activate_module_photon() { 
  68. Jetpack::check_privacy( __FILE__ ); 
  69.  
  70. /** 
  71. ** IN-CONTENT IMAGE MANIPULATION FUNCTIONS 
  72. **/ 
  73.  
  74. /** 
  75. * Match all images and any relevant <a> tags in a block of HTML. 
  76. * @param string $content Some HTML. 
  77. * @return array An array of $images matches, where $images[0] is 
  78. * an array of full matches, and the link_url, img_tag,  
  79. * and img_url keys are arrays of those matches. 
  80. */ 
  81. public static function parse_images_from_html( $content ) { 
  82. $images = array(); 
  83.  
  84. if ( preg_match_all( '#(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag><img[^>]+?src=["|\'](?P<img_url>[^\s]+?)["|\'].*?>) {1}(?:\s*</a>)?#is', $content, $images ) ) { 
  85. foreach ( $images as $key => $unused ) { 
  86. // Simplify the output as much as possible, mostly for confirming test results. 
  87. if ( is_numeric( $key ) && $key > 0 ) 
  88. unset( $images[$key] ); 
  89.  
  90. return $images; 
  91.  
  92. return array(); 
  93.  
  94. /** 
  95. * Try to determine height and width from strings WP appends to resized image filenames. 
  96. * @param string $src The image URL. 
  97. * @return array An array consisting of width and height. 
  98. */ 
  99. public static function parse_dimensions_from_filename( $src ) { 
  100. $width_height_string = array(); 
  101.  
  102. if ( preg_match( '#-(\d+)x(\d+)\.(?:' . implode('|', self::$extensions ) . ') {1}$#i', $src, $width_height_string ) ) { 
  103. $width = (int) $width_height_string[1]; 
  104. $height = (int) $width_height_string[2]; 
  105.  
  106. if ( $width && $height ) 
  107. return array( $width, $height ); 
  108.  
  109. return array( false, false ); 
  110.  
  111. /** 
  112. * Identify images in post content, and if images are local (uploaded to the current site), pass through Photon. 
  113. * @param string $content 
  114. * @uses self::validate_image_url, apply_filters, jetpack_photon_url, esc_url 
  115. * @filter the_content 
  116. * @return string 
  117. */ 
  118. public static function filter_the_content( $content ) { 
  119. $images = Jetpack_Photon::parse_images_from_html( $content ); 
  120.  
  121. if ( ! empty( $images ) ) { 
  122. $content_width = Jetpack::get_content_width(); 
  123.  
  124. $image_sizes = self::image_sizes(); 
  125. $upload_dir = wp_upload_dir(); 
  126.  
  127. foreach ( $images[0] as $index => $tag ) { 
  128. // Default to resize, though fit may be used in certain cases where a dimension cannot be ascertained 
  129. $transform = 'resize'; 
  130.  
  131. // Start with a clean attachment ID each time 
  132. $attachment_id = false; 
  133.  
  134. // Flag if we need to munge a fullsize URL 
  135. $fullsize_url = false; 
  136.  
  137. // Identify image source 
  138. $src = $src_orig = $images['img_url'][ $index ]; 
  139.  
  140. /** 
  141. * Allow specific images to be skipped by Photon. 
  142. * @module photon 
  143. * @since 2.0.3 
  144. * @param bool false Should Photon ignore this image. Default to false. 
  145. * @param string $src Image URL. 
  146. * @param string $tag Image Tag (Image HTML output). 
  147. */ 
  148. if ( apply_filters( 'jetpack_photon_skip_image', false, $src, $tag ) ) 
  149. continue; 
  150.  
  151. // Support Automattic's Lazy Load plugin 
  152. // Can't modify $tag yet as we need unadulterated version later 
  153. if ( preg_match( '#data-lazy-src=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) { 
  154. $placeholder_src = $placeholder_src_orig = $src; 
  155. $src = $src_orig = $lazy_load_src[1]; 
  156. } elseif ( preg_match( '#data-lazy-original=["|\'](.+?)["|\']#i', $images['img_tag'][ $index ], $lazy_load_src ) ) { 
  157. $placeholder_src = $placeholder_src_orig = $src; 
  158. $src = $src_orig = $lazy_load_src[1]; 
  159.  
  160. // Check if image URL should be used with Photon 
  161. if ( self::validate_image_url( $src ) ) { 
  162. // Find the width and height attributes 
  163. $width = $height = false; 
  164.  
  165. // First, check the image tag 
  166. if ( preg_match( '#width=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $width_string ) ) 
  167. $width = $width_string[1]; 
  168.  
  169. if ( preg_match( '#height=["|\']?([\d%]+)["|\']?#i', $images['img_tag'][ $index ], $height_string ) ) 
  170. $height = $height_string[1]; 
  171.  
  172. // Can't pass both a relative width and height, so unset the height in favor of not breaking the horizontal layout. 
  173. if ( false !== strpos( $width, '%' ) && false !== strpos( $height, '%' ) ) 
  174. $width = $height = false; 
  175.  
  176. // Detect WP registered image size from HTML class 
  177. if ( preg_match( '#class=["|\']?[^"\']*size-([^"\'\s]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $size ) ) { 
  178. $size = array_pop( $size ); 
  179.  
  180. if ( false === $width && false === $height && 'full' != $size && array_key_exists( $size, $image_sizes ) ) { 
  181. $width = (int) $image_sizes[ $size ]['width']; 
  182. $height = (int) $image_sizes[ $size ]['height']; 
  183. $transform = $image_sizes[ $size ]['crop'] ? 'resize' : 'fit'; 
  184. } else { 
  185. unset( $size ); 
  186.  
  187. // WP Attachment ID, if uploaded to this site 
  188. if ( 
  189. preg_match( '#class=["|\']?[^"\']*wp-image-([\d]+)[^"\']*["|\']?#i', $images['img_tag'][ $index ], $attachment_id ) && 
  190. 0 === strpos( $src, $upload_dir['baseurl'] ) || 
  191. /** 
  192. * Filter whether an image using an attachment ID in its class has to be uploaded to the local site to go through Photon. 
  193. * @module photon 
  194. * @since 2.0.3 
  195. * @param bool false Was the image uploaded to the local site. Default to false. 
  196. * @param array $args { 
  197. * Array of image details. 
  198. * @type $src Image URL. 
  199. * @type tag Image tag (Image HTML output). 
  200. * @type $images Array of information about the image. 
  201. * @type $index Image index. 
  202. * } 
  203. */ 
  204. apply_filters( 'jetpack_photon_image_is_local', false, compact( 'src', 'tag', 'images', 'index' ) ) 
  205. ) { 
  206. $attachment_id = intval( array_pop( $attachment_id ) ); 
  207.  
  208. if ( $attachment_id ) { 
  209. $attachment = get_post( $attachment_id ); 
  210.  
  211. // Basic check on returned post object 
  212. if ( is_object( $attachment ) && ! is_wp_error( $attachment ) && 'attachment' == $attachment->post_type ) { 
  213. $src_per_wp = wp_get_attachment_image_src( $attachment_id, isset( $size ) ? $size : 'full' ); 
  214.  
  215. if ( self::validate_image_url( $src_per_wp[0] ) ) { 
  216. $src = $src_per_wp[0]; 
  217. $fullsize_url = true; 
  218.  
  219. // Prevent image distortion if a detected dimension exceeds the image's natural dimensions 
  220. if ( ( false !== $width && $width > $src_per_wp[1] ) || ( false !== $height && $height > $src_per_wp[2] ) ) { 
  221. $width = false == $width ? false : min( $width, $src_per_wp[1] ); 
  222. $height = false == $height ? false : min( $height, $src_per_wp[2] ); 
  223.  
  224. // If no width and height are found, max out at source image's natural dimensions 
  225. // Otherwise, respect registered image sizes' cropping setting 
  226. if ( false == $width && false == $height ) { 
  227. $width = $src_per_wp[1]; 
  228. $height = $src_per_wp[2]; 
  229. $transform = 'fit'; 
  230. } elseif ( isset( $size ) && array_key_exists( $size, $image_sizes ) && isset( $image_sizes[ $size ]['crop'] ) ) { 
  231. $transform = (bool) $image_sizes[ $size ]['crop'] ? 'resize' : 'fit'; 
  232. } else { 
  233. unset( $attachment_id ); 
  234. unset( $attachment ); 
  235.  
  236. // If image tag lacks width and height arguments, try to determine from strings WP appends to resized image filenames. 
  237. if ( false === $width && false === $height ) { 
  238. list( $width, $height ) = Jetpack_Photon::parse_dimensions_from_filename( $src ); 
  239.  
  240. // If width is available, constrain to $content_width 
  241. if ( false !== $width && false === strpos( $width, '%' ) && is_numeric( $content_width ) ) { 
  242. if ( $width > $content_width && false !== $height && false === strpos( $height, '%' ) ) { 
  243. $height = round( ( $content_width * $height ) / $width ); 
  244. $width = $content_width; 
  245. } elseif ( $width > $content_width ) { 
  246. $width = $content_width; 
  247.  
  248. // Set a width if none is found and $content_width is available 
  249. // If width is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing 
  250. if ( false === $width && is_numeric( $content_width ) ) { 
  251. $width = (int) $content_width; 
  252.  
  253. if ( false !== $height ) 
  254. $transform = 'fit'; 
  255.  
  256. // Detect if image source is for a custom-cropped thumbnail and prevent further URL manipulation. 
  257. if ( ! $fullsize_url && preg_match_all( '#-e[a-z0-9]+(-\d+x\d+)?\.(' . implode('|', self::$extensions ) . ') {1}$#i', basename( $src ), $filename ) ) 
  258. $fullsize_url = true; 
  259.  
  260. // Build URL, first maybe removing WP's resized string so we pass the original image to Photon 
  261. if ( ! $fullsize_url ) { 
  262. $src = self::strip_image_dimensions_maybe( $src ); 
  263.  
  264. // Build array of Photon args and expose to filter before passing to Photon URL function 
  265. $args = array(); 
  266.  
  267. if ( false !== $width && false !== $height && false === strpos( $width, '%' ) && false === strpos( $height, '%' ) ) 
  268. $args[ $transform ] = $width . ', ' . $height; 
  269. elseif ( false !== $width ) 
  270. $args['w'] = $width; 
  271. elseif ( false !== $height ) 
  272. $args['h'] = $height; 
  273.  
  274. /** 
  275. * Filter the array of Photon arguments added to an image when it goes through Photon. 
  276. * By default, only includes width and height values. 
  277. * @see https://developer.wordpress.com/docs/photon/api/ 
  278. * @module photon 
  279. * @since 2.0.0 
  280. * @param array $args Array of Photon Arguments. 
  281. * @param array $args { 
  282. * Array of image details. 
  283. * @type $tag Image tag (Image HTML output). 
  284. * @type $src Image URL. 
  285. * @type $src_orig Original Image URL. 
  286. * @type $width Image width. 
  287. * @type $height Image height. 
  288. * } 
  289. */ 
  290. $args = apply_filters( 'jetpack_photon_post_image_args', $args, compact( 'tag', 'src', 'src_orig', 'width', 'height' ) ); 
  291.  
  292. $photon_url = jetpack_photon_url( $src, $args ); 
  293.  
  294. // Modify image tag if Photon function provides a URL 
  295. // Ensure changes are only applied to the current image by copying and modifying the matched tag, then replacing the entire tag with our modified version. 
  296. if ( $src != $photon_url ) { 
  297. $new_tag = $tag; 
  298.  
  299. // If present, replace the link href with a Photoned URL for the full-size image. 
  300. if ( ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) 
  301. $new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $new_tag, 1 ); 
  302.  
  303. // Supplant the original source value with our Photon URL 
  304. $photon_url = esc_url( $photon_url ); 
  305. $new_tag = str_replace( $src_orig, $photon_url, $new_tag ); 
  306.  
  307. // If Lazy Load is in use, pass placeholder image through Photon 
  308. if ( isset( $placeholder_src ) && self::validate_image_url( $placeholder_src ) ) { 
  309. $placeholder_src = jetpack_photon_url( $placeholder_src ); 
  310.  
  311. if ( $placeholder_src != $placeholder_src_orig ) 
  312. $new_tag = str_replace( $placeholder_src_orig, esc_url( $placeholder_src ), $new_tag ); 
  313.  
  314. unset( $placeholder_src ); 
  315.  
  316. // Remove the width and height arguments from the tag to prevent distortion 
  317. $new_tag = preg_replace( '#(?<=\s)(width|height)=["|\']?[\d%]+["|\']?\s?#i', '', $new_tag ); 
  318.  
  319. // Tag an image for dimension checking 
  320. $new_tag = preg_replace( '#(\s?/)?>(\s*</a>)?$#i', ' data-recalc-dims="1"\1>\2', $new_tag ); 
  321.  
  322. // Replace original tag with modified version 
  323. $content = str_replace( $tag, $new_tag, $content ); 
  324. } elseif ( preg_match( '#^http(s)?://i[\d]{1}.wp.com#', $src ) && ! empty( $images['link_url'][ $index ] ) && self::validate_image_url( $images['link_url'][ $index ] ) ) { 
  325. $new_tag = preg_replace( '#(href=["|\'])' . $images['link_url'][ $index ] . '(["|\'])#i', '\1' . jetpack_photon_url( $images['link_url'][ $index ] ) . '\2', $tag, 1 ); 
  326.  
  327. $content = str_replace( $tag, $new_tag, $content ); 
  328.  
  329. return $content; 
  330.  
  331. public static function filter_the_galleries( $galleries ) { 
  332. if ( empty( $galleries ) || ! is_array( $galleries ) ) { 
  333. return $galleries; 
  334.  
  335. // Pass by reference, so we can modify them in place. 
  336. foreach ( $galleries as &$this_gallery ) { 
  337. if ( is_string( $this_gallery ) ) { 
  338. $this_gallery = self::filter_the_content( $this_gallery ); 
  339. // LEAVING COMMENTED OUT as for the moment it doesn't seem 
  340. // necessary and I'm not sure how it would propagate through. 
  341. // } elseif ( is_array( $this_gallery ) 
  342. // && ! empty( $this_gallery['src'] ) 
  343. // && ! empty( $this_gallery['type'] ) 
  344. // && in_array( $this_gallery['type'], array( 'rectangle', 'square', 'circle' ) ) ) { 
  345. // $this_gallery['src'] = array_map( 'jetpack_photon_url', $this_gallery['src'] ); 
  346. unset( $this_gallery ); // break the reference. 
  347.  
  348. return $galleries; 
  349.  
  350. /** 
  351. ** CORE IMAGE RETRIEVAL 
  352. **/ 
  353.  
  354. /** 
  355. * Filter post thumbnail image retrieval, passing images through Photon 
  356. * @param string|bool $image 
  357. * @param int $attachment_id 
  358. * @param string|array $size 
  359. * @uses is_admin, apply_filters, wp_get_attachment_url, self::validate_image_url, this::image_sizes, jetpack_photon_url 
  360. * @filter image_downsize 
  361. * @return string|bool 
  362. */ 
  363. public function filter_image_downsize( $image, $attachment_id, $size ) { 
  364. // Don't foul up the admin side of things, and provide plugins a way of preventing Photon from being applied to images. 
  365. if ( 
  366. is_admin() || 
  367. /** 
  368. * Provide plugins a way of preventing Photon from being applied to images retrieved from WordPress Core. 
  369. * @module photon 
  370. * @since 2.0.0 
  371. * @param bool false Stop Photon from being applied to the image. Default to false. 
  372. * @param array $args { 
  373. * Array of image details. 
  374. * @type $image Image URL. 
  375. * @type $attachment_id Attachment ID of the image. 
  376. * @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer. 
  377. * } 
  378. */ 
  379. apply_filters( 'jetpack_photon_override_image_downsize', false, compact( 'image', 'attachment_id', 'size' ) ) 
  380. return $image; 
  381.  
  382. // Get the image URL and proceed with Photon-ification if successful 
  383. $image_url = wp_get_attachment_url( $attachment_id ); 
  384.  
  385. if ( $image_url ) { 
  386. // Check if image URL should be used with Photon 
  387. if ( ! self::validate_image_url( $image_url ) ) 
  388. return $image; 
  389.  
  390. // If an image is requested with a size known to WordPress, use that size's settings with Photon 
  391. if ( ( is_string( $size ) || is_int( $size ) ) && array_key_exists( $size, self::image_sizes() ) ) { 
  392. $image_args = self::image_sizes(); 
  393. $image_args = $image_args[ $size ]; 
  394.  
  395. $photon_args = array(); 
  396.  
  397. // `full` is a special case in WP 
  398. // To ensure filter receives consistent data regardless of requested size, `$image_args` is overridden with dimensions of original image. 
  399. if ( 'full' == $size ) { 
  400. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  401. if ( isset( $image_meta['width'], $image_meta['height'] ) ) { 
  402. // 'crop' is true so Photon's `resize` method is used 
  403. $image_args = array( 
  404. 'width' => $image_meta['width'],  
  405. 'height' => $image_meta['height'],  
  406. 'crop' => true 
  407. ); 
  408.  
  409. // Expose determined arguments to a filter before passing to Photon 
  410. $transform = $image_args['crop'] ? 'resize' : 'fit'; 
  411.  
  412. // Check specified image dimensions and account for possible zero values; photon fails to resize if a dimension is zero. 
  413. if ( 0 == $image_args['width'] || 0 == $image_args['height'] ) { 
  414. if ( 0 == $image_args['width'] && 0 < $image_args['height'] ) { 
  415. $photon_args['h'] = $image_args['height']; 
  416. } elseif ( 0 == $image_args['height'] && 0 < $image_args['width'] ) { 
  417. $photon_args['w'] = $image_args['width']; 
  418. } else { 
  419. if ( ( 'resize' === $transform ) && $image_meta = wp_get_attachment_metadata( $attachment_id ) ) { 
  420. // Lets make sure that we don't upscale images since wp never upscales them as well 
  421. $smaller_width = ( ( $image_meta['width'] < $image_args['width'] ) ? $image_meta['width'] : $image_args['width'] ); 
  422. $smaller_height = ( ( $image_meta['height'] < $image_args['height'] ) ? $image_meta['height'] : $image_args['height'] ); 
  423.  
  424. $photon_args[ $transform ] = $smaller_width . ', ' . $smaller_height; 
  425. } else { 
  426. $photon_args[ $transform ] = $image_args['width'] . ', ' . $image_args['height']; 
  427.  
  428.  
  429. /** 
  430. * Filter the Photon Arguments added to an image when going through Photon, when that image size is a string. 
  431. * Image size will be a string (e.g. "full", "medium") when it is known to WordPress. 
  432. * @module photon 
  433. * @since 2.0.0 
  434. * @param array $photon_args Array of Photon arguments. 
  435. * @param array $args { 
  436. * Array of image details. 
  437. * @type $image_args Array of Image arguments (width, height, crop). 
  438. * @type $image_url Image URL. 
  439. * @type $attachment_id Attachment ID of the image. 
  440. * @type $size Image size. Can be a string (name of the image size, e.g. full) or an integer. 
  441. * @type $transform Value can be resize or fit. 
  442. * @see https://developer.wordpress.com/docs/photon/api 
  443. * } 
  444. */ 
  445. $photon_args = apply_filters( 'jetpack_photon_image_downsize_string', $photon_args, compact( 'image_args', 'image_url', 'attachment_id', 'size', 'transform' ) ); 
  446.  
  447. // Generate Photon URL 
  448. $image = array( 
  449. jetpack_photon_url( $image_url, $photon_args ),  
  450. false,  
  451. false 
  452. ); 
  453. } elseif ( is_array( $size ) ) { 
  454. // Pull width and height values from the provided array, if possible 
  455. $width = isset( $size[0] ) ? (int) $size[0] : false; 
  456. $height = isset( $size[1] ) ? (int) $size[1] : false; 
  457.  
  458. // Don't bother if necessary parameters aren't passed. 
  459. if ( ! $width || ! $height ) 
  460. return $image; 
  461.  
  462. // Expose arguments to a filter before passing to Photon 
  463. $photon_args = array( 
  464. 'fit' => $width . ', ' . $height 
  465. ); 
  466.  
  467. /** 
  468. * Filter the Photon Arguments added to an image when going through Photon,  
  469. * when the image size is an array of height and width values. 
  470. * @module photon 
  471. * @since 2.0.0 
  472. * @param array $photon_args Array of Photon arguments. 
  473. * @param array $args { 
  474. * Array of image details. 
  475. * @type $width Image width. 
  476. * @type height Image height. 
  477. * @type $image_url Image URL. 
  478. * @type $attachment_id Attachment ID of the image. 
  479. * } 
  480. */ 
  481. $photon_args = apply_filters( 'jetpack_photon_image_downsize_array', $photon_args, compact( 'width', 'height', 'image_url', 'attachment_id' ) ); 
  482.  
  483. // Generate Photon URL 
  484. $image = array( 
  485. jetpack_photon_url( $image_url, $photon_args ),  
  486. false,  
  487. false 
  488. ); 
  489.  
  490. return $image; 
  491.  
  492. /** 
  493. * Filters an array of image `srcset` values, replacing each URL with its Photon equivalent. 
  494. * @since 3.8.0 
  495. * @param array $sources An array of image urls and widths. 
  496. * @uses self::validate_image_url, jetpack_photon_url 
  497. * @return array An array of Photon image urls and widths. 
  498. */ 
  499. public function filter_srcset_array( $sources ) { 
  500. foreach ( $sources as $i => $source ) { 
  501. if ( ! self::validate_image_url( $source['url'] ) ) { 
  502. continue; 
  503.  
  504. $url = Jetpack_Photon::strip_image_dimensions_maybe( $source['url'] ); 
  505.  
  506. $args = array(); 
  507. if ( 'w' === $source['descriptor'] ) { 
  508. $args['w'] = $source['value']; 
  509.  
  510. $sources[ $i ]['url'] = jetpack_photon_url( $url, $args ); 
  511.  
  512. return $sources; 
  513.  
  514. /** 
  515. ** GENERAL FUNCTIONS 
  516. **/ 
  517.  
  518. /** 
  519. * Ensure image URL is valid for Photon. 
  520. * Though Photon functions address some of the URL issues, we should avoid unnecessary processing if we know early on that the image isn't supported. 
  521. * @param string $url 
  522. * @uses wp_parse_args 
  523. * @return bool 
  524. */ 
  525. protected static function validate_image_url( $url ) { 
  526. $parsed_url = @parse_url( $url ); 
  527.  
  528. if ( ! $parsed_url ) 
  529. return false; 
  530.  
  531. // Parse URL and ensure needed keys exist, since the array returned by `parse_url` only includes the URL components it finds. 
  532. $url_info = wp_parse_args( $parsed_url, array( 
  533. 'scheme' => null,  
  534. 'host' => null,  
  535. 'port' => null,  
  536. 'path' => null 
  537. ) ); 
  538.  
  539. // Bail if scheme isn't http or port is set that isn't port 80 
  540. if ( 
  541. ( 'http' != $url_info['scheme'] || ! in_array( $url_info['port'], array( 80, null ) ) ) && 
  542. /** 
  543. * Allow Photon to fetch images that are served via HTTPS. 
  544. * @module photon 
  545. * @since 2.4.0 
  546. * @param bool true Should Photon ignore images using the HTTPS scheme. Default to true. 
  547. */ 
  548. apply_filters( 'jetpack_photon_reject_https', true ) 
  549. ) { 
  550. return false; 
  551.  
  552. // Bail if no host is found 
  553. if ( is_null( $url_info['host'] ) ) 
  554. return false; 
  555.  
  556. // Bail if the image alredy went through Photon 
  557. if ( preg_match( '#^i[\d]{1}.wp.com$#i', $url_info['host'] ) ) 
  558. return false; 
  559.  
  560. // Bail if no path is found 
  561. if ( is_null( $url_info['path'] ) ) 
  562. return false; 
  563.  
  564. // Ensure image extension is acceptable 
  565. if ( ! in_array( strtolower( pathinfo( $url_info['path'], PATHINFO_EXTENSION ) ), self::$extensions ) ) 
  566. return false; 
  567.  
  568. // If we got this far, we should have an acceptable image URL 
  569. // But let folks filter to decline if they prefer. 
  570. /** 
  571. * Overwrite the results of the validation steps an image goes through before to be considered valid to be used by Photon. 
  572. * @module photon 
  573. * @since 3.0.0 
  574. * @param bool true Is the image URL valid and can it be used by Photon. Default to true. 
  575. * @param string $url Image URL. 
  576. * @param array $parsed_url Array of information about the image. 
  577. */ 
  578. return apply_filters( 'photon_validate_image_url', true, $url, $parsed_url ); 
  579.  
  580. /** 
  581. * Checks if the file exists before it passes the file to photon 
  582. * @param string $src The image URL 
  583. * @return string 
  584. **/ 
  585. protected static function strip_image_dimensions_maybe( $src ) { 
  586. $stripped_src = $src; 
  587.  
  588. // Build URL, first removing WP's resized string so we pass the original image to Photon 
  589. if ( preg_match( '#(-\d+x\d+)\.(' . implode('|', self::$extensions ) . ') {1}$#i', $src, $src_parts ) ) { 
  590. $stripped_src = str_replace( $src_parts[1], '', $src ); 
  591. $upload_dir = wp_upload_dir(); 
  592.  
  593. // Extracts the file path to the image minus the base url 
  594. $file_path = substr( $stripped_src, strlen ( $upload_dir['baseurl'] ) ); 
  595.  
  596. if( file_exists( $upload_dir["basedir"] . $file_path ) ) 
  597. $src = $stripped_src; 
  598.  
  599. return $src; 
  600.  
  601. /** 
  602. * Provide an array of available image sizes and corresponding dimensions. 
  603. * Similar to get_intermediate_image_sizes() except that it includes image sizes' dimensions, not just their names. 
  604. * @global $wp_additional_image_sizes 
  605. * @uses get_option 
  606. * @return array 
  607. */ 
  608. protected static function image_sizes() { 
  609. if ( null == self::$image_sizes ) { 
  610. global $_wp_additional_image_sizes; 
  611.  
  612. // Populate an array matching the data structure of $_wp_additional_image_sizes so we have a consistent structure for image sizes 
  613. $images = array( 
  614. 'thumb' => array( 
  615. 'width' => intval( get_option( 'thumbnail_size_w' ) ),  
  616. 'height' => intval( get_option( 'thumbnail_size_h' ) ),  
  617. 'crop' => (bool) get_option( 'thumbnail_crop' ) 
  618. ),  
  619. 'medium' => array( 
  620. 'width' => intval( get_option( 'medium_size_w' ) ),  
  621. 'height' => intval( get_option( 'medium_size_h' ) ),  
  622. 'crop' => false 
  623. ),  
  624. 'large' => array( 
  625. 'width' => intval( get_option( 'large_size_w' ) ),  
  626. 'height' => intval( get_option( 'large_size_h' ) ),  
  627. 'crop' => false 
  628. ),  
  629. 'full' => array( 
  630. 'width' => null,  
  631. 'height' => null,  
  632. 'crop' => false 
  633. ); 
  634.  
  635. // Compatibility mapping as found in wp-includes/media.php 
  636. $images['thumbnail'] = $images['thumb']; 
  637.  
  638. // Update class variable, merging in $_wp_additional_image_sizes if any are set 
  639. if ( is_array( $_wp_additional_image_sizes ) && ! empty( $_wp_additional_image_sizes ) ) 
  640. self::$image_sizes = array_merge( $images, $_wp_additional_image_sizes ); 
  641. else 
  642. self::$image_sizes = $images; 
  643.  
  644. return is_array( self::$image_sizes ) ? self::$image_sizes : array(); 
  645.  
  646. /** 
  647. * Pass og:image URLs through Photon 
  648. * @param array $tags 
  649. * @param array $parameters 
  650. * @uses jetpack_photon_url 
  651. * @return array 
  652. */ 
  653. function filter_open_graph_tags( $tags, $parameters ) { 
  654. if ( empty( $tags['og:image'] ) ) { 
  655. return $tags; 
  656.  
  657. $photon_args = array( 
  658. 'fit' => sprintf( '%d, %d', 2 * $parameters['image_width'], 2 * $parameters['image_height'] ),  
  659. ); 
  660.  
  661. if ( is_array( $tags['og:image'] ) ) { 
  662. $images = array(); 
  663. foreach ( $tags['og:image'] as $image ) { 
  664. $images[] = jetpack_photon_url( $image, $photon_args ); 
  665. $tags['og:image'] = $images; 
  666. } else { 
  667. $tags['og:image'] = jetpack_photon_url( $tags['og:image'], $photon_args ); 
  668.  
  669. return $tags; 
  670.  
  671. /** 
  672. * Enqueue Photon helper script 
  673. * @uses wp_enqueue_script, plugins_url 
  674. * @action wp_enqueue_script 
  675. * @return null 
  676. */ 
  677. public function action_wp_enqueue_scripts() { 
  678. wp_enqueue_script( 'jetpack-photon', plugins_url( 'modules/photon/photon.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), 20130122, true );