is_active() ) { return false; } if ( ! self::user_matches( $display ) ) { return false; } // Shortcodes, blocks, widgets and PHP calls bypass the location rules. if ( 'manual' === $context ) { return true; } // WooCommerce non-singular placements (cart, checkout, shop) are governed // by the chosen placement hook, not by post-type rules. if ( in_array( $context, array( 'cart', 'checkout', 'order', 'shop' ), true ) ) { return true; } $post = get_post( $post_id ); if ( ! $post ) { return false; } $types = (array) $display->opt( 'cond_types' ); if ( $types && ! in_array( $post->post_type, $types, true ) ) { return false; } $include_ids = srpp_int_list( $display->opt( 'cond_ids_include' ) ); if ( $include_ids && ! in_array( (int) $post_id, $include_ids, true ) ) { return false; } if ( in_array( (int) $post_id, srpp_int_list( $display->opt( 'cond_ids_exclude' ) ), true ) ) { return false; } if ( ! self::term_rules_pass( $display, $post_id ) ) { return false; } if ( ! self::price_passes( $display, $post_id ) ) { return false; } return true; } /** * Include/exclude taxonomy rules. * * @param SRPP_Display $display Display. * @param int $post_id Post ID. * @return bool */ protected static function term_rules_pass( $display, $post_id ) { $exclude = (array) $display->opt( 'cond_exclude' ); foreach ( $exclude as $rule ) { if ( self::has_terms( $post_id, $rule ) ) { return false; } } $include = array_filter( (array) $display->opt( 'cond_include' ) ); if ( empty( $include ) ) { return true; } $logic = 'or' === $display->opt( 'cond_logic' ) ? 'or' : 'and'; $matched = 0; foreach ( $include as $rule ) { if ( self::has_terms( $post_id, $rule ) ) { $matched++; } elseif ( 'and' === $logic ) { return false; } } return ( 'and' === $logic ) ? true : $matched > 0; } /** * Does a post carry any of the rule's terms? * * @param int $post_id Post ID. * @param array $rule ['taxonomy' => ..., 'terms' => [...]]. * @return bool */ protected static function has_terms( $post_id, $rule ) { if ( empty( $rule['taxonomy'] ) || empty( $rule['terms'] ) ) { return false; } return has_term( array_map( 'absint', (array) $rule['terms'] ), $rule['taxonomy'], $post_id ); } /** * WooCommerce price range condition. * * @param SRPP_Display $display Display. * @param int $post_id Post ID. * @return bool */ protected static function price_passes( $display, $post_id ) { $min = $display->opt( 'cond_price_min' ); $max = $display->opt( 'cond_price_max' ); if ( '' === $min && '' === $max ) { return true; } if ( ! srpp_is_woo_active() || 'product' !== get_post_type( $post_id ) ) { return true; } $product = wc_get_product( $post_id ); if ( ! $product ) { return true; } $price = (float) $product->get_price(); if ( '' !== $min && $price < (float) $min ) { return false; } if ( '' !== $max && $price > (float) $max ) { return false; } return true; } /** * Logged-in / logged-out targeting. * * @param SRPP_Display $display Display. * @return bool */ protected static function user_matches( $display ) { $rule = $display->opt( 'cond_users', 'all' ); if ( 'logged_in' === $rule ) { return is_user_logged_in(); } if ( 'logged_out' === $rule ) { return ! is_user_logged_in(); } return true; } }