Skip to content

Commit

Permalink
fix: redirect to 404 if vendor do not exist for toc template (#927)
Browse files Browse the repository at this point in the history
* fix: redirect to 404 if vendor not exist for toc template

fixed #922

* fix: phpcs standards

* perf: check existance of query var

check the existence of query var toc, then we should fetch the seller.
  • Loading branch information
kapilpaul authored Oct 15, 2020
1 parent 8e64a80 commit 6d5f178
Showing 1 changed file with 62 additions and 48 deletions.
110 changes: 62 additions & 48 deletions includes/Rewrites.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class Rewrites {

public $query_vars = array();
public $query_vars = [];
public $custom_store_url = '';

/**
Expand All @@ -18,24 +18,24 @@ class Rewrites {
public function __construct() {
$this->custom_store_url = dokan_get_option( 'custom_store_url', 'dokan_general', 'store' );

add_action( 'init', array( $this, 'register_rule' ) );
add_action( 'pre_get_posts', array( $this, 'store_query_filter' ) );
add_action( 'init', [ $this, 'register_rule' ] );
add_action( 'pre_get_posts', [ $this, 'store_query_filter' ] );

add_filter( 'woocommerce_get_query_vars', array( $this, 'resolve_wc_query_conflict' ) );
add_filter( 'template_include', array( $this, 'store_template' ) );
add_filter( 'template_include', array( $this, 'product_edit_template' ), 99 );
add_filter( 'template_include', array( $this, 'store_toc_template' ) );
add_filter( 'query_vars', array( $this, 'register_query_var' ) );
add_filter( 'woocommerce_get_breadcrumb', array( $this, 'store_page_breadcrumb' ) );
add_filter( 'woocommerce_get_query_vars', [ $this, 'resolve_wc_query_conflict' ] );
add_filter( 'template_include', [ $this, 'store_template' ] );
add_filter( 'template_include', [ $this, 'product_edit_template' ], 99 );
add_filter( 'template_include', [ $this, 'store_toc_template' ] );
add_filter( 'query_vars', [ $this, 'register_query_var' ] );
add_filter( 'woocommerce_get_breadcrumb', [ $this, 'store_page_breadcrumb' ] );
}

/**
* Generate breadcrumb for store page
*
* @since 2.4.7
*
* @param array $crumbs
*
* @since 2.4.7
*
* @return array $crumbs
*/
public function store_page_breadcrumb( $crumbs ) {
Expand All @@ -45,13 +45,14 @@ public function store_page_breadcrumb( $crumbs ) {

if ( function_exists( 'yoast_breadcrumb' ) && \WPSEO_Options::get( 'breadcrumbs-enable' ) ) {
unset( $crumbs );

return;
}

$author = get_query_var( $this->custom_store_url );
$seller_info = get_user_by( 'slug', $author );
$crumbs[1] = array( ucwords( $this->custom_store_url ), site_url() . '/' . $this->custom_store_url );
$crumbs[2] = array( $author, dokan_get_store_url( $seller_info->data->ID ) );
$crumbs[1] = [ ucwords( $this->custom_store_url ), site_url() . '/' . $this->custom_store_url ];
$crumbs[2] = [ $author, dokan_get_store_url( $seller_info->data->ID ) ];

return $crumbs;
}
Expand All @@ -70,23 +71,23 @@ public function is_woo_installed() {
*
* @return void
*/
function register_rule() {
public function register_rule() {
$this->query_vars = apply_filters(
'dokan_query_var_filter', array(
'products',
'new-product',
'orders',
'withdraw',
'settings',
'edit-account',
)
'dokan_query_var_filter', [
'products',
'new-product',
'orders',
'withdraw',
'settings',
'edit-account',
]
);

foreach ( $this->query_vars as $var ) {
add_rewrite_endpoint( $var, EP_PAGES );
}

$permalinks = get_option( 'woocommerce_permalinks', array() );
$permalinks = get_option( 'woocommerce_permalinks', [] );
if ( isset( $permalinks['product_base'] ) ) {
$base = substr( $permalinks['product_base'], 1 );
}
Expand All @@ -99,7 +100,7 @@ function register_rule() {

// get the category base. usually: shop
$base_array = explode( '/', ltrim( $base, '/' ) ); // remove first '/' and explode
$cat_base = isset( $base_array[0] ) ? $base_array[0] : 'shop';
$cat_base = isset( $base_array[0] ) ? $base_array[0] : 'shop';

add_rewrite_rule( $cat_base . '/(.+?)/([^/]+)(/[0-9]+)?/edit?$', 'index.php?product_cat=$matches[1]&product=$matches[2]&page=$matches[3]&edit=true', 'top' );
} else {
Expand All @@ -121,10 +122,10 @@ function register_rule() {
/**
* Resolve query var conflicts with WooCommerce
*
* @since 2.9.13
*
* @param array $query_vars
*
* @since 2.9.13
*
* @return array
*/
public function resolve_wc_query_conflict( $query_vars ) {
Expand All @@ -143,11 +144,11 @@ public function resolve_wc_query_conflict( $query_vars ) {
/**
* Register the query var
*
* @param array $vars
* @param array $vars
*
* @return array
*/
function register_query_var( $vars ) {
public function register_query_var( $vars ) {
$vars[] = $this->custom_store_url;
$vars[] = 'edit';
$vars[] = 'term_section';
Expand All @@ -163,11 +164,11 @@ function register_query_var( $vars ) {
/**
* Include store template
*
* @param type $template
* @param type $template
*
* @return string
*/
function store_template( $template ) {
public function store_template( $template ) {
$store_name = get_query_var( $this->custom_store_url );

if ( ! $this->is_woo_installed() ) {
Expand Down Expand Up @@ -205,18 +206,30 @@ function store_template( $template ) {
/**
* Returns the terms_and_conditions template
*
* @since 2.3
*
* @param string $template
*
* @since 2.3
*
* @return string
*/
function store_toc_template( $template ) {
public function store_toc_template( $template ) {
if ( ! $this->is_woo_installed() ) {
return $template;
}

if ( get_query_var( 'toc' ) ) {
return dokan_locate_template( 'store-toc.php' );
$store_name = get_query_var( $this->custom_store_url );

if ( ! empty( $store_name ) ) {
$seller = get_user_by( 'slug', $store_name );

//redirect to 404 if no seller found
if ( ! $seller ) {
return get_404_template();
}

return dokan_locate_template( 'store-toc.php' );
}
}

return $template;
Expand All @@ -225,11 +238,11 @@ function store_toc_template( $template ) {
/**
* Returns the edit product template
*
* @param string $template
* @param string $template
*
* @return string
*/
function product_edit_template( $template ) {
public function product_edit_template( $template ) {
if ( ! $this->is_woo_installed() ) {
return $template;
}
Expand All @@ -243,6 +256,7 @@ function product_edit_template( $template ) {
}

$edit_product_url = dokan_locate_template( 'products/new-product-single.php' );

return apply_filters( 'dokan_get_product_edit_template', $edit_product_url );
}

Expand All @@ -251,11 +265,11 @@ function product_edit_template( $template ) {
*
* Handles the product filtering by category in store page
*
* @param object $query
* @param object $query
*
* @return void
*/
function store_query_filter( $query ) {
public function store_query_filter( $query ) {
global $wp_query;

$author = get_query_var( $this->custom_store_url );
Expand All @@ -277,15 +291,15 @@ function store_query_filter( $query ) {
$query->set( 'author_name', $author );

$tax_query = [];
$query->query['term_section'] = isset( $query->query['term_section'] ) ? $query->query['term_section'] : array();
$query->query['term_section'] = isset( $query->query['term_section'] ) ? $query->query['term_section'] : [];

if ( $query->query['term_section'] ) {
array_push(
$tax_query, [
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $query->query['term'],
]
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $query->query['term'],
]
);
}

Expand All @@ -300,11 +314,11 @@ function store_query_filter( $query ) {
if ( ! empty( $product_visibility_not_in ) ) {
array_push(
$tax_query, [
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
]
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
]
);
}

Expand Down

0 comments on commit 6d5f178

Please sign in to comment.