* @since 1.50.0 */ namespace AdvancedAds\Abstracts; defined( 'ABSPATH' ) || exit; /** * Placement Type. * * @method string get_id() Placement type id; every placement type defines it. * @method array get_options() Placement type options; every placement type defines it. */ class Placement_Type { /** * Hold allowed ads for cache purpose. * * @var array|null */ private $allowed_ads = null; /** * Hold allowed groups for cache purpose. * * @var array|null */ private $allowed_groups = null; /** * Apply filters on options by type id. * * @param array $options Options array. * * @return array */ protected function apply_filter_on_options( $options ): array { $defaults = [ 'show_position' => false, 'show_lazy_load' => false, 'uses_the_content' => false, 'amp' => false, ]; $options = wp_parse_args( $options, $defaults ); return apply_filters( 'advanced-ads-placement-' . $this->get_id() . '-options', $options ); } /** * Get allowed item array for dropdown * * @param string $add_empty Add empty option. * * @return array */ public function get_allowed_items( $add_empty = '' ): array { $options = []; if ( ! empty( $add_empty ) ) { $options[] = [ 'label' => $add_empty, 'value' => 0, ]; } $options['ads'] = [ 'label' => __( 'Ads', 'advanced-ads' ), 'items' => $this->get_allowed_ads(), ]; $options['groups'] = [ 'label' => __( 'Ad Groups', 'advanced-ads' ), 'items' => $this->get_allowed_groups(), ]; return $options; } /** * Get all allowed ads for this placement type. * * @return array */ public function get_allowed_ads(): array { if ( null !== $this->allowed_ads ) { return $this->allowed_ads; } $this->allowed_ads = []; foreach ( wp_advads_get_ad_summaries() as $ad_id => $summary ) { if ( ! $this->is_ad_type_allowed( $summary['type'] ) ) { continue; } $this->allowed_ads[ 'ad_' . $ad_id ] = $summary['title'] . ( 'draft' === $summary['status'] ? ' (' . __( 'draft', 'advanced-ads' ) . ')' : '' ); } return $this->allowed_ads; } /** * Get all allowed groups for this placement type. * * @return array */ public function get_allowed_groups(): array { if ( null !== $this->allowed_groups ) { return $this->allowed_groups; } $this->allowed_groups = []; $group_summaries = wp_advads_get_group_summaries(); foreach ( $group_summaries as $group_id => $summary ) { if ( ! $this->is_group_type_allowed( $summary['type'] ) ) { continue; } foreach ( wp_advads_get_ads_by_group_id( $group_id, 'summaries' ) as $ad_id => $ad_summary ) { if ( $this->is_ad_type_allowed( $ad_summary['type'] ) ) { $this->allowed_groups[ 'group_' . $group_id ] = $summary['title']; break; } } } return $this->allowed_groups; } /** * Check if the provided ad type is allowed * * @param string $type Ad type. * * @return bool */ public function is_ad_type_allowed( $type ): bool { return $this->is_entity_allowed( $type, 'ad' ); } /** * Check if the provided group type is allowed. * * @param string $type Group type. * * @return bool */ public function is_group_type_allowed( $type ): bool { return $this->is_entity_allowed( $type, 'group' ); } /** * Abstraction of whether entity is allowed. * * @param string $type Type to check. * @param string $entity Entity type i.e. `ad` or `group`. * * @return bool */ public function is_entity_allowed( $type, $entity ) { $options = $this->get_options(); $allowed = $options[ 'allowed_' . $entity . '_types' ] ?? true; return true === $allowed ? true : in_array( $type, $allowed, true ); } }