| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Traits; |
| 9 | 9 | |
| 10 | 10 | use WP_Error; |
| 11 | -use WP_Defender\Component\Quarantine as Quarantine_Component; | |
| 11 | +use WP_Defender\Model\Scan_Item; | |
| 12 | 12 | use WP_Filesystem_Base; |
| 13 | 13 | |
| 14 | 14 | trait Plugin { |
| 15 | 15 | |
| @@ -118,9 +118,9 @@ | ||
| 118 | 118 | public function ping_wp_org_by_plugin_slug( string $slug ) { |
| 119 | 119 | $url = 'https://api.wordpress.org/plugins/info/1.0/' . $slug . '.json'; |
| 120 | 120 | $http_args = array( |
| 121 | 121 | 'timeout' => 15, |
| 122 | - 'sslverify' => false, // Many hosts have no updated CA bundle. | |
| 122 | + 'sslverify' => true, | |
| 123 | 123 | 'user-agent' => defender_get_own_user_agent(), |
| 124 | 124 | ); |
| 125 | 125 | |
| 126 | 126 | return wp_remote_get( $url, $http_args ); |
| @@ -473,23 +473,17 @@ | ||
| 473 | 473 | |
| 474 | 474 | /** |
| 475 | 475 | * Quarantine a plugin. |
| 476 | 476 | * |
| 477 | - * @param string $parent_action Parent action. | |
| 477 | + * @param string $parent_action Parent action. | |
| 478 | + * @param Scan_Item $scan_item Scan item model object. | |
| 478 | 479 | * |
| 479 | 480 | * @return array|WP_Error |
| 480 | 481 | */ |
| 481 | - public function quarantine( string $parent_action ) { | |
| 482 | - if ( ! class_exists( 'WP_Defender\Controller\Quarantine' ) ) { | |
| 483 | - return new WP_Error( | |
| 484 | - 'DEFENDER_PRO_ONLY_FEATURE', | |
| 485 | - defender_quarantine_pro_only() | |
| 486 | - ); | |
| 487 | - } | |
| 482 | + public function quarantine( string $parent_action, Scan_Item $scan_item ) { | |
| 483 | + $quarantine_component = wd_di()->get( \WP_Defender\Component\Quarantine::class ); | |
| 488 | 484 | |
| 489 | - $quarantine_component = wd_di()->get( Quarantine_Component::class ); | |
| 490 | - | |
| 491 | - return $quarantine_component->quarantine_file( $this->owner, $parent_action ); | |
| 485 | + return $quarantine_component->quarantine_file( $scan_item, $parent_action ); | |
| 492 | 486 | } |
| 493 | 487 | |
| 494 | 488 | /** |
| 495 | 489 | * Detect if the plugin file is quarantinable. |
| @@ -502,7 +496,128 @@ | ||
| 502 | 496 | return $this->is_likely_wporg_slug( |
| 503 | 497 | $this->get_plugin_directory_name( |
| 504 | 498 | $file_path |
| 505 | 499 | ) |
| 500 | + ); | |
| 501 | + } | |
| 502 | + | |
| 503 | + /** | |
| 504 | + * Get a list of the active plugins. | |
| 505 | + * | |
| 506 | + * @return array | |
| 507 | + */ | |
| 508 | + private function get_active_and_valid_plugin_files(): array { | |
| 509 | + $active_plugins = is_multisite() ? wp_get_active_network_plugins() : array(); | |
| 510 | + $active_plugins = array_merge( $active_plugins, wp_get_active_and_valid_plugins() ); | |
| 511 | + | |
| 512 | + return array_unique( $active_plugins ); | |
| 513 | + } | |
| 514 | + | |
| 515 | + /** | |
| 516 | + * Get a plugin name. | |
| 517 | + * | |
| 518 | + * @param string $plugin_file Absolute path to the main plugin file. | |
| 519 | + * | |
| 520 | + * @return string | |
| 521 | + */ | |
| 522 | + private function get_plugin_name( $plugin_file ): string { | |
| 523 | + $plugin_data = get_plugin_data( $plugin_file ); | |
| 524 | + | |
| 525 | + return isset( $plugin_data['Name'] ) ? $plugin_data['Name'] : ''; | |
| 526 | + } | |
| 527 | + | |
| 528 | + /** | |
| 529 | + * Get active plugin names. | |
| 530 | + * | |
| 531 | + * @return array | |
| 532 | + */ | |
| 533 | + public function get_active_plugin_names(): array { | |
| 534 | + $active_plugins = $this->get_active_and_valid_plugin_files(); | |
| 535 | + $plugin_names = array(); | |
| 536 | + | |
| 537 | + // Convert paths to names. | |
| 538 | + foreach ( $active_plugins as $plugin_file ) { | |
| 539 | + $plugin_name = $this->get_plugin_name( $plugin_file ); | |
| 540 | + if ( '' !== $plugin_name ) { | |
| 541 | + $plugin_names[] = $plugin_name; | |
| 542 | + } | |
| 543 | + } | |
| 544 | + | |
| 545 | + return $plugin_names; | |
| 546 | + } | |
| 547 | + | |
| 548 | + /** | |
| 549 | + * Data for the “update Defender” notification bubble (React). | |
| 550 | + * | |
| 551 | + * Uses the update_plugins site transient (WordPress.org for Free; WPMU DEV Dashboard merges | |
| 552 | + * Pro updates into the same transient when that plugin is active). | |
| 553 | + * | |
| 554 | + * @return array{ | |
| 555 | + * available:bool, | |
| 556 | + * newVersion:string, | |
| 557 | + * learnMoreUrl:string, | |
| 558 | + * updateUrl:string, | |
| 559 | + * canUpdate:bool, | |
| 560 | + * currentVersion:string, | |
| 561 | + * releaseDate:string | |
| 562 | + * } | |
| 563 | + */ | |
| 564 | + public function get_plugin_update_notice_data(): array { | |
| 565 | + $basename = DEFENDER_PLUGIN_BASENAME; | |
| 566 | + | |
| 567 | + $default_learn_more = defender_is_wp_org_version() | |
| 568 | + ? 'https://wordpress.org/plugins/defender-security/' | |
| 569 | + : 'https://wpmudev.com/project/wp-defender/#changelog_all'; | |
| 570 | + | |
| 571 | + $learn_more_url = $default_learn_more; | |
| 572 | + $new_version = ''; | |
| 573 | + $has_update = false; | |
| 574 | + | |
| 575 | + $updates = get_site_transient( 'update_plugins' ); | |
| 576 | + if ( is_object( $updates ) && isset( $updates->response[ $basename ] ) ) { | |
| 577 | + $pkg = $updates->response[ $basename ]; | |
| 578 | + if ( is_object( $pkg ) ) { | |
| 579 | + $has_update = true; | |
| 580 | + $new_version = isset( $pkg->new_version ) ? (string) $pkg->new_version : ''; | |
| 581 | + if ( isset( $pkg->url ) && is_string( $pkg->url ) && '' !== $pkg->url ) { | |
| 582 | + $learn_more_url = $pkg->url; | |
| 583 | + } | |
| 584 | + } elseif ( is_array( $pkg ) ) { | |
| 585 | + $has_update = true; | |
| 586 | + $new_version = isset( $pkg['new_version'] ) ? (string) $pkg['new_version'] : ''; | |
| 587 | + if ( isset( $pkg['url'] ) && is_string( $pkg['url'] ) && '' !== $pkg['url'] ) { | |
| 588 | + $learn_more_url = $pkg['url']; | |
| 589 | + } | |
| 590 | + } | |
| 591 | + } | |
| 592 | + | |
| 593 | + $can_update = $has_update && current_user_can( 'update_plugins' ); | |
| 594 | + $update_url = ''; | |
| 595 | + if ( $can_update ) { | |
| 596 | + // Build a plain URL for wp_localize_script (wp_nonce_url() HTML-escapes and breaks JSON). | |
| 597 | + $update_url = add_query_arg( | |
| 598 | + array( | |
| 599 | + '_wpnonce' => wp_create_nonce( 'upgrade-plugin_' . $basename ), | |
| 600 | + 'action' => 'upgrade-plugin', | |
| 601 | + 'plugin' => $basename, | |
| 602 | + ), | |
| 603 | + self_admin_url( 'update.php' ) | |
| 604 | + ); | |
| 605 | + } | |
| 606 | + | |
| 607 | + $release_date_raw = defined( 'DEFENDER_RELEASE_DATE' ) ? DEFENDER_RELEASE_DATE : ''; | |
| 608 | + $timestamp = strtotime( $release_date_raw ); | |
| 609 | + $release_date = false !== $timestamp | |
| 610 | + ? date_i18n( get_option( 'date_format' ), $timestamp ) | |
| 611 | + : ''; | |
| 612 | + | |
| 613 | + return array( | |
| 614 | + 'available' => $has_update, | |
| 615 | + 'newVersion' => $new_version, | |
| 616 | + 'learnMoreUrl' => esc_url_raw( $learn_more_url ), | |
| 617 | + 'updateUrl' => esc_url_raw( $update_url ), | |
| 618 | + 'canUpdate' => $can_update && '' !== $update_url, | |
| 619 | + 'currentVersion' => DEFENDER_VERSION, | |
| 620 | + 'releaseDate' => $release_date, | |
| 506 | 621 | ); |
| 507 | 622 | } |
| 508 | 623 | } |