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

get_notification_data()   C

Complexity

Conditions 16
Paths 32

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 23
nc 32
nop 0
dl 0
loc 40
rs 5.5666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Add audience notification
5
 * Recurrence: 30 Days
6
 *
7
 * @since 7.12.3
8
 */
9
final class MonsterInsights_Notification_Audience extends MonsterInsights_Notification_Event {
10
11
	public $notification_id             = 'monsterinsights_notification_audience';
12
	public $notification_interval       = 30; // in days
13
	public $notification_type           = array( 'basic', 'lite', 'master', 'plus', 'pro' );
14
15
	/**
16
	 * Add report to notifications
17
	 *
18
	 * @since 7.12.3
19
	 */
20
	public function get_notification_data() {
21
		require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
22
23
		$data                       = array();
24
		$report                     = $this->get_report();
25
		$sessions                   = isset( $report['data']['infobox']['sessions']['value'] ) ? $report['data']['infobox']['sessions']['value'] : 0;
26
		$countries                  = isset( $report['data']['countries'] ) ? $report['data']['countries'] : 0;
27
		$english_speaking_countries = monsterinsights_get_english_speaking_countries();
28
29
		if ( $sessions > 0 && is_array( $countries ) && ! empty( $countries ) ) {
30
			foreach ( $countries as $country ) {
31
				if ( empty( $country['iso'] ) || array_key_exists( $country['iso'], $english_speaking_countries ) ) {
32
					continue;
33
				}
34
35
				if ( $country['sessions'] > 0 ) {
36
					// get the country's session percentage by comparing with the total sessions
37
					$country_session_percentage = round( $country['sessions'] * 100 / $sessions );
38
39
					if ( $country_session_percentage < 15 ) {
40
						continue;
41
					}
42
43
					$site_language = get_locale();
44
					$translations  = wp_get_available_translations();
45
46
					if ( is_array( $translations ) && ! empty( $translations ) ) {
47
						$site_iso = isset( $translations[ $site_language ]['iso'] ) ? $translations[ $site_language ]['iso'] : array(); // keep empty array, because site language has no iso setup for en_US language
48
49
						if ( is_array( $site_iso ) && ! in_array( $country['iso'], $site_iso ) ) {
50
							$data['country']    = $country['name'];
51
							$data['percentage'] = $country_session_percentage;
52
							break;
53
						}
54
					}
55
				}
56
			}
57
		}
58
59
		return $data;
60
	}
61
62
	/**
63
	 * Build Notification
64
	 *
65
	 * @param   array  $notification
66
	 * @param   array  $data
67
	 *
68
	 * @return array $notification notification is ready to add
69
	 *
70
	 * @since 7.12.3
71
	 */
72
	public function prepare_notification_data( $notification ) {
73
		$data = $this->get_notification_data();
74
75
		if ( ! is_array( $data ) || empty( $data ) ) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
76
			return false;
77
		}
78
79
		// Translators: Audience notification title
80
		$notification['title']   = sprintf( __( '%s%% of your Audience is from %s', 'google-analytics-for-wordpress' ), $data['percentage'], $data['country'] );
81
		// Translators: Audience notification content
82
		$notification['content'] = sprintf( __( 'Is your site properly translated? By adding translated content specific to your audience you could gain big boosts in pageviews, time spent on page and a reduced bounce rate.<br><br>If you need help choosing a translation plugin to get you started take a look at %sthis article%s for the best options available.', 'google-analytics-for-wordpress' ), '<a href="'. $this->build_external_link('https://www.wpbeginner.com/showcase/9-best-translation-plugins-for-wordpress-websites/' ) .'" target="_blank">', '</a>' );
83
		$notification['btns']    = array(
84
			"view_report" => array(
85
				'url'  => $this->get_view_url(),
86
				'text' => __( 'View Report', 'google-analytics-for-wordpress' )
87
			),
88
			"learn_more"  => array(
89
				'url'  => $this->build_external_link( 'https://www.wpbeginner.com/showcase/9-best-translation-plugins-for-wordpress-websites/' ),
90
				'text' => __( 'Learn More', 'google-analytics-for-wordpress' )
91
			),
92
		);
93
94
		return $notification;
95
	}
96
97
}
98
99
// initiate the class
100
new MonsterInsights_Notification_Audience();
101