__( 'Affiliate Boxes', 'affistyle' ), 'singular_name' => __( 'Affiliate Box', 'affistyle' ), 'menu_name' => __( 'AffiStyle', 'affistyle' ), 'add_new' => __( 'Add New Box', 'affistyle' ), 'add_new_item' => __( 'Add New Affiliate Box', 'affistyle' ), 'edit_item' => __( 'Edit Affiliate Box', 'affistyle' ), 'new_item' => __( 'New Affiliate Box', 'affistyle' ), 'view_item' => __( 'View Affiliate Box', 'affistyle' ), 'search_items' => __( 'Search Affiliate Boxes', 'affistyle' ), 'not_found' => __( 'No affiliate boxes found', 'affistyle' ), 'not_found_in_trash' => __( 'No affiliate boxes found in Trash', 'affistyle' ), ); $args = array( 'labels' => $labels, 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'menu_icon' => 'dashicons-index-card', 'capability_type' => 'post', 'hierarchical' => false, 'supports' => array( 'title' ), 'show_in_rest' => true, // Enables Gutenberg and REST API support. ); register_post_type( 'affistyle_box', $args ); } /** * Enqueue media uploader and admin styles on AffiStyle editing and list screens. * * @param string $hook Admin page hook. */ public function enqueue_admin_assets( $hook ) { $screen = get_current_screen(); if ( ! $screen || ! in_array( $screen->post_type, array( 'affistyle_box', 'aff_box' ), true ) ) { return; } if ( 'edit.php' === $hook ) { wp_enqueue_style( 'affistyle-admin-css', AFFISTYLE_URL . 'assets/css/admin-style.css', array(), AFFISTYLE_VERSION ); wp_enqueue_style( 'affistyle-block-frontend-css', AFFISTYLE_URL . 'assets/css/affistyle-frontend.css', array(), AFFISTYLE_VERSION ); wp_enqueue_script( 'affistyle-admin-preview-js', AFFISTYLE_URL . 'assets/js/admin-list-preview.js', array( 'jquery' ), AFFISTYLE_VERSION, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'affistyle-admin-preview-js', 'affistyleListPreviewData', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'preview_nonce' => wp_create_nonce( 'affistyle_preview_nonce' ), 'i18n' => array( 'copied' => __( 'Copied!', 'affistyle' ), 'copy' => __( 'Copy', 'affistyle' ), 'copyShortcode' => __( 'Copy Shortcode', 'affistyle' ), 'loading' => __( 'Rendering affiliate card preview...', 'affistyle' ), 'error' => __( 'Failed to load preview.', 'affistyle' ), ), ) ); return; } if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) { return; } wp_enqueue_media(); wp_enqueue_style( 'affistyle-admin-css', AFFISTYLE_URL . 'assets/css/admin-style.css', array(), AFFISTYLE_VERSION ); wp_enqueue_script( 'affistyle-admin-js', AFFISTYLE_URL . 'assets/js/admin-builder.js', array( 'jquery' ), AFFISTYLE_VERSION, array( 'strategy' => 'defer', 'in_footer' => true, ) ); wp_localize_script( 'affistyle-admin-js', 'affistyleAdminData', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'fetch_nonce' => wp_create_nonce( 'affistyle_fetch_product_nonce' ), 'i18n' => array( 'fetching' => __( 'Fetching product details...', 'affistyle' ), 'autoFill' => __( '⚡ Auto-Fill Details', 'affistyle' ), 'success' => __( '✓ Product details populated successfully!', 'affistyle' ), 'enterUrl' => __( 'Please enter a valid Amazon product URL.', 'affistyle' ), 'errorGeneric' => __( 'Failed to fetch product data. Please enter details manually.', 'affistyle' ), ), ) ); } /** * Add Metaboxes to the CPT screen. */ public function add_affistyle_box_metaboxes() { add_meta_box( 'affistyle_card_details', __( 'Affiliate Card Builder Details', 'affistyle' ), array( $this, 'render_metabox_content' ), array( 'affistyle_box', 'aff_box' ), 'normal', 'high' ); } /** * Render the HTML form inside the metabox. * * @param WP_Post $post Current post object. */ public function render_metabox_content( $post ) { // Unified single security nonce token matching save handler. wp_nonce_field( 'affistyle_save_box_meta', 'affistyle_box_nonce' ); // Retrieve existing values for Product 1 base fields. $template_style = get_post_meta( $post->ID, '_affistyle_template_style', true ); $title_1 = get_post_meta( $post->ID, '_affistyle_title_1', true ); $image_1 = get_post_meta( $post->ID, '_affistyle_image_1', true ); if ( empty( $image_1 ) ) { $image_1 = get_post_meta( $post->ID, '_affistyle_image_url', true ); } $description = get_post_meta( $post->ID, '_affistyle_description', true ); $destination = get_post_meta( $post->ID, '_affistyle_destination', true ); if ( empty( $destination ) ) { $destination = get_post_meta( $post->ID, '_affistyle_url_default', true ); } $button_text = get_post_meta( $post->ID, '_affistyle_button_text', true ); $badge_text = get_post_meta( $post->ID, '_affistyle_badge_text', true ); $bg_color = get_post_meta( $post->ID, '_affistyle_bg_color', true ); $btn_color = get_post_meta( $post->ID, '_affistyle_btn_color', true ); $pros = get_post_meta( $post->ID, '_affistyle_pros', true ); $cons = get_post_meta( $post->ID, '_affistyle_cons', true ); $price_1 = get_post_meta( $post->ID, '_affistyle_price_1', true ); if ( empty( $price_1 ) ) { $price_1 = get_post_meta( $post->ID, '_affistyle_price', true ); } $amazon_asin = get_post_meta( $post->ID, '_affistyle_amazon_asin', true ); $rating_score = get_post_meta( $post->ID, '_affistyle_rating_score', true ); $review_count = get_post_meta( $post->ID, '_affistyle_review_count', true ); $show_rating_meta = get_post_meta( $post->ID, '_affistyle_show_rating', true ); $show_rating = ( '' === $show_rating_meta ) ? true : ( '1' === (string) $show_rating_meta ); // Fallbacks. if ( empty( $template_style ) ) { $template_style = 'style-1'; } if ( empty( $button_text ) ) { $button_text = __( 'Claim Deal', 'affistyle' ); } if ( empty( $bg_color ) ) { $bg_color = '#ffffff'; } if ( empty( $btn_color ) ) { $btn_color = '#2563eb'; } // Strictly load local metabox-form.php view file. $view_file = AFFISTYLE_PATH . 'admin/views/metabox-form.php'; if ( file_exists( $view_file ) ) { include $view_file; } elseif ( class_exists( 'AffiStyle_Admin_Controller' ) && method_exists( 'AffiStyle_Admin_Controller', 'render_metabox_view' ) ) { AffiStyle_Admin_Controller::render_metabox_view( $post ); } } /** * Save Metabox Data securely for Product 1 Base Fields. * * @param int $post_id Post ID. */ public function save_affistyle_box_meta( $post_id ) { // 1. Verify Nonce. if ( ! isset( $_POST['affistyle_box_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['affistyle_box_nonce'] ) ), 'affistyle_save_box_meta' ) ) { return; } // 2. Prevent autosaves and revisions from overriding meta. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } // 3. Verify target post type and user edit permissions. $post_type = get_post_type( $post_id ); if ( 'affistyle_box' !== $post_type && 'aff_box' !== $post_type ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // 4. Save Template Style. if ( isset( $_POST['affistyle_template_style'] ) ) { $template_style = sanitize_text_field( wp_unslash( $_POST['affistyle_template_style'] ) ); $available_styles = function_exists( 'affistyle_get_available_styles' ) ? affistyle_get_available_styles() : array(); if ( ! isset( $available_styles[ $template_style ] ) ) { $template_style = 'style-1'; } update_post_meta( $post_id, '_affistyle_template_style', $template_style ); } // 5. Sanitize and save general card fields. if ( isset( $_POST['affistyle_description'] ) ) { update_post_meta( $post_id, '_affistyle_description', sanitize_textarea_field( wp_unslash( $_POST['affistyle_description'] ) ) ); } if ( isset( $_POST['affistyle_button_text'] ) ) { update_post_meta( $post_id, '_affistyle_button_text', sanitize_text_field( wp_unslash( $_POST['affistyle_button_text'] ) ) ); } if ( isset( $_POST['affistyle_badge_text'] ) ) { update_post_meta( $post_id, '_affistyle_badge_text', sanitize_text_field( wp_unslash( $_POST['affistyle_badge_text'] ) ) ); } if ( isset( $_POST['affistyle_bg_color'] ) ) { update_post_meta( $post_id, '_affistyle_bg_color', sanitize_hex_color( wp_unslash( $_POST['affistyle_bg_color'] ) ) ); } if ( isset( $_POST['affistyle_btn_color'] ) ) { update_post_meta( $post_id, '_affistyle_btn_color', sanitize_hex_color( wp_unslash( $_POST['affistyle_btn_color'] ) ) ); } if ( isset( $_POST['affistyle_pros'] ) ) { update_post_meta( $post_id, '_affistyle_pros', sanitize_textarea_field( wp_unslash( $_POST['affistyle_pros'] ) ) ); } if ( isset( $_POST['affistyle_cons'] ) ) { update_post_meta( $post_id, '_affistyle_cons', sanitize_textarea_field( wp_unslash( $_POST['affistyle_cons'] ) ) ); } if ( isset( $_POST['affistyle_rating_score'] ) ) { $score = floatval( wp_unslash( $_POST['affistyle_rating_score'] ) ); $score = max( 0.0, min( 5.0, $score ) ); update_post_meta( $post_id, '_affistyle_rating_score', $score ); } if ( isset( $_POST['affistyle_review_count'] ) ) { update_post_meta( $post_id, '_affistyle_review_count', sanitize_text_field( wp_unslash( $_POST['affistyle_review_count'] ) ) ); } $show_rating = isset( $_POST['affistyle_show_rating'] ) ? '1' : '0'; update_post_meta( $post_id, '_affistyle_show_rating', $show_rating ); // 6. Save Product 1 specific fields. if ( isset( $_POST['affistyle_title_1'] ) ) { update_post_meta( $post_id, '_affistyle_title_1', sanitize_text_field( wp_unslash( $_POST['affistyle_title_1'] ) ) ); } if ( isset( $_POST['affistyle_image_1'] ) ) { $img_url_1 = esc_url_raw( wp_unslash( $_POST['affistyle_image_1'] ) ); update_post_meta( $post_id, '_affistyle_image_1', $img_url_1 ); update_post_meta( $post_id, '_affistyle_image_url', $img_url_1 ); } if ( isset( $_POST['affistyle_destination'] ) ) { $sanitized_dest = esc_url_raw( wp_unslash( $_POST['affistyle_destination'] ) ); update_post_meta( $post_id, '_affistyle_destination', $sanitized_dest ); update_post_meta( $post_id, '_affistyle_url_default', $sanitized_dest ); } elseif ( isset( $_POST['affistyle_url_default'] ) ) { $sanitized_dest = esc_url_raw( wp_unslash( $_POST['affistyle_url_default'] ) ); update_post_meta( $post_id, '_affistyle_destination', $sanitized_dest ); update_post_meta( $post_id, '_affistyle_url_default', $sanitized_dest ); } // 7. Save Product 1 Price and ASIN. if ( isset( $_POST['affistyle_price_1'] ) ) { $sanitized_price = sanitize_text_field( wp_unslash( $_POST['affistyle_price_1'] ) ); update_post_meta( $post_id, '_affistyle_price_1', $sanitized_price ); update_post_meta( $post_id, '_affistyle_price', $sanitized_price ); } if ( isset( $_POST['affistyle_amazon_asin'] ) ) { update_post_meta( $post_id, '_affistyle_amazon_asin', sanitize_text_field( wp_unslash( $_POST['affistyle_amazon_asin'] ) ) ); } } /** * AJAX handler to scrape and fetch product data from an Amazon URL. */ public function ajax_fetch_amazon_product() { check_ajax_referer( 'affistyle_fetch_product_nonce', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'affistyle' ), ) ); } $url = isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : ''; if ( empty( $url ) ) { wp_send_json_error( array( 'message' => __( 'Please enter an Amazon product URL.', 'affistyle' ), ) ); } if ( ! class_exists( 'AffiStyle_Scraper' ) ) { wp_send_json_error( array( 'message' => __( 'Scraper component is not available.', 'affistyle' ), ) ); } $result = AffiStyle_Scraper::fetch_amazon_product( $url ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), ) ); } wp_send_json_success( $result ); } /** * Register custom columns for the AffiStyle admin list table. * * @param array $columns Existing columns. * @return array Modified columns. */ public function register_admin_columns( $columns ) { $new_columns = array(); foreach ( $columns as $key => $title ) { $new_columns[ $key ] = $title; if ( 'title' === $key ) { $new_columns['affistyle_shortcode'] = __( 'Shortcode & Actions', 'affistyle' ); } } if ( ! isset( $new_columns['affistyle_shortcode'] ) ) { $new_columns['affistyle_shortcode'] = __( 'Shortcode & Actions', 'affistyle' ); } return $new_columns; } /** * Render custom column content in the AffiStyle admin list table. * * @param string $column Column key. * @param int $post_id Post ID. */ public function render_admin_columns( $column, $post_id ) { if ( 'affistyle_shortcode' !== $column ) { return; } $shortcode = '[affistyle_box id="' . absint( $post_id ) . '"]'; ?>
post_type, array( 'affistyle_box', 'aff_box' ), true ) ) { return $actions; } $preview_link = sprintf( '%s', absint( $post->ID ), esc_html__( '👁 Quick Preview', 'affistyle' ) ); $new_actions = array(); foreach ( $actions as $key => $action ) { if ( 'trash' === $key || 'delete' === $key ) { $new_actions['affistyle_preview'] = $preview_link; } $new_actions[ $key ] = $action; } if ( ! isset( $new_actions['affistyle_preview'] ) ) { $new_actions['affistyle_preview'] = $preview_link; } return $new_actions; } /** * Render the Quick Preview modal HTML in admin footer on edit.php. */ public function render_admin_preview_modal() { $screen = get_current_screen(); if ( ! $screen || ! in_array( $screen->post_type, array( 'affistyle_box', 'aff_box' ), true ) ) { return; } $view_file = AFFISTYLE_PATH . 'admin/views/admin-preview-modal.php'; if ( file_exists( $view_file ) ) { include $view_file; } } /** * Handle AJAX request for box preview modal. */ public function ajax_preview_box() { check_ajax_referer( 'affistyle_preview_nonce', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to preview boxes.', 'affistyle' ), ) ); } $box_id = isset( $_POST['box_id'] ) ? absint( $_POST['box_id'] ) : 0; if ( ! $box_id ) { wp_send_json_error( array( 'message' => __( 'Invalid box ID.', 'affistyle' ), ) ); } if ( ! class_exists( 'AffiStyle_Database' ) || ! class_exists( 'AffiStyle_Shortcode' ) ) { wp_send_json_error( array( 'message' => __( 'Required plugin components are unavailable.', 'affistyle' ), ) ); } $data = AffiStyle_Database::get_box_data( $box_id ); if ( ! $data ) { wp_send_json_error( array( 'message' => __( 'Box not found.', 'affistyle' ), ) ); } $shortcode_handler = new AffiStyle_Shortcode(); $html = $shortcode_handler->generate_card_html( $data ); $style_raw = ! empty( $data['template_style'] ) ? $data['template_style'] : 'style-1'; $style_name = sprintf( /* translators: %s: style number or slug */ __( 'Style: %s', 'affistyle' ), strtoupper( str_replace( '-', ' ', $style_raw ) ) ); $title = get_the_title( $box_id ); if ( empty( $title ) ) { /* translators: %d: Box ID */ $title = sprintf( __( 'Box #%d', 'affistyle' ), $box_id ); } wp_send_json_success( array( 'id' => $box_id, 'title' => $title, 'style' => $style_name, 'shortcode' => '[affistyle_box id="' . $box_id . '"]', 'edit_url' => get_edit_post_link( $box_id, 'raw' ), 'html' => $html, ) ); } } new AffiStyle_CPT();