apis
11 months ago
purchase-orders
1 year ago
zoho-crm
1 year ago
zoho-inventory
11 months ago
class-api-handler-zoho.php
11 months ago
class-auth-zoho.php
1 year ago
class-common.php
1 year ago
class-plugin.php
11 months ago
class-wc-api.php
1 year ago
index.php
1 year ago
class-wc-api.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Extending the WC API with our custom endpoints. |
| 11 | * |
| 12 | * @author Fawad Tiemoerie <info@roadmapstudios.com> |
| 13 | * @since 2.0.0 |
| 14 | */ |
| 15 | class Cmbird_WC_API { |
| 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | add_action( 'rest_api_init', array( $this, 'register_routes' ), 15 ); |
| 22 | } |
| 23 | |
| 24 | public function register_routes() { |
| 25 | global $wp_version; |
| 26 | if ( version_compare( $wp_version, 6.0, '<' ) ) { |
| 27 | return; |
| 28 | } |
| 29 | $api_classes = array( |
| 30 | 'CMBIRD_Media_API_Controller', |
| 31 | 'CMBIRD_Metadata_API_Controller', |
| 32 | 'CMBIRD_List_Items_API_Controller', |
| 33 | ); |
| 34 | foreach ( $api_classes as $api_class ) { |
| 35 | $controller = new $api_class(); |
| 36 | $controller->register_routes(); |
| 37 | } |
| 38 | // register cost_price as meta field for products. |
| 39 | register_rest_field( |
| 40 | 'product', |
| 41 | '_cost_price', |
| 42 | array( |
| 43 | 'get_callback' => array( $this, 'cmbird_get_product_field' ), |
| 44 | 'update_callback' => array( $this, 'cmbird_update_product_field' ), |
| 45 | 'schema' => null, |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get meta field value. |
| 52 | * |
| 53 | * @param mixed $object |
| 54 | * @param mixed $object |
| 55 | * @param mixed $field_name |
| 56 | * @param mixed $request |
| 57 | * @return mixed |
| 58 | */ |
| 59 | public function cmbird_get_product_field( $object, $field_name, $request ) { |
| 60 | return get_post_meta( $object['id'], $field_name, true ); |
| 61 | } |
| 62 | /** |
| 63 | * Update meta field value. |
| 64 | * @param mixed $value |
| 65 | * @param mixed $object |
| 66 | * @param mixed $field_name |
| 67 | * @return bool|int |
| 68 | */ |
| 69 | public function cmbird_update_product_field( $value, $object, $field_name ) { |
| 70 | return update_post_meta( $object->id, $field_name, $value ); |
| 71 | } |
| 72 | } |
| 73 |