protected static function download_wpcom_theme_to_file($theme) { $wpcom_theme_slug = preg_replace('/-wpcom$/', '', $theme); $file = wp_tempnam('theme'); if (!$file) { return new WP_Error('problem_creating_theme_file', __('Problem creating file for theme download', 'jetpack')); } $url = "themes/download/{$theme}.zip"; $args = array('stream' => true, 'filename' => $file); $result = Jetpack_Client::wpcom_json_api_request_as_blog($url, '1.1', $args); $response = $result['response']; if ($response['code'] !== 200) { unlink($file); return new WP_Error('problem_fetching_theme', __('Problem downloading theme', 'jetpack')); } return $file; }
/** * Ajax method that is used by the VideoPress uploader to get a token to upload a file to the wpcom api. * * @return void */ public function wp_ajax_videopress_get_upload_token() { $options = VideoPress_Options::get_options(); $args = array('method' => 'POST'); $endpoint = "sites/{$options['shadow_blog_id']}/media/token"; $result = Jetpack_Client::wpcom_json_api_request_as_blog($endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args); if (is_wp_error($result)) { wp_send_json_error(array('message' => __('Could not obtain a VideoPress upload token. Please try again later.', 'jetpack'))); return; } $response = json_decode($result['body'], true); if (empty($response['upload_token'])) { wp_send_json_error(array('message' => __('Could not obtain a VideoPress upload token. Please try again later.', 'jetpack'))); return; } $title = sanitize_title(basename($_POST['filename'])); $response['upload_action_url'] = videopress_make_media_upload_path($options['shadow_blog_id']); $response['upload_media_id'] = videopress_create_new_media_item($title); wp_send_json_success($response); }
/** * Fetches stats data from the REST API. Caches locally for 5 minutes. * * @link: https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/stats/ * * @param array|string $args The args that are passed to the endpoint * @param string $resource Optional sub-endpoint following /stats/ * @return array|WP_Error */ function stats_get_from_restapi($args = array(), $resource = '') { $endpoint = jetpack_stats_api_path($resource); $api_version = '1.1'; $args = wp_parse_args($args, array()); $cache_key = md5(implode('|', array($endpoint, $api_version, serialize($args)))); // Get cache $stats_cache = Jetpack_Options::get_option('restapi_stats_cache', array()); if (!is_array($stats_cache)) { $stats_cache = array(); } // Return or expire this key if (isset($stats_cache[$cache_key])) { $time = key($stats_cache[$cache_key]); if (time() - $time < 5 * MINUTE_IN_SECONDS) { $cached_stats = $stats_cache[$cache_key][$time]; $cached_stats = (object) array_merge(array('cached_at' => $time), (array) $cached_stats); return $cached_stats; } unset($stats_cache[$cache_key]); } // Do the dirty work. $response = Jetpack_Client::wpcom_json_api_request_as_blog($endpoint, $api_version, $args); if (200 !== wp_remote_retrieve_response_code($response)) { // If bad, just return it, don't cache. return $response; } $data = json_decode(wp_remote_retrieve_body($response)); // Expire old keys foreach ($stats_cache as $k => $cache) { if (!is_array($cache) || 5 * MINUTE_IN_SECONDS < time() - key($cache)) { unset($stats_cache[$k]); } } // Set cache $stats_cache[$cache_key] = array(time() => $data); Jetpack_Options::update_option('restapi_stats_cache', $stats_cache, false); return $data; }
/** * Make an API call to WordPress.com for plan status * * @uses Jetpack_Options::get_option() * @uses Jetpack_Client::wpcom_json_api_request_as_blog() * @uses update_option() * * @access public * @static * * @return bool True if plan is updated, false if no update */ public static function refresh_active_plan_from_wpcom() { // Make the API request $request = sprintf('/sites/%d', Jetpack_Options::get_option('id')); $response = Jetpack_Client::wpcom_json_api_request_as_blog($request, '1.1'); // Bail if there was an error or malformed response if (is_wp_error($response) || !is_array($response) || !isset($response['body'])) { return false; } // Decode the results $results = json_decode($response['body'], true); // Bail if there were no results or plan details returned if (!is_array($results) || !isset($results['plan'])) { return false; } // Store the option and return true if updated return update_option('jetpack_active_plan', $results['plan']); }
/** * Get site data, including for example, the site's current plan. * * @since 4.3.0 * * @return array Array of Jetpack modules. */ public static function get_site_data() { if ($site_id = Jetpack_Options::get_option('id')) { $response = Jetpack_Client::wpcom_json_api_request_as_blog(sprintf('/sites/%d', $site_id), '1.1'); if (200 !== wp_remote_retrieve_response_code($response)) { return new WP_Error('site_data_fetch_failed', esc_html__('Failed fetching site data. Try again later.', 'jetpack'), array('status' => 400)); } return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('Site data correctly received.', 'jetpack'), 'data' => wp_remote_retrieve_body($response))); } return new WP_Error('site_id_missing', esc_html__('The ID of this site does not exist.', 'jetpack'), array('status' => 404)); }
/** * @param array $post * @param array|null $attachment * * @return array */ public function save_fields($post, $attachment = null) { if ($attachment === null && isset($_POST['attachment'])) { $attachment = $_POST['attachment']; } if (!isset($attachment['is_videopress_attachment']) || $attachment['is_videopress_attachment'] !== 'yes') { return $post; } $post_id = absint($post['ID']); $meta = wp_get_attachment_metadata($post_id); // If this has not been processed by videopress, we can skip the rest. if (!isset($meta['videopress'])) { return $post; } $values = array(); // Add the video title & description in, so that we save it properly. if (isset($_POST['post_title'])) { $values['title'] = trim(strip_tags($_POST['post_title'])); } if (isset($_POST['post_excerpt'])) { $values['description'] = trim(strip_tags($_POST['post_excerpt'])); } if (isset($attachment['rating'])) { $rating = $attachment['rating']; if (!empty($rating) && in_array($rating, array('G', 'PG-13', 'R-17', 'X-18'))) { $values['rating'] = $rating; } } // We set a default here, as if it isn't selected, then we'll turn it off. $values['display_embed'] = 0; if (isset($attachment['display_embed'])) { $display_embed = $attachment['display_embed']; $values['display_embed'] = 'on' === $display_embed ? 1 : 0; } $args = array('method' => 'POST'); $endpoint = "videos/{$meta['videopress']['guid']}"; $result = Jetpack_Client::wpcom_json_api_request_as_blog($endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args, $values); if (is_wp_error($result)) { $post['errors']['videopress']['errors'][] = __('There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack'); return $post; } if (isset($values['display_embed'])) { $meta['videopress']['display_embed'] = $values['display_embed']; } if (isset($values['rating'])) { $meta['videopress']['rating'] = $values['rating']; } wp_update_attachment_metadata($post_id, $meta); $response = json_decode($result['body'], true); if ('true' !== $response) { return $post; } return $post; }
/** * Get site data, including for example, the site's current plan. * * @since 4.3.0 * * @return array Array of Jetpack modules. */ public static function get_site_data() { if ($site_id = Jetpack_Options::get_option('id')) { $response = Jetpack_Client::wpcom_json_api_request_as_blog(sprintf('/sites/%d', $site_id), '1.1'); if (200 !== wp_remote_retrieve_response_code($response)) { return new WP_Error('site_data_fetch_failed', esc_html__('Failed fetching site data. Try again later.', 'jetpack'), array('status' => 400)); } // Save plan details in the database for future use without API calls $results = json_decode($response['body'], true); if (is_array($results) && isset($results['plan'])) { update_option('jetpack_active_plan', $results['plan']); } return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('Site data correctly received.', 'jetpack'), 'data' => wp_remote_retrieve_body($response))); } return new WP_Error('site_id_missing', esc_html__('The ID of this site does not exist.', 'jetpack'), array('status' => 404)); }