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-list-items-api-controller.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CMBIRD_List_Items_API_Controller extends WC_REST_CRUD_Controller { |
| 8 | |
| 9 | protected $namespace = 'wc/v3'; |
| 10 | protected $rest_base = 'list_items'; |
| 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 . '(?:/(?P<type>))?', |
| 18 | array( |
| 19 | array( |
| 20 | 'methods' => WP_REST_Server::READABLE, |
| 21 | 'callback' => array( $this, 'list_items' ), |
| 22 | 'args' => $this->get_params(), |
| 23 | 'permission_callback' => array( $this, 'check_permission_to_edit_products' ), |
| 24 | ), |
| 25 | ) |
| 26 | ); |
| 27 | register_rest_route( |
| 28 | $this->namespace, |
| 29 | '/' . $this->rest_base . '(?:/(?P<type>))?', |
| 30 | array( |
| 31 | array( |
| 32 | 'methods' => WP_REST_Server::READABLE, |
| 33 | 'callback' => array( $this, 'list_items' ), |
| 34 | 'args' => $this->get_params(), |
| 35 | 'permission_callback' => array( $this, 'check_permission_to_edit_products' ), |
| 36 | ), |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | public function check_permission_to_edit_products() { |
| 42 | return current_user_can( 'edit_products' ); |
| 43 | } |
| 44 | |
| 45 | public function get_params() { |
| 46 | $params = array( |
| 47 | 'type' => array( |
| 48 | 'required' => true, |
| 49 | 'description' => __( 'Type of item to list ("users", "posts", "products", etc)', 'commercebird' ), |
| 50 | 'type' => 'string', |
| 51 | 'sanitize_callback' => function ( $param ) { |
| 52 | return strtolower( $param ); |
| 53 | }, |
| 54 | ), |
| 55 | 'orderby' => array( |
| 56 | 'required' => false, |
| 57 | 'description' => __( 'Parameter to sort items by (default "modified")', 'commercebird' ), |
| 58 | 'type' => 'string', |
| 59 | 'default' => 'modified', |
| 60 | 'sanitize_callback' => function ( $param ) { |
| 61 | return strtolower( $param ); |
| 62 | }, |
| 63 | ), |
| 64 | 'order' => array( |
| 65 | 'required' => false, |
| 66 | 'description' => __( 'ASC or DESC (default "DESC")', 'commercebird' ), |
| 67 | 'type' => 'string', |
| 68 | 'default' => 'desc', |
| 69 | 'validate_callback' => function ( $param ) { |
| 70 | return in_array( strtolower( $param ), array( 'asc', 'desc' ), true ); |
| 71 | }, |
| 72 | 'sanitize_callback' => function ( $param ) { |
| 73 | return strtolower( $param ); |
| 74 | }, |
| 75 | ), |
| 76 | 'after_date' => array( |
| 77 | 'required' => false, |
| 78 | 'description' => __( 'last_modified after this date', 'commercebird' ), |
| 79 | 'type' => 'string', |
| 80 | 'format' => 'date-time', |
| 81 | ), |
| 82 | 'before_date' => array( |
| 83 | 'required' => false, |
| 84 | 'description' => __( 'last_modified before that date', 'commercebird' ), |
| 85 | 'type' => 'string', |
| 86 | 'format' => 'date-time', |
| 87 | ), |
| 88 | 'items_per_page' => array( |
| 89 | 'required' => false, |
| 90 | 'description' => __( 'The maximum returned number of results (needed in pagination)', 'commercebird' ), |
| 91 | 'type' => 'number', |
| 92 | 'default' => '100', |
| 93 | ), |
| 94 | 'offset' => array( |
| 95 | 'required' => false, |
| 96 | 'description' => __( 'Offset the returned results (needed in pagination)', 'commercebird' ), |
| 97 | 'type' => 'number', |
| 98 | ), |
| 99 | 'paged' => array( |
| 100 | 'required' => false, |
| 101 | 'description' => __( 'When used with number, defines the page of results to return. Default 1.', 'commercebird' ), |
| 102 | 'type' => 'number', |
| 103 | ), |
| 104 | 'post_status' => array( |
| 105 | 'required' => false, |
| 106 | 'description' => __( 'Status of posts to retrieve', 'commercebird' ), |
| 107 | 'type' => 'string', |
| 108 | 'default' => 'any', |
| 109 | ), |
| 110 | ); |
| 111 | return $params; |
| 112 | } |
| 113 | |
| 114 | public function list_items( $request ) { |
| 115 | $response = array(); |
| 116 | try { |
| 117 | $args = array(); |
| 118 | if ( ! empty( $request['orderby'] ) ) { |
| 119 | $args['orderby'] = $request['orderby']; |
| 120 | } |
| 121 | if ( ! empty( $request['order'] ) ) { |
| 122 | $args['order'] = $request['order']; |
| 123 | } |
| 124 | if ( ! empty( $request['after_date'] ) || ! empty( $request['before_date'] ) ) { |
| 125 | $args['date_query'] = array(); |
| 126 | if ( ! empty( $request['after_date'] ) ) { |
| 127 | $args['date_query']['column'] = 'post_modified'; |
| 128 | $args['date_query']['after'] = $request['after_date']; |
| 129 | } |
| 130 | if ( ! empty( $request['before_date'] ) ) { |
| 131 | $args['date_query']['column'] = 'post_modified'; |
| 132 | $args['date_query']['before'] = $request['before_date']; |
| 133 | } |
| 134 | } |
| 135 | $args['posts_per_page'] = isset( $args['date_query'] ) ? -1 : $request['items_per_page']; |
| 136 | if ( ! empty( $request['offset'] ) ) { |
| 137 | $args['offset'] = $request['offset']; |
| 138 | } |
| 139 | if ( array_key_exists( 'paged', $request ) ) { |
| 140 | $args['paged'] = $request['paged']; |
| 141 | } |
| 142 | |
| 143 | if ( ! empty( $request['post_status'] ) ) { |
| 144 | $args['post_status'] = $request['post_status']; |
| 145 | } |
| 146 | $ids = array(); |
| 147 | |
| 148 | if ( $request['type'] === 'users' ) { |
| 149 | $users = get_users( array( 'fields' => array( 'ID' ) ) ); |
| 150 | foreach ( $users as $user ) { |
| 151 | $user_data = get_user_meta( $user->ID ); |
| 152 | if ( ! empty( $request['after_date'] ) ) { |
| 153 | if ( strtotime( $user_data['cmbird_profile_updated'][0] ) > strtotime( $request['after_date'] ) ) { |
| 154 | if ( ! in_array( $user->ID, $ids ) ) { |
| 155 | $ids[] = (int) $user->ID; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } else { |
| 161 | $args['post_type'] = $request['type']; |
| 162 | |
| 163 | $query = new WP_Query( $args ); |
| 164 | $results = $query->posts; |
| 165 | |
| 166 | foreach ( $results as $item ) { |
| 167 | $value = $item->ID; |
| 168 | if ( ! in_array( $value, $ids ) ) { |
| 169 | $ids[] = (int) $value; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | $response = array( |
| 174 | 'result' => 'success', |
| 175 | 'ids' => $ids, |
| 176 | ); |
| 177 | } catch ( Exception $e ) { |
| 178 | $response = array( |
| 179 | 'result' => 'error', |
| 180 | 'message' => $e->getMessage(), |
| 181 | ); |
| 182 | } |
| 183 | return $response; |
| 184 | } |
| 185 | } |
| 186 |