Completed
Push — develop ( 9593c1...a884d9 )
by David
03:51 queued 14s
created

Wordlift_Dashboard_Latest_News   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A render() 0 3 1
A get_last_wordlift_articles() 0 14 2
B get_wordlift_articles_data() 0 37 5
A add_utm_parameter() 0 8 2
A ajax_get_latest_news() 0 8 1
A add_dashboard_latest_news_widget() 0 6 1
1
<?php
2
/**
3
 * Services: Wordlift Latest News Dashboard Widget.
4
 *
5
 * Displays the WordLift Latest News Dashboard Widget.
6
 *
7
 * @since 3.19.0
8
 * @package Wordlift
9
 * @subpackage Wordlift/admin
10
 */
11
12
/**
13
 * Wordlift_Dashboard_Latest_News Class
14
 *
15
 * Handles the latest news dashboard widget.
16
 *
17
 * @since 3.19.0
18
 * @package Wordlift
19
 * @subpackage Wordlift/admin
20
 */
21
class Wordlift_Dashboard_Latest_News {
22
23
	/**
24
	 * Add needed hooks for the latest news widget.
25
	 */
26
	public function __construct() {
27
		add_action( 'wp_ajax_wordlift_get_latest_news', array(
28
			$this,
29
			'ajax_get_latest_news',
30
		) );
31
		add_action( 'wp_dashboard_setup', array(
32
			$this,
33
			'add_dashboard_latest_news_widget',
34
		) );
35
36
		add_action( 'admin_enqueue_scripts', function () {
37
			wp_enqueue_script( 'wl-admin-dashboard', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/wordlift-admin-dashboard.js', array( 'jquery' ) );
38
		} );
39
	}
40
41
	/**
42
	 * Return latest news html.
43
	 *
44
	 * @return string Articles html markup.
45
	 */
46
	public function render() {
47
		include( plugin_dir_path( __FILE__ ) . 'partials/wordlift-admin-news-widget.php' );
48
	}
49
50
	/**
51
	 * Returns latest news data filtered by $start_position.
52
	 *
53
	 * @param int $start_position (news array key start position).
54
	 *
55
	 * @return array Latest posts data.
56
	 */
57
	public function get_last_wordlift_articles( $start_position = 0 ) {
58
		$feed_articles = $this->get_wordlift_articles_data();
59
60
		// Filter articles by $start_position
61
		if ( ! empty( $feed_articles ) ) {
62
			return array(
63
				'posts_data'     => array_slice( $feed_articles, $start_position, 3 ),
64
				'count'          => count( $feed_articles ),
65
				'start_position' => $start_position,
66
			);
67
		}
68
69
		return false;
70
	}
71
72
	/**
73
	 * Returns latest news array data.
74
	 *
75
	 * @uses  https://codex.wordpress.org/Function_Reference/fetch_feed
76
	 * @uses  https://codex.wordpress.org/Function_Reference/get_locale
77
	 *
78
	 * @param int $articles_number (articles total number).
79
	 *
80
	 * @return array Latest $articles_number feed posts.
81
	 */
82
	public function get_wordlift_articles_data( $articles_number = 10 ) {
83
		// Init cache class
84
		$cache_sistem_lib = new Wordlift_File_Cache_Service( WL_TEMP_DIR . 'articles/' );
85
		$locale           = get_locale();
86
		$cache_id         = 'news_' . date( 'Y_m_d' ) . '_' . $locale;
87
		$posts_data       = array();
88
89
		// Get latest articles from cache
90
		$feed_articles = $cache_sistem_lib->get_cache( $cache_id );
91
		if ( false === $feed_articles ) {
92
			// Check wordpress installation language to define articles rss url
93
			$feed_uri = ( 'it_IT' === $locale ) ? 'https://wordlift.io/blog/it/feed/' : 'https://wordlift.io/blog/en/feed/';
94
95
			// Get rss feed data, the response is cached by default for 12 hours
96
			$feed = fetch_feed( $feed_uri );
97
			if ( ! is_wp_error( $feed ) ) {
98
				// Build an array of requested $articles_number, starting with element 0
99
				$feed_articles = $feed->get_items( 0, $articles_number );
100
				foreach ( $feed_articles as $key => $item ) {
101
					$data = array(
102
						// fetch_feed will return the title html decoded.
103
						'post_title'       => $item->get_title(),
104
						'post_date'        => $item->get_date( 'j F Y | g:i a' ),
105
						'post_url'         => self::add_utm_parameter( $item->get_permalink() ),
106
						// fetch_feed will return the description html (not decoded).
107
						'post_description' => $item->get_description(),
108
					);
109
					array_push( $posts_data, $data );
110
				}
111
				// Set articles in cache.
112
				$cache_sistem_lib->set_cache( $cache_id, $posts_data );
113
				$feed_articles = $posts_data;
114
			}
115
		}
116
117
		return $feed_articles;
118
	}
119
120
	/**
121
	 * Add the `utm` parameter for GA.
122
	 *
123
	 * @since 3.19.0
124
	 *
125
	 * @param string $url The URL.
126
	 *
127
	 * @return string The URL with the `utm` parameter prepended by `&` or by `?`.
128
	 */
129
	private static function add_utm_parameter( $url ) {
130
131
		if ( false === strpos( $url, '?' ) ) {
132
			return $url . '?utm=wl_dash';
133
		}
134
135
		return $url . '&utm=wl_dash';
136
	}
137
138
	/**
139
	 * Ajax call for more latest news.
140
	 *
141
	 * @uses  https://codex.wordpress.org/Function_Reference/wp_send_json_success
142
	 *
143
	 * @return string JSON obj with articles data.
144
	 */
145
	public function ajax_get_latest_news() {
0 ignored issues
show
Coding Style introduced by
ajax_get_latest_news uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
146
		// Get wordlift articles
147
		$start_position = explode( '_', $_POST['more_posts_link_id'] );
148
		$data           = $this->get_last_wordlift_articles( $start_position[ count( $start_position ) - 1 ] );
149
150
		// Return response as json object
151
		wp_send_json_success( $data );
152
	}
153
154
	/**
155
	 * Add latest news widget to the administration dashboard.
156
	 */
157
	public function add_dashboard_latest_news_widget() {
158
		wp_add_dashboard_widget( 'wordlift-dashboard-latest-news-widget', 'Latest WordLift News', array(
159
			$this,
160
			'render',
161
		) );
162
	}
163
164
}
165