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