| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server side implementation of load more handling. |
| 4 |
* |
| 5 |
* @package categoryposts. |
| 6 |
* |
| 7 |
* @since 4.9 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace categoryPosts; |
| 11 |
|
| 12 |
// Don't call the file directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Embed the front end JS for load more. |
| 19 |
* |
| 20 |
* @since 4.9 |
| 21 |
*/ |
| 22 |
function embed_loadmore_scripts() { |
| 23 |
echo '<script>{'; |
| 24 |
$suffix = 'min.js'; |
| 25 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) { |
| 26 |
$suffix = 'js'; |
| 27 |
} |
| 28 |
echo 'var tiptoppress = Array();'; |
| 29 |
echo 'tiptoppress["' . esc_js( __NAMESPACE__ ) . '"] = { json_root_url : "' . esc_js( rest_url( __NAMESPACE__ . '/loadmore' ) ) . '"};'; |
| 30 |
include __DIR__ . '/js/frontend/loadmore.' . $suffix; |
| 31 |
echo '}</script>'; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Generate the JSON response which includes additional element as a response |
| 36 |
* to a "load more" request. |
| 37 |
* |
| 38 |
* @param \WP_REST_Request $request The rest request with widget aand start point info. |
| 39 |
*/ |
| 40 |
function get_next_elements( \WP_REST_Request $request ) { |
| 41 |
$id = (string) $request['id']; |
| 42 |
$start = (int) $request['start']; |
| 43 |
$number = (int) $request['number']; |
| 44 |
$context = (string) $request['context']; |
| 45 |
|
| 46 |
$ret = array(); |
| 47 |
|
| 48 |
$id_components = explode( '-', $id ); |
| 49 |
if ( 2 <= count( $id_components ) ) { |
| 50 |
switch ( $id_components[0] ) { |
| 51 |
case 'shortcode': |
| 52 |
if ( 2 === count( $id_components ) || 3 === count( $id_components ) ) { |
| 53 |
$pid = $id_components[1]; // The ID of the relevant post. |
| 54 |
$name = isset( $id_components[2] ) ? $id_components[2] : ''; // The shortcode "name". |
| 55 |
$settings = shortcode_settings( $pid, $name ); |
| 56 |
if ( ! empty( $settings ) ) { |
| 57 |
$virtual_widget = new Virtual_Widget( '', '', $settings ); |
| 58 |
$ret = $virtual_widget->get_elements_HTML( $start, $number, $context ); |
| 59 |
} |
| 60 |
} |
| 61 |
break; |
| 62 |
case 'widget': |
| 63 |
if ( 2 === count( $id_components ) ) { |
| 64 |
$id = $id_components[1]; // The ID of the widget. |
| 65 |
$class = __NAMESPACE__ . '\Widget'; |
| 66 |
$widgetclass = new $class(); |
| 67 |
$allsettings = $widgetclass->get_settings(); |
| 68 |
if ( isset( $allsettings[ $id ] ) ) { |
| 69 |
$virtual_widget = new Virtual_Widget( '', '', $allsettings[ $id ] ); |
| 70 |
$ret = $virtual_widget->get_elements_HTML( $start, $number, $context ); |
| 71 |
} |
| 72 |
} |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return new \WP_REST_Response( $ret ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* This function is where we register our routes for our example endpoint. |
| 82 |
*/ |
| 83 |
function register_route() { |
| 84 |
register_rest_route( |
| 85 |
__NAMESPACE__, |
| 86 |
'/loadmore/(?P<id>[\w-]+)/(?P<start>[\d]+)/(?P<number>[\d]+)/(?P<context>[\w]+)', |
| 87 |
array( |
| 88 |
'methods' => 'GET', |
| 89 |
'callback' => __NAMESPACE__ . '\get_next_elements', |
| 90 |
'permission_callback' => '__return_true', |
| 91 |
) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
add_action( 'rest_api_init', __NAMESPACE__ . '\register_route' ); |
| 96 |
|