load_dependencies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
class MonsterInsights_SiteNotes_Controller
4
{
5
6
	public static $instance;
7
8
	/**
9
	 * @var MonsterInsights_Site_Notes_DB_Base
10
	 */
11
	private $db;
12
13
	/**
14
	 * @return self
15
	 */
16
	public static function get_instance()
17
	{
18
		if (!isset(self::$instance) && !(self::$instance instanceof MonsterInsights_SiteNotes_Controller)) {
19
			self::$instance = new MonsterInsights_SiteNotes_Controller();
20
		}
21
		return self::$instance;
22
	}
23
24
	public function run()
25
	{
26
		$this->load_dependencies();
27
		$this->add_hooks();
28
	}
29
30
	public function load_dependencies()
31
	{
32
		require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/site-notes/Database.php';
33
		$this->db = new MonsterInsights_Site_Notes_DB_Base();
34
	}
35
36
	public function add_hooks()
37
	{
38
		add_action('init', array($this->db, 'install'));
39
		add_action('wp_ajax_monsterinsights_vue_get_notes', array($this, 'get_notes'));
40
		add_action('wp_ajax_monsterinsights_vue_get_note', array($this, 'get_note'));
41
		add_action('wp_ajax_monsterinsights_vue_get_categories', array($this, 'get_categories'));
42
		add_action('wp_ajax_monsterinsights_vue_save_note', array($this, 'save_note'));
43
		add_action('wp_ajax_monsterinsights_vue_save_category', array($this, 'save_category'));
44
		add_action('wp_ajax_monsterinsights_vue_trash_notes', array($this, 'trash_notes'));
45
		add_action('wp_ajax_monsterinsights_vue_restore_notes', array($this, 'restore_notes'));
46
		add_action('wp_ajax_monsterinsights_vue_delete_notes', array($this, 'delete_notes'));
47
		add_action('wp_ajax_monsterinsights_vue_delete_categories', array($this, 'delete_categories'));
48
49
		add_action('init', array($this, 'register_meta'));
50
51
		add_action('wp_after_insert_post', array($this, 'create_note_with_post'));
52
53
		if (!is_admin()) {
54
			return;
55
		}
56
57
		add_filter('monsterinsights_report_overview_data', array($this, 'prepare_data_overview_chart'));
58
		add_filter('monsterinsights_report_traffic_sessions_chart_data', array($this, 'prepare_traffic_sessions_chart_data'), 10, 3);
59
		add_action('save_post', array($this, 'save_custom_fields'));
60
		add_filter('monsterinsights_gutenberg_tool_vars', array($this, 'add_categories_to_editor'));
61
		add_action('admin_enqueue_scripts', array($this, 'admin_scripts'));
62
		add_action('admin_init', array($this->db, 'insert_default_categories'));
63
		add_action('admin_init', array($this, 'export_notes'));
64
		add_filter('wp_untrash_post_status', array($this, 'change_restore_note_status'), 10, 3);
65
		add_action('monsterinsights_after_exclude_metabox', array($this, 'add_metabox_contents'), 11, 2);
66
		add_action('admin_enqueue_scripts', array($this, 'load_metabox_assets'));
67
	}
68
69
	private function prepare_notes($params)
70
	{
71
		$args = wp_parse_args($params, array(
72
			'per_page' => 10,
73
			'page' => 1,
74
			'orderby' => 'id',
75
			'order' => 'desc',
76
			'filter' => [
77
				'status' => 'all',
78
				'important' => null,
79
				'date_range' => null,
80
				'category' => null,
81
			],
82
		));
83
84
		switch ($args['orderby']) {
85
			case 'note_date':
86
				$args['orderby'] = 'date';
87
				break;
88
			case 'note_title':
89
				$args['orderby'] = 'title';
90
				break;
91
		}
92
93
		if ('note_date' === $args['orderby']) {
94
			$args['orderby'] = 'date';
95
		}
96
97
		if ('note_date' === $args['orderby']) {
98
			$args['orderby'] = 'date';
99
		}
100
101
		if (!empty($params['search'])) {
102
			$args['search'] = $args['search'];
103
		}
104
105
		return $this->db->get_items($args);
106
	}
107
108
	public function get_notes()
109
	{
110
		check_ajax_referer('mi-admin-nonce', 'nonce');
111
112
		$params = !empty($_POST['params']) ? json_decode(html_entity_decode(stripslashes($_POST['params'])), true) : [];
113
114
		$output = $this->prepare_notes($params);
115
116
		$num_posts = wp_count_posts('monsterinsights_note', 'readable');
117
118
		if ($num_posts) {
119
			$output['status_filters'] = array(
120
				array(
121
					'status' => 'all',
122
					'count'  => array_sum((array) $num_posts) - $num_posts->trash,
123
				),
124
			);
125
126
			foreach ($num_posts as $status => $count) {
127
				if (0 >= $count) {
128
					continue;
129
				}
130
131
				$output['status_filters'][] = array(
132
					'status' => $status,
133
					'count'  => $count,
134
				);
135
			}
136
		}
137
138
		wp_send_json($output);
139
	}
140
141
	public function get_note()
142
	{
143
		check_ajax_referer('mi-admin-nonce', 'nonce');
144
145
		$id = !empty($_POST['id']) ? intval($_POST['id']) : null;
146
		$item = $this->db->get($id);
147
148
		if (is_wp_error($item)) {
149
			wp_send_json(
150
				array(
151
					'success' => false,
152
					'message' => $item->get_error_message(),
153
				)
154
			);
155
		}
156
157
		wp_send_json($item);
158
	}
159
160
	public function get_categories()
161
	{
162
		check_ajax_referer('mi-admin-nonce', 'nonce');
163
164
		$params = !empty($_POST['params']) ? json_decode(html_entity_decode(stripslashes($_POST['params'])), true) : [];
165
166
		$args = wp_parse_args($params, array(
167
			'per_page' => -1,
168
			'page' => 1,
169
			'orderby' => 'name',
170
			'order' => 'asc',
171
		));
172
173
		$total = intval($this->db->get_categories($args, true));
174
175
		if ($total) {
176
			$items = $this->db->get_categories($args);
177
		} else {
178
			$items = array();
179
		}
180
181
		wp_send_json(
182
			array(
183
				'items' => $items,
184
				'pagination' => array(
185
					'total' => $total,
186
					'pages' => ceil($total / $args['per_page']),
187
					'page'  => $args['page'],
188
					'per_page' => $args['per_page'],
189
				),
190
			)
191
		);
192
	}
193
194
	public function save_note()
195
	{
196
		check_ajax_referer('mi-admin-nonce', 'nonce');
197
198
		$note = !empty($_POST['note']) ? json_decode(html_entity_decode(stripslashes($_POST['note']))) : [];
199
200
		$note_details = array(
201
			'note' => sanitize_text_field($note->note_title),
202
			'category' => intval(is_object($note->category) && isset($note->category->id) && intval($note->category->id) ? $note->category->id : 0),
203
			'date' => $note->note_date_ymd,
204
			'medias' => !empty($note->medias) ? array_values(array_keys((array) $note->medias)) : [],
205
			'important' => isset($note->important) ? $note->important : false,
206
		);
207
208
		if ($note->id) {
209
			// Update Site Note.
210
			$note_details['id'] = $note->id;
211
		}
212
213
		$note_id = $this->db->create($note_details);
214
215
		if (is_wp_error($note_id)) {
216
			wp_send_json(
217
				array(
218
					'published' => false,
219
					'message' => $note_id->get_error_message(),
220
				)
221
			);
222
		}
223
224
		wp_send_json(
225
			array(
226
				'published' => true,
227
				'message' => '',
228
				'id' => $note_id,
229
			)
230
		);
231
	}
232
233
	public function save_category()
234
	{
235
		check_ajax_referer('mi-admin-nonce', 'nonce');
236
237
		$category = !empty($_POST['category']) ? json_decode(html_entity_decode(stripslashes($_POST['category']))) : [];
238
239
		if (empty($category->name)) {
240
			wp_send_json(
241
				array(
242
					'published' => false,
243
					'message' => __('Please add a category name', 'google-analytics-for-wordpress'),
244
				)
245
			);
246
		}
247
248
		if (200 < mb_strlen($category->name)) {
249
			wp_send_json(
250
				array(
251
					'published' => false,
252
					'message' => __('You can\'t exceed the 200 characters length for each site note.', 'google-analytics-for-wordpress'),
253
				)
254
			);
255
		}
256
257
		$category_id = $this->db->create_category(array(
258
			'id' => $category->id,
259
			'name' => $category->name,
260
			'background_color' => sanitize_hex_color($category->background_color),
261
		));
262
263
		if (is_wp_error($category_id)) {
264
			wp_send_json(
265
				array(
266
					'published' => false,
267
					'message' => $category_id->get_error_message(),
268
				)
269
			);
270
		}
271
272
		wp_send_json(
273
			array(
274
				'published' => true,
275
				'message' => '',
276
				'id' => $category_id,
277
			)
278
		);
279
	}
280
281
	public function change_restore_note_status($new_status, $post_id, $previous_status)
282
	{
283
		if ('monsterinsights_note' !== get_post_type($post_id)) {
284
			return $new_status;
285
		}
286
287
		return $previous_status;
288
	}
289
290
	public function trash_notes()
291
	{
292
		check_ajax_referer('mi-admin-nonce', 'nonce');
293
294
		$ids = !empty($_POST['ids']) ? json_decode(html_entity_decode(stripslashes($_POST['ids']))) : [];
295
296
		if (empty($ids)) {
297
			wp_send_json(
298
				array(
299
					'success' => false,
300
					'message' => __('Please choose a site note to trash!', 'google-analytics-for-wordpress'),
301
				)
302
			);
303
		}
304
305
		foreach ($ids as $id) {
306
			$this->db->trash_note($id);
307
		}
308
309
		wp_send_json(
310
			array(
311
				'success' => true,
312
				'message' => '',
313
			)
314
		);
315
	}
316
317
	public function restore_notes()
318
	{
319
		check_ajax_referer('mi-admin-nonce', 'nonce');
320
321
		$ids = !empty($_POST['ids']) ? json_decode(html_entity_decode(stripslashes($_POST['ids']))) : [];
322
323
		if (empty($ids)) {
324
			wp_send_json(
325
				array(
326
					'success' => false,
327
					'message' => __('Please choose a site note(s) to restore!', 'google-analytics-for-wordpress'),
328
				)
329
			);
330
		}
331
332
		foreach ($ids as $id) {
333
			$this->db->restore_note($id);
334
		}
335
336
		wp_send_json(
337
			array(
338
				'success' => true,
339
				'message' => '',
340
			)
341
		);
342
	}
343
344
	public function delete_notes()
345
	{
346
		check_ajax_referer('mi-admin-nonce', 'nonce');
347
348
		$ids = !empty($_POST['ids']) ? json_decode(html_entity_decode(stripslashes($_POST['ids']))) : [];
349
350
		if (empty($ids)) {
351
			wp_send_json(
352
				array(
353
					'success' => false,
354
					'message' => __('Please choose a site note(s) to delete!', 'google-analytics-for-wordpress'),
355
				)
356
			);
357
		}
358
359
		foreach ($ids as $id) {
360
			$this->db->delete_note($id);
361
		}
362
363
		wp_send_json(
364
			array(
365
				'success' => true,
366
				'message' => '',
367
			)
368
		);
369
	}
370
371
	public function delete_categories()
372
	{
373
		check_ajax_referer('mi-admin-nonce', 'nonce');
374
375
		$ids = !empty($_POST['ids']) ? json_decode(html_entity_decode(stripslashes($_POST['ids']))) : [];
376
377
		if (empty($ids)) {
378
			wp_send_json(
379
				array(
380
					'success' => false,
381
					'message' => __('Please choose a category to delete!', 'google-analytics-for-wordpress'),
382
				)
383
			);
384
		}
385
386
		foreach ($ids as $id) {
387
			$this->db->delete_category($id);
388
		}
389
390
		wp_send_json(
391
			array(
392
				'success' => true,
393
				'message' => '',
394
			)
395
		);
396
	}
397
398
	public function export_notes()
399
	{
400
		if (!isset($_POST['monsterinsights_action']) || empty($_POST['monsterinsights_action'])) {
401
			return;
402
		}
403
404
		if (!current_user_can('monsterinsights_save_settings')) {
405
			return;
406
		}
407
408
		if ('monsterinsights_export_notes' !== $_POST['monsterinsights_action']) {
409
			return;
410
		}
411
412
		check_admin_referer('mi-admin-nonce', 'nonce');
413
414
		//Generate the CSV.
415
		ignore_user_abort(true);
416
417
		nocache_headers();
418
		header('Content-Type: text/csv; charset=utf-8');
419
		header('Content-Disposition: attachment; filename=monsterinsights-notes-export-' . wp_date('m-d-Y') . '.csv');
420
		header("Expires: 0");
421
422
		$args = array(
423
			'per_page' => -1,
424
			'status' => ['publish', 'trash'],
425
		);
426
427
		$items = $this->db->get_items($args);
428
		$headers = array(
429
			__('Date', 'google-analytics-for-wordpress'),
430
			__('Site Note', 'google-analytics-for-wordpress'),
431
			__('Category', 'google-analytics-for-wordpress'),
432
			__('Important', 'google-analytics-for-wordpress'),
433
			__('Media', 'google-analytics-for-wordpress'),
434
		);
435
436
		$outstream = fopen("php://output", "wb");
437
438
		// phpcs:ignore
439
		fputcsv($outstream, $headers);
440
441
		foreach ($items['items'] as $item) {
442
			$item_media = __('NA', 'google-analytics-for-wordpress');
443
			foreach ($item['medias'] as $media) {
444
				if (isset($media['url'])) {
445
					$item_media = $media['url'];
446
				}
447
			}
448
			$row = array(
449
				$item['note_date'],
450
				$item['note_title'],
451
				!empty($item['category']) ? $item['category']['name'] : 'N/A',
452
				intval($item['important']),
453
				$item_media
454
			);
455
456
			// phpcs:ignore
457
			fputcsv($outstream, $row);
458
		}
459
460
		fclose($outstream);
461
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
462
	}
463
464
	public function add_categories_to_editor($vars)
465
	{
466
		$args = array(
467
			'per_page' => 0,
468
			'page' => 1,
469
			'orderby' => 'name',
470
			'order' => 'asc',
471
		);
472
473
		$categories = $this->db->get_categories($args);
474
		$output_categories = array();
475
476
		if ($categories) {
477
			foreach ($categories as $category) {
478
				$output_categories[] = (object) array(
479
					'label' => $category['name'],
480
					'value' => $category['id'],
481
				);
482
			}
483
		}
484
485
		$vars['site_notes_categories'] = $output_categories;
486
		return $vars;
487
	}
488
489
	public function register_meta()
490
	{
491
		if (!function_exists('register_post_meta')) {
492
			return;
493
		}
494
495
		register_post_meta(
496
			'',
497
			'_monsterinsights_sitenote_active',
498
			[
499
				'auth_callback' => '__return_true',
500
				'default'       => false,
501
				'show_in_rest'  => true,
502
				'single'        => true,
503
				'type'          => 'boolean',
504
			]
505
		);
506
507
		register_post_meta(
508
			'',
509
			'_monsterinsights_sitenote_note',
510
			[
511
				'auth_callback' => '__return_true',
512
				'default'       => '',
513
				'show_in_rest'  => true,
514
				'single'        => true,
515
				'type'          => 'string',
516
			]
517
		);
518
519
		register_post_meta(
520
			'',
521
			'_monsterinsights_sitenote_category',
522
			[
523
				'auth_callback' => '__return_true',
524
				'default'       => 0,
525
				'show_in_rest'  => true,
526
				'single'        => true,
527
				'type'          => 'integer',
528
			]
529
		);
530
	}
531
532
	public function save_custom_fields($current_post_id)
533
	{
534
		if (!isset($_POST['monsterinsights_metabox_nonce']) || !wp_verify_nonce($_POST['monsterinsights_metabox_nonce'], 'monsterinsights_metabox')) {
535
			return;
536
		}
537
538
		if ('monsterinsights_note' === get_post_type($current_post_id) || 'publish' !== get_post_status($current_post_id)) {
539
			return;
540
		}
541
542
		$active = intval(isset($_POST['_monsterinsights_sitenote_active']) ? $_POST['_monsterinsights_sitenote_active'] : 0);
543
		update_post_meta($current_post_id, '_monsterinsights_sitenote_active', $active);
544
545
		if (!$active) {
546
			delete_post_meta($current_post_id, '_monsterinsights_sitenote_note');
547
			delete_post_meta($current_post_id, '_monsterinsights_sitenote_category');
548
			delete_post_meta($current_post_id, '_monsterinsights_sitenote_id');
549
			return;
550
		}
551
552
		$note = isset($_POST['_monsterinsights_sitenote_note']) ? esc_html($_POST['_monsterinsights_sitenote_note']) : '';
553
		update_post_meta($current_post_id, '_monsterinsights_sitenote_note', $note);
554
555
		$category = isset($_POST['_monsterinsights_sitenote_category']) ? intval($_POST['_monsterinsights_sitenote_category']) : 0;
556
		if ($category) {
557
			update_post_meta($current_post_id, '_monsterinsights_sitenote_category', $category);
558
		}
559
	}
560
561
	public function create_note_with_post($post_ID)
562
	{
563
		if ('monsterinsights_note' === get_post_type($post_ID) || 'publish' !== get_post_status($post_ID)) {
564
			return;
565
		}
566
567
		$active = get_post_meta($post_ID, '_monsterinsights_sitenote_active', true);
568
569
		if (!$active) {
570
			delete_post_meta($post_ID, '_monsterinsights_sitenote_note');
571
			delete_post_meta($post_ID, '_monsterinsights_sitenote_category');
572
			delete_post_meta($post_ID, '_monsterinsights_sitenote_id');
573
			return;
574
		}
575
576
		$note = get_post_meta($post_ID, '_monsterinsights_sitenote_note', true);
577
578
		$category = get_post_meta($post_ID, '_monsterinsights_sitenote_category', true);
579
580
		$note_id = get_post_meta($post_ID, '_monsterinsights_sitenote_id', true);
581
582
		//create the new note
583
		$note_details = array(
584
			'note' => $note,
585
			'category' => intval($category),
586
			'date' => wp_date('Y-m-d'),
587
			'medias' => [],
588
			'important' => false,
589
		);
590
591
		if (!empty($note_id)) {
592
			// Update Site Note.
593
			$note_details['id'] = $note_id;
594
		}
595
596
		$created_note_id = $this->db->create($note_details);
597
		if (is_wp_error($created_note_id) || $note_id === $created_note_id) {
598
			return;
599
		}
600
601
		update_post_meta($post_ID, '_monsterinsights_sitenote_id', $created_note_id);
602
	}
603
604
	public function admin_scripts()
605
	{
606
		if (!function_exists('get_current_screen')) {
607
			return;
608
		}
609
610
		// Get current screen.
611
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
612
613
		// Bail if we're not on a MonsterInsights screen.
614
		if (empty($screen->id) || strpos($screen->id, 'monsterinsights') === false) {
615
			return;
616
		}
617
618
		wp_enqueue_media();
619
	}
620
621
	public function prepare_data_overview_chart($data)
622
	{
623
		if (!isset($data['data']['overviewgraph'])) {
624
			return $data;
625
		}
626
627
		$params = array(
628
			'per_page' => -1,
629
			'filter' => array(
630
				'date_range' => array(
631
					'start' => $data['data']['infobox']['current']['startDate'],
632
					'end' => $data['data']['infobox']['current']['endDate'],
633
				),
634
			),
635
		);
636
		$notes = $this->prepare_notes($params);
637
638
		$data['data']['overviewgraph']['notes'] = array();
639
640
		foreach ($notes['items'] as $note) {
641
			$date_index = date('j M', strtotime($note['note_date'], current_time('U')));
642
			if (!isset($data['data']['overviewgraph']['notes'][$date_index])) {
643
				$data['data']['overviewgraph']['notes'][$date_index] = array();
644
			}
645
			$data['data']['overviewgraph']['notes'][$date_index][] = array(
646
				'title' => $note['note_title'],
647
				'color' => (isset($note['category']) && isset($note['category']['background_color'])) ? str_replace('#', '', $note['category']['background_color']) : null,
648
				'important' => $note['important'],
649
			);
650
		}
651
652
		return $data;
653
	}
654
655
	public function add_metabox_contents($skipped, $post)
656
	{
657
		$sitenote_active = get_post_meta($post->ID, '_monsterinsights_sitenote_active', true);
658
		$sitenote_note = get_post_meta($post->ID, '_monsterinsights_sitenote_note', true);
659
		$sitenote_category = get_post_meta($post->ID, '_monsterinsights_sitenote_category', true);
660
661
		$args = array(
662
			'per_page' => 0,
663
			'page' => 1,
664
			'orderby' => 'name',
665
			'order' => 'asc',
666
		);
667
668
		$categories = $this->db->get_categories($args);
669
670
?>
671
		<div class="monsterinsights-metabox" id="monsterinsights-metabox-site-notes">
672
			<div class="monsterinsights-metabox-input monsterinsights-metabox-input-checkbox">
673
				<label class="">
674
					<input type="checkbox" name="_monsterinsights_sitenote_active" value="1" <?php checked($sitenote_active); ?>>
675
					<span class="monsterinsights-metabox-input-checkbox-label"><?php _e('Add a Site Note', 'google-analytics-for-wordpress'); ?></span>
676
				</label>
677
			</div>
678
679
			<div id="site-notes-active-container" class="<?php echo (!$sitenote_active ? 'hidden' : ''); ?>">
680
				<div class="monsterinsights-metabox-input monsterinsights-metabox-textarea">
681
					<textarea name="_monsterinsights_sitenote_note" rows="3"><?php echo esc_textarea($sitenote_note); ?></textarea>
0 ignored issues
show
Bug introduced by
It seems like $sitenote_note can also be of type false; however, parameter $text of esc_textarea() 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

681
					<textarea name="_monsterinsights_sitenote_note" rows="3"><?php echo esc_textarea(/** @scrutinizer ignore-type */ $sitenote_note); ?></textarea>
Loading history...
682
				</div>
683
684
				<div class="monsterinsights-metabox-input monsterinsights-metabox-select">
685
					<label>
686
						<?php _e('Category', 'google-analytics-for-wordpress'); ?>
687
						<select name="_monsterinsights_sitenote_category">
688
							<?php if (!empty($categories)) {
689
								foreach ($categories as $category) {
690
							?>
691
									<option <?php selected($sitenote_category, $category['id']); ?> value="<?php echo esc_attr($category['id']); ?>"><?php echo esc_html($category['name']); ?></option>
692
							<?php
693
								}
694
							} ?>
695
						</select>
696
					</label>
697
				</div>
698
			</div>
699
700
		</div>
701
<?php
702
	}
703
704
	public function load_metabox_assets()
705
	{
706
		wp_register_style('monsterinsights-admin-metabox-sitenotes-style', plugins_url('assets/css/admin-metabox-sitenotes.css', MONSTERINSIGHTS_PLUGIN_FILE), array(), monsterinsights_get_asset_version());
707
		wp_enqueue_style('monsterinsights-admin-metabox-sitenotes-style');
708
709
		wp_register_script('monsterinsights-admin-metabox-sitenotes-script', plugins_url('assets/js/admin-metabox-sitenotes.js', MONSTERINSIGHTS_PLUGIN_FILE), array('jquery'), monsterinsights_get_asset_version());
710
		wp_enqueue_script('monsterinsights-admin-metabox-sitenotes-script');
711
	}
712
713
	/**
714
	 * Add site-note to traffic sessions chart.
715
	 *
716
	 * @param array $data
717
	 * @param string $start_date
718
	 * @param string $end_date
719
	 *
720
	 * @return array
721
	 */
722
	public function prepare_traffic_sessions_chart_data( $data, $start_date, $end_date ) {
723
724
		if ( ! isset( $data['data']['sessions_chart'] ) ) {
725
			return $data;
726
		}
727
728
		$params = array(
729
			'per_page' => - 1,
730
			'filter'   => array(
731
				'date_range' => array(
732
					'start' => $start_date,
733
					'end'   => $end_date,
734
				),
735
			),
736
		);
737
738
		$notes = $this->prepare_notes( $params );
739
740
		$prepared_notes = array();
741
742
		foreach ( $notes['items'] as $note ) {
743
			$date_index = date( 'j M', strtotime( $note['note_date'], current_time( 'U' ) ) );
744
745
			if ( ! isset( $prepared_notes[ $date_index ] ) ) {
746
				$prepared_notes[ $date_index ] = array();
747
			}
748
749
			$prepared_notes[ $date_index ][] = array(
750
				'title'     => $note['note_title'],
751
				'color'     => ( isset( $note['category'] ) && isset( $note['category']['background_color'] ) ) ? str_replace( '#', '', $note['category']['background_color'] ) : null,
752
				'important' => $note['important'],
753
			);
754
		}
755
756
		$data['data']['sessions_chart']['notes'] = $prepared_notes;
757
758
		return $data;
759
	}
760
761
	/**
762
	 * Create a site note.
763
	 */
764
	public function create_note( $note_details ) {
765
		return $this->db->create( $note_details );
766
	}
767
}
768
769
MonsterInsights_SiteNotes_Controller::get_instance()->run();
770