本文整理汇总了PHP中Jetpack_Client::wpcom_json_api_request_as_blog方法的典型用法代码示例。如果您正苦于以下问题:PHP Jetpack_Client::wpcom_json_api_request_as_blog方法的具体用法?PHP Jetpack_Client::wpcom_json_api_request_as_blog怎么用?PHP Jetpack_Client::wpcom_json_api_request_as_blog使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jetpack_Client
的用法示例。
在下文中一共展示了Jetpack_Client::wpcom_json_api_request_as_blog方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: download_wpcom_theme_to_file
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;
}
示例2: wp_ajax_videopress_get_upload_token
/**
* 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);
}
示例3: stats_get_from_restapi
/**
* 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;
}
示例4: refresh_active_plan_from_wpcom
/**
* 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']);
}
示例5: get_site_data
/**
* 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));
}
示例6: save_fields
/**
* @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;
}
示例7: get_site_data
/**
* 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));
}