PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.9.0
Defender Security – Malware Scanner, Login Security & Firewall v5.9.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / behavior / scan / class-abandoned-plugin.php

class-abandoned-plugin.php in Defender Security – Malware Scanner, Login Security & Firewall 5.9.0, at src/behavior/scan/class-abandoned-plugin.php

282 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle abandoned plugin scan.
4 *
5 * @package WP_Defender\Behavior\Scan
6 */
7
8 namespace WP_Defender\Behavior\Scan;
9
10 use Calotes\Component\Behavior;
11 use WP_Defender\Component\Scan as Scan_Component;
12 use WP_Defender\Controller\Scan as Scan_Controller;
13 use WP_Defender\Model\Scan;
14 use WP_Defender\Model\Scan_Item;
15 use WP_Defender\Traits\IO;
16 use WP_Defender\Traits\Plugin;
17
18 /**
19 * Handle abandoned plugin scan.
20 */
21 class Abandoned_Plugin extends Behavior {
22
23 use IO;
24 use Plugin;
25
26 /**
27 * Hold the Scan model or null if not set.
28 *
29 * @var Scan|null
30 */
31 private ?Scan $scan;
32
33 /**
34 * Outdated plugin period.
35 *
36 * @var string
37 */
38 private const OUTDATED_PERIOD = '2 years';
39
40 /**
41 * Initialize Scan instance.
42 *
43 * @param Scan $scan The Scan model.
44 */
45 public function __construct( Scan $scan ) {
46 $this->scan = $scan;
47 }
48
49 /**
50 * Get the outdated period of the plugin.
51 *
52 * @return string
53 */
54 public static function get_outdated_period(): string {
55 // Filter to override outdated threshold period.
56 $period = apply_filters( 'wpdef_scan_outdated_period', self::OUTDATED_PERIOD );
57 $period = is_string( $period ) ? $period : (string) $period;
58 // If time period is invalid, use the default one.
59 return strtotime( '- ' . $period ) ? $period : self::OUTDATED_PERIOD;
60 }
61
62 /**
63 * Check if there are abandoned plugins.
64 *
65 * @return bool
66 */
67 public function abandoned_plugin_check(): bool {
68 $ignored_issues = $this->get_ignored_issues();
69
70 $actioned_plugins = get_site_option( Scan_Component::PLUGINS_ACTIONED );
71 $plugin_list = Scan_Component::are_actioned_plugins( $actioned_plugins )
72 ? $actioned_plugins
73 : $this->get_plugin_slugs();
74
75 foreach ( $plugin_list as $slug => $plugin_data ) {
76 $this->check_abandoned_plugin_by( $slug );
77 }
78
79 $this->scan->calculate_percent( 100, 4 );
80 $this->add_ignored_issues( $ignored_issues );
81
82 return true;
83 }
84
85 /**
86 * Retrieve the list of ignored issues.
87 *
88 * @return array The list of ignored issues.
89 */
90 private function get_ignored_issues(): array {
91 $last = Scan::get_last();
92
93 return is_object( $last )
94 ? $last->get_issues(
95 Scan::get_abandoned_types(),
96 Scan_Item::STATUS_IGNORE
97 )
98 : array();
99 }
100
101 /**
102 * Add ignored issues to the scan.
103 *
104 * @param array $ignored_issues The array of ignored issues to add.
105 */
106 private function add_ignored_issues( array $ignored_issues ) {
107 if ( array() !== $ignored_issues ) {
108 foreach ( $ignored_issues as $issue ) {
109 $this->scan->add_item( $issue->type, $issue->raw_data, Scan_Item::STATUS_IGNORE );
110 }
111 }
112 }
113
114 /**
115 * Handle the error by different actions, e.g. logging.
116 *
117 * @param string $error_message The error message.
118 */
119 private function handle_error( string $error_message ) {
120 $this->log( $error_message, Scan_Controller::SCAN_LOG );
121 }
122
123 /**
124 * Prepare data before saving.
125 *
126 * @param array $body_json Data from WP API.
127 * @param array $plugin_details Data from local plugin headers.
128 *
129 * @return array {
130 * Plugin data. Values will be empty if not supplied by the plugin or API sides.
131 *
132 * @type string $name Plugin name.
133 * @type string $slug Plugin slug.
134 * @type string $url Plugin URL.
135 * @type string $version Plugin version.
136 * @type string $tested_wp Latest tested WordPress version.
137 * @type string $last_updated Last updated date.
138 * @type string $reason_text Reason for saving or update.
139 * }
140 */
141 private function prepare_item_data( array $body_json, array $plugin_details ): array {
142 $data = array();
143 // Check plugin name.
144 if ( isset( $body_json['name'] ) ) {
145 $data['name'] = $body_json['name'];
146 } elseif ( isset( $plugin_details['Name'] ) ) {
147 $data['name'] = $plugin_details['Name'];
148 } else {
149 $data['name'] = '';
150 }
151 // Check plugin slug.
152 if ( isset( $body_json['slug'] ) ) {
153 $data['slug'] = $body_json['slug'];
154 } elseif ( isset( $plugin_details['slug'] ) ) {
155 $data['slug'] = $plugin_details['slug'];
156 } else {
157 $data['slug'] = '';
158 }
159 // Check plugin URL. Sometimes URL values are empty.
160 if ( isset( $body_json['homepage'] ) && '' !== $body_json['homepage'] ) {
161 $data['url'] = $body_json['homepage'];
162 } elseif ( isset( $plugin_details['PluginURI'] ) && '' !== $plugin_details['PluginURI'] ) {
163 $data['url'] = $plugin_details['PluginURI'];
164 } elseif ( isset( $plugin_details['AuthorURI'] ) && '' !== $plugin_details['AuthorURI'] ) {
165 $data['url'] = $plugin_details['AuthorURI'];
166 } else {
167 $data['url'] = '';
168 }
169 // Check plugin version.
170 if ( isset( $body_json['version'] ) ) {
171 $data['version'] = $body_json['version'];
172 } elseif ( isset( $plugin_details['Version'] ) ) {
173 $data['version'] = $plugin_details['Version'];
174 } else {
175 $data['version'] = '';
176 }
177 // Check a tested WP version.
178 if ( isset( $body_json['tested'] ) ) {
179 $data['tested_wp'] = $body_json['tested'];
180 } else {
181 $data['tested_wp'] = '';
182 }
183 // Check plugin updated time.
184 if ( isset( $body_json['closed_date'] ) ) {
185 $data['last_updated'] = $body_json['closed_date'];
186 } elseif ( isset( $body_json['last_updated'] ) ) {
187 $data['last_updated'] = $body_json['last_updated'];
188 } else {
189 $data['last_updated'] = '';
190 }
191 // Specific key for closed plugin.
192 if ( isset( $body_json['reason_text'] ) ) {
193 $data['reason_text'] = $body_json['reason_text'];
194 } else {
195 $data['reason_text'] = '';
196 }
197
198 return $data;
199 }
200
201 /**
202 * Get list of plugin slugs whose names do not match the specified names on wp.org.
203 */
204 private function get_plugin_slugs_with_mismatched_names(): array {
205 return array(
206 'miniorange-malware-protection',
207 'admin-bar-user-switching',
208 'custom-post-template',
209 );
210 }
211
212 /**
213 * Is abandoned plugin?
214 *
215 * @param string $slug The slug to check.
216 *
217 * @return bool
218 */
219 public function check_abandoned_plugin_by( string $slug ): bool {
220 // Check if the plugin is not from wp.org.
221 $premium_plugin_slugs = get_site_option( Plugin_Integrity::PLUGIN_PREMIUM_SLUGS, array() );
222 if ( ( is_array( $premium_plugin_slugs ) && in_array( $slug, $premium_plugin_slugs, true ) )
223 || ! $this->is_likely_wporg_slug( $slug )
224 ) {
225 return true;
226 }
227 if ( $this->scan->is_issue_ignored( $slug ) ) {
228 $this->log( sprintf( 'skip %s because it is already ignored.', $slug ), Scan_Controller::SCAN_LOG );
229 return true;
230 }
231
232 $results = $this->handle_wp_org_response_by( $slug );
233 if ( ! $results['success'] ) {
234 $this->handle_error( $results['message'] );
235
236 return false;
237 } else {
238 $body_json = $results['body'];
239 $plugin_details = $this->get_plugin_details_by( $slug );
240 if (
241 isset( $body_json['name'], $plugin_details['Name'] )
242 && $plugin_details['Name'] !== $body_json['name']
243 && ! in_array( $slug, $this->get_plugin_slugs_with_mismatched_names(), true )
244 ) {
245 // This is a premium plugin with the same slug as on wp.org but with another name OR the plugin developer named the plugin differently on wp.org and inside the main file.
246 return false;
247 }
248
249 if ( isset( $body_json['error'] ) && 'closed' === $body_json['error'] ) {
250 $this->log( sprintf( 'Plugin slug closed: %s', $slug ), Scan_Controller::SCAN_LOG );
251
252 $this->scan->add_item(
253 Scan_Item::TYPE_PLUGIN_CLOSED,
254 $this->prepare_item_data( $body_json, $plugin_details )
255 );
256
257 return true;
258 }
259
260 if ( '' !== $body_json['last_updated'] ) {
261 $last_updated_time = $body_json['last_updated'];
262 $ttl = strtotime( $last_updated_time );
263 if ( $ttl < strtotime( '- ' . self::get_outdated_period() ) ) {
264 $this->log( sprintf( 'Plugin slug outdated: %s', $slug ), Scan_Controller::SCAN_LOG );
265 $plugin_details = $this->prepare_item_data( $body_json, $plugin_details );
266 // Improve the date view.
267 $plugin_details['last_updated'] = gmdate( 'F j, Y', $ttl );
268
269 $this->scan->add_item(
270 Scan_Item::TYPE_PLUGIN_OUTDATED,
271 $plugin_details
272 );
273
274 return true;
275 }
276 }
277
278 return false;
279 }
280 }
281 }
282