| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Plugin extends Logtivity_Abstract_Logger |
| 4 |
{ |
| 5 |
public function registerHooks() |
| 6 |
{ |
| 7 |
add_action( 'activated_plugin', [$this, 'pluginActivated'], 10, 2 ); |
| 8 |
add_action( 'deactivated_plugin', [$this, 'pluginDeactivated'], 10, 2 ); |
| 9 |
add_action( 'upgrader_process_complete', [$this, 'upgradeProcessComplete'], 10, 2); |
| 10 |
add_filter( 'editable_extensions', [$this, 'pluginFileModified'], 10, 2 ); |
| 11 |
add_action( 'deleted_plugin', [$this, 'pluginDeleted'], 10, 2 ); |
| 12 |
} |
| 13 |
|
| 14 |
public function pluginActivated($plugin, $network_wide) |
| 15 |
{ |
| 16 |
return Logtivity_Logger::log() |
| 17 |
->setAction('Plugin Activated') |
| 18 |
->setContext($plugin) |
| 19 |
->addMeta('network_wide', $network_wide) |
| 20 |
->send(); |
| 21 |
} |
| 22 |
|
| 23 |
public function pluginDeactivated($plugin, $network_deactivating) |
| 24 |
{ |
| 25 |
return Logtivity_Logger::log() |
| 26 |
->setAction('Plugin Deactivated') |
| 27 |
->setContext($plugin) |
| 28 |
->addMeta('network_deactivating', $network_deactivating) |
| 29 |
->send(); |
| 30 |
} |
| 31 |
|
| 32 |
public function pluginDeleted($plugin_file, $deleted) |
| 33 |
{ |
| 34 |
return Logtivity_Logger::log() |
| 35 |
->setAction('Plugin Deleted') |
| 36 |
->setContext($plugin_file) |
| 37 |
->addMeta('Deletion Successful', $deleted) |
| 38 |
->send(); |
| 39 |
} |
| 40 |
|
| 41 |
public function upgradeProcessComplete( $upgrader_object, $options ) |
| 42 |
{ |
| 43 |
if ( $options['type'] != 'plugin' ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
if ($options['action'] == 'update') { |
| 48 |
return $this->pluginUpdated($upgrader_object, $options); |
| 49 |
} |
| 50 |
|
| 51 |
if ($options['action'] == 'install') { |
| 52 |
return $this->pluginInstalled($upgrader_object, $options); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function pluginUpdated($upgrader_object, $options) |
| 57 |
{ |
| 58 |
if ( isset( $options['bulk'] ) && true == $options['bulk'] ) { |
| 59 |
$slugs = $options['plugins']; |
| 60 |
} else { |
| 61 |
if ( ! isset( $upgrader->skin->plugin ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$slugs = array( $upgrader->skin->plugin ); |
| 66 |
} |
| 67 |
|
| 68 |
foreach ( $slugs as $slug ) |
| 69 |
{ |
| 70 |
$data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug, true, false ); |
| 71 |
|
| 72 |
Logtivity_Logger::log() |
| 73 |
->setAction('Plugin Updated') |
| 74 |
->setContext($data['Name']) |
| 75 |
->addMeta('Version', ( isset($data['Version']) ? $data['Version'] : 'Not set')) |
| 76 |
->addMeta('Bulk', $options['bulk']) |
| 77 |
->send(); |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
public function pluginInstalled($upgrader_object, $options) |
| 83 |
{ |
| 84 |
$path = $upgrader_object->plugin_info(); |
| 85 |
|
| 86 |
if ( ! $path ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$data = get_plugin_data( $upgrader_object->skin->result['local_destination'] . '/' . $path, true, false ); |
| 91 |
|
| 92 |
return Logtivity_Logger::log() |
| 93 |
->setAction('Plugin Installed') |
| 94 |
->setContext($data['Name']) |
| 95 |
->addMeta('Version', $data['Version']) |
| 96 |
->send(); |
| 97 |
} |
| 98 |
|
| 99 |
public function pluginFileModified( $editable_extensions, $plugin ) { |
| 100 |
|
| 101 |
if ( ! isset($_POST['action']) || $_POST['action'] != 'edit-theme-plugin-file' ) { |
| 102 |
return $editable_extensions; |
| 103 |
} |
| 104 |
|
| 105 |
if (!isset($_POST['plugin'])) { |
| 106 |
return $editable_extensions; |
| 107 |
} |
| 108 |
|
| 109 |
$log = Logtivity_Logger::log()->setAction('Plugin File Edited'); |
| 110 |
|
| 111 |
if ( ! empty( $_REQUEST['file'] ) ) { |
| 112 |
|
| 113 |
$plugin_dir = explode( '/', sanitize_text_field($_REQUEST['file'] )); |
| 114 |
$plugin_data = array_values( get_plugins( '/' . $plugin_dir[0] ) ); |
| 115 |
$plugin_data = array_shift( $plugin_data ); |
| 116 |
|
| 117 |
if ( ! empty( $_POST['file'] ) ) { |
| 118 |
$log->addMeta('File', sanitize_text_field($_POST['file'])); |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset($plugin_data['Name']) ) { |
| 122 |
$log->setContext($plugin_data['Name']); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
$log->send(); |
| 128 |
|
| 129 |
return $editable_extensions; |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
$Logtivity_Plugin = new Logtivity_Plugin; |