PluginProbe
Qi Blocks / 1.0.1
Qi Blocks v1.0.1
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 / blog / class-qi-blocks-blog-rest-api.php

class-qi-blocks-blog-rest-api.php in Qi Blocks 1.0.1, at inc/blog/class-qi-blocks-blog-rest-api.php

133 lines 4.5 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_Blog_Rest_API {
4 private static $instance;
5
6 public function __construct() {
7
8 // Extend main rest api routes with new case
9 add_filter( 'qi_blocks_filter_rest_api_routes', array( $this, 'add_rest_api_routes' ) );
10 }
11
12 /**
13 * @return Qi_Blocks_Blog_Rest_API
14 */
15 public static function get_instance() {
16 if ( is_null( self::$instance ) ) {
17 self::$instance = new self();
18 }
19
20 return self::$instance;
21 }
22
23 function add_rest_api_routes( $routes ) {
24
25 $routes['get-blog-posts'] = array(
26 'route' => 'get-blog-posts',
27 'methods' => WP_REST_Server::CREATABLE,
28 'callback' => array( $this, 'get_blog_posts_callback' ),
29 'permission_callback' => function () {
30 return current_user_can( 'edit_posts' );
31 },
32 'args' => array(
33 'queryAttributes' => array(
34 'required' => true,
35 'validate_callback' => function ( $param ) {
36 return intval( $param );
37 },
38 ),
39 ),
40 );
41
42 return $routes;
43 }
44
45 function get_blog_posts_callback( $response ) {
46 $results = array();
47
48 if ( ! isset( $response ) || empty( $response->get_body() ) ) {
49 qi_blocks_get_ajax_status( 'error', esc_html__( 'Rest is invalid', 'qi-blocks' ), array() );
50 } else {
51 $response_data = json_decode( $response->get_body() );
52
53 if ( ! empty( $response_data ) ) {
54 $atts = (array) $response_data->queryAttributes;
55 $inherit_global_query = isset( $atts['inheritGlobalQuery'] ) && ! empty( $atts['inheritGlobalQuery'] );
56
57 if ( $inherit_global_query ) {
58 $query_result = new WP_Query( array( 'post_type' => 'post' ) );
59 } else {
60 $atts['additional_query_args'] = qi_blocks_get_additional_query_args( $atts );
61
62 $query_result = new WP_Query( qi_blocks_get_query_params( $atts ) );
63 }
64
65 $results['maxNumPages'] = $query_result->max_num_pages;
66 $posts = array();
67
68 if ( $query_result->have_posts() ) {
69 while ( $query_result->have_posts() ) :
70 $query_result->the_post();
71 $post_id = get_the_ID();
72 $post_classes = get_post_class( 'qodef-blog-item' );
73 $date_link = empty( get_the_title() ) && ! is_single() ? get_the_permalink() : get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) );
74
75 $featured_image = '';
76 if ( has_post_thumbnail( $post_id ) ) {
77 $featured_image = qi_blocks_get_post_image( $post_id, $atts['imagesProportion'], intval( $atts['customImageWidth'] ), intval( $atts['customImageHeight'] ) );
78 }
79
80 $date_classes = 'qodef-e-info-item qodef-e-info-date entry-date';
81 if ( is_single() || is_page() || is_archive() ) {
82 $date_classes .= ' published updated';
83 }
84
85 $excerpt = get_the_excerpt();
86 $excerpt_length = 180;
87 $new_excerpt = '';
88
89 if ( isset( $atts['excerptLength'] ) && '' !== $atts['excerptLength'] ) {
90 $excerpt_length = $atts['excerptLength'];
91 }
92
93 if ( ! empty( $excerpt ) ) {
94 $new_excerpt = ( intval( $excerpt_length ) > 0 ) ? substr( $excerpt, 0, intval( $excerpt_length ) ) : $excerpt;
95 }
96
97 $posts[] = array(
98 'postID' => $post_id,
99 'blogItemClasses' => $post_classes,
100 'isPasswordRequired' => post_password_required(),
101 'authorAvatar' => get_avatar( get_the_author_meta( 'ID' ), '48' ),
102 'authorName' => get_the_author_meta( 'display_name' ),
103 'authorURL' => esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
104 'postCategories' => get_the_category_list( '<span class="qodef-category-separator"></span>' ),
105 'featuredImage' => $featured_image,
106 'dateLink' => $date_link,
107 'dateBoxed' => esc_html( get_the_time( 'j' ) . ' ' . get_the_time( 'M' ) ),
108 'date' => esc_html( get_the_time( get_option( 'date_format' ) ) ),
109 'dateClasses' => $date_classes,
110 'passwordForm' => get_the_password_form(),
111 'excerpt' => esc_html( strip_tags( strip_shortcodes( $new_excerpt ) ) ),
112 'title' => get_the_title(),
113 'permalink' => get_the_permalink(),
114 );
115
116 endwhile; // End of the loop
117
118 $results['queriedPostsData'] = $posts;
119
120 qi_blocks_get_ajax_status( 'success', esc_html__( 'Posts are successfully returned', 'qi-blocks' ), $results );
121 } else {
122 qi_blocks_get_ajax_status( 'success', esc_html__( 'No posts matching query!', 'qi-blocks' ), $results );
123 }
124
125 wp_reset_postdata();
126 }
127 }
128 }
129
130 }
131
132 Qi_Blocks_Blog_Rest_API::get_instance();
133