PageRenderTime 29ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/functions.photon.php

https://gitlab.com/Gashler/sg
PHP | 190 lines | 114 code | 37 blank | 39 comment | 34 complexity | b26c193737ec30ca5fca8d72f62d421d MD5 | raw file
  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 ( false !== apply_filters( 'jetpack_photon_skip_for_url', false, $image_url, $args, $scheme ) ) {
  14. return $image_url;
  15. }
  16. $image_url = apply_filters( 'jetpack_photon_pre_image_url', $image_url, $args, $scheme );
  17. $args = apply_filters( 'jetpack_photon_pre_args', $args, $image_url, $scheme );
  18. if ( empty( $image_url ) )
  19. return $image_url;
  20. $image_url_parts = @parse_url( $image_url );
  21. // Unable to parse
  22. if ( ! is_array( $image_url_parts ) || empty( $image_url_parts['host'] ) || empty( $image_url_parts['path'] ) )
  23. return $image_url;
  24. if ( is_array( $args ) ){
  25. // Convert values that are arrays into strings
  26. foreach ( $args as $arg => $value ) {
  27. if ( is_array( $value ) ) {
  28. $args[$arg] = implode( ',', $value );
  29. }
  30. }
  31. // Encode values
  32. // See http://core.trac.wordpress.org/ticket/17923
  33. $args = rawurlencode_deep( $args );
  34. }
  35. // You can't run a Photon URL through Photon again because query strings are stripped.
  36. // So if the image is already a Photon URL, append the new arguments to the existing URL.
  37. if ( in_array( $image_url_parts['host'], array( 'i0.wp.com', 'i1.wp.com', 'i2.wp.com' ) ) ) {
  38. $photon_url = add_query_arg( $args, $image_url );
  39. return jetpack_photon_url_scheme( $photon_url, $scheme );
  40. }
  41. // This setting is Photon Server dependent
  42. if ( ! apply_filters( 'jetpack_photon_any_extension_for_domain', false, $image_url_parts['host'] ) ) {
  43. // Photon doesn't support query strings so we ignore them and look only at the path.
  44. // However some source images are served via PHP so check the no-query-string extension.
  45. // For future proofing, this is a blacklist of common issues rather than a whitelist.
  46. $extension = pathinfo( $image_url_parts['path'], PATHINFO_EXTENSION );
  47. if ( empty( $extension ) || in_array( $extension, array( 'php' ) ) )
  48. return $image_url;
  49. }
  50. $image_host_path = $image_url_parts['host'] . $image_url_parts['path'];
  51. // Figure out which CDN subdomain to use
  52. srand( crc32( $image_host_path ) );
  53. $subdomain = rand( 0, 2 );
  54. srand();
  55. /**
  56. * Filters the domain used by the Photon module.
  57. *
  58. * @since 3.4.2
  59. *
  60. * @param string http://i{$subdomain}.wp.com Domain used by Photon. $subdomain is a random number between 0 and 2.
  61. * @param string $image_url URL of the image to be photonized.
  62. */
  63. $photon_domain = apply_filters( 'jetpack_photon_domain', "http://i{$subdomain}.wp.com", $image_url );
  64. $photon_domain = trailingslashit( $photon_domain );
  65. $photon_url = $photon_domain . $image_host_path;
  66. // This setting is Photon Server dependent
  67. if ( isset( $image_url_parts['query'] ) && apply_filters( 'jetpack_photon_add_query_string_to_domain', false, $image_url_parts['host'] ) ) {
  68. $photon_url .= '?q=' . rawurlencode( $image_url_parts['query'] );
  69. }
  70. if ( $args ) {
  71. if ( is_array( $args ) ) {
  72. $photon_url = add_query_arg( $args, $photon_url );
  73. } else {
  74. // You can pass a query string for complicated requests but where you still want CDN subdomain help, etc.
  75. $photon_url .= '?' . $args;
  76. }
  77. }
  78. return jetpack_photon_url_scheme( $photon_url, $scheme );
  79. }
  80. add_filter( 'jetpack_photon_url', 'jetpack_photon_url', 10, 3 );
  81. /**
  82. * WordPress.com
  83. *
  84. * If a cropped WP.com-hosted image is the source image, have Photon replicate the crop.
  85. */
  86. add_filter( 'jetpack_photon_pre_args', 'jetpack_photon_parse_wpcom_query_args', 10, 2 );
  87. function jetpack_photon_parse_wpcom_query_args( $args, $image_url ) {
  88. $parsed_url = @parse_url( $image_url );
  89. if ( ! $parsed_url )
  90. return $args;
  91. $image_url_parts = wp_parse_args( $parsed_url, array(
  92. 'host' => '',
  93. 'query' => ''
  94. ) );
  95. if ( '.files.wordpress.com' != substr( $image_url_parts['host'], -20 ) )
  96. return $args;
  97. if ( empty( $image_url_parts['query'] ) )
  98. return $args;
  99. $wpcom_args = wp_parse_args( $image_url_parts['query'] );
  100. if ( empty( $wpcom_args['w'] ) || empty( $wpcom_args['h'] ) )
  101. return $args;
  102. // Keep the crop by using "resize"
  103. if ( ! empty( $wpcom_args['crop'] ) ) {
  104. if ( is_array( $args ) ) {
  105. $args = array_merge( array( 'resize' => array( $wpcom_args['w'], $wpcom_args['h'] ) ), $args );
  106. } else {
  107. $args = 'resize=' . rawurlencode( absint( $wpcom_args['w'] ) . ',' . absint( $wpcom_args['h'] ) ) . '&' . $args;
  108. }
  109. } else {
  110. if ( is_array( $args ) ) {
  111. $args = array_merge( array( 'fit' => array( $wpcom_args['w'], $wpcom_args['h'] ) ), $args );
  112. } else {
  113. $args = 'fit=' . rawurlencode( absint( $wpcom_args['w'] ) . ',' . absint( $wpcom_args['h'] ) ) . '&' . $args;
  114. }
  115. }
  116. return $args;
  117. }
  118. /**
  119. * Facebook
  120. */
  121. add_filter( 'jetpack_photon_add_query_string_to_domain', 'jetpack_photon_allow_facebook_graph_domain', 10, 2 );
  122. add_filter( 'jetpack_photon_any_extension_for_domain', 'jetpack_photon_allow_facebook_graph_domain', 10, 2 );
  123. function jetpack_photon_url_scheme( $url, $scheme ) {
  124. if ( ! in_array( $scheme, array( 'http', 'https', 'network_path' ) ) ) {
  125. $scheme = is_ssl() ? 'https' : 'http';
  126. }
  127. if ( 'network_path' == $scheme ) {
  128. $scheme_slashes = '//';
  129. } else {
  130. $scheme_slashes = "$scheme://";
  131. }
  132. return preg_replace( '#^[a-z:]+//#i', $scheme_slashes, $url );
  133. }
  134. function jetpack_photon_allow_facebook_graph_domain( $allow = false, $domain ) {
  135. switch ( $domain ) {
  136. case 'graph.facebook.com' :
  137. return true;
  138. }
  139. return $allow;
  140. }
  141. add_filter( 'jetpack_photon_skip_for_url', 'jetpack_photon_banned_domains', 9, 4 );
  142. function jetpack_photon_banned_domains( $skip, $image_url, $args, $scheme ) {
  143. $banned_domains = array(
  144. 'http://chart.googleapis.com/',
  145. 'https://chart.googleapis.com/',
  146. 'http://chart.apis.google.com/',
  147. );
  148. foreach ( $banned_domains as $banned_domain ) {
  149. if ( wp_startswith( $image_url, $banned_domain ) )
  150. return true;
  151. }
  152. return $skip;
  153. }