| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, hello@logtivity.io |
| 6 |
* @copyright 2024-2026 Logtivity. All rights reserved |
| 7 |
* @license https://www.gnu.org/licenses/gpl.html GNU/GPL |
| 8 |
* |
| 9 |
* This file is part of Logtivity. |
| 10 |
* |
| 11 |
* Logtivity is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* Logtivity is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with Logtivity. If not, see <https://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
class Logtivity_Term extends Logtivity_Abstract_Logger |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var string[] |
| 29 |
*/ |
| 30 |
protected array $ignoreTaxonomies = [ |
| 31 |
'nav_menu', |
| 32 |
'edd_log_type', |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var WP_Term[] |
| 37 |
*/ |
| 38 |
public array $terms = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var array[] |
| 42 |
*/ |
| 43 |
public array $metas = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* @inheritDoc |
| 47 |
*/ |
| 48 |
protected function registerHooks(): void |
| 49 |
{ |
| 50 |
add_action('edit_term', [$this, 'saveOriginalData'], 10, 3); |
| 51 |
add_action('saved_term', [$this, 'saved_term'], 10, 5); |
| 52 |
add_action('delete_term', [$this, 'delete_term'], 10, 5); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param int $termId |
| 57 |
* @param int $ttId |
| 58 |
* @param string $taxonomy |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function saveOriginalData($termId, $ttId, $taxonomy): void |
| 63 |
{ |
| 64 |
if ( |
| 65 |
in_array($taxonomy, $this->ignoreTaxonomies) == false |
| 66 |
&& ($term = get_term($termId, $taxonomy)) |
| 67 |
&& $term instanceof WP_Term |
| 68 |
) { |
| 69 |
$this->terms[$termId] = get_object_vars($term); |
| 70 |
$this->metas[$termId] = $this->getMetadata('term', $termId); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param int $termId |
| 76 |
* @param int $ttId |
| 77 |
* @param string $taxonomy |
| 78 |
* @param bool $update |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function saved_term($termId, $ttId, $taxonomy, $update): void |
| 83 |
{ |
| 84 |
if (in_array($taxonomy, $this->ignoreTaxonomies) == false) { |
| 85 |
if (($term = get_term($termId, $taxonomy)) && $term instanceof WP_Term) { |
| 86 |
$term = get_object_vars($term); |
| 87 |
} |
| 88 |
$term = $term ?: []; |
| 89 |
$oldTerm = $this->terms[$termId] ?? []; |
| 90 |
|
| 91 |
$meta = $this->sanitizeDataFields($this->getMetadata('term', $termId)); |
| 92 |
$oldMeta = $this->sanitizeDataFields($this->metas[$termId] ?? []); |
| 93 |
|
| 94 |
if ($term != $oldTerm || $meta != $oldMeta) { |
| 95 |
Logtivity::log('Term ' . ($update ? 'Updated' : 'Created')) |
| 96 |
->setContext($taxonomy) |
| 97 |
->addMeta('Term ID', $term['term_id']) |
| 98 |
->addMeta('Name', $term['name']) |
| 99 |
->addMeta('Slug', $term['slug']) |
| 100 |
->addMeta('Edit', get_edit_term_link($termId)) |
| 101 |
->addMetaChanges($oldTerm, $term) |
| 102 |
->addMetaChanges($oldMeta, $meta) |
| 103 |
->send(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param int $termId |
| 110 |
* @param int $ttId |
| 111 |
* @param string $taxonomy |
| 112 |
* @param WP_Term $term |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function delete_term($termId, $ttId, $taxonomy, $term): void |
| 117 |
{ |
| 118 |
if (in_array($taxonomy, $this->ignoreTaxonomies) == false) { |
| 119 |
Logtivity::log() |
| 120 |
->setAction('Term Deleted') |
| 121 |
->setContext($taxonomy) |
| 122 |
->addMeta('Term ID', $term->term_id) |
| 123 |
->addMeta('Name', $term->name) |
| 124 |
->addMeta('Slug', $term->slug) |
| 125 |
->send(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
new Logtivity_Term(); |
| 131 |
|