| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Modules\ActivityLogs\Hooks; |
| 4 |
|
| 5 |
use WPAdminify\Inc\Modules\ActivityLogs\Inc\Hooks_Base; |
| 6 |
|
| 7 |
// no direct access allowed |
| 8 |
if (!defined('ABSPATH')) exit; |
| 9 |
|
| 10 |
class WP_Adminify_Logs_Plugins extends Hooks_Base |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
parent::__construct(); |
| 15 |
|
| 16 |
add_action('activated_plugin', [$this, 'hooks_activated_plugin']); |
| 17 |
add_action('deactivated_plugin', [$this, 'hooks_deactivated_plugin']); |
| 18 |
add_filter('wp_redirect', [$this, 'hooks_plugin_modify'], 10, 2); |
| 19 |
|
| 20 |
add_action('upgrader_process_complete', [$this, 'hooks_plugin_install_or_update'], 10, 2); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected function _add_log_plugin($action, $plugin_name) |
| 26 |
{ |
| 27 |
// Get plugin name if is a path |
| 28 |
if (false !== strpos($plugin_name, '/')) { |
| 29 |
$plugin_dir = explode('/', $plugin_name); |
| 30 |
$plugin_data = array_values(get_plugins('/' . $plugin_dir[0])); |
| 31 |
$plugin_data = array_shift($plugin_data); |
| 32 |
$plugin_name = $plugin_data['Name']; |
| 33 |
} |
| 34 |
|
| 35 |
adminify_activity_logs( |
| 36 |
array( |
| 37 |
'action' => $action, |
| 38 |
'object_type' => 'Plugin', |
| 39 |
'object_id' => 0, |
| 40 |
'object_name' => $plugin_name, |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function hooks_deactivated_plugin($plugin_name) |
| 46 |
{ |
| 47 |
$this->_add_log_plugin('deactivated', $plugin_name); |
| 48 |
} |
| 49 |
|
| 50 |
public function hooks_activated_plugin($plugin_name) |
| 51 |
{ |
| 52 |
$this->_add_log_plugin('activated', $plugin_name); |
| 53 |
} |
| 54 |
|
| 55 |
public function hooks_plugin_modify($location, $status) |
| 56 |
{ |
| 57 |
if (false !== strpos($location, 'plugin-editor.php')) { |
| 58 |
if ((!empty($_POST) && 'update' === $_REQUEST['action'])) { |
| 59 |
$aal_args = array( |
| 60 |
'action' => 'file_updated', |
| 61 |
'object_type' => 'Plugin', |
| 62 |
'object_subtype' => 'plugin_unknown', |
| 63 |
'object_id' => 0, |
| 64 |
'object_name' => 'file_unknown', |
| 65 |
); |
| 66 |
|
| 67 |
if (!empty($_REQUEST['file'])) { |
| 68 |
$aal_args['object_name'] = $_REQUEST['file']; |
| 69 |
// Get plugin name |
| 70 |
$plugin_dir = explode('/', $_REQUEST['file']); |
| 71 |
$plugin_data = array_values(get_plugins('/' . $plugin_dir[0])); |
| 72 |
$plugin_data = array_shift($plugin_data); |
| 73 |
|
| 74 |
$aal_args['object_subtype'] = $plugin_data['Name']; |
| 75 |
} |
| 76 |
adminify_activity_logs($aal_args); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// We are need return the instance, for complete the filter. |
| 81 |
return $location; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param Plugin_Upgrader $upgrader |
| 86 |
* @param array $extra |
| 87 |
*/ |
| 88 |
public function hooks_plugin_install_or_update($upgrader, $extra) |
| 89 |
{ |
| 90 |
if (!isset($extra['type']) || 'plugin' !== $extra['type']) |
| 91 |
return; |
| 92 |
|
| 93 |
if ('install' === $extra['action']) { |
| 94 |
$path = $upgrader->plugin_info(); |
| 95 |
if (!$path) |
| 96 |
return; |
| 97 |
|
| 98 |
$data = get_plugin_data($upgrader->skin->result['local_destination'] . '/' . $path, true, false); |
| 99 |
|
| 100 |
adminify_activity_logs( |
| 101 |
array( |
| 102 |
'action' => 'installed', |
| 103 |
'object_type' => 'Plugin', |
| 104 |
'object_name' => $data['Name'], |
| 105 |
'object_subtype' => $data['Version'], |
| 106 |
) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
if ('update' === $extra['action']) { |
| 111 |
if (isset($extra['bulk']) && true == $extra['bulk']) { |
| 112 |
$slugs = $extra['plugins']; |
| 113 |
} else { |
| 114 |
if (!isset($upgrader->skin->plugin)) |
| 115 |
return; |
| 116 |
|
| 117 |
$slugs = array($upgrader->skin->plugin); |
| 118 |
} |
| 119 |
|
| 120 |
foreach ($slugs as $slug) { |
| 121 |
$data = get_plugin_data(WP_PLUGIN_DIR . '/' . $slug, true, false); |
| 122 |
|
| 123 |
adminify_activity_logs( |
| 124 |
array( |
| 125 |
'action' => 'updated', |
| 126 |
'object_type' => 'Plugin', |
| 127 |
'object_name' => $data['Name'], |
| 128 |
'object_subtype' => $data['Version'], |
| 129 |
) |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|