listen( 'activated_plugin', 'on_activated', 2 ); $this->listen( 'deactivated_plugin', 'on_deactivated', 2 ); $this->listen( 'deleted_plugin', 'on_deleted', 2 ); $this->listen( 'upgrader_process_complete', 'on_upgrader', 2 ); $this->listen( 'update_option_auto_update_plugins', 'on_auto_update', 2 ); $this->listen( 'update_site_option_auto_update_plugins', 'on_site_auto_update', 3 ); } /** * Plugin activated. * * @param string $plugin Plugin file. * @param bool $network Network-wide. */ public function on_activated( $plugin, $network = false ) { $this->log_plugin( $plugin, 'plugin.activated', /* translators: 1: plugin name, 2: version. */ __( 'Activated the plugin "%1$s" (%2$s).', 'admintrace-activity-logger' ), 'notice', $network ); } /** * Plugin deactivated. * * @param string $plugin Plugin file. * @param bool $network Network-wide. */ public function on_deactivated( $plugin, $network = false ) { $this->log_plugin( $plugin, 'plugin.deactivated', /* translators: 1: plugin name, 2: version. */ __( 'Deactivated the plugin "%1$s" (%2$s).', 'admintrace-activity-logger' ), 'notice', $network ); } /** * Plugin deleted. * * @param string $plugin Plugin file. * @param bool $deleted Whether deletion succeeded. */ public function on_deleted( $plugin, $deleted ) { if ( ! $deleted ) { return; } $this->log( array( 'summary' => sprintf( /* translators: %s: plugin file. */ __( 'Deleted the plugin "%s".', 'admintrace-activity-logger' ), Text::plain( $plugin, 120 ) ), 'category' => 'plugin', 'action' => 'plugin.deleted', 'severity' => 'warning', 'object_type' => 'plugin', 'object_title' => wp_basename( (string) $plugin ), ) ); } /** * Install or update completed. * * @param object $upgrader Upgrader. * @param array $extra Hook data. */ public function on_upgrader( $upgrader, $extra ) { if ( empty( $extra['type'] ) || 'plugin' !== $extra['type'] ) { return; } $action = isset( $extra['action'] ) ? (string) $extra['action'] : 'update'; $files = array(); if ( ! empty( $extra['plugins'] ) && is_array( $extra['plugins'] ) ) { $files = $extra['plugins']; } elseif ( ! empty( $extra['plugin'] ) && is_string( $extra['plugin'] ) ) { $files = array( $extra['plugin'] ); } elseif ( is_object( $upgrader ) && ! empty( $upgrader->result['destination_name'] ) ) { $files = array( $this->main_file( (string) $upgrader->result['destination_name'] ) ); } foreach ( $files as $file ) { if ( 'install' === $action ) { /* translators: 1: plugin name, 2: version. */ $pattern = __( 'Installed the plugin "%1$s" (%2$s).', 'admintrace-activity-logger' ); $event = 'plugin.installed'; } else { /* translators: 1: plugin name, 2: version. */ $pattern = __( 'Updated the plugin "%1$s" (%2$s).', 'admintrace-activity-logger' ); $event = 'plugin.updated'; } $this->log_plugin( $file, $event, $pattern, 'notice', false ); } } /** * Auto-update list changed on this site. * * @param mixed $old Previous plugin files. * @param mixed $new_value New plugin files. */ public function on_auto_update( $old, $new_value ) { $this->diff_auto_updates( $old, $new_value ); } /** * Auto-update list changed for the network. * * The site-option hook passes the option name first. * * @param mixed $option Ignored option name. * @param mixed $new_value New plugin files. * @param mixed $old Previous plugin files. */ public function on_site_auto_update( $option, $new_value, $old ) { unset( $option ); $this->diff_auto_updates( $old, $new_value ); } /** * Record plugins whose auto-update flag flipped. * * @param mixed $old Previous plugin files. * @param mixed $new_value New plugin files. */ private function diff_auto_updates( $old, $new_value ) { $old = is_array( $old ) ? $old : array(); $new_value = is_array( $new_value ) ? $new_value : array(); $enabled = array_values( array_diff( $new_value, $old ) ); $disabled = array_values( array_diff( $old, $new_value ) ); if ( count( $enabled ) + count( $disabled ) > 12 ) { $this->log( array( 'summary' => sprintf( /* translators: 1: enabled count, 2: disabled count. */ __( 'Changed plugin auto-updates: %1$d enabled and %2$d disabled.', 'admintrace-activity-logger' ), count( $enabled ), count( $disabled ) ), 'category' => 'plugin', 'action' => 'plugin.auto_update', 'severity' => 'notice', ) ); return; } foreach ( $enabled as $file ) { $this->log_auto_update( $file, true ); } foreach ( $disabled as $file ) { $this->log_auto_update( $file, false ); } } /** * Record one plugin auto-update change. * * @param string $plugin Plugin file. * @param bool $enabled Whether auto-update was turned on. */ private function log_auto_update( $plugin, $enabled ) { $meta = $this->plugin_meta( $plugin ); $this->log( array( 'summary' => $enabled ? sprintf( /* translators: %s: plugin name. */ __( 'Turned on automatic updates for the plugin "%s".', 'admintrace-activity-logger' ), $meta['name'] ) : sprintf( /* translators: %s: plugin name. */ __( 'Turned off automatic updates for the plugin "%s".', 'admintrace-activity-logger' ), $meta['name'] ), 'category' => 'plugin', 'action' => 'plugin.auto_update', 'severity' => 'notice', 'object_type' => 'plugin', 'object_title' => $meta['name'], 'context' => array( 'file' => Text::plain( $plugin, 160 ), 'enabled' => (bool) $enabled, ), ) ); } /** * Log a plugin by its file. * * @param string $plugin Plugin file. * @param string $action Action. * @param string $pattern Summary pattern. * @param string $severity Severity. * @param bool $network Network flag. */ private function log_plugin( $plugin, $action, $pattern, $severity, $network ) { $meta = $this->plugin_meta( $plugin ); $version = '' !== $meta['version'] ? $meta['version'] : __( 'unknown version', 'admintrace-activity-logger' ); $this->log( array( 'summary' => sprintf( $pattern, $meta['name'], $version ), 'category' => 'plugin', 'action' => $action, 'severity' => $severity, 'object_type' => 'plugin', 'object_title' => $meta['name'], 'context' => array( 'file' => Text::plain( $plugin, 160 ), 'version' => $meta['version'], 'author' => $meta['author'], 'network' => (bool) $network, ), ) ); } /** * Public plugin header fields. * * @param string $plugin Plugin file. * @return array{name:string,version:string,author:string} */ private function plugin_meta( $plugin ) { $name = wp_basename( (string) $plugin ); $meta = array( 'name' => $name, 'version' => '', 'author' => '', ); if ( ! function_exists( 'get_plugin_data' ) ) { $file = ABSPATH . 'wp-admin/includes/plugin.php'; if ( is_readable( $file ) ) { require_once $file; } } $path = WP_PLUGIN_DIR . '/' . ltrim( (string) $plugin, '/' ); if ( function_exists( 'get_plugin_data' ) && is_file( $path ) && is_readable( $path ) ) { $data = get_plugin_data( $path, false, false ); $meta['name'] = ! empty( $data['Name'] ) ? $data['Name'] : $name; $meta['version'] = ! empty( $data['Version'] ) ? $data['Version'] : ''; $meta['author'] = ! empty( $data['Author'] ) ? wp_strip_all_tags( $data['Author'] ) : ''; } return $meta; } /** * Main plugin file inside an installed plugin folder. * * @param string $folder Folder name below the plugins directory. * @return string Plugin file relative to the plugins directory, or the folder when none is found. */ private function main_file( $folder ) { $folder = trim( $folder, '/' ); if ( '' === $folder || ! is_dir( WP_PLUGIN_DIR . '/' . $folder ) ) { return $folder; } if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } wp_clean_plugins_cache( false ); $found = get_plugins( '/' . $folder ); if ( empty( $found ) ) { return $folder; } return $folder . '/' . (string) key( $found ); } }