/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/inc/class-rewrite.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Frontend
 */

/**
 * This code handles the category rewrites.
 */
class WPSEO_Rewrite {

    
/**
     * Class constructor.
     */
    
public function __construct() {
        
add_filter'query_vars', array( $this'query_vars' ) );
        
add_filter'category_link', array( $this'no_category_base' ) );
        
add_filter'request', array( $this'request' ) );
        
add_filter'category_rewrite_rules', array( $this'category_rewrite_rules' ) );

        
add_action'created_category', array( $this'schedule_flush' ) );
        
add_action'edited_category', array( $this'schedule_flush' ) );
        
add_action'delete_category', array( $this'schedule_flush' ) );

        
add_action'init', array( $this'flush' ), 999 );
    }

    
/**
     * Save an option that triggers a flush on the next init.
     *
     * @since 1.2.8
     */
    
public function schedule_flush() {
        
update_option'wpseo_flush_rewrite');
    }

    
/**
     * If the flush option is set, flush the rewrite rules.
     *
     * @since 1.2.8
     *
     * @return bool
     */
    
public function flush() {
        if ( 
get_option'wpseo_flush_rewrite' ) ) {

            
add_action'shutdown''flush_rewrite_rules' );
            
delete_option'wpseo_flush_rewrite' );

            return 
true;
        }

        return 
false;
    }

    
/**
     * Override the category link to remove the category base.
     *
     * @param string $link Unused, overridden by the function.
     *
     * @return string
     */
    
public function no_category_base$link ) {
        
$category_base get_option'category_base' );

        if ( empty( 
$category_base ) ) {
            
$category_base 'category';
        }

        
/*
         * Remove initial slash, if there is one (we remove the trailing slash
         * in the regex replacement and don't want to end up short a slash).
         */
        
if ( '/' === substr$category_base0) ) {
            
$category_base substr$category_base);
        }

        
$category_base .= '/';

        return 
preg_replace'`' preg_quote$category_base'`' ) . '`u'''$link);
    }

    
/**
     * Update the query vars with the redirect var when stripcategorybase is active.
     *
     * @param array $query_vars Main query vars to filter.
     *
     * @return array
     */
    
public function query_vars$query_vars ) {
        if ( 
WPSEO_Options::get'stripcategorybase' ) === true ) {
            
$query_vars[] = 'wpseo_category_redirect';
        }

        return 
$query_vars;
    }

    
/**
     * Checks whether the redirect needs to be created.
     *
     * @param array $query_vars Query vars to check for existence of redirect var.
     *
     * @return array|void The query vars.
     */
    
public function request$query_vars ) {
        if ( ! isset( 
$query_vars['wpseo_category_redirect'] ) ) {
            return 
$query_vars;
        }

        
$this->redirect$query_vars['wpseo_category_redirect'] );
    }

    
/**
     * This function taken and only slightly adapted from WP No Category Base plugin by Saurabh Gupta.
     *
     * @return array
     */
    
public function category_rewrite_rules() {
        global 
$wp_rewrite;

        
$category_rewrite = array();

        
$taxonomy            get_taxonomy'category' );
        
$permalink_structure get_option'permalink_structure' );

        
$blog_prefix '';
        if ( 
is_multisite() && ! is_subdomain_install() && is_main_site() && === strpos$permalink_structure'/blog/' ) ) {
            
$blog_prefix 'blog/';
        }

        
$categories get_categories( array( 'hide_empty' => false ) );
        if ( 
is_array$categories ) && $categories !== array() ) {
            foreach ( 
$categories as $category ) {
                
$category_nicename $category->slug;
                if ( 
$category->parent == $category->cat_ID ) {
                    
// Recursive recursion.
                    
$category->parent 0;
                }
                elseif ( 
$taxonomy->rewrite['hierarchical'] != && $category->parent !== ) {
                        
$parents get_category_parents$category->parentfalse'/'true );
                    if ( ! 
is_wp_error$parents ) ) {
                        
$category_nicename $parents $category_nicename;
                    }
                    unset( 
$parents );
                }

                
$category_rewrite $this->add_category_rewrites$category_rewrite$category_nicename$blog_prefix$wp_rewrite->pagination_base );

                
// Adds rules for the uppercase encoded URIs.
                
$category_nicename_filtered $this->convert_encoded_to_upper$category_nicename );

                if ( 
$category_nicename_filtered !== $category_nicename ) {
                    
$category_rewrite $this->add_category_rewrites$category_rewrite$category_nicename_filtered$blog_prefix$wp_rewrite->pagination_base );
                }
            }
            unset( 
$categories$category$category_nicename$category_nicename_filtered );
        }

        
// Redirect support from Old Category Base.
        
$old_base                            $wp_rewrite->get_category_permastruct();
        
$old_base                            str_replace'%category%''(.+)'$old_base );
        
$old_base                            trim$old_base'/' );
        
$category_rewrite$old_base '$' ] = 'index.php?wpseo_category_redirect=$matches[1]';

        return 
$category_rewrite;
    }

    
/**
     * Adds required category rewrites rules.
     *
     * @param array  $rewrites        The current set of rules.
     * @param string $category_name   Category nicename.
     * @param string $blog_prefix     Multisite blog prefix.
     * @param string $pagination_base WP_Query pagination base.
     *
     * @return array The added set of rules.
     */
    
protected function add_category_rewrites$rewrites$category_name$blog_prefix$pagination_base ) {
        
$rewrite_name $blog_prefix '(' $category_name ')';

        
$rewrites$rewrite_name '/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ]    = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        
$rewrites$rewrite_name '/' $pagination_base '/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        
$rewrites$rewrite_name '/?$' ]                                       = 'index.php?category_name=$matches[1]';

        return 
$rewrites;
    }

    
/**
     * Walks through category nicename and convert encoded parts
     * into uppercase using $this->encode_to_upper().
     *
     * @param string $name The encoded category URI string.
     *
     * @return string The convered URI string.
     */
    
protected function convert_encoded_to_upper$name ) {
        
// Checks if name has any encoding in it.
        
if ( strpos$name'%' ) === false ) {
            return 
$name;
        }

        
$names explode'/'$name );
        
$names array_map( array( $this'encode_to_upper' ), $names );

        return 
implode'/'$names );
    }

    
/**
     * Converts the encoded URI string to uppercase.
     *
     * @param string $encoded The encoded string.
     *
     * @return string The uppercased string.
     */
    
public function encode_to_upper$encoded ) {
        if ( 
strpos$encoded'%' ) === false ) {
            return 
$encoded;
        }

        return 
strtoupper$encoded );
    }

    
/**
     * Redirect the "old" category URL to the new one.
     *
     * @codeCoverageIgnore
     *
     * @param string $category_redirect The category page to redirect to.
     * @return void
     */
    
protected function redirect$category_redirect ) {
        
$catlink trailingslashitget_option'home' ) ) . user_trailingslashit$category_redirect'category' );

        
header'X-Redirect-By: Yoast SEO' );
        
wp_redirect$catlink301'Yoast SEO' );
        exit;
    }
/* End of class */