get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5 ), null, 'wpcom' ); } /** * Get site plan usage. * * @param WP_REST_Request $req The request object. * * @return array */ public function get_site_plan_usage( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/jetpack-stats/usage?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5 ), null, 'wpcom' ); } /** * Whether site has never published post. * * @param WP_REST_Request $req The request object. * @return array */ public function site_has_never_published_post( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/site-has-never-published-post?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v2', array( 'timeout' => 5 ), null, 'wpcom' ); } /** * Get detailed WordAds earnings information for the site. * * @param WP_REST_Request $req The request object. * @return array */ public function get_wordads_earnings( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/wordads/earnings?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); } /** * Get WordAds stats for the site. * * @param WP_REST_Request $req The request object. * @return array */ public function get_wordads_stats( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/wordads/stats?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); } /** * Get Email stats as a list. * * @param WP_REST_Request $req The request object. * @return array */ public function get_email_stats_list( $req ) { switch ( $req->get_param( 'resource' ) ) { case 'summary': return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/emails/%s?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'resource' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); default: return $this->get_forbidden_error(); } } /** * Get Email opens stats for a single post. * * @param WP_REST_Request $req The request object. * @return array */ public function get_email_opens_stats_single( $req ) { switch ( $req->get_param( 'resource' ) ) { case 'client': case 'device': case 'country': case 'rate': return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/opens/emails/%d/%s?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'post_id' ), $req->get_param( 'resource' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); default: return $this->get_forbidden_error(); } } /** * Get Email clicks stats for a single post. * * @param WP_REST_Request $req The request object. * @return array */ public function get_email_clicks_stats_single( $req ) { switch ( $req->get_param( 'resource' ) ) { case 'client': case 'device': case 'country': case 'rate': case 'link': case 'user-content-link': return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/clicks/emails/%d/%s?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'post_id' ), $req->get_param( 'resource' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); default: return $this->get_forbidden_error(); } } /** * Get Email stats time series. * * @param WP_REST_Request $req The request object. * @return array */ public function get_email_stats_time_series( $req ) { switch ( $req->get_param( 'resource' ) ) { case 'opens': case 'clicks': return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/%s/emails/%d?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'resource' ), $req->get_param( 'post_id' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 5 ) ); default: return $this->get_forbidden_error(); } } /** * Get UTM stats time series. * * @param WP_REST_Request $req The request object. * @return array */ public function get_utm_stats_time_series( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/utm/%s?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'utm_params' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 10 ) ); } /** * Get Devices stats time series. * * @param WP_REST_Request $req The request object. * @return array */ public function get_devices_stats_time_series( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/stats/devices/%s?%s', Jetpack_Options::get_option( 'id' ), $req->get_param( 'device_property' ), $this->filter_and_build_query_string( $req->get_params() ) ), 'v1.1', array( 'timeout' => 10 ) ); } /** * Dismiss or delay stats notices. * * @param WP_REST_Request $req The request object. * @return array */ public function update_notice_status( $req ) { return ( new Notices() )->update_notice( $req->get_param( 'id' ), $req->get_param( 'status' ), $req->get_param( 'postponed_for' ) ); } /** * Get stats notices. * * @return array */ public function get_notice_status() { return ( new Notices() )->get_notices_to_show(); } /** * Mark a referrer as spam. * * @param WP_REST_Request $req The request object. * @return array */ public function mark_referrer_spam( $req ) { return WPCOM_Client::request_as_blog( sprintf( '/sites/%d/stats/referrers/spam/new?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v1.1', array( 'timeout' => 5, 'method' => 'POST', ) ); } /** * Unmark a referrer as spam. * * @param WP_REST_Request $req The request object. * @return array */ public function unmark_referrer_spam( $req ) { return WPCOM_Client::request_as_blog( sprintf( '/sites/%d/stats/referrers/spam/delete?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v1.1', array( 'timeout' => 5, 'method' => 'POST', ) ); } /** * Toggle modules on dashboard. * * @param WP_REST_Request $req The request object. * @return array */ public function update_dashboard_modules( $req ) { // Clear dashboard modules cache. delete_transient( static::JETPACK_STATS_DASHBOARD_MODULES_CACHE_KEY ); return WPCOM_Client::request_as_blog( sprintf( '/sites/%d/jetpack-stats-dashboard/modules?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5, 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/json' ), ), $req->get_body(), 'wpcom' ); } /** * Get modules on dashboard. * * @param WP_REST_Request $req The request object. * @return array */ public function get_dashboard_modules( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/jetpack-stats-dashboard/modules?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5, ), null, 'wpcom', true, static::JETPACK_STATS_DASHBOARD_MODULES_CACHE_KEY ); } /** * Update module settings on dashboard. * * @param WP_REST_Request $req The request object. * @return array */ public function update_dashboard_module_settings( $req ) { // Clear dashboard modules cache. delete_transient( static::JETPACK_STATS_DASHBOARD_MODULE_SETTINGS_CACHE_KEY ); return WPCOM_Client::request_as_blog( sprintf( '/sites/%d/jetpack-stats-dashboard/module-settings?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5, 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/json' ), ), $req->get_body(), 'wpcom' ); } /** * Get module settings on dashboard. * * @param WP_REST_Request $req The request object. * @return array */ public function get_dashboard_module_settings( $req ) { return WPCOM_Client::request_as_blog_cached( sprintf( '/sites/%d/jetpack-stats-dashboard/module-settings?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5, ), null, 'wpcom', true, static::JETPACK_STATS_DASHBOARD_MODULE_SETTINGS_CACHE_KEY ); } /** * Run commercial classification. * * @param WP_REST_Request $req The request object. * @return array */ public function run_commercial_classification( $req ) { return WPCOM_Client::request_as_blog( sprintf( '/sites/%d/commercial-classification?%s', Jetpack_Options::get_option( 'id' ), $this->filter_and_build_query_string( $req->get_query_params() ) ), 'v2', array( 'timeout' => 5, 'method' => 'POST', ), null, 'wpcom' ); } /** * Return a WP_Error object with a forbidden error. */ protected function get_forbidden_error() { $error_msg = esc_html__( 'You are not allowed to perform this action.', 'jetpack-stats-admin' ); return new WP_Error( 'rest_forbidden', $error_msg, array( 'status' => rest_authorization_required_code() ) ); } /** * Filter and build query string from all the requested params. * * @param array $params The params to filter. * @param array $keys_to_unset The keys to unset from the params array. * @return string The filtered and built query string. */ protected function filter_and_build_query_string( $params, $keys_to_unset = array() ) { if ( isset( $params['rest_route'] ) ) { unset( $params['rest_route'] ); } if ( ! empty( $keys_to_unset ) && is_array( $keys_to_unset ) ) { foreach ( $keys_to_unset as $key ) { if ( isset( $params[ $key ] ) ) { unset( $params[ $key ] ); } } } return http_build_query( $params ); } }