Created
January 30, 2021 01:16
-
-
Save rgadon107/ee333007cabf1b2cdcc67ab01c61c6f6 to your computer and use it in GitHub Desktop.
Solution to Code Challenge 6 from "Refactoring Tweaks Workbook"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Code Challenge 6, from “Refactoring Tweaks Workbook” by Tonya Mork ( 2016, LeanPub (https://leanpub.com/littlegreenbookofrefactoringtweaks-workbook) ). */ | |
/*************************************************** | |
* | |
* Original code to be refactored. | |
* | |
**************************************************/ | |
/** | |
* Return the special URL of a paged post. | |
* | |
* Taken from _wp_link_page() in WordPress core, but instead of anchor markup, just return the URL. | |
* | |
* @since 2.2.0 | |
* | |
* @param int $i The page number to generate the URL from. | |
* @param int $post_id The post ID | |
* | |
* @return string Unescaped URL | |
*/ | |
function prefix_paged_post_url( $i, $post_id = 0 ) { | |
global $wp_rewrite; | |
$post = get_post( $post_id ); | |
if ( 1 == $i ) { | |
$url = get_permalink( $post_id ); | |
} else { | |
if ( '' == get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ) ) ) { | |
$url = add_query_arg( 'page', $i, get_permalink( $post_id ) ); | |
} elseif ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) { | |
$url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' ); | |
} else { | |
$url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( $i, 'single_paged' ); | |
} | |
} | |
return $url; | |
} | |
/*************************************************** | |
* | |
* Refactored Code | |
* | |
**************************************************/ | |
/** | |
* Return the special URL of a paged post. | |
* | |
* Taken from _wp_link_page() in WordPress core, but instead of anchor markup, just return the URL. | |
* | |
* @since 2.2.0 | |
* | |
* @param int $page_number The page number to generate the URL from. | |
* @param int $post_id The post ID | |
* | |
* @return string Unescaped URL | |
*/ | |
function prefix_paged_post_url( $page_number, $post_id = 0 ) { | |
$post = get_post( $post_id ); | |
if ( 1 == $page_number ) { | |
$url = get_permalink( $post_id ); | |
} else { | |
url_when_page_number_does_not_equal_one( $page_number, $post_id ); | |
} | |
return $url; | |
} | |
/* | |
* Decision logic to get permalink when $page_number is not equal to 1. | |
*/ | |
function url_when_page_number_does_not_equal_one( $page_number, $post_id ) { | |
$wp_rewrite = new \WP_Rewrite(); | |
if ( '' == get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ) ) ) { | |
$url = add_query_arg( 'page', $page_number, get_permalink( $post_id ) ); | |
} elseif ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) { | |
$url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $page_number, 'single_paged' ); | |
} else { | |
$url = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( $i, 'single_paged' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment