functions.photon.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Generates a Photon URL.
  4. *
  5. * @see http://developer.wordpress.com/docs/photon/
  6. *
  7. * @param string $image_url URL to the publicly accessible image you want to manipulate
  8. * @param array|string $args An array of arguments, i.e. array( 'w' => '300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456)
  9. * @return string The raw final URL. You should run this through esc_url() before displaying it.
  10. */
  11. function jetpack_photon_url( $image_url, $args = array(), $scheme = null ) {
  12. $image_url = trim( $image_url );
  13. if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
  14. /**
  15. * Disables Photon URL processing for local development
  16. *
  17. * @module photon
  18. *
  19. * @since 4.1.0
  20. *
  21. * @param bool false Result of Jetpack::is_development_mode.
  22. */
  23. if ( true === apply_filters( 'jetpack_photon_development_mode', Jetpack::is_development_mode() ) ) {
  24. return $image_url;
  25. }
  26. }
  27. /**
  28. * Allow specific image URls to avoid going through Photon.
  29. *
  30. * @module photon
  31. *
  32. * @since 3.2.0
  33. *
  34. * @param bool false Should the image be returned as is, without going through Photon. Default to false.
  35. * @param string $image_url Image URL.
  36. * @param array|string $args Array of Photon arguments.
  37. * @param string|null $scheme Image scheme. Default to null.
  38. */
  39. if ( false !== apply_filters( 'jetpack_photon_skip_for_url', false, $image_url, $args, $scheme ) ) {
  40. return $image_url;
  41. }
  42. /**
  43. * Filter the original image URL before it goes through Photon.
  44. *
  45. * @module photon
  46. *
  47. * @since 1.9.0
  48. *
  49. * @param string $image_url Image URL.
  50. * @param array|string $args Array of Photon arguments.
  51. * @param string|null $scheme Image scheme. Default to null.
  52. */
  53. $image_url = apply_filters( 'jetpack_photon_pre_image_url', $image_url, $args, $scheme );
  54. /**
  55. * Filter the original Photon image parameters before Photon is applied to an image.
  56. *
  57. * @module photon
  58. *
  59. * @since 1.9.0
  60. *
  61. * @param array|string $args Array of Photon arguments.
  62. * @param string $image_url Image URL.
  63. * @param string|null $scheme Image scheme. Default to null.
  64. */
  65. $args = apply_filters( 'jetpack_photon_pre_args', $args, $image_url, $scheme );
  66. if ( empty( $image_url ) )
  67. return $image_url;
  68. $image_url_parts = @jetpack_photon_parse_url( $image_url );
  69. // Unable to parse
  70. if ( ! is_array( $image_url_parts ) || empty( $image_url_parts['host'] ) || empty( $image_url_parts['path'] ) )
  71. return $image_url;
  72. if ( is_array( $args ) ){
  73. // Convert values that are arrays into strings
  74. foreach ( $args as $arg => $value ) {
  75. if ( is_array( $value ) ) {
  76. $args[$arg] = implode( ',', $value );
  77. }
  78. }
  79. // Encode values
  80. // See http://core.trac.wordpress.org/ticket/17923
  81. $args = rawurlencode_deep( $args );
  82. }
  83. // Don't photon-ize WPCOM hosted images -- we can serve them up from wpcom directly.
  84. $is_wpcom_image = false;
  85. if ( wp_endswith( strtolower( $image_url_parts['host'] ), '.files.wordpress.com' ) ) {
  86. $is_wpcom_image = true;
  87. if ( isset( $args['ssl'] ) ) {
  88. // Do not send the ssl argument to prevent caching issues
  89. unset( $args['ssl'] );
  90. }
  91. }
  92. /** This filter is documented below. */
  93. $custom_photon_url = apply_filters( 'jetpack_photon_domain', '', $image_url );
  94. $custom_photon_url = esc_url( $custom_photon_url );
  95. // You can't run a Photon URL through Photon again because query strings are stripped.
  96. // So if the image is already a Photon URL, append the new arguments to the existing URL.
  97. // Alternately, if it's a *.files.wordpress.com url, then keep the domain as is.
  98. if (
  99. in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ) )
  100. || $image_url_parts['host'] === jetpack_photon_parse_url( $custom_photon_url, PHP_URL_HOST )
  101. || $is_wpcom_image
  102. ) {
  103. $photon_url = add_query_arg( $args, $image_url );
  104. return jetpack_photon_url_scheme( $photon_url, $scheme );
  105. }
  106. /**
  107. * Allow Photon to use query strings as well.
  108. * By default, Photon doesn't support query strings so we ignore them and look only at the path.
  109. * This setting is Photon Server dependent.
  110. *
  111. * @module photon
  112. *
  113. * @since 1.9.0
  114. *
  115. * @param bool false Should images using query strings go through Photon. Default is false.
  116. * @param string $image_url_parts['host'] Image URL's host.
  117. */
  118. if ( ! apply_filters( 'jetpack_photon_any_extension_for_domain', false, $image_url_parts['host'] ) ) {
  119. // Photon doesn't support query strings so we ignore them and look only at the path.
  120. // However some source images are served via PHP so check the no-query-string extension.
  121. // For future proofing, this is a blacklist of common issues rather than a whitelist.
  122. $extension = pathinfo( $image_url_parts['path'], PATHINFO_EXTENSION );
  123. if ( empty( $extension ) || in_array( $extension, array( 'php', 'ashx' ) ) )
  124. return $image_url;
  125. }
  126. $image_host_path = $image_url_parts['host'] . $image_url_parts['path'];
  127. // Figure out which CDN subdomain to use
  128. srand( crc32( $image_host_path ) );
  129. $subdomain = rand( 0, 2 );
  130. srand();
  131. /**
  132. * Filters the domain used by the Photon module.
  133. *
  134. * @module photon
  135. *
  136. * @since 3.4.2
  137. *
  138. * @param string https://i{$subdomain}.wp.com Domain used by Photon. $subdomain is a random number between 0 and 2.
  139. * @param string $image_url URL of the image to be photonized.
  140. */
  141. $photon_domain = apply_filters( 'jetpack_photon_domain', "https://i{$subdomain}.wp.com", $image_url );
  142. $photon_domain = trailingslashit( esc_url( $photon_domain ) );
  143. $photon_url = $photon_domain . $image_host_path;
  144. /**
  145. * Add query strings to Photon URL.
  146. * By default, Photon doesn't support query strings so we ignore them.
  147. * This setting is Photon Server dependent.
  148. *
  149. * @module photon
  150. *
  151. * @since 1.9.0
  152. *
  153. * @param bool false Should query strings be added to the image URL. Default is false.
  154. * @param string $image_url_parts['host'] Image URL's host.
  155. */
  156. if ( isset( $image_url_parts['query'] ) && apply_filters( 'jetpack_photon_add_query_string_to_domain', false, $image_url_parts['host'] ) ) {
  157. $photon_url .= '?q=' . rawurlencode( $image_url_parts['query'] );
  158. }
  159. if ( $args ) {
  160. if ( is_array( $args ) ) {
  161. $photon_url = add_query_arg( $args, $photon_url );
  162. } else {
  163. // You can pass a query string for complicated requests but where you still want CDN subdomain help, etc.
  164. $photon_url .= '?' . $args;
  165. }
  166. }
  167. if ( isset( $image_url_parts['scheme'] ) && 'https' == $image_url_parts['scheme'] ) {
  168. $photon_url = add_query_arg( array( 'ssl' => 1 ), $photon_url );
  169. }
  170. return jetpack_photon_url_scheme( $photon_url, $scheme );
  171. }
  172. add_filter( 'jetpack_photon_url', 'jetpack_photon_url', 10, 3 );
  173. /**
  174. * WordPress.com
  175. *
  176. * If a cropped WP.com-hosted image is the source image, have Photon replicate the crop.
  177. */
  178. add_filter( 'jetpack_photon_pre_args', 'jetpack_photon_parse_wpcom_query_args', 10, 2 );
  179. function jetpack_photon_parse_wpcom_query_args( $args, $image_url ) {
  180. $parsed_url = @parse_url( $image_url );
  181. if ( ! $parsed_url )
  182. return $args;
  183. $image_url_parts = wp_parse_args( $parsed_url, array(
  184. 'host' => '',
  185. 'query' => ''
  186. ) );
  187. if ( '.files.wordpress.com' != substr( $image_url_parts['host'], -20 ) )
  188. return $args;
  189. if ( empty( $image_url_parts['query'] ) )
  190. return $args;
  191. $wpcom_args = wp_parse_args( $image_url_parts['query'] );
  192. if ( empty( $wpcom_args['w'] ) || empty( $wpcom_args['h'] ) )
  193. return $args;
  194. // Keep the crop by using "resize"
  195. if ( ! empty( $wpcom_args['crop'] ) ) {
  196. if ( is_array( $args ) ) {
  197. $args = array_merge( array( 'resize' => array( $wpcom_args['w'], $wpcom_args['h'] ) ), $args );
  198. } else {
  199. $args = 'resize=' . rawurlencode( absint( $wpcom_args['w'] ) . ',' . absint( $wpcom_args['h'] ) ) . '&' . $args;
  200. }
  201. } else {
  202. if ( is_array( $args ) ) {
  203. $args = array_merge( array( 'fit' => array( $wpcom_args['w'], $wpcom_args['h'] ) ), $args );
  204. } else {
  205. $args = 'fit=' . rawurlencode( absint( $wpcom_args['w'] ) . ',' . absint( $wpcom_args['h'] ) ) . '&' . $args;
  206. }
  207. }
  208. return $args;
  209. }
  210. function jetpack_photon_url_scheme( $url, $scheme ) {
  211. if ( ! in_array( $scheme, array( 'http', 'https', 'network_path' ) ) ) {
  212. if ( preg_match( '#^(https?:)?//#', $url ) ) {
  213. return $url;
  214. }
  215. $scheme = 'http';
  216. }
  217. if ( 'network_path' == $scheme ) {
  218. $scheme_slashes = '//';
  219. } else {
  220. $scheme_slashes = "$scheme://";
  221. }
  222. return preg_replace( '#^([a-z:]+)?//#i', $scheme_slashes, $url );
  223. }
  224. /**
  225. * A wrapper for PHP's parse_url, prepending assumed scheme for network path
  226. * URLs. PHP versions 5.4.6 and earlier do not correctly parse without scheme.
  227. *
  228. * @see http://php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-changelog
  229. *
  230. * @param string $url The URL to parse
  231. * @param integer $component Retrieve specific URL component
  232. * @return mixed Result of parse_url
  233. */
  234. function jetpack_photon_parse_url( $url, $component = -1 ) {
  235. if ( 0 === strpos( $url, '//' ) ) {
  236. $url = ( is_ssl() ? 'https:' : 'http:' ) . $url;
  237. }
  238. return parse_url( $url, $component );
  239. }
  240. add_filter( 'jetpack_photon_skip_for_url', 'jetpack_photon_banned_domains', 9, 2 );
  241. function jetpack_photon_banned_domains( $skip, $image_url ) {
  242. $banned_host_patterns = array(
  243. '/^chart\.googleapis\.com$/',
  244. '/^chart\.apis\.google\.com$/',
  245. '/^graph\.facebook\.com$/',
  246. '/\.fbcdn\.net$/'
  247. );
  248. $host = jetpack_photon_parse_url( $image_url, PHP_URL_HOST );
  249. foreach ( $banned_host_patterns as $banned_host_pattern ) {
  250. if ( 1 === preg_match( $banned_host_pattern, $host ) ) {
  251. return true;
  252. }
  253. }
  254. return $skip;
  255. }
  256. /**
  257. * Jetpack Photon - Support Text Widgets.
  258. *
  259. * @access public
  260. * @param string $content Content from text widget.
  261. * @return string
  262. */
  263. function jetpack_photon_support_text_widgets( $content ) {
  264. if ( class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' ) ) {
  265. return Jetpack_Photon::filter_the_content( $content );
  266. }
  267. return $content;
  268. }
  269. add_filter( 'widget_text', 'jetpack_photon_support_text_widgets' );