Skip to content

Commit e091b17

Browse files
authored
Merge pull request #19 from joshuafredrickson/master
Add support for Yoast SEO fields, update how slugs are populated
2 parents e6fec3b + 1086375 commit e091b17

13 files changed

+251
-11
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Gets a collection of posts. Accepts the following parameters:
1818
- exclude (int) a post ID to exclude from the response
1919
- author (string) limit posts by author nice name (user_nicename)
2020
- acf (boolean - setting to false omits `acf` from being returned)
21+
- yoast (boolean - setting to false omits `yoast` from being returned)
2122
- media (boolean - setting to false omits `media` (featured media) from being returned)
2223

2324
It returns a JSON response with the following:
@@ -34,6 +35,7 @@ It returns a JSON response with the following:
3435
- Tags
3536
- Tag IDs
3637
- ACF fields, if applicable
38+
- Yoast SEO fields, if applicable
3739

3840
### Post
3941
**`better-rest-endpoints/v1/post/{id}`**
@@ -58,6 +60,7 @@ Returns a JSON response with the following:
5860
- Tags
5961
- Tag IDs
6062
- ACF fields, if applicable
63+
- Yoast SEO fields, if applicable
6164

6265
### Post by slug
6366
**`better-rest-endpoints/v1/post/{slug}`**
@@ -82,6 +85,7 @@ Returns a JSON response with the following:
8285
- Tags
8386
- Tag IDs
8487
- ACF fields, if applicable
88+
- Yoast SEO fields, if applicable
8589

8690
### Pages
8791
**`better-rest-endpoints/v1/pages`**
@@ -95,6 +99,7 @@ Gets a collection of pages. Accepts the following parameters:
9599
- content (boolean - setting to false hides the content from the response)
96100
- exclude (int) a post ID to exclude from the response
97101
- acf (boolean - setting to false omits `acf` from being returned)
102+
- yoast (boolean - setting to false omits `yoast` from being returned)
98103
- media (boolean - setting to false omits `media` (featured media) from being returned)
99104

100105
Returns the following JSON Response:
@@ -105,6 +110,7 @@ Returns the following JSON Response:
105110
- Title
106111
- Content
107112
- ACF Fields
113+
- Yoast SEO Fields
108114
- all possible thumbnail sizes & URLs
109115

110116
### Page by ID
@@ -124,6 +130,7 @@ Returns a JSON response with the following:
124130
- content
125131
- all possible thumbnail sizes & URLs
126132
- ACF fields, if applicable
133+
- Yoast SEO fields, if applicable
127134

128135
### Custom Post Type Collection
129136
**`better-rest-endpoints/v1/{custom_post_type}`**
@@ -135,6 +142,7 @@ Gets a collection of posts from a custom post type. Accepts the following parame
135142
- orderby (string) - see the [codex](https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters) for options, currently does not support multiple values
136143
- exclude (int) a post ID to exclude from the response
137144
- acf (boolean - setting to false omits `acf` from being returned)
145+
- yoast (boolean - setting to false omits `yoast` from being returned)
138146
- media (boolean - setting to false omits `media` (featured media) from being returned)
139147

140148
Returns the following JSON response:
@@ -149,6 +157,7 @@ Returns the following JSON response:
149157
- all possible thumbnail sizes & URLs
150158
- Author, user_nicename, & Author ID
151159
- ACF fields if applicable
160+
- Yoast SEO fields if applicable
152161

153162
### Custom Post Type Post by ID
154163
**`better-rest-endpoints/v1/{custom_post_type}/{id}`**
@@ -168,6 +177,7 @@ Returns the following JSON Response:
168177
- all possible thumbnail sizes & URLs
169178
- Author, user_nicename, & Author ID
170179
- ACF Fields, if applicable
180+
- Yoast SEO Fields, if applicable
171181

172182
### Custom Post Type Post by Slug
173183
**`better-rest-endpoints/v1/{custom_post_type}/{slug}`**
@@ -187,6 +197,7 @@ Returns the following JSON Response:
187197
- all possible thumbnail sizes & URLs
188198
- Author, user_nicename, & Author ID
189199
- ACF Fields, if applicable
200+
- Yoast SEO Fields, if applicable
190201

