Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats: enable ads page for Odyssey #27791

Merged
merged 12 commits into from
Jan 20, 2023
4 changes: 4 additions & 0 deletions projects/packages/stats-admin/changelog/fix-ads-page
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Stats: enable Ads page
5 changes: 3 additions & 2 deletions projects/packages/stats-admin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"require": {
"automattic/jetpack-constants": "@dev",
"automattic/jetpack-options": "@dev",
"automattic/jetpack-stats": "@dev"
"automattic/jetpack-stats": "@dev",
"automattic/jetpack-status": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.0.4",
Expand Down Expand Up @@ -48,7 +49,7 @@
"autotagger": true,
"mirror-repo": "Automattic/jetpack-stats-admin",
"branch-alias": {
"dev-trunk": "0.3.x-dev"
"dev-trunk": "0.4.x-dev"
},
"textdomain": "jetpack-stats-admin",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/stats-admin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-stats-admin",
"version": "0.3.1",
"version": "0.4.0-alpha",
"description": "Stats Dashboard",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/stats-admin/#readme",
"bugs": {
Expand Down
47 changes: 42 additions & 5 deletions projects/packages/stats-admin/src/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Automattic\Jetpack\Admin_UI\Admin_Menu;
use Automattic\Jetpack\Assets;
use Automattic\Jetpack\Modules;
use Jetpack_Options;

/**
Expand Down Expand Up @@ -223,27 +224,32 @@ protected function config_data() {
'features' => array(),
'intial_state' => array(
'currentUser' => array(
'id' => 1000,
'user' => array(
'id' => 1000,
'user' => array(
'ID' => 1000,
'username' => 'no-user',
),
'capabilities' => array(
"$blog_id" => self::get_current_user_capabilities(),
),
),
'sites' => array(
'items' => array(
'items' => array(
"$blog_id" => array(
'ID' => $blog_id,
'URL' => site_url(),
'jetpack' => true,
'visible' => true,
'capabilities' => $empty_object,
'products' => array(),
'plan' => $empty_object, // we need this empty object, otherwise the front end would crash on insight page.
'options' => array(
'wordads' => ( new Modules() )->is_active( 'wordads' ),
'admin_url' => admin_url(),
),
'products' => array(),
'plan' => $empty_object, // we need this empty object, otherwise the front end would crash on insight page.
),
),
'features' => array( "$blog_id" => array( 'data' => self::get_plan_features() ) ),
),
),
);
Expand Down Expand Up @@ -284,4 +290,35 @@ protected static function get_site_locale() {
return $locale;
}

/**
* Get the features of the current plan.
*/
protected function get_plan_features() {
if ( ! class_exists( 'Jetpack_Plan' ) ) {
return array();
}
$plan = \Jetpack_Plan::get();
if ( empty( $plan['features'] ) ) {
return array();
}
return $plan['features'];
}

/**
* Get the capabilities of the current user.
*
* @return array An array of capabilities.
*/
protected function get_current_user_capabilities() {
// Feature lock.
if ( ! isset( $_GET['flags'] ) || $_GET['flags'] !== 'stats/ads-page' ) {// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return array();
}
$user = wp_get_current_user();
if ( ! $user || is_wp_error( $user ) ) {
return array();
}
return $user->allcaps;
}

}
2 changes: 1 addition & 1 deletion projects/packages/stats-admin/src/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Main {
/**
* Stats version.
*/
const VERSION = '0.3.1';
const VERSION = '0.4.0-alpha';

/**
* Singleton Main instance.
Expand Down
90 changes: 82 additions & 8 deletions projects/packages/stats-admin/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_single_resource_stats' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -69,7 +69,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_stats_resource' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -80,7 +80,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_single_post' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -91,7 +91,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_single_post_likes' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -102,7 +102,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_site_stats' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -113,7 +113,7 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'site_has_never_published_post' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

Expand All @@ -124,7 +124,29 @@ public function register_rest_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_site_posts' ),
'permission_callback' => array( $this, 'check_user_privileges_callback' ),
'permission_callback' => array( $this, 'can_user_view_general_stats_callback' ),
)
);

// WordAds Earnings.
register_rest_route(
static::$namespace,
sprintf( '/sites/%d/wordads/earnings', Jetpack_Options::get_option( 'id' ) ),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_wordads_earnings' ),
'permission_callback' => array( $this, 'can_user_view_wordads_stats_callback' ),
)
);

// WordAds Stats.
register_rest_route(
static::$namespace,
sprintf( '/sites/%d/wordads/stats', Jetpack_Options::get_option( 'id' ) ),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_wordads_stats' ),
'permission_callback' => array( $this, 'can_user_view_wordads_stats_callback' ),
)
);
}
Expand All @@ -134,14 +156,26 @@ public function register_rest_routes() {
*
* @return bool|WP_Error True if a blog token was used to sign the request, WP_Error otherwise.
*/
public function check_user_privileges_callback() {
public function can_user_view_general_stats_callback() {
if ( current_user_can( 'manage_options' ) || current_user_can( 'view_stats' ) ) {
return true;
}

return $this->get_forbidden_error();
}

/**
* Only administrators or users with capability `activate_wordads` can access the API.
*/
public function can_user_view_wordads_stats_callback() {
// phpcs:ignore WordPress.WP.Capabilities.Unknown
if ( current_user_can( 'manage_options' ) || current_user_can( 'activate_wordads' ) ) {
return true;
}

return $this->get_forbidden_error();
}

/**
* Stats resource endpoint.
*
Expand Down Expand Up @@ -358,6 +392,46 @@ public function site_has_never_published_post( $req ) {
);
}

/**
* 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 $this->request_as_blog_cached(
sprintf(
'/sites/%d/wordads/earnings',
Jetpack_Options::get_option( 'id' ),
http_build_query(
$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 $this->request_as_blog_cached(
sprintf(
'/sites/%d/wordads/stats',
Jetpack_Options::get_option( 'id' ),
http_build_query(
$req->get_params()
)
),
'v1.1',
array( 'timeout' => 5 )
);
}

/**
* Query the WordPress.com REST API using the blog token
*
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/fix-ads-page
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


7 changes: 4 additions & 3 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.