Passed
Push — master ( e0709e...805894 )
by Chris
06:51
created

MonsterInsights_Lite_Report_YearInReview   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B prepare_report_data() 0 30 8
1
<?php
2
// Exit if accessed directly
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
final class MonsterInsights_Lite_Report_YearInReview extends MonsterInsights_Report {
8
9
	public $title;
10
	public $class   = 'MonsterInsights_Lite_Report_YearInReview';
11
	public $name    = 'yearinreview';
12
	public $version = '1.0.0';
13
	public $level   = 'lite';
14
15
	/**
16
	 * Primary class constructor.
17
	 *
18
	 * @access public
19
	 * @since 7.11.0
20
	 */
21
	public function __construct() {
22
		$this->title = __( 'Year in Review', 'google-analytics-for-wordpress' );
23
		parent::__construct();
24
	}
25
26
	/**
27
	 * Prepare report-specific data for output.
28
	 *
29
	 * @param array $data The data from the report before it gets sent to the frontend.
30
	 *
31
	 * @return mixed
32
	 */
33
	public function prepare_report_data( $data ) {
34
		// Add flags to the countries report.
35
		if ( ! empty( $data['data']['countries'] ) ) {
36
			$country_names = monsterinsights_get_country_list( true );
37
			foreach ( $data['data']['countries'] as $key => $country ) {
38
				$data['data']['countries'][ $key ]['name'] = isset( $country_names[ $country['iso'] ] ) ? $country_names[ $country['iso'] ] : $country['iso'];
39
			}
40
		}
41
42
		// Escape urls for the top pages report.
43
		if ( ! empty( $data['data']['toppages'] ) ) {
44
			foreach ( $data['data']['toppages'] as $key => $page ) {
45
				$title = $data['data']['toppages'][ $key ]['title'];
46
				$url   = '(not set)' === $title ? '' : esc_url( $data['data']['toppages'][ $key ]['hostname'] );
47
48
				$data['data']['toppages'][ $key ]['hostname'] = $url;
49
			}
50
		}
51
52
		// Add logged in user name
53
		$user_info = wp_get_current_user();
54
		$data['data']['user_name'] = '';
55
56
		if ( ! empty( $user_info->user_firstname ) ) {
57
			$first_name = $user_info->user_firstname;
58
59
			$data['data']['user_name'] = $first_name;
60
		}
61
62
		return $data;
63
	}
64
}
65