Completed
Push — fix/holiday-snow-option-visibi... ( c16fdf...db4054 )
by
unknown
20:28 queued 11:53
created

()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Holiday Snow
5
 * Adds falling snow to a blog starting December 1 and ending January 3.
6
 * Not a module that is activated/deactivated
7
 * First Introduced: 2.0.3 ??
8
 * Requires Connection: No
9
 * Auto Activate: Yes
10
 */
11
12
class Jetpack_Holiday_Snow_Settings {
13
	function __construct() {
14
		add_filter( 'admin_init' , array( &$this , 'register_fields' ) );
15
	}
16
17
	public function register_fields() {
18
		register_setting( 'general', jetpack_holiday_snow_option_name(), 'esc_attr' );
19
		add_settings_field( jetpack_holiday_snow_option_name(), '<label for="' . esc_attr( jetpack_holiday_snow_option_name() ) . '">' . __( 'Snow' , 'jetpack') . '</label>' , array( &$this, 'blog_field_html' ) , 'general' );
20
		add_action( 'update_option_' . jetpack_holiday_snow_option_name(), array( &$this, 'holiday_snow_option_updated' ) );
21
	}
22
23
	public function blog_field_html() {
24
		$id = esc_attr( jetpack_holiday_snow_option_name() );
25
		?>
26
			<label for="<?php echo $id; ?>">
27
				<input type="checkbox" name="<?php echo $id; ?>" id="<?php echo $id; ?>" value="letitsnow"<?php checked( get_option( jetpack_holiday_snow_option_name() ), 'letitsnow' ); ?> />
28
				<span><?php _e( 'Show falling snow on my blog until January 4<sup>th</sup>.' , 'jetpack'); ?></span>
29
			</label>
30
		<?php
31
	}
32
33
	public function holiday_snow_option_updated() {
34
35
		/**
36
		 * Fires when the holiday snow option is updated.
37
		 *
38
		 * @module theme-tools
39
		 *
40
		 * @since 2.0.3
41
		 */
42
		do_action( 'jetpack_holiday_snow_option_updated' );
43
	}
44
}
45
46
function jetpack_holiday_snow_script() {
47
48
	/**
49
	 * Allow holiday snow.
50
	 *
51
	 * Note: there's no actual randomness involved in whether it snows
52
	 * or not, despite the filter mentioning a "chance of snow."
53
	 *
54
	 * @module theme-tools
55
	 *
56
	 * @since 2.0.3
57
	 *
58
	 * @param bool True to allow snow, false to disable it.
59
	 */
60
	if ( ! apply_filters( 'jetpack_holiday_chance_of_snow', true ) )
61
		return;
62
63
	/**
64
	 * Fires when it's snowing.
65
	 *
66
	 * @module theme-tools
67
	 *
68
	 * @since 2.0.3
69
	 */
70
	do_action( 'jetpack_holiday_snowing' );
71
72
	/**
73
	 * Filter the holiday snow JavaScript URL.
74
	 *
75
	 * @module theme-tools
76
	 *
77
	 * @since 2.0.3
78
	 *
79
	 * @param str URL to the holiday snow JavaScript file.
80
	 */
81
	$snowstorm_url = apply_filters( 'jetpack_holiday_snow_js_url', plugins_url( 'holiday-snow/snowstorm.js', __FILE__ ) );
82
	wp_enqueue_script( 'snowstorm', $snowstorm_url, array(), '1.43.20111201', true );
83
}
84
85
function jetpack_maybe_holiday_snow() {
86
	if ( ! jetpack_is_holiday_snow_season() )
87
		return;
88
89
	if ( is_admin() ) {
90
		global $jetpack_holiday_snow;
91
		$jetpack_holiday_snow = new Jetpack_Holiday_Snow_Settings();
92
	} elseif ( get_option( jetpack_holiday_snow_option_name() ) ) {
93
		add_action( 'init', 'jetpack_holiday_snow_script' );
94
	}
95
}
96
97
function jetpack_holiday_snow_option_name() {
98
99
	/**
100
	 * Filter the holiday snow option name.
101
	 *
102
	 * @module theme-tools
103
	 *
104
	 * @since 2.0.3
105
	 *
106
	 * @param str The holiday snow option name.
107
	 */
108
	return apply_filters( 'jetpack_holiday_snow_option_name', 'jetpack_holiday_snow_enabled' );
109
}
110
111
function jetpack_is_holiday_snow_season() {
112
	$today          = time();
113
	$first_snow_day = mktime( 0, 0, 0, 12, 1 );
114
	$last_snow_day  = mktime( 0, 0, 0, 1, 4 );
115
116
	$snow = ( $today >= $first_snow_day || $today < $last_snow_day );
117
118
	/**
119
	 * Filter whether it's winter or not.
120
	 *
121
	 * You can use this filter if, for example, you live in the
122
	 * Southern Hemisphere. In that case, the dates for winter
123
	 * above are incorrect for your location.
124
	 *
125
	 * @module theme-tools
126
	 *
127
	 * @since 2.1.0
128
	 *
129
	 * @param bool $snow True if it's snow season, false if not.
130
	 */
131
	return apply_filters( 'jetpack_is_holiday_snow_season', $snow );
132
}
133
134
jetpack_maybe_holiday_snow();
135