'js' (default): placeholders only, so the response is cacheable. * 'auto': each placement decides like on a page view (SSR ads are baked in). * * @return string The callback's output. */ public static function run( $post, callable $callback, array $args = [] ): string { if ( did_action( 'wp' ) && ! wp_doing_ajax() && ! Util::is_rest_request() ) { RuntimeDebugger::add_doing_it_wrong_entry( 'adpresso_render_in_post_context() is only for REST/AJAX requests: inside a page view it would replace the page\'s own pipeline state.' ); return ''; } $post = get_post( $post ); if ( ! $post instanceof WP_Post ) { return ''; } global $wp_query, $wp_the_query; $saved_query = $wp_query; $saved_main_query = $wp_the_query; $saved_post = $GLOBALS['post'] ?? null; $saved_context = Plugin::get()->context; $query = new WP_Query( [ 'p' => $post->ID, 'post_type' => $post->post_type, 'post_status' => $post->post_status, ] ); $wp_query = $query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the post must be the main query, restored below. $wp_the_query = $query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $force_js = static fn() => true; if ( ( $args['output'] ?? 'js' ) === 'js' ) { add_filter( 'adpresso_requires_frontend_logic', $force_js, PHP_INT_MAX ); } ob_start(); try { Plugin::get()->gather_context(); Runtime::reset_request_state(); RuntimeDebugger::reset_logs(); Runtime::maybe_execute_processor(); $returned = ''; if ( $query->have_posts() ) { $query->the_post(); $returned = (string) $callback( $post ); } return ob_get_contents() . $returned; } finally { ob_end_clean(); remove_filter( 'adpresso_requires_frontend_logic', $force_js, PHP_INT_MAX ); $query->in_the_loop = false; $wp_query = $saved_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $wp_the_query = $saved_main_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $GLOBALS['post'] = $saved_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited if ( $saved_post instanceof WP_Post ) { setup_postdata( $saved_post ); } Plugin::get()->context = $saved_context; } } }