Advertisement
Guest User

report-post-viewer

a guest
Sep 1st, 2023
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | Source Code | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Report Post Plugin
  4. Description: This plugin adds a report post feature and a view reported posts admin page.
  5. Version: 1.0
  6. Author: Anonim
  7. */
  8.  
  9. // Create the reported_posts table
  10. function create_reported_posts_table() {
  11. global $wpdb;
  12. $table_name = $wpdb->prefix . 'reported_posts';
  13.  
  14. $charset_collate = $wpdb->get_charset_collate();
  15.  
  16. $sql = "CREATE TABLE $table_name (
  17. id mediumint(9) NOT NULL AUTO_INCREMENT,
  18. flag_id varchar(255) NOT NULL,
  19. video_id bigint(20) NOT NULL,
  20. flag_message text,
  21. reported_at datetime NOT NULL,
  22. PRIMARY KEY (id)
  23. ) $charset_collate;";
  24.  
  25. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  26. dbDelta($sql);
  27. }
  28. register_activation_hook(__FILE__, 'create_reported_posts_table');
  29.  
  30. // Hook to deactivate the plugin and perform cleanup
  31. register_deactivation_hook(__FILE__, 'deactivate_plugin_function');
  32.  
  33. function deactivate_plugin_function() {
  34. global $wpdb;
  35. $table_name = $wpdb->prefix . 'reported_posts';
  36.  
  37. // Drop the table if it exists
  38. $wpdb->query("DROP TABLE IF EXISTS $table_name");
  39. }
  40.  
  41. // Register uninstall hook
  42. register_uninstall_hook(__FILE__, 'delete_reported_posts_table');
  43.  
  44. // Function to delete the reported_posts table
  45. function delete_reported_posts_table() {
  46. global $wpdb;
  47. $table_name = $wpdb->prefix . 'reported_posts';
  48.  
  49. // Delete the table if it exists
  50. $wpdb->query("DROP TABLE IF EXISTS $table_name");
  51. }
  52.  
  53. // Enqueue JavaScript for admin
  54. function enqueue_report_post_js_admin() {
  55. wp_enqueue_script('report-post-js', plugin_dir_url(__FILE__) . 'js/report-post.js', array('jquery'), '1.0', true);
  56.  
  57. // Define and localize variables for JavaScript
  58. $report_post_vars = array(
  59. 'nonce' => wp_create_nonce('report_post_nonce'),
  60. );
  61. wp_localize_script('report-post-js', 'report_post_vars', $report_post_vars);
  62. }
  63. add_action('admin_enqueue_scripts', 'enqueue_report_post_js_admin');
  64.  
  65. // Add a menu item in the admin panel
  66. function create_report_post_menu() {
  67. add_menu_page(
  68. 'Reported Posts',
  69. 'Reported Posts',
  70. 'manage_options',
  71. 'report_post_viewer',
  72. 'report_post_page'
  73. );
  74. }
  75. add_action('admin_menu', 'create_report_post_menu');
  76.  
  77. // Callback function to display the reported post data
  78. function report_post_page() {
  79. // Check user capability (you can modify this based on your needs)
  80. if (!current_user_can('manage_options')) {
  81. wp_die('Access Denied');
  82. }
  83.  
  84. // Include WordPress database access
  85. global $wpdb;
  86.  
  87. // Define the table name
  88. $table_name = $wpdb->prefix . 'reported_posts';
  89.  
  90. // Retrieve the reported post data
  91. $reported_posts = $wpdb->get_results("SELECT * FROM $table_name");
  92.  
  93. // Display the data in a table
  94. echo '<div class="wrap">';
  95. echo '<h2>Reported Posts</h2>';
  96. echo '<table class="wp-list-table widefat fixed">';
  97. echo '<thead>';
  98. echo '<tr>';
  99. echo '<th>ID</th>';
  100. echo '<th>Flag ID</th>';
  101. echo '<th>Video ID</th>';
  102. echo '<th>Flag Message</th>';
  103. echo '<th>Reported At</th>';
  104. echo '<th>Actions</th>';
  105. echo '</tr>';
  106. echo '</thead>';
  107. echo '<tbody>';
  108.  
  109. foreach ($reported_posts as $post) {
  110. echo '<tr>';
  111. echo '<td>' . esc_html($post->id) . '</td>';
  112. echo '<td>' . esc_html($post->flag_id) . '</td>';
  113. echo '<td>' . esc_html($post->video_id) . '</td>';
  114. echo '<td>' . esc_html($post->flag_message) . '</td>';
  115. echo '<td>' . esc_html($post->reported_at) . '</td>';
  116. echo '<td>';
  117. echo '<a href="#" class="delete-post" data-post-id="' . esc_attr($post->id) . '">Delete</a>';
  118. echo ' | ';
  119. echo '<a href="' . get_edit_post_link($post->video_id) . '">Edit</a>';
  120. echo '</td>';
  121. echo '</tr>';
  122. }
  123.  
  124. echo '</tbody>';
  125. echo '</table>';
  126. echo '</div>';
  127. }
  128.  
  129. // AJAX handler for deleting reported data
  130. add_action('wp_ajax_delete_reported_data', 'delete_reported_data_callback');
  131. add_action('wp_ajax_nopriv_delete_reported_data', 'delete_reported_data_callback'); // Handle for non-logged-in users
  132.  
  133. function delete_reported_data_callback() {
  134. check_ajax_referer('report_post_nonce', 'nonce');
  135.  
  136. // Get the post ID from the AJAX request
  137. $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
  138.  
  139. // Check user permissions or any other validation
  140. if (current_user_can('manage_options')) {
  141. global $wpdb;
  142. $table_name = $wpdb->prefix . 'reported_posts';
  143.  
  144. // Delete data from the reported_posts table based on ID
  145. $result = $wpdb->delete($table_name, array('id' => $post_id));
  146.  
  147. if ($result !== false) {
  148. wp_send_json_success(array('message' => 'Data deleted successfully.'));
  149. } else {
  150. wp_send_json_error(array('message' => 'Failed to delete data.'));
  151. }
  152. } else {
  153. wp_send_json_error(array('message' => 'Access denied.'));
  154. }
  155. }
  156.  
  157. // Hook to handle form submission
  158. add_action('init', 'process_report_form');
  159.  
  160. function process_report_form() {
  161. if (isset($_POST['action']) && $_POST['action'] === 'flag') {
  162. // Sanitize and validate form data
  163. $flag_id = sanitize_text_field($_POST['flag_id']);
  164. $video_id = intval($_POST['video_id']);
  165. $flag_message = sanitize_text_field($_POST['flag_message']);
  166.  
  167. // Insert data into the database
  168. global $wpdb;
  169. $table_name = $wpdb->prefix . 'reported_posts';
  170.  
  171. $data = array(
  172. 'flag_id' => $flag_id,
  173. 'video_id' => $video_id,
  174. 'flag_message' => $flag_message,
  175. 'reported_at' => current_time('mysql'),
  176. );
  177.  
  178. $wpdb->insert($table_name, $data);
  179.  
  180. // Display a success message
  181. echo '<div class="success">Thank you! We appreciate your help.</div>';
  182. }
  183. }
  184. ?>
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement