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

Prevent the usage of event titles that can conflict with event URLs #113

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions assets/js/translation-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@
$gp.notices.success( response.data.message );
}
},
error: function ( error ) {
$gp.notices.error( response.data.message );
}
error: function ( xhr, msg ) {
/* translators: %s: Error message. */
msg = xhr.responseJSON.data ? wp.i18n.sprintf( wp.i18n.__( 'Error: %s', 'gp-translation-events' ), xhr.responseJSON.data ) : wp.i18n.__( 'Error saving the event!', 'gp-translation-events' );
$gp.notices.error( msg );
},
}
);
}
Expand Down
12 changes: 8 additions & 4 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,18 @@ function submit_event_ajax() {
}
}
if ( ! $is_nonce_valid ) {
wp_send_json_error( 'Nonce verification failed' );
wp_send_json_error( 'Nonce verification failed', 403 );
}

// This is a list of slugs that are not allowed, as they conflict with the event URLs.
$invalid_slugs = array( 'new', 'edit', 'attend', 'my-events' );
$title = isset( $_POST['event_title'] ) ? sanitize_text_field( wp_unslash( $_POST['event_title'] ) ) : '';
$description = isset( $_POST['event_description'] ) ? sanitize_text_field( wp_unslash( $_POST['event_description'] ) ) : '';
$event_start = isset( $_POST['event_start'] ) ? sanitize_text_field( wp_unslash( $_POST['event_start'] ) ) : '';
$event_end = isset( $_POST['event_end'] ) ? sanitize_text_field( wp_unslash( $_POST['event_end'] ) ) : '';
$event_timezone = isset( $_POST['event_timezone'] ) ? sanitize_text_field( wp_unslash( $_POST['event_timezone'] ) ) : '';
if ( isset( $title ) && in_array( sanitize_title( $title ), $invalid_slugs, true ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been sanitized above, there's no need to sanitize, right? Though maybe above it should be changed to sanitize_title() instead of sanitize_text_field()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I want in this line is the slug, to look for it in the array, so I think is the correct function, because the title has been sanitized before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I understand now, thanks 👍

wp_send_json_error( 'Invalid slug', 403 );
}

$is_valid_event_date = false;
try {
Expand Down Expand Up @@ -336,10 +340,10 @@ function gp_event_nav_menu_items( array $items, string $location ): array {
*
* Generate a slug based on the event title if it's not provided.
*
* @param array $data An array of slashed post data.
* @param array $data An array of slashed post data.
* @return array The modified post data.
*/
function generate_event_slug( $data ) {
function generate_event_slug( array $data ): array {
if ( 'event' === $data['post_type'] && 'draft' === $data['post_status'] ) {
if ( empty( $data['post_name'] ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
Expand Down
Loading