get_status() || ! $product->is_visible() || post_password_required( $id ) ) {
wp_send_json_error( null, 404 );
}
// Register (and enqueue) what a normal page would; that is the baseline the list page already has.
ob_start();
do_action( 'wp_enqueue_scripts' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core action.
ob_end_clean();
$scripts_before = wp_scripts()->queue;
$styles_before = wp_styles()->queue;
$html = $this->render( $product );
if ( $product->is_type( 'variable' ) ) {
wp_enqueue_script( 'wc-add-to-cart-variation' );
}
// WooCommerce adds its script settings (and the variation template) only when printing the page.
if ( class_exists( '\WC_Frontend_Scripts' ) ) {
ob_start();
\WC_Frontend_Scripts::localize_printed_scripts();
$html .= ob_get_clean();
}
$scripts = array_diff( wp_scripts()->queue, $scripts_before );
if ( false !== strpos( $html, 'wcpa_form_outer' ) && wp_script_is( 'wcpa-front', 'registered' ) ) {
$scripts[] = 'wcpa-front';
}
$styles = array_diff( wp_styles()->queue, $styles_before );
if ( false !== strpos( $html, 'wcpa_form_outer' ) && wp_style_is( 'wcpa-frontend', 'registered' ) ) {
$styles[] = 'wcpa-frontend';
}
// Advanced Product Fields: its files are on every page, but send them anyway in case the list page left them out.
if ( false !== strpos( $html, 'class="wapf"' ) ) {
foreach ( array( 'wapf-frontend', 'wapf-extended', 'wapf-dp', 'wapf-dropzone', 'wapf-layers' ) as $handle ) {
if ( wp_script_is( $handle, 'registered' ) ) {
$scripts[] = $handle;
}
if ( wp_style_is( $handle, 'registered' ) ) {
$styles[] = $handle;
}
}
}
nocache_headers();
wp_send_json_success(
array(
'html' => $html,
'scripts' => self::assets( wp_scripts(), $scripts, true ),
'styles' => self::assets( wp_styles(), $styles, false ),
)
);
}
/**
* Popup markup for one product.
*
* @param \WC_Product $product Product.
* @return string
*/
private function render( $product ) {
global $post;
$post = get_post( $product->get_id() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- WooCommerce templates read the global post.
setup_postdata( $post );
$GLOBALS['product'] = $product; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce's global product for its templates.
$s = Settings::all();
$apf_images = $s['show_gallery'] ? self::apf_images( $product ) : array();
// Recommendations, Buy More Save More and other blocks of the product page stay on the product page.
\CPODKit\Core\Frontend::$in_quick_view = true;
ob_start();
?>
>
' . Button::link( 'd_', Settings::text( 'details_text' ), $product->get_permalink() ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in link().
}
?>
get_image_id() ), array_map( 'intval', $product->get_gallery_image_ids() ) ) );
$ids = array_values( array_unique( $ids ) );
// Advanced Product Fields finds the product images (to swap them by field) by this attribute.
$att = function_exists( 'wapf_pro' );
$layers = self::layers_callback();
echo '';
echo '
';
if ( ! $ids ) {
echo '
' . wc_placeholder_img( 'woocommerce_single' ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
foreach ( $ids as $i => $image_id ) {
$layered = $layers ? self::layered( $layers, $image_id ) : '';
if ( '' !== $layered ) {
echo '
' . $layered . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the add-on's markup.
continue;
}
$attr = array(
'class' => 0 === $i ? 'wp-post-image' : '',
'loading' => 0 === $i ? 'eager' : 'lazy',
);
echo '
' . wp_get_attachment_image( $image_id, 'woocommerce_single', false, $attr ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '
';
if ( count( $ids ) > 1 ) {
echo '
‹ ';
echo '
› ';
echo '
';
foreach ( $ids as $i => $image_id ) {
echo '' . wp_get_attachment_image( $image_id, 'woocommerce_gallery_thumbnail', false, array( 'loading' => 'lazy' ) ) . ' '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '
';
}
// The swap images that are not slides: Advanced Product Fields reads their data from these.
$extra = array_diff( $apf_images, $ids );
if ( $extra ) {
echo '
';
foreach ( $extra as $image_id ) {
$p = wc_get_product_attachment_props( $image_id );
if ( empty( $p['src'] ) ) {
continue;
}
printf(
'
',
esc_attr( $image_id ),
esc_url( isset( $p['gallery_thumbnail_src'] ) ? $p['gallery_thumbnail_src'] : $p['src'] ),
esc_url( $p['src'] ),
esc_attr( isset( $p['src_w'] ) ? $p['src_w'] : '' ),
esc_attr( isset( $p['src_h'] ) ? $p['src_h'] : '' ),
esc_attr( isset( $p['title'] ) ? $p['title'] : '' ),
esc_attr( isset( $p['alt'] ) ? $p['alt'] : '' ),
esc_attr( isset( $p['srcset'] ) ? (string) $p['srcset'] : '' ),
esc_attr( isset( $p['sizes'] ) ? (string) $p['sizes'] : '' ),
esc_url( isset( $p['full_src'] ) ? $p['full_src'] : $p['src'] ),
esc_attr( isset( $p['caption'] ) ? $p['caption'] : '' ),
esc_attr( isset( $p['full_src_w'] ) ? $p['full_src_w'] : '' ),
esc_attr( isset( $p['full_src_h'] ) ? $p['full_src_h'] : '' )
);
}
echo '
';
}
echo '
';
}
/**
* The product image filter of the "layered images" add-on of Advanced Product Fields (wapf-layers), if active.
* Only that callback is used, not the whole filter, so theme gallery filters do not change the popup.
*
* @return callable|null
*/
private static function layers_callback() {
global $wp_filter;
if ( ! class_exists( '\SW_WAPF_LAYERS\Includes\Public_Controller', false ) || empty( $wp_filter['woocommerce_single_product_image_thumbnail_html'] ) ) {
return null;
}
foreach ( $wp_filter['woocommerce_single_product_image_thumbnail_html']->callbacks as $callbacks ) {
foreach ( $callbacks as $cb ) {
if ( is_array( $cb['function'] ) && $cb['function'][0] instanceof \SW_WAPF_LAYERS\Includes\Public_Controller ) {
return $cb['function'];
}
}
}
return null;
}
/**
* Layered image markup for one image ('' when the add-on leaves it alone), followed by the layer
* data the add-on prints in the footer of a normal page (window.wapf_lrs).
*
* @param callable $callback From layers_callback().
* @param int $image_id Image.
* @return string
*/
private static function layered( $callback, $image_id ) {
global $wp_filter;
$before = array();
if ( isset( $wp_filter['wp_footer'] ) ) {
foreach ( $wp_filter['wp_footer']->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $id => $cb ) {
$before[ $priority . ':' . $id ] = true;
}
}
}
try {
$html = (string) call_user_func( $callback, '', $image_id );
} catch ( \Throwable $e ) {
return '';
}
if ( '' === trim( $html ) || ! isset( $wp_filter['wp_footer'] ) ) {
return '';
}
foreach ( $wp_filter['wp_footer']->callbacks as $priority => $callbacks ) {
foreach ( $callbacks as $id => $cb ) {
if ( isset( $before[ $priority . ':' . $id ] ) ) {
continue;
}
ob_start();
call_user_func( $cb['function'] );
$html .= ob_get_clean();
remove_action( 'wp_footer', $cb['function'], $priority );
}
}
return false !== strpos( $html, 'wapf_lrs' ) ? $html : '';
}
/**
* Images the product's Advanced Product Fields groups swap in by option ("gallery images" rules).
* On the product page the theme's gallery holds the ones picked from the product gallery;
* the popup prints them itself so the swap finds them.
*
* @param \WC_Product $product Product.
* @return int[]
*/
private static function apf_images( $product ) {
if ( ! function_exists( 'wapf_pro' ) || ! class_exists( '\SW_WAPF_PRO\Includes\Classes\Field_Groups' ) ) {
return array();
}
$ids = array();
try {
foreach ( (array) \SW_WAPF_PRO\Includes\Classes\Field_Groups::get_field_groups_of_product( $product ) as $group ) {
if ( ! is_object( $group ) || ! method_exists( $group, 'has_gallery_image_rules' ) || ! $group->has_gallery_image_rules() ) {
continue;
}
$rules = $group->get_gallery_image_rules();
foreach ( isset( $rules['rules'] ) ? $rules['rules'] : array() as $rule ) {
$ids[] = isset( $rule['image'] ) ? absint( $rule['image'] ) : 0;
}
}
} catch ( \Throwable $e ) {
return array();
}
return array_values( array_unique( array_filter( $ids ) ) );
}
/**
* Scripts or styles with their dependencies, in load order.
*
* @param \WP_Dependencies $deps wp_scripts() or wp_styles().
* @param string[] $handles Handles the popup needs.
* @param bool $script Scripts (true) or styles.
* @return array[] { handle, src, data, before, after } / { handle, src, media, after }
*/
private static function assets( $deps, $handles, $script ) {
$handles = array_values( array_unique( array_filter( $handles ) ) );
if ( ! $handles ) {
return array();
}
$deps->to_do = array();
$deps->done = array();
$deps->all_deps( $handles );
$out = array();
foreach ( $deps->to_do as $handle ) {
$item = isset( $deps->registered[ $handle ] ) ? $deps->registered[ $handle ] : null;
if ( ! $item ) {
continue;
}
$src = '';
if ( $item->src ) {
$src = $item->src;
if ( ! preg_match( '#^(https?:)?//#', $src ) && ! ( $deps->content_url && 0 === strpos( $src, $deps->content_url ) ) ) {
$src = $deps->base_url . $src;
}
$ver = false === $item->ver ? $deps->default_version : $item->ver;
if ( $ver ) {
$src = add_query_arg( 'ver', $ver, $src );
}
}
if ( $script ) {
$out[] = array(
'handle' => $handle,
'src' => $src,
'data' => (string) $deps->get_data( $handle, 'data' ),
'before' => implode( "\n", array_filter( (array) $deps->get_data( $handle, 'before' ) ) ),
'after' => implode( "\n", array_filter( (array) $deps->get_data( $handle, 'after' ) ) ),
);
} else {
$out[] = array(
'handle' => $handle,
'src' => $src,
'media' => $item->args ? $item->args : 'all',
'after' => implode( "\n", array_filter( (array) $deps->get_data( $handle, 'after' ) ) ),
);
}
}
$deps->to_do = array();
return $out;
}
}