Skip to content

Commit

Permalink
refactor: extract conditional logic to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mindctrl committed Nov 3, 2023
1 parent 5ed2451 commit ca09f06
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
10 changes: 3 additions & 7 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ function post_link( $link ) {
global $pagenow;
$target_pages = array( 'admin-ajax.php', 'index.php', 'edit.php', 'post.php', 'post-new.php', 'upload.php', 'media-new.php' );

// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `is_ajax_generate_permalink_request()` and `is_wp_link_ajax_request()`.
if ( empty( $_POST ) && 'post-new.php' === $pagenow ) {
return $link;
}

// Ajax requests to generate permalink.
if ( in_array( $pagenow, $target_pages, true )
&& ! empty( $_POST['samplepermalinknonce'] )
&& check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' )
&& is_ajax_generate_permalink_request()
) {
return $link;
}
Expand All @@ -225,11 +225,7 @@ function post_link( $link ) {
}

// Check for wp-link-ajax requests. Used by Classic Editor when linking content.
if ( wp_doing_ajax()
&& ! empty( $_POST['_ajax_linking_nonce'] )
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
&& ! empty( $_POST['action'] )
&& 'wp-link-ajax' === $_POST['action'] ) {
if ( is_wp_link_ajax_request() ) {
return $link;
}

Expand Down
21 changes: 21 additions & 0 deletions plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ function has_file_extension( $string ) {
}
}

/**
* Determines if an AJAX request to generate permalinks is in progress.
*
* @return boolean
*/
function is_ajax_generate_permalink_request(): bool {
return ( ! empty( $_POST['samplepermalinknonce'] ) && check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ) );
}

/**
* Determines if a wp-link-ajax request is in progress.
*
* @return boolean
*/
function is_wp_link_ajax_request(): bool {
return ( wp_doing_ajax()
&& ! empty( $_POST['_ajax_linking_nonce'] )
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
&& ! empty( $_POST['action'] )
&& 'wp-link-ajax' === $_POST['action'] );
}

0 comments on commit ca09f06

Please sign in to comment.