Aegir_

Override parse_request() WP

Sep 20th, 2013
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Solution d'override de la fonction parse_request du tuto référecement WP et Ajax SEOMix
  2. // URL du tuto : http://www.seomix.fr/referencement-ajax/
  3. //
  4. // Site encore en test, donc pas encore tester sur SE
  5. // Code à placer dans le fichiers function.php du thème WP
  6.  
  7. add_action("parse_request", 'my_parse_request');
  8. function my_parse_request($extra_query_vars = '' ){
  9.     global $wp_rewrite;
  10.  
  11.     $extra_query_vars->query_vars = array();
  12.     $post_type_query_vars = array();
  13.  
  14.     // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
  15.  
  16.     // Fetch the rewrite rules.
  17.     $rewrite = $wp_rewrite->wp_rewrite_rules();
  18.  
  19.     if ( ! empty($rewrite) ) {
  20.         // If we match a rewrite rule, this will be cleared.
  21.         $error = '404';
  22.         $extra_query_vars->did_permalink = true;
  23.  
  24.         if ( isset($_SERVER['PATH_INFO']) )
  25.             $pathinfo = $_SERVER['PATH_INFO'];
  26.         else
  27.             $pathinfo = '';
  28.         $pathinfo_array = explode('?', $pathinfo);
  29.         $pathinfo       = str_replace("%", "%25", $pathinfo_array[0]);
  30.         $req_uri        = str_replace('?_escaped_fragment_=','',$_SERVER['REQUEST_URI']); //iciiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
  31.         $req_uri_array  = explode('?', $req_uri);
  32.         $req_uri        = $req_uri_array[0];
  33.         $self           = $_SERVER['PHP_SELF'];
  34.         $home_path      = parse_url(home_url());
  35.         if ( isset($home_path['path']) )
  36.             $home_path = $home_path['path'];
  37.         else
  38.             $home_path = '';
  39.         $home_path = trim($home_path, '/');
  40.  
  41.         // Trim path info from the end and the leading home path from the
  42.         // front.  For path info requests, this leaves us with the requesting
  43.         // filename, if any.  For 404 requests, this leaves us with the
  44.         // requested permalink.
  45.         $req_uri  = str_replace($pathinfo, '', $req_uri);
  46.         $req_uri  = trim($req_uri, '/');
  47.         $req_uri  = preg_replace("|^$home_path|", '', $req_uri);
  48.         $req_uri  = trim($req_uri, '/');
  49.         $pathinfo = trim($pathinfo, '/');
  50.         $pathinfo = preg_replace("|^$home_path|", '', $pathinfo);
  51.         $pathinfo = trim($pathinfo, '/');
  52.         $self     = trim($self, '/');
  53.         $self     = preg_replace("|^$home_path|", '', $self);
  54.         $self     = trim($self, '/');
  55.  
  56.         // The requested permalink is in $pathinfo for path info requests and
  57.         //  $req_uri for other requests.
  58.         if ( ! empty($pathinfo) && !preg_match('|^.*' . $wp_rewrite->index . '$|', $pathinfo) ) {
  59.             $request = $pathinfo;
  60.         } else {
  61.             // If the request uri is the index, blank it out so that we don't try to match it against a rule.
  62.             if ( $req_uri == $wp_rewrite->index )
  63.                 $req_uri = '';
  64.             $request = $req_uri;
  65.         }
  66.  
  67.         $extra_query_vars->request = $request;
  68.  
  69.         // Look for matches.
  70.         $request_match = $request;
  71.         foreach ( (array) $rewrite as $match => $query) {
  72.             // Don't try to match against AtomPub calls
  73.             if ( $req_uri == 'wp-app.php' )
  74.                 break;
  75.  
  76.             // If the requesting file is the anchor of the match, prepend it
  77.             // to the path info.
  78.             if ( (! empty($req_uri)) && (strpos($match, $req_uri) === 0) && ($req_uri != $request) )
  79.                 $request_match = $req_uri . '/' . $request;
  80.  
  81.             if ( preg_match("#^$match#", $request_match, $matches) ||
  82.                 preg_match("#^$match#", urldecode($request_match), $matches) ) {
  83.                 // Got a match.
  84.                 $extra_query_vars->matched_rule = $match;
  85.  
  86.                 // Trim the query of everything up to the '?'.
  87.                 $query = preg_replace("!^.+\?!", '', $query);
  88.  
  89.                 // Substitute the substring matches into the query.
  90.                 $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
  91.  
  92.                 $extra_query_vars->matched_query = $query;
  93.  
  94.                 // Parse the query.
  95.                 parse_str($query, $perma_query_vars);
  96.  
  97.                 // If we're processing a 404 request, clear the error var
  98.                 // since we found something.
  99.                 if ( isset($_GET['error']) )
  100.                     unset($_GET['error']);
  101.                 if ( isset($error) )
  102.                     unset($error);
  103.                 break;
  104.             }
  105.         }
  106.  
  107.         // If req_uri is empty or if it is a request for ourself, unset error.
  108.         if ( empty($request) || $req_uri == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) {
  109.             if ( isset($_GET['error']) )
  110.                 unset($_GET['error']);
  111.             if ( isset($error) )
  112.                 unset($error);
  113.             if ( isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false )
  114.                 unset($perma_query_vars);
  115.             $extra_query_vars->did_permalink = false;
  116.         }
  117.     }
  118.  
  119.     $extra_query_vars->public_query_vars = apply_filters('query_vars', $extra_query_vars->public_query_vars);
  120.  
  121.     foreach ( $GLOBALS['wp_post_types'] as $post_type => $t )
  122.         if ( $t->query_var )
  123.             $post_type_query_vars[$t->query_var] = $post_type;
  124.  
  125.     foreach ( $extra_query_vars->public_query_vars as $wpvar ) {
  126.         if ( isset( $extra_query_vars->extra_query_vars[$wpvar] ) )
  127.             $extra_query_vars->query_vars[$wpvar] = $extra_query_vars->extra_query_vars[$wpvar];
  128.         elseif ( isset( $_POST[$wpvar] ) )
  129.             $extra_query_vars->query_vars[$wpvar] = $_POST[$wpvar];
  130.         elseif ( isset( $_GET[$wpvar] ) )
  131.             $extra_query_vars->query_vars[$wpvar] = $_GET[$wpvar];
  132.         elseif ( isset( $perma_query_vars[$wpvar] ) )
  133.             $extra_query_vars->query_vars[$wpvar] = $perma_query_vars[$wpvar];
  134.  
  135.         if ( !empty( $extra_query_vars->query_vars[$wpvar] ) ) {
  136.             if ( ! is_array( $extra_query_vars->query_vars[$wpvar] ) ) {
  137.                 $extra_query_vars->query_vars[$wpvar] = (string) $extra_query_vars->query_vars[$wpvar];
  138.             } else {
  139.                 foreach ( $extra_query_vars->query_vars[$wpvar] as $vkey => $v ) {
  140.                     if ( !is_object( $v ) ) {
  141.                         $extra_query_vars->query_vars[$wpvar][$vkey] = (string) $v;
  142.                     }
  143.                 }
  144.             }
  145.  
  146.             if ( isset($post_type_query_vars[$wpvar] ) ) {
  147.                 $extra_query_vars->query_vars['post_type'] = $post_type_query_vars[$wpvar];
  148.                 $extra_query_vars->query_vars['name'] = $extra_query_vars->query_vars[$wpvar];
  149.             }
  150.         }
  151.     }
  152.  
  153.     // Convert urldecoded spaces back into +
  154.     foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t )
  155.         if ( $t->query_var && isset( $extra_query_vars->query_vars[$t->query_var] ) )
  156.             $extra_query_vars->query_vars[$t->query_var] = str_replace( ' ', '+', $extra_query_vars->query_vars[$t->query_var] );
  157.  
  158.     // Limit publicly queried post_types to those that are publicly_queryable
  159.     if ( isset( $extra_query_vars->query_vars['post_type']) ) {
  160.         $queryable_post_types = get_post_types( array('publicly_queryable' => true) );
  161.         if ( ! is_array( $extra_query_vars->query_vars['post_type'] ) ) {
  162.             if ( ! in_array( $extra_query_vars->query_vars['post_type'], $queryable_post_types ) )
  163.                 unset( $extra_query_vars->query_vars['post_type'] );
  164.         } else {
  165.             $extra_query_vars->query_vars['post_type'] = array_intersect( $extra_query_vars->query_vars['post_type'], $queryable_post_types );
  166.         }
  167.     }
  168.     foreach ( (array) $extra_query_vars->private_query_vars as $var) {
  169.         if ( isset($extra_query_vars->extra_query_vars[$var]) )
  170.             $extra_query_vars->query_vars[$var] = $extra_query_vars->extra_query_vars[$var];
  171.     }
  172.     if ( isset($error) )
  173.         $extra_query_vars->query_vars['error'] = $error;
  174.  
  175.     $extra_query_vars->query_vars = apply_filters('request', $extra_query_vars->query_vars);
  176.     do_action_ref_array('my_parse_request', &$extra_query_vars->extra_query_vars);
  177. }
RAW Paste Data