__( 'Variation Gallery', 'addonn-attribute-image-gallery-woocommerce' ), 'target' => 'wcvg_product_data', 'class' => array( 'show_if_variable' ), 'priority' => 65, ); return $tabs; } public function enqueue_assets( $hook ) { global $post; if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) { return; } if ( ! $post || 'product' !== $post->post_type ) { return; } wp_enqueue_media(); wp_enqueue_style( 'wcvg-admin', WCVG_PLUGIN_URL . 'assets/css/wcvg-admin.css', array(), WCVG_VERSION ); wp_enqueue_script( 'wcvg-admin', WCVG_PLUGIN_URL . 'assets/js/wcvg-admin.js', array( 'jquery' ), WCVG_VERSION, true ); wp_localize_script( 'wcvg-admin', 'WCVG_Admin', array( 'chooseImage' => __( 'Choose image', 'addonn-attribute-image-gallery-woocommerce' ), 'useImage' => __( 'Use this image', 'addonn-attribute-image-gallery-woocommerce' ), 'chooseGallery' => __( 'Choose gallery images', 'addonn-attribute-image-gallery-woocommerce' ), 'useGallery' => __( 'Use these images', 'addonn-attribute-image-gallery-woocommerce' ), ) ); } /** * Read this product's saved attribute image/gallery map. * Format: [ term_id => [ 'image_id' => int, 'gallery_ids' => int[] ] ] */ public static function get_product_map( $product_id ) { $saved = get_post_meta( $product_id, self::META_KEY, true ); return is_array( $saved ) ? $saved : array(); } public function render_panel() { global $post; $taxonomy = WCVG_Settings::get_primary_attribute(); ?>
' . wp_kses_post( sprintf( /* translators: %s: link to plugin settings */ __( 'Set a primary attribute first under %s.', 'addonn-attribute-image-gallery-woocommerce' ), '' . esc_html__( 'WooCommerce > Variation Gallery', 'addonn-attribute-image-gallery-woocommerce' ) . '' ) ) . '

'; return; } $terms = wc_get_product_terms( $post->ID, $taxonomy, array( 'fields' => 'all' ) ); if ( empty( $terms ) ) { echo '

' . esc_html__( 'This product is not using any values of the primary attribute yet. Add them under the Attributes tab first.', 'addonn-attribute-image-gallery-woocommerce' ) . '

'; return; } $saved = self::get_product_map( $post->ID ); wp_nonce_field( 'wcvg_save_product_fields', 'wcvg_product_nonce' ); ?>

term_id ] ) ? $saved[ $term->term_id ] : array(); $image_id = isset( $entry['image_id'] ) ? (int) $entry['image_id'] : 0; $gallery_ids = isset( $entry['gallery_ids'] ) ? array_filter( array_map( 'intval', (array) $entry['gallery_ids'] ) ) : array(); $image_url = $image_id ? wp_get_attachment_image_url( $image_id, 'thumbnail' ) : ''; ?>

name ); ?>

$image_id ) { $term_id = absint( $term_id ); $image_id = absint( $image_id ); $gallery_raw = isset( $_POST['wcvg_term_gallery'][ $term_id ] ) ? sanitize_text_field( wp_unslash( $_POST['wcvg_term_gallery'][ $term_id ] ) ) : ''; $gallery_ids = array_filter( array_map( 'absint', explode( ',', $gallery_raw ) ) ); if ( $term_id && ( $image_id || ! empty( $gallery_ids ) ) ) { $data[ $term_id ] = array( 'image_id' => $image_id, 'gallery_ids' => array_values( $gallery_ids ), ); } } } if ( ! empty( $data ) ) { update_post_meta( $product_id, self::META_KEY, $data ); } else { delete_post_meta( $product_id, self::META_KEY ); } } }