Ejemplo n.º 1
0
 public function get_orders_from_trash($params)
 {
     global $wpdb;
     $posts_per_page = $this->orEq($params['arguments'], 'per_page', 15);
     $paged = $this->orEq($params['arguments'], 'page', 0);
     $ids = $this->orEq($params['arguments'], 'ids', false);
     if (!$ids) {
         $args = array('posts_per_page' => $posts_per_page, 'paged' => $paged, 'fields' => 'ids', 'post_type' => 'shop_order', 'post_status' => 'trash');
         $ids = get_posts($args);
         goto fetch;
         // $models = API\Order::all("*")->per($posts_per_page)->page($paged)->fetch();
         // foreach ( $models as $model ) {
         //   $orders[] = $model->asApiArray();
         // }
     } else {
         if ($ids) {
             fetch:
             if (empty($ids)) {
                 $this->result->addWarning(__("There were no Orders in the trash found."), JSONAPI_NO_RESULTS_POSSIBLE);
                 return $this->done();
             }
             //$posts = $ids;
             $joined_ids = join(',', array_map(function ($id) {
                 return "'{$id}'";
             }, $ids));
             $sql = "SELECT ID FROM {$wpdb->posts} WHERE `post_type` IN ('shop_order') AND `post_status` = 'trash' AND `ID` IN ({$joined_ids})";
             $posts = $wpdb->get_col($sql);
             if (is_wp_error($posts)) {
                 throw new Exception($posts->get_messages());
             }
             $orders = array();
             foreach ($posts as $post_id) {
                 try {
                     $post = API\Order::find($post_id);
                 } catch (Exception $e) {
                     JSONAPIHelpers::error("An exception occurred attempting to instantiate a Order object: " . $e->getMessage());
                     $this->result->addError(__("Error occurred instantiating Order object"), -99);
                     return $this->done();
                 }
                 if (!$post) {
                     $this->result->addWarning($post_id . ': ' . __('Order does not exist', 'woocommerce_json_api'), JSONAPI_ORDER_NOT_EXISTS, array('id' => $post_id));
                 } else {
                     $orders[] = $post->asApiArray();
                 }
             }
         }
     }
     $this->result->setPayload($orders);
     return $this->done();
 }
 /**
  *  This function is the single entry point into the API.
  *  
  *  The order of operations goes like this:
  *  
  *  1) A new result object is created.
  *  2) Check to see if it's a valid API User, if not, do stuff and quit
  *  3) Check to see if the method requested has been implemented
  *  4) If it's implemented, call and turn over control to the method
  *  
  *  This function takes a single hash,  usually $_REQUEST
  *  
  *  WHY? 
  *  
  *  Well, as you will notice with WooCommerce, there is an irritatingly large
  *  dependence on _defined_ and $_GET/$_POST variables, throughout their plugin,
  *  each function "depends" on request state, which is fine, except this
  *  violates 'dependency injection'. We don't know where data might come from
  *  in the future, what if another plugin wants to call this one inside of PHP
  *  within a request, multiple times? 
  *  
  *  No module should ever 'depend' on objects outside of itself, they should be
  *  provided with operating data, or 'injected' with it.
  *  
  *  There is nothing 'wrong' with the way WooCommerce does things, only it leads
  *  to a certain inflexibility in what you can do with it.
  */
 public function route($params)
 {
     global $wpdb;
     $method = $this->orEq($params, 'method', false);
     $proc = $this->orEq($params, 'proc', false);
     if ($method && $proc && !strpos('get_') == 0 && !strpos('set_') == 0 && !strpos('delete_') == 0) {
         switch (strtolower($method)) {
             case 'get':
                 $proc = 'get_' . $proc;
                 break;
             case 'put':
                 $proc = 'set_' . $proc;
                 break;
             case 'delete':
                 $proc = 'delete_' . $proc;
                 break;
         }
     }
     /*
      * The idea behind the provider is that there will be
      * several versions of the API in the future, and the
      * user can choose which one they are writing against.
      * This simplifies the provider files a bit and makes
      * the code more modular.
      */
     $version = intval($this->orEq($params, 'version', 1));
     if (!is_numeric($version)) {
         $version = 1;
     }
     if (file_exists(dirname(__FILE__) . '/API_VERSIONS/version' . $version . '.php')) {
         require_once dirname(__FILE__) . '/API_VERSIONS/version' . $version . '.php';
         $klass = "WC_JSON_API_Provider_v{$version}";
         $klass = str_replace('.', '_', $klass);
         $this->provider = new $klass($this);
     }
     // Reorganize any uploaded files and put them in
     // the params
     $files = array();
     $params['uploads'] = $files;
     $this->createNewResult($params);
     // Now we need to allow for people to add dynamic
     // filters to the models.
     if (isset($params['model_filters']) && is_array($params['model_filters'])) {
         JSONAPIHelpers::debug("Model Filters are Present");
         JSONAPIHelpers::debug(var_export($params['model_filters'], true));
         foreach ($params['model_filters'] as $filter_text => $filter) {
             foreach ($filter as $key => &$value) {
                 $value['name'] = substr($wpdb->prepare("%s", $value['name']), 1, strlen($value['name']));
             }
             $callback = function ($table) use($filter) {
                 return array_merge($table, $filter);
             };
             JSONAPIHelpers::debug("Adding filter: " . $filter_text);
             add_filter($filter_text, $callback);
         }
     } else {
         JSONAPIHelpers::debug("No Model Filters Present");
     }
     if (isset($params['image_sizes']) && is_array($params['image_sizes'])) {
         JSONAPIHelpers::debug("Image Sizes Are Defined");
         foreach ($params['image_sizes'] as $size) {
             foreach (array('name', 'width', 'height', 'crop') as $key) {
                 if (!isset($size[$key])) {
                     throw new \Exception(sprintf(__('%s is required when adding image sizes', $this->td), ucwords($key)));
                 }
             }
             add_image_size($size['name'], $size['width'], $size['height'], $size['crop']);
         }
     }
     JSONAPIHelpers::debug("Beggining request");
     JSONAPIHelpers::debug(var_export($params, true));
     if (!$this->isValidAPIUser($params)) {
         $this->result->addError(__('Not a valid API User', 'woocommerce_json_api'), JSONAPI_INVALID_CREDENTIALS);
         return $this->done();
     }
     if (isset($params['arguments']['password'])) {
         // We shouldn't pass back the password.
         $params['arguments']['password'] = '******';
     }
     if ($this->provider->isImplemented($proc)) {
         try {
             // The arguments are passed by reference here
             $this->validateParameters($params['arguments'], $this->result);
             if ($this->result->status() == false) {
                 JSONAPIHelpers::warn("Arguments did not pass validation");
                 return $this->done();
             } else {
                 JSONAPIHelpers::debug("Arguments have passed validation");
             }
             return $this->provider->{$proc}($params);
         } catch (Exception $e) {
             JSONAPIHelpers::error($e->getMessage());
             $this->unexpectedError($params, $e);
         }
     } else {
         JSONAPIHelpers::warn("{$proc} is not implemented...");
         $this->notImplemented($params);
     }
 }