Açıklama Yok

functions.photon.php 11KB

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