-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert-box.php
541 lines (476 loc) · 19.6 KB
/
alert-box.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
<?php
if (!class_exists('The_Alert_Box')){
class The_Alert_Box {
public static $status = 'alert_specimen';
public static $option_name = 'alert_box_options';
var $settings;
public static function init() {
static $instance;
if ( ! is_a( $instance, 'The_Alert_Box' ) ) {
$instance = new self();
}
return $instance;
}
/**
* Constructor
*/
public function __construct() {
#$this->status = self::$status;
#$this->option_name = self::$option_name;
$this->settings = get_option( self::option_name(), array() );
add_action( 'init', array( $this, 'register_bug_status') );
if (is_admin()){
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget') );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_ajax_nopriv_remove_alerted_posts', array( $this, 'remove_alerted_posts') );
add_action( 'wp_ajax_remove_alerted_posts', array( $this, 'remove_alerted_posts') );
add_action( 'wp_ajax_dismiss_alerts_ajax', array( $this, 'dismiss_alerts_ajax') );
}
#add_action( 'admin_init', array($this, 'settings_field_settings_page') );
}
public function status(){
return self::$status;
}
public function option_name(){
return self::$option_name;
}
public function register_bug_status(){
register_post_status(self::$status, array(
'label' => __('Alert', 'pf'),
'public' => false,
'exclude_from_search' => true,
'show_in_admin_all_list'=> true,
'label_count' => _n_noop(
'Alert <span class="count">(%s)</span>',
'Alerts <span class="count">(%s)</span>',
'pf'
)
) );
}
private function depreciated_alert_name_filters($alert_names){
$alert_names['dismiss_all'] = apply_filters('ab_alert_specimens_dismiss_all_text', $alert_names['dismiss_all']);
$alert_names['delete_all_check'] = apply_filters('ab_alert_specimens_check_message', $alert_names['delete_all_check']);
$alert_names['dismiss_all_check'] = apply_filters('ab_alert_specimens_check_dismiss_message', $alert_names['dismiss_all_check']);
$alert_names['delete_all'] = apply_filters('ab_alert_specimens_delete_all_text', $alert_names['delete_all']);
return $alert_names;
}
public function alert_name_maker(){
$alert_names = array(
'name' => _x( 'Alerts', 'post type general name', 'pf' ),
'singular_name' => _x( 'Alert', 'post type singular name', 'pf' ),
'menu_name' => _x( 'Alerts', 'admin menu', 'pf' ),
'name_admin_bar' => _x( 'Alert', 'add new on admin bar', 'pf' ),
'add_new' => _x( 'Add Alert', 'alert', 'pf' ),
'add_new_item' => __( 'Add New Alert', 'pf' ),
'new_item' => __( 'New Alert', 'pf' ),
'edit_item' => __( 'Edit Alert', 'pf' ),
'view_item' => __( 'View Alert', 'pf' ),
'all_items' => __( 'All Alerts', 'pf' ),
'search_items' => __( 'Search Alerts', 'pf' ),
'parent_item_colon' => __( 'Parent Alerts:', 'pf' ),
'not_found' => __( 'No alerts found.', 'pf' ),
'not_found_in_trash' => __( 'No alerts found in Trash.', 'pf' ),
'dismiss_one_check' => __( 'Are you sure you want to dismiss the alert on', 'pf' ),
'dismiss_all_check' => __( 'Are you sure you want to dismiss all alerts?', 'pf' ),
'dismiss_all' => __( 'Dismiss all alerts', 'pf' ),
'delete_all_check' => __( 'Are you sure you want to delete all posts with alerts?', 'pf' ),
'delete_all' => __( 'Delete all posts with alerts', 'pf' ),
'turned_off' => __( 'Alert boxes not active.', 'pf')
);
$alert_names = self::depreciated_alert_name_filters($alert_names);
$alert_names = apply_filters('ab_alert_specimens_labels', $alert_names);
return $alert_names;
}
public function alert_label($label = 'name', $nocaps = false){
$labels = self::alert_name_maker();
if (empty($labels[$label])){
return $labels['name'];
}
if ($nocaps) {
return strtolower($labels[$label]);
}
return $labels[$label];
}
public function add_bug_type_to_post($id, $string){
$metas = get_post_meta($id, 'ab_alert_msg', false);
if (!in_array($string, $metas)){
$result = add_post_meta($id, 'ab_alert_msg', $string, false);
return $result;
}
else {
return false;
}
}
public function get_bug_type($id){
$result = get_post_meta($id, 'ab_alert_msg', false);
$s_result = implode(', ', $result);
return $s_result;
}
public function switch_post_type($id){
$argup = array(
'ID' => $id,
'post_status' => $this->status(),
);
update_post_meta( $id, 'pre_alert_status', get_post_status($id) );
$result = wp_update_post($argup);
return $result;
}
public function get_specimens($page = 0, $post_types = false){
if (0 != $page){
$ppp = 100;
} else {
$ppp = -1;
}
if (!$post_types){
$post_types = get_post_types('', 'names');
}
$post_types = apply_filters('ab_alert_specimens_post_types', $post_types);
$args = array(
'post_type' => $post_types,
'post_status' => self::$status,
'posts_per_page'=>$ppp
);
if (0 != $page){
$args['paged'] = $page;
} else {
$args['nopaging'] = 'true';
}
$q = new WP_Query( $args );
return $q;
}
public function add_dashboard_widget(){
if(self::is_on()){
wp_add_dashboard_widget(
'specimen_alert_box',
__('Alerts', 'pf'),
array($this, 'alert_box_insides_function')
);
}
}
public function remove_alert_on_edit($post_id){
$status = $this->status;
if ( $status != $_POST['post_status'] ) {
return;
}
self::dismiss_alert($post_id);
}
public function dismiss_all_alerts($page = 0, $post_types = false){
if (current_user_can('edit_posts')){
$q = $this->get_specimens($page, $post_types);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) : $q->the_post();
self::dismiss_alert( get_the_ID() );
endwhile;
} else {
return false;
}
wp_reset_postdata();
return $q->post_count;
} else {
return false;
}
}
public function dismiss_alert($post_id){
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', array($this, 'remove_alert_on_edit') );
$post_status_d = get_post_meta( $post_id, 'pre_alert_status', true);
if (empty($post_status_d)){
$post_status_d = array();
$post_status_d['status'] = 'draft';
$post_status_d['type'] = $_POST['post_type'];
}
$post_status = apply_filters('ab_alert_specimens_update_post_type', $post_status_d);
// update the post, which calls save_post again
$id = wp_update_post( array( 'ID' => $post_id, 'post_status' => $post_status['status'] ) );
// re-hook this function
add_action( 'save_post', array($this, 'remove_alert_on_edit') );
return $id;
}
public function dismiss_alerts_ajax(){
ob_start();
$filtered_post_types = sanitize_text_field($_POST['filtered_post_types']);
if (!current_user_can('edit_posts')){
$response = array(
'what'=>'the_alert_box',
'action'=>'remove_alerted_posts',
'id'=>0,
'data'=>__('You do not have permission to dismiss alerts.', 'pf')
);
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();
ob_end_flush();
die();
}
if (empty($filtered_post_types)){
$fpt_array = false;
} else {
$fpt_array = explode(',', $filtered_post_types);
}
if (!empty($_POST['all_alerts'])){
$alerts = the_alert_box()->dismiss_all_alerts(0, $fpt_array);
} else {
$alert = intval( $_POST['alert'] );
$alerts = the_alert_box()->dismiss_alert($alert);
}
if (!isset($alerts) || !$alerts || (0 == $alerts)){
$response = array(
'what'=>'the_alert_box',
'action'=>'dismiss_alerts_ajax',
'id'=>0,
'data'=>__('No alerted posts to dismiss.', 'pf')
);
} elseif ( empty($_POST['all_alerts'] ) ) {
$response = array(
'what'=>'the_alert_box',
'action'=>'dismiss_alerts_ajax',
'id'=>$pages,
'data'=> sprintf(__('Alert on post ID %s dismissed.', 'pf'), $alert),
'supplemental' => array(
'buffered' => ob_get_contents()
)
);
} else {
$response = array(
'what'=>'the_alert_box',
'action'=>'dismiss_alerts_ajax',
'id'=>$pages,
'data'=> __('Alerts dismissed.', 'pf'),
'supplemental' => array(
'buffered' => ob_get_contents()
)
);
}
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();
ob_end_flush();
die();
}
public function remove_alerted_posts(){
ob_start();
# @todo Nonce this function
$filtered_post_types = $_POST['filtered_post_types'];
if (!current_user_can('delete_others_posts')){
$response = array(
'what'=>'the_alert_box',
'action'=>'remove_alerted_posts',
'id'=>0,
'data'=>__('You do not have permission to delete these posts.', 'pf')
);
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();
ob_end_flush();
die();
}
if (empty($filtered_post_types)){
$fpt_array = false;
} else {
$fpt_array = explode(',', $filtered_post_types);
}
$alerts = the_alert_box()->get_specimens(0, $fpt_array);
if (0 < $alerts->post_count){
$count = 0;
foreach ($alerts->query['post_type'] as $pt){
$counter = wp_count_posts($pt);
$atype = the_alert_box()->status;
$count += $counter->$atype;
}
$pages = $count/100;
$pages = ceil($pages);
$c = $pages;
while (0 < $c){
$q = the_alert_box()->get_specimens($c, $fpt_array);
while ( $q->have_posts() ) : $q->the_post();
wp_delete_post(get_the_ID());
endwhile;
wp_reset_postdata();
$c--;
}
} else {
$alerts = false;
}
if (!isset($alerts) || !$alerts || (0 == $alerts)){
$response = array(
'what'=>'the_alert_box',
'action'=>'remove_alerted_posts',
'id'=>0,
'data'=>__('No alerted posts to delete.', 'pf')
);
} else {
$response = array(
'what'=>'the_alert_box',
'action'=>'remove_alerted_posts',
'id'=>$pages,
'data'=> $alerts->post_count . __(' posts deleted.', 'pf'),
'supplemental' => array(
'buffered' => ob_get_contents()
)
);
}
$xmlResponse = new WP_Ajax_Response($response);
$xmlResponse->send();
ob_end_flush();
die();
}
public function the_alert(){
edit_post_link(get_the_title(), '<span style="color:red;font-weight:bold;">'. __('Alert', 'pf') . '</span> for ', ': '.$this->get_bug_type(get_the_ID()));
echo ' ';
edit_post_link(__('Edit', 'pf'));
echo ' ';
if (current_user_can('edit_posts')){
echo '| <a href="#" class="alert-dismisser" title="'. __('Dismiss', 'pf') . '" data-alert-post-id="'. get_the_ID() .'" data-alert-dismiss-check="'.sprintf(__('Are you sure you want to dismiss the alert on %s', 'pf'), get_the_title()).'" '.self::alert_box_type_data( get_post_type( get_the_ID() ) ).' >' . __('Dismiss', 'pf').'</a>';
}
echo ' ';
if (current_user_can('delete_others_posts')){
echo '| <a href="'.get_delete_post_link( get_the_ID() ).'" title="'. __('Delete', 'pf') .'" '.self::alert_box_type_data( get_post_type( get_the_ID() ) ).' >'. __('Delete', 'pf') .'</a>';
}
}
public function alert_box_type_data($v){
if ( is_string($v) ){
return 'alert-types="'.$v.'"';
} elseif ( is_bool($v) ) {
return '';
} else {
return 'alert-types="'.implode(',',$v->query['post_type']).'"';
}
}
public function alert_box_insides_function(){
if(self::is_on()){
$q = $this->get_specimens();
if ( $q->have_posts() ) {
while ( $q->have_posts() ) : $q->the_post();
echo '<p>';
the_alert_box()->the_alert();
echo '</p>';
endwhile;
wp_reset_postdata();
$alertCheck = __('Are you sure you want to delete all posts with alerts?', 'pf');
$alertCheck = apply_filters('ab_alert_specimens_check_message', $alertCheck);
if (current_user_can('edit_posts')){
$editText = __('Dismiss all alerts', 'pf');
$editText = apply_filters('ab_alert_specimens_dismiss_all_text', $editText);
$editCheck = __('Are you sure you want to dismiss all alerts?', 'pf');
$editCheck = apply_filters('ab_alert_specimens_check_dismiss_message', $editCheck);
echo '<p><a href="#" id="dismiss_all_alert_specimens" style="color:GoldenRod;font-weight:bold;" title="' . $editText . '" data-dismiss-all-check="' . $editCheck . '" '.self::alert_box_type_data($q).' >' . $editText . '</a></p>';
}
if (current_user_can('delete_others_posts')){
$deleteText = __('Delete all posts with alerts', 'pf');
$deleteText = apply_filters('ab_alert_specimens_delete_all_text', $deleteText);
echo '<p><a href="#" id="delete_all_alert_specimens" style="color:red;font-weight:bold;" title="' . __('Delete all posts with alerts', 'pf') . '" alert-check="' . $alertCheck . '" '.self::alert_box_type_data($q).' >' . $deleteText . '</a></p>';
}
} else {
$return_string = __('No problems!', 'pf');
$return_string = apply_filters('ab_alert_safe', $return_string);
echo $return_string;
}
} else {
echo 'Alert boxes not active.';
}
}
public function alert_box_outsides(){
$this->alert_box_insides_function();
}
public function admin_enqueue_scripts(){
if(self::is_on()){
$dir = plugins_url('/', __FILE__);
wp_register_script('alert-box-handler', $dir . 'assets/js/alert-handler.js', array( 'jquery' ));
if (is_admin()){
wp_enqueue_script('alert-box-handler');
}
}
}
public static function find_a_setting($args, $default = array(), $settings){
if (!empty($settings) && !isset($_POST)){
$settings = $settings;
} else {
$settings = get_option( self::$option_name, array() );
}
if (empty($settings)) {
$r = '';
} elseif (empty($settings[$args['parent_element']]) || empty($settings[$args['parent_element']][$args['element']])){
$r = '';
} elseif (!empty($args['parent_element']) && !empty($args['element'])){
$r = $settings[$args['parent_element']][$args['element']];
} elseif (!empty($args['parent_element'])) {
$r = $settings[$args['parent_element']];
} else {
$r = '';
}
if (empty($r) && !empty($settings)){
$r = 'false';
}
if (empty($r)){
#$default = array($args['parent_element'] => array($args['element'] => ''));
return $default;
} else {
return $r;
}
}
public function setting($args, $default = array()){
$settings = $this->settings;
$r = self::find_a_setting($args, $default, $settings);
return $r;
}
public function settings_field_maker($args){
$parent_element = $args['parent_element'];
$element = $args['element'];
$type = $args['type'];
$label = $args['label_for'];
$default = $args['default'];
switch ($type) {
case 'checkbox':
$check = self::setting($args, $default);
if ('true' == $check){
$mark = 'checked';
} else {
$mark = '';
}
echo '<input id="'.$element.'" type="checkbox" name="'.$this->option_name.'['.$parent_element.']['.$element.']" value="true" '.$mark.' class="'.$args['parent_element'].' '.$args['element'].'" /> <label for="'.$this->option_name.'['.$parent_element.']['.$element.']" class="'.$args['parent_element'].' '.$args['element'].'" >' . $label . '</label>';
break;
case 'text':
echo "<input type='text' id='".$element."' name='".$this->option_name."[".$parent_element."][".$element."]' value='".esc_attr(self::setting($args, $default))."' class='".$args['parent_element']." ".$args['element']."' /> <label for='".$this->option_name."[".$parent_element."][".$element."]' class='".$args['parent_element']." ".$args['element']."' >" . $label . "</label>";
break;
}
}
public static function settings_fields(){
$switch = array(
'parent_element' => 'alert_check',
'element' => 'alert_switch',
'type' => 'checkbox',
'label_for' => __('Turn alerts on.', 'pf'),
'default' => 'true'
);
return array('switch' => $switch);
}
public function settings_field_settings_page(){
#var_dump('die');die();
register_setting( 'general', self::$option_name, array($this, 'validator') );
$args = the_alert_box()->settings_fields();
add_settings_field( 'alert_box_check', __('Active Alert Boxes?', 'pf'), array($this, 'settings_field_maker'), 'general', 'default', $args['switch']);
}
public function validator($input){
#$output = get_option( $this->option_name );
#update_option($this->option_name, $_POST['alert_box_options']);
#var_dump($_POST['alert_box_options']); die();
return $input;
}
public function is_on(){
$alert_settings = self::settings_fields();
$alert_switch = $alert_settings['switch'];
#var_dump('<pre>');
#var_dump($alert_switch);
$check = self::find_a_setting($alert_switch, $alert_switch['default'], get_option( self::$option_name, array() ));#die();
#var_dump($check);
if ('true' == $check){
$state = true;
} else {
$state = false;
}
return $state;
}
}
function the_alert_box() {
return The_Alert_Box::init();
}
// Start me up!
the_alert_box();
}