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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // initial scripts & styles (to subtract)
  16. ob_start();
  17. wp_head();
  18. wp_footer();
  19. ob_end_clean();
  20. $initial_scripts = $wp_scripts->done;
  21. $initial_styles = $wp_styles->done;
  22. // actually render the shortcode, get the result, and do the resource loading again so we can subtract..
  23. ob_start();
  24. wp_head();
  25. ob_end_clean();
  26. $result = call_user_func( $callback, $callback_arg );
  27. ob_start();
  28. wp_footer();
  29. ob_end_clean();
  30. // find the difference (the new resource files)
  31. $loaded_scripts = array_diff( $wp_scripts->done, $initial_scripts );
  32. $loaded_styles = array_diff( $wp_styles->done, $initial_styles );
  33. return array(
  34. 'result' => $result,
  35. 'loaded_scripts' => $loaded_scripts,
  36. 'loaded_styles' => $loaded_styles,
  37. );
  38. }
  39. /**
  40. * Takes the list of styles and scripts and adds them to the JSON response
  41. */
  42. function add_assets( $return, $loaded_scripts, $loaded_styles ) {
  43. global $wp_scripts, $wp_styles;
  44. // scripts first, just cuz
  45. if ( count( $loaded_scripts ) > 0 ) {
  46. $scripts = array();
  47. foreach ( $loaded_scripts as $handle ) {
  48. if ( !isset( $wp_scripts->registered[ $handle ] ) )
  49. continue;
  50. $src = $wp_scripts->registered[ $handle ]->src;
  51. // attach version and an extra query parameters
  52. $ver = $this->get_version( $wp_scripts->registered[ $handle ]->ver, $wp_scripts->default_version );
  53. if ( isset( $wp_scripts->args[ $handle ] ) ) {
  54. $ver = $ver ? $ver . '&amp;' . $wp_scripts->args[$handle] : $wp_scripts->args[$handle];
  55. }
  56. $src = add_query_arg( 'ver', $ver, $src );
  57. // add to an aray so we can return all this info
  58. $scripts[ $handle ] = array(
  59. 'src' => $src,
  60. );
  61. $extra = $wp_scripts->print_extra_script( $handle, false );
  62. if ( !empty( $extra ) ) {
  63. $scripts[$handle]['extra'] = $extra;
  64. }
  65. }
  66. $return['scripts'] = $scripts;
  67. }
  68. // now styles
  69. if ( count( $loaded_styles ) > 0 ) {
  70. $styles = array();
  71. foreach ( $loaded_styles as $handle ) {
  72. if ( !isset( $wp_styles->registered[ $handle ] ) )
  73. continue;
  74. $src = $wp_styles->registered[ $handle ]->src;
  75. // attach version and an extra query parameters
  76. $ver = $this->get_version( $wp_styles->registered[ $handle ]->ver, $wp_styles->default_version );
  77. if ( isset( $wp_styles->args[ $handle ] ) ) {
  78. $ver = $ver ? $ver . '&amp;' . $wp_styles->args[$handle] : $wp_styles->args[$handle];
  79. }
  80. $src = add_query_arg( 'ver', $ver, $src );
  81. // is there a special media (print, screen, etc) for this? if not, default to 'all'
  82. $media = 'all';
  83. if ( isset( $wp_styles->registered[ $handle ]->args ) ) {
  84. $media = esc_attr( $wp_styles->registered[ $handle ]->args );
  85. }
  86. // add to an array so we can return all this info
  87. $styles[ $handle ] = array (
  88. 'src' => $src,
  89. 'media' => $media,
  90. );
  91. }
  92. $return['styles'] = $styles;
  93. }
  94. return $return;
  95. }
  96. /**
  97. * Returns the 'version' string set by the shortcode so different versions of scripts/styles can be loaded
  98. */
  99. function get_version( $this_scripts_version, $default_version ) {
  100. if ( null === $this_scripts_version ) {
  101. $ver = '';
  102. } else {
  103. $ver = $this_scripts_version ? $this_scripts_version : $default_version;
  104. }
  105. return $ver;
  106. }
  107. /**
  108. * given a shortcode, process and return the result
  109. */
  110. function do_shortcode( $shortcode ) {
  111. return do_shortcode( $shortcode );
  112. }
  113. /**
  114. * given a one-line embed URL, process and return the result
  115. */
  116. function do_embed( $embed_url ) {
  117. // in order for oEmbed to fire in the `$wp_embed->shortcode` method, we need to set a post as the current post
  118. $_posts = get_posts( array( 'posts_per_page' => 1, 'suppress_filters' => false ) );
  119. if ( ! empty( $_posts ) ) {
  120. global $post;
  121. $post = array_shift( $_posts );
  122. }
  123. global $wp_embed;
  124. return $wp_embed->shortcode( array(), $embed_url );
  125. }
  126. }