0 ], $atts )['id'] ); if ( ! $id ) { return ''; } $entity = Util::get_entity_from_post( $id ); return FrontendHooks::render_virtual_entity( $entity, 'shortcode' ); } /** * Shortcode-Callback for [adpresso-group id="456"]. * * @param array $atts Shortcode-Attributes. * * @return string The rendered HTML. */ public function render_group_shortcode( array $atts ): string { $id = absint( shortcode_atts( [ 'id' => 0 ], $atts )['id'] ); if ( ! $id ) { return ''; } $entity = Util::get_entity_from_post( $id ); return FrontendHooks::render_virtual_entity( $entity, 'shortcode' ); } /** * Renders a physical, manual placement. * * @param int $placement_id The ID of the Placements. * @param string $trigger Where it has been triggered. * * @return string The rendered HTML. */ public static function render_manual_placement( int $placement_id, string $trigger ): string { if ( ! $placement_id ) { return ''; } $placement = adpresso_get_placement( $placement_id ); if ( ! $placement ) { return ''; } $processed_data = Runtime::get_processed_data(); $placement_data = $processed_data[ $placement_id ] ?? null; $item = $placement_data['item'] ?? $placement->get_item(); // Prepare debugging. $snapshot = RuntimeDebugger::build_snapshot( $placement, $item ); $snapshot['execution']['trigger'] = $trigger; $snapshot['execution']['hook_fired'] = true; if ( $placement->get_type() !== 'manual' ) { $snapshot['execution']['status'] = 'error'; $snapshot['execution']['skip_reason'] = __( 'Wrong usage of manual placement', 'adpresso' ); RuntimeDebugger::add_log_entry( $placement_id, $snapshot ); RuntimeDebugger::mark_output_triggered( $placement_id ); NotificationManager::get()->register( 'adpresso-manual-placement', __( 'Wrong usage of manual placement', 'adpresso' ), [ 'location' => 'admin_bar', 'context' => 'adpresso-notifications', 'type' => 'info', ] ); return ''; } // RENDERING LOGIK if ( ! $placement_data ) { // Fallback-Loading $renderer = PlacementRenderer::get_instance(); $output = $renderer->render( $placement, '' ); $snapshot['execution']['output_mode'] = 'fallback'; } else { $snapshot['execution']['output_mode'] = $placement_data['output_mode'] ?? 'unknown'; if ( ! empty( $placement_data['skip_reason'] ) ) { $snapshot['execution']['skip_reason'] = $placement_data['skip_reason']; if ( $placement_data['output_mode'] === 'skipped' ) { $snapshot['execution']['status'] = 'skipped'; } } $output = match ( $placement_data['output_mode'] ) { 'ssr' => $placement->render_with_wrapper( $placement_data['content'] ), 'js' => $placement->get_placeholder_html(), default => '', }; } RuntimeDebugger::add_log_entry( $placement_id, $snapshot ); RuntimeDebugger::mark_output_triggered( $placement_id ); return $output; } /** * Renders an ad or group with a virtual placement. * * @param Entity $entity The Ad- or Group-Object. * @param string $trigger The origin of the call (e.g. 'shortcode', 'manual_block'). * * @return string The rendered HTML. */ public static function render_virtual_entity( Entity $entity, string $trigger ): string { if ( ! ( $entity instanceof Ad || $entity instanceof Group ) ) { return ''; } $prefix = $entity instanceof Ad ? 'ad' : 'group'; $placement = PlacementFactory::make_virtual( $prefix, $entity->get_id() ); $placement->processBackendData(); $uid = $placement->get_uid(); $placement->frontend_config_data['uid'] = $uid; $context = [ 'placement_id' => $placement->get_id(), 'placement' => $placement, 'is_raw_output' => ! $placement->should_wrap(), ]; if ( ! $entity->can_be_displayed() ) { $entity->is_backend_valid = false; $entity->skip_reason = 'Display once limit reached (Virtual Placement)'; } $output = ''; if ( $entity->is_backend_valid ) { if ( $entity->requires_frontend_logic ) { $output = $placement->get_placeholder_html(); // We need to make sure, that virtual config is available here. FrontendHooks::$virtual_js_configs[ $uid ] = $placement->frontend_config_data; $entity->mark_as_rendered(); } else { $renderer = PlacementRenderer::get_instance(); $inner_content = $entity->get_type_object()->output( $entity, $context ); $output = $renderer->render( $placement, $inner_content ); if ( ! empty( $inner_content ) ) { $entity->mark_as_rendered(); } } } $snapshot = RuntimeDebugger::build_snapshot( $placement, $entity ); $snapshot['execution']['trigger'] = $trigger . '_virtual'; $snapshot['execution']['output_mode'] = $entity->requires_frontend_logic ? 'js' : 'ssr'; $snapshot['execution']['hook_fired'] = true; RuntimeDebugger::add_log_entry( $uid, $snapshot ); RuntimeDebugger::mark_output_triggered( $uid ); return $output; } /** * Sends the gathered virtual configs to the global JS object. * * @return void */ public function localize_virtual_configs(): void { if ( empty( self::$virtual_js_configs ) ) { return; } ?>