Completed
Pull Request — 2.x (#3560)
by Scott Kingsley
06:14
created

PodsRESTHandlers::get_handler()   D

Complexity

Conditions 16
Paths 30

Size

Total Lines 111
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 16
eloc 41
c 4
b 0
f 0
nc 30
nop 3
dl 0
loc 111
rs 4.8736

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Class PodsRESTHandlers
4
 *
5
 * Handlers for reading and writing Pods fields via REST API
6
 *
7
 * @package Pods
8
 * @since   2.5.6
9
 */
10
class PodsRESTHandlers {
11
12
	/**
13
	 * Holds a Pods object to avoid extra DB queries
14
	 *
15
	 * @since 2.5.6
16
	 *
17
	 * @var Pods
18
	 */
19
	private static $pod;
20
21
	/**
22
	 * Get Pod object
23
	 *
24
	 * @since 2.5.6
25
	 *
26
	 * @param $pod_name
27
	 * @param $id
28
	 *
29
	 * @return bool|Pods
30
	 */
31
	protected static function get_pod( $pod_name, $id ) {
32
33
		if ( ! self::$pod || self::$pod->pod != $pod_name ) {
34
			self::$pod = pods( $pod_name, $id, true );
35
		}
36
37
		if ( self::$pod && self::$pod->id != $id ) {
38
			self::$pod->fetch( $id );
39
		}
40
41
		return self::$pod;
42
43
	}
44
45
	/**
46
	 * Handler for getting custom field data.
47
	 *
48
	 * @since 2.5.6
49
	 *
50
	 * @param array           $object     The object from the response
51
	 * @param string          $field_name Name of field
52
	 * @param WP_REST_Request $request    Current request
53
	 *
54
	 * @return mixed
55
	 */
56
	public static function get_handler( $object, $field_name, $request ) {
57
58
		$pod_name = pods_v( 'type', $object );
59
60
		/**
61
		 * If $pod_name in the line above is empty then the route invoked
62
		 * may be for a taxonomy, so lets try and check for that
63
		 *
64
		 */
65
		if ( empty( $pod_name ) ) {
66
			$pod_name = pods_v( 'taxonomy', $object );
67
		}
68
69
		$id  = pods_v( 'id', $object );
70
		$pod = self::get_pod( $pod_name, $id );
71
72
		$value = false;
73
74
		if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'read' ) ) {
75
			$params = null;
76
77
			$field_data = $pod->fields( $field_name );
78
79
			if ( 'pick' == pods_v( 'type', $field_data ) ) {
80
				$output_type = pods_v( 'rest_pick_response', $field_data['options'], 'array' );
81
82
				if ( 'array' == $output_type ) {
83
					$related_pod_items = $pod->field( $field_name, array( 'output' => 'pod' ) );
84
85
					if ( $related_pod_items ) {
86
						$fields = false;
87
						$items  = array();
88
						$depth  = pods_v( 'rest_pick_depth', $field_data['options'], 2 );
89
90
						if ( ! is_array( $related_pod_items ) ) {
91
							$related_pod_items = array( $related_pod_items );
92
						}
93
94
						/**
95
						 * @var $related_pod Pods
96
						 */
97
						foreach ( $related_pod_items as $related_pod ) {
98
							if ( ! is_object( $related_pod ) || ! is_a( $related_pod, 'Pods' ) ) {
99
								$items = $related_pod_items;
100
101
								break;
102
							}
103
104
							if ( false === $fields ) {
105
								$fields = $related_pod->fields();
106
								$fields = array_keys( $fields );
107
108
								if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) {
109
									$fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) );
110
								}
111
112
								/**
113
								 * What fields to show in a related field REST response.
114
								 *
115
								 * @since 0.0.1
116
								 *
117
								 * @param array                  $fields     The fields to show
118
								 * @param string                 $field_name The name of the field
119
								 * @param object|Pods            $pod        The Pods object for Pod relationship is from.
120
								 * @param object|Pods            $pod        The Pods object for Pod relationship is to.
121
								 * @param int                    $id         Current item ID
122
								 * @param object|WP_REST_Request Current     request object.
123
								 */
124
								$fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request );
125
							}
126
127
							/**
128
							 * What depth to use for a related field REST response.
129
							 *
130
							 * @since 0.0.1
131
							 *
132
							 * @param array                  $depth      The depth.
133
							 * @param string                 $field_name The name of the field
134
							 * @param object|Pods            $pod        The Pods object for Pod relationship is from.
135
							 * @param object|Pods            $pod        The Pods object for Pod relationship is to.
136
							 * @param int                    $id         Current item ID
137
							 * @param object|WP_REST_Request Current     request object.
138
							 */
139
							$depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request );
140
141
							$params = array(
142
								'fields' => $fields,
143
								'depth'  => $depth,
144
							);
145
146
							$items[] = $related_pod->export( $params );
147
						}
