-
Notifications
You must be signed in to change notification settings - Fork 125
Add Activity Library — activity_kit post type, archive, single pages, stats dashboard #3496
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
Open
Piyopiyo-Kitsune
wants to merge
18
commits into
trunk
Choose a base branch
from
feature/activity-library
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
62e74e9
Add Activity Library feature for learn.wordpress.org
Piyopiyo-Kitsune 3b2f17b
Apply prettier formatting to existing plugin JS files
Piyopiyo-Kitsune 7974c72
Refine Activity Library: UX polish, stats dashboard, feedback strip, …
Piyopiyo-Kitsune 9d0422e
Fix JS lint failures: root ESLint scope, import order, identifier names
Piyopiyo-Kitsune 84f131d
Add "Last updated" date to single activity kit header
Piyopiyo-Kitsune 170e2be
Update "Read the handbook" link to activity kits handbook page
Piyopiyo-Kitsune 0c4a6c0
Address Copilot code review: fix bugs, remove dead code, harden CI lint
Piyopiyo-Kitsune 25d9232
Fix PluginDocumentSettingPanel import: use @wordpress/edit-post
Piyopiyo-Kitsune 1463689
Fix import command: seed kits as drafts, not published
Piyopiyo-Kitsune c63a0cc
Fix PHPCS docblock: align @param spacing in activity_kit_query_vars
Piyopiyo-Kitsune 088d209
Fix activity library review issues
dd32 c627518
Remove redundant activity library guards
dd32 7d4a2be
Use unknown author for imported activity kits
dd32 1e2ee14
Remove plugin-local lint config
dd32 fb49fa8
Migrate activity kit tracking to Jetpack Stats
Piyopiyo-Kitsune 6bc2a98
Address Copilot review: build pipeline, tracking attrs, lint fixes
Piyopiyo-Kitsune f0169c7
Address second Copilot review: sanitization, colors, duration display
Piyopiyo-Kitsune 03a481a
Fix locale-notice.js object-shorthand lint error
Piyopiyo-Kitsune File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "useTabs": true, | ||
| "tabWidth": 4, | ||
| "printWidth": 120 | ||
| } |
138 changes: 138 additions & 0 deletions
138
wp-content/plugins/wporg-learn/inc/activity-kit-import.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| <?php | ||
|
|
||
| namespace WPOrg_Learn\Activity_Kit_Import; | ||
|
|
||
| defined( 'WPINC' ) || die(); | ||
|
|
||
| if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { | ||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * WP-CLI commands for Activity Kit management. | ||
| */ | ||
| class Activity_Kit_CLI { | ||
|
|
||
| /** | ||
| * Import the initial 11 Activity Kit posts. | ||
| * | ||
| * ## OPTIONS | ||
| * | ||
| * [--force] | ||
| * : Re-import even if posts already exist. | ||
| * | ||
| * ## EXAMPLES | ||
| * | ||
| * wp activity-kit import | ||
| * | ||
| * @when after_wp_load | ||
| */ | ||
| public function import( $args, $assoc_args ) { | ||
| $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false ); | ||
|
|
||
| $kits = array( | ||
| array( | ||
| 'title' => 'Debugging for Developers', | ||
| 'topics' => array( 'development' ), | ||
| 'levels' => array( 'Intermediate' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Debugging for Site Owners', | ||
| 'topics' => array( 'site-management' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'eCommerce with WooCommerce', | ||
| 'topics' => array( 'woocommerce', 'ecommerce' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'SEO Foundations', | ||
| 'topics' => array( 'seo' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'WordPress Playground', | ||
| 'topics' => array( 'playground' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Content Creation', | ||
| 'topics' => array( 'content-creation' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Using AI in your WordPress Dashboard', | ||
| 'topics' => array( 'ai', 'site-management' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Managing your WordPress site with AI', | ||
| 'topics' => array( 'ai', 'site-management' ), | ||
| 'levels' => array( 'Intermediate' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Contributor Onboarding', | ||
| 'topics' => array( 'contributing' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'WordPress Security Essentials', | ||
| 'topics' => array( 'security' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| array( | ||
| 'title' => 'Accessibility Testing in WordPress', | ||
| 'topics' => array( 'accessibility' ), | ||
| 'levels' => array( 'Beginner' ), | ||
| ), | ||
| ); | ||
|
|
||
| foreach ( $kits as $kit_data ) { | ||
| $existing_query = new \WP_Query( | ||
| array( | ||
| 'post_type' => 'activity_kit', | ||
| 'post_status' => 'any', | ||
| 'title' => $kit_data['title'], | ||
| 'posts_per_page' => 1, | ||
| 'fields' => 'ids', | ||
| ) | ||
| ); | ||
| $existing = $existing_query->have_posts() ? get_post( $existing_query->posts[0] ) : null; | ||
|
|
||
| if ( $existing && ! $force ) { | ||
| \WP_CLI::log( sprintf( 'Skipping "%s" — already exists (ID %d). Use --force to re-import.', $kit_data['title'], $existing->ID ) ); | ||
| continue; | ||
| } | ||
|
|
||
| $post_id = wp_insert_post( | ||
| array( | ||
| 'post_title' => $kit_data['title'], | ||
| 'post_type' => 'activity_kit', | ||
| 'post_status' => 'draft', | ||
| 'post_author' => 0, | ||
| ), | ||
| true | ||
| ); | ||
|
|
||
| if ( is_wp_error( $post_id ) ) { | ||
| \WP_CLI::warning( sprintf( 'Failed to create "%s": %s', $kit_data['title'], $post_id->get_error_message() ) ); | ||
| continue; | ||
| } | ||
|
|
||
| if ( ! empty( $kit_data['topics'] ) ) { | ||
| wp_set_object_terms( $post_id, $kit_data['topics'], 'topic' ); | ||
| } | ||
|
|
||
| if ( ! empty( $kit_data['levels'] ) ) { | ||
| wp_set_object_terms( $post_id, $kit_data['levels'], 'level' ); | ||
| } | ||
|
|
||
| \WP_CLI::success( sprintf( 'Created "%s" (ID %d)', $kit_data['title'], $post_id ) ); | ||
| } | ||
|
|
||
| \WP_CLI::log( 'Import complete.' ); | ||
| } | ||
| } | ||
|
|
||
| \WP_CLI::add_command( 'activity-kit', __NAMESPACE__ . '\Activity_Kit_CLI' ); | ||
232 changes: 232 additions & 0 deletions
232
wp-content/plugins/wporg-learn/inc/activity-kit-rest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| <?php | ||
| /** | ||
| * REST API routes for activity kits: stats endpoint backed by Jetpack Stats. | ||
| * | ||
| * @package WPOrg_Learn | ||
| */ | ||
|
|
||
| namespace WPOrg_Learn\Activity_Kit_REST; | ||
|
|
||
| defined( 'WPINC' ) || die(); | ||
|
|
||
| /** | ||
| * Actions and filters. | ||
| */ | ||
| add_action( 'rest_api_init', __NAMESPACE__ . '\register_routes' ); | ||
|
|
||
| /** | ||
| * Register REST API routes for activity kits. | ||
| */ | ||
| function register_routes() { | ||
| register_rest_route( | ||
| 'activity-kits/v1', | ||
| '/stats', | ||
| array( | ||
| 'methods' => 'GET', | ||
| 'callback' => __NAMESPACE__ . '\handle_stats', | ||
| 'permission_callback' => function () { | ||
| return current_user_can( 'manage_options' ); | ||
| }, | ||
| 'args' => array( | ||
| 'metric' => array( | ||
| 'default' => 'both', | ||
| 'enum' => array( 'both', 'views', 'downloads' ), | ||
| ), | ||
| 'range' => array( | ||
| 'default' => 'all', | ||
| 'enum' => array( '7d', '30d', '90d', 'all' ), | ||
| ), | ||
| 'kit' => array( | ||
| 'default' => '', | ||
| 'type' => 'string', | ||
| 'sanitize_callback' => 'sanitize_text_field', | ||
| ), | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Handle GET /activity-kits/v1/stats | ||
| * | ||
| * @param \WP_REST_Request $request The REST request. | ||
| * @return \WP_REST_Response | ||
| */ | ||
| function handle_stats( $request ) { | ||
| $metric = $request->get_param( 'metric' ); | ||
| $range = $request->get_param( 'range' ); | ||
| $kit = $request->get_param( 'kit' ); | ||
|
|
||
| $kits = get_posts( | ||
| array( | ||
| 'post_type' => 'activity_kit', | ||
| 'posts_per_page' => -1, | ||
| 'post_status' => 'publish', | ||
| 'orderby' => 'title', | ||
| 'order' => 'ASC', | ||
| ) | ||
| ); | ||
|
|
||
| $jetpack_unavailable = ! class_exists( '\Automattic\Jetpack\Stats\WPCOM_Stats' ); | ||
|
|
||
| // Build ZIP URL => post ID map for download click matching. | ||
| $zip_url_map = array(); | ||
| foreach ( $kits as $kit_post ) { | ||
| $zip_id = (int) get_post_meta( $kit_post->ID, '_activity_zip_id', true ); | ||
| if ( $zip_id ) { | ||
| $zip_url = wp_get_attachment_url( $zip_id ); | ||
| if ( $zip_url ) { | ||
| $zip_url_map[ $zip_url ] = $kit_post->ID; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $views_map = array(); | ||
| $downloads_map = array(); | ||
|
|
||
| if ( ! $jetpack_unavailable ) { | ||
| if ( 'both' === $metric || 'views' === $metric ) { | ||
| $views_map = get_jetpack_post_views( $range ); | ||
| } | ||
| if ( 'both' === $metric || 'downloads' === $metric ) { | ||
| $downloads_map = get_jetpack_download_clicks( $range, $zip_url_map ); | ||
| } | ||
| } | ||
|
|
||
| $results = array(); | ||
|
|
||
| foreach ( $kits as $kit_post ) { | ||
| if ( $kit && $kit_post->post_name !== $kit ) { | ||
| continue; | ||
| } | ||
|
|
||
| $data = array( | ||
| 'id' => $kit_post->ID, | ||
| 'title' => $kit_post->post_title, | ||
| 'slug' => $kit_post->post_name, | ||
| 'updated' => get_the_modified_date( 'Y-m-d', $kit_post->ID ), | ||
| ); | ||
|
|
||
| if ( $jetpack_unavailable ) { | ||
| $data['jetpack_unavailable'] = true; | ||
| $data['views'] = 0; | ||
| $data['downloads'] = 0; | ||
| } else { | ||
| if ( 'both' === $metric || 'views' === $metric ) { | ||
| $data['views'] = $views_map[ $kit_post->ID ] ?? 0; | ||
| } | ||
| if ( 'both' === $metric || 'downloads' === $metric ) { | ||
| $data['downloads'] = $downloads_map[ $kit_post->ID ] ?? 0; | ||
| } | ||
| } | ||
|
|
||
| $results[] = $data; | ||
| } | ||
|
|
||
| return rest_ensure_response( $results ); | ||
| } | ||
|
|
||
| /** | ||
| * Get per-post view counts from Jetpack Stats for a given time range. | ||
| * | ||
| * @param string $range One of '7d', '30d', '90d', 'all'. | ||
| * @return array Map of post_id (int) => view_count (int). Empty on failure. | ||
| */ | ||
| function get_jetpack_post_views( $range ) { | ||
| if ( ! class_exists( '\Automattic\Jetpack\Stats\WPCOM_Stats' ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $stats = new \Automattic\Jetpack\Stats\WPCOM_Stats(); | ||
|
|
||
| if ( 'all' === $range ) { | ||
| $period = 'month'; | ||
| $num = 36; | ||
| } else { | ||
| $period = 'day'; | ||
| $num = intval( str_replace( 'd', '', $range ) ); | ||
| } | ||
|
|
||
| $result = $stats->get_top_posts( | ||
| array( | ||
| 'period' => $period, | ||
| 'num' => $num, | ||
| 'date' => gmdate( 'Y-m-d' ), | ||
| 'summarize' => true, | ||
| 'max' => 1000, | ||
| ) | ||
| ); | ||
|
|
||
| if ( is_wp_error( $result ) || ! is_array( $result ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $top_posts = isset( $result['summary']['top-posts'] ) ? $result['summary']['top-posts'] : array(); | ||
| if ( ! is_array( $top_posts ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $map = array(); | ||
| foreach ( $top_posts as $post_data ) { | ||
| if ( isset( $post_data['id'], $post_data['views'] ) ) { | ||
| $map[ (int) $post_data['id'] ] = (int) $post_data['views']; | ||
| } | ||
| } | ||
|
|
||
| return $map; | ||
| } | ||
|
|
||
| /** | ||
| * Get per-kit download click counts from Jetpack Clicks report. | ||
| * | ||
| * @param string $range One of '7d', '30d', '90d', 'all'. | ||
| * @param array $zip_url_map Map of zip_url (string) => post_id (int). | ||
| * @return array Map of post_id (int) => click_count (int). Empty on failure. | ||
| */ | ||
| function get_jetpack_download_clicks( $range, $zip_url_map ) { | ||
| if ( ! class_exists( '\Automattic\Jetpack\Stats\WPCOM_Stats' ) || empty( $zip_url_map ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $stats = new \Automattic\Jetpack\Stats\WPCOM_Stats(); | ||
|
|
||
| if ( 'all' === $range ) { | ||
| $period = 'month'; | ||
| $num = 36; | ||
| } else { | ||
| $period = 'day'; | ||
| $num = intval( str_replace( 'd', '', $range ) ); | ||
| } | ||
|
|
||
| $result = $stats->get_clicks( | ||
| array( | ||
| 'period' => $period, | ||
| 'num' => $num, | ||
| 'date' => gmdate( 'Y-m-d' ), | ||
| 'summarize' => true, | ||
| 'max' => 1000, | ||
| ) | ||
| ); | ||
|
|
||
| if ( is_wp_error( $result ) || ! is_array( $result ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $clicks = isset( $result['summary']['clicks'] ) ? $result['summary']['clicks'] : array(); | ||
| if ( ! is_array( $clicks ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $map = array(); | ||
| foreach ( $clicks as $click ) { | ||
| if ( ! isset( $click['url'], $click['views'] ) ) { | ||
| continue; | ||
| } | ||
| if ( isset( $zip_url_map[ $click['url'] ] ) ) { | ||
| $post_id = $zip_url_map[ $click['url'] ]; | ||
| $map[ $post_id ] = ( isset( $map[ $post_id ] ) ? $map[ $post_id ] : 0 ) + (int) $click['views']; | ||
| } | ||
| } | ||
|
|
||
| return $map; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.