class-api-for-cmbird.php
7 months ago
class-api-for-exact-webhooks.php
5 months ago
class-api-for-product-webhook.php
5 months ago
class-api-for-shipping-status.php
9 months ago
class-api-for-woo-order.php
4 months ago
class-api-for-zoho-inventory.php
9 months ago
class-commercebird-list-items-api-controller.php
10 months ago
class-commercebird-media-api-controller.php
10 months ago
class-commercebird-metadata-controller.php
5 months ago
index.php
1 year ago
trait-api-permission.php
7 months ago
class-commercebird-metadata-controller.php
335 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CMBIRD_Metadata_API_Controller extends WC_REST_CRUD_Controller { |
| 8 | |
| 9 | protected $namespace = 'wc/v3'; |
| 10 | protected $rest_base = 'metadata'; |
| 11 | |
| 12 | public $post_fields = array( 'post_name', 'post_title', 'post_content' ); |
| 13 | |
| 14 | public function register_routes() { |
| 15 | register_rest_route( |
| 16 | $this->namespace, |
| 17 | '/' . $this->rest_base, |
| 18 | array( |
| 19 | array( |
| 20 | 'methods' => WP_REST_Server::EDITABLE, |
| 21 | 'callback' => array( $this, 'update_meta' ), |
| 22 | 'permission_callback' => array( $this, 'check_permission_to_edit_products' ), |
| 23 | 'args' => $this->get_params(), |
| 24 | ), |
| 25 | ) |
| 26 | ); |
| 27 | register_rest_route( |
| 28 | $this->namespace, |
| 29 | '/' . $this->rest_base . '/delete', |
| 30 | array( |
| 31 | array( |
| 32 | 'methods' => WP_REST_Server::EDITABLE, |
| 33 | 'callback' => array( $this, 'delete_meta' ), |
| 34 | 'permission_callback' => array( $this, 'check_permission_to_edit_products' ), |
| 35 | 'args' => $this->get_params(), |
| 36 | ), |
| 37 | ) |
| 38 | ); |
| 39 | register_rest_route( |
| 40 | $this->namespace, |
| 41 | '/' . $this->rest_base . '/list', |
| 42 | array( |
| 43 | array( |
| 44 | 'methods' => WP_REST_Server::EDITABLE, |
| 45 | 'callback' => array( $this, 'get_meta' ), |
| 46 | 'permission_callback' => array( $this, 'check_permission_to_edit_products' ), |
| 47 | 'args' => $this->get_product_params(), |
| 48 | ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | public function check_permission_to_edit_products() { |
| 54 | return current_user_can( 'edit_products' ); |
| 55 | } |
| 56 | |
| 57 | public function get_product_params() { |
| 58 | $params = array( |
| 59 | 'ids' => array( |
| 60 | 'type' => 'array', |
| 61 | 'sanitize_callback' => 'rest_sanitize_request_arg', |
| 62 | 'validate_callback' => 'rest_validate_request_arg', |
| 63 | ), |
| 64 | ); |
| 65 | return $params; |
| 66 | } |
| 67 | |
| 68 | public function get_meta( $request ) { |
| 69 | $response = array( 'products' => array() ); |
| 70 | foreach ( $request['ids'] as $post_id ) { |
| 71 | try { |
| 72 | if ( get_post_status( $post_id ) === false ) { |
| 73 | throw new Exception( "Post with id '{$post_id}' does not exist." ); |
| 74 | } |
| 75 | |
| 76 | $product_meta = array(); |
| 77 | $zi_item_id = get_post_meta( $post_id, 'zi_item_id', true ); |
| 78 | $eo_item_id = get_post_meta( $post_id, 'eo_item_id', true ); |
| 79 | // Construct an array of meta data for the current product ID. |
| 80 | $product_meta = array( |
| 81 | 'zi_item_id' => $zi_item_id, |
| 82 | 'eo_item_id' => $eo_item_id, |
| 83 | ); |
| 84 | $response['products'][] = array( |
| 85 | 'id' => $post_id, |
| 86 | 'data' => $product_meta, |
| 87 | ); |
| 88 | } catch ( Exception $e ) { |
| 89 | $response['products'][] = array( |
| 90 | 'id' => $post_id, |
| 91 | 'result' => 'error', |
| 92 | 'message' => $e->getMessage(), |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | return $response; |
| 97 | } |
| 98 | |
| 99 | public function get_params() { |
| 100 | $params = array( |
| 101 | 'products' => array( |
| 102 | 'required' => true, |
| 103 | 'description' => __( 'Array of products to change.', 'commercebird' ), |
| 104 | 'type' => 'array', |
| 105 | 'items' => array( |
| 106 | 'description' => __( 'Post object', 'commercebird' ), |
| 107 | 'type' => 'object', |
| 108 | 'properties' => array( |
| 109 | 'id' => array( |
| 110 | 'required' => true, |
| 111 | 'description' => __( 'Post ID.', 'commercebird' ), |
| 112 | 'type' => 'integer', |
| 113 | ), |
| 114 | 'data' => array( |
| 115 | 'required' => true, |
| 116 | 'description' => __( 'Array of meta and taxonomy fields to change.', 'commercebird' ), |
| 117 | 'type' => 'array', |
| 118 | 'items' => array( |
| 119 | 'description' => __( 'Array of meta and taxonomy fields to change.', 'commercebird' ), |
| 120 | 'type' => 'object', |
| 121 | 'properties' => array( |
| 122 | 'key' => array( |
| 123 | 'description' => __( 'Field or taxonomy name.', 'commercebird' ), |
| 124 | 'type' => 'string', |
| 125 | 'sanitize_callback' => 'sanitize_text_field', |
| 126 | ), |
| 127 | 'value' => array( |
| 128 | 'description' => __( 'Value.', 'commercebird' ), |
| 129 | 'default' => '', |
| 130 | 'sanitize_callback' => 'sanitize_text_field', |
| 131 | ), |
| 132 | 'type' => array( |
| 133 | 'description' => __( 'Key type. Possible values are "meta" and "taxonomy".', 'commercebird' ), |
| 134 | 'type' => 'string', |
| 135 | 'enum' => array( 'post', 'meta', 'taxonomy' ), |
| 136 | ), |
| 137 | ), |
| 138 | ), |
| 139 | ), |
| 140 | ), |
| 141 | ), |
| 142 | ), |
| 143 | ); |
| 144 | return $params; |
| 145 | } |
| 146 | |
| 147 | public function update_meta( $request ) { |
| 148 | $response = $this->iterate_through_data( $request, 'update' ); |
| 149 | return $response; |
| 150 | } |
| 151 | public function delete_meta( $request ) { |
| 152 | $response = $this->iterate_through_data( $request, 'delete' ); |
| 153 | return $response; |
| 154 | } |
| 155 | public function list_meta( $request ) { |
| 156 | $response = $this->iterate_through_data( $request, 'list_data' ); |
| 157 | return $response; |
| 158 | } |
| 159 | |
| 160 | public function iterate_through_data( $request, $action ) { |
| 161 | $response = array( 'products' => array() ); |
| 162 | foreach ( $request['products'] as $i => $post ) { |
| 163 | try { |
| 164 | $post_id = $post['id']; |
| 165 | if ( get_post_status( $post_id ) === false ) { |
| 166 | throw new Exception( "Post with id '{$post_id}' does not exist." ); |
| 167 | } |
| 168 | |
| 169 | $response_data = array(); |
| 170 | foreach ( $post['data'] as $j => $meta ) { |
| 171 | try { |
| 172 | if ( ! isset( $meta['key'] ) || empty( $meta['key'] ) ) { |
| 173 | throw new Exception( 'Meta key is required.' ); |
| 174 | } |
| 175 | $key = $meta['key']; |
| 176 | $value = $meta['value']; |
| 177 | |
| 178 | if ( isset( $meta['type'] ) ) { |
| 179 | $type = $meta['type']; |
| 180 | } elseif ( taxonomy_exists( $key ) ) { |
| 181 | $type = 'taxonomy'; |
| 182 | } elseif ( $post = get_post( $post_id ) && isset( $post->$key ) ) { |
| 183 | $type = 'post'; |
| 184 | } else { |
| 185 | $type = 'meta'; |
| 186 | } |
| 187 | |
| 188 | $result = $this->$action( $post_id, $type, $key, $value ); |
| 189 | |
| 190 | $response_data[] = $result; |
| 191 | } catch ( Exception $e ) { |
| 192 | $response_data[] = array_merge( |
| 193 | $meta, |
| 194 | array( |
| 195 | 'result' => 'error', |
| 196 | 'message' => $e->getMessage(), |
| 197 | ) |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | $response['products'][] = array( |
| 202 | 'id' => $post_id, |
| 203 | 'data' => $response_data, |
| 204 | ); |
| 205 | } catch ( Exception $e ) { |
| 206 | $response['products'][] = array( |
| 207 | 'id' => $post_id, |
| 208 | 'result' => 'error', |
| 209 | 'message' => $e->getMessage(), |
| 210 | ); |
| 211 | } |
| 212 | } |
| 213 | return $response; |
| 214 | } |
| 215 | |
| 216 | public function update( $post_id, $type, $key, $value ) { |
| 217 | switch ( $type ) { |
| 218 | case 'post': |
| 219 | $post = array( |
| 220 | 'ID' => $post_id, |
| 221 | $key => $value, |
| 222 | ); |
| 223 | $update_post = wp_update_post( $post, true ); |
| 224 | if ( is_wp_error( $update_post ) ) { |
| 225 | throw new Exception( esc_html( $update_post->get_error_message() ) ); |
| 226 | } |
| 227 | break; |
| 228 | |
| 229 | case 'meta': |
| 230 | if ( $value != get_post_meta( $post_id, $key, true ) ) { |
| 231 | // add_post_meta($post_id, $key, $value, true); |
| 232 | $update_post_meta = update_post_meta( $post_id, $key, $value ); |
| 233 | if ( false === $update_post_meta ) { |
| 234 | throw new Exception( 'Meta update failed.' ); |
| 235 | } |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case 'taxonomy': |
| 240 | $post_type = get_post_type( $post_id ); |
| 241 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 242 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 243 | } |
| 244 | if ( empty( $value ) ) { |
| 245 | throw new Exception( 'Value is required to add taxonomy term.' ); |
| 246 | } |
| 247 | $add_terms = wp_set_object_terms( $post_id, $value, $key, true ); |
| 248 | if ( is_wp_error( $add_terms ) ) { |
| 249 | throw new Exception( esc_html( $add_terms->get_error_message() ) ); |
| 250 | } |
| 251 | break; |
| 252 | } |
| 253 | return array( |
| 254 | 'type' => $type, |
| 255 | 'key' => $key, |
| 256 | 'value' => $value, |
| 257 | 'status' => 'success', |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | public function delete( $post_id, $type, $key, $value ) { |
| 262 | switch ( $type ) { |
| 263 | case 'post': |
| 264 | $post = array( |
| 265 | 'ID' => $post_id, |
| 266 | $key => '', |
| 267 | ); |
| 268 | $update_post = wp_update_post( $post, true ); |
| 269 | if ( is_wp_error( $update_post ) ) { |
| 270 | throw new Exception( esc_html( $update_post->get_error_message() ) ); |
| 271 | } |
| 272 | break; |
| 273 | |
| 274 | case 'meta': |
| 275 | $delete_post_meta = delete_post_meta( $post_id, $key ); |
| 276 | if ( false === $delete_post_meta ) { |
| 277 | throw new Exception( 'Meta delete failed.' ); |
| 278 | } |
| 279 | break; |
| 280 | |
| 281 | case 'taxonomy': |
| 282 | $post_type = get_post_type( $post_id ); |
| 283 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 284 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 285 | } |
| 286 | if ( empty( $value ) ) { |
| 287 | throw new Exception( 'Value is required to delete taxonomy term.' ); |
| 288 | } |
| 289 | $remove_term = wp_remove_object_terms( $post_id, $value, $key ); |
| 290 | if ( is_wp_error( $remove_term ) ) { |
| 291 | throw new Exception( esc_html( $remove_term->get_error_message() ) ); |
| 292 | } elseif ( false === $remove_term ) { |
| 293 | throw new Exception( 'Term delete failed.' ); |
| 294 | } |
| 295 | break; |
| 296 | } |
| 297 | return array( |
| 298 | 'type' => $type, |
| 299 | 'key' => $key, |
| 300 | 'value' => $value, |
| 301 | 'status' => 'success', |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | public function list_data( $post_id, $type, $key, $value ) { |
| 306 | switch ( $type ) { |
| 307 | case 'post': |
| 308 | $post = get_post( $post_id ); |
| 309 | if ( ! isset( $post->$key ) ) { |
| 310 | throw new Exception( esc_html( "Post table don't have '{$key}' field." ) ); |
| 311 | } |
| 312 | $value = $post->$key; |
| 313 | break; |
| 314 | |
| 315 | case 'meta': |
| 316 | $value = get_post_meta( $post_id, $key, true ); |
| 317 | break; |
| 318 | |
| 319 | case 'taxonomy': |
| 320 | $post_type = get_post_type( $post_id ); |
| 321 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 322 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 323 | } |
| 324 | $terms = wp_get_object_terms( $post_id, $key, array( 'fields' => 'slugs' ) ); |
| 325 | $value = implode( '|', $terms ); |
| 326 | break; |
| 327 | } |
| 328 | return array( |
| 329 | 'type' => $type, |
| 330 | 'key' => $key, |
| 331 | 'value' => $value, |
| 332 | ); |
| 333 | } |
| 334 | } |
| 335 |