← All changes
|
Loggers/Code_Snippets/Logtivity_Code_Snippets.php
+59
-20
3.3.8
→
3.3.10
View file →
| @@ -21,8 +21,9 @@ | ||
| 21 | 21 | * You should have received a copy of the GNU General Public License |
| 22 | 22 | * along with Logtivity. If not, see <https://www.gnu.org/licenses/>. |
| 23 | 23 | */ |
| 24 | 24 | |
| 25 | +use Code_Snippets\Model; | |
| 25 | 26 | use Code_Snippets\Snippet; |
| 26 | 27 | |
| 27 | 28 | class Logtivity_Code_Snippets |
| 28 | 29 | { |
| @@ -27,8 +28,9 @@ | ||
| 27 | 28 | class Logtivity_Code_Snippets |
| 28 | 29 | { |
| 29 | 30 | public function __construct() |
| 30 | 31 | { |
| 32 | + add_action('code_snippets/trash_snippet', [$this, 'snippetTrashed'], 10, 2); | |
| 31 | 33 | add_action('code_snippets/delete_snippet', [$this, 'snippetDeleted'], 10, 2); |
| 32 | 34 | add_action('code_snippets/create_snippet', [$this, 'snippetCreated'], 10, 2); |
| 33 | 35 | add_action('code_snippets/update_snippet', [$this, 'snippetUpdated'], 10, 2); |
| 34 | 36 | add_action('code_snippets/activate_snippet', [$this, 'snippetActivated'], 10, 2); |
| @@ -35,61 +37,98 @@ | ||
| 35 | 37 | add_action('code_snippets/deactivate_snippet', [$this, 'snippetDeactivated'], 10, 2); |
| 36 | 38 | } |
| 37 | 39 | |
| 38 | 40 | /** |
| 39 | - * @param int $snippetId | |
| 40 | - * @param ?bool $network | |
| 41 | + * @param Model\Snippet $snippet | |
| 41 | 42 | * |
| 42 | 43 | * @return void |
| 43 | 44 | */ |
| 44 | - public function snippetDeleted(int $snippetId, ?bool $network): void | |
| 45 | + public function snippetTrashed($snippet): void | |
| 45 | 46 | { |
| 46 | - Logtivity::log() | |
| 47 | + $logger = Logtivity::log() | |
| 48 | + ->setAction('Code Snippet Trashed') | |
| 49 | + ->setContext($snippet->id); | |
| 50 | + | |
| 51 | + $this->logWithMeta($logger, $snippet); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * @param int|Model\Snippet $snippet | |
| 56 | + * @param ?bool $network | |
| 57 | + * | |
| 58 | + * @return void | |
| 59 | + */ | |
| 60 | + public function snippetDeleted($snippet, $network): void | |
| 61 | + { | |
| 62 | + $snippetId = $snippet->id ?? $snippet; | |
| 63 | + | |
| 64 | + $logger = Logtivity::log() | |
| 47 | 65 | ->setAction('Code Snippet Deleted') |
| 48 | - ->setContext($snippetId) | |
| 49 | - ->addMeta('Network', $network ? 'Yes' : 'No') | |
| 50 | - ->send(); | |
| 66 | + ->setContext($snippetId); | |
| 67 | + | |
| 68 | + if (is_object($snippet)) { | |
| 69 | + $this->logWithMeta($logger, $snippet); | |
| 70 | + | |
| 71 | + } else { | |
| 72 | + $logger | |
| 73 | + ->addMeta('Network', $network ? 'Yes' : 'No') | |
| 74 | + ->send(); | |
| 75 | + } | |
| 51 | 76 | } |
| 52 | 77 | |
| 53 | 78 | /** |
| 54 | - * @param Snippet $snippet | |
| 79 | + * @param Snippet|Model\Snippet $snippet | |
| 80 | + * @param string $table | |
| 55 | 81 | * |
| 56 | 82 | * @return void |
| 57 | 83 | */ |
| 58 | - public function snippetCreated(Snippet $snippet): void | |
| 84 | + public function snippetCreated($snippet, $table): void | |
| 59 | 85 | { |
| 60 | - $cloneId = sanitize_text_field($_GET['action'] ?? null) == 'clone' | |
| 61 | - ? sanitize_text_field($_GET['id'] ?? null) | |
| 86 | + // Will not work in newer versions of Code Snippets | |
| 87 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Passive logger reads Code Snippets request state after that plugin handles auth. | |
| 88 | + $action = isset($_GET['action']) | |
| 89 | + ? sanitize_text_field(wp_unslash($_GET['action'])) | |
| 62 | 90 | : null; |
| 91 | + $id = isset($_GET['id']) | |
| 92 | + ? sanitize_text_field(wp_unslash($_GET['id'])) | |
| 93 | + : null; | |
| 94 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended | |
| 63 | 95 | |
| 96 | + $cloneId = $action == 'clone' | |
| 97 | + ? $id | |
| 98 | + : null; | |
| 99 | + | |
| 64 | 100 | $logger = Logtivity::log() |
| 65 | 101 | ->setAction('Code Snippet Created') |
| 66 | 102 | ->setContext($snippet->id) |
| 67 | - ->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId); | |
| 103 | + ->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId) | |
| 104 | + ->addMetaIf((bool)$table, 'Table', $table); | |
| 68 | 105 | |
| 69 | 106 | $this->logWithMeta($logger, $snippet); |
| 70 | 107 | } |
| 71 | 108 | |
| 72 | 109 | /** |
| 73 | - * @param Snippet $snippet | |
| 110 | + * @param Snippet|Model\Snippet $snippet | |
| 111 | + * @param string $table | |
| 74 | 112 | * |
| 75 | 113 | * @return void |
| 76 | 114 | */ |
| 77 | - public function snippetUpdated(Snippet $snippet): void | |
| 115 | + public function snippetUpdated($snippet, $table): void | |
| 78 | 116 | { |
| 79 | 117 | $logger = Logtivity::log() |
| 80 | 118 | ->setAction('Code Snippet Updated') |
| 81 | - ->setContext($snippet->id); | |
| 119 | + ->setContext($snippet->id) | |
| 120 | + ->addMetaIf((bool)$table, 'Table', $table); | |
| 82 | 121 | |
| 83 | 122 | $this->logWithMeta($logger, $snippet); |
| 84 | 123 | } |
| 85 | 124 | |
| 86 | 125 | /** |
| 87 | - * @param Snippet $snippet | |
| 126 | + * @param Snippet|Model\Snippet $snippet | |
| 88 | 127 | * |
| 89 | 128 | * @return void |
| 90 | 129 | */ |
| 91 | - public function snippetActivated(Snippet $snippet): void | |
| 130 | + public function snippetActivated($snippet): void | |
| 92 | 131 | { |
| 93 | 132 | Logtivity::log() |
| 94 | 133 | ->setAction('Code Snippet Activated') |
| 95 | 134 | ->setContext($snippet->id) |
| @@ -111,14 +150,14 @@ | ||
| 111 | 150 | ->send(); |
| 112 | 151 | } |
| 113 | 152 | |
| 114 | 153 | /** |
| 115 | - * @param Logtivity_Logger $logger | |
| 116 | - * @param ?Snippet $snippet | |
| 154 | + * @param Logtivity_Logger $logger | |
| 155 | + * @param Snippet|Model\Snippet $snippet | |
| 117 | 156 | * |
| 118 | 157 | * @return void |
| 119 | 158 | */ |
| 120 | - public function logWithMeta(Logtivity_Logger $logger, ?Snippet $snippet = null): void | |
| 159 | + public function logWithMeta(Logtivity_Logger $logger, object $snippet): void | |
| 121 | 160 | { |
| 122 | 161 | $logger |
| 123 | 162 | ->addMeta('Snippet Name', $snippet->name) |
| 124 | 163 | ->addMeta('Active', $snippet->active ? 'Yes' : 'No') |