Passed
Push — master ( 2bd1cf...bfbffa )
by
unknown
05:03
created

build_external_link()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
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
	 * When the notification will repeat (e.g: 7) here `7` to repeat the notification after each 7 days
23
	 * Only accept numeric value
24
	 *
25
	 * @var number
26
	 *
27
	 * @since 7.12.3
28
	 */
29
	public $notification_interval;
30
31
	/**
32
	 * When the notification will active, default: now
33
	 *
34
	 * @var string
35
	 *
36
	 * @since 7.12.3
37
	 */
38
	public $notification_active_from;
39
40
	/**
41
	 * For how many days notification will be active
42
	 *
43
	 * @var string
44
	 *
45
	 * @since 7.12.3
46
	 */
47
	public $notification_active_for;
48
49
	/**
50
	 * Which type of license is allowed to view this notification
51
	 *
52
	 * @var array
53
	 *
54
	 * @since 7.12.3
55
	 */
56
	public $notification_type;
57
58
	/**
59
	 * Report start date if required e.g: "-15 day"(Readable Time)
60
	 *
61
	 * @var string
62
	 *
63
	 * @since 7.12.3
64
	 */
65
	public $report_start_from;
66
67
	/**
68
	 * Report end date if required e.g: "-1 day"(Readable Time)
69
	 *
70
	 * @var string
71
	 *
72
	 * @since 7.12.3
73
	 */
74
	public $report_end_to;
75
76
	/**
77
	 * Notification icon to display with content
78
	 *
79
	 * @var string
80
	 *
81
	 * @since 7.12.3
82
	 */
83
	public $notification_icon;
84
85
	/**
86
	 * Constructor
87
	 *
88
	 * @since 7.12.3
89
	 */
90
	public function __construct() {
91
92
		$this->notification_active_from = date( 'm/d/Y g:i a', strtotime( 'now' ) );
93
		$this->report_end_to            = '-1 day'; // Yesterday.
94
95
		if ( ! empty( $this->notification_id ) && ! empty( $this->notification_interval ) ) {
96
97
			// Register notification in our custom runner.
98
			monsterinsights_notification_event_runner()->register_notification( $this );
99
100
			$this->notification_active_for = date( 'm/d/Y', strtotime( '+' . ( $this->notification_interval - 2 ) . ' day' ) );
101
			$this->report_start_from       = '-' . $this->notification_interval . ' day';
102
103
			if ( ! isset( $this->notification_icon ) || empty( $this->notification_icon ) ) {
104
				$this->notification_icon = '<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
105
<circle cx="16" cy="16" r="16" fill="#D3F8EA"/>
106
<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"/>
107
</svg>';
108
			}
109
110
		}
111
	}
112
113
	/**
114
	 * Get the formatted date.
115
	 *
116
	 * @param string $readable_time Readable time to convert to date.
117
	 *
118
	 * @return string date, format: Y-m-d
119
	 *
120
	 * @since 7.12.3
121
	 */
122
	public function get_formatted_date( $readable_time ) {
123
		return date( 'Y-m-d', strtotime( $readable_time ) );
124
	}
125
126
	/**
127
	 * Get the upgrade URL for pro plugin
128
	 *
129
	 * @return string
130
	 */
131
	public function get_upgrade_url() {
132
		return wp_specialchars_decode( monsterinsights_get_upgrade_link( 'monsterinsights-notifications-sidebar', 'notifications', 'https://www.monsterinsights.com/lite/' ) );
133
	}
134
135
	/**
136
	 * Build external link by including UTM data
137
	 *
138
	 * @return string
139
	 */
140
	public function build_external_link( $url ) {
141
		return wp_specialchars_decode( monsterinsights_get_url( 'monsterinsights-notifications-sidebar', 'notifications', $url ) );
142
	}
143
144
	/**
145
	 * Get the URL for the page where users can see/read notifications.
146
	 *
147
	 * @return string
148
	 */
149
	public function get_view_url() {
150
		return MonsterInsights()->notifications->get_view_url();
151
	}
152
153
	/**
154
	 * @param array $data
155
	 *
156
	 * @return array
157
	 */
158
	public function prepare_notification_data( $data ) {
159
		return $data;
160
	}
161
162
	/**
163
	 * Add Notification not the notifications instance.
164
	 *
165
	 * @since 7.12.3
166
	 */
167
	public function add_notification() {
168
		$notification            = array();
169
		$notification['id']      = $this->notification_id . '_' . date( 'Ymd' ); // Make sure we never add the same notification on the same day.
170
		$notification['icon']    = $this->notification_icon;
171
		$notification['title']   = '';
172
		$notification['content'] = '';
173
		$notification['type']    = $this->notification_type;
174
		$notification['btns']    = array();
175
		$notification['start']   = $this->notification_active_from;
176
		$notification['end']     = $this->notification_active_for;
177
		$notification_data       = $this->prepare_notification_data( $notification );
178
179
		if ( is_array( $notification_data ) && ! empty( $notification_data ) ) {
180
			MonsterInsights()->notifications->add( $notification_data );
181
		}
182
	}
183
184
	/**
185
	 * Get report
186
	 *
187
	 * @param string $report_name report name, default overview report.
188
	 * @param string $report_start_from report start date, default -30 days/last 30 days.
189
	 * @param string $report_end_to report end date, default -1 day/yesterday.
190
	 *
191
	 * @return array $data Overview data
192
	 *
193
	 * @since 7.12.3
194
	 */
195
	public function get_report( $report_name = 'overview', $report_start_from = '-30 day', $report_end_to = '-1 day' ) {
196
197
		$report = MonsterInsights()->reporting->get_report( $report_name );
198
		if ( $report ) {
199
			// Mark the report request as coming from Notifications.
200
			$report->set_report_source( 'notifications' );
201
		}
202
		$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

202
		$isnetwork = ! empty( $_REQUEST['isnetwork'] ) ? sanitize_text_field( /** @scrutinizer ignore-type */ wp_unslash( $_REQUEST['isnetwork'] ) ) : '';
Loading history...
203
		$args      = array(
204
			'start' => $this->get_formatted_date( $report_start_from ),
205
			'end'   => $this->get_formatted_date( $report_end_to ),
206
		);
207
208
		if ( $isnetwork ) {
209
			$args['network'] = true;
210
		}
211
212
		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...
213
			$data = array(
214
				'success' => false,
215
				'message' => __( "You don't have permission to view MonsterInsights reports.", 'google-analytics-for-wordpress' ),
216
			);
217
		} else {
218
			$data = $report->get_data( $args );
219
		}
220
221
		return $data;
222
	}
223
}
224