banner.php
6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* WPMUDEV Black Friday common module
*
* Used by wordpress.org free plugins only to show Black Friday deal on admin dashboard.
*
* @since 1.0
* @author WPMUDEV
* @package WPMUDEV\BlackFriday
*/
namespace WPMUDEV\BlackFriday;
if ( ! class_exists( __NAMESPACE__ . '\\Banner' ) ) {
/**
* Class Load.
*
* @since 1.0
* @package WPMUDEV\BlackFriday\Banner
*/
class Banner {
/**
* Version number.
*
* @since 1.0
* @var string $version
*/
private $version = '1.0.0';
/**
* SUI version number (in class format).
*
* @since 1.0
* @var string $sui_version
*/
private $sui_version = '2-12-13';
/**
* Option name to store data.
*
* @since 1.0
* @var string $option_name
*/
protected $option_name = 'wpmudev_black_friday_flag';
/**
* Nonce.
*
* @since 1.0
* @var string $nonce
*/
protected $nonce = 'wpmudev-bf-common';
/**
* Constants to be used by plugins.
*/
const SMUSH = 0;
const FORMINATOR = 10;
const HUMMIGNBIRD = 20;
const HUSTLE = 30;
const DEFENDER = 40;
const SMARTCRAWL = 50;
const BRANDA = 60;
const BEEHIVE = 70;
/**
* Boolean that holds a notification printed state.
*
* @since 1.0
* @var bool
*/
private static $printed = false;
/**
* The plugin id. Passed via data attributes to js.
*
* @since 1.0
* @var array
*/
private $labels;
/**
* The plugin utm link. Passed via data attributes to js.
*
* @since 1.0
* @var string
*/
private $utm;
/**
* Plugin priority to be used as admin_notices hook priority.
*
* @since 1.0
* @var int
*/
private $priority = 0;
/**
* Construct handler class.
*
* @since 1.0
*
* @param array $labels Banner labels.
* @param string $utm_link UTM link.
* @param int $priority Priority.
*
* @return void
*/
public function __construct( $labels, $utm_link, $priority = 10 ) {
if ( empty( $labels ) || empty( $utm_link ) ) {
return;
}
$this->labels = wp_parse_args(
$labels,
array(
'close' => 'Close',
'get_deal' => 'Get deal',
'intro' => 'Black Friday offer for WP businesses and agencies',
'off' => 'Off',
'title' => 'Everything you need to run your WP business for',
'discount' => '83.5',
'price' => '3000',
'description' => 'WPMU DEV’s all-in-one platform gives you all the Pro tools and support you need to run and grow a web development business. Trusted by over 50,000 web developers. Limited deals available.',
)
);
$this->utm = $utm_link;
$this->priority = $priority;
// Current screen actions.
add_action( 'current_screen', array( $this, 'current_screen_actions' ) );
// Ajax request to dismiss.
add_action( 'wp_ajax_wpmudev_bf_dismiss', array( $this, 'dismiss_banner' ) );
}
/**
* Perform actions for current screen.
*
* @since 1.0
*
* @return void
*/
public function current_screen_actions() {
if ( ! $this->can_load() ) {
return;
}
// Enqueue assets.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
// Render dashboard notice.
add_action( 'all_admin_notices', array( $this, 'dashboard_notice' ), $this->priority );
}
/**
* Enqueues scripts and styles for the banner.
*
* @since 1.0
*
* @return void
*/
public function enqueue_styles() {
wp_enqueue_style(
'wpmudev-bf-common',
plugin_dir_url( __FILE__ ) . 'assets/css/banner.min.css',
array(),
$this->version
);
}
/**
* Loads the Dashboard Notice
*
* @since 1.0
*
* @return void
*/
public function dashboard_notice() {
if ( self::$printed ) {
return;
}
self::$printed = true;
wp_enqueue_script(
'wpmudev-bf-common',
plugin_dir_url( __FILE__ ) . '/assets/js/banner.min.js',
array( 'wp-dom-ready' ),
$this->version,
true
);
wp_localize_script(
'wpmudev-bf-common',
'wpmudevBFBanner',
array(
'nonce' => wp_create_nonce( $this->nonce ),
'labels' => $this->labels,
'utm' => $this->utm,
)
);
echo '<div class="sui-' . esc_html( $this->sui_version ) . '">
<div class="sui-wrap">
<div id="wpmudev-bf-common-notice"></div>
</div>
</div>';
}
/**
* Checks if plugin's Black Friday deal can be loaded.
*
* @since 1.0
*
* @return boolean
*/
public function can_load() {
if (
! $this->is_admin_dash() ||
! current_user_can( 'delete_users' ) ||
$this->event_expired() ||
$this->dashboard_plugin_installed() ||
$this->banner_shown_previously()
) {
return false;
}
return true;
}
/**
* Checks if current page is admin dashboard.
*
* @since 1.0
*
* @return boolean
*/
public function is_admin_dash() {
if ( is_network_admin() || is_main_site() ) {
return function_exists( 'get_current_screen' ) && in_array( get_current_screen()->id, array( 'dashboard', 'dashboard-network' ), true );
}
return false;
}
/**
* Checks if offer has expired.
*
* @since 1.0
*
* @return boolean
*/
public function event_expired() {
$current_date = apply_filters( 'wpmudev_blackfriday_current_date', 'd-m-Y' );
$start_date = apply_filters( 'wpmudev_blackfriday_start_date', '21-11-2022' );
$expire_date = apply_filters( 'wpmudev_blackfriday_expire_date', '29-11-2022' );
// Expires on 29 Nov 2022.
return (
date_create( date_i18n( $current_date ) )->getTimestamp() < date_create( date_i18n( $start_date ) )->getTimestamp() ||
date_create( date_i18n( $current_date ) )->getTimestamp() >= date_create( date_i18n( $expire_date ) )->getTimestamp()
);
}
/**
* Checks if Dashboard plugin is installed.
*
* @since 1.0
*
* @return boolean
*/
public function dashboard_plugin_installed() {
return class_exists( 'WPMUDEV_Dashboard' );
}
/**
* Checks if notice has been already shown.
*
* @since 1.0
*
* @return bool
*/
public function banner_shown_previously() {
return get_site_option( $this->option_name );
}
/**
* Ajax request handler for banner dismissal.
*
* @since 1.0
*
* @return void
*/
public function dismiss_banner() {
// Verify nonce.
check_ajax_referer( $this->nonce, 'nonce' );
// Dismiss notice.
update_site_option( $this->option_name, true );
wp_send_json_success( array( 'success' => true ) );
}
}
}