Passed
Push — master ( 8161cf...b8b7ed )
by Chris
04:50
created

AM_Dashboard_Widget_Extend_Feed   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dashboard_items_count() 0 7 1
A dashboard_update_feed_urls() 0 23 3
A get_feed_urls() 0 26 4
A __construct() 0 7 1
1
<?php
2
3
if ( ! class_exists( 'AM_Dashboard_Widget_Extend_Feed' ) ) {
4
	/**
5
	 * Awesome Motive Events and News Feed.
6
	 *
7
	 * This appends additional blog feeds to the WordPress Events and News Feed Widget
8
	 * available in the WP-Admin Dashboard.
9
	 *
10
	 * @package    AwesomeMotive
11
	 * @author     AwesomeMotive Team
12
	 * @license    GPL-2.0+
13
	 * @copyright  Copyright (c) 2018, Awesome Motive LLC
14
	 * @version    1.0.0
15
	 */
16
	class AM_Dashboard_Widget_Extend_Feed {
17
18
		/**
19
		 * The number of feed items to show.
20
		 *
21
		 * @since 1.0.0
22
		 *
23
		 * @var int
24
		 */
25
		const FEED_COUNT = 6;
26
27
		/**
28
		 * Construct.
29
		 *
30
		 * @since 1.0.0
31
		 */
32
		public function __construct() {
33
34
			// Actions.
35
			add_action( 'wp_feed_options', array( $this, 'dashboard_update_feed_urls' ), 10, 2 );
36
37
			// Filters.
38
			add_filter( 'dashboard_secondary_items', array( $this, 'dashboard_items_count' ) );
39
		}
40
41
		/**
42
		 * Set the number of feed items to show.
43
		 *
44
		 * @since 1.0.0
45
		 *
46
		 * @return int Count of feed items.
47
		 */
48
		public function dashboard_items_count() {
49
50
			/**
51
			 * Apply the filters am_dashboard_feed_count for letting an admin
52
			 * override this count.
53
			 */
54
			return (int) apply_filters( 'am_dashboard_feed_count', self::FEED_COUNT );
55
		}
56
57
		/**
58
		 * Update the planet feed to add other AM blog feeds.
59
		 *
60
		 * @since 1.0.0
61
		 *
62
		 * @param object $feed SimplePie feed object (passed by reference).
63
		 * @param string $url URL of feed to retrieve (original planet feed url). If an array of URLs, the feeds are merged.
64
		 */
65
		public function dashboard_update_feed_urls( $feed, $url ) {
66
67
			global $pagenow;
68
69
			// Return early if not on the right page.
70
			if ( 'admin-ajax.php' !== $pagenow ) {
71
				return;
72
			}
73
74
			/**
75
			 * Return early if not on the right feed.
76
			 * We want to modify the feed URLs only for the
77
			 * WordPress Events & News Dashboard Widget
78
			 */
79
			if ( strpos( $url, 'planet.wordpress.org' ) === false ) {
80
				return;
81
			}
82
83
			// Build the feed sources.
84
			$all_feed_urls = $this->get_feed_urls( $url );
85
86
			// Update the feed sources.
87
			$feed->set_feed_url( $all_feed_urls );
88
		}
89
90
		/**
91
		 * Get the feed URLs for various active AM Products.
92
		 *
93
		 * @since 1.0.0
94
		 *
95
		 * @param string $url Planet Feed URL.
96
		 *
97
		 * @return array Array of Feed URLs.
98
		 */
99
		public function get_feed_urls( $url ) {
100
101
			// Initialize the feeds array.
102
			$feed_urls = array(
103
				'https://www.wpbeginner.com/feed/',
104
				'https://www.isitwp.com/feed/',
105
				$url,
106
			);
107
108
			// Check if MonsterInsights is active.
109
			if ( function_exists( 'MonsterInsights' ) ) {
110
				$feed_urls[] = 'https://www.monsterinsights.com/feed/';
111
			}
112
113
			// Check if WPForms is active.
114
			if ( class_exists( 'WPForms', false ) ) {
115
				$feed_urls[] = 'https://wpforms.com/feed/';
116
			}
117
118
			// Check if OptinMonster is active.
119
			if ( class_exists( 'OMAPI', false ) ) {
120
				$feed_urls[] = 'https://optinmonster.com/feed/';
121
			}
122
123
			// Return the feed URLs.
124
			return array_unique( $feed_urls );
125
		}
126
	}
127
128
	// Create an instance.
129
	new AM_Dashboard_Widget_Extend_Feed();
130
}
131