191202
### Get Posts Belonging To A Taxonomy Term
192203
**`better-rest-endpoints/v1/{taxonomy}/{term}`**
@@ -198,6 +209,7 @@ Gets posts from a taxonomy term. Accepts the following parameters:
198209
- orderby (string) - see the [codex](https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters) for options, currently does not support multiple values
199210
- exclude (int) a post ID to exclude from the response
200211
- acf (boolean - setting to false omits `acf` from being returned)
212+
- yoast (boolean - setting to false omits `yoast` from being returned)
201213
- media (boolean - setting to false omits `media` (featured media) from being returned)
202214

203215
Returns the following JSON Response:
@@ -212,6 +224,7 @@ Returns the following JSON Response:
212224
- all possible thumbnail sizes & URLs
213225
- Author, user_nicename, & Author ID
214226
- ACF Fields, if applicable
227+
- Yoast SEO Fields, if applicable
215228

216229
### Menus
217230
**`better-rest-endpoints/v1/menus/{menu-slug}`**
@@ -251,6 +264,7 @@ Gets a collection of posts and pages based on the search parameter. Accepts the
251264
- content (boolean) set to false to omit content from showing in JSON response
252265
- search (string | required)
253266
- acf (boolean - setting to false omits `acf` from being returned)
267+
- yoast (boolean - setting to false omits `yoast` from being returned)
254268
- media (boolean - setting to false omits `media` (featured media) from being returned)
255269

