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

PodsRESTHandlers::get_handler()   D

Complexity

Conditions 14
Paths 22

Size

Total Lines 102
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 14
eloc 38
c 3
b 0
f 0
nc 22
nop 3
dl 0
loc 102
rs 4.9516

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
						foreach ( $related_pod_items as $related_pod ) {
95
							if ( false === $fields ) {
96
								$fields = $related_pod->fields();
97
								$fields = array_keys( $fields );
98
99
								if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) {
100
									$fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) );
101
								}
102
103
								/**
104
								 * What fields to show in a related field REST response.
105
								 *
106
								 * @since 0.0.1
107
								 *
108
								 * @param array                  $fields     The fields to show
109
								 * @param string                 $field_name The name of the field
110
								 * @param object|Pods            $pod        The Pods object for Pod relationship is from.
111
								 * @param object|Pods            $pod        The Pods object for Pod relationship is to.
112
								 * @param int                    $id         Current item ID
113
								 * @param object|WP_REST_Request Current     request object.
114
								 */
115
								$fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request );
116
							}
117
118
							/**
119
							 * What depth to use for a related field REST response.
120
							 *
121
							 * @since 0.0.1
122
							 *
123
							 * @param array                  $depth      The depth.
124
							 * @param string                 $field_name The name of the field
125
							 * @param object|Pods            $pod        The Pods object for Pod relationship is from.
126
							 * @param object|Pods            $pod        The Pods object for Pod relationship is to.
127
							 * @param int                    $id         Current item ID
128
							 * @param object|WP_REST_Request Current     request object.
129
							 */
130
							$depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request );
131
132
							$params = array(
133
								'fields' => $fields,
134
								'depth'  => $depth,
135
							);
136
137
							$items[] = $pod->api->export_pod_item( $related_pod, $params );
138
						}
139
140
						$value = $items;
141
					}
142
				}
143
144
				$params = array(
145
					'output' => $output_type,
146
				);
147
			}
148
149
			// If no value set yet, get normal field value
150
			if ( ! $value && ! is_array( $value ) ) {
151
				$value = $pod->field( $field_name, $params );
152
			}
153
		}
154
155
		return $value;
156
157
	}
158
159
	/**
160
	 * Handler for updating custom field data.
161
	 *
162
	 * @since 2.5.6
163
	 *
164
	 * @param mixed  $value      Value to write
165
	 * @param object $object     The object from the response
166
	 * @param string $field_name Name of field
167
	 *
168
	 * @return bool|int
169
	 */
170
	public static function write_handler( $value, $object, $field_name ) {
171
172
		$pod_name = pods_v( 'type', $object );
173
174
		$id = pods_v( 'id', $object );
175
176
		$pod = self::get_pod( $pod_name, $id );
177
178
		if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'write' ) ) {
179
			$pod->save( $field_name, $value, $id );
180
181
			return $pod->field( $field_name );
182
		}
183
184
		return false;
185
186
	}
187
188
	/**
189
	 * Add REST API support to a post type
190
	 *
191
	 * @since 2.5.6
192
	 *
193
	 * @param string     $post_type_name Name of post type
194
	 * @param bool|false $rest_base      Optional. Base url segment. If not set, post type name is used
195
	 * @param string     $controller     Optional, controller class for route. If not set "WP_REST_Posts_Controller" is
196
	 *                                   used.
197
	 */
198 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...
199
200
		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...
201
202
		if ( isset( $wp_post_types[ $post_type_name ] ) ) {
203
			if ( ! $rest_base ) {
204
				$rest_base = $post_type_name;
205
			}
206
207
			$wp_post_types[ $post_type_name ]->show_in_rest          = true;
208
			$wp_post_types[ $post_type_name ]->rest_base             = $rest_base;
209
			$wp_post_types[ $post_type_name ]->rest_controller_class = $controller;
210
		}
211
212
	}
213
214
	/**
215
	 * Add REST API support to an already registered taxonomy.
216
	 *
217
	 * @since 2.5.6
218
	 *
219
	 * @param string     $taxonomy_name Taxonomy name.
220
	 * @param bool|false $rest_base     Optional. Base url segment. If not set, taxonomy name is used.
221
	 * @param string     $controller    Optional, controller class for route. If not set "WP_REST_Terms_Controller" is
222
	 *                                  used.
223
	 */
224 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...
225
226
		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...
227
228
		if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
229
			if ( ! $rest_base ) {
230
				$rest_base = $taxonomy_name;
231
			}
232
233
			$wp_taxonomies[ $taxonomy_name ]->show_in_rest          = true;
234
			$wp_taxonomies[ $taxonomy_name ]->rest_base             = $rest_base;
235
			$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = $controller;
236
		}
237
238
	}
239
240
	/**
241
	 * Check if a Pod supports extending core REST response.
242
	 *
243
	 * @since 2.5.6
244
	 *
245
	 * @param array|Pods $pod Pod object or the pod_data array
246
	 *
247
	 * @return bool
248
	 */
249
	public static function pod_extends_core_route( $pod ) {
250
251
		$enabled = false;
252
253
		if ( is_object( $pod ) ) {
254
			$pod = $pod->pod_data;
255
		}
256
257
		if ( is_array( $pod ) ) {
258
			$enabled = (boolean) pods_v( 'rest_enable', $pod['options'], false );
259
		}
260
261
		return $enabled;
262
263
	}
264
265
}