PluginProbe
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity / 3.3.10
Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity v3.3.10
3.3.10 3.3.9 3.3.8 trunk 1.0 1.1.0 1.10.0 1.11.0 1.11.1 1.12.0 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 1.20.0 1.20.1 1.3.0 1.3.1 1.4.0 1.5.0 All 68 releases
logtivity / Loggers / Core / Logtivity_Term.php

Logtivity_Term.php in Activity Logs, User Activity Tracking, Multisite Activity Log from Logtivity 3.3.10, at Loggers/Core/Logtivity_Term.php

132 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package Logtivity
5 * @contact logtivity.io, [email protected]
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 (in_array($taxonomy, $this->ignoreTaxonomies) == false) {
65 $term = get_term($termId, $taxonomy);
66
67 if ($term && $term instanceof WP_Term) {
68 $this->terms[$termId] = get_object_vars($term);
69 $this->metas[$termId] = $this->getMetadata('term', $termId);
70 }
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 $term = get_term($termId, $taxonomy);
86 if ($term && $term instanceof WP_Term) {
87 $term = get_object_vars($term);
88 }
89 $term = $term ?: [];
90 $oldTerm = $this->terms[$termId] ?? [];
91
92 $meta = $this->sanitizeDataFields($this->getMetadata('term', $termId));
93 $oldMeta = $this->sanitizeDataFields($this->metas[$termId] ?? []);
94
95 if ($term != $oldTerm || $meta != $oldMeta) {
96 Logtivity::log('Term ' . ($update ? 'Updated' : 'Created'))
97 ->setContext($taxonomy)
98 ->addMeta('Term ID', $term['term_id'])
99 ->addMeta('Name', $term['name'])
100 ->addMeta('Slug', $term['slug'])
101 ->addMeta('Edit', get_edit_term_link($termId))
102 ->addMetaChanges($oldTerm, $term)
103 ->addMetaChanges($oldMeta, $meta)
104 ->send();
105 }
106 }
107 }
108
109 /**
110 * @param int $termId
111 * @param int $ttId
112 * @param string $taxonomy
113 * @param WP_Term $term
114 *
115 * @return void
116 */
117 public function delete_term($termId, $ttId, $taxonomy, $term): void
118 {
119 if (in_array($taxonomy, $this->ignoreTaxonomies) == false) {
120 Logtivity::log()
121 ->setAction('Term Deleted')
122 ->setContext($taxonomy)
123 ->addMeta('Term ID', $term->term_id)
124 ->addMeta('Name', $term->name)
125 ->addMeta('Slug', $term->slug)
126 ->send();
127 }
128 }
129 }
130
131 new Logtivity_Term();
132