| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles theme events for Audit Logging. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Audit |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Audit; |
| 9 |
|
| 10 |
use WP_Defender\Traits\User; |
| 11 |
use WP_Defender\Model\Audit_Log; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handles theme related all events for Audit Logging. |
| 15 |
* |
| 16 |
* @since 5.8.0 |
| 17 |
*/ |
| 18 |
class Theme_Audit extends Audit_Event { |
| 19 |
use User; |
| 20 |
|
| 21 |
public const CONTEXT_NETWORK_THEME = 'network_theme'; |
| 22 |
public const ACTION_NETWORK_ACTIVATED = 'network_activated'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns the hooks to monitor theme events. |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public function get_hooks(): array { |
| 30 |
if ( is_multisite() ) { |
| 31 |
return array( |
| 32 |
'update_site_option_allowedthemes' => array( |
| 33 |
'args' => array( 'option', 'value', 'old_value', 'network_id' ), |
| 34 |
'event_type' => Audit_Log::EVENT_TYPE_SYSTEM, |
| 35 |
'context' => self::CONTEXT_NETWORK_THEME, |
| 36 |
'action_type' => self::ACTION_NETWORK_ACTIVATED, |
| 37 |
'callback' => array( $this, 'network_theme_enabled' ), |
| 38 |
), |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
return array(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Callback for logging when a theme is activated on network |
| 47 |
* |
| 48 |
* @param string $hook_name The hook name. |
| 49 |
* @param array $params Hook parameters. |
| 50 |
* |
| 51 |
* @return array An array containing the log text, context, and action type. |
| 52 |
*/ |
| 53 |
public function network_theme_enabled( string $hook_name, array $params ): array { |
| 54 |
unset( $hook_name ); // Not used, but kept for parity with callback signature. |
| 55 |
|
| 56 |
$new_allowed = is_array( $params['value'] ) ? $params['value'] : array(); |
| 57 |
$old_allowed = is_array( $params['old_value'] ) ? $params['old_value'] : array(); |
| 58 |
$new_themes = array_diff( array_keys( $new_allowed ), array_keys( $old_allowed ) ); |
| 59 |
|
| 60 |
if ( array() === $new_themes ) { |
| 61 |
return array(); |
| 62 |
} |
| 63 |
|
| 64 |
$theme_names = array(); |
| 65 |
foreach ( $new_themes as $theme_slug ) { |
| 66 |
$theme = wp_get_theme( $theme_slug ); |
| 67 |
$theme_names[] = $theme && $theme->exists() ? $theme->get( 'Name' ) : $theme_slug; |
| 68 |
} |
| 69 |
|
| 70 |
$blog_name = is_multisite() ? '[' . get_bloginfo( 'name' ) . ']' : ''; |
| 71 |
$message = sprintf( |
| 72 |
/* translators: 1: Blog theme, 2: Source of action (username or Hub) 3: Theme name. */ |
| 73 |
esc_html__( '%1$s %2$s activated theme %3$s on network', 'defender-security' ), |
| 74 |
$blog_name, |
| 75 |
$this->get_source_of_action(), |
| 76 |
implode( ', ', $theme_names ) |
| 77 |
); |
| 78 |
|
| 79 |
return array( $message, self::CONTEXT_NETWORK_THEME, self::ACTION_NETWORK_ACTIVATED ); |
| 80 |
} |
| 81 |
} |
| 82 |
|