PluginProbe
Qi Blocks / 1.0.7
Qi Blocks v1.0.7
1.5.3 1.5.2 1.5.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 All 45 releases
qi-blocks / inc / comments / class-qi-blocks-comments-rest-api.php

class-qi-blocks-comments-rest-api.php in Qi Blocks 1.0.7, at inc/comments/class-qi-blocks-comments-rest-api.php

74 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Qi_Blocks_Comments_Rest_API {
4 private static $instance;
5
6 public function __construct() {
7 // Extend main rest api routes with new case
8 add_filter( 'qi_blocks_filter_rest_api_routes', array( $this, 'add_rest_api_routes' ) );
9 }
10
11 /**
12 * @return Qi_Blocks_Comments_Rest_API
13 */
14 public static function get_instance() {
15 if ( is_null( self::$instance ) ) {
16 self::$instance = new self();
17 }
18
19 return self::$instance;
20 }
21
22 function add_rest_api_routes( $routes ) {
23 $routes['render-comments'] = array(
24 'route' => 'render-comments',
25 'methods' => WP_REST_Server::CREATABLE,
26 'callback' => array( $this, 'render_comments_callback' ),
27 'permission_callback' => function () {
28 return current_user_can( 'edit_posts' );
29 },
30 'args' => array(
31 'postID' => array(
32 'required' => true,
33 'validate_callback' => function ( $param ) {
34 return intval( $param );
35 },
36 ),
37 'attributes' => array(
38 'required' => true,
39 'validate_callback' => function ( $param ) {
40 return (array) $param;
41 },
42 ),
43 ),
44 );
45
46 return $routes;
47 }
48
49 function render_comments_callback( $response ) {
50
51 if ( ! isset( $response ) || empty( $response->get_body() ) ) {
52 qi_blocks_get_ajax_status( 'error', esc_html__( 'Rest is invalid', 'qi-blocks' ), array() );
53 } else {
54 $response_data = json_decode( $response->get_body() );
55 $post_ID = isset( $response_data->postID ) && ! empty( $response_data->postID ) ? intval( $response_data->postID ) : 0;
56 $attributes = isset( $response_data->attributes ) && ! empty( $response_data->attributes ) ? (array) $response_data->attributes : array();
57
58 if ( ! empty( $post_ID ) ) {
59 ob_start();
60
61 qi_blocks_template_part( 'comments', 'templates/comments-template', '', array( 'post_ID' => $post_ID, 'attributes' => $attributes, 'ajax' => true ) );
62
63 $html = ob_get_clean();
64
65 qi_blocks_get_ajax_status( 'success', esc_html__( 'Returned comments HTML content', 'qi-blocks' ), $html );
66 } else {
67 qi_blocks_get_ajax_status( 'error', esc_html__( 'Parameters are invalid', 'qi-blocks' ), array() );
68 }
69 }
70 }
71 }
72
73 Qi_Blocks_Comments_Rest_API::get_instance();
74