class-api-for-cmbird.php
1 year ago
class-api-for-exact-webhooks.php
1 year ago
class-api-for-product-webhook.php
1 year ago
class-api-for-shipping-status.php
1 year ago
class-api-for-woo-order.php
1 year ago
class-api-for-zoho-inventory.php
1 year ago
class-commercebird-list-items-api-controller.php
1 year ago
class-commercebird-media-api-controller.php
1 year ago
class-commercebird-metadata-controller.php
1 year ago
index.php
1 year ago
trait-api-permission.php
11 months ago
class-commercebird-metadata-controller.php
336 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 | '_cost_price' => get_post_meta( $post_id, '_cost_price', true ), |
| 84 | ); |
| 85 | $response['products'][] = array( |
| 86 | 'id' => $post_id, |
| 87 | 'data' => $product_meta, |
| 88 | ); |
| 89 | } catch ( Exception $e ) { |
| 90 | $response['products'][] = array( |
| 91 | 'id' => $post_id, |
| 92 | 'result' => 'error', |
| 93 | 'message' => $e->getMessage(), |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | return $response; |
| 98 | } |
| 99 | |
| 100 | public function get_params() { |
| 101 | $params = array( |
| 102 | 'products' => array( |
| 103 | 'required' => true, |
| 104 | 'description' => __( 'Array of products to change.', 'commercebird' ), |
| 105 | 'type' => 'array', |
| 106 | 'items' => array( |
| 107 | 'description' => __( 'Post object', 'commercebird' ), |
| 108 | 'type' => 'object', |
| 109 | 'properties' => array( |
| 110 | 'id' => array( |
| 111 | 'required' => true, |
| 112 | 'description' => __( 'Post ID.', 'commercebird' ), |
| 113 | 'type' => 'integer', |
| 114 | ), |
| 115 | 'data' => array( |
| 116 | 'required' => true, |
| 117 | 'description' => __( 'Array of meta and taxonomy fields to change.', 'commercebird' ), |
| 118 | 'type' => 'array', |
| 119 | 'items' => array( |
| 120 | 'description' => __( 'Array of meta and taxonomy fields to change.', 'commercebird' ), |
| 121 | 'type' => 'object', |
| 122 | 'properties' => array( |
| 123 | 'key' => array( |
| 124 | 'description' => __( 'Field or taxonomy name.', 'commercebird' ), |
| 125 | 'type' => 'string', |
| 126 | 'sanitize_callback' => 'sanitize_text_field', |
| 127 | ), |
| 128 | 'value' => array( |
| 129 | 'description' => __( 'Value.', 'commercebird' ), |
| 130 | 'default' => '', |
| 131 | 'sanitize_callback' => 'sanitize_text_field', |
| 132 | ), |
| 133 | 'type' => array( |
| 134 | 'description' => __( 'Key type. Possible values are "meta" and "taxonomy".', 'commercebird' ), |
| 135 | 'type' => 'string', |
| 136 | 'enum' => array( 'post', 'meta', 'taxonomy' ), |
| 137 | ), |
| 138 | ), |
| 139 | ), |
| 140 | ), |
| 141 | ), |
| 142 | ), |
| 143 | ), |
| 144 | ); |
| 145 | return $params; |
| 146 | } |
| 147 | |
| 148 | public function update_meta( $request ) { |
| 149 | $response = $this->iterate_through_data( $request, 'update' ); |
| 150 | return $response; |
| 151 | } |
| 152 | public function delete_meta( $request ) { |
| 153 | $response = $this->iterate_through_data( $request, 'delete' ); |
| 154 | return $response; |
| 155 | } |
| 156 | public function list_meta( $request ) { |
| 157 | $response = $this->iterate_through_data( $request, 'list_data' ); |
| 158 | return $response; |
| 159 | } |
| 160 | |
| 161 | public function iterate_through_data( $request, $action ) { |
| 162 | $response = array( 'products' => array() ); |
| 163 | foreach ( $request['products'] as $i => $post ) { |
| 164 | try { |
| 165 | $post_id = $post['id']; |
| 166 | if ( get_post_status( $post_id ) === false ) { |
| 167 | throw new Exception( "Post with id '{$post_id}' does not exist." ); |
| 168 | } |
| 169 | |
| 170 | $response_data = array(); |
| 171 | foreach ( $post['data'] as $j => $meta ) { |
| 172 | try { |
| 173 | if ( ! isset( $meta['key'] ) || empty( $meta['key'] ) ) { |
| 174 | throw new Exception( 'Meta key is required.' ); |
| 175 | } |
| 176 | $key = $meta['key']; |
| 177 | $value = $meta['value']; |
| 178 | |
| 179 | if ( isset( $meta['type'] ) ) { |
| 180 | $type = $meta['type']; |
| 181 | } elseif ( taxonomy_exists( $key ) ) { |
| 182 | $type = 'taxonomy'; |
| 183 | } elseif ( $post = get_post( $post_id ) && isset( $post->$key ) ) { |
| 184 | $type = 'post'; |
| 185 | } else { |
| 186 | $type = 'meta'; |
| 187 | } |
| 188 | |
| 189 | $result = $this->$action( $post_id, $type, $key, $value ); |
| 190 | |
| 191 | $response_data[] = $result; |
| 192 | } catch ( Exception $e ) { |
| 193 | $response_data[] = array_merge( |
| 194 | $meta, |
| 195 | array( |
| 196 | 'result' => 'error', |
| 197 | 'message' => $e->getMessage(), |
| 198 | ) |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | $response['products'][] = array( |
| 203 | 'id' => $post_id, |
| 204 | 'data' => $response_data, |
| 205 | ); |
| 206 | } catch ( Exception $e ) { |
| 207 | $response['products'][] = array( |
| 208 | 'id' => $post_id, |
| 209 | 'result' => 'error', |
| 210 | 'message' => $e->getMessage(), |
| 211 | ); |
| 212 | } |
| 213 | } |
| 214 | return $response; |
| 215 | } |
| 216 | |
| 217 | public function update( $post_id, $type, $key, $value ) { |
| 218 | switch ( $type ) { |
| 219 | case 'post': |
| 220 | $post = array( |
| 221 | 'ID' => $post_id, |
| 222 | $key => $value, |
| 223 | ); |
| 224 | $update_post = wp_update_post( $post, true ); |
| 225 | if ( is_wp_error( $update_post ) ) { |
| 226 | throw new Exception( esc_html( $update_post->get_error_message() ) ); |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case 'meta': |
| 231 | if ( $value != get_post_meta( $post_id, $key, true ) ) { |
| 232 | //add_post_meta($post_id, $key, $value, true); |
| 233 | $update_post_meta = update_post_meta( $post_id, $key, $value ); |
| 234 | if ( $update_post_meta === false ) { |
| 235 | throw new Exception( 'Meta update failed.' ); |
| 236 | } |
| 237 | } |
| 238 | break; |
| 239 | |
| 240 | case 'taxonomy': |
| 241 | $post_type = get_post_type( $post_id ); |
| 242 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 243 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 244 | } |
| 245 | if ( empty( $value ) ) { |
| 246 | throw new Exception( 'Value is required to add taxonomy term.' ); |
| 247 | } |
| 248 | $add_terms = wp_set_object_terms( $post_id, $value, $key, true ); |
| 249 | if ( is_wp_error( $add_terms ) ) { |
| 250 | throw new Exception( esc_html( $add_terms->get_error_message() ) ); |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | return array( |
| 255 | 'type' => $type, |
| 256 | 'key' => $key, |
| 257 | 'value' => $value, |
| 258 | 'status' => 'success', |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | public function delete( $post_id, $type, $key, $value ) { |
| 263 | switch ( $type ) { |
| 264 | case 'post': |
| 265 | $post = array( |
| 266 | 'ID' => $post_id, |
| 267 | $key => '', |
| 268 | ); |
| 269 | $update_post = wp_update_post( $post, true ); |
| 270 | if ( is_wp_error( $update_post ) ) { |
| 271 | throw new Exception( esc_html( $update_post->get_error_message() ) ); |
| 272 | } |
| 273 | break; |
| 274 | |
| 275 | case 'meta': |
| 276 | $delete_post_meta = delete_post_meta( $post_id, $key ); |
| 277 | if ( $delete_post_meta === false ) { |
| 278 | throw new Exception( 'Meta delete failed.' ); |
| 279 | } |
| 280 | break; |
| 281 | |
| 282 | case 'taxonomy': |
| 283 | $post_type = get_post_type( $post_id ); |
| 284 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 285 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 286 | } |
| 287 | if ( empty( $value ) ) { |
| 288 | throw new Exception( 'Value is required to delete taxonomy term.' ); |
| 289 | } |
| 290 | $remove_term = wp_remove_object_terms( $post_id, $value, $key ); |
| 291 | if ( is_wp_error( $remove_term ) ) { |
| 292 | throw new Exception( esc_html( $remove_term->get_error_message() ) ); |
| 293 | } elseif ( $remove_term === false ) { |
| 294 | throw new Exception( 'Term delete failed.' ); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | return array( |
| 299 | 'type' => $type, |
| 300 | 'key' => $key, |
| 301 | 'value' => $value, |
| 302 | 'status' => 'success', |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | public function list_data( $post_id, $type, $key, $value ) { |
| 307 | switch ( $type ) { |
| 308 | case 'post': |
| 309 | $post = get_post( $post_id ); |
| 310 | if ( ! isset( $post->$key ) ) { |
| 311 | throw new Exception( esc_html( "Post table don't have '{$key}' field." ) ); |
| 312 | } |
| 313 | $value = $post->$key; |
| 314 | break; |
| 315 | |
| 316 | case 'meta': |
| 317 | $value = get_post_meta( $post_id, $key, true ); |
| 318 | break; |
| 319 | |
| 320 | case 'taxonomy': |
| 321 | $post_type = get_post_type( $post_id ); |
| 322 | if ( is_object_in_taxonomy( $post_type, $key ) === false ) { |
| 323 | throw new Exception( esc_html( "Taxonomy '{$key}' is not applicable to '{$post_type}' post type." ) ); |
| 324 | } |
| 325 | $terms = wp_get_object_terms( $post_id, $key, array( 'fields' => 'slugs' ) ); |
| 326 | $value = implode( '|', $terms ); |
| 327 | break; |
| 328 | } |
| 329 | return array( |
| 330 | 'type' => $type, |
| 331 | 'key' => $key, |
| 332 | 'value' => $value, |
| 333 | ); |
| 334 | } |
| 335 | } |
| 336 |