Няма описание

class.wpcom-json-api-render-endpoint.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // these are helpers for the shortcode and embed render endpoints
  3. abstract class WPCOM_JSON_API_Render_Endpoint extends WPCOM_JSON_API_Endpoint {
  4. /*
  5. * Figure out what scripts and styles to load.
  6. * props to o2's o2_Read_API::poll() function for inspiration.
  7. *
  8. * In short we figure out what scripts load for a "normal" page load by executing wp_head and wp_footer
  9. * then we render the embed/shortcode (to both get our result, and to have the shortcode files enqueue their resources)
  10. * then we load wp_head and wp_footer again to see what new resources were added
  11. * finally we find out the url to the source file and any extra info (like media or init js)
  12. */
  13. function process_render( $callback, $callback_arg ) {
  14. global $wp_scripts, $wp_styles;
  15. if ( false === defined( 'STYLESHEETPATH' ) ) {
  16. wp_templating_constants();
  17. }
  18. // initial scripts & styles (to subtract)
  19. ob_start();
  20. wp_head();
  21. wp_footer();
  22. ob_end_clean();
  23. $initial_scripts = $wp_scripts->done;
  24. $initial_styles = $wp_styles->done;
  25. // actually render the shortcode, get the result, and do the resource loading again so we can subtract..
  26. ob_start();
  27. wp_head();
  28. ob_end_clean();
  29. $result = call_user_func( $callback, $callback_arg );
  30. ob_start();
  31. wp_footer();
  32. ob_end_clean();
  33. // find the difference (the new resource files)
  34. $loaded_scripts = array_diff( $wp_scripts->done, $initial_scripts );
  35. $loaded_styles = array_diff( $wp_styles->done, $initial_styles );
  36. return array(
  37. 'result' => $result,
  38. 'loaded_scripts' => $loaded_scripts,
  39. 'loaded_styles' => $loaded_styles,
  40. );
  41. }
  42. /**
  43. * Takes the list of styles and scripts and adds them to the JSON response
  44. */
  45. function add_assets( $return, $loaded_scripts, $loaded_styles ) {
  46. global $wp_scripts, $wp_styles;
  47. // scripts first, just cuz
  48. if ( count( $loaded_scripts ) > 0 ) {
  49. $scripts = array();
  50. foreach ( $loaded_scripts as $handle ) {
  51. if ( !isset( $wp_scripts->registered[ $handle ] ) )
  52. continue;
  53. $src = $wp_scripts->registered[ $handle ]->src;
  54. // attach version and an extra query parameters
  55. $ver = $this->get_version( $wp_scripts->registered[ $handle ]->ver, $wp_scripts->default_version );
  56. if ( isset( $wp_scripts->args[ $handle ] ) ) {
  57. $ver = $ver ? $ver . '&amp;' . $wp_scripts->args[$handle] : $wp_scripts->args[$handle];
  58. }
  59. $src = add_query_arg( 'ver', $ver, $src );
  60. // add to an aray so we can return all this info
  61. $scripts[ $handle ] = array(
  62. 'src' => $src,
  63. );
  64. $extra = $wp_scripts->print_extra_script( $handle, false );
  65. if ( !empty( $extra ) ) {
  66. $scripts[$handle]['extra'] = $extra;
  67. }
  68. }
  69. $return['scripts'] = $scripts;
  70. }
  71. // now styles
  72. if ( count( $loaded_styles ) > 0 ) {
  73. $styles = array();
  74. foreach ( $loaded_styles as $handle ) {
  75. if ( !isset( $wp_styles->registered[ $handle ] ) )
  76. continue;
  77. $src = $wp_styles->registered[ $handle ]->src;
  78. // attach version and an extra query parameters
  79. $ver = $this->get_version( $wp_styles->registered[ $handle ]->ver, $wp_styles->default_version );
  80. if ( isset( $wp_styles->args[ $handle ] ) ) {
  81. $ver = $ver ? $ver . '&amp;' . $wp_styles->args[$handle] : $wp_styles->args[$handle];
  82. }
  83. $src = add_query_arg( 'ver', $ver, $src );
  84. // is there a special media (print, screen, etc) for this? if not, default to 'all'
  85. $media = 'all';
  86. if ( isset( $wp_styles->registered[ $handle ]->args ) ) {
  87. $media = esc_attr( $wp_styles->registered[ $handle ]->args );
  88. }
  89. // add to an array so we can return all this info
  90. $styles[ $handle ] = array (
  91. 'src' => $src,
  92. 'media' => $media,
  93. );
  94. }
  95. $return['styles'] = $styles;
  96. }
  97. return $return;
  98. }
  99. /**
  100. * Returns the 'version' string set by the shortcode so different versions of scripts/styles can be loaded
  101. */
  102. function get_version( $this_scripts_version, $default_version ) {
  103. if ( null === $this_scripts_version ) {
  104. $ver = '';
  105. } else {
  106. $ver = $this_scripts_version ? $this_scripts_version : $default_version;
  107. }
  108. return $ver;
  109. }
  110. /**
  111. * given a shortcode, process and return the result
  112. */
  113. function do_shortcode( $shortcode ) {
  114. return do_shortcode( $shortcode );
  115. }
  116. /**
  117. * given a one-line embed URL, process and return the result
  118. */
  119. function do_embed( $embed_url ) {
  120. // in order for oEmbed to fire in the `$wp_embed->shortcode` method, we need to set a post as the current post
  121. $_posts = get_posts( array( 'posts_per_page' => 1, 'suppress_filters' => false ) );
  122. if ( ! empty( $_posts ) ) {
  123. global $post;
  124. $post = array_shift( $_posts );
  125. }
  126. global $wp_embed;
  127. return $wp_embed->shortcode( array(), $embed_url );
  128. }
  129. }