Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Report Post Plugin
- Description: This plugin adds a report post feature and a view reported posts admin page.
- Version: 1.0
- Author: Anonim
- */
- // Create the reported_posts table
- function create_reported_posts_table() {
- global $wpdb;
- $table_name = $wpdb->prefix . 'reported_posts';
- $charset_collate = $wpdb->get_charset_collate();
- $sql = "CREATE TABLE $table_name (
- id mediumint(9) NOT NULL AUTO_INCREMENT,
- flag_id varchar(255) NOT NULL,
- video_id bigint(20) NOT NULL,
- flag_message text,
- reported_at datetime NOT NULL,
- PRIMARY KEY (id)
- ) $charset_collate;";
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
- dbDelta($sql);
- }
- register_activation_hook(__FILE__, 'create_reported_posts_table');
- // Hook to deactivate the plugin and perform cleanup
- register_deactivation_hook(__FILE__, 'deactivate_plugin_function');
- function deactivate_plugin_function() {
- global $wpdb;
- $table_name = $wpdb->prefix . 'reported_posts';
- // Drop the table if it exists
- $wpdb->query("DROP TABLE IF EXISTS $table_name");
- }
- // Register uninstall hook
- register_uninstall_hook(__FILE__, 'delete_reported_posts_table');
- // Function to delete the reported_posts table
- function delete_reported_posts_table() {
- global $wpdb;
- $table_name = $wpdb->prefix . 'reported_posts';
- // Delete the table if it exists
- $wpdb->query("DROP TABLE IF EXISTS $table_name");
- }
- // Enqueue JavaScript for admin
- function enqueue_report_post_js_admin() {
- wp_enqueue_script('report-post-js', plugin_dir_url(__FILE__) . 'js/report-post.js', array('jquery'), '1.0', true);
- // Define and localize variables for JavaScript
- $report_post_vars = array(
- 'nonce' => wp_create_nonce('report_post_nonce'),
- );
- wp_localize_script('report-post-js', 'report_post_vars', $report_post_vars);
- }
- add_action('admin_enqueue_scripts', 'enqueue_report_post_js_admin');
- // Add a menu item in the admin panel
- function create_report_post_menu() {
- add_menu_page(
- 'Reported Posts',
- 'Reported Posts',
- 'manage_options',
- 'report_post_viewer',
- 'report_post_page'
- );
- }
- add_action('admin_menu', 'create_report_post_menu');
- // Callback function to display the reported post data
- function report_post_page() {
- // Check user capability (you can modify this based on your needs)
- if (!current_user_can('manage_options')) {
- wp_die('Access Denied');
- }
- // Include WordPress database access
- global $wpdb;
- // Define the table name
- $table_name = $wpdb->prefix . 'reported_posts';
- // Retrieve the reported post data
- $reported_posts = $wpdb->get_results("SELECT * FROM $table_name");
- // Display the data in a table
- echo '<div class="wrap">';
- echo '<h2>Reported Posts</h2>';
- echo '<table class="wp-list-table widefat fixed">';
- echo '<thead>';
- echo '<tr>';
- echo '<th>ID</th>';
- echo '<th>Flag ID</th>';
- echo '<th>Video ID</th>';
- echo '<th>Flag Message</th>';
- echo '<th>Reported At</th>';
- echo '<th>Actions</th>';
- echo '</tr>';
- echo '</thead>';
- echo '<tbody>';
- foreach ($reported_posts as $post) {
- echo '<tr>';
- echo '<td>' . esc_html($post->id) . '</td>';
- echo '<td>' . esc_html($post->flag_id) . '</td>';
- echo '<td>' . esc_html($post->video_id) . '</td>';
- echo '<td>' . esc_html($post->flag_message) . '</td>';
- echo '<td>' . esc_html($post->reported_at) . '</td>';
- echo '<td>';
- echo '<a href="#" class="delete-post" data-post-id="' . esc_attr($post->id) . '">Delete</a>';
- echo ' | ';
- echo '<a href="' . get_edit_post_link($post->video_id) . '">Edit</a>';
- echo '</td>';
- echo '</tr>';
- }
- echo '</tbody>';
- echo '</table>';
- echo '</div>';
- }
- // AJAX handler for deleting reported data
- add_action('wp_ajax_delete_reported_data', 'delete_reported_data_callback');
- add_action('wp_ajax_nopriv_delete_reported_data', 'delete_reported_data_callback'); // Handle for non-logged-in users
- function delete_reported_data_callback() {
- check_ajax_referer('report_post_nonce', 'nonce');
- // Get the post ID from the AJAX request
- $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
- // Check user permissions or any other validation
- if (current_user_can('manage_options')) {
- global $wpdb;
- $table_name = $wpdb->prefix . 'reported_posts';
- // Delete data from the reported_posts table based on ID
- $result = $wpdb->delete($table_name, array('id' => $post_id));
- if ($result !== false) {
- wp_send_json_success(array('message' => 'Data deleted successfully.'));
- } else {
- wp_send_json_error(array('message' => 'Failed to delete data.'));
- }
- } else {
- wp_send_json_error(array('message' => 'Access denied.'));
- }
- }
- // Hook to handle form submission
- add_action('init', 'process_report_form');
- function process_report_form() {
- if (isset($_POST['action']) && $_POST['action'] === 'flag') {
- // Sanitize and validate form data
- $flag_id = sanitize_text_field($_POST['flag_id']);
- $video_id = intval($_POST['video_id']);
- $flag_message = sanitize_text_field($_POST['flag_message']);
- // Insert data into the database
- global $wpdb;
- $table_name = $wpdb->prefix . 'reported_posts';
- $data = array(
- 'flag_id' => $flag_id,
- 'video_id' => $video_id,
- 'flag_message' => $flag_message,
- 'reported_at' => current_time('mysql'),
- );
- $wpdb->insert($table_name, $data);
- // Display a success message
- echo '<div class="success">Thank you! We appreciate your help.</div>';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement