/**
  * We pass in the params, usually $params['arguments'] by reference, as well as
  * a reference to the result object so that we can invalidate and add errors to it.
  */
 public function validateParameters(&$params, &$target)
 {
     $params = apply_filters('rede_pre_validate_parameters', $params, $target);
     foreach ($params as $key => &$value) {
         $tmp_key = str_replace('_', '-', $key);
         $fname = "validators/class-{$tmp_key}-argument-validator.php";
         $tmp_key = str_replace('-', ' ', $tmp_key);
         $tmp_key = ucwords($tmp_key);
         $tmp_key = str_replace(" ", '', $tmp_key);
         $class_name = "JSONAPI_{$tmp_key}_Argument_Validator";
         JSONAPIHelpers::debug("validator class name to load is {$class_name}");
         JSONAPIHelpers::debug("path to validator should be {$fname}");
         $path = $this->findClassFile($fname, false);
         if ($path) {
             require_once $path;
             if (class_exists($class_name)) {
                 $validator = new $class_name();
                 $validator->validate($this, $value, $target);
             } else {
                 JSONAPIHelpers::debug("validator class {$class_name} does not exist?");
             }
         } else {
             JSONAPIHelpers::debug("validator {$fname} does not exist");
         }
     }
     $params = apply_filters('rede_post_validate_parameters', $params, $target);
 }
 public function set_images($params)
 {
     JSONAPIHelpers::debug("set_images beginning");
     $images = $this->orEq($params, 'payload', array());
     foreach ($images as &$attrs) {
         $image = null;
         if (isset($attrs['id'])) {
             $image = API\Image::find($attrs['id']);
         }
         if ($image && is_object($image) && $image->isValid()) {
             $image->fromApiArray($attrs);
             $image->update();
             $attrs = $image->asApiArray();
         } else {
             $this->result->addWarning(__('Image does not exist.', 'woocommerce_json_api'), JSONAPI_PRODUCT_NOT_EXISTS, array('id' => isset($attrs['id']) ? $attrs['id'] : 'none'));
             // Let's create the image if it doesn't exist.
             JSONAPIHelpers::debug("Creating a new image");
             $image = new API\Image();
             $image->create($attrs);
             if (!$image->isValid()) {
                 JSONAPIHelpers::debug("Image is not valid!");
                 return $this->done();
             } else {
                 $this->result->addNotification(__('Created image', 'woocommerce_json_api'), array('id' => $image->_actual_model_id));
             }
             $attrs = $image->asApiArray();
         }
     }
     $this->result->setPayload($images);
     JSONAPIHelpers::debug("set_images done.");
     return $this->done();
 }
