Passed
Push — master ( 02fe7f...406f6c )
by
unknown
10:53
created

MonsterInsights_Report_Site_Summary   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 62
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare_data() 0 30 4
A __construct() 0 5 1
A prepare_report_data() 0 2 1
1
<?php
2
/**
3
 * Site Summary Report
4
 *
5
 * Ensures all of the reports have a uniform class with helper functions.
6
 *
7
 * @since 6.0.0
8
 *
9
 * @package MonsterInsights
10
 * @subpackage Reports
11
 * @author  Andrei Lupu
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
final class MonsterInsights_Report_Site_Summary extends MonsterInsights_Report {
20
21
	public $title;
22
	public $class = 'MonsterInsights_Report_Site_Summary';
23
	public $name = 'site_summary';
24
	public $version = '1.0.0';
25
	public $level = 'lite';
26
27
	/**
28
	 * Primary class constructor.
29
	 *
30
	 * @access public
31
	 * @since 6.0.0
32
	 */
33
	public function __construct() {
34
		$this->title = __( 'Site Summary', 'google-analytics-for-wordpress' );
35
		parent::__construct();
36
37
		add_filter( 'monsterinsights_report_site_summary_data', array( $this, 'prepare_data' ) );
38
	}
39
40
	/**
41
	 * Prepare report-specific data for output.
42
	 *
43
	 * @param array $data The data from the report before it gets sent to the frontend.
44
	 *
45
	 * @return mixed
46
	 */
47
	public function prepare_report_data( $data ) {
48
		return apply_filters( 'monsterinsights_report_site_summary_data', $data );
49
	}
50
51
	public function prepare_data( $data ) {
52
53
		// Fill summary data with total number of posts, pages, and comments.
54
		if ( isset( $data['data']['summary'] ) ) {
55
			$posts = wp_count_posts('post');
56
			$data['data']['summary']['total_posts'] = $posts->publish;
57
			$pages = wp_count_posts('page');
58
			$data['data']['summary']['total_pages'] = $pages->publish;
59
			$comments = get_comment_count();
60
			$data['data']['summary']['total_comments'] = $comments['all'];
61
		}
62
63
		if ( ! empty( $data['data']['popular_post'] ) ) {
64
			// Get the thumbnail for the post.
65
			$post_id = url_to_postid( home_url() . $data['data']['popular_post']['url'] );
66
			$data['data']['popular_post']['id'] = $post_id;
67
			$data['data']['popular_post']['title'] = esc_html( get_the_title($post_id) );
68
			$data['data']['popular_post']['img'] = get_the_post_thumbnail_url( $post_id, 'thumbnail' );
69
		}
70
71
		if ( !empty( $data['data']['popular_page'] ) ) {
72
			// Compose the page url from homepage and the path we fetch from GA.
73
			$data['data']['popular_page']['url'] = esc_url( home_url() . $data['data']['popular_page']['url'] );
74
			// Get the thumbnail for the post.
75
			$post_id = url_to_postid( $data['data']['popular_page']['url'] );
76
			$data['data']['popular_page']['id'] = $post_id;
77
			$data['data']['popular_page']['img'] = get_the_post_thumbnail_url( $post_id, 'thumbnail' );
78
		}
79
80
		return $data;
81
	}
82
}
83