Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -1650,49 +1650,54 @@ public static function is_output_buffering() {
*/
public static function finish_output_buffering( $response ) {
self::$is_output_buffering = false;
register_shutdown_function(
function () use ( $response ) {
try {
$response = self::prepare_response( $response );
} catch ( Exception $exception ) {
$title = __( 'Failed to prepare AMP page', 'amp' );
$message = __( 'A PHP error occurred while trying to prepare the AMP response. This may not be caused by the AMP plugin but by some other active plugin or the current theme. You will need to review the error details to determine the source of the error.', 'amp' );

$error_page = Services::get( 'dev_tools.error_page' );

$error_page
->with_title( $title )
->with_message( $message )
->with_exception( $exception )
->with_response_code( 500 );

// Add link to non-AMP version if not canonical.
if ( ! amp_is_canonical() ) {
$non_amp_url = amp_remove_paired_endpoint( amp_get_current_url() );

// Prevent user from being redirected back to AMP version.
if ( true === AMP_Options_Manager::get_option( Option::MOBILE_REDIRECT ) ) {
$non_amp_url = add_query_arg( QueryVar::NOAMP, QueryVar::NOAMP_MOBILE, $non_amp_url );
}

$error_page->with_back_link(
$non_amp_url,
__( 'Go to non-AMP version', 'amp' )
);
}

try {
$response = self::prepare_response( $response );
} catch ( Exception $exception ) {
$title = __( 'Failed to prepare AMP page', 'amp' );
$message = __( 'A PHP error occurred while trying to prepare the AMP response. This may not be caused by the AMP plugin but by some other active plugin or the current theme. You will need to review the error details to determine the source of the error.', 'amp' );

$error_page = Services::get( 'dev_tools.error_page' );

$error_page
->with_title( $title )
->with_message( $message )
->with_exception( $exception )
->with_response_code( 500 );

// Add link to non-AMP version if not canonical.
if ( ! amp_is_canonical() ) {
$non_amp_url = amp_remove_paired_endpoint( amp_get_current_url() );

// Prevent user from being redirected back to AMP version.
if ( true === AMP_Options_Manager::get_option( Option::MOBILE_REDIRECT ) ) {
$non_amp_url = add_query_arg( QueryVar::NOAMP, QueryVar::NOAMP_MOBILE, $non_amp_url );
$response = $error_page->render();
}

$error_page->with_back_link(
$non_amp_url,
__( 'Go to non-AMP version', 'amp' )
);
}

$response = $error_page->render();
}
/**
* Fires when server timings should be sent.
*
* This is immediately before the processed output buffer is sent to the client.
*
* @since 2.0
* @internal
*/
do_action( 'amp_server_timing_send' );

/**
* Fires when server timings should be sent.
*
* This is immediately before the processed output buffer is sent to the client.
*
* @since 2.0
* @internal
*/
do_action( 'amp_server_timing_send' );
return $response;
echo $response; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
);
return '';
}

/**
Expand Down