ID)) ? $post->ID : 0; $affirm_featured = $post_id ? get_post_meta($post_id, 'affirm_featured_image_id', true) : ''; $post_type = $post_id ? get_post_type($post_id) : ''; wp_localize_script('affirm-media-vault', 'affirmVault', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('affirm_admin_nonce'), 'postId' => $post_id, 'postType' => $post_type, 'backendUrl' => affirm_get_backend_url(), 'featuredImageId' => $affirm_featured, )); } /** * AJAX: Set a vault image as the featured image for a post */ public function ajax_set_featured_image() { check_ajax_referer('affirm_admin_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; $image_id = isset($_POST['image_id']) ? sanitize_text_field(wp_unslash($_POST['image_id'])) : ''; $thumb_url = isset($_POST['thumbnail_url']) ? esc_url_raw(wp_unslash($_POST['thumbnail_url'])) : ''; $filename = isset($_POST['filename']) ? sanitize_text_field(wp_unslash($_POST['filename'])) : ''; if (!$post_id || !$image_id) { wp_send_json_error(array('message' => 'Post ID and Image ID required')); } // Store vault image ID in post meta (no WP attachment created) update_post_meta($post_id, 'affirm_featured_image_id', $image_id); update_post_meta($post_id, 'affirm_featured_thumbnail_url', $thumb_url); update_post_meta($post_id, 'affirm_featured_filename', $filename); // Remove any standard WP featured image delete_post_thumbnail($post_id); wp_send_json_success(array( 'imageId' => $image_id, 'thumbnailUrl' => $thumb_url, 'filename' => $filename, )); } /** * AJAX: Remove the vault featured image from a post */ public function ajax_remove_featured_image() { check_ajax_referer('affirm_admin_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; if (!$post_id) { wp_send_json_error(array('message' => 'Post ID required')); } delete_post_meta($post_id, 'affirm_featured_image_id'); delete_post_meta($post_id, 'affirm_featured_thumbnail_url'); delete_post_meta($post_id, 'affirm_featured_filename'); wp_send_json_success(); } /** * AJAX: Get the current vault featured image for a post */ public function ajax_get_featured_image() { check_ajax_referer('affirm_admin_nonce', 'nonce'); $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; if (!$post_id) { wp_send_json_error(array('message' => 'Post ID required')); } $image_id = get_post_meta($post_id, 'affirm_featured_image_id', true); if (!$image_id) { wp_send_json_error(array('message' => 'No vault featured image')); } wp_send_json_success(array( 'imageId' => $image_id, 'thumbnailUrl' => get_post_meta($post_id, 'affirm_featured_thumbnail_url', true), 'filename' => get_post_meta($post_id, 'affirm_featured_filename', true), )); } /** * AJAX: Save vault image IDs for WooCommerce product gallery */ public function ajax_save_product_gallery() { check_ajax_referer('affirm_admin_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; $image_ids = isset($_POST['image_ids']) ? sanitize_text_field(wp_unslash($_POST['image_ids'])) : '[]'; if (!$post_id) { wp_send_json_error(array('message' => 'Post ID required')); } $ids = json_decode(stripslashes($image_ids), true); if (!is_array($ids)) $ids = array(); // Sanitize each ID $ids = array_map('sanitize_text_field', $ids); update_post_meta($post_id, '_affirm_product_gallery', $ids); wp_send_json_success(array('saved' => count($ids))); } /** * AJAX: Get saved vault gallery image IDs for a product */ public function ajax_get_product_gallery() { check_ajax_referer('affirm_admin_nonce', 'nonce'); $post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0; if (!$post_id) { wp_send_json_error(array('message' => 'Post ID required')); } $ids = get_post_meta($post_id, '_affirm_product_gallery', true); if (!is_array($ids)) $ids = array(); wp_send_json_success(array('image_ids' => $ids)); } /** * AJAX: Toggle image delivery on/off */ public function ajax_toggle_image_delivery() { check_ajax_referer('affirm_admin_nonce', 'nonce'); if (!current_user_can('edit_posts')) { wp_send_json_error(array('message' => 'Unauthorized')); } $image_id = isset($_POST['image_id']) ? sanitize_text_field(wp_unslash($_POST['image_id'])) : ''; $enabled = isset($_POST['enabled']) ? filter_var(wp_unslash($_POST['enabled']), FILTER_VALIDATE_BOOLEAN) : false; if (!$image_id) { wp_send_json_error(array('message' => 'Image ID required')); } $result = Affirm_Api::toggle_image_delivery($image_id, $enabled); if (is_wp_error($result)) { wp_send_json_error(array('message' => $result->get_error_message())); } wp_send_json_success(array( 'message' => $enabled ? 'Delivery enabled' : 'Delivery disabled', 'enabled' => $enabled )); } /** * Override the Featured Image metabox to show vault image if set */ public function override_featured_image_metabox($content, $post_id, $thumbnail_id) { $affirm_id = get_post_meta($post_id, 'affirm_featured_image_id', true); if (!$affirm_id) return $content; $thumb_url = get_post_meta($post_id, 'affirm_featured_thumbnail_url', true); $filename = get_post_meta($post_id, 'affirm_featured_filename', true); $html = '
'; $html .= ' aFFirmFirst Protected'; $html .= '
'; $html .= '' . esc_html($filename) . '
'; $html .= '