PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.2.2
Jetpack – WP Security, Backup, Speed, & Growth v10.2.2
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
jetpack / modules / widgets / wordpress-post-widget / class.jetpack-display-posts-widget.php

class.jetpack-display-posts-widget.php in Jetpack – WP Security, Backup, Speed, & Growth 10.2.2, at modules/widgets/wordpress-post-widget/class.jetpack-display-posts-widget.php

277 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Automattic\Jetpack\Status;
4
5 /*
6 * Display a list of recent posts from a WordPress.com or Jetpack-enabled blog.
7 */
8
9 class Jetpack_Display_Posts_Widget extends Jetpack_Display_Posts_Widget__Base {
10 /**
11 * @var string Widget options key prefix.
12 */
13 public $widget_options_key_prefix = 'display_posts_site_data_';
14
15 /**
16 * @var string The name of the cron that will update widget data.
17 */
18 public static $cron_name = 'jetpack_display_posts_widget_cron_update';
19
20
21 // DATA STORE
22
23 /**
24 * Gets blog data from the cache.
25 *
26 * @param string $site
27 *
28 * @return array|WP_Error
29 */
30 public function get_blog_data( $site ) {
31 // load from cache, if nothing return an error
32 $site_hash = $this->get_site_hash( $site );
33
34 $cached_data = $this->wp_get_option( $this->widget_options_key_prefix . $site_hash );
35
36 /**
37 * If the cache is empty, return an empty_cache error.
38 */
39 if ( false === $cached_data ) {
40 return new WP_Error(
41 'empty_cache',
42 __( 'Information about this blog is currently being retrieved.', 'jetpack' )
43 );
44 }
45
46 return $cached_data;
47
48 }
49
50 /**
51 * Update a widget instance.
52 *
53 * @param string $site The site to fetch the latest data for.
54 *
55 * @return array - the new data
56 */
57 public function update_instance( $site ) {
58
59 /**
60 * Fetch current information for a site.
61 */
62 $site_hash = $this->get_site_hash( $site );
63
64 $option_key = $this->widget_options_key_prefix . $site_hash;
65
66 $instance_data = $this->wp_get_option( $option_key );
67
68 /**
69 * Fetch blog data and save it in $instance_data.
70 */
71 $new_data = $this->fetch_blog_data( $site, $instance_data );
72
73 /**
74 * If the option doesn't exist yet - create a new option
75 */
76 if ( false === $instance_data ) {
77 $this->wp_add_option( $option_key, $new_data );
78 }
79 else {
80 $this->wp_update_option( $option_key, $new_data );
81 }
82
83 return $new_data;
84 }
85
86
87 // WIDGET API
88
89 public function update( $new_instance, $old_instance ) {
90 $instance = parent::update( $new_instance, $old_instance );
91
92 /**
93 * Forcefully activate the update cron when saving widget instance.
94 *
95 * So we can be sure that it will be running later.
96 */
97 $this->activate_cron();
98
99 return $instance;
100 }
101
102
103 // CRON
104
105 /**
106 * Activates widget update cron task.
107 */
108 public static function activate_cron() {
109 if ( ! wp_next_scheduled( self::$cron_name ) ) {
110 wp_schedule_event( time(), 'minutes_10', self::$cron_name );
111 }
112 }
113
114 /**
115 * Deactivates widget update cron task.
116 *
117 * This is a wrapper over the static method as it provides some syntactic sugar.
118 */
119 public function deactivate_cron() {
120 self::deactivate_cron_static();
121 }
122
123 /**
124 * Deactivates widget update cron task.
125 */
126 public static function deactivate_cron_static() {
127 $next_scheduled_time = wp_next_scheduled( self::$cron_name );
128 wp_unschedule_event( $next_scheduled_time, self::$cron_name );
129 }
130
131 /**
132 * Checks if the update cron should be running and returns appropriate result.
133 *
134 * @return bool If the cron should be running or not.
135 */
136 public function should_cron_be_running() {
137 /**
138 * The cron doesn't need to run empty loops.
139 */
140 $widget_instances = $this->get_instances_sites();
141
142 if ( empty( $widget_instances ) || ! is_array( $widget_instances ) ) {
143 return false;
144 }
145
146 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
147 /**
148 * If Jetpack is not active or in offline mode, we don't want to update widget data.
149 */
150 if ( ! Jetpack::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
151 return false;
152 }
153
154 /**
155 * If Extra Sidebar Widgets module is not active, we don't need to update widget data.
156 */
157 if ( ! Jetpack::is_module_active( 'widgets' ) ) {
158 return false;
159 }
160 }
161
162 /**
163 * If none of the above checks failed, then we definitely want to update widget data.
164 */
165 return true;
166 }
167
168 /**
169 * Main cron code. Updates all instances of the widget.
170 *
171 * @return bool
172 */
173 public function cron_task() {
174
175 /**
176 * If the cron should not be running, disable it.
177 */
178 if ( false === $this->should_cron_be_running() ) {
179 return true;
180 }
181
182 $instances_to_update = $this->get_instances_sites();
183
184 /**
185 * If no instances are found to be updated - stop.
186 */
187 if ( empty( $instances_to_update ) || ! is_array( $instances_to_update ) ) {
188 return true;
189 }
190
191 foreach ( $instances_to_update as $site_url ) {
192 $this->update_instance( $site_url );
193 }
194
195 return true;
196 }
197
198 /**
199 * Get a list of unique sites from all instances of the widget.
200 *
201 * @return array|bool
202 */
203 public function get_instances_sites() {
204
205 $widget_settings = $this->wp_get_option( 'widget_jetpack_display_posts_widget' );
206
207 /**
208 * If the widget still hasn't been added anywhere, the config will not be present.
209 *
210 * In such case we don't want to continue execution.
211 */
212 if ( false === $widget_settings || ! is_array( $widget_settings ) ) {
213 return false;
214 }
215
216 $urls = array();
217
218 foreach ( $widget_settings as $widget_instance_data ) {
219 if ( isset( $widget_instance_data['url'] ) && ! empty( $widget_instance_data['url'] ) ) {
220 $urls[] = $widget_instance_data['url'];
221 }
222 }
223
224 /**
225 * Make sure only unique URLs are returned.
226 */
227 $urls = array_unique( $urls );
228
229 return $urls;
230
231 }
232
233
234 // MOCKABLES
235
236 /**
237 * This is just to make method mocks in the unit tests easier.
238 *
239 * @param string $param Option key to get
240 *
241 * @return mixed
242 *
243 * @codeCoverageIgnore
244 */
245 public function wp_get_option( $param ) {
246 return get_option( $param );
247 }
248
249 /**
250 * This is just to make method mocks in the unit tests easier.
251 *
252 * @param string $option_name Option name to be added
253 * @param mixed $option_value Option value
254 *
255 * @return mixed
256 *
257 * @codeCoverageIgnore
258 */
259 public function wp_add_option( $option_name, $option_value ) {
260 return add_option( $option_name, $option_value );
261 }
262
263 /**
264 * This is just to make method mocks in the unit tests easier.
265 *
266 * @param string $option_name Option name to be updated
267 * @param mixed $option_value Option value
268 *
269 * @return mixed
270 *
271 * @codeCoverageIgnore
272 */
273 public function wp_update_option( $option_name, $option_value ) {
274 return update_option( $option_name, $option_value );
275 }
276 }
277