256270
It returns a JSON response with the following (returns an empty array if no posts found):
@@ -267,6 +281,7 @@ It returns a JSON response with the following (returns an empty array if no post
267281
- Tags
268282
- Tag IDs
269283
- ACF fields, if applicable
284+
- Yoast SEO fields, if applicable
270285

271286
## Hooks and Filters
272287

@@ -282,4 +297,4 @@ add_filter('better_rest_endpoints_cpt_collection', function($cpt_collection){
282297
$cpt_collection = array_values( array_flip($cpt_collection) );
283298
return $cpt_collection;
284299
});
285-
```
300+
```

better-rest-endpoints.php

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private function includes() {
7474
// include acf function
7575
include_once self::$plugin_dir . 'includes/get_acf.php';
7676

77+
// include yoast seo function
78+
include_once self::$plugin_dir . 'includes/get_yoast.php';
79+
7780
// get a post by id
7881
include_once self::$plugin_dir . 'includes/get_post_by_id.php';
7982

includes/create_cpt_endpoints.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function bre_build_cpt_endpoints() {
3030
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
3131
$acf = $request['acf'];
3232
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
33+
$yoast = $request['yoast'];
34+
$show_yoast = filter_var($yoast, FILTER_VALIDATE_BOOLEAN);
3335
$media = $request['media'];
3436
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
3537
$orderby = $request['orderby']?: null;
@@ -63,14 +65,16 @@ function bre_build_cpt_endpoints() {
6365
while ( $query->have_posts() ) {
6466
$query->the_post();
6567

68+
global $post;
69+
6670
// better wordpress endpoint post object
6771
$bre_post = new stdClass();
6872

6973
// get post data
7074
$permalink = get_permalink();
7175
$bre_post->id = get_the_ID();
7276
$bre_post->title = get_the_title();
73-
$bre_post->slug = basename($permalink);
77+
$bre_post->slug = $post->post_name;
7478
$bre_post->permalink = $permalink;
7579
$bre_post->date = get_the_date('c');
7680
$bre_post->excerpt = get_the_excerpt();
@@ -107,6 +111,15 @@ function bre_build_cpt_endpoints() {
107111
$bre_post->acf = bre_get_acf();
108112
}
109113

114+
/*
115+
*
116+
* return Yoast SEO fields if they exist and depending on query string
117+
*
118+
*/
119+
if( $yoast === null || $show_yoast === true ) {
120+
$bre_post->yoast = bre_get_yoast( $bre_post->id );
121+
}
122+
110123
/*
111124
*
112125
* get possible thumbnail sizes and urls if query set to true or by default
@@ -192,6 +205,20 @@ function bre_build_cpt_endpoints() {
192205
return is_bool( $param );
193206
}
194207
),
208+
'yoast' => array(
209+
'description' => 'Hide or show Yoast SEO fields from the collection.',
210+
'type' => 'boolean',
211+
'validate_callback' => function( $param, $request, $key ) {
212+
213+
if ( $param == 'true' || $param == 'TRUE' ) {
214+
$param = true;
215+
} else if( $param == 'false' || $param == 'FALSE') {
216+
$param = false;
217+
}
218+
219+
return is_bool( $param );
220+
}
221+
),
195222
'order' => array(
196223
'description' => 'Change order of the collection.',
197224
'type' => 'string',

includes/get_cpt_by_id.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ function bre_build_single_cpt_endpoints() {
4444
while( $query->have_posts() ) {
4545
$query->the_post();
4646

47+
global $post;
4748

4849
// get post data
4950
$permalink = get_permalink();
5051
$bre_cpt_post->id = get_the_ID();
5152
$bre_cpt_post->title = get_the_title();
52-
$bre_cpt_post->slug = basename($permalink);
53+
$bre_cpt_post->slug = $post->post_name;
5354
$bre_cpt_post->permalink = $permalink;
5455
$bre_cpt_post->date = get_the_date('c');
5556
$bre_cpt_post->excerpt = get_the_excerpt();
@@ -80,6 +81,13 @@ function bre_build_single_cpt_endpoints() {
8081
*/
8182
$bre_cpt_post->acf = bre_get_acf();
8283

84+
/*
85+
*
86+
* return Yoast SEO fields if they exist
87+
*
88+
*/
89+
$bre_cpt_post->yoast = bre_get_yoast( $bre_cpt_post->id );
90+
8391
/*
8492
*
8593
* get possible thumbnail sizes and urls

includes/get_cpt_by_slug.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ function bre_build_single_cpt_endpoints_slug() {
4444
while( $query->have_posts() ) {
4545
$query->the_post();
4646

47+
global $post;
4748

4849
// get post data
4950
$permalink = get_permalink();
5051
$bre_cpt_post->id = get_the_ID();
5152
$bre_cpt_post->title = get_the_title();
52-
$bre_cpt_post->slug = basename($permalink);
53+
$bre_cpt_post->slug = $post->post_name;
5354
$bre_cpt_post->permalink = $permalink;
5455
$bre_cpt_post->date = get_the_date('c');
5556
$bre_cpt_post->excerpt = get_the_excerpt();
@@ -80,6 +81,13 @@ function bre_build_single_cpt_endpoints_slug() {
8081
*/
8182
$bre_cpt_post->acf = bre_get_acf();
8283

84+
/*
85+
*
86+
* return Yoast SEO fields if they exist
87+
*
88+
*/
89+
$bre_cpt_post->yoast = bre_get_yoast( $bre_cpt_post->id );
90+
8391
/*
8492
*
8593
* get possible thumbnail sizes and urls

includes/get_page_by_id.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function get_page_by_id( WP_REST_Request $request ){
2929
$permalink = get_permalink();
3030
$bre_page->id = get_the_ID();
3131
$bre_page->title = get_the_title();
32-
$bre_page->slug = basename($permalink);
32+
$bre_page->slug = $post->post_name;
3333
$bre_page->permalink = $permalink;
3434

3535
/*
@@ -68,6 +68,13 @@ function get_page_by_id( WP_REST_Request $request ){
6868
*/
6969
$bre_page->acf = bre_get_acf();
7070

71+
/*
72+
*
73+
* return Yoast SEO fields if they exist
74+
*
75+
*/
76+
$bre_page->yoast = bre_get_yoast( $bre_page->id );
77+
7178
/*
7279
*
7380
* get possible thumbnail sizes and urls

includes/get_pages.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function bre_get_pages( WP_REST_Request $request ) {
1717
$show_content = filter_var($content, FILTER_VALIDATE_BOOLEAN);
1818
$acf = $request['acf'];
1919
$show_acf = filter_var($acf, FILTER_VALIDATE_BOOLEAN);
20+
$yoast = $request['yoast'];
21+
$show_yoast = filter_var($yoast, FILTER_VALIDATE_BOOLEAN);
2022
$media = $request['media'];
2123
$show_media = filter_var($media, FILTER_VALIDATE_BOOLEAN);
2224
$orderby = $request['orderby']?: null;
@@ -62,7 +64,7 @@ function bre_get_pages( WP_REST_Request $request ) {
6264
$permalink = get_permalink();
6365
$bre_page->id = get_the_ID();
6466
$bre_page->title = get_the_title();
65-
$bre_page->slug = basename($permalink);
67+
$bre_page->slug = $post->post_name;
6668
$bre_page->permalink = $permalink;
6769

6870
/*
@@ -107,6 +109,15 @@ function bre_get_pages( WP_REST_Request $request ) {
107109
$bre_page->acf = bre_get_acf();
108110
}
109111

112+
/*
113+
*
114+
* return Yoast SEO fields if they exist
115+
*
116+
*/
117+
if( $yoast === null || $show_yoast === true ) {
118+
$bre_page->yoast = bre_get_yoast( $bre_page->id );
119+
}
120+
110121
/*
111122
*
112123
* get possible thumbnail sizes and urls if query set to true or by default
@@ -229,6 +240,20 @@ function bre_get_pages( WP_REST_Request $request ) {
229240
return is_bool( $param );
230241
}
231242
),
243+
'yoast' => array(
244+
'description' => 'Hide or show Yoast SEO fields from the collection.',
245+
'type' => 'boolean',
246+
'validate_callback' => function( $param, $request, $key ) {
247+
248+
if ( $param == 'true' || $param == 'TRUE' ) {
249+
$param = true;
250+
} else if( $param == 'false' || $param == 'FALSE') {
251+
$param = false;
252+
}
253+
254+
return is_bool( $param );
255+
}
256+
),
232257
'media' => array(
233258
'description' => 'Hide or show featured media from the collection.',
234259
'type' => 'boolean',

includes/get_post_by_id.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ function get_post_by_id( $data ) {
2121
while ( $query->have_posts() ) {
2222
$query->the_post();
2323

24+
global $post;
25+
2426
// better wordpress endpoint post object
2527
$bre_post = new stdClass();
2628

2729
$permalink = get_permalink();
2830
$bre_post->id = get_the_ID();
2931
$bre_post->title = get_the_title();
30-
$bre_post->slug = basename($permalink);
32+
$bre_post->slug = $post->post_name;
3133
$bre_post->permalink = $permalink;
3234
$bre_post->date = get_the_date('c');
3335
$bre_post->excerpt = get_the_excerpt();
@@ -83,6 +85,13 @@ function get_post_by_id( $data ) {
8385
*/
8486
$bre_post->acf = bre_get_acf();
8587

88+
/*
89+
*
90+
* return Yoast SEO fields if they exist
91+
*
92+
*/
93+
$bre_post->yoast = bre_get_yoast( $bre_post->id );
94+
8695
/*
8796
*
8897
* get possible thumbnail sizes and urls

includes/get_post_by_slug.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ function get_post_by_slug( WP_REST_Request $request ) {
2323
while ( $query->have_posts() ) {
2424
$query->the_post();
2525

26+
global $post;
27+
2628
// better wordpress endpoint post object
2729
$bre_post = new stdClass();
2830

2931
$permalink = get_permalink();
3032
$bre_post->id = get_the_ID();
3133
$bre_post->title = get_the_title();
32-
$bre_post->slug = basename($permalink);
34+
$bre_post->slug = $post->post_name;
3335
$bre_post->permalink = $permalink;
3436
$bre_post->date = get_the_date('c');
3537
$bre_post->excerpt = get_the_excerpt();
@@ -85,6 +87,13 @@ function get_post_by_slug( WP_REST_Request $request ) {
8587
*/
8688
$bre_post->acf = bre_get_acf();
8789

90+
/*
91+
*
92+
* return Yoast SEO fields if they exist
93+
*
94+
*/
95+
$bre_post->yoast = bre_get_yoast( $bre_post->id );
96+
8897
/*
8998
*
9099
* get possible thumbnail sizes and urls

0 commit comments

Comments
 (0)