*/ private array $types = []; /** * Get the singleton instance. * * @return self */ public static function get(): self { if ( self::$instance === null ) { self::$instance = new self(); } return self::$instance; } /** * Register a placement type. * * @param PlacementTypeInterface $type * * @return void */ public function register( PlacementTypeInterface $type ): void { $id = $type->get_id(); if ( isset( $this->types[ $id ] ) ) { RuntimeDebugger::add_doing_it_wrong_entry( sprintf( 'Placement Type ID "%s" was overwritten. Origin type: %s, new type: %s', esc_attr( $id ), esc_html( get_class( $this->types[ $id ] ) ), esc_html( get_class( $type ) ) ) ); } $this->types[ $id ] = $type; if ( did_action( 'adpresso_placement_types_registered' ) ) { if ( method_exists( $type, 'register_fields' ) ) { $type->register_fields(); } } else { add_action( 'adpresso_placement_types_registered', [ $type, 'register_fields' ], 10, 0 ); } if ( method_exists( $type, 'register_misc' ) ) { $type->register_misc(); } if ( method_exists( $type, 'hooks' ) ) { $type->hooks(); } } /** * Get a placement type by ID. * * @param string $id * * @return PlacementTypeInterface|null */ public function get_by_id( string $id ): ?PlacementTypeInterface { return $this->types[ $id ] ?? null; } /** * Get all registered placement types. * * @return PlacementTypeInterface[] */ public function get_all_types(): array { return $this->types; } /** * Define Pro types for upselling. * * @return array[] */ private function get_pro_types( ): array { return [ 'above_title' => [ 'label' => __( 'Above Title', 'adpresso' ), 'image' => SvgLoader::get( ADPRESSO_ABSPATH . 'admin/assets/img/placement-types/ap-ui-plcmnts-abovehead.svg', [ 'class' => 'adpresso-icon-unselected' ] ), 'premium' => true, 'disabled' => true, ], 'ad_bar' => [ 'label' => __( 'Ad Bar (Sticky)', 'adpresso' ), 'image' => SvgLoader::get( ADPRESSO_ABSPATH . 'admin/assets/img/placement-types/ap-ui-plcmnts-barsticky.svg', [ 'class' => 'adpresso-icon-unselected' ] ), 'premium' => true, 'disabled' => true, ], 'background' => [ 'label' => __( 'Background', 'adpresso' ), 'image' => SvgLoader::get( ADPRESSO_ABSPATH . 'admin/assets/img/placement-types/ap-ui-plcmnts-background.svg', [ 'class' => 'adpresso-icon-unselected' ] ), 'premium' => true, 'disabled' => true, ], ]; } /** * Returns all registered placement types formatted for a select field. * * @return array [ 'id' => 'Title', ... ] */ public function get_all_types_for_select(): array { $field_registry = PlacementFieldRegistry::get(); $available_types = array_reduce( $this->get_all_types(), function( $carry, $type ) use ( $field_registry ) { /** @var PlacementTypeInterface $type */ $type_id = $type->get_id(); $carry[ $type_id ] = [ 'label' => $type->get_title(), 'image' => SvgLoader::get( ADPRESSO_ABSPATH . $type->get_image(), [ 'class' => 'adpresso-icon-unselected' ] ), 'description' => $type->get_description(), // Fields with an explicit 'box' are exclusively owned/rendered by that box // (via the normal box-filtered get_fields()); don't also inject them here, // or they'd render twice. 'dependent_fields' => array_values( array_filter( $field_registry->get_fields_for_type_filtered( $type_id ), static fn( array $field ): bool => ! isset( $field['box'] ) ) ), // null = no restriction 'compatible_conditions' => apply_filters( 'adpresso_placement_type_compatible_conditions', null, $type_id ), // Empty array = no section hidden. 'hidden_sections' => apply_filters( 'adpresso_placement_type_hidden_sections', [], $type_id ), ]; return $carry; }, [] ); // A locked teaser is only shown for a Pro placement type that isn't genuinely registered. foreach ( $this->get_pro_types() as $id => $teaser ) { if ( ! isset( $available_types[ $id ] ) ) { $available_types[ $id ] = $teaser; } } return $available_types; } }