ACFV1.php
8 months ago
ElImport.php
8 months ago
FrontEndFilterV1.php
8 months ago
GetBuilderData.php
8 months ago
GetCategories.php
8 months ago
GetPostsV1.php
8 months ago
GetTermObject.php
8 months ago
GetTickerPostsV1.php
8 months ago
ImageSizeV1.php
8 months ago
RestApi.php
8 months ago
RestApi.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Api; |
| 4 | |
| 5 | use RT\ThePostGrid\Helpers\Fns; |
| 6 | |
| 7 | /** |
| 8 | * RestApi Class |
| 9 | */ |
| 10 | class RestApi { |
| 11 | /** |
| 12 | * Register rest route |
| 13 | */ |
| 14 | public function __construct() { |
| 15 | add_action( 'rest_api_init', [ $this, 'init_rest_routes' ], 99 ); |
| 16 | new ImageSizeV1(); |
| 17 | new GetPostsV1(); |
| 18 | new GetTickerPostsV1(); |
| 19 | new ACFV1(); |
| 20 | new FrontEndFilterV1(); |
| 21 | //new ElImport(); |
| 22 | new GetCategories(); |
| 23 | new GetBuilderData(); |
| 24 | new GetTermObject(); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Init rest route |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | public function init_rest_routes() { |
| 33 | $this->rttpg_register_rest_fields(); |
| 34 | } |
| 35 | |
| 36 | function rttpg_register_rest_fields() { |
| 37 | $post_type = Fns::get_post_types(); |
| 38 | |
| 39 | foreach ( $post_type as $key => $value ) { |
| 40 | |
| 41 | // Featured image. |
| 42 | register_rest_field( |
| 43 | $key, |
| 44 | 'rttpg_featured_image_url', |
| 45 | [ |
| 46 | 'get_callback' => [ $this, 'rttpg_get_featured_image_url' ], |
| 47 | 'update_callback' => null, |
| 48 | 'schema' => [ |
| 49 | 'description' => __( 'Different sized featured images' ), |
| 50 | 'type' => 'array', |
| 51 | ], |
| 52 | ] |
| 53 | ); |
| 54 | |
| 55 | // Author info. |
| 56 | register_rest_field( |
| 57 | $key, |
| 58 | 'rttpg_author', |
| 59 | [ |
| 60 | 'get_callback' => [ $this, 'rttpg_get_author_info' ], |
| 61 | 'update_callback' => null, |
| 62 | 'schema' => null, |
| 63 | ] |
| 64 | ); |
| 65 | |
| 66 | // Add comment info. |
| 67 | register_rest_field( |
| 68 | $key, |
| 69 | 'rttpg_comment', |
| 70 | [ |
| 71 | 'get_callback' => [ $this, 'rttpg_get_comment_info' ], |
| 72 | 'update_callback' => null, |
| 73 | 'schema' => null, |
| 74 | ] |
| 75 | ); |
| 76 | |
| 77 | // Category links. |
| 78 | register_rest_field( |
| 79 | $key, |
| 80 | 'rttpg_category', |
| 81 | [ |
| 82 | 'get_callback' => [ $this, 'rttpg_get_category_list' ], |
| 83 | 'update_callback' => null, |
| 84 | 'schema' => [ |
| 85 | 'description' => __( 'Category list links' ), |
| 86 | 'type' => 'string', |
| 87 | ], |
| 88 | ] |
| 89 | ); |
| 90 | |
| 91 | // Excerpt. |
| 92 | register_rest_field( |
| 93 | $key, |
| 94 | 'rttpg_excerpt', |
| 95 | [ |
| 96 | 'get_callback' => [ $this, 'rttpg_get_excerpt' ], |
| 97 | 'update_callback' => null, |
| 98 | 'schema' => null, |
| 99 | ] |
| 100 | ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | // Author. |
| 106 | function rttpg_get_author_info( $object ) { |
| 107 | $author = ( isset( $object['author'] ) ) ? $object['author'] : ''; |
| 108 | |
| 109 | $author_data['display_name'] = get_the_author_meta( 'display_name', $author ); |
| 110 | $author_data['author_link'] = get_author_posts_url( $author ); |
| 111 | |
| 112 | return $author_data; |
| 113 | } |
| 114 | |
| 115 | // Comment. |
| 116 | function rttpg_get_comment_info( $object ) { |
| 117 | $comments_count = wp_count_comments( $object['id'] ); |
| 118 | |
| 119 | return $comments_count->total_comments; |
| 120 | } |
| 121 | |
| 122 | // Category list. |
| 123 | |
| 124 | function rttpg_get_category_list( $object ) { |
| 125 | $taxonomies = get_post_taxonomies( $object['id'] ); |
| 126 | if ( 'post' === get_post_type() ) { |
| 127 | return get_the_category_list( esc_html( ' ' ), '', $object['id'] ); |
| 128 | } else { |
| 129 | if ( ! empty( $taxonomies ) ) { |
| 130 | return get_the_term_list( $object['id'], $taxonomies[0], ' ' ); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | // Feature image. |
| 137 | function rttpg_get_featured_image_url( $object ) { |
| 138 | |
| 139 | $featured_images = []; |
| 140 | if ( ! isset( $object['featured_media'] ) ) { |
| 141 | return $featured_images; |
| 142 | } |
| 143 | |
| 144 | $image = wp_get_attachment_image_src( $object['featured_media'], 'full', false ); |
| 145 | if ( is_array( $image ) ) { |
| 146 | $featured_images['full'] = $image; |
| 147 | $featured_images['landscape'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_landscape', false ); |
| 148 | $featured_images['portraits'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_portrait', false ); |
| 149 | $featured_images['thumbnail'] = wp_get_attachment_image_src( $object['featured_media'], 'rttpg_thumbnail', false ); |
| 150 | |
| 151 | $image_sizes = Fns::get_image_sizes(); |
| 152 | foreach ( $image_sizes as $key => $value ) { |
| 153 | $size = $key; |
| 154 | $featured_images[ $size ] = wp_get_attachment_image_src( |
| 155 | $object['featured_media'], |
| 156 | $size, |
| 157 | false |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | return $featured_images; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | // Excerpt. |
| 167 | function rttpg_get_excerpt( $object ) { |
| 168 | $excerpt = wp_trim_words( get_the_excerpt( $object['id'] ) ); |
| 169 | if ( ! $excerpt ) { |
| 170 | $excerpt = null; |
| 171 | } |
| 172 | |
| 173 | return $excerpt; |
| 174 | } |
| 175 | |
| 176 | } |