Skip to content

Commit

Permalink
Add nonce verification to preview link
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Jan 6, 2025
1 parent 0417263 commit 0fa2421
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/includes/conditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ function mc_is_tablet() {
*/
function mc_is_preview() {
if ( isset( $_GET['preview'] ) && 'true' === $_GET['preview'] && current_user_can( 'mc_manage_events' ) ) {
$nonce = sanitize_text_field( wp_unslash( $_GET['mcpreviewnonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'mcpreviewnonce' ) ) {
return false;
}
return true;
}

Expand Down
14 changes: 12 additions & 2 deletions src/my-calendar-event-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,12 @@ function mc_form_fields( $data, $mode, $event_id ) {
} else {
$view_url = mc_get_permalink( $first );
if ( ! mc_event_published( $data ) ) {
$view_url = add_query_arg( 'preview', 'true', mc_get_permalink( $first ) );
$nonce = wp_create_nonce( 'mcpreviewnonce' );
$args = array(
'preview' => 'true',
'mcpreviewnonce' => $nonce,
);
$view_url = add_query_arg( $args, mc_get_permalink( $first ) );
}
$tag_preview = add_query_arg(
array(
Expand Down Expand Up @@ -3118,7 +3123,12 @@ function mc_controls( $mode, $has_data, $event, $position = 'header' ) {
if ( mc_event_published( $event ) ) {
$controls['view'] = "<span class='dashicons dashicons-laptop' aria-hidden='true'></span><a href='" . esc_url( $view_url ) . "' class='view'>" . __( 'View', 'my-calendar' ) . '</a>';
} elseif ( current_user_can( 'mc_manage_events' ) ) {
$controls['view'] = "<span class='dashicons dashicons-laptop' aria-hidden='true'></span><a href='" . add_query_arg( 'preview', 'true', $view_url ) . "' class='view'>" . __( 'Preview', 'my-calendar' ) . '</a>';
$nonce = wp_create_nonce( 'mcpreviewnonce' );
$args = array(
'preview' => 'true',
'mcpreviewnonce' => $nonce,
);
$controls['view'] = "<span class='dashicons dashicons-laptop' aria-hidden='true'></span><a href='" . add_query_arg( $args, $view_url ) . "' class='view'>" . __( 'Preview', 'my-calendar' ) . '</a>';
}
$manage_text = __( 'Events', 'my-calendar' );
$controls['manage'] = "<span class='dashicons dashicons-calendar' aria-hidden='true'></span>" . '<a href="' . admin_url( 'admin.php?page=my-calendar-manage' ) . '">' . $manage_text . '</a>';
Expand Down

0 comments on commit 0fa2421

Please sign in to comment.