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

Adding pretty permalinks for checkout #2446

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions adminpages/pagesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
//assume success
$msg = true;
$msgt = __("Your page settings have been updated.", 'paid-memberships-pro' );

// flush rewrite rules.
flush_rewrite_rules();
}

//check nonce for generating pages
Expand Down Expand Up @@ -156,6 +159,9 @@
$msg = true;
$msgt = __("The following pages have been created for you", 'paid-memberships-pro' ) . ": " . implode(", ", $pages_created) . ".";
}

// Flush rewrite rules.
flush_rewrite_rules();
}

require_once(dirname(__FILE__) . "/admin_header.php");
Expand Down
44 changes: 44 additions & 0 deletions includes/checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,47 @@ function pmpro_calculate_profile_start_date( $order, $date_format, $filter = tru
// Convert $profile_start_date to correct format.
return date_i18n( $date_format, strtotime( $profile_start_date ) );
}

/**
* Set up rewrite rules so that pretty permalinks can be used on the checkout page.
*
* @since TBD
*/
function pmpro_checkout_rewrite_rules() {
global $pmpro_pages;

// If the checkout page is not set, return.
if ( empty( $pmpro_pages['checkout'] ) ) {
return;
}

// Get the permalink for the checkout page.
$checkout_page = get_the_permalink( $pmpro_pages['checkout'] );

// Get the base site url.
$site_url = get_site_url();

// Get the base checkout page url.
$checkout_page_base = str_replace( $site_url . '/', '', $checkout_page );

// Add the rewrite rule.
add_rewrite_rule( $checkout_page_base . '([^/]*)/?', 'index.php?page_id=' . $pmpro_pages['checkout'] . '&pmpro_checkout_level=$matches[1]', 'top' );
}
add_action( 'init', 'pmpro_checkout_rewrite_rules' );

/**
* Adding the pmpro_checkout_level query var so that it can be used in the rewrite rule.
*
* @since TBD
*
* @param array $query_vars The query vars.
* @return array The query vars.
*/
function pmpro_checkout_custom_query_vars( $query_vars ) {

$query_vars[] = 'pmpro_checkout_level';

return $query_vars;
}

add_filter( 'query_vars', 'pmpro_checkout_custom_query_vars', 10, 1 );
24 changes: 23 additions & 1 deletion includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ function pmpro_are_any_visible_levels() {
* @return mixed|void
*/
function pmpro_getLevelAtCheckout( $level_id = null, $discount_code = null ) {
global $pmpro_level, $wpdb, $post;
global $pmpro_level, $wpdb, $post, $wp_query;

// reset pmpro_level
$pmpro_level = null;
Expand All @@ -2346,6 +2346,28 @@ function pmpro_getLevelAtCheckout( $level_id = null, $discount_code = null ) {
$level_id = intval( $_REQUEST['level'] );
}

// If a pretty permalink was used, grab the level from the URL.
if ( empty( $level_id ) && ! empty( $wp_query->query_vars['pmpro_checkout_level'] ) ) {
$query_checkout_level = $wp_query->query_vars['pmpro_checkout_level'];
$all_levels = pmpro_getAllLevels( false, false );

// If the level is a number, check if we have a level with that ID.
if ( is_numeric( $query_checkout_level ) && ! empty( $all_levels[ $query_checkout_level ] ) ) {
$level_id = $query_checkout_level;
}

// If we still don't have a level, check if we have a level with that name.
if ( empty( $level_id ) ) {
$lowercase_query_checkout_level = strtolower( $query_checkout_level );
foreach ( $all_levels as $level ) {
if ( strtolower( urlencode( $level->name ) ) === $lowercase_query_checkout_level ) {
$level_id = $level->id;
break;
}
}
}
}

// no level, check for a default level in the custom fields for this post
if ( empty( $level_id ) && ! empty( $post ) ) {

Expand Down
9 changes: 9 additions & 0 deletions includes/upgradecheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@ function pmpro_checkForUpgrades()
pmpro_upgrade_2_10();
pmpro_setOption( 'db_version', '2.95' );
}

/**
* Version 2.11
* Unless we push this feature to 3.0, implementing pretty permalinks for checkout so we need to flush the rewrite rules.
*/
if ( $pmpro_db_version < 2.96 ) { // 2.96 since 2.11 would be lower than previous update.
flush_rewrite_rules();
pmpro_setOption( 'db_version', '2.96' );
}
}

function pmpro_db_delta()
Expand Down