prefix . 'cookieadmin_do_not_sell'; $current_tab = !empty($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'requests'; if(!empty($_POST['cookieadmin_dns_save_settings'])){ check_admin_referer('cookieadmin_dns_settings', 'cookieadmin_pro_security'); // Process from data $dns_settings = !empty($_POST['cookieadmin_dns']) ? map_deep(wp_unslash($_POST['cookieadmin_dns']), 'sanitize_text_field') : []; // create Do not sell table, when enabling the feature. if(!\CookieAdminPro\Admin::cookieadmin_pro_table_exists($table_name)){ self::create_do_not_sell_table($table_name); } if(!empty($settings['custom_page'])){ $dns_settings['custom_page'] = $settings['custom_page']; } $dns_settings['exp_content'] = !empty($_POST['cookieadmin_dns_exp_content']) ? wp_kses_post($_POST['cookieadmin_dns_exp_content']) : ''; $dns_settings['selected_page'] = !empty($_POST['cookieadmin_dns_form_page']) ? (int) sanitize_text_field(wp_unslash($_POST['cookieadmin_dns_form_page'])) : 0; // Add the shorcode to the selected page at the end of the page if(($dns_settings['selected_page'] > 0) && ($dns_settings['selected_page'] !== (int) $settings['selected_page'])){ $selected_page = get_post($dns_settings['selected_page']); // Add the shortcode on the selected page if(!empty($selected_page) && !empty($selected_page->ID)){ if(strpos($selected_page->post_content, '[cookieadmin_opt_out_consent]') === false){ $result = wp_update_post(array( 'ID' => $selected_page->ID, 'post_content' => $selected_page->post_content . '[cookieadmin_opt_out_consent]' )); if($result instanceof WP_Error){ $cookieadmin_error = __('Error updating the shorcode on selected page', 'cookieadmin'); } } } } // Remove the existing shortcode from the prev saved page $prev_shortcode_page = !empty($settings['selected_page']) ? get_post((int) $settings['selected_page']) : 0; if(!empty($prev_shortcode_page) && ($dns_settings['selected_page'] !== (int) $settings['selected_page'])){ if(!empty($prev_shortcode_page->post_content) && (strpos($prev_shortcode_page->post_content, '[cookieadmin_opt_out_consent]') !== false)){ $result = wp_update_post(array( 'ID' => $prev_shortcode_page->ID, 'post_content' => str_replace('[cookieadmin_opt_out_consent]', '', $prev_shortcode_page->post_content) )); if($result instanceof WP_Error){ $cookieadmin_error = __('Error updating the shorcode on selected page', 'cookieadmin'); } } } $dns_settings; if(empty($cookieadmin_error)){ update_option('cookieadmin_do_not_sell', $dns_settings); $cookieadmin_msg = __('Settings saved successfully', 'cookieadmin'); } } // handle bulk actions if(!empty($_POST['cookieadmin_dns_bulk_action']) && !empty($_POST['cookieadmin_dns_ids'])){ check_admin_referer('cookieadmin_pro_admin_nonce', 'cookieadmin_pro_security'); $ids = map_deep(wp_unslash($_POST['cookieadmin_dns_ids']), 'intval'); // Mark proccess as Processed if($_POST['cookieadmin_dns_bulk_action'] === 'processed'){ foreach($ids as $id){ $wpdb->update( $table_name, array('status' => 'Processed', 'processed_at' => time()), array('id' => $id), array('%s', '%d'), array('%d') ); } $cookieadmin_msg = __('Requests marked as processed', 'cookieadmin'); } } \CookieAdmin\Admin::header_theme(__('Do Not Sell', 'cookieadmin')); echo '
'; if($current_tab === 'settings'){ self::render_settings_tab(); }else{ self::render_requests_tab($table_name); } \CookieAdmin\Admin::footer_theme(); } static function generate_do_not_sell_page(){ $settings = get_option('cookieadmin_do_not_sell', []); check_ajax_referer('cookieadmin_pro_admin_js_nonce', 'cookieadmin_pro_security'); if(!current_user_can('administrator')){ wp_send_json_error(__('Invalid user', 'cookieadmin')); } // check if the page is alredy generated if(!empty($settings['custom_page'])){ $custom_page = get_post((int) $settings['custom_page']); if(!empty($custom_page && $custom_page->ID)){ wp_send_json_error(__('Page already generated', 'cookieadmin')); } } $page_content = '[cookieadmin_opt_out_consent]'; $page_title = __('Do Not Sell My Personal Information', 'cookieadmin'); $post_args = array( 'post_type' => 'page', 'post_title' => $page_title, 'post_content' => $page_content, 'post_status' => 'publish', ); $page_id = wp_insert_post($post_args, true); if(!($page_id instanceof WP_Error)){ $permalink = get_permalink($page_id); if(!empty($permalink)){ $settings['selected_page'] = $page_id; $settings['custom_page'] = $page_id; $settings['enabled'] = '1'; update_option('cookieadmin_do_not_sell', $settings); wp_send_json_success(); } } wp_send_json_error(__('Something went wrong', 'cookieadmin')); } // Export Consent Logs from the Database static function export_dns_requests() { global $wpdb; $cookieadmin_export_type = !empty($_REQUEST['cookieadmin_export_type']) ? sanitize_text_field(wp_unslash($_REQUEST['cookieadmin_export_type'])) : ''; if(!empty($cookieadmin_export_type)){ if($cookieadmin_export_type == 'dns_requests'){ $table_name = esc_sql($wpdb->prefix . 'cookieadmin_do_not_sell'); //First will check if the table in the database exists or not? if(!\CookieAdminPro\Admin::cookieadmin_pro_table_exists($table_name)){ echo -1; echo esc_html__('Configure Do Not Sell settings', 'cookieadmin'); wp_die(); } $requests = $wpdb->get_results("SELECT * from $table_name WHERE status = 'processed'", ARRAY_N); } } if(empty($requests)){ echo -1; echo esc_html__('No data to export', 'cookieadmin'); wp_die(); } // Export to CSV header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=cookieadmin-do-not-sell-requests.csv'); // Create file handle for output $output = fopen('php://output', 'w'); // Add column headers fputcsv($output, array('ID', 'Email', 'FirstName', 'LastName', 'Phone', 'ZIP', 'Status', 'Received', 'Processed')); // Add data rows foreach($requests as $request){ fputcsv($output, $request); } fclose($output); wp_die(); } static function do_not_sell_requests_pagination(){ global $wpdb; if($_POST && count($_POST) > 0){ $nonce_slug = (wp_doing_ajax() ? 'cookieadmin_pro_admin_js_nonce' : 'cookieadmin_pro_admin_nonce'); check_admin_referer($nonce_slug, 'cookieadmin_pro_security'); } if(!current_user_can('administrator')){ wp_send_json_error(array('message' => __('Sorry, but you do not have permissions to perform this action', 'cookieadmin'))); } $num_items = 0; $current_page = isset($_POST['current_page']) ? intval($_POST['current_page']) : 1; $table_name = esc_sql($wpdb->prefix . 'cookieadmin_do_not_sell'); if (!\CookieAdminPro\Admin::cookieadmin_pro_table_exists($table_name)) { // wp_send_json_error(['message' => 'Table does not exist']); return array(); } // Get total number of logs $total_dns_req = (int) $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); $req_per_page = 20; // Calculate max pages $max_page = ceil($total_dns_req / $req_per_page); $max_page = max(1, $max_page); // Ensure current page is within valid range if ($current_page > $max_page) { $current_page = $max_page; } elseif ($current_page < 1) { $current_page = 1; } // Calculate pagination offset $offset = ($current_page - 1) * $req_per_page; // Fetch paginated requets $dns_request = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name ORDER BY id DESC LIMIT %d OFFSET %d", $req_per_page, $offset ), ARRAY_A ); $num_items = (!empty($dns_request) && is_array($dns_request)) ? count($dns_request) : 1; $min_items = $offset + 1; $max_items = $min_items + ($num_items - 1); // Adding the human diff format of the processed_at timestamp foreach($dns_request as $key => $request){ if(!empty($request['processed_at'])){ $dns_request[$key]['processed_at_human'] = human_time_diff($request['processed_at']); } if(!empty($request['created_at'])){ $dns_request[$key]['created_at'] = wp_date('Y-m-d H:i:s', $request['created_at']); } } $return = [ 'logs' => $dns_request, 'total_logs' => $total_dns_req, 'logs_per_page' => $req_per_page, 'current_page' => $current_page, 'total_pages' => $max_page, 'min_items' => $min_items, 'max_items' => $max_items ]; // Return logs as JSON response if (defined('DOING_AJAX') && DOING_AJAX) { wp_send_json_success($return); } // Return paginated data return $return; } static function create_do_not_sell_table($do_not_sell_table){ global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $db_path = !defined('SITEPAD') ? ABSPATH . 'wp-admin/includes/upgrade.php' : ABSPATH . 'site-admin/includes/upgrade.php'; require_once($db_path); $sql = 'CREATE TABLE ' . $do_not_sell_table . ' ( id INT NOT NULL AUTO_INCREMENT, user_email VARCHAR(100) NOT NULL, first_name VARCHAR(100) DEFAULT NULL, last_name VARCHAR(100) DEFAULT NULL, phone VARCHAR(30) DEFAULT NULL, zip VARCHAR(10) DEFAULT NULL, status VARCHAR(20) NOT NULL DEFAULT \'pending\', created_at BIGINT NOT NULL, processed_at BIGINT DEFAULT NULL, PRIMARY KEY (id) ) ' . $charset_collate . ';'; dbDelta($sql); } static function render_requests_tab($table_name){ global $cookieadmin_msg; $log_data = self::do_not_sell_requests_pagination(); $consent_logs = (!empty($log_data['logs']) ? $log_data['logs'] : array()); echo ''; } static function render_settings_tab(){ $settings = get_option('cookieadmin_do_not_sell', []); if(empty($settings['exp_content'])){ $settings['exp_content'] = 'Users have the right to request that their personal information not be sold or shared with third parties for advertising or marketing purposes, and businesses must honor this choice and stop such data use upon request.'; } $published_pages = get_pages(); $custom_page = !empty($settings['custom_page']) ? get_post((int) $settings['custom_page']) : ''; $preview_page_url = !empty($settings['selected_page']) ? get_permalink((int) $settings['selected_page']) : ''; echo ''; } }