148
149
						$value = $items;
150
					}
151
				}
152
153
				$params = array(
154
					'output' => $output_type,
155
				);
156
			}
157
158
			// If no value set yet, get normal field value
159
			if ( ! $value && ! is_array( $value ) ) {
160
				$value = $pod->field( $field_name, $params );
161
			}
162
		}
163
164
		return $value;
165
166
	}
167
168
	/**
169
	 * Handler for updating custom field data.
170
	 *
171
	 * @since 2.5.6
172
	 *
173
	 * @param mixed  $value      Value to write
174
	 * @param object $object     The object from the response
175
	 * @param string $field_name Name of field
176
	 *
177
	 * @return bool|int
178
	 */
179
	public static function write_handler( $value, $object, $field_name ) {
180
181
		$pod_name = pods_v( 'type', $object );
182
183
		$id = pods_v( 'id', $object );
184
185
		$pod = self::get_pod( $pod_name, $id );
186
187
		if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'write' ) ) {
188
			$pod->save( $field_name, $value, $id );
189
190
			return $pod->field( $field_name );
191
		}
192
193
		return false;
194
195
	}
196
197
	/**
198
	 * Add REST API support to a post type
199
	 *
200
	 * @since 2.5.6
201
	 *
202
	 * @param string     $post_type_name Name of post type
203
	 * @param bool|false $rest_base      Optional. Base url segment. If not set, post type name is used
204
	 * @param string     $controller     Optional, controller class for route. If not set "WP_REST_Posts_Controller" is
205
	 *                                   used.
206
	 */
207 View Code Duplication
	public static function post_type_rest_support( $post_type_name, $rest_base = false, $controller = 'WP_REST_Posts_Controller' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
209
		global $wp_post_types;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
210
211
		if ( isset( $wp_post_types[ $post_type_name ] ) ) {
212
			if ( ! $rest_base ) {
213
				$rest_base = $post_type_name;
214
			}
215
216
			$wp_post_types[ $post_type_name ]->show_in_rest          = true;
217
			$wp_post_types[ $post_type_name ]->rest_base             = $rest_base;
218
			$wp_post_types[ $post_type_name ]->rest_controller_class = $controller;
219
		}
220
221
	}
222
223
	/**
224
	 * Add REST API support to an already registered taxonomy.
225
	 *
226
	 * @since 2.5.6
227
	 *
228
	 * @param string     $taxonomy_name Taxonomy name.
229
	 * @param bool|false $rest_base     Optional. Base url segment. If not set, taxonomy name is used.
230
	 * @param string     $controller    Optional, controller class for route. If not set "WP_REST_Terms_Controller" is
231
	 *                                  used.
232
	 */
233 View Code Duplication
	public static function taxonomy_rest_support( $taxonomy_name, $rest_base = false, $controller = 'WP_REST_Terms_Controller' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
235
		global $wp_taxonomies;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
236
237
		if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
238
			if ( ! $rest_base ) {
239
				$rest_base = $taxonomy_name;
240
			}
241
242
			$wp_taxonomies[ $taxonomy_name ]->show_in_rest          = true;
243
			$wp_taxonomies[ $taxonomy_name ]->rest_base             = $rest_base;
244
			$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = $controller;
245
		}
246
247
	}
248
249
	/**
250
	 * Check if a Pod supports extending core REST response.
251
	 *
252
	 * @since 2.5.6
253
	 *
254
	 * @param array|Pods $pod Pod object or the pod_data array
255
	 *
256
	 * @return bool
257
	 */
258
	public static function pod_extends_core_route( $pod ) {
259
260
		$enabled = false;
261
262
		if ( is_object( $pod ) ) {
263
			$pod = $pod->pod_data;
264
		}
265
266
		if ( is_array( $pod ) ) {
267
			$enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
268
		}
269
270
		return $enabled;
271
272
	}
273
274
}