Last active
July 28, 2017 06:56
-
-
Save seriusokhatsky/8fad016ac7490729baeb4a585b92d634 to your computer and use it in GitHub Desktop.
WP rewrite permalinks demos
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
public function wp_rewrite_rules() { | |
$this->rules = get_option('rewrite_rules'); | |
if ( empty($this->rules) ) { | |
$this->matches = 'matches'; | |
$this->rewrite_rules(); | |
if ( ! did_action( 'wp_loaded' ) ) { | |
add_action( 'wp_loaded', array( $this, 'flush_rules' ) ); | |
return $this->rules; | |
} | |
update_option('rewrite_rules', $this->rules); | |
} | |
// ar($this->rules ); | |
$old_rules = $this->rules; | |
$this->rules = array(); | |
$force_add = array('shop/?$', 'portfolio/?$'); | |
$reverse_order_keys = array( | |
'order-pay', | |
'order-received', | |
'orders', | |
'view-order', | |
'downloads', | |
'edit-account', | |
'edit-address', | |
'payment-methods', | |
'lost-password', | |
'customer-logout', | |
'add-payment-method', | |
'delete-payment-method', | |
'set-default-payment-method', | |
); | |
foreach ( $old_rules as $key => $value) { | |
$last_index = (int) substr($value, -2, 1); | |
$new_index = $last_index + 1; | |
$new_route_after = false; //!( strpos($key, 'product-category') !== false && strpos($key, 'page') !== false ); | |
$found = false; | |
if( $new_route_after ) $this->rules[ $key ] = $value; | |
foreach ($reverse_order_keys as $val) { | |
if( ! strpos( $key, $val ) ) continue; | |
$key_parts = explode($val, $key); | |
$value_parts = explode($val, $value); | |
$new_key = $key_parts[0] . 'demo/([^/]+)/' . $val . $key_parts[1]; | |
$new_value = $value_parts[0] .'&demo=$matches[' . $last_index . ']' . '&' . $val . '=$matches[' . $new_index . ']'; | |
$this->rules[ $new_key ] = $new_value; | |
} | |
if( ! $found && strpos($key, 'page/') !== false ) { | |
$key_parts = explode('page/', $key); | |
$value_parts = explode('&paged=', $value); | |
$new_key = $key_parts[0] . 'demo/([^/]+)/' . 'page/' . $key_parts[1]; | |
$new_value = $value_parts[0] .'&demo=$matches[' . $last_index . ']' . '&paged=$matches[' . $new_index . ']'; | |
$this->rules[ $new_key ] = $new_value; | |
} else if( ! $found && ( $last_index > 0 && substr($key, -2, 2) == '?$' || in_array( $key, $force_add ) ) ) { | |
$new_key = substr($key, 0, -2) . 'demo/([^/]+)/' . '?$'; | |
$new_value = $value . '&demo=$matches[' . $new_index . ']'; | |
$this->rules[ $new_key ] = $new_value; | |
} | |
if( ! $new_route_after ) $this->rules[ $key ] = $value; | |
} | |
// $this->rules[ 'demo/([^/]+)/?$' ] = 'index.php?demo=$matches[1]'; | |
// ar($this->rules); | |
return $this->rules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment