| 1 |
<?php |
| 2 |
|
| 3 |
// Do not allow the file to be called directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This class is used to log any plugin related action. |
| 10 |
*/ |
| 11 |
class P_Event_Plugins extends P_Event_Log { |
| 12 |
|
| 13 |
/** |
| 14 |
* Hook into the actions so we can log it. |
| 15 |
* |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'activated_plugin', [ &$this, 'activated_plugin' ] ); |
| 20 |
add_action( 'deactivated_plugin', [ &$this, 'deactivated_plugin' ] ); |
| 21 |
add_action( 'upgrader_process_complete', [ &$this, 'plugin_install_or_update' ], 10, 2 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Log the action regarding the plugin. |
| 26 |
* |
| 27 |
* @param string $action |
| 28 |
* @param string $plugin_name |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
protected function _add_log_plugin( $action, $plugin_name ) { |
| 32 |
// Get the plugin name if it is a path. |
| 33 |
if ( strpos( $plugin_name, '/' ) !== false ) { |
| 34 |
$plugin_dir = explode( '/', $plugin_name ); |
| 35 |
$plugin_data = array_values( get_plugins( '/' . $plugin_dir[0] ) ); |
| 36 |
$plugin_data = array_shift( $plugin_data ); |
| 37 |
$plugin_name = $plugin_data['Name']; |
| 38 |
} |
| 39 |
|
| 40 |
$this->insert( |
| 41 |
[ |
| 42 |
'action' => $action, |
| 43 |
'object' => 'plugin', |
| 44 |
'object_id' => 0, |
| 45 |
'object_name' => esc_html( $plugin_name ), |
| 46 |
] |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* When a plugin is de-activated. |
| 52 |
* |
| 53 |
* @param string $plugin_name |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function deactivated_plugin( $plugin_name ) { |
| 57 |
$this->_add_log_plugin( 'deactivated', $plugin_name ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* When a plugin is activated. |
| 62 |
* |
| 63 |
* @param string $plugin_name |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function activated_plugin( $plugin_name ) { |
| 67 |
$this->_add_log_plugin( 'activated', $plugin_name ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* When a plugin is installed or updated. |
| 72 |
* |
| 73 |
* @param Plugin_Upgrader $upgrader |
| 74 |
* @param array $extra |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function plugin_install_or_update( $upgrader, $extra ) { |
| 78 |
if ( ! isset( $extra['type'] ) || $extra['type'] !== 'plugin' ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// If plugin is installed. |
| 83 |
if ( $extra['action'] === 'install' ) { |
| 84 |
$path = $upgrader->plugin_info(); |
| 85 |
if ( ! $path ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$data = get_plugin_data( $upgrader->skin->result['local_destination'] . '/' . $path, true, false ); |
| 90 |
$this->_add_log_plugin( 'installed', $data['Name'] ); |
| 91 |
} |
| 92 |
|
| 93 |
// If plugin is updated. |
| 94 |
if ( $extra['action'] === 'update' ) { |
| 95 |
// Bulk update? |
| 96 |
if ( isset( $extra['bulk'] ) && $extra['bulk'] == true ) { |
| 97 |
$slugs = $extra['plugins']; |
| 98 |
} else { |
| 99 |
if ( ! isset( $upgrader->skin->plugin ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
$slugs = [ $upgrader->skin->plugin ]; |
| 104 |
} |
| 105 |
|
| 106 |
// Loop through all updated plugins. |
| 107 |
foreach ( $slugs as $slug ) { |
| 108 |
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug, true, false ); |
| 109 |
$this->_add_log_plugin( 'updated', $data['Name'] ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|