· 7 years ago · Jul 21, 2018, 01:58 AM
1<?php
2
3/**
4 *
5 * Plugin Name: WP Report Post
6 * Plugin URI: http://www.esiteq.com/projects/wordpress-report-post-plugin/
7 * Description: Simple and lighweight plugin to let your site visitors report inappropriate posts
8 * Author: Alex Raven
9 * Company: ESITEQ
10 * Version: 2.1.2
11 * Updated 2016-06-12
12 * Created 2013-09-22
13 * Text Domain: wp-report-post
14 * Domain Path: /languages
15 * Author URI: http://www.esiteq.com/
16 * License: GPL3
17 *
18 */
19
20if(!class_exists('WP_List_Table'))
21{
22 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
23}
24// this is for
25if(!class_exists( 'WP_Http' ))
26{
27 require_once(ABSPATH . WPINC. '/class-http.php');
28}
29//
30class WP_Report_Post_2
31{
32 var $options;
33 var $proto = 'http';
34 var $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
35 // Default values, used if option is not found
36 var $defaults;
37 // Editable texts
38 var $text_options;
39 var $post;
40 // Import old reports generated by plugin v0.2 (used once)
41 function import_old()
42 {
43 global $wpdb;
44 if (get_option('wp_report_post_import_v0') == '1') { return false; }
45 $sql = $wpdb->prepare("SELECT *, UNIX_TIMESTAMP(`dt`) AS ts FROM {$wpdb->prefix}reported_posts LEFT JOIN {$wpdb->posts} ON {$wpdb->prefix}reported_posts.post_id={$wpdb->posts}.ID", 1);
46 $reports = $wpdb->get_results($sql, ARRAY_A);
47 foreach ($reports as $row)
48 {
49 if ($row['ID'])
50 {
51 $data = array
52 (
53 'user_id' => $row['user_id'],
54 'email' => $row['user_email'],
55 'name' => $row['user_name'],
56 'msg' => $row['message'],
57 'post_id' => $row['post_id'],
58 'timestamp' => $row['ts']
59 );
60 $rep = get_post_meta($row['post_id'], '_wp_report_post', true);
61 if (is_array($rep))
62 {
63 $exist = false;
64 foreach ($rep as $rrow)
65 {
66 if ($rrow['email'] == $data['email'])
67 {
68 $exist = true;
69 }
70 }
71 if (!$exist)
72 {
73 $rep[] = $data;
74 }
75 }
76 else
77 {
78 $rep[] = $data;
79 }
80 update_post_meta($row['post_id'], '_wp_report_post', $rep);
81 }
82 }
83 update_option('wp_report_post_import_v0', '1');
84 return true;
85 }
86 //
87 function enqueue_scripts()
88 {
89 wp_enqueue_style('wp-report-post', plugins_url( '/css/style.css', __file__ ), false);
90 wp_enqueue_style('remodal', plugins_url( '/lib/remodal/remodal.css', __file__ ), false);
91 wp_enqueue_script('remodal', plugins_url( '/lib/remodal/remodal.js', __file__ ), array('jquery'));
92 if ($this->get_option('use_captcha') == '1')
93 {
94 wp_enqueue_script('recaptcha', $this->proto. '://www.google.com/recaptcha/api.js');
95 }
96 }
97 // Returns true if user is not logged in; If require_login is false, always returns false
98 function not_logged_in()
99 {
100 if ($this->get_option('require_login', $this->defaults['require_login']) == '0')
101 {
102 return false;
103 }
104 return !is_user_logged_in();
105 }
106 // Frontend footer scripts
107 function footer_scripts()
108 {
109 $report_post_name_val = '';
110 $report_post_email_val = '';
111 if (is_user_logged_in())
112 {
113 $user = wp_get_current_user();
114 $report_post_name_val = ' value="'. esc_attr($user->display_name). '" readonly="readonly"';
115 $report_post_email_val = ' value="'. esc_attr($user->user_email). '" readonly="readonly"';
116 }
117?>
118<div class="remodal" data-remodal-id="report-post" role="dialog" aria-labelledby="report-post-modal-title" aria-describedby="report-post-modal-desc">
119 <a data-remodal-action="close" class="remodal-close" aria-label="<?php _e('Close', 'wp-report-post'); ?>"></a>
120 <div>
121 <h2 id="report-post-modal-title"><?php echo $this->get_option('text_report_post', $this->defaults['text_report_post']); ?></h2>
122 <p id="report-post-modal-desc">
123<?php if ($this->not_logged_in())
124{
125?>
126 <?php echo $this->get_option(sprintf('text_require_login', wp_login_url()), sprintf($this->defaults['text_require_login'], wp_login_url())); ?>
127<?php
128}
129else
130{
131?>
132 «<b><span id="report-post-title"> </span></b>»
133 <?php } ?>
134 </p>
135 <?php if (!$this->not_logged_in()) { ?>
136 <p id="report-post-modal-msg"> </p>
137 <form class="report-post-form" id="report-post-form">
138 <input type="hidden" name="subaction" value="report-post" />
139 <input type="hidden" name="report_post_id" id="report-post-id" value="0" />
140 <div class="report-post-half-left">
141 <p><?php echo $this->get_option('text_your_name'); ?></p>
142 <input class="report-post-control" id="report_post_name" name="report_post_name"<?php echo $report_post_name_val; ?> />
143 </div>
144 <div class="report-post-half-right">
145 <p><?php echo $this->get_option('text_your_email'); ?></p>
146 <input class="report-post-control" id="report_post_email" name="report_post_email"<?php echo $report_post_email_val; ?> />
147 </div>
148 <div style="clear: both;"></div>
149 <div>
150 <p><?php echo $this->get_option('text_your_msg'); ?></p>
151 <textarea class="report-post-control" rows="5" id="report_post_msg" name="report_post_msg"></textarea>
152 </div>
153 <div class="report-post-half-left">
154<?php
155if ($this->get_option('use_captcha') == '1')
156{
157echo ' <p>', $this->get_option('text_verification_code'), '</p>'."\n";
158echo ' <div class="g-recaptcha" data-sitekey="', esc_attr($this->get_option('recaptcha_sitekey')), '"></div>'."\n";
159}
160?>
161 </div>
162 <div class="report-post-half-right">
163<?php
164if ($this->get_option('show_ip_to_client') == '1')
165{
166 if ($this->get_option('use_captcha') == '1')
167 {
168?>
169 <p> </p>
170 <h1 class="report-post-ip"><?php echo $this->get_option('text_your_ip'); ?> <?php echo $this->get_user_ip(); ?></h1>
171<?php
172 }
173 else
174 {
175?>
176 <p class="report-post-ip"><?php echo $this->get_option('text_your_ip'); ?> <?php echo $this->get_user_ip(); ?></p>
177<?php
178 }
179}
180?>
181 </div>
182 <div style="clear: both;"></div>
183 </form>
184 <?php } ?>
185 </div>
186 <?php if (!$this->not_logged_in()) { ?>
187 <div id="report-post-buttons">
188 <br />
189 <a data-remodal-action="cancel" class="remodal-cancel"><?php echo $this->get_option('text_cancel', $this->defaults['text_cancel']); ?></a>
190 <a id="report-post-submit" class="remodal-confirm"><?php echo $this->get_option('text_submit', $this->defaults['text_submit']); ?></a>
191 </div>
192 <?php } ?>
193</div>
194
195<script type="text/javascript">
196jQuery(document).ready(function($)
197{
198 window.REMODAL_GLOBALS =
199 {
200 NAMESPACE: 'report-post',
201 DEFAULTS:
202 {
203 hashTracking: false,
204 closeOnConfirm: false
205 }
206 }
207 // add after
208<?php
209 if ($this->get_option('add_what', $this->defaults['add_what_option']) != '')
210 {
211?>
212 var report_post_link = '<a href="#" class="report-post-<?php echo $this->get_option('add_what', $this->defaults['add_what_option']); ?>"><?php echo esc_js($this->get_option('text_report_link', $this->defaults['text_report_link'])); ?></a>';
213 $('<?php echo esc_js($this->get_option('add_after', $this->defaults['add_after_option'])); ?>').after(report_post_link);
214<?php
215 }
216?>
217 var _remodal = $('[data-remodal-id=report-post]').remodal({modifier: 'with-red-theme', hashTracking: false, closeOnConfirm: false});
218 $(document).on('opened', '.remodal', function ()
219 {
220 $('#report-post-buttons').slideDown(1000);
221 $('#report-post-form').slideDown(1000, function()
222 {
223 if ($('#report_post_name').val()=='')
224 {
225 $('#report_post_name').focus();
226 }
227 else
228 {
229 $('#report_post_msg').focus();
230 }
231 });
232 });
233 $('#report-post-submit').click(function(e)
234 {
235 e.preventDefault();
236 $('#report-post-modal-desc').css('display', 'block');
237 $('#report-post-modal-msg').css('display', 'none');
238 $('.report-post-control').removeClass('report-post-control-error');
239 //_remodal.close();
240 $.post('<?php echo admin_url('admin-ajax.php'); ?>?action=wp_report_post', $('#report-post-form').serialize(), function(data)
241 {
242 if (data.errmsg)
243 {
244 $('#report-post-modal-desc').css('display', 'none');
245 $('#report-post-modal-msg').css('display', 'block');
246 $('#report-post-modal-msg').html(data.errmsg);
247 $('#report-post-modal-msg').addClass('report-post-error');
248 $('#report-post-modal-msg').removeClass('report-post-success');
249 if (data.field)
250 {
251 $('#'+data.field).addClass('report-post-control-error');
252 $('#'+data.field).focus();
253 }
254 else
255 {
256 $('#report_post_msg').focus();
257 }
258 }
259 if (data.msg)
260 {
261 $('#report-post-modal-desc').css('display', 'none');
262 $('#report-post-modal-msg').css('display', 'block');
263 $('#report-post-modal-msg').html(data.msg);
264 $('#report-post-modal-msg').removeClass('report-post-error');
265 $('#report-post-modal-msg').addClass('report-post-success');
266 $('#report_post_msg').val('');
267 $('#report-post-form').slideUp(1000);
268 $('#report-post-buttons').slideUp(1000);
269 }
270 }, 'json');
271
272 });
273 $('.report-post-link,.report-post-button,.report-post-custom-link,.report-post-custom-button').click(function(e)
274 {
275 e.preventDefault();
276 $('#report-post-modal-desc').css('display', 'block');
277 $('#report-post-modal-msg').css('display', 'none');
278 $('.report-post-control').removeClass('report-post-control-error');
279 var post_id=0;
280 if ($(this).attr('post-id') != undefined)
281 {
282 post_id = parseInt($(this).attr('post-id'));
283 }
284 else
285 {
286 var article_id = $(this).closest('article').attr('id');
287 if (article_id != undefined)
288 {
289 var post_id = parseInt(article_id.replace( /^\D+/g, ''));
290
291 }
292 }
293 $('#report-post-id').val(post_id);
294 $.post('<?php echo admin_url('admin-ajax.php'); ?>?action=wp_report_post', {subaction: 'get-post', post_id: post_id}, function(data)
295 {
296 $('#report-post-title').html(data.post_title);
297 _remodal.open();
298 }, 'json');
299 });
300});
301</script>
302<?php
303 }
304 //
305 function admin_menu()
306 {
307 add_menu_page( __('Reported Posts', 'wp-report-post'), __('Reported Posts', 'wp-report-post'), 'edit_others_posts', 'wp-report-post', array($this, 'reported_posts'), 'dashicons-megaphone' );
308 add_submenu_page('wp-report-post', __('Options', 'wp-report-post'), __('Options', 'wp-report-post'), 'edit_others_posts', 'wp-report-post-options', array($this, 'options_page'));
309 }
310 // Display reported posts
311 function reported_posts()
312 {
313 $this->import_old();
314 $reports = new WP_Report_Post_List();
315 $reports->options = $this->options;
316 $reports->defaults = $this->defaults;
317 $reports->prepare_items();
318?>
319<div class="wrap">
320 <h2>Reported Posts</h2>
321 <form id="reports-filter" method="get">
322 <input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
323 <?php $reports->display(); ?>
324 </form>
325</div>
326<?php
327 }
328 // Select
329 function select($name, $options, $value)
330 {
331 echo '<select name="', $name, '">';
332 foreach ($options as $key=>$val)
333 {
334 $sel = ($key == $value) ? ' selected="selected"' : '';
335 echo '<option value="', esc_attr($key), '"', $sel, '>', esc_html($val), '</option>';
336 }
337 echo '</select>';
338 }
339 // Input
340 function input($name, $value, $type='text', $class='')
341 {
342 echo '<input type="', esc_attr($type), '" name="', esc_attr($name), '" id="', esc_attr($name), '" value="', esc_attr($value), '"', ($class!='') ? ' class="'. esc_attr($class). '"' : '', ' />';
343 }
344 // Textarea
345 function textarea($name, $value, $rows='5', $class='')
346 {
347 echo '<textarea name="', esc_attr($name), '" id="', esc_attr($name), '" rows="', esc_attr($rows), '"', ($class!='') ? ' class="'. esc_attr($class). '"' : '', '>', esc_html($value), '</textarea>';
348 }
349 // Checkbox
350 function checkbox($name, $value)
351 {
352 $checked = ($value == '1') ? ' checked="checked"' : '';
353 echo '<input type="checkbox" name="', esc_attr($name), '" id="', esc_attr($name), '" value="1"', $checked, ' />';
354 }
355 // Get plugin option (if none found, taken from $this->defaults)
356 function get_option($name, $default='')
357 {
358 if ($default == '') $default = $this->defaults[$name];
359 return (isset($this->options[$name])) ? $this->options[$name] : $default;
360 }
361 //
362 function set_option($name, $value)
363 {
364 $this->options[$name] = $value;
365 }
366 // Update plugin options
367 function update_options()
368 {
369 return update_option('wp_report_post_options', $this->options);
370 }
371 // Display options page
372 function options_page()
373 {
374 if ($_POST)
375 {
376 foreach ($_POST as $key=>$value)
377 {
378 $this->options[$key] = stripslashes($value);
379 }
380 $this->options['require_login'] = (!isset($_POST['require_login'])) ? '0' : '1';
381 $this->options['use_captcha'] = (!isset($_POST['use_captcha'])) ? '0' : '1';
382 $this->options['show_ip_to_client'] = (!isset($_POST['show_ip_to_client'])) ? '0' : '1';
383 $this->options['email_notification'] = (!isset($_POST['email_notification'])) ? '0' : '1';
384 $this->update_options();
385 }
386 ?>
387<div class="wrap">
388 <form method="post" id="report-post-options">
389 <h2><?php _e('WP Report Post Options', 'wp-report-post'); ?></h2>
390 <h2 class="wp-report-post-options-section"><?php _e('Integration', 'wp-report-post'); ?></h2>
391 <table class="form-table wp-report-post-options-table">
392 <tr>
393 <th scope="row"><?php _e('Automatically add', 'wp-report-post'); ?></th>
394 <td><?php $this->select('add_what', $this->defaults['add_what_options'], $this->get_option('add_what')); ?><span> <?php _e('(Select Nothing if you want to add link or button manually - in template file)', 'wp-report-post'); ?></span>
395 </td>
396 </tr>
397 <tr>
398 <th scope="row"><?php _e('Add after element', 'wp-report-post'); ?></th>
399 <td><?php $this->input('add_after', $this->get_option('add_after')); ?></td>
400 </tr>
401 <tr>
402 <th scope="row"><?php _e('Message Min Length', 'wp-report-post'); ?></th>
403 <td><?php $this->input('msg_min_length', $this->get_option('msg_min_length')); ?></td>
404 </tr>
405 <tr>
406 <th scope="row"><?php _e('Date / Time format', 'wp-report-post'); ?></th>
407 <td><?php $this->select('date_format', $this->defaults['date_format_options'], $this->get_option('date_format')); ?></td>
408 </tr>
409 <tr>
410 <th scope="row"> </th>
411 <td><label for="require_login"><?php $this->checkbox('require_login', $this->get_option('require_login')); ?> <?php _e('Require user to be logged in to report', 'wp-report-post'); ?></label></td>
412 </tr>
413<?php
414/* Wordpress will handle it automatically - if no CURL, it will use fopen() :-)
415if (!function_exists('curl_init'))
416{
417?>
418 <tr>
419 <th scope="row"> </th>
420 <td><p class="report-post-error">WARNING! You need PHP Curl extension installed and running on your server to use reCaptcha!</p></td>
421 </tr>
422<?php
423}
424*/
425?>
426 </table>
427 <h2 class="wp-report-post-options-section"><?php _e('Antispam', 'wp-report-post'); ?></h2>
428 <table class="form-table wp-report-post-options-table">
429 <tr>
430 <th scope="row"> </th>
431 <td><label for="use_captcha"><?php $this->checkbox('use_captcha', $this->get_option('use_captcha')); ?> <?php _e('Use reCaptcha (you need both site-key and secret-key that you can obtain for free <a href="https://www.google.com/recaptcha/admin" target="_blank">here</a>)', 'wp-report-post'); ?></label></td>
432 </tr>
433 <tr>
434 <th scope="row"><?php _e('reCaptcha Site Key', 'wp-report-post'); ?></th>
435 <td><?php $this->input('recaptcha_sitekey', $this->get_option('recaptcha_sitekey')); ?></td>
436 </tr>
437 <tr>
438 <th scope="row"><?php _e('reCaptcha Secret Key'); ?></th>
439 <td><?php $this->input('recaptcha_secret', $this->get_option('recaptcha_secret')); ?></td>
440 </tr>
441 <tr>
442 <th scope="row"> </th>
443 <td><label for="show_ip_to_client"><?php $this->checkbox('show_ip_to_client', $this->get_option('show_ip_to_client')); ?> <?php _e('Show IP to client that is reporting (otherwise to Admin only)', 'wp-report-post'); ?></label></td>
444 </tr>
445 <tr>
446 <th scope="row"><?php _e('IP Whois URL:', 'wp-report-post'); ?></th>
447 <td><?php $this->input('whois_url', $this->get_option('whois_url')); ?></td>
448 </tr>
449 </table>
450 <h2 class="wp-report-post-options-section"><?php _e('Email Notification', 'wp-report-post'); ?></h2>
451 <table class="form-table wp-report-post-options-table">
452 <tr>
453 <th scope="row"> </th>
454 <td><label for="require_login"><?php $this->checkbox('email_notification', $this->get_option('email_notification')); ?> <?php _e('Send email notification when somebody reports a post or page', 'wp-report-post'); ?></label></td>
455 </tr>
456 <tr>
457 <th scope="row"><?php _e('Send Notification to', 'wp-report-post'); ?></th>
458 <td><?php $this->input('email_address', $this->get_option('email_address')); ?></td>
459 </tr>
460 <tr>
461 <th scope="row"><?php _e('Email Subject', 'wp-report-post'); ?></th>
462 <td><?php $this->input('email_subject', $this->get_option('email_subject')); ?></td>
463 </tr>
464 <tr>
465 <th scope="row"><?php _e('Email Body', 'wp-report-post'); ?></th>
466 <td><?php $this->textarea('email_body', $this->get_option('email_body'), 8); ?>
467 <p><b><?php _e('Variables:', 'wp-report-post'); ?> </b>[post_title], [post_id], [name], [email], [ip], [message], [date], [time], [permalink]</p>
468 </td>
469 </tr>
470 </table>
471 <h2 class="wp-report-post-options-section"><?php _e('Texts', 'wp-report-post'); ?></h2>
472 <table class="form-table wp-report-post-options-table">
473<?php
474 foreach ($this->text_options as $key => $value)
475 {
476?>
477 <tr>
478 <th scope="row"><?php echo $value; ?></th>
479 <td><?php $this->input($key, $this->get_option($key, $this->defaults[$key])); ?></td>
480 </tr>
481<?php
482 }
483 ?>
484 </table>
485 <p class="submit">
486 <input type="submit" class="button-primary" value="<?php _e('Save Options', 'wp-report-post'); ?>" />
487 </p>
488 </form>
489</div>
490 <?php
491 }
492 // Returns current user IP
493 function get_user_ip()
494 {
495 foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
496 {
497 if (array_key_exists($key, $_SERVER) === true)
498 {
499 foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
500 {
501 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
502 {
503 return $ip;
504 }
505 }
506 }
507 }
508 return $_SERVER['REMOTE_ADDR'];
509 }
510 // Sends email notification (if feature is checked in Options)
511 function email_notification()
512 {
513 if ($this->get_option('email_notification') != '1')
514 {
515 return false;
516 }
517 $args = array(
518 '[post_title]' => $this->post['post_title'],
519 '[email]' => $this->post['report_post_email'],
520 '[name]' => $this->post['report_post_name'],
521 '[post_id]' => $this->post['report_post_id'],
522 '[date]' => date(get_option('date_format'), current_time('timestamp')),
523 '[time]' => date(get_option('time_format'), current_time('timestamp')),
524 '[ip]' => $this->get_user_ip(),
525 '[message]' => $this->post['report_post_msg'],
526 '[permalink]' => get_post_permalink($this->post['report_post_id'])
527 );
528 $subject = $this->get_option('email_subject');
529 $body = $this->get_option('email_body');
530 $email = ($this->get_option('email_address') != '') ? $this->get_option('email_address') : get_option('admin_email');
531 foreach ($args as $key => $value)
532 {
533 $body = str_replace($key, $value, $body);
534 }
535 return wp_mail($email, $subject, $body);
536 }
537 // AJAX functions
538 function wp_report_post()
539 {
540 global $wpdb;
541 $this->post = $_POST;
542 $json = array('errmsg'=>'', 'msg'=>'');
543 if ($_POST['subaction'] == 'unpublish-post' && current_user_can('edit_others_posts'))
544 {
545 $post = get_post($_POST['post_id']);
546 $json['post_id'] = 0;
547 if ($post)
548 {
549 $new_status = ($post->post_status == 'publish') ? 'draft' : 'publish';
550 $args = array(
551 'ID' => $_POST['post_id'],
552 'post_status' => $new_status
553 );
554 ob_start();
555 $json['post_id'] = wp_update_post($args);
556 $json['post_status'] = ucwords($new_status);
557 $json['post_action'] = ($new_status == 'publish') ? 'Unpublish' : 'Publish';
558 ob_end_clean();
559 }
560 echo json_encode($json);
561 die();
562 }
563 //
564 if ($_POST['subaction'] == 'report-post')
565 {
566 $post = get_post($_POST['report_post_id']);
567 if (!$post)
568 {
569 echo json_encode(array('errmsg'=>$this->get_option('text_post_doesnt_exist', $this->defaults['text_post_doesnt_exist']), 'field'=>''));
570 die();
571 }
572 $json['post'] = $post;
573 $this->post['post_title'] = $post->post_title;
574 if (!filter_var($_POST['report_post_email'], FILTER_VALIDATE_EMAIL) === false)
575 {
576 //
577 }
578 else
579 {
580 echo json_encode(array('errmsg'=>$this->get_option('text_email_invalid', $this->defaults['text_email_invalid']), 'field'=>'report_post_email'));
581 die();
582 }
583 if (strlen($_POST['report_post_name']) < 2)
584 {
585 echo json_encode(array('errmsg'=>$this->get_option('text_name_invalid'), 'field'=>'report_post_name'));
586 die();
587 }
588 if (strlen($_POST['report_post_msg']) < $this->get_option('msg_min_length'))
589 {
590 echo json_encode(array('errmsg'=>$this->get_option('text_msg_invalid'), 'field'=>'report_post_msg'));
591 die();
592 }
593 if ($this->get_option('use_captcha') == '1')
594 {
595 $response = wp_remote_post
596 (
597 $this->recaptcha_url, array
598 (
599 'method' => 'POST',
600 'blocking' => true,
601 'body' => array
602 (
603 'response' => $_POST['g-recaptcha-response'],
604 'secret' => $this->get_option('recaptcha_secret'),
605 'remoteip' => $this->get_user_ip()
606 ),
607 )
608 );
609 $tmp = (array)@json_decode($response['body']);
610 $success = intval($tmp['success']);
611 if ($success !== 1)
612 {
613 echo json_encode(array('errmsg'=>$this->get_option('text_captcha_invalid'), 'field'=>''));
614 die();
615 }
616 }
617 $data = array
618 (
619 'user_id' => get_current_user_id(),
620 'user_ip' => $this->get_user_ip(),
621 'email' => $_POST['report_post_email'],
622 'name' => $_POST['report_post_name'],
623 'msg' => $_POST['report_post_msg'],
624 'post_id' => $_POST['report_post_id'],
625 'timestamp' => current_time('timestamp')
626 );
627 $reports = get_post_meta($_POST['report_post_id'], '_wp_report_post', true);
628 if (is_array($reports))
629 {
630 foreach ($reports as $report)
631 {
632 if ($report['email'] == $_POST['report_post_email'])
633 {
634 echo json_encode(array('errmsg'=>$this->get_option('text_already_reported', $this->defaults['text_already_reported']), 'field'=>'report_post_msg'));
635 die();
636 }
637 }
638 $reports[] = $data;
639 }
640 else
641 {
642 $reports = array();
643 $reports[] = $data;
644 }
645 $meta_id = update_post_meta($_POST['report_post_id'], '_wp_report_post', $reports);
646 if ($meta_id)
647 {
648 $this->email_notification();
649 echo json_encode(array('msg'=>$this->get_option('text_success'), 'field'=>'', 'meta_id'=>$meta_id, 'reports'=>$reports));
650 die();
651 }
652 else
653 {
654 echo json_encode(array('errmsg'=>$this->get_option('text_error'), 'field'=>''));
655 die();
656 }
657 }
658 if ($_POST['subaction'] == 'get-post')
659 {
660 $post_id = intval($_POST['post_id']);
661 $post = get_post($post_id);
662 $json['post_title'] = $post->post_title;
663 $json['post'] = $post;
664 }
665 $json['errmsg'] = '';
666 echo json_encode($json);
667 die();
668 }
669 // Admin footer scripts
670 function admin_footer_scripts()
671 {
672?>
673<div class="remodal" data-remodal-id="remodal-confirm" role="dialog" aria-labelledby="remodal-confirm-modal-title" aria-describedby="remodal-confirm-modal-desc">
674 <a data-remodal-action="close" class="remodal-close" aria-label="<?php _e('Close', 'wp-report-post'); ?>"></a>
675 <div>
676 <h2 id="remodal-confirm-modal-title"> </h2>
677 <p id="remodal-confirm-modal-desc"> </p>
678 </div>
679<br />
680<a data-remodal-action="cancel" class="remodal-cancel"><?php _e('Cancel', 'wp-report-post'); ?></a>
681<a id="remodal-confirm-submit" href="#" class="remodal-confirm"><?php _e('Confirm', 'wp-report-post'); ?></a>
682</div>
683<script>
684var _remodal_confirm;
685function getQueryParams(qs)
686{
687 qs = qs.split('+').join(' ');
688
689 var params = {},
690 tokens,
691 re = /[?&]?([^=]+)=([^&]*)/g;
692
693 while (tokens = re.exec(qs)) {
694 params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
695 }
696
697 return params;
698}
699var query = getQueryParams(document.location.search);
700//
701function remodal_confirm(title, desc, confirm_link)
702{
703 jQuery(function($)
704 {
705 $('#remodal-confirm-modal-title').html(title);
706 $('#remodal-confirm-modal-desc').html(desc);
707 $('#remodal-confirm-submit').attr('href', confirm_link);
708 _remodal_confirm.open();
709 });
710}
711//
712jQuery(document).ready(function($)
713{
714 _remodal_confirm = $('[data-remodal-id=remodal-confirm]').remodal({modifier: 'with-red-theme', hashTracking: false, closeOnConfirm: false});
715 $('.report-post-unpublish-link').click(function(e)
716 {
717 var post_id = $(this).attr('post-id');
718 $.post(ajaxurl + '?action=wp_report_post', {subaction: 'unpublish-post', post_id: post_id}, function(data)
719 {
720 var $tr = $('#post-status-'+data.post_id).closest('tr');
721 $('#post-status-'+data.post_id).html(data.post_status);
722 $tr.addClass('highlighted');
723 setTimeout(function()
724 {
725 $tr.removeClass('highlighted');
726 }, 5000);
727 $('#unpublish-'+data.post_id).html(data.post_action);
728 }, 'json');
729 e.preventDefault();
730 });
731 $('.remodal-confirm-link').click(function(e)
732 {
733 var href = $(this).attr('href');
734 var the_title = $(this).attr('post-title');
735 var post_action = $(this).attr('post-action');
736 remodal_confirm(post_action+' <?php _e('Posts', 'wp-report-post'); ?>', '<?php _e('Are you sure you want to', 'wp-report-post'); ?> '+post_action+' <?php _e('the post titled', 'wp-report-post'); ?> «'+the_title+'»?', href);
737 e.preventDefault();
738 });
739 $('.report-user-row').click(function(e)
740 {
741 $div = $(this).next().next().next();
742 if ($div.css('display') == 'none')
743 {
744 $div.slideDown();
745 }
746 else
747 {
748 $div.slideUp();
749 }
750 e.preventDefault();
751 });
752 $('.report-user-hide-link').click(function(e)
753 {
754 $(this).parent().slideUp();
755 e.preventDefault();
756 });
757});
758</script>
759<?php
760 }
761 //
762 function __construct()
763 {
764 $this->defaults = array
765 (
766 'whois_url' => 'http://whois.domaintools.com/[ip]',
767 'email_notification' => '1',
768 'email_address' => '',
769 'email_subject' => __('[WP Report Post] Somebody reported a post!', 'wp-report-post'),
770 'email_body' => __("Hey, Admin!\nSomebody has just reported a post.\nTitle: [post_title]\nLink: [permalink]\nPost ID: [post_id]\nReporter Name: [name]\nReporter Email: [email]\nReporter IP: [ip]\nMessage: [message]\n--\nSincerely, WP Report Post Plugin", 'wp-report-post'),
771 'msg_min_length' => '16',
772 'use_captcha' => '0',
773 'show_ip_to_client' => '0',
774 'recaptcha_sitekey' => '',
775 'recaptcha_secret' => '',
776 'date_format' => 'human',
777 'date_format_options' => array('human' => __('Human (e.g 5 minutes ago)', 'wp-report-post'), 'date' => __('Date / Time', 'wp-report-post')),
778 'require_login' => '0',
779 'add_what_options' => array(''=>__('Nothing', 'wp-report-post'), 'link'=>__('Link', 'wp-report-post'), 'button'=>__('Button', 'wp-report-post')),
780 'add_what_option' => 'link',
781 'add_after_option' => 'span.byline',
782 'text_report_post' => __('Report Post', 'wp-report-post'),
783 'text_report_link' => __('Report Post', 'wp-report-post'),
784 'text_your_name' => __('Your Name:', 'wp-report-post'),
785 'text_your_email' => __('Your Email:', 'wp-report-post'),
786 'text_your_msg' => __('Please tell us why do you think this post is inappropriate and shouldn\'t be there:', 'wp-report-post'),
787 'text_cancel' => __('Cancel', 'wp-report-post'),
788 'text_submit' => __('Report', 'wp-report-post'),
789 'text_post_doesnt_exist'=> __('Specified Post does not exist', 'wp-report-post'),
790 'text_email_invalid' => __('Please provide a valid email address', 'wp-report-post'),
791 'text_name_invalid' => __('Please enter your name', 'wp-report-post'),
792 'text_msg_invalid' => __('Please describe why do you think this post is inappropriate', 'wp-report-post'),
793 'text_already_reported' => __('You have already reported this post', 'wp-report-post'),
794 'text_success' => __('You have successfully reported inappropriate post', 'wp-report-post'),
795 'text_error' => __('Error submitting report', 'wp-report-post'),
796 'text_require_login' => __('Please <a href="%s">log in</a> to report posts', 'wp-report-post'),
797 'text_verification_code'=> __('Verification Code:', 'wp-report-post'),
798 'text_your_ip' => __('Your IP:', 'wp-report-post'),
799 'text_captcha_invalid' => __('Invalid verification code', 'wp-report-post')
800 );
801 $this->text_options = array
802 (
803 'text_report_link' => __('Link Text', 'wp-report-post'),
804 'text_report_post' => __('Modal Form Title', 'wp-report-post'),
805 'text_your_name' => __('Your Name:', 'wp-report-post'),
806 'text_your_email' => __('Your Email:', 'wp-report-post'),
807 'text_your_msg' => __('Your Message:', 'wp-report-post'),
808 'text_cancel' => __('Cancel button', 'wp-report-post'),
809 'text_submit' => __('Submit button', 'wp-report-post'),
810 'text_post_doesnt_exist'=> __('Invalid Post', 'wp-report-post'),
811 'text_email_invalid' => __('Invalid Email', 'wp-report-post'),
812 'text_name_invalid' => __('Invalid Name', 'wp-report-post'),
813 'text_msg_invalid' => __('Invalid Message', 'wp-report-post'),
814 'text_already_reported' => __('Already reported', 'wp-report-post'),
815 'text_success' => __('Successfully reported', 'wp-report-post'),
816 'text_error' => __('Error reporting', 'wp-report-post'),
817 'text_verification_code'=> __('Text for Captcha', 'wp-report-post'),
818 'text_your_ip' => __('Your IP: text', 'wp-report-post'),
819 'text_captcha_invalid' => __('Invalid Captcha', 'wp-report-post')
820 );
821 $this->proto = is_ssl() ? 'https' : 'http';
822 $this->options = maybe_unserialize(get_option('wp_report_post_options'));
823 add_action('init', array($this, 'enqueue_scripts'));
824 add_action('wp_print_footer_scripts', array($this, 'footer_scripts'));
825 add_action('admin_menu', array($this, 'admin_menu'));
826 add_action('wp_ajax_wp_report_post', array($this, 'wp_report_post'));
827 add_action('wp_ajax_nopriv_wp_report_post', array($this, 'wp_report_post'));
828 add_action('admin_print_footer_scripts', array($this, 'admin_footer_scripts'));
829 }
830}
831
832// Reported posts list
833class WP_Report_Post_List extends WP_List_Table
834{
835 var $options, $defaults;
836 function __construct()
837 {
838 global $status, $page;
839 parent::__construct( array(
840 'singular' => 'reported_posts',
841 'plural' => 'reported_post',
842 'ajax' => false
843 ) );
844 }
845 //
846 function time_elapsed_string($ptime)
847 {
848 $etime = current_time('timestamp') - $ptime;
849 if ($etime < 1)
850 {
851 return 'just now';
852 }
853 $a = array
854 (
855 365 * 24 * 60 * 60 => __('year', 'wp-report-post'),
856 30 * 24 * 60 * 60 => __('month', 'wp-report-post'),
857 24 * 60 * 60 => __('day', 'wp-report-post'),
858 60 * 60 => __('hour', 'wp-report-post'),
859 60 => __('minute', 'wp-report-post'),
860 1 => __('second', 'wp-report-post')
861 );
862 $a_plural = array
863 (
864 __('year', 'wp-report-post') => __('years', 'wp-report-post'),
865 __('month', 'wp-report-post') => __('months', 'wp-report-post'),
866 __('day', 'wp-report-post') => __('days', 'wp-report-post'),
867 __('hour', 'wp-report-post') => __('hours', 'wp-report-post'),
868 __('minute', 'wp-report-post') => __('minutes', 'wp-report-post'),
869 __('second', 'wp-report-post') => __('seconds', 'wp-report-post')
870 );
871 foreach ($a as $secs => $str)
872 {
873 $d = $etime / $secs;
874 if ($d >= 1)
875 {
876 $r = round($d);
877 return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . __(' ago', 'wp-report-post');
878 }
879 }
880 }
881 //
882 function format_timestamp($ts)
883 {
884 $date_format = isset($this->options['date_format']) ? $this->options['date_format'] : $this->defaults['date_format'];
885 if ($date_format == 'human')
886 {
887 return $this->time_elapsed_string($ts);
888 }
889 return date(get_option('date_format'). ', '. get_option('time_format'), intval($ts));
890 }
891 //
892 function format_reports($reports)
893 {
894 $rep = maybe_unserialize($reports);
895 if (is_array($rep))
896 {
897 $html = '';
898 foreach ($rep as $row)
899 {
900 $html .= '<a href="#" title="'. __('Click to view report', 'wp-report-post'). '" class="report-user-row">'. esc_html($row['name']). ' <'. esc_html($row['email']). '></a> ('. $this->format_timestamp($row['timestamp']). ')';
901 if ($row['user_ip'])
902 {
903 $whois = str_replace('[ip]', urlencode($row['user_ip']), $this->options['whois_url']);
904 $html .= ' <a href="'. $whois. '" target="_blank">'. esc_html($row['user_ip']). '</a>';
905 }
906 $html .= '<br /><div class="report-user-hidden">'. esc_html($row['msg']);
907 $html .= ' <a href="#" class="report-user-hide-link">'. __('Hide', 'wp-report-post'). '</a>';
908 $html .= '</div>';
909 }
910 return $html;
911 }
912 else
913 {
914 return __('Error', 'wp-report-post');
915 }
916 }
917 //
918 function column_default($item, $column_name)
919 {
920 switch($column_name)
921 {
922 case 'post_id':
923 case 'post_date': return mysql2date(get_option('date_format'), $item['post_date']) . '<br />'. mysql2date(get_option('time_format'), $item['post_date']); break;
924 case 'post_author':
925 $user = get_userdata($item['post_author']);
926 return sprintf('%s <%s>', $user->display_name, $user->user_email);
927 break;
928 case 'post_title':
929 return sprintf('<a href="%s" title="View post in new tab" target="_blank">%s</a>', get_post_permalink($item['post_id']), $item['post_title']);
930 break;
931 case 'meta_value':
932 return $this->format_reports($item[$column_name]);
933 break;
934 case 'post_status':
935 return '<span id="post-status-'. $item['post_id']. '">'. ucwords($item['post_status']). '</span>';
936 default:
937 return $item[$column_name];
938 }
939 }
940 //
941 function column_post_title($item)
942 {
943 $new_status = ($item['post_status'] == 'publish') ? __('Unpublish', 'wp-report-post') : __('Publish', 'wp-report-post');
944 $actions = array(
945 'view' => sprintf('<a href="%s" title="%s" target="_blank">View</a>', get_post_permalink($item['post_id']), __('View post in new tab', 'wp-report-post')),
946 'edit' => sprintf('<a href="%s">%s</a>', get_edit_post_link($item['post_id']), __('Edit', 'wp-report-post')),
947 'delete' => sprintf('<a class="remodal-confirm-link" href="?page=%s&action=%s&post_id=%d" post-action="Delete" post-title="%s">%s</a>',$_REQUEST['page'], 'delete', $item['post_id'], esc_attr($item['post_title']), __('Delete', 'wp-report-post')),
948 'unpublish' => sprintf('<a href="#" class="report-post-unpublish-link" post-id="%d" id="unpublish-%d">%s</a>', $item['post_id'], $item['post_id'], $new_status),
949 'delete_rep'=> sprintf('<a class="remodal-confirm-link" href="?page=%s&action=%s&post_id=%d" post-action="%s" post-title="%s">%s</a>',$_REQUEST['page'], 'delete_rep', $item['post_id'], __('Delete Reports', 'wp-report-post'), esc_attr($item['post_title']), __('Delete Reports', 'wp-report-post')),
950 );
951 return sprintf('%1$s %2$s',
952 /*$1%s*/ sprintf('<a href="%s" title="%s" target="_blank">%s</a>', get_post_permalink($item['post_id']), __('View post in new tab', 'wp-report-post'), $item['post_title']),
953 /*$2%s*/ $this->row_actions($actions)
954 );
955 }
956 //
957 function column_cb($item)
958 {
959 return sprintf(
960 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
961 /*$1%s*/ 'post_id',
962 /*$2%s*/ $item['post_id']
963 );
964 }
965 //
966 function get_columns()
967 {
968 $columns = array(
969 'cb' => '<input type="checkbox" />',
970 'post_title' => __('Post Title', 'wp-report-post'),
971 'post_date' => __('Post Date', 'wp-report-post'),
972 'post_author'=> __('Post Author', 'wp-report-post'),
973 'post_status'=> __('Post Status', 'wp-report-post'),
974 'meta_value' => __('Reports', 'wp-report-post')
975 );
976 return $columns;
977 }
978 //
979 function get_sortable_columns()
980 {
981 $sortable_columns = array(
982 'post_date' => array('post_date', true),
983 'post_title' => array('post_title', false),
984 'post_author' => array('post_author', false),
985 'post_status' => array('post_status', false),
986 'meta_value' => array('meta_value', false)
987 );
988 return $sortable_columns;
989 }
990 //
991 function get_bulk_actions()
992 {
993 $actions = array(
994 'delete' => __('Delete', 'wp-report-post'),
995 'unpublish' => __('Unpublish', 'wp-report-post'),
996 'publish' => __('Publish', 'wp-report-post'),
997 'delete_rep'=> __('Delete Reports', 'wp-report-post')
998 );
999 return $actions;
1000 }
1001 //
1002 function process_bulk_action()
1003 {
1004 if (current_user_can('edit_others_posts'))
1005 {
1006 if (is_array($_GET['post_id']))
1007 {
1008 $ids = $_GET['post_id'];
1009 }
1010 else
1011 {
1012 $ids = array($_GET['post_id']);
1013 }
1014 foreach ($ids as $id)
1015 {
1016 if ('delete'===$this->current_action())
1017 {
1018 // trash it!
1019 wp_delete_post($id);
1020 }
1021 if ('unpublish' === $this->current_action())
1022 {
1023 $args = array(
1024 'ID' => $id,
1025 'post_status' => 'draft'
1026 );
1027 ob_start();
1028 wp_update_post($args);
1029 ob_end_clean();
1030 }
1031 if ('publish' === $this->current_action())
1032 {
1033 $args = array(
1034 'ID' => $id,
1035 'post_status' => 'publish'
1036 );
1037 ob_start();
1038 wp_update_post($args);
1039 ob_end_clean();
1040 }
1041 if ('delete_rep' === $this->current_action())
1042 {
1043 delete_post_meta($id, '_wp_report_post');
1044 }
1045 }
1046 }
1047 }
1048 //
1049 function prepare_items()
1050 {
1051 global $wpdb;
1052 $per_page = 25;
1053 $columns = $this->get_columns();
1054 $hidden = array();
1055 $sortable = $this->get_sortable_columns();
1056 $this->_column_headers = array($columns, $hidden, $sortable);
1057 $this->process_bulk_action();
1058 $orderby = esc_sql( isset($_GET['orderby']) ? $_GET['orderby'] : 'post_id' );
1059 $order = esc_sql( isset($_GET['order']) ? $_GET['order'] : 'desc' );
1060 $current_page = $this->get_pagenum();
1061 $start = ($current_page-1) * $per_page;
1062 $args = array(
1063 'posts_per_page' => 5,
1064 'offset' =>0
1065 );
1066 $sql_count = $wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->postmeta} LEFT JOIN {$wpdb->posts} ON {$wpdb->postmeta}.post_id={$wpdb->posts}.ID WHERE meta_key='_wp_report_post'", 1);
1067 $sql = $wpdb->prepare("SELECT * FROM {$wpdb->postmeta} LEFT JOIN {$wpdb->posts} ON {$wpdb->postmeta}.post_id={$wpdb->posts}.ID WHERE meta_key='_wp_report_post' ORDER BY {$orderby} {$order} LIMIT %d,%d", $start, $per_page);
1068 //echo $sql, '<hr>';
1069 $data = $wpdb->get_results($sql, ARRAY_A);
1070 $total_items = $wpdb->get_var($sql_count);
1071 $this->items = $data;
1072 $this->set_pagination_args( array
1073 (
1074 'total_items' => $total_items,
1075 'per_page' => $per_page,
1076 'total_pages' => ceil($total_items/$per_page)
1077 ) );
1078 }
1079}
1080
1081$_wp_report_post_2 = new WP_Report_Post_2;
1082?>