WPCOM_JSON_API_Render_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Render Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-render-endpoint.php  
  1. abstract class WPCOM_JSON_API_Render_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. /** 
  4. * Figure out what scripts and styles to load. 
  5. * props to o2's o2_Read_API::poll() function for inspiration. 
  6. * In short we figure out what scripts load for a "normal" page load by executing wp_head and wp_footer 
  7. * then we render our shortcode (to both get our result, and to have the shortcode files enqueue their resources) 
  8. * then we load wp_head and wp_footer again to see what new resources were added 
  9. * finally we find out the url to the source file and any extra info (like media or init js) 
  10. */ 
  11. function process_render( $callback, $callback_arg ) { 
  12. global $wp_scripts, $wp_styles; 
  13.  
  14. // initial scripts & styles (to subtract) 
  15. ob_start(); 
  16. wp_head(); 
  17. wp_footer(); 
  18. ob_end_clean(); 
  19. $initial_scripts = $wp_scripts->done; 
  20. $initial_styles = $wp_styles->done; 
  21.  
  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.  
  31. // find the difference (the new resource files) 
  32. $loaded_scripts = array_diff( $wp_scripts->done, $initial_scripts ); 
  33. $loaded_styles = array_diff( $wp_styles->done, $initial_styles ); 
  34. return array( 
  35. 'result' => $result,  
  36. 'loaded_scripts' => $loaded_scripts,  
  37. 'loaded_styles' => $loaded_styles,  
  38. ); 
  39.  
  40. /** 
  41. * Takes the list of styles and scripts and adds them to the JSON response 
  42. */ 
  43. function add_assets( $return, $loaded_scripts, $loaded_styles ) { 
  44. global $wp_scripts, $wp_styles; 
  45. // scripts first, just cuz 
  46. if ( count( $loaded_scripts ) > 0 ) { 
  47. $scripts = array(); 
  48. foreach ( $loaded_scripts as $handle ) { 
  49. if ( !isset( $wp_scripts->registered[ $handle ] ) ) 
  50. continue; 
  51.  
  52. $src = $wp_scripts->registered[ $handle ]->src; 
  53.  
  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 . '&' . $wp_scripts->args[$handle] : $wp_scripts->args[$handle]; 
  58. $src = add_query_arg( 'ver', $ver, $src ); 
  59.  
  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. $return['scripts'] = $scripts; 
  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.  
  75. $src = $wp_styles->registered[ $handle ]->src; 
  76.  
  77. // attach version and an extra query parameters 
  78. $ver = $this->get_version( $wp_styles->registered[ $handle ]->ver, $wp_styles->default_version ); 
  79. if ( isset( $wp_styles->args[ $handle ] ) ) { 
  80. $ver = $ver ? $ver . '&' . $wp_styles->args[$handle] : $wp_styles->args[$handle]; 
  81. $src = add_query_arg( 'ver', $ver, $src ); 
  82.  
  83. // is there a special media (print, screen, etc) for this? if not, default to 'all' 
  84. $media = 'all'; 
  85. if ( isset( $wp_styles->registered[ $handle ]->args ) ) { 
  86. $media = esc_attr( $wp_styles->registered[ $handle ]->args ); 
  87.  
  88. // add to an aray so we can return all this info 
  89. $styles[ $handle ] = array ( 
  90. 'src' => $src,  
  91. 'media' => $media,  
  92. ); 
  93.  
  94. $return['styles'] = $styles; 
  95.  
  96. return $return; 
  97.  
  98. /** 
  99. * Returns the 'version' string set by the shortcode so different versions of scripts/styles can be loaded 
  100. */ 
  101. function get_version( $this_scripts_version, $default_version ) { 
  102. if ( null === $this_scripts_version ) { 
  103. $ver = ''; 
  104. } else { 
  105. $ver = $this_scripts_version ? $this_scripts_version : $default_version; 
  106. return $ver; 
  107.