Completed
Push — fix/visibility-condition-issue... ( eee6fd...a71c15 )
by
unknown
11:07
created

holiday-snow.php ➔ jetpack_show_holiday_snow_option()   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 View Code Duplication
function jetpack_show_holiday_snow_option() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
	// Always show snow option if a custom snow season has been set.
113
	if ( has_filter( 'jetpack_is_holiday_snow_season' ) ) {
114
		return true;
115
	}
116
117
	$today            = time();
118
	$first_option_day = mktime( 0, 0, 0, 11, 24 ); // Nov 24
119
	$last_option_day  = mktime( 0, 0, 0, 1, 4 );   // Jan 4
120
121
	return ( $today >= $first_option_day || $today < $last_option_day );
122
}
123
124 View Code Duplication
function jetpack_is_holiday_snow_season() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
	$today          = time();
126
	$first_snow_day = mktime( 0, 0, 0, 12, 1 );
127
	$last_snow_day  = mktime( 0, 0, 0, 1, 4 );
128
129
	$snow = ( $today >= $first_snow_day || $today < $last_snow_day );
130
131
	/**
132
	 * Filter whether it's winter or not.
133
	 *
134
	 * You can use this filter if, for example, you live in the
135
	 * Southern Hemisphere. In that case, the dates for winter
136
	 * above are incorrect for your location.
137
	 *
138
	 * @module theme-tools
139
	 *
140
	 * @since 2.1.0
141
	 *
142
	 * @param bool $snow True if it's snow season, false if not.
143
	 */
144
	return apply_filters( 'jetpack_is_holiday_snow_season', $snow );
145
}
146
147
jetpack_maybe_holiday_snow();
148