| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_Theme extends Logtivity_Abstract_Logger |
| 4 |
{ |
| 5 |
public function registerHooks() |
| 6 |
{ |
| 7 |
add_action( 'switch_theme', [$this, 'themeSwitched'], 10, 3 ); |
| 8 |
add_action( 'upgrader_process_complete', [$this, 'upgradeProcessComplete'], 10, 2); |
| 9 |
add_filter( 'wp_theme_editor_filetypes', [$this, 'themeFileModified'], 10, 2 ); |
| 10 |
add_action( 'customize_save', [$this, 'themeCustomizerModified'], 10, 2 ); |
| 11 |
add_action( 'delete_site_transient_update_themes', [$this, 'themeMaybeDeleted'] ); |
| 12 |
} |
| 13 |
|
| 14 |
public function themeSwitched($new_name, $new_theme, $old_theme) |
| 15 |
{ |
| 16 |
return Logtivity_Logger::log() |
| 17 |
->setAction("Theme Switched") |
| 18 |
->addMeta('Old Theme', ( is_object($old_theme) ? $old_theme->name : $old_theme)) |
| 19 |
->addMeta('New Theme', ( is_object($new_theme) ? $new_theme->name : $new_theme)) |
| 20 |
->send(); |
| 21 |
} |
| 22 |
|
| 23 |
public function themeMaybeDeleted() |
| 24 |
{ |
| 25 |
$delete_theme_call = $this->getDeleteThemeCall(); |
| 26 |
|
| 27 |
if ( empty( $delete_theme_call ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$slug = $delete_theme_call['args'][0]; |
| 32 |
$theme = wp_get_theme( $slug ); |
| 33 |
|
| 34 |
return Logtivity_Logger::log() |
| 35 |
->setAction("Theme Deleted") |
| 36 |
->addMeta('Theme Name', $theme->get( 'Name' )) |
| 37 |
->addMeta('Version', $theme->get( 'Version' )) |
| 38 |
->addMeta('URI', $theme->get( 'ThemeURI' )) |
| 39 |
->send(); |
| 40 |
} |
| 41 |
|
| 42 |
public function upgradeProcessComplete( $upgrader_object, $options ) |
| 43 |
{ |
| 44 |
if ( $options['type'] != 'theme' ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if ($options['action'] == 'update') { |
| 49 |
|
| 50 |
return $this->themeUpdated($upgrader_object, $options); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
if ($options['action'] == 'install') { |
| 55 |
|
| 56 |
return $this->themeInstalled($upgrader_object, $options); |
| 57 |
|
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function themeUpdated($upgrader_object, $options) |
| 62 |
{ |
| 63 |
if ( isset( $options['bulk'] ) && true == $options['bulk'] ) { |
| 64 |
$slugs = $options['themes']; |
| 65 |
} |
| 66 |
else { |
| 67 |
$slugs = array( $upgrader->skin->theme ); |
| 68 |
} |
| 69 |
|
| 70 |
foreach ( $slugs as $slug ) |
| 71 |
{ |
| 72 |
$theme = wp_get_theme($slug); |
| 73 |
|
| 74 |
Logtivity_Logger::log() |
| 75 |
->setAction('Theme Updated') |
| 76 |
->addMeta('Name', $theme->name) |
| 77 |
->addMeta('Version', $theme->version) |
| 78 |
->addMeta('Bulk Update', $options['bulk']) |
| 79 |
->send(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function themeInstalled($upgrader_object, $options) |
| 84 |
{ |
| 85 |
$slug = $upgrader_object->theme_info(); |
| 86 |
|
| 87 |
if ( ! $slug ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
wp_clean_themes_cache(); |
| 92 |
|
| 93 |
$theme = wp_get_theme( $slug ); |
| 94 |
|
| 95 |
return Logtivity_Logger::log() |
| 96 |
->setAction('Theme Installed') |
| 97 |
->addMeta('Name', $theme->name) |
| 98 |
->addMeta('Version', $theme->version) |
| 99 |
->send(); |
| 100 |
} |
| 101 |
|
| 102 |
public function themeFileModified( $default_types, $theme ) |
| 103 |
{ |
| 104 |
if ( ! isset($_POST['action']) || $_POST['action'] != 'edit-theme-plugin-file' ) { |
| 105 |
return $default_types; |
| 106 |
} |
| 107 |
|
| 108 |
if (!isset($_POST['theme'])) { |
| 109 |
return $default_types; |
| 110 |
} |
| 111 |
|
| 112 |
$log = Logtivity_Logger::log()->setAction('Theme File Edited'); |
| 113 |
|
| 114 |
if ( ! empty( $_POST['file'] ) && is_string($_POST['file']) ) { |
| 115 |
|
| 116 |
$log->addMeta('File', sanitize_text_field($_POST['file'])); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
if ( ! empty( $_POST['theme'] ) ) { |
| 121 |
|
| 122 |
$log->addMeta('Theme', $theme->display( 'Name' )); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
$log->send(); |
| 127 |
|
| 128 |
return $default_types; |
| 129 |
} |
| 130 |
|
| 131 |
public function themeCustomizerModified( WP_Customize_Manager $obj ) |
| 132 |
{ |
| 133 |
return Logtivity_Logger::log() |
| 134 |
->setAction('Theme Customizer Updated') |
| 135 |
->addMeta('Name', $obj->theme()->display( 'Name' )) |
| 136 |
->send(); |
| 137 |
} |
| 138 |
|
| 139 |
private function getDeleteThemeCall() |
| 140 |
{ |
| 141 |
$backtrace = debug_backtrace(); |
| 142 |
|
| 143 |
$delete_theme_call = null; |
| 144 |
|
| 145 |
foreach ( $backtrace as $call ) { |
| 146 |
if ( isset( $call['function'] ) && 'delete_theme' === $call['function'] ) { |
| 147 |
$delete_theme_call = $call; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $delete_theme_call; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$Logtivity_Theme = new Logtivity_Theme; |