'ocean_after_single_product_title', 'after_price' => 'ocean_after_single_product_price', 'after_excerpt' => 'ocean_after_single_product_excerpt', 'after_meta' => 'ocean_after_single_product_meta', ); } /** The boxes: 1 and 2, each with its own settings. */ public static function boxes() { return array( 1, 2 ); } /** * A box's settings, with the same keys for both boxes (feat_*). * * @param int $box Box number. * @param array|null $s All settings. * @return array */ public static function box_settings( $box, $s = null ) { $s = $s ? $s : H23PF_Settings::get(); if ( 2 !== (int) $box ) { return $s; } $out = $s; foreach ( $s as $key => $value ) { if ( 0 === strpos( $key, 'feat2_' ) ) { $out[ 'feat_' . substr( $key, 6 ) ] = $value; } } return $out; } /** Hooks each box at its chosen position (once, at the start of the summary). */ public static function maybe_render() { $ocean = self::oceanwp_hooks(); $pos = H23PF_Settings::feature_positions(); foreach ( self::boxes() as $box ) { $s = self::box_settings( $box ); $position = $s['feat_position']; $callback = array( __CLASS__, 'render_box_' . $box ); if ( empty( $s['feat_enabled'] ) || 'shortcode' === $position ) { continue; } if ( 'before_cart' === $position ) { add_action( 'woocommerce_before_add_to_cart_form', $callback ); } elseif ( 'after_cart' === $position ) { add_action( 'woocommerce_after_add_to_cart_form', $callback ); } elseif ( 'oceanwp' === get_template() && isset( $ocean[ $position ] ) ) { add_action( $ocean[ $position ], $callback ); } else { add_action( 'woocommerce_single_product_summary', $callback, isset( $pos[ $position ] ) ? $pos[ $position ][1] : 21 ); } } } public static function render_box_1() { self::render_current( 1 ); } public static function render_box_2() { self::render_current( 2 ); } /** Prints a box for the current product, once per page. */ public static function render_current( $box = 1 ) { global $product; static $done = array(); if ( empty( $done[ $box ] ) && $product instanceof WC_Product ) { $done[ $box ] = true; self::output( self::for_product( $product->get_id(), $box ), self::box_settings( $box ), $box ); } } /** * [h23pf_features], [h23pf_features box="2"] or [h23pf_features id="123" box="2"]. * * @param array|string $atts Shortcode attributes. * @return string */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0, 'box' => 1, ), $atts, 'h23pf_features' ); $id = absint( $atts['id'] ); $box = 2 === absint( $atts['box'] ) ? 2 : 1; if ( ! $id ) { global $product; $id = $product instanceof WC_Product ? $product->get_id() : ( is_singular( 'product' ) ? get_the_ID() : 0 ); } wp_enqueue_style( 'h23pf-features' ); $items = $id ? self::for_product( $id, $box ) : self::in_box( self::enabled_items( H23PF_Settings::get( 'features' ) ), $box ); return self::kses( self::html( $items, self::box_settings( $box ), $box ) ); } public static function enabled_items( $items ) { return array_values( array_filter( (array) $items, static function ( $i ) { return ! empty( $i['enabled'] ); } ) ); } /** The rows of one box (rows saved before boxes existed belong to box 1). */ public static function in_box( $items, $box ) { return array_values( array_filter( (array) $items, static function ( $i ) use ( $box ) { return ( isset( $i['box'] ) && 2 === (int) $i['box'] ? 2 : 1 ) === (int) $box; } ) ); } /** The features of a product in one box: the matching general ones and its own. */ public static function for_product( $product_id, $box = 1 ) { $product_id = (int) $product_id; $out = array(); $hide = 'yes' === get_post_meta( $product_id, '_h23pf_hide_features', true ); if ( ! $hide ) { $cats = wc_get_product_term_ids( $product_id, 'product_cat' ); foreach ( self::enabled_items( H23PF_Settings::get( 'features' ) ) as $item ) { if ( empty( $item['cats'] ) || array_intersect( $cats, $item['cats'] ) ) { $out[] = $item; } } } $own = get_post_meta( $product_id, '_h23pf_features', true ); if ( is_array( $own ) && $own ) { $own = self::enabled_items( $own ); $out = 'before' === get_post_meta( $product_id, '_h23pf_features_order', true ) ? array_merge( $own, $out ) : array_merge( $out, $own ); } return apply_filters( 'h23pf_product_features', self::in_box( $out, $box ), $product_id, $box ); } /** HTML allowed in the box. */ public static function allowed_box_html() { $allowed = H23PF_Settings::allowed_html(); $allowed['div'] = array( 'class' => true, 'style' => true, ); $allowed['ul'] = array( 'class' => true ); $allowed['li'] = array( 'class' => true, 'style' => true, ); $allowed['p'] = array( 'class' => true ); $allowed['img'] = array( 'src' => true, 'alt' => true, 'loading' => true, 'decoding' => true, 'width' => true, 'height' => true, ); return $allowed; } /** CSS custom properties used in the box's style attributes, allowed through wp_kses(). */ public static function safe_style( $styles ) { return array_merge( $styles, array( '--h23pf-icon', '--h23pf-fs', '--h23pf-gap', '--h23pf-cols', '--h23pf-radius', '--h23pf-pad', '--h23pf-mt', '--h23pf-mb', '--h23pf-bg', '--h23pf-border', '--h23pf-color', '--s', '--c' ) ); } /** wp_kses() that keeps the plugin's CSS variables. */ public static function kses( $html, $allowed = null ) { add_filter( 'safe_style_css', array( __CLASS__, 'safe_style' ) ); $out = wp_kses( $html, null === $allowed ? self::allowed_box_html() : $allowed ); remove_filter( 'safe_style_css', array( __CLASS__, 'safe_style' ) ); return $out; } /** Echoes the box. */ public static function output( $items, $s = null, $box = 1 ) { echo self::kses( self::html( $items, $s, $box ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped by wp_kses() in self::kses(). } /** The box's HTML (every value escaped). */ public static function html( $items, $s = null, $box = 1 ) { if ( ! $items ) { return ''; } $s = $s ? $s : H23PF_Settings::get(); $vars = array( '--h23pf-icon' => (int) $s['feat_icon_size'] . 'px', '--h23pf-fs' => (int) $s['feat_text_size'] . 'px', '--h23pf-gap' => (int) $s['feat_gap'] . 'px', '--h23pf-cols' => (int) $s['feat_columns'], '--h23pf-radius' => (int) $s['feat_radius'] . 'px', '--h23pf-pad' => (int) $s['feat_padding'] . 'px', '--h23pf-mt' => (int) ( isset( $s['feat_space_top'] ) ? $s['feat_space_top'] : 0 ) . 'px', '--h23pf-mb' => (int) ( isset( $s['feat_space_bottom'] ) ? $s['feat_space_bottom'] : 20 ) . 'px', ); foreach ( array( 'feat_bg' => '--h23pf-bg', 'feat_border' => '--h23pf-border', 'feat_text_color' => '--h23pf-color' ) as $key => $var ) { $c = sanitize_hex_color( $s[ $key ] ); if ( $c ) { $vars[ $var ] = $c; } } $style = ''; foreach ( $vars as $k => $v ) { $style .= $k . ':' . $v . ';'; } $classes = array( 'h23pf-features', 'h23pf-features--box' . ( 2 === (int) $box ? 2 : 1 ), 'h23pf-va-' . ( 'top' === $s['feat_valign'] ? 'top' : 'middle' ) ); if ( ! empty( $s['feat_boxed'] ) ) { $classes[] = 'is-boxed'; } $html = '
' . esc_html( $s['feat_title'] ) . '
'; } $html .= '