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