function callback($path = '', $blog_id = 0)
 {
     global $wpdb;
     if ('mine' === $blog_id) {
         $api = WPCOM_JSON_API::init();
         if (!$api->token_details || empty($api->token_details['blog_id'])) {
             return new WP_Error('authorization_required', 'An active access token must be used to query information about the current blog.', 403);
         }
         $blog_id = $api->token_details['blog_id'];
     }
     $blog_id = $this->api->switch_to_blog_and_validate_user($this->api->get_blog_id($blog_id));
     if (is_wp_error($blog_id)) {
         return $blog_id;
     }
     $response = $this->build_current_site_response();
     do_action('wpcom_json_api_objects', 'sites');
     return $response;
 }
 function callback($path = '', $blog_id = 0)
 {
     if ('mine' === $blog_id) {
         $api = WPCOM_JSON_API::init();
         if (!$api->token_details || empty($api->token_details['blog_id'])) {
             return new WP_Error('authorization_required', 'An active access token must be used to query information about the current blog.', 403);
         }
         $blog_id = $api->token_details['blog_id'];
     }
     $blog_id = $this->api->switch_to_blog_and_validate_user($this->api->get_blog_id($blog_id));
     if (is_wp_error($blog_id)) {
         return $blog_id;
     }
     // TODO: enable this when we can do so without being interfered with by
     // other endpoints that might be wrapping this one.
     // Uncomment and see failing test: test_jetpack_site_should_have_true_jetpack_property_via_site_meta
     // $this->filter_fields_and_options();
     $response = $this->build_current_site_response();
     /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
     do_action('wpcom_json_api_objects', 'sites');
     return $response;
 }
 protected function __construct()
 {
     $this->api = WPCOM_JSON_API::init();
 }
 function json_api($args = array())
 {
     $json_api_args = $args[0];
     $verify_api_user_args = $args[1];
     $method = (string) $json_api_args[0];
     $url = (string) $json_api_args[1];
     $post_body = is_null($json_api_args[2]) ? null : (string) $json_api_args[2];
     $my_id = (int) $json_api_args[3];
     $user_details = (array) $json_api_args[4];
     if (!$verify_api_user_args) {
         $user_id = 0;
     } elseif ('internal' === $verify_api_user_args[0]) {
         $user_id = (int) $verify_api_user_args[1];
         if ($user_id) {
             $user = get_user_by('id', $user_id);
             if (!$user || is_wp_error($user)) {
                 return false;
             }
         }
     } else {
         $user_id = call_user_func(array($this, 'test_api_user_code'), $verify_api_user_args);
         if (!$user_id) {
             return false;
         }
     }
     /* debugging
     		error_log( "-- begin json api via jetpack debugging -- " );
     		error_log( "METHOD: $method" );
     		error_log( "URL: $url" );
     		error_log( "POST BODY: $post_body" );
     		error_log( "MY JETPACK ID: $my_id" );
     		error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) );
     		error_log( "VERIFIED USER_ID: " . (int) $user_id );
     		error_log( "-- end json api via jetpack debugging -- " );
     		*/
     $old_user = wp_get_current_user();
     wp_set_current_user($user_id);
     $token = Jetpack_Data::get_access_token(get_current_user_id());
     if (!$token || is_wp_error($token)) {
         return false;
     }
     define('REST_API_REQUEST', true);
     define('WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1');
     // needed?
     require_once ABSPATH . 'wp-admin/includes/admin.php';
     require_once dirname(__FILE__) . '/class.json-api.php';
     $api = WPCOM_JSON_API::init($method, $url, $post_body);
     $api->token_details['user'] = $user_details;
     require_once dirname(__FILE__) . '/class.json-api-endpoints.php';
     $display_errors = ini_set('display_errors', 0);
     ob_start();
     $content_type = $api->serve(false);
     $output = ob_get_clean();
     ini_set('display_errors', $display_errors);
     $nonce = wp_generate_password(10, false);
     $hmac = hash_hmac('md5', $nonce . $output, $token->secret);
     wp_set_current_user(isset($old_user->ID) ? $old_user->ID : 0);
     return array((string) $output, (string) $nonce, (string) $hmac);
 }
 function __construct($args)
 {
     $defaults = array('in_testing' => false, 'description' => '', 'group' => '', 'method' => 'GET', 'path' => '/', 'min_version' => '0', 'max_version' => WPCOM_JSON_API__CURRENT_VERSION, 'force' => '', 'deprecated' => false, 'new_version' => WPCOM_JSON_API__CURRENT_VERSION, 'jp_disabled' => false, 'path_labels' => array(), 'request_format' => array(), 'response_format' => array(), 'query_parameters' => array(), 'version' => 'v1', 'example_request' => '', 'example_request_data' => '', 'example_response' => '', 'required_scope' => '', 'pass_wpcom_user_details' => false, 'can_use_user_details_instead_of_blog_membership' => false, 'custom_fields_filtering' => false, 'allow_cross_origin_request' => false);
     $args = wp_parse_args($args, $defaults);
     $this->in_testing = $args['in_testing'];
     $this->description = $args['description'];
     $this->group = $args['group'];
     $this->stat = $args['stat'];
     $this->force = $args['force'];
     $this->jp_disabled = $args['jp_disabled'];
     $this->method = $args['method'];
     $this->path = $args['path'];
     $this->path_labels = $args['path_labels'];
     $this->min_version = $args['min_version'];
     $this->max_version = $args['max_version'];
     $this->deprecated = $args['deprecated'];
     $this->new_version = $args['new_version'];
     $this->pass_wpcom_user_details = $args['pass_wpcom_user_details'];
     $this->custom_fields_filtering = (bool) $args['custom_fields_filtering'];
     $this->can_use_user_details_instead_of_blog_membership = $args['can_use_user_details_instead_of_blog_membership'];
     $this->allow_cross_origin_request = (bool) $args['allow_cross_origin_request'];
     $this->version = $args['version'];
     $this->required_scope = $args['required_scope'];
     if ($this->request_format) {
         $this->request_format = array_filter(array_merge($this->request_format, $args['request_format']));
     } else {
         $this->request_format = $args['request_format'];
     }
     if ($this->response_format) {
         $this->response_format = array_filter(array_merge($this->response_format, $args['response_format']));
     } else {
         $this->response_format = $args['response_format'];
     }
     if (false === $args['query_parameters']) {
         $this->query = array();
     } elseif (is_array($args['query_parameters'])) {
         $this->query = array_filter(array_merge($this->query, $args['query_parameters']));
     }
     $this->api = WPCOM_JSON_API::init();
     // Auto-add to WPCOM_JSON_API
     /** Example Request/Response ******************************************/
     // Examples for endpoint documentation request
     $this->example_request = $args['example_request'];
     $this->example_request_data = $args['example_request_data'];
     $this->example_response = $args['example_response'];
     $this->api->add($this);
 }
