1
1
<?php
2
2
/**
3
- * Primary_Term_Rest class file
3
+ * Create WordPress Plugin Features: Primary_Term_Rest class
4
4
*
5
5
* @package create-wordpress-plugin
6
6
*/
7
7
8
8
namespace Create_WordPress_Plugin \Features ;
9
- use function Create_WordPress_Plugin \register_meta_helper ;
10
9
11
10
use Alley \WP \Types \Feature ;
11
+ use WP_Term ;
12
12
13
+ /**
14
+ * Feature: Adds a REST API field for the primary term.
15
+ *
16
+ * @package create-wordpress-plugin
17
+ */
13
18
final class Primary_Term_Rest implements Feature {
14
19
/**
15
20
* Set up.
16
21
*
17
- * @param array $taxonomies The taxonomies to support primary term for.
22
+ * @param string[] $taxonomies The taxonomies to support primary term for.
18
23
*/
19
24
public function __construct (
20
25
private readonly array $ taxonomies = [ 'category ' ],
@@ -28,18 +33,17 @@ public function boot(): void {
28
33
}
29
34
30
35
/**
31
- * Adds support for subheadline to the `post` post type.
32
- * Registers the meta field only for post types that support subheadline.
36
+ * Adds the create_wordpress_plugin_primary_term field to the REST API to return the primary term for a post.
33
37
*/
34
- public function register_rest_field () {
38
+ public function register_rest_field (): void {
35
39
register_rest_field (
36
40
'post ' ,
37
41
'create_wordpress_plugin_primary_term ' ,
38
42
[
39
43
'get_callback ' => [ $ this , 'rest_callback ' ],
40
44
'update_callback ' => null ,
41
45
'schema ' => [
42
- 'description ' => __ ( 'The primary term for the post. ' ),
46
+ 'description ' => __ ( 'The primary term for the post. ' , ' create-wordpress-plugin ' ),
43
47
'type ' => [
44
48
'term_name ' => 'string ' ,
45
49
'term_id ' => 'integer ' ,
@@ -51,13 +55,18 @@ public function register_rest_field() {
51
55
}
52
56
53
57
/**
54
- * Callback functionf for the rest field.
58
+ * Callback function for the REST field.
59
+ *
60
+ * @param array $request The REST request.
61
+ *
62
+ * @phpstan-param array{id: int} $request
55
63
*
56
- * @param \WP_REST_Reqest $request The rest request.
57
- * @return array
64
+ * @return array An array containing taxonomy slugs as keys with term objects as values.
65
+ *
66
+ * @phpstan-return array<string, array{term_name?: string, term_id?: int, term_link?: string}>
58
67
*/
59
- public function rest_callback ( $ request ) {
60
- $ output = [];
68
+ public function rest_callback ( array $ request ): array {
69
+ $ output = [];
61
70
$ post_id = $ request ['id ' ];
62
71
foreach ( $ this ->taxonomies as $ taxonomy ) {
63
72
$ output [ $ taxonomy ] = $ this ->get_primary_term ( $ post_id , $ taxonomy );
@@ -66,39 +75,46 @@ public function rest_callback( $request ) {
66
75
}
67
76
68
77
/**
69
- * Gets the primary term for the post.
78
+ * Gets the primary term for the post for a given taxonomy .
70
79
*
71
- * @param int $post_id The post id.
80
+ * @param int $post_id The post id.
72
81
* @param string $taxonomy The taxonomy to get the primary term for.
73
- * @return array
82
+ *
83
+ * @return array An array containing the term name, ID, and link.
84
+ *
85
+ * @phpstan-return array{term_name?: string, term_id?: int, term_link?: string}
74
86
*/
75
- public function get_primary_term ( $ post_id , $ taxonomy ) {
87
+ public function get_primary_term ( int $ post_id , string $ taxonomy ): array {
88
+ $ term = [];
76
89
if ( function_exists ( 'yoast_get_primary_term_id ' ) ) {
77
90
$ primary_term_id = yoast_get_primary_term_id ( $ taxonomy , $ post_id );
78
91
}
79
92
if ( empty ( $ primary_term_id ) ) {
80
93
$ terms = get_the_terms ( $ post_id , $ taxonomy );
81
- if ( ! empty ( $ terms ) ) {
94
+ if ( ! empty ( $ terms[ 0 ]-> term_id ) ) {
82
95
$ primary_term_id = $ terms [0 ]->term_id ;
83
96
}
84
97
}
85
- if ( empty ( $ primary_term_id ) ) {
86
- $ term = [];
87
- } else {
98
+ if ( ! empty ( $ primary_term_id ) ) {
88
99
$ primary_term = get_term ( $ primary_term_id , $ taxonomy );
89
- $ term = [
90
- 'term_name ' => $ primary_term ->name ,
91
- 'term_id ' => $ primary_term ->term_id ,
92
- 'term_link ' => get_term_link ( $ primary_term ),
93
- ];
100
+ if ( $ primary_term instanceof WP_Term ) {
101
+ $ term_link = get_term_link ( $ primary_term );
102
+ $ term = [
103
+ 'term_name ' => $ primary_term ->name ,
104
+ 'term_id ' => $ primary_term ->term_id ,
105
+ 'term_link ' => ! is_wp_error ( $ term_link ) ? $ term_link : '' ,
106
+ ];
107
+ }
94
108
}
95
109
/**
96
110
* Filters the primary term for the post.
97
111
*
98
112
* @param array $term The primary term for the post.
99
113
* @param int $id The post ID.
100
114
* @param string $taxonomy The taxonomy.
115
+ *
116
+ * @phpstan-param array{term_name?: string, term_id?: int, term_link?: string} $term
101
117
*/
102
- return \ apply_filters ( 'create_wordpress_plugin_primary_term ' , $ term , $ post_id , $ taxonomy );
118
+ return apply_filters ( 'create_wordpress_plugin_primary_term ' , $ term , $ post_id , $ taxonomy );
103
119
}
104
120
}
0 commit comments