[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/ -> rewrite.php (source)

   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 ( $rewrite_rules != get_option( 'gp_rewrite_rule' ) ) {
  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      global $wp;
  79  
  80      if ( is_glotpress() ) {
  81          GP::$router->route();
  82      }
  83  }
  84  
  85  /**
  86   * Determine if the page requested is handled by GlotPress.
  87   *
  88   * @since 1.0.0
  89   *
  90   * @return bool Whether the request is handled by GlotPress.
  91   */
  92  function is_glotpress() {
  93      global $wp;
  94  
  95      if ( ! is_admin() && GP_ROUTING && $wp instanceof WP && null !== $wp->query_vars && array_key_exists( 'gp_route', $wp->query_vars ) ) {
  96          return true;
  97      }
  98      return false;
  99  }
 100  
 101  /**
 102   * Sets `WP_Query->is_home` to false during GlotPress requests.
 103   *
 104   * @since 1.0.0
 105   *
 106   * @param WP_Query $query The WP_Query instance.
 107   */
 108  function gp_set_is_home_false( $query ) {
 109      if ( is_glotpress() && $query->is_home() && $query->is_main_query() ) {
 110          $query->is_home = false;
 111      }
 112  }


Generated: Tue Jan 22 01:02:31 2019 Cross-referenced by PHPXref 0.7.1