-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds functions for gathering stats about patterns and saving them to snapshot posts, similar to what is done on the 5ftf site. A cron job is set up to run at 00:00 UTC daily to save a snapshot. Also adds a simple, work-in-progress page in WP Admin to view the current stats and some basic info about the number of snapshots that have been created. Fixes #292
- Loading branch information
1 parent
05c024f
commit 8993803
Showing
6 changed files
with
671 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
175 changes: 175 additions & 0 deletions
175
public_html/wp-content/plugins/pattern-directory/includes/admin-stats.php
This file contains 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,175 @@ | ||
<?php | ||
|
||
namespace WordPressdotorg\Pattern_Directory\Admin\Stats; | ||
|
||
use function WordPressdotorg\Pattern_Directory\Stats\{ get_meta_field_schema, get_snapshot_data, get_snapshots }; | ||
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE as PATTERN_POST_TYPE; | ||
|
||
defined( 'WPINC' ) || die(); | ||
|
||
/** | ||
* Actions and filters. | ||
*/ | ||
add_action( 'admin_menu', __NAMESPACE__ . '\add_subpage' ); | ||
|
||
/** | ||
* Register an admin page under Block Patterns for viewing stats. | ||
* | ||
* @return void | ||
*/ | ||
function add_subpage() { | ||
$parent_slug = add_query_arg( 'post_type', PATTERN_POST_TYPE, 'edit.php' ); | ||
|
||
$post_type_object = get_post_type_object( PATTERN_POST_TYPE ); | ||
|
||
add_submenu_page( | ||
$parent_slug, | ||
__( 'Pattern Stats', 'wporg-patterns' ), | ||
__( 'Stats', 'wporg-patterns' ), | ||
$post_type_object->cap->edit_posts, | ||
PATTERN_POST_TYPE . '-stats', | ||
__NAMESPACE__ . '\render_subpage' | ||
); | ||
} | ||
|
||
/** | ||
* Render the stats subpage. | ||
* | ||
* @return void | ||
*/ | ||
function render_subpage() { | ||
$schema = get_meta_field_schema(); | ||
$data = get_snapshot_data(); | ||
$snapshot_query = get_snapshots( array(), true ); | ||
$snapshots = $snapshot_query->get_posts(); | ||
$next_snapshot = wp_get_scheduled_event( PATTERN_POST_TYPE . '_record_snapshot' ); | ||
|
||
?> | ||
<style> | ||
.number { | ||
font-weight: 700; | ||
text-align: right; | ||
} | ||
|
||
.widefat.but-not-too-wide { | ||
width: auto; | ||
} | ||
</style> | ||
<div class="wrap"> | ||
<h1 class="wp-heading-inline"> | ||
<?php esc_html_e( 'Pattern Stats', 'wporg-patterns' ); ?> | ||
</h1> | ||
|
||
<p> | ||
This page is a work in progress. Someday there might be charts! | ||
</p> | ||
|
||
<h2> | ||
Right now | ||
</h2> | ||
|
||
<table class="widefat but-not-too-wide striped"> | ||
<thead> | ||
<tr> | ||
<th> | ||
Meta Key | ||
</th> | ||
<td> | ||
Description | ||
</td> | ||
<td> | ||
Value | ||
</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<?php foreach ( $schema['properties'] as $field_name => $field_schema ) : ?> | ||
<tr> | ||
<th> | ||
<code><?php echo esc_html( $field_name ); ?></code> | ||
</th> | ||
<td> | ||
<?php echo esc_html( $field_schema['description'] ); ?> | ||
</td> | ||
<td class="<?php echo ( is_numeric( $data[ $field_name ] ) ) ? 'number' : ''; ?>"> | ||
<?php if ( isset( $data[ $field_name ] ) ) : ?> | ||
<?php if ( is_numeric( $data[ $field_name ] ) ) : ?> | ||
<?php echo esc_html( number_format_i18n( $data[ $field_name ] ) ); ?> | ||
<?php else : ?> | ||
<?php echo esc_html( $data[ $field_name ] ); ?> | ||
<?php endif; ?> | ||
<?php else : ?> | ||
Data missing. | ||
<?php endif; ?> | ||
</td> | ||
</tr> | ||
<?php endforeach; ?> | ||
</tbody> | ||
</table> | ||
|
||
<h2> | ||
Snapshots | ||
</h2> | ||
|
||
<p> | ||
Snapshot frequency should be daily at around 00:00 UTC. | ||
<strong> | ||
<?php if ( $next_snapshot ) : ?> | ||
<?php | ||
printf( | ||
'The next snapshot will be captured on %s.', | ||
esc_html( wp_date( 'r', $next_snapshot->timestamp ) ) | ||
); | ||
?> | ||
<?php else : ?> | ||
No snapshot is currently scheduled. | ||
<?php endif; ?> | ||
</strong> | ||
</p> | ||
|
||
<table class="widefat but-not-too-wide striped"> | ||
<tbody> | ||
<tr> | ||
<td> | ||
Number of snapshots | ||
</td> | ||
<td class="number"> | ||
<?php echo esc_html( number_format_i18n( $snapshot_query->found_posts ) ); ?> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
Earliest snapshot | ||
</td> | ||
<td> | ||
<?php if ( $snapshot_query->found_posts > 0 ) : ?> | ||
<?php echo esc_html( reset( $snapshots )->post_title ); ?> | ||
<?php else : ?> | ||
No data. | ||
<?php endif; ?> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td> | ||
Latest snapshot | ||
</td> | ||
<td> | ||
<?php if ( $snapshot_query->found_posts > 0 ) : ?> | ||
<?php echo esc_html( end( $snapshots )->post_title ); ?> | ||
<?php else : ?> | ||
No data. | ||
<?php endif; ?> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
|
||
<h2> | ||
Export | ||
</h2> | ||
<p> | ||
Coming soon! 🎉 | ||
</p> | ||
</div> | ||
<?php | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.