function woocommerce_json_api_template_redirect()
{
    global $wpdb;
    global $logout_redirect;
    $helpers = new JSONAPIHelpers();
    $headers = woocommerce_json_api_parse_headers();
    if (function_exists('_wc_jsonapi_log')) {
        _wc_jsonapi_log(__FILE__ . ' In template Redirect');
    }
    if (isset($headers['Content-Type']) && $headers['Content-Type'] == 'application/json' && !isset($_REQUEST['json'])) {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsing Request Body');
        }
        $fp = @fopen('php://input', 'r');
        $body = '';
        if ($fp) {
            while (!feof($fp)) {
                $buf = fread($fp, 1024);
                if (is_string($buf)) {
                    $body .= $buf;
                }
            }
            fclose($fp);
        }
        $hash = json_decode($body, true);
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsed Hash Is: ' . print_r($hash, true));
        }
        foreach ($hash as $key => $value) {
            $_REQUEST[$key] = $value;
        }
    } elseif (isset($_REQUEST['json']) && !empty($_REQUEST['json'])) {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Parsing json out of request ');
        }
        $hash = json_decode(stripslashes($_REQUEST['json']), true);
        foreach ($hash as $key => $value) {
            $_REQUEST[$key] = $value;
        }
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Now request is ');
        }
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log($_REQUEST);
        }
    }
    if (!isset($_REQUEST['action']) || $_REQUEST['action'] != 'woocommerce_json_api') {
        //die("action not set");
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Action is not set');
        }
        return;
    }
    if (is_user_logged_in()) {
        //die("user is logged in");
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' User is logged in! ');
        }
        if (!defined('WCJSONAPI_USER_NO_CARE')) {
            return;
        }
    }
    JSONAPIHelpers::debug(var_export($headers, true));
    if (isset($_REQUEST['action']) && 'woocommerce_json_api' == $_REQUEST['action']) {
        $enabled = get_option($helpers->getPluginPrefix() . '_enabled');
        $require_https = get_option($helpers->getPluginPrefix() . '_require_https');
        if ($enabled != 'no') {
            if ($require_https == 'yes' && $helpers->isHTTPS() == false) {
                JSONAPIHelpers::debug("Cannot continue, HTTPS is required.");
                return;
            }
            if (defined('WC_JSON_API_DEBUG')) {
                JSONAPIHelpers::truncateDebug();
            }
            remove_filter('wp_logout', array($logout_redirect, 'redirect'));
            $api = new WooCommerce_JSON_API();
            $api->setOut('HTTP');
            $api->setUser(null);
            $params = array();
            // maybe we had to serialize some subarrays, so we'll have to unserialize them here
            foreach ($_REQUEST as $key => $value) {
                $params[$key] = $value;
            }
            foreach (array('payload', 'arguments', 'model_filters', 'wordpress_filters', 'image_sizes') as $key) {
                if (isset($_REQUEST[$key]) && is_string($_REQUEST[$key])) {
                    $params[$key] = json_decode(stripslashes($_REQUEST[$key]), true);
                }
            }
            $api->route($params);
        } else {
            JSONAPIHelpers::debug("JSON API is not set to enabled.");
        }
    } else {
        if (function_exists('_wc_jsonapi_log')) {
            _wc_jsonapi_log(__FILE__ . ' Action is still not set!');
        }
    }
}
 /**
  * We pass in the params, usually $params['arguments'] by reference, as well as
  * a reference to the result object so that we can invalidate and add errors to it.
  */
 public function validateParameters(&$params, &$target)
 {
     $params = apply_filters('rede_pre_validate_parameters', $params, $target);
     foreach ($params as $key => &$value) {
         $tmp_key = str_replace('_', '-', $key);
         $fname = "class-{$tmp_key}-validator.php";
         $tmp_key = str_replace('-', ' ', $tmp_key);
         $tmp_key = ucwords($tmp_key);
         $tmp_key = str_replace(" ", '', $tmp_key);
         $class_name = "{$tmp_key}Validator";
         JSONAPIHelpers::debug("class name to load is {$class_name}");
         $path = $this->findClassFile($fname, false);
         if ($path) {
             require_once $path;
             if (class_exists($class_name)) {
                 $validator = new $class_name();
                 $validator->validate($this, $params, $target);
             }
         }
     }
     $params = apply_filters('rede_post_validate_parameters', $params, $target);
     //return array($params, $target);
 }
 public function done()
 {
     JSONAPIHelpers::debug("WooCommerce_JSON_API::done() called..");
     wp_logout();
     if ($this->return_type == 'HTTP') {
         if (!defined('WCJSONAPI_NO_HEADERS')) {
             header("Content-type: application/json");
         }
         echo $this->result->asJSON();
         if (!defined('WCJSONAPI_NO_DIE')) {
             die;
         }
     } else {
         if ($this->return_type == "ARRAY") {
             return $this->result->getParams();
         } else {
             if ($this->return_type == "JSON") {
                 return $this->result->asJSON();
             } else {
                 if ($this->return_type == "OBJECT") {
                     return $this->result;
                 }
             }
         }
     }
 }