/
SimplePluginLogger.php
1179 lines (985 loc) Β· 49 KB
/
SimplePluginLogger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
defined('ABSPATH') || die();
/**
* Logs plugin installs, updates, and deletions.
*/
class SimplePluginLogger extends SimpleLogger
{
/**
* The logger slug. Defaulting to the class name is nice and logical I think.
*
* @var string $slug
*/
public $slug = __CLASS__;
/**
* This variable is set if a plugins has been disabled due to an error,
* like when the plugin file does not exist. We need to store this in this
* weird way because there is no other way for us to get the reason.
*
* @var string $latest_plugin_deactivation_because_of_error_reason
*/
public $latest_plugin_deactivation_because_of_error_reason = array();
/**
* Get array with information about this logger
*
* @return array
*/
public function getInfo()
{
$arr_info = array(
'name' => 'Plugin Logger',
'description' => 'Logs plugin installs, uninstalls and updates',
'capability' => 'activate_plugins',
'messages' => array(
'plugin_activated' => _x(
'Activated plugin "{plugin_name}"',
'Plugin was non-silently activated by a user',
'simple-history'
),
'plugin_deactivated' => _x(
'Deactivated plugin "{plugin_name}"',
'Plugin was non-silently deactivated by a user',
'simple-history'
),
'plugin_installed' => _x(
'Installed plugin "{plugin_name}"',
'Plugin was installed',
'simple-history'
),
'plugin_installed_failed' => _x(
'Failed to install plugin "{plugin_name}"',
'Plugin failed to install',
'simple-history'
),
'plugin_updated' => _x(
'Updated plugin "{plugin_name}" to version {plugin_version} from {plugin_prev_version}',
'Plugin was updated',
'simple-history'
),
'plugin_update_failed' => _x(
'Failed to update plugin "{plugin_name}"',
'Plugin update failed',
'simple-history'
),
'plugin_deleted' => _x(
'Deleted plugin "{plugin_name}"',
'Plugin files was deleted',
'simple-history'
),
// Bulk versions.
'plugin_bulk_updated' => _x(
'Updated plugin "{plugin_name}" to {plugin_version} from {plugin_prev_version}',
'Plugin was updated in bulk',
'simple-history'
),
// Plugin disabled due to some error.
'plugin_disabled_because_error' => _x(
'Deactivated plugin "{plugin_slug}" because of an error ("{deactivation_reason}").',
'Plugin was disabled because of an error',
'simple-history'
),
), // Messages.
'labels' => array(
'search' => array(
'label' => _x('Plugins', 'Plugin logger: search', 'simple-history'),
'label_all' => _x('All plugin activity', 'Plugin logger: search', 'simple-history'),
'options' => array(
_x('Activated plugins', 'Plugin logger: search', 'simple-history') => array(
'plugin_activated',
),
_x('Deactivated plugins', 'Plugin logger: search', 'simple-history') => array(
'plugin_deactivated',
'plugin_disabled_because_error',
),
_x('Installed plugins', 'Plugin logger: search', 'simple-history') => array(
'plugin_installed',
),
_x('Failed plugin installs', 'Plugin logger: search', 'simple-history') => array(
'plugin_installed_failed',
),
_x('Updated plugins', 'Plugin logger: search', 'simple-history') => array(
'plugin_updated',
'plugin_bulk_updated',
),
_x('Failed plugin updates', 'Plugin logger: search', 'simple-history') => array(
'plugin_update_failed',
),
_x('Deleted plugins', 'Plugin logger: search', 'simple-history') => array(
'plugin_deleted',
),
),
), // search array.
), // labels.
);
return $arr_info;
}
/**
* Plugin loaded
*/
public function loaded()
{
/**
* At least the plugin bulk upgrades fires this action before upgrade
* We use it to fetch the current version of all plugins, before they are upgraded
*/
add_filter('upgrader_pre_install', array( $this, 'save_versions_before_update' ), 10, 2);
// Clear our transient after an update is done
// Removed because something probably changed in core and this was fired earlier than it used to be
// add_action( 'delete_site_transient_update_plugins', array( $this, "remove_saved_versions" ) );
// Fires after a plugin has been activated.
// If a plugin is silently activated (such as during an update),
// this hook does not fire.
add_action('activated_plugin', array( $this, 'on_activated_plugin' ), 10, 2);
// Fires after a plugin is deactivated.
// If a plugin is silently deactivated (such as during an update),
// this hook does not fire.
add_action('deactivated_plugin', array( $this, 'on_deactivated_plugin' ), 10, 2);
// Fires after the upgrades has done it's thing.
// Check hook extra for upgrader initiator.
add_action('upgrader_process_complete', array( $this, 'on_upgrader_process_complete' ), 10, 2);
// Detect files removed.
add_action('setted_transient', array( $this, 'on_setted_transient_for_remove_files' ), 10, 2);
add_action('admin_action_delete-selected', array( $this, 'on_action_delete_selected' ), 10, 1);
// Ajax function to get info from GitHub repo. Used by "View plugin info"-link for plugin installs.
add_action('wp_ajax_SimplePluginLogger_GetGitHubPluginInfo', array( $this, 'ajax_GetGitHubPluginInfo' ));
// If the Github Update plugin is not installed we need to get extra fields used by it.
// So need to hook filter "extra_plugin_headers" ourself.
add_filter(
'extra_plugin_headers',
function ($arr_headers) {
$arr_headers[] = 'GitHub Plugin URI';
return $arr_headers;
}
);
// There is no way to use a filter and detect a plugin that is disabled because it can't be found or similar error.
// So we hook into gettext and look for the usage of the error that is returned when this happens.
add_filter('gettext', array( $this, 'on_gettext_detect_plugin_error_deactivation_reason' ), 10, 3);
add_filter('gettext', array( $this, 'on_gettext' ), 10, 3);
}
/**
* Things
*
* @param string $translation Translation.
* @param string $text Text.
* @param string $domain Domin.
*/
function on_gettext_detect_plugin_error_deactivation_reason($translation, $text, $domain)
{
global $pagenow;
// We only act on page plugins.php.
if (! isset($pagenow) || 'plugins.php' !== $pagenow) {
return $translation;
}
// We only act if the untranslated text is among the following ones
// (Literally these, no translation).
$untranslated_texts = array(
'Plugin file does not exist.',
'Invalid plugin path.',
'The plugin does not have a valid header.',
);
if (! in_array($text, $untranslated_texts, true)) {
return $translation;
}
// Text was among our wanted texts.
switch ($text) {
case 'Plugin file does not exist.':
$this->latest_plugin_deactivation_because_of_error_reason[] = 'file_does_not_exist';
break;
case 'Invalid plugin path.':
$this->latest_plugin_deactivation_because_of_error_reason[] = 'invalid_path';
break;
case 'The plugin does not have a valid header.':
$this->latest_plugin_deactivation_because_of_error_reason[] = 'no_valid_header';
break;
}
return $translation;
}
/**
* There is no way to use a filter and detect a plugin that is disabled because it can't be found or similar error.
* we hook into gettext and look for the usage of the error that is returned when this happens.
*
* A plugin gets deactivated when plugins.php is visited function validate_active_plugins()
* return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
* and if invalid plugin is found then this is outputed
* printf(
* /* translators: 1: plugin file 2: error message
* __( 'The plugin %1$s has been <strong>deactivated</strong> due to an error: %2$s' ),
* '<code>' . esc_html( $plugin_file ) . '</code>',
* $error->get_error_message() );
*
* @param string $translation Translation.
* @param string $text Text.
* @param string $domain Domin.
*/
function on_gettext($translation, $text, $domain)
{
global $pagenow;
// We only act on page plugins.php.
if (! isset($pagenow) || 'plugins.php' !== $pagenow) {
return $translation;
}
// We only act if the untranslated text is among the following ones
// (Literally these, no translation)
$untranslated_texts = array(
// This string is called later than the above
'The plugin %1$s has been <strong>deactivated</strong> due to an error: %2$s',
);
if (! in_array($text, $untranslated_texts)) {
return $translation;
}
// Directly after the string is translated 'esc_html' is called with the plugin name.
// This is one of the few ways we can get the name of the plugin.
// The esc_html filter is used pretty much but we make sure we only do our.
// stuff the first time it's called (directly after the gettet for the plugin disabled-error..).
$logger_instance = $this;
add_filter(
'esc_html',
function ($safe_text, $text) use ($logger_instance) {
static $is_called = false;
if (false === $is_called) {
$is_called = true;
$deactivation_reason = array_shift($logger_instance->latest_plugin_deactivation_because_of_error_reason);
// We don't know what plugin that was that got this error and currently there does not seem to be a way to determine that.
// So that's why we use such generic log messages.
$logger_instance->warningMessage(
'plugin_disabled_because_error',
array(
'_initiator' => SimpleLoggerLogInitiators::WORDPRESS,
'plugin_slug' => $text,
'deactivation_reason' => $deactivation_reason,
)
);
}
return $safe_text;
},
10,
2
);
return $translation;
} // on_gettext
/**
* Show readme from github in a modal win
*/
function ajax_GetGitHubPluginInfo()
{
if (! current_user_can('install_plugins')) {
wp_die(__("You don't have access to this page.", 'simple-history'));
}
$repo = isset($_GET['repo']) ? (string) $_GET['repo'] : '';
if (! $repo) {
wp_die(__('Could not find GitHub repository.', 'simple-history'));
}
$repo_parts = explode('/', rtrim($repo, '/'));
if (count($repo_parts) !== 5) {
wp_die(__('Could not find GitHub repository.', 'simple-history'));
}
$repo_username = $repo_parts[3];
$repo_repo = $repo_parts[4];
// https://developer.github.com/v3/repos/contents/
// https://api.github.com/repos/<username>/<repo>/readme
$api_url = sprintf('https://api.github.com/repos/%1$s/%2$s/readme', urlencode($repo_username), urlencode($repo_repo));
// Get file. Use accept-header to get file as HTML instead of JSON
$response = wp_remote_get(
$api_url,
array(
'headers' => array(
'accept' => 'application/vnd.github.VERSION.html',
),
)
);
$response_body = wp_remote_retrieve_body($response);
$repo_info = '<p>' . sprintf(
__('Viewing <code>readme</code> from repository <code><a target="_blank" href="%1$s">%2$s</a></code>.', 'simple-history'),
esc_url($repo),
esc_html($repo)
) . '</p>';
$github_markdown_css_path = SIMPLE_HISTORY_PATH . '/css/github-markdown.css';
printf(
'
<!doctype html>
<style>
body {
font-family: sans-serif;
font-size: 16px;
}
.repo-info {
padding: 1.25em 1em;
background: #fafafa;
line-height: 1;
}
.repo-info p {
margin: 0;
}
.markdown-body {
min-width: 200px;
max-width: 790px;
margin: 0 auto;
padding: 30px;
}
@import url("%3$s");
</style>
<base href="%4$s/raw/master/">
<header class="repo-info">
%1$s
</header>
<div class="markdown-body readme-contents">
%2$s
</div>
',
$repo_info,
$response_body,
$github_markdown_css_path,
esc_url($repo) // 4
);
// echo($response_body);
exit;
}
/*
* When a plugin has been deleted there is no way for us to get
* the real name of the plugin, only the dir and main index file.
* So before a plugin is deleted we save all needed info in a transient
*/
function on_action_delete_selected()
{
// Same as in plugins.php
if (! current_user_can('delete_plugins')) {
wp_die(__('You do not have sufficient permissions to delete plugins for this site.'));
}
// Verify delete must be set
if (! isset($_POST['verify-delete']) || ! $_POST['verify-delete']) {
return;
}
// An arr of plugins must be set
if (! isset($_POST['checked']) || ! is_array($_POST['checked'])) {
return;
}
// If we get this far it looks like a plugin is begin deleted
// Get and save info about it
$this->save_versions_before_update();
}
/**
* Saves info about all installed plugins to an option.
* When we are done logging then we remove the option.
*/
function save_versions_before_update($bool = null, $hook_extra = null)
{
$plugins = get_plugins();
// does not work
$option_name = $this->slug . '_plugin_info_before_update';
$r = update_option($option_name, SimpleHistory::json_encode($plugins));
return $bool;
}
/**
* Detect plugin being deleted
* When WP is done deleting a plugin it sets a transient called plugins_delete_result:
* set_transient('plugins_delete_result_' . $user_ID, $delete_result);
*
* We detect when that transient is set and then we have all info needed to log the plugin delete
*/
public function on_setted_transient_for_remove_files($transient = '', $value = '')
{
if (! $user_id = get_current_user_id()) {
return;
}
$transient_name = '_transient_plugins_delete_result_' . $user_id;
if ($transient_name !== $transient) {
return;
}
// We found the transient we were looking for
if (isset($_POST['action'])
&& 'delete-selected' == $_POST['action']
&& isset($_POST['checked'])
&& is_array($_POST['checked'])
) {
/*
[checked] => Array
(
[0] => the-events-calendar/the-events-calendar.php
)
*/
$plugins_deleted = $_POST['checked'];
$plugins_before_update = json_decode(get_option($this->slug . '_plugin_info_before_update', false), true);
foreach ($plugins_deleted as $plugin) {
$context = array(
'plugin' => $plugin, // plugin-name-folder/plugin-main-file.php
);
if (is_array($plugins_before_update) && isset($plugins_before_update[ $plugin ])) {
$context['plugin_name'] = $plugins_before_update[ $plugin ]['Name'];
$context['plugin_title'] = $plugins_before_update[ $plugin ]['Title'];
$context['plugin_description'] = $plugins_before_update[ $plugin ]['Description'];
$context['plugin_author'] = $plugins_before_update[ $plugin ]['Author'];
$context['plugin_version'] = $plugins_before_update[ $plugin ]['Version'];
$context['plugin_url'] = $plugins_before_update[ $plugin ]['PluginURI'];
}
$this->infoMessage(
'plugin_deleted',
$context
);
}
}
$this->remove_saved_versions();
}
/**
* Save all plugin information before a plugin is updated or removed.
* This way we can know both the old (pre updated/removed) and the current version of the plugin
*/
/*
public function save_versions_before_update() {
$current_screen = get_current_screen();
$request_uri = $_SERVER["SCRIPT_NAME"];
// Only add option on pages where needed
$do_store = false;
if (
SimpleHistory::ends_with( $request_uri, "/wp-admin/update.php" )
&& isset( $current_screen->base )
&& "update" == $current_screen->base
) {
// Plugin update screen
$do_store = true;
} else if (
SimpleHistory::ends_with( $request_uri, "/wp-admin/plugins.php" )
&& isset( $current_screen->base )
&& "plugins" == $current_screen->base
&& ( isset( $_POST["action"] ) && "delete-selected" == $_POST["action"] )
) {
// Plugin delete screen, during delete
$do_store = true;
}
if ( $do_store ) {
update_option( $this->slug . "_plugin_info_before_update", SimpleHistory::json_encode( get_plugins() ) );
}
}
*/
/**
* when plugin updates are done wp_clean_plugins_cache() is called,
* which in its turn run:
* delete_site_transient( 'update_plugins' );
* do_action( 'delete_site_transient_' . $transient, $transient );
* delete_site_transient_update_plugins
*/
public function remove_saved_versions()
{
delete_option($this->slug . '_plugin_info_before_update');
}
/**
* Called when plugins is updated or installed
* Called from class-wp-upgrader.php
*
* @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
* be a Theme_Upgrader or Core_Upgrade instance.
* @param array $data {
* Array of bulk item update data.
*/
function on_upgrader_process_complete($plugin_upgrader_instance, $arr_data)
{
// Can't use get_plugins() here to get version of plugins updated from
// Tested that, and it will get the new version (and that's the correct answer I guess. but too bad for us..)
/*
If an update fails then $plugin_upgrader_instance->skin->result->errors contains something like:
Array
(
[remove_old_failed] => Array
(
[0] => Could not remove the old plugin.
)
)
*/
/*
# Contents of $arr_data in different scenarios
## WordPress core update
$arr_data:
Array
(
[action] => update
[type] => core
)
# Plugin install
$arr_data:
Array
(
[type] => plugin
[action] => install
)
## Plugin update
$arr_data:
Array
(
[type] => plugin
[action] => install
)
## Bulk actions
array(
'action' => 'update',
'type' => 'plugin',
'bulk' => true,
'plugins' => $plugins,
)
*/
// To keep track of if something was logged, so wen can output debug info only
// only if we did not log anything
$did_log = false;
if (isset($arr_data['type']) && 'plugin' == $arr_data['type']) {
// Single plugin install
if (isset($arr_data['action']) && 'install' == $arr_data['action'] && ! $plugin_upgrader_instance->bulk) {
$upgrader_skin_options = isset($plugin_upgrader_instance->skin->options) && is_array($plugin_upgrader_instance->skin->options) ? $plugin_upgrader_instance->skin->options : array();
$upgrader_skin_result = isset($plugin_upgrader_instance->skin->result) && is_array($plugin_upgrader_instance->skin->result) ? $plugin_upgrader_instance->skin->result : array();
$upgrader_skin_api = isset($plugin_upgrader_instance->skin->api) ? $plugin_upgrader_instance->skin->api : (object) array();
$plugin_slug = isset($upgrader_skin_result['destination_name']) ? $upgrader_skin_result['destination_name'] : '';
// Upgrader contains current info
$context = array(
'plugin_slug' => $plugin_slug,
'plugin_name' => isset($upgrader_skin_api->name) ? $upgrader_skin_api->name : '',
'plugin_version' => isset($upgrader_skin_api->version) ? $upgrader_skin_api->version : '',
'plugin_author' => isset($upgrader_skin_api->author) ? $upgrader_skin_api->author : '',
'plugin_last_updated' => isset($upgrader_skin_api->last_updated) ? $upgrader_skin_api->last_updated : '',
'plugin_requires' => isset($upgrader_skin_api->requires) ? $upgrader_skin_api->requires : '',
'plugin_tested' => isset($upgrader_skin_api->tested) ? $upgrader_skin_api->tested : '',
'plugin_rating' => isset($upgrader_skin_api->rating) ? $upgrader_skin_api->rating : '',
'plugin_num_ratings' => isset($upgrader_skin_api->num_ratings) ? $upgrader_skin_api->num_ratings : '',
'plugin_downloaded' => isset($upgrader_skin_api->downloaded) ? $upgrader_skin_api->downloaded : '',
'plugin_added' => isset($upgrader_skin_api->added) ? $upgrader_skin_api->added : '',
'plugin_source_files' => $this->simpleHistory->json_encode($plugin_upgrader_instance->result['source_files']),
// To debug comment out these:
// "debug_skin_options" => $this->simpleHistory->json_encode( $upgrader_skin_options ),
// "debug_skin_result" => $this->simpleHistory->json_encode( $upgrader_skin_result ),
);
/*
Detect install plugin from wordpress.org
- options[type] = "web"
- options[api] contains all we need
Detect install from upload ZIP
- options[type] = "upload"
Also: plugins hosted at GitHub have a de-facto standard field of "GitHub Plugin URI"
*/
$install_source = 'unknown';
if (isset($upgrader_skin_options['type'])) {
$install_source = (string) $upgrader_skin_options['type'];
}
$context['plugin_install_source'] = $install_source;
// If uploaded plugin store name of ZIP
if ('upload' == $install_source) {
/*
_debug_files
{
"pluginzip": {
"name": "WPThumb-master.zip",
"type": "application\/zip",
"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpnThImc",
"error": 0,
"size": 2394625
}
}
*/
if (isset($_FILES['pluginzip']['name'])) {
$plugin_upload_name = $_FILES['pluginzip']['name'];
$context['plugin_upload_name'] = $plugin_upload_name;
}
}
if (is_a($plugin_upgrader_instance->skin->result, 'WP_Error')) {
// Add errors
// Errors is in original wp admin language
$context['error_messages'] = $this->simpleHistory->json_encode($plugin_upgrader_instance->skin->result->errors);
$context['error_data'] = $this->simpleHistory->json_encode($plugin_upgrader_instance->skin->result->error_data);
$this->infoMessage(
'plugin_installed_failed',
$context
);
$did_log = true;
} else {
// Plugin was successfully installed
// Try to grab more info from the readme
// Would be nice to grab a screenshot, but that is difficult since they often are stored remotely
$plugin_destination = isset($plugin_upgrader_instance->result['destination']) ? $plugin_upgrader_instance->result['destination'] : null;
if ($plugin_destination) {
$plugin_info = $plugin_upgrader_instance->plugin_info();
$plugin_data = array();
if (file_exists(WP_PLUGIN_DIR . '/' . $plugin_info)) {
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_info, true, false);
}
$context['plugin_name'] = isset($plugin_data['Name']) ? $plugin_data['Name'] : '';
$context['plugin_description'] = isset($plugin_data['Description']) ? $plugin_data['Description'] : '';
$context['plugin_url'] = isset($plugin_data['PluginURI']) ? $plugin_data['PluginURI'] : '';
$context['plugin_version'] = isset($plugin_data['Version']) ? $plugin_data['Version'] : '';
$context['plugin_author'] = isset($plugin_data['AuthorName']) ? $plugin_data['AuthorName'] : '';
// Comment out these to debug plugin installs
// $context["debug_plugin_data"] = $this->simpleHistory->json_encode( $plugin_data );
// $context["debug_plugin_info"] = $this->simpleHistory->json_encode( $plugin_info );
if (! empty($plugin_data['GitHub Plugin URI'])) {
$context['plugin_github_url'] = $plugin_data['GitHub Plugin URI'];
}
}
$this->infoMessage(
'plugin_installed',
$context
);
$did_log = true;
}// End if().
} // End if().
// Single plugin update
if (isset($arr_data['action']) && 'update' == $arr_data['action'] && ! $plugin_upgrader_instance->bulk) {
// No plugin info in instance, so get it ourself
$plugin_data = array();
if (file_exists(WP_PLUGIN_DIR . '/' . $arr_data['plugin'])) {
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $arr_data['plugin'], true, false);
}
// autoptimize/autoptimize.php
$plugin_slug = dirname($arr_data['plugin']);
$context = array(
'plugin_slug' => $plugin_slug,
'request' => $this->simpleHistory->json_encode($_REQUEST),
'plugin_name' => $plugin_data['Name'],
'plugin_title' => $plugin_data['Title'],
'plugin_description' => $plugin_data['Description'],
'plugin_author' => $plugin_data['Author'],
'plugin_version' => $plugin_data['Version'],
'plugin_url' => $plugin_data['PluginURI'],
'plugin_source_files' => $this->simpleHistory->json_encode($plugin_upgrader_instance->result['source_files']),
);
// update status for plugins are in response
// plugin folder + index file = key
// use transient to get url and package
$update_plugins = get_site_transient('update_plugins');
if ($update_plugins && isset($update_plugins->response[ $arr_data['plugin'] ])) {
/*
$update_plugins[plugin_path/slug]:
{
"id": "8986",
"slug": "autoptimize",
"plugin": "autoptimize/autoptimize.php",
"new_version": "1.9.1",
"url": "https://wordpress.org/plugins/autoptimize/",
"package": "https://downloads.wordpress.org/plugin/autoptimize.1.9.1.zip"
}
*/
// for debug purposes the update_plugins key can be added
// $context["update_plugins"] = $this->simpleHistory->json_encode( $update_plugins );
$plugin_update_info = $update_plugins->response[ $arr_data['plugin'] ];
// autoptimize/autoptimize.php
if (isset($plugin_update_info->plugin)) {
$context['plugin_update_info_plugin'] = $plugin_update_info->plugin;
}
// https://downloads.wordpress.org/plugin/autoptimize.1.9.1.zip
if (isset($plugin_update_info->package)) {
$context['plugin_update_info_package'] = $plugin_update_info->package;
}
}
// To get old version we use our option
$plugins_before_update = json_decode(get_option($this->slug . '_plugin_info_before_update', false), true);
if (is_array($plugins_before_update) && isset($plugins_before_update[ $arr_data['plugin'] ])) {
$context['plugin_prev_version'] = $plugins_before_update[ $arr_data['plugin'] ]['Version'];
}
if (is_a($plugin_upgrader_instance->skin->result, 'WP_Error')) {
// Add errors
// Errors is in original wp admin language
$context['error_messages'] = json_encode($plugin_upgrader_instance->skin->result->errors);
$context['error_data'] = json_encode($plugin_upgrader_instance->skin->result->error_data);
$this->infoMessage(
'plugin_update_failed',
$context
);
$did_log = true;
} else {
$this->infoMessage(
'plugin_updated',
$context
);
// echo "on_upgrader_process_complete";
// sf_d( $plugin_upgrader_instance, '$plugin_upgrader_instance' );
// sf_d( $arr_data, '$arr_data' );
$did_log = true;
}
} // End if().
/**
* For bulk updates $arr_data looks like:
* Array
* (
* [action] => update
* [type] => plugin
* [bulk] => 1
* [plugins] => Array
* (
* [0] => plugin-folder-1/plugin-index.php
* [1] => my-plugin-folder/my-plugin.php
* )
* )
*/
if (isset($arr_data['bulk']) && $arr_data['bulk'] && isset($arr_data['action']) && 'update' == $arr_data['action']) {
$plugins_updated = isset($arr_data['plugins']) ? (array) $arr_data['plugins'] : array();
foreach ($plugins_updated as $plugin_name) {
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_name, true, false);
$plugin_slug = dirname($plugin_name);
$context = array(
'plugin_slug' => $plugin_slug,
'plugin_name' => $plugin_data['Name'],
'plugin_title' => $plugin_data['Title'],
'plugin_description' => $plugin_data['Description'],
'plugin_author' => $plugin_data['Author'],
'plugin_version' => $plugin_data['Version'],
'plugin_url' => $plugin_data['PluginURI'],
);
// get url and package
$update_plugins = get_site_transient('update_plugins');
if ($update_plugins && isset($update_plugins->response[ $plugin_name ])) {
/*
$update_plugins[plugin_path/slug]:
{
"id": "8986",
"slug": "autoptimize",
"plugin": "autoptimize/autoptimize.php",
"new_version": "1.9.1",
"url": "https://wordpress.org/plugins/autoptimize/",
"package": "https://downloads.wordpress.org/plugin/autoptimize.1.9.1.zip"
}
*/
$plugin_update_info = $update_plugins->response[ $plugin_name ];
// autoptimize/autoptimize.php
if (isset($plugin_update_info->plugin)) {
$context['plugin_update_info_plugin'] = $plugin_update_info->plugin;
}
// https://downloads.wordpress.org/plugin/autoptimize.1.9.1.zip
if (isset($plugin_update_info->package)) {
$context['plugin_update_info_package'] = $plugin_update_info->package;
}
}
// To get old version we use our option
// @TODO: this does not always work, why?
$plugins_before_update = json_decode(get_option($this->slug . '_plugin_info_before_update', false), true);
if (is_array($plugins_before_update) && isset($plugins_before_update[ $plugin_name ])) {
$context['plugin_prev_version'] = $plugins_before_update[ $plugin_name ]['Version'];
}
$this->infoMessage(
'plugin_bulk_updated',
$context
);
}// End foreach().
}// End if().
} // End if().
if (! $did_log) {
// echo "on_upgrader_process_complete";
// sf_d( $plugin_upgrader_instance, '$plugin_upgrader_instance' );
// sf_d( $arr_data, '$arr_data' );
// exit;
}
$this->remove_saved_versions();
} // on upgrader_process_complete
/**
* Plugin is activated
* plugin_name is like admin-menu-tree-page-view/index.php
*/
function on_activated_plugin($plugin_name, $network_wide)
{
/*
Plugin data returned array contains the following:
'Name' - Name of the plugin, must be unique.
'Title' - Title of the plugin and the link to the plugin's web site.
'Description' - Description of what the plugin does and/or notes from the author.
'Author' - The author's name
'AuthorURI' - The authors web site address.
'Version' - The plugin version number.
'PluginURI' - Plugin web site address.
'TextDomain' - Plugin's text domain for localization.
'DomainPath' - Plugin's relative directory path to .mo files.
'Network' - Boolean. Whether the plugin can only be activated network wide.
*/
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_name, true, false);
$plugin_slug = dirname($plugin_name);
$context = array(
'plugin_name' => $plugin_data['Name'],
'plugin_slug' => $plugin_slug,
'plugin_title' => $plugin_data['Title'],
'plugin_description' => $plugin_data['Description'],
'plugin_author' => $plugin_data['Author'],
'plugin_version' => $plugin_data['Version'],
'plugin_url' => $plugin_data['PluginURI'],
);
if (! empty($plugin_data['GitHub Plugin URI'])) {
$context['plugin_github_url'] = $plugin_data['GitHub Plugin URI'];
}
$this->infoMessage('plugin_activated', $context);
} // on_activated_plugin
/**
* Plugin is deactivated
* plugin_name is like admin-menu-tree-page-view/index.php
*/
function on_deactivated_plugin($plugin_name)
{
$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/' . $plugin_name, true, false);
$plugin_slug = dirname($plugin_name);
$context = array(
'plugin_name' => $plugin_data['Name'],
'plugin_slug' => $plugin_slug,
'plugin_title' => $plugin_data['Title'],
'plugin_description' => $plugin_data['Description'],
'plugin_author' => $plugin_data['Author'],
'plugin_version' => $plugin_data['Version'],
'plugin_url' => $plugin_data['PluginURI'],
);
if (! empty($plugin_data['GitHub Plugin URI'])) {
$context['plugin_github_url'] = $plugin_data['GitHub Plugin URI'];
}
$this->infoMessage('plugin_deactivated', $context);
} // on_deactivated_plugin
/**
* Get output for detailed log section
*/
function getLogRowDetailsOutput($row)
{
$context = $row->context;
$message_key = $context['_message_key'];
$output = '';
// When a plugin is installed we show a bit more information
// We do it only on install because we don't want to clutter to log,