public function asJSON()
 {
     $this->params['payload_length'] = count($this->params['payload']);
     if (PHP_MINOR_VERSION < 4) {
         JSONAPIHelpers::warn("PHP 5.4 and above recommended for the API.");
         $text = json_encode($this->params);
     } else {
         $text = json_encode($this->params, JSON_PRETTY_PRINT);
     }
     $jsonp = false;
     if (isset($this->params['callback'])) {
         $jsonp = $this->params['callback'];
     }
     if (isset($this->params['jsonp'])) {
         $jsonp = $this->params['jsonp'];
     }
     if ($jsonp) {
         return "{$jsonp}({$text});";
     } else {
         return $text;
     }
 }
 /**
  * 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();
 }
 /**
  * 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);
 }
function woocommerce_json_api_settings_page()
{
    $helpers = new JSONAPIHelpers();
    $params = $_POST;
    $nonce = $helpers->orEq($params, '_wpnonce', false);
    $key = $helpers->getPluginPrefix() . '_sitewide_settings';
    if ($nonce && wp_verify_nonce($nonce, $helpers->getPluginPrefix() . '_sitewide_settings') && isset($params[$key])) {
        foreach ($params[$key] as $key2 => $value) {
            update_option($helpers->getPluginPrefix() . '_' . $key2, maybe_serialize($value));
        }
        $key = $helpers->getPluginPrefix() . '_default_permissions';
        if (isset($params[$key])) {
            update_option($key, $params[$key]);
        }
    }
    //$json_api_slug = get_option( $helpers->getPluginPrefix() . '_slug' );
    // $pages = get_pages( array('post_type' => 'page') );
    // $options = array();
    // foreach ( $pages as $page ) {
    //   $options[] = array('value' => $page->post_name, 'content' => $page->post_title);
    // }
    $attrs = array('json_api_sitewide_settings' => array('title' => __('WooCommerce JSON API Settings', 'woocommerce_json_api'), 'fields' => array(array('name' => $helpers->getPluginPrefix() . '_sitewide_settings[enabled]', 'id' => 'json_api_enabled_id', 'value' => get_option($helpers->getPluginPrefix() . '_enabled'), 'options' => array(array('value' => 'yes', 'content' => __('Yes', 'woocommerce_json_api')), array('value' => 'no', 'content' => __('No', 'woocommerce_json_api'))), 'type' => 'select', 'label' => __('API Enabled?', 'woocommerce_json_api'), 'description' => __('Quickly enable/disable The API', 'woocommerce_json_api')), array('name' => $helpers->getPluginPrefix() . '_sitewide_settings[require_https]', 'id' => 'json_api_require_https_id', 'value' => get_option($helpers->getPluginPrefix() . '_require_https'), 'options' => array(array('value' => 'yes', 'content' => __('Yes', 'woocommerce_json_api')), array('value' => 'no', 'content' => __('No', 'woocommerce_json_api'))), 'type' => 'select', 'label' => __('Require HTTPS', 'woocommerce_json_api'), 'description' => __('Only serve HTTPS requests?', 'woocommerce_json_api')), array('name' => $helpers->getPluginPrefix() . '_sitewide_settings[auto_generate_token]', 'id' => 'json_api_auto_generate_token', 'value' => get_option($helpers->getPluginPrefix() . '_auto_generate_token'), 'options' => array(array('value' => 'yes', 'content' => __('Yes', 'woocommerce_json_api')), array('value' => 'no', 'content' => __('No', 'woocommerce_json_api'))), 'type' => 'select', 'label' => __('Automatically Generate Token', 'woocommerce_json_api'), 'description' => __('Generate a token automatically when a user is registered?', 'woocommerce_json_api')))));
    // Here we implement some permissions, a simple yes/no.
    $meta = get_option($helpers->getPluginPrefix() . '_default_permissions');
    if (!is_array($meta)) {
        $meta = array();
    }
    $method = 'access_the_api';
    $field = array('name' => $helpers->getPluginPrefix() . '_default_permissions[can_' . $method . ']', 'id' => 'json_api_can_' . $method . '_id', 'value' => $helpers->orEq($meta, 'can_' . $method, 'yes'), 'type' => 'select', 'options' => array(array('value' => 'yes', 'content' => __('Yes', 'woocommerce_json_api')), array('value' => 'no', 'content' => __('No', 'woocommerce_json_api'))), 'label' => __('Default Can access ', 'woocommerce_json_api') . ucwords(str_replace('_', ' ', $method)), 'description' => __('Whether or not this user can access this method', 'woocommerce_json_api'));
    $attrs['json_api_sitewide_settings']['fields'][] = $field;
    foreach (WooCommerce_JSON_API::getImplementedMethods() as $method) {
        if (strpos($method, 'set_') !== false) {
            $default_value = 'no';
        } else {
            $default_value = 'yes';
        }
        $field = array('name' => $helpers->getPluginPrefix() . '_default_permissions[can_' . $method . ']', 'id' => 'json_api_can_' . $method . '_id', 'value' => $helpers->orEq($meta, 'can_' . $method, $default_value), 'type' => 'select', 'options' => array(array('value' => 'yes', 'content' => __('Yes', 'woocommerce_json_api')), array('value' => 'no', 'content' => __('No', 'woocommerce_json_api'))), 'label' => __('Default Can access ', 'woocommerce_json_api') . ucwords(str_replace('_', ' ', $method)), 'description' => __('Whether or not this user can access this method', 'woocommerce_json_api'));
        $attrs['json_api_sitewide_settings']['fields'][] = $field;
    }
    $attrs = apply_filters('woocommerce_json_api_sitewide_settings_fields', $attrs);
    echo $helpers->renderTemplate('admin-settings-page.php', array('attrs' => $attrs));
}
 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;
                 }
             }
         }
     }
 }