$row ) {
if ( empty( $row['enabled'] ) ) {
continue;
}
$target = Store::target( $row );
if ( ! $target ) {
continue;
}
$row['_index'] = $index;
$groups[ $target[0] ][ (int) $target[1] ][] = $row;
}
return $groups;
}
public function attach() {
foreach ( self::groups() as $hook => $by_priority ) {
foreach ( $by_priority as $priority => $rows ) {
usort(
$rows,
static function ( $a, $b ) {
return array( (int) $a['priority'], $a['_index'] ) <=> array( (int) $b['priority'], $b['_index'] );
}
);
add_action(
$hook,
static function () use ( $rows ) {
foreach ( $rows as $row ) {
echo wp_kses( self::render( $row ), cpod_allowed_html() );
}
},
$priority,
0
);
}
}
}
/** Small stylesheet for the "computers only / phones only" option. */
public function device_css() {
$needed = false;
foreach ( Store::all() as $row ) {
if ( ! empty( $row['enabled'] ) && 'all' !== $row['device'] ) {
$needed = true;
break;
}
}
if ( ! $needed ) {
return;
}
$max = self::BREAKPOINT - 1;
wp_register_style( 'cpod-hook-blocks', false, array(), CPOD_VERSION );
wp_enqueue_style( 'cpod-hook-blocks' );
wp_add_inline_style(
'cpod-hook-blocks',
'@media (max-width:' . $max . 'px){.cpod-hb--desktop{display:none!important}}@media (min-width:' . self::BREAKPOINT . 'px){.cpod-hb--mobile{display:none!important}}'
);
}
/**
* HTML of one row on the current page, '' when it does not apply.
*
* @param array $row Row.
* @return string
*/
public static function render( $row ) {
if ( ! self::matches( $row ) ) {
return '';
}
$html = cpod_strip_code_tags( do_shortcode( Store::source( $row ) ) );
if ( '' === trim( $html ) ) {
return '';
}
$classes = 'cpod-hb cpod-hb-' . sanitize_html_class( $row['id'] );
if ( 'all' !== $row['device'] ) {
$classes .= ' cpod-hb--' . $row['device'];
}
$html = '
' . $html . '
';
return (string) apply_filters( 'cpod_hook_block_html', $html, $row );
}
/** Whether the row's page condition matches the current request. */
public static function matches( $row ) {
$is_product = function_exists( 'is_product' ) && is_product();
switch ( $row['page'] ) {
case 'everywhere':
$match = true;
break;
case 'all_products':
$match = $is_product;
break;
case 'products':
$match = $is_product && in_array( (int) get_queried_object_id(), array_map( 'intval', $row['products'] ), true );
break;
case 'categories':
$match = self::term_match( $row['categories'], 'product_cat', $is_product );
break;
case 'tags':
$match = self::term_match( $row['tags'], 'product_tag', $is_product );
break;
case 'shop':
$match = function_exists( 'is_shop' ) && is_shop();
break;
case 'cart':
$match = function_exists( 'is_cart' ) && is_cart();
break;
case 'checkout':
$match = function_exists( 'is_checkout' ) && is_checkout() && ! ( function_exists( 'is_order_received_page' ) && is_order_received_page() );
break;
case 'thankyou':
$match = function_exists( 'is_order_received_page' ) && is_order_received_page();
break;
case 'rankings':
case 'collections':
// All pages of that kind, or only the chosen ones.
$ids = array_map( 'intval', (array) $row[ $row['page'] ] );
$match = is_singular( Store::LISTINGS[ $row['page'] ] ) && ( ! $ids || in_array( (int) get_queried_object_id(), $ids, true ) );
break;
default:
$match = false;
}
return (bool) apply_filters( 'cpod_hook_block_matches', $match, $row );
}
/**
* Products in the terms (or their child terms), or the archive page of one of them.
*
* @param int[] $term_ids Chosen terms.
* @param string $taxonomy Taxonomy.
* @param bool $is_product On a single product page.
* @return bool
*/
private static function term_match( $term_ids, $taxonomy, $is_product ) {
$term_ids = array_map( 'intval', (array) $term_ids );
if ( ! $term_ids ) {
return false;
}
$all = $term_ids;
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
foreach ( $term_ids as $term_id ) {
$children = get_term_children( $term_id, $taxonomy );
if ( is_array( $children ) ) {
$all = array_merge( $all, array_map( 'intval', $children ) );
}
}
}
if ( $is_product ) {
return has_term( $all, $taxonomy, (int) get_queried_object_id() );
}
return is_tax( $taxonomy, $all );
}
}