| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Tracking |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Represents the plugin data. |
| 10 |
*/ |
| 11 |
class WPSEO_Tracking_Plugin_Data implements WPSEO_Collection { |
| 12 |
|
| 13 |
/** |
| 14 |
* Plugins with auto updating enabled. |
| 15 |
* |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
private $auto_update_plugin_list; |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns the collection data. |
| 22 |
* |
| 23 |
* @return array The collection data. |
| 24 |
*/ |
| 25 |
public function get() { |
| 26 |
return [ |
| 27 |
'plugins' => $this->get_plugin_data(), |
| 28 |
]; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns all plugins. |
| 33 |
* |
| 34 |
* @return array The formatted plugins. |
| 35 |
*/ |
| 36 |
protected function get_plugin_data() { |
| 37 |
|
| 38 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 39 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 40 |
} |
| 41 |
$plugins = wp_get_active_and_valid_plugins(); |
| 42 |
|
| 43 |
$plugins = array_map( 'get_plugin_data', $plugins ); |
| 44 |
$this->set_auto_update_plugin_list(); |
| 45 |
$plugins = array_map( [ $this, 'format_plugin' ], $plugins ); |
| 46 |
|
| 47 |
$plugin_data = []; |
| 48 |
foreach ( $plugins as $plugin ) { |
| 49 |
$plugin_key = sanitize_title( $plugin['name'] ); |
| 50 |
$plugin_data[ $plugin_key ] = $plugin; |
| 51 |
} |
| 52 |
|
| 53 |
return $plugin_data; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Sets all auto updating plugin data so it can be used in the tracking list. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function set_auto_update_plugin_list() { |
| 62 |
|
| 63 |
$auto_update_plugins = []; |
| 64 |
$auto_update_plugin_files = get_option( 'auto_update_plugins' ); |
| 65 |
if ( $auto_update_plugin_files ) { |
| 66 |
foreach ( $auto_update_plugin_files as $auto_update_plugin ) { |
| 67 |
$data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $auto_update_plugin ); |
| 68 |
$auto_update_plugins[ $data['Name'] ] = $data; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$this->auto_update_plugin_list = $auto_update_plugins; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Formats the plugin array. |
| 77 |
* |
| 78 |
* @param array $plugin The plugin details. |
| 79 |
* |
| 80 |
* @return array The formatted array. |
| 81 |
*/ |
| 82 |
protected function format_plugin( array $plugin ) { |
| 83 |
|
| 84 |
return [ |
| 85 |
'name' => $plugin['Name'], |
| 86 |
'version' => $plugin['Version'], |
| 87 |
'auto_updating' => array_key_exists( $plugin['Name'], $this->auto_update_plugin_list ), |
| 88 |
]; |
| 89 |
} |
| 90 |
} |
| 91 |
|