P_DEBUG_LOG */
__( 'Please check your server PHP error logs; to do this you may need to enable %2$s.', 'amp' ),
esc_url( 'https://wordpress.org/support/article/debugging-in-wordpress/' ),
'WP_DEBUG_LOG
'
);
if ( $error_message ) {
$error_message = rtrim( $error_message, '.' ) . '.';
}
$support_forum_message = sprintf(
/* translators: %1$s: Link to support forum. %2$s: Link to new topic form in support forum. */
__( 'If you are stuck, please search the support forum for possible related topics, or otherwise start a new support topic including the error message, the URL to your site, and your active theme/plugins.', 'amp' ),
esc_url( 'https://wordpress.org/support/plugin/amp/' ),
esc_url( 'https://wordpress.org/support/plugin/amp/#new-topic-0' )
);
$site_health_message = sprintf(
/* translators: %s is link to Site Health */
__( 'Please check your Site Health to verify it can perform loopback requests.', 'amp' ),
esc_url( admin_url( 'site-health.php' ) )
);
$support_forum_message .= ' ' . sprintf(
/* translators: %s is the URL to Site Health Info. */
__( 'Please include your Site Health Info.', 'amp' ),
esc_url( admin_url( 'site-health.php?tab=debug' ) )
);
$implode_non_empty_strings_with_spaces_and_sanitize = static function ( $strings ) {
return wp_kses(
implode( ' ', array_filter( $strings ) ),
[
'a' => array_fill_keys( [ 'href', 'target' ], true ),
'code' => [],
]
);
};
switch ( $error_code ) {
case 'http_request_failed':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'Failed to fetch URL to validate.', 'amp' ),
esc_html( $error_message ),
$site_health_message,
$support_forum_message,
]
);
case 'white_screen_of_death':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'Unable to validate URL. A white screen of death was encountered which is likely due to a PHP fatal error.', 'amp' ),
esc_html( $error_message ),
$check_error_log,
$support_forum_message,
]
);
case '404':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'The fetched URL was not found. It may have been deleted. If so, you can trash this.', 'amp' ),
esc_html( $error_message ),
$support_forum_message,
]
);
case '500':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'An internal server error occurred when fetching the URL for validation.', 'amp' ),
esc_html( $error_message ),
$check_error_log,
$support_forum_message,
]
);
case 'fatal_error_during_validation':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'A PHP fatal error occurred while validating the URL. This may indicate either a bug in theme/plugin code or it may be due to an issue in the AMP plugin itself.', 'amp' ),
defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY
? esc_html__( 'The error details appear below.', 'amp' )
/* translators: %s is WP_DEBUG_DISPLAY */
: $check_error_log . ' ' . wp_kses_post( sprintf( __( 'Alternatively, you may enable %s to show the error details below.', 'amp' ), 'WP_DEBUG_DISPLAY
' ) ),
$support_forum_message,
]
);
case 'response_not_json':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'URL validation failed due to the AMP validation request not returning JSON data. This may be due to a PHP fatal error occurring.', 'amp' ),
esc_html( $error_message ),
$check_error_log,
$support_forum_message,
]
);
case 'malformed_json_validation_errors':
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
esc_html__( 'URL validation failed due to unexpected JSON in AMP validation response.', 'amp' ),
esc_html( $error_message ),
$support_forum_message,
]
);
default:
return $implode_non_empty_strings_with_spaces_and_sanitize(
[
/* translators: %s is error code */
esc_html( sprintf( __( 'URL validation failed. Error code: %s.', 'amp' ), $error_code ) ),
esc_html( $error_message ),
$support_forum_message,
]
);
}
}
/**
* Enqueues the block validation script.
*
* @return void
*/
public static function enqueue_block_validation() {
/*
* The AMP_Validation_Manager::post_supports_validation() method is not being used here because
* a post's status for validation checking can change during the life of the editor, such as when
* the user toggles AMP back on after having turned it off, and then gets the validation
* warnings appearing due to the amp-block-validation having been enqueued already.
*/
if ( ! self::get_dev_tools_user_access()->is_user_enabled() ) {
return;
}
// Only enqueue scripts on the block editor for AMP-enabled posts.
$editor_support = Services::get( 'editor.editor_support' );
if ( ! $editor_support->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
return;
}
$slug = 'amp-block-validation';
$asset_file = AMP__DIR__ . '/assets/js/' . $slug . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];
wp_enqueue_script(
$slug,
amp_get_asset_url( "js/{$slug}.js" ),
$dependencies,
$version,
true
);
wp_enqueue_style(
$slug,
amp_get_asset_url( "css/{$slug}.css" ),
false,
AMP__VERSION
);
wp_styles()->add_data( $slug, 'rtl', 'replace' );
$block_sources = Services::has( 'dev_tools.block_sources' ) ? Services::get( 'dev_tools.block_sources' ) : null;
$plugin_registry = Services::get( 'plugin_registry' );
$plugin_names = array_map(
static function ( $plugin ) {
return isset( $plugin['Name'] ) ? $plugin['Name'] : '';
},
$plugin_registry->get_plugins()
);
$data = [
'HTML_ATTRIBUTE_ERROR_TYPE' => AMP_Validation_Error_Taxonomy::HTML_ATTRIBUTE_ERROR_TYPE,
'HTML_ELEMENT_ERROR_TYPE' => AMP_Validation_Error_Taxonomy::HTML_ELEMENT_ERROR_TYPE,
'JS_ERROR_TYPE' => AMP_Validation_Error_Taxonomy::JS_ERROR_TYPE,
'CSS_ERROR_TYPE' => AMP_Validation_Error_Taxonomy::CSS_ERROR_TYPE,
'VALIDATION_ERROR_NEW_REJECTED_STATUS' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_REJECTED_STATUS,
'VALIDATION_ERROR_NEW_ACCEPTED_STATUS' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_NEW_ACCEPTED_STATUS,
'VALIDATION_ERROR_ACK_REJECTED_STATUS' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_REJECTED_STATUS,
'VALIDATION_ERROR_ACK_ACCEPTED_STATUS' => AMP_Validation_Error_Taxonomy::VALIDATION_ERROR_ACK_ACCEPTED_STATUS,
'isSanitizationAutoAccepted' => self::is_sanitization_auto_accepted(),
'blockSources' => $block_sources ? $block_sources->get_block_sources() : null,
'pluginNames' => $plugin_names,
'themeName' => wp_get_theme()->get( 'Name' ),
'themeSlug' => wp_get_theme()->get_stylesheet(),
];
wp_add_inline_script(
$slug,
sprintf(
'var ampBlockValidation = %s;',
wp_json_encode( $data )
),
'before'
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( $slug, 'amp' );
} elseif ( function_exists( 'wp_get_jed_locale_data' ) || function_exists( 'gutenberg_get_jed_locale_data' ) ) {
$locale_data = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'amp' ) : gutenberg_get_jed_locale_data( 'amp' );
$translations = wp_json_encode( $locale_data );
wp_add_inline_script(
$slug,
'wp.i18n.setLocaleData( ' . $translations . ', "amp" );',
'after'
);
}
}
}