PluginProbe
UpdraftCentral Dashboard / trunk
UpdraftCentral Dashboard vtrunk
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / classes / class-rest-terms-controller.php

class-rest-terms-controller.php in UpdraftCentral Dashboard trunk, at classes/class-rest-terms-controller.php

171 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('Access denied.');
3
4 if (!class_exists('WP_REST_Terms_Controller')) {
5 include_once ABSPATH . 'wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php';
6 }
7
8 /**
9 * UpdraftCentral_REST_Terms_Controller class. Used to override some basic response
10 * details to integrate the remote term information.
11 *
12 * Originally copied and edited from WP_REST_Terms_Controller (WordPress 5.0.3). Updated
13 * to support higher versions up to WordPress 5.4.
14 *
15 * @see WP_REST_Terms_Controller
16 */
17 class UpdraftCentral_REST_Terms_Controller extends WP_REST_Terms_Controller {
18
19 public function __construct($taxonomy) {
20 parent::__construct($taxonomy);
21 }
22
23 /**
24 * Prepares a single term output for response.
25 *
26 * @since 4.7.0
27 *
28 * @param obj $item Term object.
29 * @param WP_REST_Request $request Request object.
30 * @param array $misc Array containing remote term data.
31 * @return WP_REST_Response $response Response object.
32 */
33 public function prepare_remote_item_for_response($item, $request, $misc) {
34
35 $fields = $this->get_fields_for_response($request);
36 $params = $request->get_params();
37 $data = array();
38
39 // If we have an array then make sure that it is converted to object
40 $item = $this->maybe_convert_to_object($item);
41
42 if (in_array('id', $fields, true)) {
43 $data['id'] = (int) $item->term_id;
44 }
45
46 if (in_array('count', $fields, true)) {
47 $data['count'] = (int) $item->count;
48 }
49
50 if (in_array('description', $fields, true)) {
51 $data['description'] = $item->description;
52 }
53
54 if (in_array('link', $fields, true)) {
55 $data['link'] = $misc['link'];
56 }
57
58 if (in_array('name', $fields, true)) {
59 $data['name'] = $item->name;
60 }
61
62 if (in_array('slug', $fields, true)) {
63 $data['slug'] = $item->slug;
64 }
65
66 if (in_array('taxonomy', $fields, true)) {
67 $data['taxonomy'] = $item->taxonomy;
68 }
69
70 if (in_array('parent', $fields, true)) {
71 $data['parent'] = (int) $item->parent;
72 }
73
74 if (in_array('meta', $fields, true)) {
75 $data['meta'] = $this->meta->get_value($item->term_id, $request);
76 }
77
78 $context = !empty($request['context']) ? $request['context'] : 'view';
79 $data = $this->add_additional_fields_to_object($data, $request);
80 $data = $this->filter_response_by_context($data, $context);
81
82 $response = rest_ensure_response($data);
83 if (empty($params['_fields']) || (is_array($params['_fields']) && in_array('_links', $params['_fields']))) {
84 $response->add_links($this->prepare_remote_links($item, $misc));
85 }
86
87 return $response;
88 }
89
90 /**
91 * Prepares remote links for the request.
92 *
93 * @param object $term Term object.
94 * @param array $misc Miscellaneous data for the Term object
95 * @return array Links for the given term.
96 */
97 private function prepare_remote_links($term, $misc) {
98 $base = $this->namespace . '/' . $this->rest_base;
99 $links = array(
100 'self' => array(
101 'href' => rest_url(trailingslashit($base) . $term->term_id),
102 ),
103 'collection' => array(
104 'href' => rest_url($base),
105 ),
106 'about' => array(
107 'href' => rest_url(sprintf('wp/v2/taxonomies/%s', $this->taxonomy)),
108 ),
109 );
110
111 if ($term->parent) {
112 $parent_term = $misc['parent_term'];
113 if (!empty($parent_term)) {
114 $parent_term = $this->maybe_convert_to_object(json_decode($parent_term));
115 $links['up'] = array(
116 'href' => rest_url(trailingslashit($base) . $parent_term->term_id),
117 'embeddable' => true,
118 );
119 }
120 }
121
122 $taxonomy_obj = $this->maybe_convert_to_object($misc['taxonomy_obj']);
123 if (empty($taxonomy_obj->object_type)) {
124 return $links;
125 }
126
127 $post_type_links = array();
128 foreach ($taxonomy_obj->object_type as $type) {
129 if (!in_array(strtolower($type), array('post', 'page'))) continue;
130
131 $post_type_object = get_post_type_object($type);
132 if (empty($post_type_object) || empty($post_type_object->show_in_rest)) {
133 continue;
134 }
135
136 $rest_base = !empty($post_type_object->rest_base) ? $post_type_object->rest_base : $post_type_object->name;
137 $post_type_links[] = array(
138 'href' => add_query_arg($this->rest_base, $term->term_id, rest_url(sprintf('wp/v2/%s', $rest_base))),
139 );
140 }
141
142 if (!empty($post_type_links)) {
143 $links['https://api.w.org/post_type'] = $post_type_links;
144 }
145
146 return $links;
147 }
148
149 /**
150 * Convert an array of data into its object representation
151 *
152 * @param array $item Item to check and convert into object
153 * @return object
154 */
155 private function maybe_convert_to_object($item) {
156 if (!empty($item) && is_array($item)) {
157 $term = new stdClass();
158 foreach ($item as $key => $value) {
159 if (!empty($value) && is_array($value)) {
160 $this->maybe_convert_to_object($value);
161 } else {
162 $term->{$key} = $value;
163 }
164 }
165 $item = $term;
166 }
167
168 return $item;
169 }
170 }
171