PluginProbe
Category Posts Block / trunk
Category Posts Block vtrunk
5.0.0 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.2 2.3 3.1 3.2 3.3 4.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 All 62 releases
category-posts / loadmore.php

loadmore.php in Category Posts Block trunk, at loadmore.php

125 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Normalize a block load-more ID so stored block settings can be resolved
36 * consistently for both the prefixed and unprefixed form.
37 *
38 * @param string $id The load-more identifier.
39 *
40 * @return string The normalized block ID.
41 */
42 function normalize_block_loadmore_id( $id ) {
43 if ( empty( $id ) ) {
44 return '';
45 }
46
47 return 0 === strpos( $id, 'block-' ) ? $id : 'block-' . $id;
48 }
49
50 /**
51 * Generate the JSON response which includes additional element as a response
52 * to a "load more" request.
53 *
54 * @param \WP_REST_Request $request The rest request with widget aand start point info.
55 */
56 function get_next_elements( \WP_REST_Request $request ) {
57 $id = (string) $request['id'];
58 $start = (int) $request['start'];
59 $number = (int) $request['number'];
60 $context = (string) $request['context'];
61
62 $ret = array();
63
64 $id_components = explode( '-', $id );
65 if ( 2 <= count( $id_components ) ) {
66 switch ( $id_components[0] ) {
67 case 'shortcode':
68 if ( 2 === count( $id_components ) || 3 === count( $id_components ) ) {
69 $pid = $id_components[1]; // The ID of the relevant post.
70 $name = isset( $id_components[2] ) ? $id_components[2] : ''; // The shortcode "name".
71 $settings = shortcode_settings( $pid, $name );
72 if ( ! empty( $settings ) ) {
73 $settings['context'] = CONTEXT_SHORTCODE;
74 $virtual_widget = new Virtual_Widget( '', '', $settings );
75 $ret = $virtual_widget->get_elements_HTML( $start, $number, $context );
76 }
77 }
78 break;
79 case 'widget':
80 if ( 2 === count( $id_components ) ) {
81 $id = $id_components[1]; // The ID of the widget.
82 $class = __NAMESPACE__ . '\Widget';
83 $widgetclass = new $class();
84 $allsettings = $widgetclass->get_settings();
85 if ( isset( $allsettings[ $id ] ) ) {
86 $allsettings[ $id ]['context'] = CONTEXT_WIDGET;
87 $virtual_widget = new Virtual_Widget( '', '', $allsettings[ $id ] );
88 $ret = $virtual_widget->get_elements_HTML( $start, $number, $context );
89 }
90 }
91 break;
92 case 'block':
93 if ( 2 === count( $id_components ) ) {
94 $block_id = normalize_block_loadmore_id( $id_components[1] );
95 $settings = get_block_loadmore_settings( $block_id );
96 if ( false !== $settings ) {
97 $instance = build_block_instance( $settings );
98 $widget = new Widget();
99 $ret = $widget->get_elements_HTML( $instance, $context, $start, $number );
100 }
101 }
102 break;
103 }
104 }
105
106 return new \WP_REST_Response( $ret );
107 }
108
109 /**
110 * This function is where we register our routes for our example endpoint.
111 */
112 function register_route() {
113 register_rest_route(
114 __NAMESPACE__,
115 '/loadmore/(?P<id>[\w-]+)/(?P<start>[\d]+)/(?P<number>[\d]+)/(?P<context>[\w]+)',
116 array(
117 'methods' => 'GET',
118 'callback' => __NAMESPACE__ . '\get_next_elements',
119 'permission_callback' => '__return_true',
120 )
121 );
122 }
123
124 add_action( 'rest_api_init', __NAMESPACE__ . '\register_route' );
125