PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.7.31.4
Pods – Custom Content Types and Fields v2.7.31.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / PodsRESTFields.php
pods / classes Last commit date
cli 3 days ago fields 3 days ago widgets 3 days ago Pods.php 3 days ago PodsAPI.php 3 days ago PodsAdmin.php 3 days ago PodsArray.php 3 days ago PodsComponent.php 3 days ago PodsComponents.php 3 days ago PodsData.php 3 days ago PodsField.php 3 days ago PodsForm.php 3 days ago PodsI18n.php 3 days ago PodsInit.php 3 days ago PodsMeta.php 3 days ago PodsMigrate.php 3 days ago PodsRESTFields.php 3 days ago PodsRESTHandlers.php 3 days ago PodsTermSplitting.php 3 days ago PodsUI.php 3 days ago PodsView.php 3 days ago
PodsRESTFields.php
195 lines
1 <?php
2
3 /**
4 * Class PodsRESTFields
5 *
6 * Creates an object that adds read/write handlers for Pods fields in default responses.
7 *
8 * @package Pods
9 * @since 2.5.6
10 */
11 class PodsRESTFields {
12
13 /**
14 * Pods object
15 *
16 * @since 2.5.6
17 *
18 * @access protected
19 *
20 * @var Pods
21 */
22 protected $pod;
23
24 /**
25 * Constructor for class
26 *
27 * @since 2.5.6
28 *
29 * @param string|object|Pods $pod Pods object
30 */
31 public function __construct( $pod ) {
32
33 if ( ! function_exists( 'register_rest_field' ) ) {
34 return;
35 }
36
37 $this->set_pod( $pod );
38
39 if ( $this->pod ) {
40 $this->add_fields();
41 }
42
43 }
44
45 /**
46 * Set the Pods object
47 *
48 * @since 2.5.6
49 *
50 * @access protected
51 *
52 * @param string|Pods $pod Pods object or name of Pods object
53 */
54 private function set_pod( $pod ) {
55
56 if ( is_string( $pod ) ) {
57 $pod = pods( $pod, null, true );
58
59 $this->set_pod( $pod );
60 } else {
61 $type = $pod->pod_data['type'];
62
63 $supported_pod_types = array(
64 'post_type',
65 'taxonomy',
66 'media',
67 'user',
68 'comment',
69 );
70
71 if ( in_array( $type, $supported_pod_types, true ) ) {
72 $this->pod = $pod;
73 } else {
74 $this->pod = false;
75 }
76 }//end if
77
78 }
79
80 /**
81 * Add fields, based on options to REST read/write requests
82 *
83 * @since 2.5.6
84 *
85 * @access protected
86 */
87 protected function add_fields() {
88
89 $fields = $this->pod->fields();
90
91 $rest_hook_name = $this->pod->pod_data['name'];
92
93 if ( 'media' === $rest_hook_name ) {
94 $rest_hook_name = 'attachment';
95 } elseif ( 'comment' === $this->pod->pod_data['type'] ) {
96 $rest_hook_name = 'comment';
97 }
98
99 $rest_hook_name = 'rest_insert_' . $rest_hook_name;
100
101 if ( ! has_action( $rest_hook_name, array( 'PodsRESTHandlers', 'save_handler' ) ) ) {
102 add_action( $rest_hook_name, array( 'PodsRESTHandlers', 'save_handler' ), 10, 3 );
103 }
104
105 foreach ( $fields as $field_name => $field ) {
106 $read = self::field_allowed_to_extend( $field_name, $this->pod, 'read' );
107 $write = self::field_allowed_to_extend( $field_name, $this->pod, 'write' );
108
109 $this->register( $field_name, $read, $write );
110 }
111
112 }
113
114 /**
115 * Register fields and their callbacks for read/write via REST
116 *
117 * @since 2.5.6
118 *
119 * @access protected
120 *
121 * @param string $field_name Name of field
122 * @param bool $read Field allows REST API read access
123 * @param bool $write Field allows REST API write access
124 */
125 protected function register( $field_name, $read, $write ) {
126
127 $args = array();
128
129 if ( $read ) {
130 $args['get_callback'] = array( 'PodsRESTHandlers', 'get_handler' );
131 }
132
133 if ( $write ) {
134 $args['pods_update'] = true;
135 }
136
137 $object_type = $this->pod->pod;
138
139 if ( 'media' === $object_type ) {
140 $object_type = 'attachment';
141 }
142
143 if ( $read || $write ) {
144 register_rest_field( $object_type, $field_name, $args );
145 }
146
147 }
148
149 /**
150 * Check if a field supports read or write via the REST API.
151 *
152 * @since 2.5.6
153 *
154 * @param string $field_name The field name.
155 * @param Pods $pod Pods object.
156 * @param string $mode Are we checking read or write?
157 *
158 * @return bool If supports, true, else false.
159 */
160 public static function field_allowed_to_extend( $field_name, $pod, $mode = 'read' ) {
161
162 $allowed = false;
163
164 if ( is_object( $pod ) ) {
165 $field = $pod->fields( $field_name );
166
167 if ( $field ) {
168 $pod_options = $pod->pod_data['options'];
169
170 $read_all = (int) pods_v( 'read_all', $pod_options, 0 );
171 $write_all = (int) pods_v( 'write_all', $pod_options, 0 );
172
173 if ( 'read' === $mode && 1 === $read_all ) {
174 $allowed = true;
175 } elseif ( 'write' === $mode && 1 === $write_all ) {
176 $allowed = true;
177 } else {
178 $rest_read = (int) $pod->fields( $field_name, 'rest_read' );
179 $rest_write = (int) $pod->fields( $field_name, 'rest_write' );
180
181 if ( 'read' === $mode && 1 === $rest_read ) {
182 $allowed = true;
183 } elseif ( 'write' === $mode && 1 === $rest_write ) {
184 $allowed = true;
185 }
186 }
187 }//end if
188 }//end if
189
190 return $allowed;
191
192 }
193
194 }
195