at_path ); $cat_slug = urldecode( $cat_slug ); $category_rules[ "{$cat_slug}/?\$" ] = 'index.php?product_cat=' . $category['slug']; $category_rules[ "{$cat_slug}/embed/?\$" ] = 'index.php?product_cat=' . $category['slug'] . '&embed=true'; $category_rules[ "{$cat_slug}/{$wp_rewrite->feed_base}/{$feed}/?\$" ] = 'index.php?product_cat=' . $category['slug'] . '&feed=$matches[1]'; $category_rules[ "{$cat_slug}/{$feed}/?\$" ] = 'index.php?product_cat=' . $category['slug'] . '&feed=$matches[1]'; $category_rules[ "{$cat_slug}/{$wp_rewrite->pagination_base}/?([0-9]{1,})/?\$" ] = 'index.php?product_cat=' . $category['slug'] . '&paged=$matches[1]'; if ( $this->remove_product_base && $use_parent_slug ) { $cat_path = urldecode( $cat_path ); $product_rules[ $cat_path . '/([^/]+)/?$' ] = 'index.php?product=$matches[1]'; $product_rules[ $cat_path . '/([^/]+)/' . $wp_rewrite->comments_pagination_base . '-([0-9]{1,})/?$' ] = 'index.php?product=$matches[1]&cpage=$matches[2]'; } } /** * Register WPML filters back */ Sitepress::get()->restore_term_filters(); $rules = empty( $rules ) ? [] : $rules; return $category_rules + $product_rules + $rules; } /** * Returns categories array. * * ['category id' => ['slug' => 'category slug', 'parent' => 'parent category id']] * * @return array */ private function get_categories() { if ( is_null( $this->categories ) ) { $categories = get_categories( [ 'taxonomy' => 'product_cat', 'hide_empty' => false, ] ); $slugs = []; foreach ( $categories as $category ) { $slugs[ $category->term_id ] = [ 'parent' => $category->parent, 'slug' => $category->slug, ]; } $this->categories = $slugs; } return $this->categories; } /** * Recursively builds category full path. * * @param object $category Term object. * * @return string */ private function get_category_fullpath( $category ) { $categories = $this->get_categories(); $parent = $category['parent']; if ( $parent > 0 && array_key_exists( $parent, $categories ) ) { return $this->get_category_fullpath( $categories[ $parent ] ) . '/' . $category['slug']; } return $category['slug']; } /** * Get product base. * * @return string */ private function get_product_base() { if ( is_null( $this->product_base ) ) { $permalink_structure = wc_get_permalink_structure(); $this->product_base = $permalink_structure['product_rewrite_slug']; if ( strpos( $this->product_base, '%product_cat%' ) !== false ) { $this->product_base = str_replace( '%product_cat%', '', $this->product_base ); } $this->product_base = '/' . trim( $this->product_base, '/' ) . '/'; } return $this->product_base; } /** * Check if the link can be changed or not. * * @param string $check Check string. * @param string $against Against this. * * @return bool */ private function can_change_link( $check, $against ) { return $check !== $against || ! get_option( 'permalink_structure' ); } }