| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
function wppb_toolbox_flush_rewrite_rules() { |
| 6 |
$base = wppb_toolbox_get_settings( 'userlisting', 'modify-permalinks-single' ); |
| 7 |
|
| 8 |
if ( $base == false ) return; |
| 9 |
|
| 10 |
$rules = get_option( 'rewrite_rules' ); |
| 11 |
$frontpage_id = get_option( 'page_on_front' ); |
| 12 |
$lang_pattern = function_exists( 'wppb_userlisting_get_language_pattern' ) ? wppb_userlisting_get_language_pattern() : ''; |
| 13 |
|
| 14 |
$needs_flush = ( |
| 15 |
!isset($rules['(.+?)/'.$base.'/([^/]+)']) || |
| 16 |
!isset($rules['(.?.+?)/' . wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$'] ) || |
| 17 |
( !empty( $frontpage_id ) && !isset( $rules[wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$'] ) ) |
| 18 |
); |
| 19 |
|
| 20 |
if ( ! $needs_flush && $lang_pattern !== '' && ! isset( $rules[ $lang_pattern . '/(.+?)/' . $base . '/([^/]+)' ] ) ) { |
| 21 |
$needs_flush = true; |
| 22 |
} |
| 23 |
|
| 24 |
if ( $needs_flush ) { |
| 25 |
global $wp_rewrite; |
| 26 |
$wp_rewrite->flush_rules(); |
| 27 |
} |
| 28 |
} |
| 29 |
add_action( 'wp_loaded', 'wppb_toolbox_flush_rewrite_rules' ); |
| 30 |
|
| 31 |
function wppb_toolbox_insert_userlisting_rule( $rules ) { |
| 32 |
$base = wppb_toolbox_get_settings( 'userlisting', 'modify-permalinks-single' ); |
| 33 |
|
| 34 |
if ( $base == false ) return $rules; |
| 35 |
|
| 36 |
$wppb_addonOptions = get_option('wppb_module_settings'); |
| 37 |
|
| 38 |
if( $wppb_addonOptions['wppb_userListing'] == 'show' ) { |
| 39 |
$new_rules = array(); |
| 40 |
$frontpage_id = get_option('page_on_front'); |
| 41 |
$lang_pattern = function_exists( 'wppb_userlisting_get_language_pattern' ) ? wppb_userlisting_get_language_pattern() : ''; |
| 42 |
|
| 43 |
// Polylang compatibility: capture the language slug separately so the pagename only |
| 44 |
// contains the actual page slug. |
| 45 |
if ( $lang_pattern !== '' ) { |
| 46 |
$new_rules[ $lang_pattern . '/(.+?)/' . $base . '/([^/]+)' ] = 'index.php?lang=$matches[1]&pagename=$matches[2]&username=$matches[3]'; |
| 47 |
|
| 48 |
if ( !empty($frontpage_id) ) { |
| 49 |
$new_rules[ $lang_pattern . '/' . wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$' ] = 'index.php?lang=$matches[1]&page_id=' . $frontpage_id . '&wppb_page=$matches[2]'; |
| 50 |
} |
| 51 |
|
| 52 |
$new_rules[ $lang_pattern . '/(.?.+?)/' . wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$' ] = 'index.php?lang=$matches[1]&pagename=$matches[2]&wppb_page=$matches[3]'; |
| 53 |
} |
| 54 |
|
| 55 |
//user rule |
| 56 |
$new_rules['(.+?)/'. $base .'/([^/]+)'] = 'index.php?pagename=$matches[1]&username=$matches[2]'; |
| 57 |
|
| 58 |
//users-page rule |
| 59 |
if (!empty($frontpage_id)) { |
| 60 |
$new_rules[wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$'] = 'index.php?&page_id=' . $frontpage_id . '&wppb_page=$matches[1]'; |
| 61 |
} |
| 62 |
|
| 63 |
$new_rules['(.?.+?)/' . wppb_get_users_pagination_slug() . '/?([0-9]{1,})/?$'] = 'index.php?pagename=$matches[1]&wppb_page=$matches[2]'; |
| 64 |
|
| 65 |
$rules = $new_rules + $rules; |
| 66 |
} |
| 67 |
|
| 68 |
return $rules; |
| 69 |
|
| 70 |
} |
| 71 |
add_filter( 'rewrite_rules_array', 'wppb_toolbox_insert_userlisting_rule' ); |
| 72 |
|
| 73 |
add_action('init', 'wppb_toolbox_remove_ul_rewrite_rules'); |
| 74 |
function wppb_toolbox_remove_ul_rewrite_rules() { |
| 75 |
remove_action( 'wp_loaded', 'wppb_flush_rewrite_rules' ); |
| 76 |
remove_filter( 'rewrite_rules_array', 'wppb_insert_userlisting_rule' ); |
| 77 |
} |
| 78 |
|
| 79 |
add_filter( 'wppb_userlisting_more_info_link_structure2', 'wppb_toolbox_modify_more_info_link', 20, 3 ); |
| 80 |
add_filter( 'wppb_userlisting_more_info_link_structure3', 'wppb_toolbox_modify_more_info_link', 20, 3 ); |
| 81 |
function wppb_toolbox_modify_more_info_link( $final_url, $url, $user_info ) { |
| 82 |
$base = wppb_toolbox_get_settings( 'userlisting', 'modify-permalinks-single' ); |
| 83 |
|
| 84 |
if ( $base == false ) return $final_url; |
| 85 |
|
| 86 |
if ( apply_filters( 'wppb_userlisting_get_user_by_id', true ) ) |
| 87 |
$new_url = trailingslashit( $url ) . $base . '/' . $user_info->ID; |
| 88 |
else |
| 89 |
$new_url = trailingslashit( $url ) . $base . '/' . $user_info; |
| 90 |
|
| 91 |
return $new_url; |
| 92 |
} |
| 93 |
|