<?php

if (!class_exists('AFFI_JSON_API_Query')) {

    class AFFI_JSON_API_Query {

        // Default values
        protected $defaults = array(
            'date_format' => 'Y-m-d H:i:s',
            'read_more' => 'Read more'
        );

        function __construct() {
            // Register JSON API query vars
            add_filter('query_vars', array(&$this, 'query_vars'));
        }

        function get($key) {
            if (is_array($key)) {
                $result = array();
                foreach ($key as $k) {
                    $result[$k] = $this->get($k);
                }
                return $result;
            }
            $query_var = (isset($_REQUEST[$key])) ? $_REQUEST[$key] : '';
            $wp_query_var = $this->wp_query_var($key);
            if ($wp_query_var) {
                return $wp_query_var;
            } else if ($query_var) {
                return $this->strip_magic_quotes($query_var);
            } else if (isset($this->defaults[$key])) {
                return $this->defaults[$key];
            } else {
                return '';
            }
        }

        function __get($key) {
            return $this->get($key);
        }

        function __isset($key) {
            return ($this->get($key) !== '');
        }

        function wp_query_var($key) {
            $wp_translation = array(
                'affi-json' => 'affi-json',
                'post_id' => 'p',
                'post_slug' => 'name',
                'page_id' => 'page_id',
                'page_slug' => 'name',
                'category_id' => 'cat',
                'category_slug' => 'category_name',
                'tag_id' => 'tag_id',
                'tag_slug' => 'tag',
                'author_id' => 'author',
                'author_slug' => 'author_name',
                'search' => 's',
                'order' => 'order',
                'order_by' => 'orderby'
            );
            if ($key == 'date') {
                $date = '';
                if (get_query_var('year')) {
                    $date = get_query_var('year');
                }
                if (get_query_var('monthnum')) {
                    $month = get_query_var('monthnum');
                    if ($month < 10) {
                        $month = "0$month";
                    }
                    $date .= $month;
                }
                if (get_query_var('day')) {
                    $day = get_query_var('day');
                    if ($day < 10) {
                        $day = "0$day";
                    }
                    $date .= $day;
                }
                return $date;
            } else if (isset($wp_translation[$key])) {
                return get_query_var($wp_translation[$key]);
            } else {
                return '';
            }
        }

        function strip_magic_quotes($value) {
            if (get_magic_quotes_gpc()) {
                return stripslashes($value);
            } else {
                return $value;
            }
        }

        function query_vars($wp_vars) {
            $wp_vars[] = 'affi-json';
            return $wp_vars;
        }

        function get_controller() {
            $json = $this->get('affi-json');
            if (empty($json)) {
                return false;
            }
            if (preg_match('/^[a-zA-Z_]+$/', $json)) {
                return $this->get_legacy_controller($json);
            } else if (preg_match('/^([a-zA-Z0-9_]+)(\/|\.)[a-zA-Z0-9_]+$/', $json, $matches)) {
                return $matches[1];
            } else {
                return 'core';
            }
        }

        function get_legacy_controller($json) {
            global $affi_json_api;
            if ($json == 'submit_comment') {
                if ($affi_json_api->controller_is_active('respond')) {
                    return 'respond';
                } else {
                    $affi_json_api->error("The 'submit_comment' method has been removed from the Core controller. To use this method you must enable the Respond controller from WP Admin > Settings > JSON API.");
                }
            } else if ($json == 'create_post') {
                if ($affi_json_api->controller_is_active('posts')) {
                    return 'posts';
                } else {
                    $affi_json_api->error("The 'create_post' method has been removed from the Core controller. To use this method you must enable the Posts controller from WP Admin > Settings > JSON API.");
                }
            } else {
                return 'core';
            }
        }

        function get_method($controller) {

            global $affi_json_api;

            $method = $this->get('affi-json');
            if (strpos($method, '/') !== false) {
                $method = substr($method, strpos($method, '/') + 1);
            } else if (strpos($method, '.') !== false) {
                $method = substr($method, strpos($method, '.') + 1);
            }

            if (empty($method)) {
                // Case 1: we're not being invoked (done!)
                return false;
            } else if (method_exists("AFFI_JSON_API_{$controller}_Controller", $method)) {
                // Case 2: an explicit method was specified
                return $method;
            } else if ($controller == 'core') {
                $affi_json_api->error("Unknown method '$method'.");
                return 'info';
            }
            // Case 4: either the method doesn't exist or we don't support the page implicitly
            return 'error';
        }

    }

}