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