-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
1882 lines (1577 loc) · 71.1 KB
/
index.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
/*
Plugin Name: Ortho History
Description: (Ortho) Get log/history of the changes made by users in WordPress across multi site.
Version: 1.0.9
Author: EY
*/
/*
*
* Original Plugin URI: http://eskapism.se/code-playground/simple-history/
*
* * This is a highly modified version of the Simple History plugin by Par Thernstrom.
* It has been extended for the Orhtopedic Surgery department at UCSF in the following ways:
* - view all sub (multi-site/multi-post) logs in one place. Each multi-site will show the same history.
* - support special page types (defined with Magic Fields module) such as Biography, News, etc.
* - aggregate multi post entries into a single line entry.
* - multi post entries are tagged as "multipost";
* - clicking on a multi-post item expands the items to show links to each site post edit page.
* - extended occasions to support multi-post entries
* - right-side tags indicate the site or multi-sites of an entry, in color columns.
*
* Special Notes (ey):
* The customization is tied specifically to blog ids 1, 4 & 5, implying they exist and are there.
* It also expects that the simple_history database exists for each of the implied blogs.
* *** it will probably not function if the above are not satisifed.
*
* Original Author: Pär Thernström
* Original URI: http://eskapism.se/
*/
/* Copyright 2010 Pär Thernström (email: par.thernstrom@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
load_plugin_textdomain('simple-history', false, "/simple-history/languages");
define( "SIMPLE_HISTORY_VERSION", "1.0.9");
define( "SIMPLE_HISTORY_NAME", "Simple History");
// define( "SIMPLE_HISTORY_URL", WP_PLUGIN_URL . '/ortho-simple-history/'); // http://playground.ep/wordpress/wp-content/plugins/simple-history/
define( "SIMPLE_HISTORY_URL", plugins_url() . '/ortho-simple-history/'); // http://playground.ep/wordpress/wp-content/plugins/simple-history/
//require_once ABSPATH . WPINC . '/logger.php';
//$logger = new Logger('info',$logger_wp_file);
/**
* Let's begin on a class, since they rule so much more than functions.
*/
class simple_history {
var
$plugin_foldername_and_filename,
$view_history_capability
;
function __construct() {
add_action( 'admin_init', array($this, 'admin_init') );
add_action( 'init', array($this, 'init') );
add_action( 'admin_menu', array($this, 'admin_menu') );
add_action( 'wp_dashboard_setup', array($this, 'wp_dashboard_setup') );
add_action( 'wp_ajax_simple_history_ajax', array($this, 'ajax') );
add_filter( 'plugin_action_links_simple-history/index.php', array($this, "plugin_action_links"), 10, 4);
$this->plugin_foldername_and_filename = basename(dirname(__FILE__)) . "/" . basename(__FILE__);
$this->view_history_capability = "edit_pages";
$this->view_history_capability = apply_filters("simple_history_view_history_capability", $this->view_history_capability);
$this->add_types_for_translation();
}
function get_pager_size() {
$pager_size = get_option("simple_history_pager_size", 5);
return $pager_size;
}
/**
* Some post types etc are added as variables from the log, so to catch these for translation I just add them as dummy stuff here.
* There is probably a better way to do this, but this should work anyway
*/
function add_types_for_translation() {
$dummy = __("approved", "simple-history");
$dummy = __("unapproved", "simple-history");
$dummy = __("marked as spam", "simple-history");
$dummy = __("trashed", "simple-history");
$dummy = __("untrashed", "simple-history");
$dummy = __("created", "simple-history");
$dummy = __("deleted", "simple-history");
$dummy = __("updated", "simple-history");
$dummy = __("nav_menu_item", "simple-history");
$dummy = __("attachment", "simple-history");
$dummy = __("user", "simple-history");
$dummy = __("settings page", "simple-history");
$dummy = __("edited", "simple-history");
$dummy = __("comment", "simple-history");
$dummy = __("logged in", "simple-history");
$dummy = __("logged out", "simple-history");
}
function plugin_action_links($actions, $b, $c, $d) {
$settings_page_url = menu_page_url("simple_history_settings_menu_slug", 0);
$actions[] = "<a href='$settings_page_url'>Settings</a>";
return $actions;
}
function wp_dashboard_setup() {
if (simple_history_setting_show_on_dashboard()) {
if (current_user_can($this->view_history_capability)) {
wp_add_dashboard_widget("simple_history_dashboard_widget", __("History", 'simple-history'), "simple_history_dashboard");
}
}
}
// stuff that happens in the admin
// "admin_init is triggered before any other hook when a user access the admin area"
function admin_init() {
// posts
add_action("save_post", "simple_history_save_post");
add_action("transition_post_status", "simple_history_transition_post_status", 10, 3);
add_action("delete_post", "simple_history_delete_post");
// attachments/media
add_action("add_attachment", "simple_history_add_attachment");
add_action("edit_attachment", "simple_history_edit_attachment");
add_action("delete_attachment", "simple_history_delete_attachment");
// comments
add_action("edit_comment", "simple_history_edit_comment");
add_action("delete_comment", "simple_history_delete_comment");
add_action("wp_set_comment_status", "simple_history_set_comment_status", 10, 2);
// settings (all built in except permalinks)
$arr_option_pages = array("general", "writing", "reading", "discussion", "media", "privacy");
foreach ($arr_option_pages as $one_option_page_name) {
$new_func = create_function('$capability', '
return simple_history_add_update_option_page($capability, "'.$one_option_page_name.'");
');
add_filter("option_page_capability_{$one_option_page_name}", $new_func);
}
// settings page for permalinks
add_action('check_admin_referer', "simple_history_add_update_option_page_permalinks", 10, 2);
// core update = wordpress updates
add_action( '_core_updated_successfully', array($this, "action_core_updated") );
// add donate link to plugin list page
add_action("plugin_row_meta", array($this, "action_plugin_row_meta"), 10, 2);
// check if database needs upgrade
$this->check_upgrade_stuff();
// add scripts and styles
add_action("admin_enqueue_scripts", array($this, "admin_enqueue"));
}
// enqueue styles and scripts, but only to our own pages
function admin_enqueue($hook) {
if ( ($hook == "settings_page_simple_history_settings_menu_slug") || (simple_history_setting_show_on_dashboard() && $hook == "index.php") || (simple_history_setting_show_as_page() && $hook == "dashboard_page_simple_history_page")) {
wp_enqueue_style( "simple_history_styles", SIMPLE_HISTORY_URL . "styles.css", false, SIMPLE_HISTORY_VERSION );
wp_enqueue_script("simple_history", SIMPLE_HISTORY_URL . "scripts.js", array("jquery"), SIMPLE_HISTORY_VERSION);
}
}
// WordPress Core updated
function action_core_updated($wp_version) {
simple_history_add("action=updated&object_type=wordpress_core&object_id=wordpress_core&object_name=".sprintf(__('WordPress %1$s', 'simple-history'), $wp_version));
}
function filter_option_page_capability($capability) {
return $capability;
}
// Add link to donate page. Note to self: does not work on dev install because of dir being trunk and not "simple-history"
function action_plugin_row_meta($links, $file) {
if ($file == $this->plugin_foldername_and_filename) {
return array_merge(
$links,
array( sprintf( '<a href="http://eskapism.se/sida/donate/?utm_source=wordpress&utm_medium=pluginpage&utm_campaign=simplehistory">%1$s</a>', __('Donate', "simple-history") ) )
);
}
return $links;
}
// check some things regarding update
function check_upgrade_stuff() {
global $wpdb;
$db_version = get_option("simple_history_db_version");
// $db_version = FALSE;
if ($db_version === FALSE) {
// db fix has never been run
// user is on version 0.4 or earlier
// = database is not using utf-8
// so fix that
$table_name = $wpdb->prefix . "simple_history";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
#echo "begin upgrading database";
// We change the varchar size to add one num just to force update of encoding. dbdelta didn't see it otherwise.
$sql = "CREATE TABLE " . $table_name . " (
id int(10) NOT NULL AUTO_INCREMENT,
date datetime NOT NULL,
action VARCHAR(256) NOT NULL COLLATE utf8_general_ci,
object_type VARCHAR(256) NOT NULL COLLATE utf8_general_ci,
object_subtype VARCHAR(256) NOT NULL COLLATE utf8_general_ci,
user_id int(10) NOT NULL,
object_id int(10) NOT NULL,
object_name VARCHAR(256) NOT NULL COLLATE utf8_general_ci,
tag bigint(20) NOT NULL,
PRIMARY KEY (id)
) CHARACTER SET=utf8;";
// Upgrade db / fix utf for varchars
dbDelta($sql);
// Fix UTF-8 for table
$sql = sprintf('alter table %1$s charset=utf8;', $table_name);
$wpdb->query($sql);
// Store this upgrade in ourself :)
simple_history_add("action=" . 'upgraded it\'s database' . "&object_type=plugin&object_name=" . SIMPLE_HISTORY_NAME);
#echo "done upgrading database";
update_option("simple_history_db_version", 1);
} else {
// echo "db up to date";
}
}
function settings_page() {
?>
<div class="wrap">
<form method="post" action="options.php">
<h2><?php _e("Ortho History Settings", "simple-history") ?></h2>
<?php do_settings_sections("simple_history_settings_menu_slug"); ?>
<?php settings_fields("simple_history_settings_group"); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function admin_menu() {
// show as page?
if (simple_history_setting_show_as_page()) {
//add_dashboard_page(SIMPLE_HISTORY_NAME, __("History", 'simple-history'), $this->view_history_capability, "simple_history_page", "simple_history_management_page");
add_dashboard_page("Ortho History", __("Ortho History", 'simple-history'), $this->view_history_capability, "simple_history_page", "simple_history_management_page");
}
// add page for settings
$show_settings_page = TRUE;
$show_settings_page = apply_filters("simple_history_show_settings_page", $show_settings_page);
if ($show_settings_page) {
//add_options_page(__('Simple History Settings', "simple-history"), SIMPLE_HISTORY_NAME, $this->view_history_capability, "simple_history_settings_menu_slug", array($this, 'settings_page'));
add_options_page(__('Ortho History Settings', "simple-history"), "Ortho History", $this->view_history_capability, "simple_history_settings_menu_slug", array($this, 'settings_page'));
}
add_settings_section("simple_history_settings_section", __("", "simple-history"), "simple_history_settings_page", "simple_history_settings_menu_slug");
add_settings_field("simple_history_settings_field_1", __("Show Simple History", "simple-history"), "simple_history_settings_field", "simple_history_settings_menu_slug", "simple_history_settings_section");
add_settings_field("simple_history_settings_field_5", __("Number of items per page", "simple-history"), "simple_history_settings_field_number_of_items", "simple_history_settings_menu_slug", "simple_history_settings_section");
add_settings_field("simple_history_settings_field_2", __("RSS feed", "simple-history"), "simple_history_settings_field_rss", "simple_history_settings_menu_slug", "simple_history_settings_section");
add_settings_field("simple_history_settings_field_4", __("Clear log", "simple-history"), "simple_history_settings_field_clear_log", "simple_history_settings_menu_slug", "simple_history_settings_section");
add_settings_field("simple_history_settings_field_3", __("Donate", "simple-history"), "simple_history_settings_field_donate", "simple_history_settings_menu_slug", "simple_history_settings_section");
register_setting("simple_history_settings_group", "simple_history_show_on_dashboard");
register_setting("simple_history_settings_group", "simple_history_show_as_page");
register_setting("simple_history_settings_group", "simple_history_pager_size");
}
function init() {
// users and stuff
add_action("wp_login", "simple_history_wp_login");
add_action("wp_logout", "simple_history_wp_logout");
add_action("delete_user", "simple_history_delete_user");
add_action("user_register", "simple_history_user_register");
add_action("profile_update", "simple_history_profile_update");
// options
#add_action("updated_option", "simple_history_updated_option", 10, 3);
#add_action("updated_option", "simple_history_updated_option2", 10, 2);
#add_action("updated_option", "simple_history_updated_option3", 10, 1);
#add_action("update_option", "simple_history_update_option", 10, 3);
// plugin
add_action("activated_plugin", "simple_history_activated_plugin");
add_action("deactivated_plugin", "simple_history_deactivated_plugin");
// check for RSS
// don't know if this is the right way to do this, but it seems to work!
if (isset($_GET["simple_history_get_rss"])) {
$rss_secret_option = get_option("simple_history_rss_secret");
$rss_secret_get = $_GET["rss_secret"];
echo '<?xml version="1.0"?>';
$self_link = simple_history_get_rss_address();
if ($rss_secret_option == $rss_secret_get) {
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><?php printf(__("History for %s", 'simple-history'), get_bloginfo("name")) ?></title>
<description><?php printf(__("WordPress History for %s", 'simple-history'), get_bloginfo("name")) ?></description>
<link><?php echo get_bloginfo("url") ?></link>
<atom:link href="<?php echo $self_link; ?>" rel="self" type="application/rss+xml" />
<?php
$arr_items = simple_history_get_items_array("items=10");
foreach ($arr_items as $one_item) {
$object_type = ucwords($one_item->object_type);
$object_name = esc_html($one_item->object_name);
$user = get_user_by("id", $one_item->user_id);
$user_nicename = esc_html(@$user->user_nicename);
$description = "";
if ($user_nicename) {
$description .= sprintf(__("By %s", 'simple-history'), $user_nicename);
$description .= "<br />";
}
if ($one_item->occasions) {
$description .= sprintf(__("%d occasions", 'simple-history'), sizeof($one_item->occasions));
$description .= "<br />";
}
$item_title = esc_html($object_type) . " \"" . esc_html($object_name) . "\" {$one_item->action}";
$item_title = html_entity_decode($item_title, ENT_COMPAT, "UTF-8");
$item_guid = get_bloginfo("siteurl") . "?simple-history-guid=" . $one_item->id;
?>
<item>
<title><![CDATA[<?php echo $item_title; ?>]]></title>
<description><![CDATA[<?php echo $description ?>]]></description>
<author><?php echo $user_nicename ?></author>
<pubDate><?php echo date("D, d M Y H:i:s", strtotime($one_item->date)) ?> GMT</pubDate>
<guid isPermaLink="false"><?php echo $item_guid ?></guid>
</item>
<?php
}
?>
</channel>
</rss>
<?php
} else {
// not ok rss secret
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><?php printf(__("History for %s", 'simple-history'), get_bloginfo("name")) ?></title>
<description><?php printf(__("WordPress History for %s", 'simple-history'), get_bloginfo("name")) ?></description>
<link><?php echo get_bloginfo("siteurl") ?></link>
<item>
<title><?php _e("Wrong RSS secret", 'simple-history')?></title>
<description><?php _e("Your RSS secret for Simple History RSS feed is wrong. Please see WordPress settings for current link to the RSS feed.", 'simple-history')?></description>
<pubDate><?php echo date("D, d M Y H:i:s", time()) ?> GMT</pubDate>
<guid><?php echo get_bloginfo("siteurl") . "?simple-history-guid=wrong-secret" ?></guid>
</item>
</channel>
</rss>
<?php
}
exit;
}
}
function ajax() {
global $simple_history;
$type = isset($_POST["type"]) ? $_POST["type"] : "";
$subtype = isset($_POST["subtype"]) ? $_POST["subtype"] : "";
$user = $_POST["user"];
if ($user == __( "By all users", 'simple-history' )) { $user = ""; }
// page to show. 1 = first page.
$page = 0;
if (isset($_POST["page"])) {
$page = (int) $_POST["page"];
}
// number of items to get
$items = (int) (isset($_POST["items"])) ? $_POST["items"] : $simple_history->get_pager_size();
// number of prev added items = number of items to skip before starting to add $items num of new items
$num_added = (int) (isset($_POST["num_added"])) ? $_POST["num_added"] : $simple_history->get_pager_size();
$search = (isset($_POST["search"])) ? $_POST["search"] : "";
$filter_type = $type . "/" . $subtype;
$args = array(
"is_ajax" => true,
"filter_type" => $filter_type,
"filter_user" => $user,
"page" => $page,
"items" => $items,
"num_added" => $num_added,
"search" => $search
);
$arr_json = array(
"status" => "ok",
"error" => "",
"items_li" => "",
"filtered_items_total_count" => 0,
"filtered_items_total_count_string" => "",
"filtered_items_total_pages" => 0
);
// ob_start();
$return = simple_history_print_history($args);
// $return = ob_get_clean();
if ("noMoreItems" == $return) {
$arr_json["status"] = "error";
$arr_json["error"] = "noMoreItems";
} else {
$arr_json["items_li"] = $return;
// total number of event. really bad way since we get them all again. need to fix this :/
$args["items"] = "all";
$all_items = simple_history_get_items_array($args);
$arr_json["filtered_items_total_count"] = sizeof($all_items);
$arr_json["filtered_items_total_count_string"] = sprintf(_n('One item', '%1$d items', sizeof($all_items), "simple-history"), sizeof($all_items));
$arr_json["filtered_items_total_pages"] = ceil($arr_json["filtered_items_total_count"] / $simple_history->get_pager_size());
}
header("Content-type: application/json");
echo json_encode($arr_json);
exit;
}
} // class
// Boot up
$simple_history = new simple_history;
function simple_history_settings_page() {
// never remove this function, it must exist.
// echo "Please choose options for simple history ...";
}
// get settings if plugin should be visible on dasboard. default in no since 0.7
function simple_history_setting_show_on_dashboard() {
$show_on_dashboard = get_option("simple_history_show_on_dashboard", 0);
$show_on_dashboard = apply_filters("simple_history_show_on_dashboard", $show_on_dashboard);
return (bool) $show_on_dashboard;
}
function simple_history_setting_show_as_page() {
$setting = get_option("simple_history_show_as_page", 1);
$setting = apply_filters("simple_history_show_as_page", $setting);
return (bool) $setting;
}
function simple_history_settings_field_number_of_items() {
global $simple_history;
$current_pager_size = $simple_history->get_pager_size();
?>
<select name="simple_history_pager_size">
<option <?php echo $current_pager_size == 5 ? "selected" : "" ?> value="5">5</option>
<option <?php echo $current_pager_size == 10 ? "selected" : "" ?> value="10">10</option>
<option <?php echo $current_pager_size == 15 ? "selected" : "" ?> value="15">15</option>
<option <?php echo $current_pager_size == 20 ? "selected" : "" ?> value="20">20</option>
<option <?php echo $current_pager_size == 25 ? "selected" : "" ?> value="25">25</option>
<option <?php echo $current_pager_size == 30 ? "selected" : "" ?> value="30">30</option>
<option <?php echo $current_pager_size == 40 ? "selected" : "" ?> value="40">40</option>
<option <?php echo $current_pager_size == 50 ? "selected" : "" ?> value="50">50</option>
<option <?php echo $current_pager_size == 75 ? "selected" : "" ?> value="75">75</option>
<option <?php echo $current_pager_size == 100 ? "selected" : "" ?> value="100">100</option>
</select>
<?php
}
function simple_history_settings_field() {
$show_on_dashboard = simple_history_setting_show_on_dashboard();
$show_as_page = simple_history_setting_show_as_page();
?>
<input <?php echo $show_on_dashboard ? "checked='checked'" : "" ?> type="checkbox" value="1" name="simple_history_show_on_dashboard" id="simple_history_show_on_dashboard" class="simple_history_show_on_dashboard" />
<label for="simple_history_show_on_dashboard"><?php _e("on the dashboard", 'simple-history') ?></label>
<br />
<input <?php echo $show_as_page ? "checked='checked'" : "" ?> type="checkbox" value="1" name="simple_history_show_as_page" id="simple_history_show_as_page" class="simple_history_show_as_page" />
<label for="simple_history_show_as_page"><?php _e("as a page under the dashboard menu", 'simple-history') ?></label>
<?php
}
/**
* Settings section to clear database
*/
function simple_history_settings_field_clear_log() {
$clear_log = false;
if (isset($_GET["simple_history_clear_log"]) && $_GET["simple_history_clear_log"]) {
$clear_log = true;
echo "<div class='simple-history-settings-page-updated'><p>";
_e("Cleared database", 'simple-history');
echo "</p></div>";
}
if ($clear_log) {
simple_history_clear_log();
}
_e("Items in the database are automatically removed after 60 days.", 'simple-history');
$update_link = add_query_arg("simple_history_clear_log", "1");
printf(' <a href="%2$s">%1$s</a>', __('Clear it now.', 'simple-history'), $update_link);
}
function simple_history_clear_log() {
global $wpdb;
$tableprefix = $wpdb->prefix;
$sql = "DELETE FROM {$tableprefix}simple_history";
$wpdb->query($sql);
}
function simple_history_settings_field_donate() {
?>
<p>
<?php
_e('
Please
<a href="http://eskapism.se/sida/donate/?utm_source=wordpress&utm_medium=settingpage&utm_campaign=simplehistory">
donate
</a> to support the development of this plugin and to keep it free.
Thanks!
', "simple-history")
?>
</p>
<?php
}
function simple_history_get_rss_address() {
$rss_secret = get_option("simple_history_rss_secret");
$rss_address = add_query_arg(array("simple_history_get_rss" => "1", "rss_secret" => $rss_secret), get_bloginfo("url") . "/");
$rss_address = htmlspecialchars($rss_address, ENT_COMPAT, "UTF-8");
return $rss_address;
}
function simple_history_update_rss_secret() {
$rss_secret = "";
for ($i=0; $i<20; $i++) {
$rss_secret .= chr(rand(97,122));
}
update_option("simple_history_rss_secret", $rss_secret);
return $rss_secret;
}
function simple_history_settings_field_rss() {
$create_new_secret = false;
if (isset($_GET["simple_history_rss_update_secret"]) && $_GET["simple_history_rss_update_secret"]) {
$create_new_secret = true;
echo "<div class='simple-history-settings-page-updated'><p>";
_e("Created new secret RSS adress", 'simple-history');
echo "</p></div>";
}
if ($create_new_secret) {
simple_history_update_rss_secret();
}
$rss_address = simple_history_get_rss_address();
echo "<code><a href='$rss_address'>$rss_address</a></code>";
echo "<br />";
_e("This is a secret RSS feed for Simple History. Only share the link with people you trust", 'simple-history');
echo "<br />";
$update_link = add_query_arg("simple_history_rss_update_secret", "1");
printf(__("You can <a href='%s'>generate a new address</a> for the RSS feed. This is useful if you think that the address has fallen into the wrong hands.", 'simple-history'), $update_link);
}
// @todo: move all add-related stuff to own file? there are so many of them.. kinda confusing, ey.
function simple_history_activated_plugin($plugin_name) {
$plugin_name = urlencode($plugin_name);
simple_history_add("action=activated&object_type=plugin&object_name=$plugin_name");
}
function simple_history_deactivated_plugin($plugin_name) {
$plugin_name = urlencode($plugin_name);
simple_history_add("action=deactivated&object_type=plugin&object_name=$plugin_name");
}
function simple_history_edit_comment($comment_id) {
$comment_data = get_commentdata($comment_id, 0, true);
$comment_post_ID = $comment_data["comment_post_ID"];
$post = get_post($comment_post_ID);
$post_title = get_the_title($comment_post_ID);
$excerpt = get_comment_excerpt($comment_id);
$author = get_comment_author($comment_id);
$str = sprintf( "$excerpt [" . __('From %1$s on %2$s') . "]", $author, $post_title );
$str = urlencode($str);
simple_history_add("action=edited&object_type=comment&object_name=$str&object_id=$comment_id");
}
function simple_history_delete_comment($comment_id) {
$comment_data = get_commentdata($comment_id, 0, true);
$comment_post_ID = $comment_data["comment_post_ID"];
$post = get_post($comment_post_ID);
$post_title = get_the_title($comment_post_ID);
$excerpt = get_comment_excerpt($comment_id);
$author = get_comment_author($comment_id);
$str = sprintf( "$excerpt [" . __('From %1$s on %2$s') . "]", $author, $post_title );
$str = urlencode($str);
simple_history_add("action=deleted&object_type=comment&object_name=$str&object_id=$comment_id");
}
function simple_history_set_comment_status($comment_id, $new_status) {
#echo "<br>new status: $new_status<br>"; // 0
// $new_status hold (unapproved), approve, spam, trash
$comment_data = get_commentdata($comment_id, 0, true);
$comment_post_ID = $comment_data["comment_post_ID"];
$post = get_post($comment_post_ID);
$post_title = get_the_title($comment_post_ID);
$excerpt = get_comment_excerpt($comment_id);
$author = get_comment_author($comment_id);
$action = "";
if ("approve" == $new_status) {
$action = 'approved';
} elseif ("hold" == $new_status) {
$action = 'unapproved';
} elseif ("spam" == $new_status) {
$action = 'marked as spam';
} elseif ("trash" == $new_status) {
$action = 'trashed';
} elseif ("0" == $new_status) {
$action = 'untrashed';
}
$action = urlencode($action);
$str = sprintf( "$excerpt [" . __('From %1$s on %2$s') . "]", $author, $post_title );
$str = urlencode($str);
simple_history_add("action=$action&object_type=comment&object_name=$str&object_id=$comment_id");
}
function simple_history_update_option($option, $oldval, $newval) {
if ($option == "active_plugins") {
$debug = "\n";
$debug .= "\nsimple_history_update_option()";
$debug .= "\noption: $option";
$debug .= "\noldval: " . print_r($oldval, true);
$debug .= "\nnewval: " . print_r($newval, true);
// Returns an array containing all the entries from array1 that are not present in any of the other arrays.
// alltså:
// om newval är array1 och innehåller en rad så är den tillagd
// om oldval är array1 och innhåller en rad så är den bortagen
$diff_added = array_diff((array) $newval, (array) $oldval);
$diff_removed = array_diff((array) $oldval, (array) $newval);
$debug .= "\ndiff_added: " . print_r($diff_added, true);
$debug .= "\ndiff_removed: " . print_r($diff_removed, true);
}
}
function simple_history_updated_option($option, $oldval, $newval) {
/*
echo "<br><br>simple_history_updated_option()";
echo "<br>Updated option $option";
echo "<br>oldval: ";
bonny_d($oldval);
echo "<br>newval:";
bonny_d($newval);
*/
}
/*
function simple_history_updated_option2($option, $oldval) {
echo "<br><br>xxx_simple_history_updated_option2";
bonny_d($option);
bonny_d($oldval);
}
function simple_history_updated_option3($option) {
echo "<br><br>xxx_simple_history_updated_option3";
echo "<br>option: $option";
}
*/
function simple_history_add_attachment($attachment_id) {
$post = get_post($attachment_id);
$post_title = urlencode(get_the_title($post->ID));
simple_history_add("action=added&object_type=attachment&object_id=$attachment_id&object_name=$post_title");
}
function simple_history_edit_attachment($attachment_id) {
// is this only being called if the title of the attachment is changed?!
$post = get_post($attachment_id);
$post_title = urlencode(get_the_title($post->ID));
simple_history_add("action=updated&object_type=attachment&object_id=$attachment_id&object_name=$post_title");
}
function simple_history_delete_attachment($attachment_id) {
$post = get_post($attachment_id);
$post_title = urlencode(get_the_title($post->ID));
simple_history_add("action=deleted&object_type=attachment&object_id=$attachment_id&object_name=$post_title");
}
// user is updated
function simple_history_profile_update($user_id) {
$user = get_user_by("id", $user_id);
$user_nicename = urlencode($user->user_nicename);
simple_history_add("action=updated&object_type=user&object_id=$user_id&object_name=$user_nicename");
}
// user is created
function simple_history_user_register($user_id) {
$user = get_user_by("id", $user_id);
$user_nicename = urlencode($user->user_nicename);
simple_history_add("action=created&object_type=user&object_id=$user_id&object_name=$user_nicename");
}
// user is deleted
function simple_history_delete_user($user_id) {
$user = get_user_by("id", $user_id);
$user_nicename = urlencode($user->user_nicename);
simple_history_add("action=deleted&object_type=user&object_id=$user_id&object_name=$user_nicename");
}
// user logs in
function simple_history_wp_login($user) {
$current_user = wp_get_current_user();
$user = get_user_by("login", $user);
$user_nicename = urlencode($user->user_nicename);
// if user id = null then it's because we are logged out and then no one is acutally loggin in.. like a.. ghost-user!
if ($current_user->ID == 0) {
$user_id = $user->ID;
} else {
$user_id = $current_user->ID;
}
simple_history_add("action=logged in&object_type=user&object_id=".$user->ID."&user_id=$user_id&object_name=$user_nicename");
}
// user logs out
function simple_history_wp_logout() {
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$user_nicename = urlencode($current_user->user_nicename);
simple_history_add("action=logged out&object_type=user&object_id=$current_user_id&object_name=$user_nicename");
}
function simple_history_delete_post($post_id) {
if (wp_is_post_revision($post_id) == false) {
$post = get_post($post_id);
if ($post->post_status != "auto-draft" && $post->post_status != "inherit") {
$post_title = urlencode(get_the_title($post->ID));
simple_history_add("action=deleted&object_type=post&object_subtype=" . $post->post_type . "&object_id=$post_id&object_name=$post_title");
}
}
}
function simple_history_save_post($post_id) {
if (wp_is_post_revision($post_id) == false) {
// not a revision
// it should also not be of type auto draft
$post = get_post($post_id);
if ($post->post_status != "auto-draft") {
// bonny_d($post);
#echo "save";
// [post_title] => neu
// [post_type] => page
}
}
}
// post has changed status
function simple_history_transition_post_status($new_status, $old_status, $post) {
//global $logger;
#echo "<br>From $old_status to $new_status";
// compute tag using checksum, offering some uniqueness - ey
$tag = 0;
/*if (isset($post['ortho_tag'])) $tag = $post['ortho_tag'];
else */$tag = -1;
// From new to auto-draft <- ignore
// From new to inherit <- ignore
// From auto-draft to draft <- page/post created
// From draft to draft
// From draft to pending
// From pending to publish
# From pending to trash
// if not from & to = same, then user has changed something
//bonny_d($post); // regular post object
if ($old_status == "auto-draft" && ($new_status != "auto-draft" && $new_status != "inherit")) {
// page created
//$logger->info("history post - create".print_r($post,true));
//$action = "created";
} elseif ($new_status == "auto-draft" || ($old_status == "new" && $new_status == "inherit")) {
// page...eh.. just leave it.
return;
} elseif ($new_status == "trash") {
$action = "deleted";
} else {
// page updated. i guess.
//$logger->info("history post - update".print_r($post,true));
$action = "updated";
}
$object_type = "post";
$object_subtype = $post->post_type;
// Attempt to auto-translate post types*/
// no, no longer, do it at presentation instead
#$object_type = __( ucfirst ( $object_type ) );
#$object_subtype = __( ucfirst ( $object_subtype ) );
if ($object_subtype == "revision") {
// don't log revisions
return;
}
if (wp_is_post_revision($post->ID) === false) {
// ok, no revision
$object_id = $post->ID;
} else {
return;
}
$post_title = get_the_title($post->ID);
$post_title = urlencode($post_title);
simple_history_add("action=$action&object_type=$object_type&object_subtype=$object_subtype&object_id=$object_id&object_name=$post_title&tag=$tag");
}
/**
* add event to history table
*/
function simple_history_add($args) {
$defaults = array(
"action" => null,
"object_type" => null,
"object_subtype" => null,
"object_id" => null,
"object_name" => null,
"user_id" => null,
"tag" => null // ey
);
$args = wp_parse_args( $args, $defaults );
$action = mysql_real_escape_string($args["action"]);
$object_type = $args["object_type"];
$object_subtype = $args["object_subtype"];
$object_id = $args["object_id"];
$object_name = mysql_real_escape_string($args["object_name"]);
$user_id = $args["user_id"];
$tag = $args["tag"]; // ey
global $wpdb;
$tableprefix = $wpdb->prefix;
if ($user_id) {
$current_user_id = $user_id;
} else {
$current_user = wp_get_current_user();
$current_user_id = (int) $current_user->ID;
}
// date, store at utc or local time
// anything is better than now() anyway!
// WP seems to use the local time, so I will go with that too I think
// GMT/UTC-time is: date_i18n($timezone_format, false, 'gmt'));
// local time is: date_i18n($timezone_format));
$localtime = current_time("mysql");
$sql = "INSERT INTO {$tableprefix}simple_history SET date = '$localtime', action = '$action', object_type = '$object_type', object_subtype = '$object_subtype', user_id = '$current_user_id', object_id = '$object_id', object_name = '$object_name', tag = '$tag'";
$wpdb->query($sql);
}
/**
* Removes old entries from the db
* @todo: let user set value, if any
*/
function simple_history_purge_db() {
$do_purge_history = TRUE;
$do_purge_history = apply_filters("simple_history_allow_db_purge", $do_purge_history);
global $wpdb;
$tableprefix = $wpdb->prefix;
$sql = "DELETE FROM {$tableprefix}simple_history WHERE DATE_ADD(date, INTERVAL 60 DAY) < now()";
if ($do_purge_history) {
$wpdb->query($sql);
}
}
// widget on dashboard
function simple_history_dashboard() {
simple_history_purge_db();
simple_history_print_nav();
echo simple_history_print_history();
echo simple_history_get_pagination();
}
// own page under dashboard
function simple_history_management_page() {
global $simple_history;
simple_history_purge_db();
?>
<div class="wrap">
<h2><?php echo __("Ortho History", 'simple-history') ?></h2>
<?php
simple_history_print_nav(array("from_page=1"));
echo simple_history_print_history(array("items" => $simple_history->get_pager_size(), "from_page" => "1"));
echo simple_history_get_pagination();
?>
</div>
<?php
}
if (!function_exists("bonny_d")) {
function bonny_d($var) {
echo "<pre>";
print_r($var);
echo "</pre>";
}
}
// when activating plugin: create tables
// __FILE__ doesnt work for me because of soft linkes directories
register_activation_hook( WP_PLUGIN_DIR . "/simple-history/index.php" , 'simple_history_install' );
/*
The theory behind the right way to do this. The proper way to handle an upgrade path is to only
run an upgrade procedure when you need to. Ideally, you would store a “version” in your
plugin’s database option, and then a version in the code. If they do not match, you
would fire your upgrade procedure, and then set the database option to equal the version in
the code. This is how many plugins handle upgrades, and this is how core works as well.
*/
// when installing plugin: create table
function simple_history_install() {
global $wpdb;
$table_name = $wpdb->prefix . "simple_history";
#if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id int(10) NOT NULL AUTO_INCREMENT,
date datetime NOT NULL,
action varchar(255) NOT NULL COLLATE utf8_general_ci,
object_type varchar(255) NOT NULL COLLATE utf8_general_ci,
object_subtype VARCHAR(255) NOT NULL COLLATE utf8_general_ci,