| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fediverse Stats Post starter pattern. |
| 4 |
* |
| 5 |
* This pattern is only registered in December and January, giving users |
| 6 |
* a seasonal nudge to share their annual Fediverse stats. |
| 7 |
* |
| 8 |
* @package Activitypub |
| 9 |
*/ |
| 10 |
|
| 11 |
$selected_user = ! \Activitypub\is_user_type_disabled( 'blog' ) ? 'blog' : 'inherit'; |
| 12 |
|
| 13 |
/* |
| 14 |
* Skip if a post with the stats block was already published |
| 15 |
* during the current December–January window. |
| 16 |
* |
| 17 |
* The month is re-read here (already checked in class-blocks.php) |
| 18 |
* because we need to distinguish December from January to build |
| 19 |
* the correct date range. |
| 20 |
*/ |
| 21 |
$month = (int) \gmdate( 'n' ); |
| 22 |
$current_year = (int) \gmdate( 'Y' ); |
| 23 |
$stats_year = 12 === $month ? $current_year : ( $current_year - 1 ); |
| 24 |
$transient = 'activitypub_stats_pattern_' . $stats_year; |
| 25 |
$cached = \get_transient( $transient ); |
| 26 |
|
| 27 |
if ( 'hide' === $cached ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if ( false === $cached ) { |
| 32 |
$after = $stats_year . '-12-01'; |
| 33 |
|
| 34 |
global $wpdb; |
| 35 |
|
| 36 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 37 |
$has_stats_post = $wpdb->get_var( |
| 38 |
$wpdb->prepare( |
| 39 |
"SELECT 1 FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date_gmt >= %s AND post_content LIKE %s LIMIT 1", |
| 40 |
$after, |
| 41 |
'%<!-- wp:activitypub/stats%' |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
if ( $has_stats_post ) { |
| 46 |
\set_transient( $transient, 'hide', MONTH_IN_SECONDS ); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
\set_transient( $transient, 'show', DAY_IN_SECONDS ); |
| 51 |
} |
| 52 |
|
| 53 |
\register_block_pattern( |
| 54 |
'activitypub/stats-post', |
| 55 |
array( |
| 56 |
'title' => _x( 'Fediverse Year in Review', 'Block pattern title', 'activitypub' ), |
| 57 |
'categories' => array( 'activitypub' ), |
| 58 |
'keywords' => array( |
| 59 |
_x( 'stats', 'Block pattern keyword', 'activitypub' ), |
| 60 |
_x( 'fediverse', 'Block pattern keyword', 'activitypub' ), |
| 61 |
_x( 'year in review', 'Block pattern keyword', 'activitypub' ), |
| 62 |
), |
| 63 |
'description' => _x( 'Share your annual Fediverse stats as a post.', 'Block pattern description', 'activitypub' ), |
| 64 |
'viewportWidth' => 1200, |
| 65 |
'postTypes' => array( 'post' ), |
| 66 |
'blockTypes' => array( 'core/post-content' ), |
| 67 |
'content' => '<!-- wp:activitypub/stats {"selectedUser":"' . $selected_user . '","year":' . $stats_year . '} /-->', |
| 68 |
) |
| 69 |
); |
| 70 |
|