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-media-api-controller.php
241 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class CMBIRD_Media_API_Controller extends WC_REST_CRUD_Controller { |
| 8 | |
| 9 | protected $namespace = 'wc/v3'; |
| 10 | protected $rest_base = 'media'; |
| 11 | protected $upload_dir; |
| 12 | protected $media_controller; |
| 13 | |
| 14 | public function register_routes() { |
| 15 | try { |
| 16 | if ( ! class_exists( 'WP_REST_Attachments_Controller' ) ) { |
| 17 | throw new Exception( 'WP API not installed.' ); |
| 18 | } |
| 19 | $this->media_controller = new WP_REST_Attachments_Controller( 'attachment' ); |
| 20 | } catch ( Exception $e ) { |
| 21 | wp_die( esc_html( $e->getMessage() ) ); |
| 22 | } |
| 23 | |
| 24 | register_rest_route( |
| 25 | $this->namespace, |
| 26 | '/' . $this->rest_base, |
| 27 | array( |
| 28 | array( |
| 29 | 'methods' => WP_REST_Server::READABLE, |
| 30 | 'callback' => array( $this, 'list_images' ), |
| 31 | 'permission_callback' => array( $this->media_controller, 'get_items_permissions_check' ), |
| 32 | ), |
| 33 | array( |
| 34 | 'methods' => WP_REST_Server::CREATABLE, |
| 35 | 'callback' => array( $this, 'upload_image' ), |
| 36 | 'args' => $this->get_params_upload(), |
| 37 | 'permission_callback' => array( $this->media_controller, 'create_item_permissions_check' ), |
| 38 | ), |
| 39 | ) |
| 40 | ); |
| 41 | register_rest_route( |
| 42 | $this->namespace, |
| 43 | '/' . $this->rest_base . '/(?P<id>\d+)', |
| 44 | array( |
| 45 | array( |
| 46 | 'methods' => WP_REST_Server::READABLE, |
| 47 | 'callback' => array( $this, 'get_item' ), |
| 48 | 'permission_callback' => array( $this->media_controller, 'get_item_permissions_check' ), |
| 49 | ), |
| 50 | array( |
| 51 | 'methods' => WP_REST_Server::EDITABLE, |
| 52 | 'callback' => array( $this, 'update_image' ), |
| 53 | 'args' => $this->media_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 54 | 'permission_callback' => array( $this->media_controller, 'update_item_permissions_check' ), |
| 55 | ), |
| 56 | array( |
| 57 | 'methods' => WP_REST_Server::DELETABLE, |
| 58 | 'callback' => array( $this, 'delete_image' ), |
| 59 | 'args' => $this->get_params_delete(), |
| 60 | 'permission_callback' => array( $this->media_controller, 'delete_item_permissions_check' ), |
| 61 | ), |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | register_rest_route( |
| 66 | $this->namespace, |
| 67 | '/' . $this->rest_base, |
| 68 | array( |
| 69 | array( |
| 70 | 'methods' => WP_REST_Server::READABLE, |
| 71 | 'callback' => array( $this, 'list_images' ), |
| 72 | 'permission_callback' => array( $this->media_controller, 'get_items_permissions_check' ), |
| 73 | ), |
| 74 | array( |
| 75 | 'methods' => WP_REST_Server::CREATABLE, |
| 76 | 'callback' => array( $this, 'upload_image' ), |
| 77 | 'args' => $this->get_params_upload(), |
| 78 | 'permission_callback' => array( $this->media_controller, 'create_item_permissions_check' ), |
| 79 | ), |
| 80 | ) |
| 81 | ); |
| 82 | register_rest_route( |
| 83 | $this->namespace, |
| 84 | '/' . $this->rest_base . '/(?P<id>\d+)', |
| 85 | array( |
| 86 | array( |
| 87 | 'methods' => WP_REST_Server::READABLE, |
| 88 | 'callback' => array( $this, 'get_item' ), |
| 89 | 'permission_callback' => array( $this->media_controller, 'get_item_permissions_check' ), |
| 90 | ), |
| 91 | array( |
| 92 | 'methods' => WP_REST_Server::EDITABLE, |
| 93 | 'callback' => array( $this, 'update_image' ), |
| 94 | 'args' => $this->media_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
| 95 | 'permission_callback' => array( $this->media_controller, 'update_item_permissions_check' ), |
| 96 | ), |
| 97 | array( |
| 98 | 'methods' => WP_REST_Server::DELETABLE, |
| 99 | 'callback' => array( $this, 'delete_image' ), |
| 100 | 'args' => $this->get_params_delete(), |
| 101 | 'permission_callback' => array( $this->media_controller, 'delete_item_permissions_check' ), |
| 102 | ), |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | public function get_params_upload() { |
| 108 | $params = array( |
| 109 | 'media_attachment' => array( |
| 110 | 'required' => true, |
| 111 | 'description' => __( 'Image encoded as base64.', 'commercebird' ), |
| 112 | 'type' => 'string', |
| 113 | ), |
| 114 | 'title' => array( |
| 115 | 'required' => true, |
| 116 | 'description' => __( 'The title for the object.', 'commercebird' ), |
| 117 | 'type' => 'json', |
| 118 | ), |
| 119 | 'media_path' => array( |
| 120 | 'description' => __( 'Path to directory where file will be uploaded.', 'commercebird' ), |
| 121 | 'type' => 'string', |
| 122 | ), |
| 123 | ); |
| 124 | return $params; |
| 125 | } |
| 126 | |
| 127 | public function get_params_delete() { |
| 128 | return array( |
| 129 | 'force' => array( |
| 130 | 'type' => 'boolean', |
| 131 | 'default' => false, |
| 132 | 'description' => __( 'Whether to bypass trash and force deletion.', 'commercebird' ), |
| 133 | ), |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | public function upload_image( $request ) { |
| 138 | $response = array(); |
| 139 | // $fd = fopen(__DIR__.'/test_media.txt','w+'); |
| 140 | try { |
| 141 | if ( ! empty( $request['media_path'] ) ) { |
| 142 | $this->upload_dir = $request['media_path']; |
| 143 | $this->upload_dir = '/' . trim( $this->upload_dir, '/' ); |
| 144 | add_filter( 'upload_dir', array( $this, 'change_wp_upload_dir' ) ); |
| 145 | } |
| 146 | // fwrite($fd,PHP_EOL.'$filename : '.$filename); |
| 147 | // $filename = $request['title']['rendered']; |
| 148 | $filename = $request['media_name']; |
| 149 | // fwrite($fd,PHP_EOL.'$filename : '.$filename); |
| 150 | $img = $request['media_attachment']; |
| 151 | // fwrite($fd,PHP_EOL.'Function Before Incode : '.$img); |
| 152 | $img = preg_replace( '/^.*base64,/', '', $img ); |
| 153 | $decoded = base64_decode( $img ); |
| 154 | // $url = $file['url']; |
| 155 | // $type = $file['type']; |
| 156 | // $file = $file['file']; |
| 157 | // fwrite($fd,PHP_EOL.'File URL : '.$decoded['url']); |
| 158 | // fwrite($fd,PHP_EOL.'File type : '.$decoded['type']); |
| 159 | // fwrite($fd,PHP_EOL.'File file : '.print_r($decoded['file'],true)); |
| 160 | $request->set_body( $decoded ); |
| 161 | $br_content_type = ( ! empty( $request['mime_type'] ) ? $request['mime_type'] : 'image/jpeg' ); |
| 162 | $request->remove_header( 'Content-Type' ); |
| 163 | $request->add_header( 'Content-Type', $br_content_type ); |
| 164 | $request->add_header( 'Content-Disposition', "attachment;filename=\"{$filename}\"" ); |
| 165 | // fwrite($fd,PHP_EOL.'$request : '.print_r($request,true)); |
| 166 | $result = $this->media_controller->create_item( $request ); |
| 167 | // fwrite($fd,PHP_EOL.'Upload response : '.print_r($result,true)); |
| 168 | $response = rest_ensure_response( $result ); |
| 169 | // fwrite($fd,PHP_EOL.'$response After upload: ',print_r($response,true)); |
| 170 | } catch ( Exception $e ) { |
| 171 | $response['result'] = 'error'; |
| 172 | $response['message'] = $e->getMessage(); |
| 173 | } |
| 174 | |
| 175 | if ( ! empty( $request['media_path'] ) ) { |
| 176 | remove_filter( 'upload_dir', array( $this, 'change_wp_upload_dir' ) ); |
| 177 | } |
| 178 | // fwrite($fd,PHP_EOL.'$response : ',print_r($response,true)); |
| 179 | // fclose($fd); |
| 180 | return $response; |
| 181 | } |
| 182 | |
| 183 | public function list_images( $request ) { |
| 184 | $response = array(); |
| 185 | try { |
| 186 | $result = $this->media_controller->get_items( $request ); |
| 187 | $response = rest_ensure_response( $result ); |
| 188 | } catch ( Exception $e ) { |
| 189 | $response['result'] = 'error'; |
| 190 | $response['message'] = $e->getMessage(); |
| 191 | } |
| 192 | return $response; |
| 193 | } |
| 194 | |
| 195 | public function get_item( $request ) { |
| 196 | $response = array(); |
| 197 | try { |
| 198 | $result = $this->media_controller->get_item( $request ); |
| 199 | $response = rest_ensure_response( $result ); |
| 200 | } catch ( Exception $e ) { |
| 201 | $response['result'] = 'error'; |
| 202 | $response['message'] = $e->getMessage(); |
| 203 | } |
| 204 | return $response; |
| 205 | } |
| 206 | |
| 207 | public function delete_image( $request ) { |
| 208 | $response = array(); |
| 209 | try { |
| 210 | $result = $this->media_controller->delete_item( $request ); |
| 211 | $response = rest_ensure_response( $result ); |
| 212 | } catch ( Exception $e ) { |
| 213 | $response['result'] = 'error'; |
| 214 | $response['message'] = $e->getMessage(); |
| 215 | } |
| 216 | return $response; |
| 217 | } |
| 218 | |
| 219 | public function update_image( $request ) { |
| 220 | $response = array(); |
| 221 | try { |
| 222 | $result = $this->media_controller->update_item( $request ); |
| 223 | $response = rest_ensure_response( $result ); |
| 224 | } catch ( Exception $e ) { |
| 225 | $response['result'] = 'error'; |
| 226 | $response['message'] = $e->getMessage(); |
| 227 | } |
| 228 | return $response; |
| 229 | } |
| 230 | |
| 231 | public function change_wp_upload_dir( $dirs ) { |
| 232 | if ( ! empty( $this->upload_dir ) ) { |
| 233 | $dirs['subdir'] = $this->upload_dir; |
| 234 | $dirs['path'] = $dirs['basedir'] . $this->upload_dir; |
| 235 | $dirs['url'] = $dirs['baseurl'] . $this->upload_dir; |
| 236 | } |
| 237 | |
| 238 | return $dirs; |
| 239 | } |
| 240 | } |
| 241 |