Skip to content

Commit

Permalink
wpcom Features: add new Holiday Snow feature (#40478)
Browse files Browse the repository at this point in the history
This commit brings a new feature to the mu-wpcom package:

- That feature can be toggled via Settings > General in wp-admin.
- It is active from December 1st to January 4th by default.
  • Loading branch information
jeherve authored Dec 6, 2024
1 parent 6da315e commit d416736
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/packages/jetpack-mu-wpcom/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/features/newspack-blocks/synced-newspack-blocks
src/features/holiday-snow/snowstorm.js
4 changes: 4 additions & 0 deletions projects/packages/jetpack-mu-wpcom/changelog/add-holiday-snow
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

WordPress.com Features: add Holiday Snow functionality.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public static function load_features() {
require_once __DIR__ . '/features/first-posts-stream/first-posts-stream-helpers.php';
require_once __DIR__ . '/features/font-smoothing-antialiased/font-smoothing-antialiased.php';
require_once __DIR__ . '/features/google-analytics/google-analytics.php';
require_once __DIR__ . '/features/holiday-snow/class-holiday-snow.php';
require_once __DIR__ . '/features/import-customizations/import-customizations.php';
require_once __DIR__ . '/features/marketplace-products-updater/class-marketplace-products-updater.php';
require_once __DIR__ . '/features/media/heif-support.php';
Expand All @@ -118,6 +119,8 @@ public static function load_features() {
\Automattic\Jetpack\Classic_Theme_Helper\Main::init();
\Automattic\Jetpack\Classic_Theme_Helper\Featured_Content::setup();

\Automattic\Jetpack\Jetpack_Mu_Wpcom\Holiday_Snow::init();

// Gets autoloaded from the Scheduled_Updates package.
if ( class_exists( 'Automattic\Jetpack\Scheduled_Updates' ) ) {
Scheduled_Updates::init();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<?php
/**
* Holiday Snow
* Adds falling snow to a blog starting December 1 and ending January 3.
*
* @since $$next-version$$
*
* @package automattic/jetpack-mu-wpcom
*/

namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;

/**
* Holiday Snow (admin and frontend).
*/
class Holiday_Snow {
/**
* Option to decide if Holiday snow is enabled on the site.
*/
private const HOLIDAY_SNOW_OPTION_NAME = 'jetpack_holiday_snow_enabled';

/**
* Check if it is the holiday snow season.
* The season starts on December 1 and ends on January 4 by default.
*
* @return bool
*/
public static function is_snow_season() {
$is_snow_season = false;
$today = time();
$first_snow_day = mktime( 0, 0, 0, 12, 1 );
$last_snow_day = mktime( 0, 0, 0, 1, 4 );

if ( $today >= $first_snow_day || $today < $last_snow_day ) {
$is_snow_season = true;
}

/**
* Filter to check if it is the snow season.
* It allows to change the start and end dates of the season,
* for regions where the holiday season may be different.
*
* @since $$next-version$$
*
* @param bool $is_holiday_snow_season Is it the snow season?
*/
return apply_filters( 'jetpack_is_holiday_snow_season', $is_snow_season );
}

/**
* Check if the snow is enabled.
*
* @return bool
*/
public static function is_snow_enabled() {
return (bool) get_option( self::HOLIDAY_SNOW_OPTION_NAME );
}

/**
* Register the hooks.
*
* @return void
*/
public static function init() {
if ( ! self::is_snow_season() ) {
return;
}

add_filter( 'site_settings_endpoint_get', array( __CLASS__, 'add_option_api' ) );
add_filter( 'rest_api_update_site_settings', array( __CLASS__, 'update_option_api' ), 10, 2 );
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
add_action( 'update_option_' . self::HOLIDAY_SNOW_OPTION_NAME, array( __CLASS__, 'holiday_snow_option_updated' ) );

if ( self::is_snow_enabled() ) {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'holiday_snow_script' ) );
}
}

/**
* Enqueue the snowstorm script on the frontend.
*
* @return void
*/
public static function holiday_snow_script() {
if (
/**
* Allow short-circuiting the snow, even when enabled on the site in settings.
*
* @since $$next-version$$
*
* @param bool true Whether to show the snow.
*/
! apply_filters( 'jetpack_holiday_chance_of_snow', true )
) {
return;
}

/**
* Fires when the snow is falling.
*
* @since $$next-version$$
*/
do_action( 'jetpack_stats_extra', 'holiday_snow', 'snowing' );

/**
* Filter the URL of the snowstorm script.
*
* @since $$next-version$$
*
* @param string $snowstorm_url URL of the snowstorm script.
*/
$snowstorm_url = apply_filters(
'jetpack_holiday_snow_js_url',
plugins_url( 'snowstorm.js', __FILE__ )
);

wp_enqueue_script(
'snowstorm',
$snowstorm_url,
array(),
\Automattic\Jetpack\Jetpack_Mu_Wpcom::PACKAGE_VERSION,
true
);
}

/**
* Add the option to the v1 API site settings endpoint.
*
* @param array $settings A single site's settings.
* @return array
*/
public static function add_option_api( $settings ) {
$settings[ self::HOLIDAY_SNOW_OPTION_NAME ] = self::is_snow_enabled();

return $settings;
}

/**
* Update settings via public-api.wordpress.com.
*
* @param array $input Associative array of site settings to be updated.
* Cast and filtered based on documentation.
* @param array $unfiltered_input Associative array of site settings to be updated.
* Neither cast nor filtered. Contains raw input.
* @return array
*/
public static function update_option_api( $input, $unfiltered_input ) {
if ( isset( $unfiltered_input[ self::HOLIDAY_SNOW_OPTION_NAME ] ) ) {
$input[ self::HOLIDAY_SNOW_OPTION_NAME ] = (bool) $unfiltered_input[ self::HOLIDAY_SNOW_OPTION_NAME ];
}

return $input;
}

/**
* Registers the settings section and fields.
*
* @return void
*/
public static function register_settings() {
register_setting(
'general',
self::HOLIDAY_SNOW_OPTION_NAME,
array(
'type' => 'boolean',
'description' => esc_attr__( 'Show falling snow on my site', 'jetpack-mu-wpcom' ),
'show_in_rest' => true,
'default' => false,
'sanitize_callback' => function ( $value ) {
return (bool) $value;
},
)
);

add_settings_field(
self::HOLIDAY_SNOW_OPTION_NAME,
esc_attr__( 'Snow', 'jetpack-mu-wpcom' ),
array( __CLASS__, 'option_field_display' ),
'general',
'default',
array(
'label_for' => self::HOLIDAY_SNOW_OPTION_NAME,
)
);
}

/**
* Renders the Snow settings markup.
*
* @return void
*/
public static function option_field_display() {
printf(
'<input type="checkbox" name="%1$s" id="%1$s" %2$s/><label for="%1$s">%3$s</label>',
esc_attr( self::HOLIDAY_SNOW_OPTION_NAME ),
checked( self::is_snow_enabled(), true, false ),
wp_kses(
__( 'Show falling snow on my site until January 4<sup>th</sup>.', 'jetpack-mu-wpcom' ),
array(
'sup' => array(),
)
)
);
}

/**
* Fires whenever the holiday snow option is updated.
* Used to gather stats about modified options.
*
* @return void
*/
public static function holiday_snow_option_updated() {
/** This action is already documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'holiday_snow', 'toggle' );
}
}

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

4 changes: 4 additions & 0 deletions projects/packages/sync/changelog/add-holiday-snow
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

WordPress.com Features: add Holiday Snow functionality.
1 change: 1 addition & 0 deletions projects/packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Defaults {
'jetpack_comment_form_color_scheme',
'jetpack_comment_likes_enabled',
'jetpack_excluded_extensions',
'jetpack_holiday_snow_enabled',
'jetpack_mailchimp',
'jetpack_options',
'jetpack_portfolio',
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/add-holiday-snow
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Sync: synchronize Holiday snow option.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public function test_sync_default_options() {
'carousel_background_color' => 'pineapple',
'carousel_display_exif' => 'pineapple',
'carousel_display_comments' => 'pineapple',
'jetpack_holiday_snow_enabled' => false,
'jetpack_portfolio' => 'pineapple',
'jetpack_portfolio_posts_per_page' => 'pineapple',
'jetpack_testimonial' => 'pineapple',
Expand Down

0 comments on commit d416736

Please sign in to comment.