Passed
Push — master ( 6f0a6d...90a9ca )
by
unknown
09:41
created

MonsterInsights_Notification_Event   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 301
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
dl 0
loc 301
rs 10
c 1
b 0
f 0
wmc 23

10 Methods

Rating   Name   Duplication   Size   Complexity  
A hooks() 0 7 1
A add_cron_schedule() 0 8 1
B __construct() 0 27 8
A get_upgrade_url() 0 2 1
A add_notification() 0 15 3
A get_formatted_date() 0 2 1
A get_view_url() 0 2 1
A get_report() 0 24 5
A get_first_cron_date() 0 13 1
A build_external_link() 0 2 1
1
<?php
2
/**
3
 * Parent Class for MonsterInsights Notification Event
4
 *
5
 * @since 7.12.3
6
 *
7
 * @package MonsterInsights
8
 */
9
10
class MonsterInsights_Notification_Event {
11
12
	/**
13
	 * Generate unique notification id
14
	 *
15
	 * @var string
16
	 *
17
	 * @since 7.12.3
18
	 */
19
	public $notification_id;
20
21
	/**
22
	 * Notification hook name for cron schedule
23
	 *
24
	 * @var string
25
	 *
26
	 * @since 7.12.3
27
	 */
28
	public $notification_cron_hook_name;
29
30
	/**
31
	 * Unique recurrence name to set up the notification interval
32
	 * Only accept numeric value
33
	 *
34
	 * @var string
35
	 *
36
	 * @since 7.12.3
37
	 */
38
	public $notification_recurrence_name;
39
40
	/**
41
	 * When the notification will run for the first time
42
	 * Value should be readable time, (e.g: +30 day) to run the notification after 30 days from now
43
	 *
44
	 * @var string
45
	 *
46
	 * @since 7.12.3
47
	 */
48
	public $notification_first_run_time;
49
50
	/**
51
	 * When the notification will repeat (e.g: 7) here `7` to repeat the notification after each 7 days
52
	 * Only accept numeric value
53
	 *
54
	 * @var number
55
	 *
56
	 * @since 7.12.3
57
	 */
58
	public $notification_interval;
59
60
	/**
61
	 * When the notification will active, default: now
62
	 *
63
	 * @var string
64
	 *
65
	 * @since 7.12.3
66
	 */
67
	public $notification_active_from;
68
69
	/**
70
	 * For how many days notification will be active
71
	 *
72
	 * @var string
73
	 *
74
	 * @since 7.12.3
75
	 */
76
	public $notification_active_for;
77
78
	/**
79
	 * Which type of license is allowed to view this notification
80
	 *
81
	 * @var array
82
	 *
83
	 * @since 7.12.3
84
	 */
85
	public $notification_type;
86
87
	/**
88
	 * Report start date if required e.g: "-15 day"(Readable Time)
89
	 *
90
	 * @var string
91
	 *
92
	 * @since 7.12.3
93
	 */
94
	public $report_start_from;
95
96
	/**
97
	 * Report end date if required e.g: "-1 day"(Readable Time)
98
	 *
99
	 * @var string
100
	 *
101
	 * @since 7.12.3
102
	 */
103
	public $report_end_to;
104
105
	/**
106
	 * Notification icon to display with content
107
	 *
108
	 * @var string
109
	 *
110
	 * @since 7.12.3
111
	 */
112
	public $notification_icon;
113
114
	/**
115
	 * Constructor
116
	 *
117
	 * @since 7.12.3
118
	 */
119
	public function __construct() {
120
		$this->notification_active_from = date( "m/d/Y g:i a", strtotime( "now" ) );
121
		$this->report_end_to            = "-1 day"; // yesterday
122
123
		if ( ! empty( $this->notification_id ) && ! empty( $this->notification_interval ) ) {
124
125
			$this->notification_cron_hook_name  = $this->notification_id . '_cron';
126
			$this->notification_recurrence_name = $this->notification_id . '_' . $this->notification_interval . '_days';
127
			$this->notification_id              = $this->notification_id . '_' . time();
128
			$this->notification_active_for      = date( "m/d/Y", strtotime( "+" . ( $this->notification_interval - 2 ) . " day" ) );
129
			$this->report_start_from            = "-". $this->notification_interval ." day";
130
131
			if( ! isset( $this->notification_first_run_time ) || empty( $this->notification_first_run_time ) ) {
132
				$this->notification_first_run_time = "+". $this->notification_interval ." day";
133
			}
134
135
			if( ! isset( $this->notification_icon ) || empty( $this->notification_icon ) ) {
136
				$this->notification_icon        = '<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
137
<circle cx="16" cy="16" r="16" fill="#D3F8EA"/>
138
<path d="M21.8634 18.6429C21.8634 18.8571 21.7831 19.0268 21.6224 19.1518C21.5688 19.3482 21.542 19.6786 21.542 20.1429C21.542 20.6071 21.5688 20.9375 21.6224 21.1339C21.7831 21.2768 21.8634 21.4464 21.8634 21.6429V22.0714C21.8634 22.25 21.8009 22.4018 21.6759 22.5268C21.5509 22.6518 21.3992 22.7143 21.2206 22.7143H12.4349C11.7206 22.7143 11.1134 22.4643 10.6134 21.9643C10.1134 21.4643 9.86345 20.8571 9.86345 20.1429V11.5714C9.86345 10.8571 10.1134 10.25 10.6134 9.75C11.1134 9.25 11.7206 9 12.4349 9H21.2206C21.3992 9 21.5509 9.0625 21.6759 9.1875C21.8009 9.3125 21.8634 9.46429 21.8634 9.64286V18.6429ZM13.292 12.5893V13.125C13.292 13.2321 13.3456 13.2857 13.4527 13.2857H19.1313C19.2384 13.2857 19.292 13.2321 19.292 13.125V12.5893C19.292 12.4821 19.2384 12.4286 19.1313 12.4286H13.4527C13.3456 12.4286 13.292 12.4821 13.292 12.5893ZM13.292 14.3036V14.8393C13.292 14.9464 13.3456 15 13.4527 15H19.1313C19.2384 15 19.292 14.9464 19.292 14.8393V14.3036C19.292 14.1964 19.2384 14.1429 19.1313 14.1429H13.4527C13.3456 14.1429 13.292 14.1964 13.292 14.3036ZM20.0688 21C20.0152 20.4286 20.0152 19.8571 20.0688 19.2857H12.4349C12.2027 19.2857 11.9974 19.375 11.8188 19.5536C11.6581 19.7143 11.5777 19.9107 11.5777 20.1429C11.5777 20.375 11.6581 20.5804 11.8188 20.7589C11.9974 20.9196 12.2027 21 12.4349 21H20.0688Z" fill="#1EC185"/>
139
</svg>';
140
			}
141
142
			$this->hooks();
143
144
			if ( ! wp_next_scheduled( $this->notification_cron_hook_name ) ) {
145
				wp_schedule_event( $this->get_first_cron_date(), $this->notification_recurrence_name, $this->notification_cron_hook_name );
146
			}
147
148
		}
149
	}
150
151
	/**
152
	 * Register hooks.
153
	 *
154
	 * @since 7.12.3
155
	 */
156
	public function hooks() {
157
158
		add_filter( $this->notification_id, array( $this, 'prepare_notification_data' ) );
159
160
		add_filter( 'cron_schedules', array( $this, 'add_cron_schedule' ) );
161
162
		add_action( $this->notification_cron_hook_name, array( $this, 'add_notification' ) );
163
164
	}
165
166
	/**
167
	 * Cron to add notification
168
	 *
169
	 * @param   array  $schedules  WP cron schedules.
170
	 *
171
	 * @return  array  $schedules  WP cron schedules.
172
	 *
173
	 * @since 7.12.3
174
	 */
175
	public function add_cron_schedule( $schedules ) {
176
		$schedules[$this->notification_recurrence_name] = array(
177
			'interval' => DAY_IN_SECONDS * $this->notification_interval,
178
			// Translators: notification cron interval
179
			'display'  => sprintf( __( '%s Days', 'google-analytics-for-wordpress' ), $this->notification_interval ),
180
		);
181
182
		return $schedules;
183
	}
184
185
	/**
186
	 * get date
187
	 *
188
	 * @param string $readable_time readable time to convert to date
189
	 *
190
	 * @return string date, format: Y-m-d
191
	 *
192
	 * @since 7.12.3
193
	 */
194
	public function get_formatted_date( $readable_time ) {
195
		return date( "Y-m-d", strtotime( $readable_time ) );
196
	}
197
198
	/**
199
	 * Get the upgrade URL for pro plugin
200
	 *
201
	 * @return string
202
	 */
203
	public function get_upgrade_url() {
204
		return wp_specialchars_decode( monsterinsights_get_upgrade_link( 'monsterinsights-notifications-sidebar', 'notifications', 'https://www.monsterinsights.com/lite/' ) );
205
	}
206
207
	/**
208
	 * Build external link by including UTM data
209
	 *
210
	 * @return string
211
	 */
212
	public function build_external_link( $url ) {
213
		return wp_specialchars_decode( monsterinsights_get_url( 'monsterinsights-notifications-sidebar', 'notifications', $url ) );
214
	}
215
216
	/**
217
	 * Get next cron occurrence date.
218
	 *
219
	 * (e:g +30 day) to run after 30 days from now
220
	 *
221
	 * @return int $date to run the first cron
222
	 *
223
	 * @since 7.12.3
224
	 */
225
	public function get_first_cron_date() {
226
		$schedule           = array();
227
		$schedule['day']    = rand( 0, 1 );
228
		$schedule['hour']   = rand( 0, 23 );
229
		$schedule['minute'] = rand( 0, 59 );
230
		$schedule['second'] = rand( 0, 59 );
231
		$schedule['offset'] = ( $schedule['day'] * DAY_IN_SECONDS ) +
232
		                      ( $schedule['hour'] * HOUR_IN_SECONDS ) +
233
		                      ( $schedule['minute'] * MINUTE_IN_SECONDS ) +
234
		                      $schedule['second'];
235
		$date               = strtotime( $this->notification_first_run_time ) + $schedule['offset'];
236
237
		return $date;
238
	}
239
240
	/**
241
	 * Get the URL for the page where users can see/read notifications.
242
	 *
243
	 * @return string
244
	 */
245
	public function get_view_url() {
246
		return MonsterInsights()->notifications->get_view_url();
247
	}
248
249
	/**
250
	 * Build Notification
251
	 *
252
	 * @param array $data
253
	 *
254
	 * @return array $notification notification is ready to add
255
	 *
256
	 * @since 7.12.3
257
	 */
258
	public function add_notification() {
259
		$notification            = array();
260
		$notification['id']      = $this->notification_id;
261
		$notification['icon']    = $this->notification_icon;
262
		$notification['title']   = '';
263
		$notification['content'] = '';
264
		$notification['type']    = $this->notification_type;
265
		$notification['btns']    = array();
266
		$notification['start']   = $this->notification_active_from;
267
		$notification['end']     = $this->notification_active_for;
268
269
		$notification_data =  apply_filters( $this->notification_id, $notification );
270
271
		if ( is_array( $notification_data ) && ! empty( $notification_data ) ) {
272
			MonsterInsights()->notifications->add( $notification_data );
273
		}
274
	}
275
276
	/**
277
	 * Get report
278
	 *
279
	 * @param   string  $report_name  report name, default overview report
280
	 * @param   string  $report_start_from  report start date, default -30 days/last 30 days
281
	 * @param   string  $report_end_to  report end date, default -1 day/yesterday
282
	 *
283
	 * @return array $data Overview data
284
	 *
285
	 * @since 7.12.3
286
	 */
287
	public function get_report( $report_name="overview", $report_start_from="-30 day", $report_end_to="-1 day" ) {
288
		// get overview report data
289
		$data      = array();
290
		$report    = MonsterInsights()->reporting->get_report( $report_name );
291
		$isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) : '';
0 ignored issues
show
Bug introduced by
It seems like wp_unslash($_REQUEST['isnetwork']) can also be of type string[]; however, parameter $str of sanitize_text_field() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

291
		$isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? sanitize_text_field( /** @scrutinizer ignore-type */ wp_unslash( $_REQUEST['isnetwork'] ) ) : '';
Loading history...
292
		$args = array(
293
			'start' => $this->get_formatted_date( $report_start_from ),
294
			'end'   => $this->get_formatted_date( $report_end_to ),
295
		);
296
297
		if ( $isnetwork ) {
298
			$args['network'] = true;
299
		}
300
301
		if ( monsterinsights_is_pro_version() && ! MonsterInsights()->license->license_can( $report->level ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The property $license is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
302
			$data = array(
303
				'success' => false,
304
				'message' => __( "You don't have permission to view MonsterInsights reports.", 'google-analytics-for-wordpress' ),
305
			);
306
		} else {
307
			$data = apply_filters( 'monsterinsights_vue_reports_data', $report->get_data( $args ), $report_name, $report );
308
		}
309
310
		return $data;
311
	}
312
}
313