예제 #6
0
 function callback($path = '', $blog_id = 0)
 {
     global $wpdb;
     if ('mine' === $blog_id) {
         $api = WPCOM_JSON_API::init();
         if (!$api->token_details || empty($api->token_details['blog_id'])) {
             return new WP_Error('authorization_required', 'An active access token must be used to query information about the current blog.', 403);
         }
         $blog_id = $api->token_details['blog_id'];
     }
     $blog_id = $this->api->switch_to_blog_and_validate_user($this->api->get_blog_id($blog_id));
     if (is_wp_error($blog_id)) {
         return $blog_id;
     }
     $is_user_logged_in = is_user_logged_in();
     $response = array();
     foreach (array_keys($this->response_format) as $key) {
         switch ($key) {
             case 'ID':
                 $response[$key] = (int) $this->api->get_blog_id_for_output();
                 break;
             case 'name':
                 $response[$key] = (string) get_bloginfo('name');
                 break;
             case 'description':
                 $response[$key] = (string) get_bloginfo('description');
                 break;
             case 'URL':
                 $response[$key] = (string) home_url();
                 break;
             case 'jetpack':
                 if ($is_user_logged_in) {
                     $response[$key] = false;
                 }
                 // magic
                 break;
             case 'post_count':
                 if ($is_user_logged_in) {
                     $response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish'");
                 }
                 break;
             case 'lang':
                 if ($is_user_logged_in) {
                     $response[$key] = (string) get_bloginfo('language');
                 }
                 break;
             case 'meta':
                 $response[$key] = (object) array('links' => (object) array('self' => (string) $this->get_site_link($this->api->get_blog_id_for_output()), 'help' => (string) $this->get_site_link($this->api->get_blog_id_for_output(), 'help'), 'posts' => (string) $this->get_site_link($this->api->get_blog_id_for_output(), 'posts/'), 'comments' => (string) $this->get_site_link($this->api->get_blog_id_for_output(), 'comments/')));
                 break;
         }
     }
     do_action('wpcom_json_api_objects', 'sites');
     return $response;
 }
 function json_api($args = array())
 {
     $json_api_args = $args[0];
     $verify_api_user_args = $args[1];
     $method = (string) $json_api_args[0];
     $url = (string) $json_api_args[1];
     $post_body = is_null($json_api_args[2]) ? null : (string) $json_api_args[2];
     $user_details = (array) $json_api_args[4];
     $locale = (string) $json_api_args[5];
     if (!$verify_api_user_args) {
         $user_id = 0;
     } elseif ('internal' === $verify_api_user_args[0]) {
         $user_id = (int) $verify_api_user_args[1];
         if ($user_id) {
             $user = get_user_by('id', $user_id);
             if (!$user || is_wp_error($user)) {
                 return false;
             }
         }
     } else {
         $user_id = call_user_func(array($this, 'test_api_user_code'), $verify_api_user_args);
         if (!$user_id) {
             return false;
         }
     }
     /* debugging
     		error_log( "-- begin json api via jetpack debugging -- " );
     		error_log( "METHOD: $method" );
     		error_log( "URL: $url" );
     		error_log( "POST BODY: $post_body" );
     		error_log( "VERIFY_ARGS: " . print_r( $verify_api_user_args, 1 ) );
     		error_log( "VERIFIED USER_ID: " . (int) $user_id );
     		error_log( "-- end json api via jetpack debugging -- " );
     		*/
     if ('en' !== $locale) {
         // .org mo files are named slightly different from .com, and all we have is this the locale -- try to guess them.
         $new_locale = $locale;
         if (strpos($locale, '-') !== false) {
             $pieces = explode('-', $locale);
             $new_locale = $locale_pieces[0];
             $new_locale .= !empty($locale_pieces[1]) ? '_' . strtoupper($locale_pieces[1]) : '';
         } else {
             // .com might pass 'fr' because thats what our language files are named as, where core seems
             // to do fr_FR - so try that if we don't think we can load the file.
             if (!file_exists(WP_LANG_DIR . '/' . $locale . '.mo')) {
                 $new_locale = $locale . '_' . strtoupper($locale);
             }
         }
         if (file_exists(WP_LANG_DIR . '/' . $new_locale . '.mo')) {
             unload_textdomain('default');
             load_textdomain('default', WP_LANG_DIR . '/' . $new_locale . '.mo');
         }
     }
     $old_user = wp_get_current_user();
     wp_set_current_user($user_id);
     $token = Jetpack_Data::get_access_token(get_current_user_id());
     if (!$token || is_wp_error($token)) {
         return false;
     }
     define('REST_API_REQUEST', true);
     define('WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1');
     // needed?
     require_once ABSPATH . 'wp-admin/includes/admin.php';
     require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
     $api = WPCOM_JSON_API::init($method, $url, $post_body);
     $api->token_details['user'] = $user_details;
     require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
     $display_errors = ini_set('display_errors', 0);
     ob_start();
     $content_type = $api->serve(false);
     $output = ob_get_clean();
     ini_set('display_errors', $display_errors);
     $nonce = wp_generate_password(10, false);
     $hmac = hash_hmac('md5', $nonce . $output, $token->secret);
     wp_set_current_user(isset($old_user->ID) ? $old_user->ID : 0);
     return array((string) $output, (string) $nonce, (string) $hmac);
 }