[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Defines GlotPress rewrite rules and query vars. 4 * 5 * @package GlotPress 6 * @subpackage Rewrite 7 */ 8 9 10 /** 11 * Generate the WP rewrite rules. 12 * 13 * @since 1.0.0 14 * 15 * @param string|bool $gp_base Optional. The base of all GlotPress URLs. 16 * Defaults to the `GP_URL_BASE` constant. 17 * @return array Rewrite rules that transform the URL structure 18 * to a set of query vars 19 */ 20 function gp_generate_rewrite_rules( $gp_base = false ) { 21 if ( false === $gp_base ) { 22 $gp_base = trim( gp_url_base_path(), '/' ); 23 } 24 25 $rules = array(); 26 if ( ! $gp_base ) { 27 $rules['$'] = 'index.php?gp_route'; 28 $rules['^(.*)$'] = 'index.php?gp_route=$matches[1]'; 29 } else { 30 $rules[ '^' . $gp_base . '$' ] = 'index.php?gp_route'; 31 $rules[ '^' . $gp_base . '\/+(.*)$' ] = 'index.php?gp_route=$matches[1]'; 32 } 33 34 return $rules; 35 } 36 37 /** 38 * Add WP rewrite rules to avoid WP thinking that GP pages are 404. 39 * 40 * @since 1.0.0 41 */ 42 function gp_rewrite_rules() { 43 $rewrite_rules = gp_generate_rewrite_rules(); 44 foreach ( $rewrite_rules as $regex => $query ) { 45 add_rewrite_rule( $regex, $query, 'top' ); 46 } 47 48 /* 49 * Check to see if the rewrite rule has changed, if so, update the option 50 * and flush the rewrite rules. 51 * Save the rewrite rule to an option so we have something to compare against later. 52 * We don't need to worry about the root rewrite rule above as it is always the same. 53 */ 54 if ( get_option( 'gp_rewrite_rule' ) != $rewrite_rules ) { 55 update_option( 'gp_rewrite_rule', $rewrite_rules ); 56 flush_rewrite_rules( false ); 57 } 58 } 59 60 /** 61 * Query vars for GP rewrite rules 62 * 63 * @since 1.0.0 64 */ 65 function gp_query_vars( $query_vars ) { 66 $query_vars[] = 'gp_route'; 67 return $query_vars; 68 } 69 70 /** 71 * GP run route 72 * 73 * @since 1.0.0 74 */ 75 function gp_run_route() { 76 gp_populate_notices(); 77 78 if ( is_glotpress() ) { 79 GP::$router->route(); 80 } 81 } 82 83 /** 84 * Determine if the page requested is handled by GlotPress. 85 * 86 * @since 1.0.0 87 * 88 * @return bool Whether the request is handled by GlotPress. 89 */ 90 function is_glotpress() { 91 global $wp; 92 93 if ( ! is_admin() && GP_ROUTING && $wp instanceof WP && null !== $wp->query_vars && array_key_exists( 'gp_route', $wp->query_vars ) ) { 94 return true; 95 } 96 return false; 97 } 98 99 /** 100 * Sets `WP_Query->is_home` to false during GlotPress requests. 101 * 102 * @since 1.0.0 103 * 104 * @param WP_Query $query The WP_Query instance. 105 */ 106 function gp_set_is_home_false( $query ) { 107 if ( is_glotpress() && $query->is_home() && $query->is_main_query() ) { 108 $query->is_home = false; 109 } 110 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Dec 6 01:01:08 2024 | Cross-referenced by PHPXref 0.7.1 |