kollapsminoriteten/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-render...

180 lines
5.4 KiB
PHP
Raw Normal View History

2022-06-16 14:01:47 +02:00
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
/**
* These are helpers for the shortcode and embed render endpoints.
*/
2019-11-15 23:26:29 +01:00
abstract class WPCOM_JSON_API_Render_Endpoint extends WPCOM_JSON_API_Endpoint {
2022-06-16 14:01:47 +02:00
/**
2019-11-15 23:26:29 +01:00
* Figure out what scripts and styles to load.
* props to o2's o2_Read_API::poll() function for inspiration.
*
* In short we figure out what scripts load for a "normal" page load by executing wp_head and wp_footer
* then we render the embed/shortcode (to both get our result, and to have the shortcode files enqueue their resources)
* then we load wp_head and wp_footer again to see what new resources were added
* finally we find out the url to the source file and any extra info (like media or init js)
2022-06-16 14:01:47 +02:00
*
* @param mixed $callback - the function callback.
* @param mixed $callback_arg - the callback arguments.
*
* @return array
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function process_render( $callback, $callback_arg ) {
2019-11-15 23:26:29 +01:00
global $wp_scripts, $wp_styles;
2020-11-18 09:10:44 +01:00
if ( false === defined( 'STYLESHEETPATH' ) ) {
wp_templating_constants();
}
2019-11-15 23:26:29 +01:00
// initial scripts & styles (to subtract)
ob_start();
wp_head();
wp_footer();
ob_end_clean();
$initial_scripts = $wp_scripts->done;
2022-06-16 14:01:47 +02:00
$initial_styles = $wp_styles->done;
2019-11-15 23:26:29 +01:00
// actually render the shortcode, get the result, and do the resource loading again so we can subtract..
ob_start();
wp_head();
ob_end_clean();
$result = call_user_func( $callback, $callback_arg );
ob_start();
wp_footer();
ob_end_clean();
// find the difference (the new resource files)
$loaded_scripts = array_diff( $wp_scripts->done, $initial_scripts );
2022-06-16 14:01:47 +02:00
$loaded_styles = array_diff( $wp_styles->done, $initial_styles );
2019-11-15 23:26:29 +01:00
return array(
2022-06-16 14:01:47 +02:00
'result' => $result,
2019-11-15 23:26:29 +01:00
'loaded_scripts' => $loaded_scripts,
2022-06-16 14:01:47 +02:00
'loaded_styles' => $loaded_styles,
2019-11-15 23:26:29 +01:00
);
}
/**
2022-06-16 14:01:47 +02:00
* Takes the list of styles and scripts and adds them to the JSON response.
*
* @param array $return - what was returned.
* @param array $loaded_scripts - the loaded scripts.
* @param array $loaded_styles - the loaded styles.
*
* @return array
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function add_assets( $return, $loaded_scripts, $loaded_styles ) {
2019-11-15 23:26:29 +01:00
global $wp_scripts, $wp_styles;
// scripts first, just cuz
if ( count( $loaded_scripts ) > 0 ) {
$scripts = array();
foreach ( $loaded_scripts as $handle ) {
2022-06-16 14:01:47 +02:00
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
2019-11-15 23:26:29 +01:00
continue;
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
$src = $wp_scripts->registered[ $handle ]->src;
// attach version and an extra query parameters
$ver = $this->get_version( $wp_scripts->registered[ $handle ]->ver, $wp_scripts->default_version );
if ( isset( $wp_scripts->args[ $handle ] ) ) {
2022-06-16 14:01:47 +02:00
$ver = $ver ? $ver . '&amp;' . $wp_scripts->args[ $handle ] : $wp_scripts->args[ $handle ];
2019-11-15 23:26:29 +01:00
}
$src = add_query_arg( 'ver', $ver, $src );
// add to an aray so we can return all this info
$scripts[ $handle ] = array(
'src' => $src,
);
2022-06-16 14:01:47 +02:00
$extra = $wp_scripts->print_extra_script( $handle, false );
if ( ! empty( $extra ) ) {
$scripts[ $handle ]['extra'] = $extra;
2019-11-15 23:26:29 +01:00
}
}
$return['scripts'] = $scripts;
}
// now styles
if ( count( $loaded_styles ) > 0 ) {
$styles = array();
foreach ( $loaded_styles as $handle ) {
2022-06-16 14:01:47 +02:00
if ( ! isset( $wp_styles->registered[ $handle ] ) ) {
2019-11-15 23:26:29 +01:00
continue;
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
$src = $wp_styles->registered[ $handle ]->src;
// attach version and an extra query parameters
$ver = $this->get_version( $wp_styles->registered[ $handle ]->ver, $wp_styles->default_version );
if ( isset( $wp_styles->args[ $handle ] ) ) {
2022-06-16 14:01:47 +02:00
$ver = $ver ? $ver . '&amp;' . $wp_styles->args[ $handle ] : $wp_styles->args[ $handle ];
2019-11-15 23:26:29 +01:00
}
$src = add_query_arg( 'ver', $ver, $src );
// is there a special media (print, screen, etc) for this? if not, default to 'all'
$media = 'all';
if ( isset( $wp_styles->registered[ $handle ]->args ) ) {
$media = esc_attr( $wp_styles->registered[ $handle ]->args );
}
// add to an array so we can return all this info
2022-06-16 14:01:47 +02:00
$styles[ $handle ] = array(
'src' => $src,
2019-11-15 23:26:29 +01:00
'media' => $media,
);
}
$return['styles'] = $styles;
}
return $return;
}
/**
2022-06-16 14:01:47 +02:00
* Returns the 'version' string set by the shortcode so different versions of scripts/styles can be loaded.
*
* @param string $this_scripts_version - this scripts version.
* @param string $default_version - the default version.
*
* @return string
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function get_version( $this_scripts_version, $default_version ) {
2019-11-15 23:26:29 +01:00
if ( null === $this_scripts_version ) {
$ver = '';
} else {
$ver = $this_scripts_version ? $this_scripts_version : $default_version;
}
return $ver;
}
/**
2022-06-16 14:01:47 +02:00
* Given a shortcode, process and return the result.
*
* @param string $shortcode - the shortcode.
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function do_shortcode( $shortcode ) {
2019-11-15 23:26:29 +01:00
return do_shortcode( $shortcode );
}
/**
2022-06-16 14:01:47 +02:00
* Given a one-line embed URL, process and return the result.
*
* @param string $embed_url - the embed URL.
*
* @return string|false
2019-11-15 23:26:29 +01:00
*/
2022-06-16 14:01:47 +02:00
public function do_embed( $embed_url ) {
2019-11-15 23:26:29 +01:00
// in order for oEmbed to fire in the `$wp_embed->shortcode` method, we need to set a post as the current post
2022-06-16 14:01:47 +02:00
$_posts = get_posts(
array(
'posts_per_page' => 1,
'suppress_filters' => false,
)
);
2019-11-15 23:26:29 +01:00
if ( ! empty( $_posts ) ) {
global $post;
2022-06-16 14:01:47 +02:00
$post = array_shift( $_posts ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
2019-11-15 23:26:29 +01:00
}
global $wp_embed;
return $wp_embed->shortcode( array(), $embed_url );
}
2020-11-18 09:10:44 +01:00
}