| @@ -4,55 +4,55 @@ | ||
| 4 | 4 | |
| 5 | 5 | use WPDeveloper\BetterDocs\Utils\Base; |
| 6 | 6 | |
| 7 | 7 | class Request extends Base { |
| 8 | - /** | |
| 9 | - * Flag for already parsed or not | |
| 10 | - * | |
| 11 | - * Specially needed for those who don't update pro yet. | |
| 12 | - * @var boolean | |
| 13 | - */ | |
| 14 | - protected static $already_parsed = false; | |
| 8 | + /** | |
| 9 | + * Flag for already parsed or not | |
| 10 | + * | |
| 11 | + * Specially needed for those who don't update pro yet. | |
| 12 | + * @var boolean | |
| 13 | + */ | |
| 14 | + protected static $already_parsed = false; | |
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * List of BetterDocs Perma Structure | |
| 18 | - * @var array | |
| 19 | - */ | |
| 20 | - private $perma_structure = []; | |
| 16 | + /** | |
| 17 | + * List of BetterDocs Perma Structure | |
| 18 | + * @var array | |
| 19 | + */ | |
| 20 | + private $perma_structure = []; | |
| 21 | 21 | |
| 22 | - /** | |
| 23 | - * List of BetterDocs Query Vars Agains Page Structure. | |
| 24 | - * @var array | |
| 25 | - */ | |
| 26 | - private $query_vars = []; | |
| 22 | + /** | |
| 23 | + * List of BetterDocs Query Vars Agains Page Structure. | |
| 24 | + * @var array | |
| 25 | + */ | |
| 26 | + private $query_vars = []; | |
| 27 | 27 | |
| 28 | - /** | |
| 29 | - * List of Query Variables from $wp->query_vars. | |
| 30 | - * @var array | |
| 31 | - */ | |
| 32 | - private $wp_query_vars = []; | |
| 28 | + /** | |
| 29 | + * List of Query Variables from $wp->query_vars. | |
| 30 | + * @var array | |
| 31 | + */ | |
| 32 | + private $wp_query_vars = []; | |
| 33 | 33 | |
| 34 | - /** | |
| 35 | - * Rewrite Class Reference of BetterDocs | |
| 36 | - * @var Rewrite | |
| 37 | - */ | |
| 38 | - protected $rewrite; | |
| 34 | + /** | |
| 35 | + * Rewrite Class Reference of BetterDocs | |
| 36 | + * @var Rewrite | |
| 37 | + */ | |
| 38 | + protected $rewrite; | |
| 39 | 39 | |
| 40 | - /** | |
| 41 | - * Settings Class Reference of BetterDocs | |
| 42 | - * @var Settings | |
| 43 | - */ | |
| 44 | - protected $settings; | |
| 40 | + /** | |
| 41 | + * Settings Class Reference of BetterDocs | |
| 42 | + * @var Settings | |
| 43 | + */ | |
| 44 | + protected $settings; | |
| 45 | 45 | |
| 46 | - public function __construct( Rewrite $rewrite, Settings $settings ) { | |
| 47 | - $this->rewrite = $rewrite; | |
| 48 | - $this->settings = $settings; | |
| 49 | - } | |
| 46 | + public function __construct( Rewrite $rewrite, Settings $settings ) { | |
| 47 | + $this->rewrite = $rewrite; | |
| 48 | + $this->settings = $settings; | |
| 49 | + } | |
| 50 | 50 | |
| 51 | - public function init() { | |
| 52 | - if ( is_admin() ) { | |
| 53 | - return; | |
| 54 | - } | |
| 51 | + public function init() { | |
| 52 | + if ( is_admin() ) { | |
| 53 | + return; | |
| 54 | + } | |
| 55 | 55 | |
| 56 | 56 | $this->perma_structure = [ |
| 57 | 57 | 'is_docs' => trim( $this->rewrite->get_base_slug(), '/' ), |
| 58 | 58 | 'is_docs_feed' => trim( $this->rewrite->get_base_slug(), '/' ) . '/%feed%', |
| @@ -57,9 +57,10 @@ | ||
| 57 | 57 | 'is_docs' => trim( $this->rewrite->get_base_slug(), '/' ), |
| 58 | 58 | 'is_docs_feed' => trim( $this->rewrite->get_base_slug(), '/' ) . '/%feed%', |
| 59 | 59 | 'is_docs_category' => trim( $this->settings->get( 'category_slug', 'docs-category' ), '/' ) . '/%doc_category%', |
| 60 | 60 | 'is_docs_tag' => trim( $this->settings->get( 'tag_slug', 'docs-tag' ), '/' ) . '/%doc_tag%', |
| 61 | - 'is_single_docs' => trim( $this->settings->get( 'permalink_structure', 'docs' ), '/' ) . '/%name%' | |
| 61 | + 'is_single_docs' => trim( $this->settings->get( 'permalink_structure', 'docs' ), '/' ) . '/%name%', | |
| 62 | + 'is_docs_author' => trim( $this->rewrite->get_base_slug(), '/' ) . '/authors/%author%' | |
| 62 | 63 | ]; |
| 63 | 64 | |
| 64 | 65 | $this->query_vars = [ |
| 65 | 66 | 'is_docs' => ['post_type'], |
| @@ -65,43 +66,48 @@ | ||
| 65 | 66 | 'is_docs' => ['post_type'], |
| 66 | 67 | 'is_docs_feed' => ['doc_category'], |
| 67 | 68 | 'is_docs_category' => ['doc_category'], |
| 68 | 69 | 'is_docs_tag' => ['doc_tag'], |
| 69 | - 'is_single_docs' => ['name', 'docs', 'post_type'] | |
| 70 | + 'is_single_docs' => ['name', 'docs', 'post_type'], | |
| 71 | + 'is_docs_author' => ['post_type', 'author'] | |
| 70 | 72 | ]; |
| 71 | 73 | |
| 72 | - add_action( 'parse_request', [$this, 'parse'] ); | |
| 74 | + add_action( 'parse_request', [ $this, 'parse' ] ); | |
| 73 | 75 | |
| 74 | - /** | |
| 75 | - * This is for Backward compatibility if pro not updated. | |
| 76 | - */ | |
| 77 | - add_action( 'parse_request', [$this, 'backward_compability'], 11 ); | |
| 76 | + /** | |
| 77 | + * This is for Backward compatibility if pro not updated. | |
| 78 | + */ | |
| 79 | + add_action( 'parse_request', [ $this, 'backward_compability' ], 11 ); | |
| 78 | 80 | |
| 79 | - /** | |
| 80 | - * Make Compatible With Permalink Manager Plugin | |
| 81 | - */ | |
| 82 | - add_filter( 'permalink_manager_detected_element_id', [$this, 'provide_compatibility'], 10, 3 ); | |
| 83 | - } | |
| 81 | + /** | |
| 82 | + * Make Compatible With Permalink Manager Plugin | |
| 83 | + */ | |
| 84 | + add_filter( 'permalink_manager_detected_element_id', [ $this, 'provide_compatibility' ], 10, 3 ); | |
| 85 | + } | |
| 84 | 86 | |
| 85 | - public function provide_compatibility( $element_id, $uri_parts, $request_url ) { | |
| 86 | - if ( $request_url == $this->settings->get( 'docs_slug' ) ) { | |
| 87 | - $element_id = ''; | |
| 88 | - } | |
| 89 | - return $element_id; | |
| 90 | - } | |
| 87 | + public function provide_compatibility( $element_id, $uri_parts, $request_url ) { | |
| 88 | + if ( $request_url == $this->settings->get( 'docs_slug' ) ) { | |
| 89 | + $element_id = ''; | |
| 90 | + } | |
| 91 | + return $element_id; | |
| 92 | + } | |
| 91 | 93 | |
| 92 | - protected function is_docs( &$query_vars ) { | |
| 93 | - if ( ! $this->settings->get( 'builtin_doc_page', true ) ) { | |
| 94 | - $query_vars['post_type'] = 'page'; | |
| 95 | - $query_vars['name'] = trim( $this->rewrite->get_base_slug(), '/' ); | |
| 96 | - } | |
| 94 | + protected function is_docs( &$query_vars ) { | |
| 95 | + if ( ! $this->settings->get( 'builtin_doc_page', true ) ) { | |
| 96 | + $query_vars['post_type'] = 'page'; | |
| 97 | + $query_vars['name'] = trim( $this->rewrite->get_base_slug(), '/' ); | |
| 98 | + } | |
| 97 | 99 | |
| 98 | - return $query_vars; | |
| 99 | - } | |
| 100 | + return $query_vars; | |
| 101 | + } | |
| 100 | 102 | |
| 101 | - public function is_docs_feed( $query_vars ) { | |
| 102 | - global $wp_rewrite; | |
| 103 | - return isset( $query_vars['feed'] ) && in_array( $query_vars['feed'], $wp_rewrite->feeds ); | |
| 103 | + public function is_docs_feed( $query_vars ) { | |
| 104 | + global $wp_rewrite; | |
| 105 | + return isset( $query_vars['feed'] ) && in_array( $query_vars['feed'], $wp_rewrite->feeds ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + public function is_docs_author( $query_vars ) { | |
| 109 | + return isset( $query_vars['author'] ) ? true : false; | |
| 104 | 110 | } |
| 105 | 111 | |
| 106 | 112 | protected function is_single_docs( $query_vars ) { |
| 107 | 113 | if ( ! isset( $query_vars['name'] ) ) { |
| @@ -107,73 +113,75 @@ | ||
| 107 | 113 | if ( ! isset( $query_vars['name'] ) ) { |
| 108 | 114 | return false; |
| 109 | 115 | } |
| 110 | 116 | |
| 111 | - global $wpdb; | |
| 112 | - $name = $query_vars['name']; | |
| 117 | + global $wpdb; | |
| 118 | + $name = $query_vars['name']; | |
| 113 | 119 | |
| 114 | - $_post_id = (int) $wpdb->get_var( | |
| 115 | - $wpdb->prepare( | |
| 116 | - "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s LIMIT 1", | |
| 117 | - esc_sql( $name ), | |
| 118 | - 'docs' | |
| 119 | - ) | |
| 120 | - ); | |
| 120 | + $_post_id = (int) $wpdb->get_var( | |
| 121 | + $wpdb->prepare( | |
| 122 | + "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = %s LIMIT 1", | |
| 123 | + esc_sql( $name ), | |
| 124 | + 'docs' | |
| 125 | + ) | |
| 126 | + ); | |
| 121 | 127 | |
| 122 | - return $_post_id > 0; | |
| 123 | - } | |
| 128 | + return $_post_id > 0; | |
| 129 | + } | |
| 124 | 130 | |
| 125 | - protected function is_docs_category( $query_vars ) { | |
| 126 | - return $this->term_exists( $query_vars, 'doc_category' ); | |
| 127 | - } | |
| 131 | + protected function is_docs_category( $query_vars ) { | |
| 132 | + return $this->term_exists( $query_vars, 'doc_category' ); | |
| 133 | + } | |
| 128 | 134 | |
| 129 | - protected function is_docs_tag( $query_vars ) { | |
| 130 | - return $this->term_exists( $query_vars, 'doc_tag' ); | |
| 131 | - } | |
| 135 | + protected function is_docs_tag( $query_vars ) { | |
| 136 | + return $this->term_exists( $query_vars, 'doc_tag' ); | |
| 137 | + } | |
| 132 | 138 | |
| 133 | - protected function term_exists( $query_vars, $taxonomy ) { | |
| 134 | - if ( ! isset( $query_vars[$taxonomy] ) ) { | |
| 135 | - return false; | |
| 136 | - } | |
| 139 | + protected function term_exists( $query_vars, $taxonomy ) { | |
| 140 | + if ( ! isset( $query_vars[ $taxonomy ] ) ) { | |
| 141 | + return false; | |
| 142 | + } | |
| 137 | 143 | |
| 138 | - return term_exists( $query_vars[$taxonomy], $taxonomy ); | |
| 139 | - } | |
| 144 | + return term_exists( $query_vars[ $taxonomy ], $taxonomy ); | |
| 145 | + } | |
| 140 | 146 | |
| 141 | - public function set_perma_structure( $structures = [] ) { | |
| 142 | - $this->perma_structure = array_merge( $this->perma_structure, $structures ); | |
| 143 | - } | |
| 147 | + public function set_perma_structure( $structures = [] ) { | |
| 148 | + $this->perma_structure = array_merge( $this->perma_structure, $structures ); | |
| 149 | + } | |
| 144 | 150 | |
| 145 | - public function set_query_vars( $query_vars = [] ) { | |
| 146 | - $this->query_vars = array_merge( $this->query_vars, $query_vars ); | |
| 147 | - } | |
| 151 | + public function set_query_vars( $query_vars = [] ) { | |
| 152 | + $this->query_vars = array_merge( $this->query_vars, $query_vars ); | |
| 153 | + } | |
| 148 | 154 | |
| 149 | - public function backward_compability( $wp ) { | |
| 150 | - if ( static::$already_parsed ) { | |
| 151 | - return; | |
| 152 | - } | |
| 155 | + public function backward_compability( $wp ) { | |
| 156 | + if ( static::$already_parsed ) { | |
| 157 | + return; | |
| 158 | + } | |
| 153 | 159 | |
| 154 | - $this->permalink_magic( $wp ); | |
| 155 | - } | |
| 160 | + $this->permalink_magic( $wp ); | |
| 161 | + } | |
| 156 | 162 | |
| 157 | - public function parse( $wp ) { | |
| 158 | - static::$already_parsed = true; | |
| 163 | + public function parse( $wp ) { | |
| 164 | + static::$already_parsed = true; | |
| 159 | 165 | |
| 166 | + $this->perma_structure = apply_filters('docs_rewrite_rules', $this->perma_structure); | |
| 167 | + | |
| 160 | 168 | $this->permalink_magic( $wp ); |
| 161 | 169 | } |
| 162 | 170 | |
| 163 | - protected function permalink_magic( $wp ) { | |
| 164 | - $this->wp_query_vars = $wp->query_vars; | |
| 171 | + protected function permalink_magic( $wp ) { | |
| 172 | + $this->wp_query_vars = $wp->query_vars; | |
| 165 | 173 | |
| 166 | - if ( ! empty( $this->perma_structure ) ) { | |
| 167 | - $_valid = []; | |
| 174 | + if ( ! empty( $this->perma_structure ) ) { | |
| 175 | + $_valid = []; | |
| 168 | 176 | |
| 169 | - foreach ( $this->perma_structure as $_type => $structure ) { | |
| 170 | - $_perma_vars = $this->is_perma_valid_for( $structure, $wp->request ); | |
| 177 | + foreach ( $this->perma_structure as $_type => $structure ) { | |
| 178 | + $_perma_vars = $this->is_perma_valid_for( $structure, $wp->request ); | |
| 171 | 179 | |
| 172 | 180 | // $_valid = empty( $_valid ) && $_perma_vars ? [ 'type' => $_type, 'query_vars' => $_perma_vars ] : $_valid; |
| 173 | - if ( ( $_perma_vars && method_exists( $this, $_type ) && call_user_func_array( [$this, $_type], [ & $_perma_vars] ) ) ) { | |
| 181 | + if ( ( $_perma_vars && method_exists( $this, $_type ) && call_user_func_array( [$this, $_type], [ & $_perma_vars] ) ) ) { | |
| 174 | 182 | // dump( $_type, $_perma_vars ); |
| 175 | - if ( $_type === 'is_single_docs' || $_type == 'is_docs_feed' ) { | |
| 183 | + if ( $_type === 'is_single_docs' || $_type == 'is_docs_feed' || $_type == 'is_docs_author' ) { | |
| 176 | 184 | $_perma_vars['post_type'] = 'docs'; |
| 177 | 185 | } |
| 178 | 186 | $_valid = ['type' => $_type, 'query_vars' => $_perma_vars]; |
| 179 | 187 | } |
| @@ -178,66 +186,74 @@ | ||
| 178 | 186 | $_valid = ['type' => $_type, 'query_vars' => $_perma_vars]; |
| 179 | 187 | } |
| 180 | 188 | } |
| 181 | 189 | |
| 182 | - $type = isset( $_valid['type'] ) ? $_valid['type'] : ''; | |
| 183 | - $query_vars = isset( $_valid['query_vars'] ) ? $_valid['query_vars'] : []; | |
| 190 | + $type = isset( $_valid['type'] ) ? $_valid['type'] : ''; | |
| 191 | + $query_vars = isset( $_valid['query_vars'] ) ? $_valid['query_vars'] : []; | |
| 184 | 192 | |
| 185 | - if ( ! empty( $type ) ) { | |
| 186 | - unset( $this->query_vars[$type] ); | |
| 187 | - array_map( function ( $_vars ) use ( &$wp ) { | |
| 188 | - array_map( function ( $_var ) use ( &$wp ) { | |
| 189 | - unset( $wp->query_vars[$_var] ); | |
| 190 | - }, $_vars ); | |
| 191 | - }, $this->query_vars ); | |
| 192 | - } | |
| 193 | + if ( ! empty( $type ) ) { | |
| 194 | + unset( $this->query_vars[ $type ] ); | |
| 195 | + array_map( | |
| 196 | + function ( $_vars ) use ( &$wp ) { | |
| 197 | + array_map( | |
| 198 | + function ( $_var ) use ( &$wp ) { | |
| 199 | + unset( $wp->query_vars[ $_var ] ); | |
| 200 | + }, | |
| 201 | + $_vars | |
| 202 | + ); | |
| 203 | + }, | |
| 204 | + $this->query_vars | |
| 205 | + ); | |
| 206 | + } | |
| 193 | 207 | |
| 194 | 208 | $wp->query_vars = is_array( $query_vars ) ? array_merge( $wp->query_vars, $query_vars ) : $wp->query_vars; |
| 195 | - //var_dump($_valid); | |
| 196 | 209 | // Fallback |
| 197 | - if( ! empty( $_valid ) ) { | |
| 210 | + if ( ! empty( $_valid ) ) { | |
| 198 | 211 | unset( $wp->query_vars['attachment'] ); |
| 199 | 212 | } |
| 200 | 213 | } |
| 201 | 214 | } |
| 202 | 215 | |
| 203 | - /** | |
| 204 | - * This method is responsible for checking a structure is valid again a request. | |
| 205 | - * | |
| 206 | - * @param string $structure | |
| 207 | - * @param string $request | |
| 208 | - * @return array|bool | |
| 209 | - */ | |
| 210 | - private function is_perma_valid_for( $structure, $request ) { | |
| 211 | - if ( empty( $structure ) ) { | |
| 212 | - return false; | |
| 213 | - } | |
| 216 | + /** | |
| 217 | + * This method is responsible for checking a structure is valid again a request. | |
| 218 | + * | |
| 219 | + * @param string $structure | |
| 220 | + * @param string $request | |
| 221 | + * @return array|bool | |
| 222 | + */ | |
| 223 | + private function is_perma_valid_for( $structure, $request ) { | |
| 224 | + if ( empty( $structure ) ) { | |
| 225 | + return false; | |
| 226 | + } | |
| 214 | 227 | |
| 215 | - $_tags = explode( '/', trim( $structure, '/' ) ); | |
| 216 | - $_replace_matched_tags = []; | |
| 228 | + $_tags = explode( '/', trim( $structure, '/' ) ); | |
| 229 | + $_replace_matched_tags = []; | |
| 217 | 230 | |
| 218 | - $_replace_tags = array_filter( $_tags, function ( $item ) use ( &$_replace_matched_tags ) { | |
| 219 | - $_is_valid = strpos( $item, '%' ) !== false; | |
| 220 | - if ( $_is_valid ) { | |
| 221 | - $_replace_matched_tags[] = trim( $item, '%' ); | |
| 222 | - } | |
| 223 | - return $_is_valid; | |
| 224 | - } ); | |
| 231 | + $_replace_tags = array_filter( | |
| 232 | + $_tags, | |
| 233 | + function ( $item ) use ( &$_replace_matched_tags ) { | |
| 234 | + $_is_valid = strpos( $item, '%' ) !== false; | |
| 235 | + if ( $_is_valid ) { | |
| 236 | + $_replace_matched_tags[] = trim( $item, '%' ); | |
| 237 | + } | |
| 238 | + return $_is_valid; | |
| 239 | + } | |
| 240 | + ); | |
| 225 | 241 | |
| 226 | - $_perma_structure = preg_quote( $structure, '/' ); | |
| 227 | - $_perma_structure = str_replace( $_replace_tags, '([^\/]+)', $_perma_structure ); | |
| 242 | + $_perma_structure = preg_quote( $structure, '/' ); | |
| 243 | + $_perma_structure = str_replace( $_replace_tags, '([^\/]+)', $_perma_structure ); | |
| 228 | 244 | |
| 229 | - preg_match( "/^$_perma_structure$/", $request, $matches ); | |
| 245 | + preg_match( "/^$_perma_structure$/", $request, $matches ); | |
| 230 | 246 | |
| 231 | - if ( empty( $matches ) || ! is_array( $matches ) ) { | |
| 232 | - return false; | |
| 233 | - } | |
| 247 | + if ( empty( $matches ) || ! is_array( $matches ) ) { | |
| 248 | + return false; | |
| 249 | + } | |
| 234 | 250 | |
| 235 | - if ( count( $matches ) === 1 ) { | |
| 236 | - return ['post_type' => 'docs']; | |
| 237 | - } | |
| 251 | + if ( count( $matches ) === 1 ) { | |
| 252 | + return [ 'post_type' => 'docs' ]; | |
| 253 | + } | |
| 238 | 254 | |
| 239 | - unset( $matches[0] ); | |
| 255 | + unset( $matches[0] ); | |
| 240 | 256 | |
| 241 | - return array_combine( $_replace_matched_tags, $matches ); | |
| 242 | - } | |
| 257 | + return array_combine( $_replace_matched_tags, $matches ); | |
| 258 | + } | |
| 243 | 259 | } |