| @@ -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,29 +37,54 @@ | ||
| 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 | { |
| 86 | + // Will not work in newer versions of Code Snippets | |
| 60 | 87 | $cloneId = sanitize_text_field($_GET['action'] ?? null) == 'clone' |
| 61 | 88 | ? sanitize_text_field($_GET['id'] ?? null) |
| 62 | 89 | : null; |
| 63 | 90 | |
| @@ -63,33 +90,36 @@ | ||
| 63 | 90 | |
| 64 | 91 | $logger = Logtivity::log() |
| 65 | 92 | ->setAction('Code Snippet Created') |
| 66 | 93 | ->setContext($snippet->id) |
| 67 | - ->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId); | |
| 94 | + ->addMetaIf($cloneId !== null, 'Cloned From ID', $cloneId) | |
| 95 | + ->addMetaIf((bool)$table, 'Table', $table); | |
| 68 | 96 | |
| 69 | 97 | $this->logWithMeta($logger, $snippet); |
| 70 | 98 | } |
| 71 | 99 | |
| 72 | 100 | /** |
| 73 | - * @param Snippet $snippet | |
| 101 | + * @param Snippet|Model\Snippet $snippet | |
| 102 | + * @param string $table | |
| 74 | 103 | * |
| 75 | 104 | * @return void |
| 76 | 105 | */ |
| 77 | - public function snippetUpdated(Snippet $snippet): void | |
| 106 | + public function snippetUpdated($snippet, $table): void | |
| 78 | 107 | { |
| 79 | 108 | $logger = Logtivity::log() |
| 80 | 109 | ->setAction('Code Snippet Updated') |
| 81 | - ->setContext($snippet->id); | |
| 110 | + ->setContext($snippet->id) | |
| 111 | + ->addMetaIf((bool)$table, 'Table', $table); | |
| 82 | 112 | |
| 83 | 113 | $this->logWithMeta($logger, $snippet); |
| 84 | 114 | } |
| 85 | 115 | |
| 86 | 116 | /** |
| 87 | - * @param Snippet $snippet | |
| 117 | + * @param Snippet|Model\Snippet $snippet | |
| 88 | 118 | * |
| 89 | 119 | * @return void |
| 90 | 120 | */ |
| 91 | - public function snippetActivated(Snippet $snippet): void | |
| 121 | + public function snippetActivated($snippet): void | |
| 92 | 122 | { |
| 93 | 123 | Logtivity::log() |
| 94 | 124 | ->setAction('Code Snippet Activated') |
| 95 | 125 | ->setContext($snippet->id) |
| @@ -111,14 +141,14 @@ | ||
| 111 | 141 | ->send(); |
| 112 | 142 | } |
| 113 | 143 | |
| 114 | 144 | /** |
| 115 | - * @param Logtivity_Logger $logger | |
| 116 | - * @param ?Snippet $snippet | |
| 145 | + * @param Logtivity_Logger $logger | |
| 146 | + * @param Snippet|Model\Snippet $snippet | |
| 117 | 147 | * |
| 118 | 148 | * @return void |
| 119 | 149 | */ |
| 120 | - public function logWithMeta(Logtivity_Logger $logger, ?Snippet $snippet = null): void | |
| 150 | + public function logWithMeta(Logtivity_Logger $logger, object $snippet): void | |
| 121 | 151 | { |
| 122 | 152 | $logger |
| 123 | 153 | ->addMeta('Snippet Name', $snippet->name) |
| 124 | 154 | ->addMeta('Active', $snippet->active ? 'Yes' : 'No') |