hooks(); } return self::$instance; } private function __construct() {} private function hooks(): void { add_action( 'admin_init', array( $this, 'handle_save' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_page_styles' ) ); add_action( 'wp_ajax_arshid6social_delete_ad', array( $this, 'ajax_delete' ) ); add_action( 'wp_ajax_arshid6social_toggle_ad_status', array( $this, 'ajax_toggle_status' ) ); } public function enqueue_page_styles(): void { $screen = get_current_screen(); if ( ! $screen || false === strpos( $screen->id, 'arshid6social-ads' ) ) { return; } wp_add_inline_style( 'arshid6social-admin', '.arshid6social-status-badge{display:inline-block;padding:2px 8px;border-radius:10px;font-size:12px;font-weight:600}.arshid6social-status-active{background:#d4edda;color:#155724}.arshid6social-status-inactive{background:#f8d7da;color:#721c24}.arshid6social-ads-table td{vertical-align:middle}' ); } // ── Save (add / edit) ───────────────────────────────────────────────────── public function handle_save(): void { if ( empty( $_POST['arshid6social_ads_nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['arshid6social_ads_nonce'] ) ), 'arshid6social_save_ad' ) ) { wp_die( esc_html__( 'Nonce check failed.', '6arshid-social-community' ) ); } if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { wp_die( esc_html__( 'Forbidden.', '6arshid-social-community' ) ); } $ad_id = absint( $_POST['ad_id'] ?? 0 ); $title = sanitize_text_field( wp_unslash( $_POST['ad_title'] ?? '' ) ); $ad_type = sanitize_key( $_POST['ad_type'] ?? 'image' ); $file_url = esc_url_raw( wp_unslash( $_POST['ad_file_url'] ?? '' ) ); $click_url = esc_url_raw( wp_unslash( $_POST['ad_click_url'] ?? '' ) ); $js_code = sanitize_textarea_field( wp_unslash( $_POST['ad_js_code'] ?? '' ) ); $placement = sanitize_key( $_POST['ad_placement'] ?? 'both' ); $every_n = max( 1, absint( $_POST['ad_every_n_posts'] ?? 5 ) ); $status = in_array( $_POST['ad_status'] ?? 'active', array( 'active', 'inactive' ), true ) ? sanitize_key( $_POST['ad_status'] ) : 'active'; $start_date = sanitize_text_field( wp_unslash( $_POST['ad_start_date'] ?? '' ) ) ?: null; $end_date = sanitize_text_field( wp_unslash( $_POST['ad_end_date'] ?? '' ) ) ?: null; global $wpdb; $table = $wpdb->prefix . 'arshid6social_ads'; $data = array( 'title' => $title, 'ad_type' => $ad_type, 'file_url' => $file_url, 'click_url' => $click_url, 'js_code' => $js_code, 'placement' => $placement, 'every_n_posts' => $every_n, 'status' => $status, 'start_date' => $start_date, 'end_date' => $end_date, ); if ( $ad_id ) { $wpdb->update( $table, $data, array( 'id' => $ad_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { $data['date_created'] = current_time( 'mysql' ); $wpdb->insert( $table, $data ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } wp_safe_redirect( add_query_arg( array( 'page' => 'arshid6social-ads', 'saved' => '1', ), admin_url( 'admin.php' ) ) ); exit; } // ── AJAX: delete ────────────────────────────────────────────────────────── public function ajax_delete(): void { check_ajax_referer( 'arshid6social_admin_ads_nonce', 'nonce' ); if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { wp_send_json_error( __( 'Forbidden', '6arshid-social-community' ) ); } $ad_id = absint( $_POST['ad_id'] ?? 0 ); global $wpdb; $wpdb->delete( $wpdb->prefix . 'arshid6social_ads', array( 'id' => $ad_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery wp_send_json_success(); } // ── AJAX: toggle status ─────────────────────────────────────────────────── public function ajax_toggle_status(): void { check_ajax_referer( 'arshid6social_admin_ads_nonce', 'nonce' ); if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { wp_send_json_error( __( 'Forbidden', '6arshid-social-community' ) ); } $ad_id = absint( $_POST['ad_id'] ?? 0 ); global $wpdb; $current = $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT status FROM {$wpdb->prefix}arshid6social_ads WHERE id = %d", $ad_id ) ); $new_status = ( 'active' === $current ) ? 'inactive' : 'active'; $wpdb->update( $wpdb->prefix . 'arshid6social_ads', array( 'status' => $new_status ), array( 'id' => $ad_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery wp_send_json_success( array( 'status' => $new_status, 'status_label' => 'active' === $new_status ? __( 'Active', '6arshid-social-community' ) : __( 'Inactive', '6arshid-social-community' ), 'label' => 'active' === $new_status ? __( 'Deactivate', '6arshid-social-community' ) : __( 'Activate', '6arshid-social-community' ), ) ); } // ── Render ──────────────────────────────────────────────────────────────── public function render(): void { if ( ! current_user_can( 'arshid6social_manage_settings' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', '6arshid-social-community' ) ); } $action = sanitize_key( $_GET['action'] ?? 'list' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only admin view routing from a GET nav link; no state change (saves go through nonce-verified handle_save()). $ad_id = absint( $_GET['ad_id'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only: pre-fills the edit view from a GET nav link, no state change. if ( 'add' === $action || 'edit' === $action ) { $this->render_form( $ad_id ); } else { $this->render_list(); } } // ── List view ───────────────────────────────────────────────────────────── private function render_list(): void { global $wpdb; $ads = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT * FROM {$wpdb->prefix}arshid6social_ads ORDER BY id DESC", ARRAY_A ) ?: array(); $saved = ! empty( $_GET['saved'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only success-notice flag set by our own post-save redirect. $add_url = add_query_arg( array( 'page' => 'arshid6social-ads', 'action' => 'add', ), admin_url( 'admin.php' ) ); $nonce = wp_create_nonce( 'arshid6social_admin_ads_nonce' ); $ajax = admin_url( 'admin-ajax.php' ); ?>


'arshid6social-ads', 'action' => 'edit', 'ad_id' => $ad['id'], ), admin_url( 'admin.php' ) ); $ctr = $ad['impressions'] > 0 ? round( ( $ad['clicks'] / $ad['impressions'] ) * 100, 2 ) : 0; $is_active = 'active' === $ad['status']; $toggle_lbl = $is_active ? __( 'Deactivate', '6arshid-social-community' ) : __( 'Activate', '6arshid-social-community' ); $status_lbl = $is_active ? __( 'Active', '6arshid-social-community' ) : __( 'Inactive', '6arshid-social-community' ); ?>
ID CTR
%
0, 'title' => '', 'ad_type' => 'image', 'file_url' => '', 'click_url' => '', 'js_code' => '', 'placement' => 'both', 'every_n_posts' => 5, 'status' => 'active', 'start_date' => '', 'end_date' => '', ); if ( $ad_id ) { $row = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.DirectDatabaseQuery "SELECT * FROM {$wpdb->prefix}arshid6social_ads WHERE id = %d", $ad_id ), ARRAY_A ); if ( $row ) { $ad = array_merge( $ad, $row ); } } $list_url = add_query_arg( array( 'page' => 'arshid6social-ads' ), admin_url( 'admin.php' ) ); $page_title = $ad_id ? __( 'Edit Ad', '6arshid-social-community' ) : __( 'Add New Ad', '6arshid-social-community' ); $ajax_url = admin_url( 'admin-ajax.php' ); $nonce = wp_create_nonce( 'arshid6social_admin_ads_nonce' ); ?>

←

  

':''; }else{statusSpan.textContent='';statusSpan.style.color='';alert((r&&r.data)?r.data:uploadErr);} }) .catch(function(){statusSpan.textContent='';alert(uploadErr);}) .finally(function(){uploadBtn.disabled=false;fileInput.value='';}); }); })(); ENDJS; wp_add_inline_script( 'arshid6social-admin', $js_form ); } }