| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles abandoned plugin scan item. |
| 4 |
* |
| 5 |
* @package WP_Defender\Behavior\Scan_Item |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Behavior\Scan_Item; |
| 9 |
|
| 10 |
use WP_Defender\Traits\IO; |
| 11 |
use WP_Defender\Model\Scan; |
| 12 |
use WP_Defender\Traits\Plugin; |
| 13 |
use Calotes\Component\Behavior; |
| 14 |
use WP_Defender\Model\Scan_Item; |
| 15 |
use WP_Defender\Controller\Scan as Scan_Controller; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Abandoned_Result. |
| 19 |
*/ |
| 20 |
class Abandoned_Result extends Behavior { |
| 21 |
|
| 22 |
use IO; |
| 23 |
use Plugin; |
| 24 |
|
| 25 |
/** |
| 26 |
* Return general data so we can output on frontend. |
| 27 |
* |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public function to_array(): array { |
| 31 |
$data = $this->owner->raw_data; |
| 32 |
|
| 33 |
return array( |
| 34 |
'id' => $this->owner->id, |
| 35 |
'type' => $this->owner->type, |
| 36 |
'file_name' => $data['name'], |
| 37 |
'version' => $data['version'], |
| 38 |
'plugin_url' => $data['url'], |
| 39 |
'tested_wp' => $data['tested_wp'], |
| 40 |
'short_desc' => $this->get_short_desc( $this->owner->type ), |
| 41 |
'deletable' => ! $this->is_active_plugin( $data['slug'] ), |
| 42 |
'reason_text' => $data['reason_text'], |
| 43 |
'last_updated' => $data['last_updated'], |
| 44 |
// Follow consistency with other types. Need for WP-CLI command. Full path = base slug for this item. |
| 45 |
'full_path' => $data['slug'], |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* There is not resolve-action for the current scan type. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function resolve() {} |
| 55 |
|
| 56 |
/** |
| 57 |
* Ignore the abandoned plugin for the current scan. |
| 58 |
* |
| 59 |
* @return array An array with a message indicating successful ignore. |
| 60 |
*/ |
| 61 |
public function ignore(): array { |
| 62 |
$scan = Scan::get_last(); |
| 63 |
$scan->ignore_issue( $this->owner->id ); |
| 64 |
|
| 65 |
return array( 'message' => esc_html__( 'The plugin has been successfully ignored.', 'defender-security' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Allow the abandoned plugin for the current scan. |
| 70 |
* |
| 71 |
* @return array An array with a message indicating successful restoration. |
| 72 |
*/ |
| 73 |
public function unignore(): array { |
| 74 |
$scan = Scan::get_last(); |
| 75 |
$scan->unignore_issue( $this->owner->id ); |
| 76 |
|
| 77 |
return array( 'message' => esc_html__( 'The plugin has been successfully restored.', 'defender-security' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Remove a plugin at the specified path. |
| 82 |
* |
| 83 |
* @param string $path The plugin path. |
| 84 |
* |
| 85 |
* @return bool |
| 86 |
*/ |
| 87 |
private function maybe_remove( string $path ): bool { |
| 88 |
if ( is_dir( $path ) ) { |
| 89 |
return $this->delete_dir( $path ); |
| 90 |
} else { |
| 91 |
// Sometimes a plugin consists of one file. |
| 92 |
wp_delete_file( $path ); |
| 93 |
return true; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Removes a plugin based on the provided data, checking if it is active and has the necessary permissions. |
| 99 |
* |
| 100 |
* @param array $data An array containing information about the plugin to be removed. |
| 101 |
* |
| 102 |
* @return array An array with the status of the removal process and any related messages. |
| 103 |
*/ |
| 104 |
private function remove_plugin( array $data ): array { |
| 105 |
$active = $this->is_active_plugin( $data['slug'] ); |
| 106 |
if ( $active ) { |
| 107 |
return array( |
| 108 |
'type_notice' => 'error', |
| 109 |
'message' => esc_html__( 'This plugin cannot be removed because it is active.', 'defender-security' ), |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
$abs_path = $this->get_abs_plugin_path_by_slug( $data['slug'] ); |
| 114 |
if ( file_exists( $abs_path ) && ! $this->maybe_remove( $abs_path ) ) { |
| 115 |
return array( |
| 116 |
'type_notice' => 'error', |
| 117 |
'message' => esc_html__( |
| 118 |
'Defender does not have enough permission to remove this plugin.', |
| 119 |
'defender-security' |
| 120 |
), |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
$message = sprintf( |
| 125 |
/* translators: %s: Plugin name. */ |
| 126 |
esc_html__( '%s plugin', 'defender-security' ), |
| 127 |
'<b>' . $data['name'] . '</b>' |
| 128 |
); |
| 129 |
$this->log( $message . ' is deleted', Scan_Controller::SCAN_LOG ); |
| 130 |
$model = Scan::get_last(); |
| 131 |
$model->remove_issue( $this->owner->id ); |
| 132 |
// No makes sense to remove related issue(-s) because it's whole folder. Not file. |
| 133 |
|
| 134 |
do_action( 'wpdef_fixed_scan_issue', 'abandoned_plugin', 'delete' ); |
| 135 |
|
| 136 |
return array( |
| 137 |
'message' => esc_html__( 'This item has been permanently removed', 'defender-security' ), |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Delete whole folder. |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function delete(): array { |
| 147 |
$data = $this->owner->raw_data; |
| 148 |
return $this->remove_plugin( $data ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the short description. |
| 153 |
* |
| 154 |
* @param string $type Scan type. |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
private function get_short_desc( string $type ): string { |
| 159 |
return Scan_Item::TYPE_PLUGIN_OUTDATED === $type |
| 160 |
? sprintf( |
| 161 |
/* translators: %s: Time period. */ |
| 162 |
esc_html__( 'No updates released by the author in the past %s', 'defender-security' ), |
| 163 |
\WP_Defender\Behavior\Scan\Abandoned_Plugin::get_outdated_period() |
| 164 |
) |
| 165 |
: esc_html__( 'Removed from the wordpress.org repository', 'defender-security' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|