get_meta( 'placement_type', 'manual' ); } /** * Gets the PlacementType object associated with this placement. * * @return PlacementTypeInterface|null */ public function get_type_object(): ?object { return PlacementTypeRegistry::get()->get_by_id( $this->get_type() ); } /** * Checks if this Placement is of a specific type. * * @param string $type * * @return bool */ public function is_type( string $type ): bool { return $this->get_type() === $type; } /** * Gets the assigned item (Ad or Group). * The returned item is already processed (backend checks, etc.). * * @param bool $process_data * * @return Entity|null */ public function get_item( bool $process_data = true ): Entity|null { // Cache the placement item internally. if ( $this->item_cache !== null ) { return ( $this->item_cache === 'checked_no_item' ) ? null : $this->item_cache; } $item_id = absint( $this->get_meta( 'placement_item_id', $this->item_id ?? 0 ) ); if ( empty( $item_id ) ) { $this->item_cache = 'checked_no_item'; return null; } $item = Util::get_entity_from_post( $item_id, $process_data ); $this->item_cache = $item; return $item; } /** * Runs all backend checks for this placement, conditions, and item. * Prepares the frontend config data. */ public function processBackendData( array $context_info = [] ): void { if ( $this->backend_processed ) { return; } $this->prepare_backend_data( $context_info ); $item = $this->get_item(); if ( ! $item ) { // Keep an earlier, more specific reason (not published, conditions failed). if ( $this->is_backend_valid || ! $this->skip_reason ) { $this->skip_reason = __( 'No valid item associated', 'adpresso' ); } $this->is_backend_valid = false; $this->backend_processed = true; return; } if ( ConditionHelper::tree_contains_frontend_conditions( $this->conditions_json ) || ( $item->is_backend_valid && $item->requires_frontend_logic ) ) { $this->requires_frontend_logic = true; } $placement_type_object = $this->get_type_object(); if ( $placement_type_object && $placement_type_object->requires_js_rendering() ) { $this->requires_frontend_logic = true; } /** * Filter to determine if this placement requires frontend logic. */ $this->requires_frontend_logic = apply_filters( 'adpresso_placement_requires_frontend_logic', $this->requires_frontend_logic, $this, $item ); // Add frontend config data for specific placement types. if ( $item->is_backend_valid ) { // Every JS path (processor, manual/virtual placements) ships this item config, so the consent flag is set once here. $this->frontend_config_data['item'] = ConsentPolicy::add_js_flag( $item->frontend_config_data, $item, $this ); if ( ConditionHelper::tree_contains_frontend_conditions( $item->conditions_json ) ) { $this->frontend_config_data['item_conditions_json'] = $item->get_frontend_conditions_json(); } } $this->frontend_config_data['should_wrap'] = $this->should_wrap(); $this->frontend_config_data['raw_items'] = $this->outputs_raw_items(); $this->frontend_config_data['placement_type'] = $this->get_type(); // One per page (header code, bars, overlays): a copy inside content loaded later is ignored. $this->frontend_config_data['page_scoped'] = in_array( $this->get_type(), [ 'header', 'footer', 'ad_bar', 'popup', 'background' ], true ); $this->frontend_config_data['ad_label_enabled'] = (bool) Settings::get_setting( 'ad_label', false ); $this->frontend_config_data['ad_label_text'] = Settings::get_setting( 'ad_label_text' ) ?? __( 'Advertisement', 'adpresso' ); $this->frontend_config_data['ad_label_allow_html'] = (bool) Settings::get_setting( 'ad_label_allow_html', false ); $this->frontend_config_data['label_override'] = $this->get_meta( 'label_override', '' ); $this->frontend_config_data['id'] = $this->get_id(); $this->frontend_config_data['public_id'] = $this->get_public_id(); $this->frontend_config_data['layout_styles'] = $this->get_layout_styles_string(); $this->frontend_config_data['is_inline_override'] = $this->is_inline_override; $this->frontend_config_data['classes_override'] = $this->classes_override; $this->frontend_config_data['css_override'] = $this->css_override; // Read by createPlacementWrapper() (utils.js), so the JS-built wrapper matches the SSR one. $this->frontend_config_data['custom_classes'] = Util::sanitize_html_classes( (string) $this->get_meta( 'custom_classes', '' ) ); $this->frontend_config_data['inline_css'] = safecss_filter_attr( $this->get_meta( 'inline_css', '' ) ); // Printed after the wrapper by rendering-pipeline.js, like render_with_wrapper() does for SSR. $this->frontend_config_data['custom_placement_code'] = Util::get_custom_code( $this, 'custom_placement_code' ); $this->frontend_config_data = apply_filters( 'adpresso_placement_frontend_config_data', $this->frontend_config_data, $this, $item ); $this->backend_processed = true; } /** * Generates the final SSR output for this placement. * Takes the already server-side rendered inner content from the item. * * @param string $inner_content The content that has already been server-side rendered. * * @return string Final HTML for the SSR output of this placement. */ public function render_with_wrapper( string $inner_content ): string { if ( empty( $inner_content ) ) { return ''; } // If the site or the author is not allowed to have unfiltered HTML, we need to sanitize the inner content, regardless of how it was saved. if ( ! Util::can_author_unfiltered_html( $this->post ) ) { $inner_content = wp_kses_post( $inner_content ); } // Added after the kses pass above, which would strip ', $frontend_prefix ); Runtime::$label_css_added = true; } return $label_html; } /** * Prepares the entity's data for a REST API response. * Extends the base method from the Entity class to add Placement-specific data. * * @param bool $include_edit_only_data See Entity::prepare_for_rest_response(). * * @return array The prepared data array for the REST response. */ public function prepare_for_rest_response( bool $include_edit_only_data = true ): array { $data = parent::prepare_for_rest_response( $include_edit_only_data ); $type_object = $this->get_type_object(); $item = $this->get_item( false ); $id = $this->get_id(); $data['type'] = $type_object ? $type_object->get_title() : $this->get_type(); if ( $item instanceof Entity ) { $item_id = $item->get_id(); $data['item'] = [ 'id' => $item_id, 'title' => $item->get_title(), 'type' => $item instanceof Ad ? 'Ad' : 'Group', 'edit_link' => Util::get_entity_edit_url( $item instanceof Ad ? 'ads' : 'groups', $item_id, 'placements' ), ]; } else { $data['item'] = null; } $data['image'] = $this->get_image(); $data['preview'] = null; $data['used_in'] = []; if ( $this->get_type() === 'manual' ) { $data['shortcode'] = '[adpresso_placement id="' . $id . '"]'; } /** * Filters a Placement's fully-assembled REST response data, right before it's sent to the edit modal * * @param array $data The prepared response data. * @param Placement $placement This placement. */ return apply_filters( 'adpresso_placement_rest_response', $data, $this ); } /** * Get the icon of the placement type as a SVG string. * * @return string */ public function get_image(): string { $type_object = $this->get_type_object(); $icon_path = $type_object ? $type_object->get_image() : 'admin/assets/img/placement-types/ap-ui-plcmnts-fallback.svg'; return SvgLoader::get( ADPRESSO_ABSPATH . $icon_path, [ 'class' => 'adpresso-icon-main' ] ); } }