Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the 404 template and add a new generic error template #1842

Merged
merged 10 commits into from
May 28, 2024
6 changes: 6 additions & 0 deletions assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1703,3 +1703,9 @@ a.tour-button.button-primary:hover {
.webui-popover a {
color: #fff;
}

.error-template {
text-align: center;
margin: 0 auto;
padding: 7rem 0;
}
28 changes: 26 additions & 2 deletions gp-includes/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,33 @@ public function __construct() {
add_action( 'gp_before_request', array( $this, 'check_uri_trailing_slash' ) );
}

public function die_with_error( $message, $status = 500 ) {
/**
* Shows a template and exit.
*
* @param string $message The message to display.
* @param int $status The HTTP status code.
* @param string|null $title The title of the page.
* @param string $template The template to use.
*
* @return void
*/
public function die_with_error( string $message, int $status = 500, ?string $title = null, string $template = 'error' ) {
if ( null === $title ) {
$title = esc_html__( 'Error', 'glotpress' );
}
$this->status_header( $status );
$this->exit_( $message );
if ( isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
$this->exit_( $message );
} else {
$this->tmpl(
$template,
array(
'title' => $title,
'message' => $message,
)
);
$this->exit_();
}
}

public function before_request() {
Expand Down
4 changes: 2 additions & 2 deletions gp-includes/routes/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function mass_create_sets_post( $project_path ) {
$other_project = GP::$project->get( gp_post( 'project_id' ) );

if ( ! $other_project ) {
return $this->die_with_error( __( 'Project wasn’found', 'glotpress' ) );
return $this->die_with_error( esc_html__( 'Project wasn’found', 'glotpress' ), 404, __esc_html__( 'Not found', 'glotpress' ), '404' );
}

$changes = $project->set_difference_from( $other_project );
Expand Down Expand Up @@ -518,7 +518,7 @@ public function mass_create_sets_preview_post( $project_path ) {
$other_project = GP::$project->get( gp_post( 'project_id' ) );

if ( ! $other_project ) {
return $this->die_with_error( __( 'Project wasn’found', 'glotpress' ) );
return $this->die_with_error( esc_html__( 'Project wasn’found', 'glotpress' ), 404, __esc_html__( 'Not found', 'glotpress' ), '404' );
}

header( 'Content-Type: application/json' );
Expand Down
4 changes: 2 additions & 2 deletions gp-includes/routes/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ private function edit_single_translation( $project_path, $locale_slug, $translat
$translation = GP::$translation->get( gp_post( 'translation_id' ) );

if ( ! $translation ) {
return $this->die_with_error( 'Translation doesn’t exist!' );
return $this->die_with_error( esc_html__( 'Translation doesn’t exist!', 'glotpress' ), 404, esc_html__( 'Not Found', 'glotpress' ), '404' );
}

$this->can_approve_translation_or_forbidden( $translation );
Expand Down Expand Up @@ -776,7 +776,7 @@ private function edit_single_translation( $project_path, $locale_slug, $translat
*/
private function discard_warning_edit_function( $project, $locale, $translation_set, $translation ) {
if ( ! isset( $translation->warnings[ gp_post( 'index' ) ][ gp_post( 'key' ) ] ) ) {
return $this->die_with_error( 'The warning doesn’exist!' );
return $this->die_with_error( esc_html__( 'The warning doesn’t exist!', 'glotpress' ), 404, esc_html__( 'Not Found', 'glotpress' ), '404' );
}

$warning = array(
Expand Down
9 changes: 6 additions & 3 deletions gp-templates/404.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
gp_title( __( 'Not Found &lt; GlotPress', 'glotpress' ) );
gp_tmpl_header();

_e( 'Not Found', 'glotpress' );

?>
<div class="error-template">
<h2><?php esc_html_e( 'Not Found', 'glotpress' ); ?></h2>
<?php esc_html_e( 'The requested URL was not found on this server.', 'glotpress' ); ?>
</div>
<?php
gp_tmpl_footer();
18 changes: 18 additions & 0 deletions gp-templates/error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Template for errors.
*/

/** @var string $title */
/** @var string $message */

/* Translators: %s: title */
gp_title( esc_html( sprintf( __( '%s &lt; GlotPress', 'glotpress' ), $title ? $title : esc_html__( 'Error', 'glotpress' ) ) ) );
gp_tmpl_header();
?>
<div class="error-template">
<h2><?php echo esc_html( $title ); ?></h2>
<?php echo esc_html( $message ); ?>
</div>
<?php
gp_tmpl_footer();
Loading