Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Display_Posts_Widget often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Display_Posts_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 122 | class Jetpack_Display_Posts_Widget extends WP_Widget { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var string Remote service API URL prefix. |
||
| 126 | */ |
||
| 127 | public $service_url = 'https://public-api.wordpress.com/rest/v1.1/'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var string Widget options key prefix. |
||
| 131 | */ |
||
| 132 | public $widget_options_key_prefix = 'display_posts_site_data_'; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @var string The name of the cron that will update widget data. |
||
| 136 | */ |
||
| 137 | public static $cron_name = 'jetpack_display_posts_widget_cron_update'; |
||
| 138 | |||
| 139 | |||
| 140 | public function __construct() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Expiring transients have a name length maximum of 45 characters, |
||
| 159 | * so this function returns an abbreviated MD5 hash to use instead of |
||
| 160 | * the full URI. |
||
| 161 | * |
||
| 162 | * @param string $site Site to get the hash for. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function get_site_hash( $site ) { |
||
| 167 | return substr( md5( $site ), 0, 21 ); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Fetch a remote service endpoint and parse it. |
||
| 172 | * |
||
| 173 | * Timeout is set to 15 seconds right now, because sometimes the WordPress API |
||
| 174 | * takes more than 5 seconds to fully respond. |
||
| 175 | * |
||
| 176 | * Caching is used here so we can avoid re-downloading the same endpoint |
||
| 177 | * in a single request. |
||
| 178 | * |
||
| 179 | * @param string $endpoint Parametrized endpoint to call. |
||
| 180 | * |
||
| 181 | * @param int $timeout How much time to wait for the API to respond before failing. |
||
| 182 | * |
||
| 183 | * @return array|WP_Error |
||
| 184 | */ |
||
| 185 | public function fetch_service_endpoint( $endpoint, $timeout = 15 ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Holds endpoint request cache. |
||
| 189 | */ |
||
| 190 | static $cache = array(); |
||
| 191 | |||
| 192 | if ( ! isset( $cache[ $endpoint ] ) ) { |
||
| 193 | $raw_data = $this->wp_wp_remote_get( $this->service_url . ltrim( $endpoint, '/' ), array( 'timeout' => $timeout ) ); |
||
| 194 | $cache[ $endpoint ] = $this->parse_service_response( $raw_data ); |
||
| 195 | } |
||
| 196 | |||
| 197 | return $cache[ $endpoint ]; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Parse data from service response. |
||
| 202 | * Do basic error handling for general service and data errors |
||
| 203 | * |
||
| 204 | * @param array $service_response Response from the service. |
||
| 205 | * |
||
| 206 | * @return array|WP_Error |
||
| 207 | */ |
||
| 208 | public function parse_service_response( $service_response ) { |
||
| 209 | /** |
||
| 210 | * If there is an error, we add the error message to the parsed response |
||
| 211 | */ |
||
| 212 | if ( is_wp_error( $service_response ) ) { |
||
| 213 | return new WP_Error( |
||
| 214 | 'general_error', |
||
| 215 | __( 'An error occurred fetching the remote data.', 'jetpack' ), |
||
| 216 | $service_response->get_error_messages() |
||
|
|
|||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Validate HTTP response code. |
||
| 222 | */ |
||
| 223 | if ( 200 !== wp_remote_retrieve_response_code( $service_response ) ) { |
||
| 224 | return new WP_Error( |
||
| 225 | 'http_error', |
||
| 226 | __( 'An error occurred fetching the remote data.', 'jetpack' ), |
||
| 227 | wp_remote_retrieve_response_message( $service_response ) |
||
| 228 | ); |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * Extract service response body from the request. |
||
| 234 | */ |
||
| 235 | |||
| 236 | $service_response_body = wp_remote_retrieve_body( $service_response ); |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * No body has been set in the response. This should be pretty bad. |
||
| 241 | */ |
||
| 242 | if ( ! $service_response_body ) { |
||
| 243 | return new WP_Error( |
||
| 244 | 'no_body', |
||
| 245 | __( 'Invalid remote response.', 'jetpack' ), |
||
| 246 | 'No body in response.' |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Parse the JSON response from the API. Convert to associative array. |
||
| 252 | */ |
||
| 253 | $parsed_data = json_decode( $service_response_body ); |
||
| 254 | |||
| 255 | /** |
||
| 256 | * If there is a problem with parsing the posts return an empty array. |
||
| 257 | */ |
||
| 258 | if ( is_null( $parsed_data ) ) { |
||
| 259 | return new WP_Error( |
||
| 260 | 'no_body', |
||
| 261 | __( 'Invalid remote response.', 'jetpack' ), |
||
| 262 | 'Invalid JSON from remote.' |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Check for errors in the parsed body. |
||
| 268 | */ |
||
| 269 | if ( isset( $parsed_data->error ) ) { |
||
| 270 | return new WP_Error( |
||
| 271 | 'remote_error', |
||
| 272 | __( 'We cannot display information for this blog.', 'jetpack' ), |
||
| 273 | $parsed_data->error |
||
| 274 | ); |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * No errors found, return parsed data. |
||
| 280 | */ |
||
| 281 | return $parsed_data; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Fetch site information from the WordPress public API |
||
| 286 | * |
||
| 287 | * @param string $site URL of the site to fetch the information for. |
||
| 288 | * |
||
| 289 | * @return array|WP_Error |
||
| 290 | */ |
||
| 291 | public function fetch_site_info( $site ) { |
||
| 292 | |||
| 293 | $response = $this->fetch_service_endpoint( sprintf( '/sites/%s', urlencode( $site ) ) ); |
||
| 294 | |||
| 295 | return $response; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Parse external API response from the site info call and handle errors if they occur. |
||
| 300 | * |
||
| 301 | * @param array|WP_Error $service_response The raw response to be parsed. |
||
| 302 | * |
||
| 303 | * @return array|WP_Error |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function parse_site_info_response( $service_response ) { |
|
| 306 | |||
| 307 | /** |
||
| 308 | * If the service returned an error, we pass it on. |
||
| 309 | */ |
||
| 310 | if ( is_wp_error( $service_response ) ) { |
||
| 311 | return $service_response; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Check if the service returned proper site information. |
||
| 316 | */ |
||
| 317 | if ( ! isset( $service_response->ID ) ) { |
||
| 318 | return new WP_Error( |
||
| 319 | 'no_site_info', |
||
| 320 | __( 'Invalid site information returned from remote.', 'jetpack' ), |
||
| 321 | 'No site ID present in the response.' |
||
| 322 | ); |
||
| 323 | } |
||
| 324 | |||
| 325 | return $service_response; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Fetch list of posts from the WordPress public API. |
||
| 330 | * |
||
| 331 | * @param int $site_id The site to fetch the posts for. |
||
| 332 | * |
||
| 333 | * @return array|WP_Error |
||
| 334 | */ |
||
| 335 | public function fetch_posts_for_site( $site_id ) { |
||
| 336 | |||
| 337 | $response = $this->fetch_service_endpoint( |
||
| 338 | sprintf( |
||
| 339 | '/sites/%1$d/posts/%2$s', |
||
| 340 | $site_id, |
||
| 341 | /** |
||
| 342 | * Filters the parameters used to fetch for posts in the Display Posts Widget. |
||
| 343 | * |
||
| 344 | * @see https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/posts/ |
||
| 345 | * |
||
| 346 | * @module widgets |
||
| 347 | * |
||
| 348 | * @since 3.6.0 |
||
| 349 | * |
||
| 350 | * @param string $args Extra parameters to filter posts returned from the WordPress.com REST API. |
||
| 351 | */ |
||
| 352 | apply_filters( 'jetpack_display_posts_widget_posts_params', '' ) |
||
| 353 | ) |
||
| 354 | ); |
||
| 355 | |||
| 356 | return $response; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Parse external API response from the posts list request and handle errors if any occur. |
||
| 361 | * |
||
| 362 | * @param object|WP_Error $service_response The raw response to be parsed. |
||
| 363 | * |
||
| 364 | * @return array|WP_Error |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function parse_posts_response( $service_response ) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * If the service returned an error, we pass it on. |
||
| 370 | */ |
||
| 371 | if ( is_wp_error( $service_response ) ) { |
||
| 372 | return $service_response; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Check if the service returned proper posts array. |
||
| 377 | */ |
||
| 378 | if ( ! isset( $service_response->posts ) || ! is_array( $service_response->posts ) ) { |
||
| 379 | return new WP_Error( |
||
| 380 | 'no_posts', |
||
| 381 | __( 'No posts data returned by remote.', 'jetpack' ), |
||
| 382 | 'No posts information set in the returned data.' |
||
| 383 | ); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Format the posts to preserve storage space. |
||
| 388 | */ |
||
| 389 | |||
| 390 | return $this->format_posts_for_storage( $service_response ); |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Format the posts for better storage. Drop all the data that is not used. |
||
| 395 | * |
||
| 396 | * @param object $parsed_data Array of posts returned by the APIs. |
||
| 397 | * |
||
| 398 | * @return array Formatted posts or an empty array if no posts were found. |
||
| 399 | */ |
||
| 400 | public function format_posts_for_storage( $parsed_data ) { |
||
| 401 | |||
| 402 | $formatted_posts = array(); |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Only go through the posts list if we have valid posts array. |
||
| 406 | */ |
||
| 407 | if ( isset( $parsed_data->posts ) && is_array( $parsed_data->posts ) ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Loop through all the posts and format them appropriately. |
||
| 411 | */ |
||
| 412 | foreach ( $parsed_data->posts as $single_post ) { |
||
| 413 | |||
| 414 | $prepared_post = array( |
||
| 415 | 'title' => $single_post->title ? $single_post->title : '', |
||
| 416 | 'excerpt' => $single_post->excerpt ? $single_post->excerpt : '', |
||
| 417 | 'featured_image' => $single_post->featured_image ? $single_post->featured_image : '', |
||
| 418 | 'url' => $single_post->URL, |
||
| 419 | ); |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Append the formatted post to the results. |
||
| 423 | */ |
||
| 424 | $formatted_posts[] = $prepared_post; |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | return $formatted_posts; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Fetch site information and posts list for a site. |
||
| 433 | * |
||
| 434 | * @param string $site Site to fetch the data for. |
||
| 435 | * @param array $original_data Optional original data to updated. |
||
| 436 | * |
||
| 437 | * @param bool $site_data_only Fetch only site information, skip posts list. |
||
| 438 | * |
||
| 439 | * @return array Updated or new data. |
||
| 440 | */ |
||
| 441 | public function fetch_blog_data( $site, $original_data = array(), $site_data_only = false ) { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * If no optional data is supplied, initialize a new structure |
||
| 445 | */ |
||
| 446 | if ( ! empty( $original_data ) ) { |
||
| 447 | $widget_data = $original_data; |
||
| 448 | } |
||
| 449 | else { |
||
| 450 | $widget_data = array( |
||
| 451 | 'site_info' => array( |
||
| 452 | 'last_check' => null, |
||
| 453 | 'last_update' => null, |
||
| 454 | 'error' => null, |
||
| 455 | 'data' => array(), |
||
| 456 | ), |
||
| 457 | 'posts' => array( |
||
| 458 | 'last_check' => null, |
||
| 459 | 'last_update' => null, |
||
| 460 | 'error' => null, |
||
| 461 | 'data' => array(), |
||
| 462 | ) |
||
| 463 | ); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Update check time and fetch site information. |
||
| 468 | */ |
||
| 469 | $widget_data['site_info']['last_check'] = time(); |
||
| 470 | |||
| 471 | $site_info_raw_data = $this->fetch_site_info( $site ); |
||
| 472 | $site_info_parsed_data = $this->parse_site_info_response( $site_info_raw_data ); |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * If there is an error with the fetched site info, save the error and update the checked time. |
||
| 477 | */ |
||
| 478 | View Code Duplication | if ( is_wp_error( $site_info_parsed_data ) ) { |
|
| 479 | $widget_data['site_info']['error'] = $site_info_parsed_data; |
||
| 480 | |||
| 481 | return $widget_data; |
||
| 482 | } |
||
| 483 | /** |
||
| 484 | * If data is fetched successfully, update the data and set the proper time. |
||
| 485 | * |
||
| 486 | * Data is only updated if we have valid results. This is done this way so we can show |
||
| 487 | * something if external service is down. |
||
| 488 | * |
||
| 489 | */ |
||
| 490 | else { |
||
| 491 | $widget_data['site_info']['last_update'] = time(); |
||
| 492 | $widget_data['site_info']['data'] = $site_info_parsed_data; |
||
| 493 | $widget_data['site_info']['error'] = null; |
||
| 494 | } |
||
| 495 | |||
| 496 | |||
| 497 | /** |
||
| 498 | * If only site data is needed, return it here, don't fetch posts data. |
||
| 499 | */ |
||
| 500 | if ( true === $site_data_only ) { |
||
| 501 | return $widget_data; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Update check time and fetch posts list. |
||
| 506 | */ |
||
| 507 | $widget_data['posts']['last_check'] = time(); |
||
| 508 | |||
| 509 | $site_posts_raw_data = $this->fetch_posts_for_site( $site_info_parsed_data->ID ); |
||
| 510 | $site_posts_parsed_data = $this->parse_posts_response( $site_posts_raw_data ); |
||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * If there is an error with the fetched posts, save the error and update the checked time. |
||
| 515 | */ |
||
| 516 | View Code Duplication | if ( is_wp_error( $site_posts_parsed_data ) ) { |
|
| 517 | $widget_data['posts']['error'] = $site_posts_parsed_data; |
||
| 518 | |||
| 519 | return $widget_data; |
||
| 520 | } |
||
| 521 | /** |
||
| 522 | * If data is fetched successfully, update the data and set the proper time. |
||
| 523 | * |
||
| 524 | * Data is only updated if we have valid results. This is done this way so we can show |
||
| 525 | * something if external service is down. |
||
| 526 | * |
||
| 527 | */ |
||
| 528 | else { |
||
| 529 | $widget_data['posts']['last_update'] = time(); |
||
| 530 | $widget_data['posts']['data'] = $site_posts_parsed_data; |
||
| 531 | $widget_data['posts']['error'] = null; |
||
| 532 | } |
||
| 533 | |||
| 534 | return $widget_data; |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Gets blog data from the cache. |
||
| 539 | * |
||
| 540 | * @param string $site |
||
| 541 | * |
||
| 542 | * @return array|WP_Error |
||
| 543 | */ |
||
| 544 | public function get_blog_data( $site ) { |
||
| 545 | // load from cache, if nothing return an error |
||
| 546 | $site_hash = $this->get_site_hash( $site ); |
||
| 547 | |||
| 548 | $cached_data = $this->wp_get_option( $this->widget_options_key_prefix . $site_hash ); |
||
| 549 | |||
| 550 | /** |
||
| 551 | * If the cache is empty, return an empty_cache error. |
||
| 552 | */ |
||
| 553 | if ( false === $cached_data ) { |
||
| 554 | return new WP_Error( |
||
| 555 | 'empty_cache', |
||
| 556 | __( 'Information about this blog is currently being retrieved.', 'jetpack' ) |
||
| 557 | ); |
||
| 558 | } |
||
| 559 | |||
| 560 | return $cached_data; |
||
| 561 | |||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Activates widget update cron task. |
||
| 566 | */ |
||
| 567 | public static function activate_cron() { |
||
| 568 | if ( ! wp_next_scheduled( self::$cron_name ) ) { |
||
| 569 | wp_schedule_event( time(), 'minutes_10', self::$cron_name ); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Deactivates widget update cron task. |
||
| 575 | * |
||
| 576 | * This is a wrapper over the static method as it provides some syntactic sugar. |
||
| 577 | */ |
||
| 578 | public function deactivate_cron() { |
||
| 579 | self::deactivate_cron_static(); |
||
| 580 | } |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Deactivates widget update cron task. |
||
| 584 | */ |
||
| 585 | public static function deactivate_cron_static() { |
||
| 586 | $next_scheduled_time = wp_next_scheduled( self::$cron_name ); |
||
| 587 | wp_unschedule_event( $next_scheduled_time, self::$cron_name ); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Checks if the update cron should be running and returns appropriate result. |
||
| 592 | * |
||
| 593 | * @return bool If the cron should be running or not. |
||
| 594 | */ |
||
| 595 | public function should_cron_be_running() { |
||
| 596 | /** |
||
| 597 | * The cron doesn't need to run empty loops. |
||
| 598 | */ |
||
| 599 | $widget_instances = $this->get_instances_sites(); |
||
| 600 | |||
| 601 | if ( empty( $widget_instances ) || ! is_array( $widget_instances ) ) { |
||
| 602 | return false; |
||
| 603 | } |
||
| 604 | |||
| 605 | if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { |
||
| 606 | /** |
||
| 607 | * If Jetpack is not active or in development mode, we don't want to update widget data. |
||
| 608 | */ |
||
| 609 | if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
| 610 | return false; |
||
| 611 | } |
||
| 612 | |||
| 613 | /** |
||
| 614 | * If Extra Sidebar Widgets module is not active, we don't need to update widget data. |
||
| 615 | */ |
||
| 616 | if ( ! Jetpack::is_module_active( 'widgets' ) ) { |
||
| 617 | return false; |
||
| 618 | } |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * If none of the above checks failed, then we definitely want to update widget data. |
||
| 623 | */ |
||
| 624 | return true; |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Main cron code. Updates all instances of the widget. |
||
| 629 | * |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | public function cron_task() { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * If the cron should not be running, disable it. |
||
| 636 | */ |
||
| 637 | if ( false === $this->should_cron_be_running() ) { |
||
| 638 | return true; |
||
| 639 | } |
||
| 640 | |||
| 641 | $instances_to_update = $this->get_instances_sites(); |
||
| 642 | |||
| 643 | /** |
||
| 644 | * If no instances are found to be updated - stop. |
||
| 645 | */ |
||
| 646 | if ( empty( $instances_to_update ) || ! is_array( $instances_to_update ) ) { |
||
| 647 | return true; |
||
| 648 | } |
||
| 649 | |||
| 650 | foreach ( $instances_to_update as $site_url ) { |
||
| 651 | $this->update_instance( $site_url ); |
||
| 652 | } |
||
| 653 | |||
| 654 | return true; |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Get a list of unique sites from all instances of the widget. |
||
| 659 | * |
||
| 660 | * @return array|bool |
||
| 661 | */ |
||
| 662 | public function get_instances_sites() { |
||
| 663 | |||
| 664 | $widget_settings = $this->wp_get_option( 'widget_jetpack_display_posts_widget' ); |
||
| 665 | |||
| 666 | /** |
||
| 667 | * If the widget still hasn't been added anywhere, the config will not be present. |
||
| 668 | * |
||
| 669 | * In such case we don't want to continue execution. |
||
| 670 | */ |
||
| 671 | if ( false === $widget_settings || ! is_array( $widget_settings ) ) { |
||
| 672 | return false; |
||
| 673 | } |
||
| 674 | |||
| 675 | $urls = array(); |
||
| 676 | |||
| 677 | foreach ( $widget_settings as $widget_instance_data ) { |
||
| 678 | if ( isset( $widget_instance_data['url'] ) && ! empty( $widget_instance_data['url'] ) ) { |
||
| 679 | $urls[] = $widget_instance_data['url']; |
||
| 680 | } |
||
| 681 | } |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Make sure only unique URLs are returned. |
||
| 685 | */ |
||
| 686 | $urls = array_unique( $urls ); |
||
| 687 | |||
| 688 | return $urls; |
||
| 689 | |||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Update a widget instance. |
||
| 694 | * |
||
| 695 | * @param string $site The site to fetch the latest data for. |
||
| 696 | */ |
||
| 697 | public function update_instance( $site ) { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Fetch current information for a site. |
||
| 701 | */ |
||
| 702 | $site_hash = $this->get_site_hash( $site ); |
||
| 703 | |||
| 704 | $option_key = $this->widget_options_key_prefix . $site_hash; |
||
| 705 | |||
| 706 | $instance_data = $this->wp_get_option( $option_key ); |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Fetch blog data and save it in $instance_data. |
||
| 710 | */ |
||
| 711 | $new_data = $this->fetch_blog_data( $site, $instance_data ); |
||
| 712 | |||
| 713 | /** |
||
| 714 | * If the option doesn't exist yet - create a new option |
||
| 715 | */ |
||
| 716 | if ( false === $instance_data ) { |
||
| 717 | $this->wp_add_option( $option_key, $new_data ); |
||
| 718 | } |
||
| 719 | else { |
||
| 720 | $this->wp_update_option( $option_key, $new_data ); |
||
| 721 | } |
||
| 722 | } |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Set up the widget display on the front end. |
||
| 726 | * |
||
| 727 | * @param array $args |
||
| 728 | * @param array $instance |
||
| 729 | */ |
||
| 730 | public function widget( $args, $instance ) { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Scan and extract first error from blog data array. |
||
| 814 | * |
||
| 815 | * @param array|WP_Error $blog_data Blog data to scan for errors. |
||
| 816 | * |
||
| 817 | * @return string First error message found |
||
| 818 | */ |
||
| 819 | public function extract_errors_from_blog_data( $blog_data ) { |
||
| 820 | |||
| 821 | $errors = array( |
||
| 822 | 'message' => '', |
||
| 823 | 'debug' => '', |
||
| 824 | 'where' => '', |
||
| 825 | ); |
||
| 826 | |||
| 827 | |||
| 828 | /** |
||
| 829 | * When the cache result is an error. Usually when the cache is empty. |
||
| 830 | * This is not an error case for now. |
||
| 831 | */ |
||
| 832 | if ( is_wp_error( $blog_data ) ) { |
||
| 833 | return $errors; |
||
| 834 | } |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Loop through `site_info` and `posts` keys of $blog_data. |
||
| 838 | */ |
||
| 839 | foreach ( array( 'site_info', 'posts' ) as $info_key ) { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Contains information on which stage the error ocurred. |
||
| 843 | */ |
||
| 844 | $errors['where'] = $info_key; |
||
| 845 | |||
| 846 | /** |
||
| 847 | * If an error is set, we want to check it for usable messages. |
||
| 848 | */ |
||
| 849 | if ( isset( $blog_data[ $info_key ]['error'] ) && ! empty( $blog_data[ $info_key ]['error'] ) ) { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Extract error message from the error, if possible. |
||
| 853 | */ |
||
| 854 | if ( is_wp_error( $blog_data[ $info_key ]['error'] ) ) { |
||
| 855 | /** |
||
| 856 | * In the case of WP_Error we want to have the error message |
||
| 857 | * and the debug information available. |
||
| 858 | */ |
||
| 859 | $error_messages = $blog_data[ $info_key ]['error']->get_error_messages(); |
||
| 860 | $errors['message'] = reset( $error_messages ); |
||
| 861 | |||
| 862 | $extra_data = $blog_data[ $info_key ]['error']->get_error_data(); |
||
| 863 | if ( is_array( $extra_data ) ) { |
||
| 864 | $errors['debug'] = implode( '; ', $extra_data ); |
||
| 865 | } |
||
| 866 | else { |
||
| 867 | $errors['debug'] = $extra_data; |
||
| 868 | } |
||
| 869 | |||
| 870 | break; |
||
| 871 | } |
||
| 872 | elseif ( is_array( $blog_data[ $info_key ]['error'] ) ) { |
||
| 873 | /** |
||
| 874 | * In this case we don't have debug information, because |
||
| 875 | * we have no way to know the format. The widget works with |
||
| 876 | * WP_Error objects only. |
||
| 877 | */ |
||
| 878 | $errors['message'] = reset( $blog_data[ $info_key ]['error'] ); |
||
| 879 | break; |
||
| 880 | } |
||
| 881 | |||
| 882 | /** |
||
| 883 | * We do nothing if no usable error is found. |
||
| 884 | */ |
||
| 885 | } |
||
| 886 | } |
||
| 887 | |||
| 888 | return $errors; |
||
| 889 | } |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Enqueue CSS and JavaScript. |
||
| 893 | * |
||
| 894 | * @since 4.0.0 |
||
| 895 | */ |
||
| 896 | public function enqueue_scripts() { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Display the widget administration form. |
||
| 902 | * |
||
| 903 | * @param array $instance Widget instance configuration. |
||
| 904 | * |
||
| 905 | * @return string|void |
||
| 906 | */ |
||
| 907 | public function form( $instance ) { |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Initialize widget configuration variables. |
||
| 911 | */ |
||
| 912 | $title = ( isset( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', 'jetpack' ); |
||
| 913 | $url = ( isset( $instance['url'] ) ) ? $instance['url'] : ''; |
||
| 914 | $number_of_posts = ( isset( $instance['number_of_posts'] ) ) ? $instance['number_of_posts'] : 5; |
||
| 915 | $open_in_new_window = ( isset( $instance['open_in_new_window'] ) ) ? $instance['open_in_new_window'] : false; |
||
| 916 | $featured_image = ( isset( $instance['featured_image'] ) ) ? $instance['featured_image'] : false; |
||
| 917 | $show_excerpts = ( isset( $instance['show_excerpts'] ) ) ? $instance['show_excerpts'] : false; |
||
| 1035 | |||
| 1036 | public function update( $new_instance, $old_instance ) { |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * This is just to make method mocks in the unit tests easier. |
||
| 1089 | * |
||
| 1090 | * @param string $param Option key to get |
||
| 1091 | * |
||
| 1092 | * @return mixed |
||
| 1093 | * |
||
| 1094 | * @codeCoverageIgnore |
||
| 1095 | */ |
||
| 1096 | public function wp_get_option( $param ) { |
||
| 1099 | |||
| 1100 | /** |
||
| 1101 | * This is just to make method mocks in the unit tests easier. |
||
| 1102 | * |
||
| 1103 | * @param string $option_name Option name to be added |
||
| 1104 | * @param mixed $option_value Option value |
||
| 1105 | * |
||
| 1106 | * @return mixed |
||
| 1107 | * |
||
| 1108 | * @codeCoverageIgnore |
||
| 1109 | */ |
||
| 1110 | public function wp_add_option( $option_name, $option_value ) { |
||
| 1113 | |||
| 1114 | /** |
||
| 1115 | * This is just to make method mocks in the unit tests easier. |
||
| 1116 | * |
||
| 1117 | * @param string $option_name Option name to be updated |
||
| 1118 | * @param mixed $option_value Option value |
||
| 1119 | * |
||
| 1120 | * @return mixed |
||
| 1121 | * |
||
| 1122 | * @codeCoverageIgnore |
||
| 1123 | */ |
||
| 1124 | public function wp_update_option( $option_name, $option_value ) { |
||
| 1127 | |||
| 1128 | |||
| 1129 | /** |
||
| 1130 | * This is just to make method mocks in the unit tests easier. |
||
| 1131 | * |
||
| 1132 | * @param string $url The URL to fetch |
||
| 1133 | * @param array $args Optional. Request arguments. |
||
| 1134 | * |
||
| 1135 | * @return array|WP_Error |
||
| 1136 | * |
||
| 1137 | * @codeCoverageIgnore |
||
| 1138 | */ |
||
| 1139 | public function wp_wp_remote_get( $url, $args = array() ) { |
||
| 1142 | } |
||
| 1143 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.