Completed
Push — 2.x ( e88784...f57756 )
by Scott Kingsley
25:22 queued 17:24
created

PodsRESTFields::field_allowed_to_extend()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 5.1612
cc 12
eloc 16
nc 8
nop 3

How to fix   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 PodsRESTFields
4
 *
5
 * Creates an object that adds read/write handlers for Pods fields in default responses.
6
 *
7
 * @package Pods
8
 * @since   2.5.6
9
 */
10
class PodsRESTFields {
11
12
	/**
13
	 * Pods object
14
	 *
15
	 * @since 2.5.6
16
	 *
17
	 * @access protected
18
	 *
19
	 * @var Pods
20
	 */
21
	protected $pod;
22
23
	/**
24
	 * Constructor for class
25
	 *
26
	 * @since 2.5.6
27
	 *
28
	 * @param string|object|Pods $pod Pods object
29
	 */
30
	public function __construct( $pod ) {
31
32
		if ( ! function_exists( 'register_api_field' ) ) {
33
			return;
34
		}
35
36
		$this->set_pod( $pod );
37
38
		if ( $this->pod ) {
39
			$this->add_fields();
40
		}
41
42
	}
43
44
	/**
45
	 * Set the Pods object
46
	 *
47
	 * @since  2.5.6
48
	 *
49
	 * @access protected
50
	 *
51
	 * @param string|Pods $pod Pods object or name of Pods object
52
	 */
53
	private function set_pod( $pod ) {
54
55
		if ( is_string( $pod ) ) {
56
			$this->set_pod( pods( $pod, null, true ) );
57
58
		} else {
59
			$type = $pod->pod_data['type'];
60
61
			if ( in_array( $type, array( 'post_type', 'user', 'taxonomy' ) ) ) {
62
				$this->pod = $pod;
63
			} else {
64
				$this->pod = false;
65
			}
66
		}
67
68
	}
69
70
	/**
71
	 * Add fields, based on options to REST read/write requests
72
	 *
73
	 * @since  2.5.6
74
	 *
75
	 * @access protected
76
	 */
77
	protected function add_fields() {
78
79
		$fields = $this->pod->fields();
80
81
		foreach ( $fields as $field_name => $field ) {
82
			$read  = self::field_allowed_to_extend( $field_name, $this->pod, 'read' );
83
			$write = self::field_allowed_to_extend( $field_name, $this->pod, 'write' );
84
85
			$this->register( $field_name, $read, $write );
86
		}
87
88
	}
89
90
	/**
91
	 * Register fields and their callbacks for read/write via REST
92
	 *
93
	 * @since  2.5.6
94
	 *
95
	 * @access protected
96
	 *
97
	 * @param string            $field_name Name of fields.
98
	 * @param bool|string|array $read       Allowing reading?
99
	 * @param bool|string|array $write      Allow writing?
100
	 */
101
	protected function register( $field_name, $read, $write ) {
102
103
		$args = array();
104
105 View Code Duplication
		switch ( $read ) {
106
			case true === $read :
107
				$args['get_callback'] = array( 'PodsRESTHandlers', 'get_handler' );
108
				break;
109
			case is_callable( $read ) :
110
				$args['get_callback'] = $read;
111
				$read                 = true;
112
				break;
113
		}
114
115 View Code Duplication
		switch ( $write ) {
116
			case true === $write :
117
				$args['update_callback'] = array( 'PodsRESTHandlers', 'write_handler' );
118
				break;
119
			case is_callable( $write ) :
120
				$args['update_callback'] = $write;
121
				$write                   = true;
122
				break;
123
		}
124
125
		if ( $read || $write ) {
126
			register_api_field( $this->pod->pod, $field_name, $args );
127
		}
128
129
	}
130
131
	/**
132
	 * Check if a field supports read or write via the REST API.
133
	 *
134
	 * @since 2.5.6
135
	 *
136
	 * @param string      $field_name The field name.
137
	 * @param object|Pods $pod        Pods object.
138
	 * @param string      $mode       Are we checking read or write?
139
	 *
140
	 * @return bool If supports, true, else false.
141
	 */
142
	public static function field_allowed_to_extend( $field_name, $pod, $mode = 'read' ) {
143
144
		$allowed = false;
145
146
		if ( is_object( $pod ) ) {
147
			$fields = $pod->fields();
148
149
			if ( array_key_exists( $field_name, $fields ) ) {
150
				$pod_options = $pod->pod_data['options'];
151
152
				if ( 'read' === $mode && pods_v( 'read_all', $pod_options, false ) ) {
153
					$allowed = true;
154
				} elseif ( 'write' === $mode && pods_v( 'write_all', $pod_options, false ) ) {
155
					$allowed = true;
156
				} elseif ( isset( $fields[ $field_name ] ) ) {
157
					if ( 'read' === $mode && 1 == (int) $pod->fields( $field_name, 'rest_read' ) ) {
158
						$allowed = true;
159
					} elseif ( 'write' === $mode && 1 == (int) $pod->fields( $field_name, 'rest_write' ) ) {
160
						$allowed = true;
161
					}
162
				}
163
			}
164
		}
165
166
		return $allowed;
167
168
	}
169
170
}