unset( $labels['product_tag_plu'] ); } // Brand. Old: 'brand', 'brand_plu'. if ( isset( $labels['brand'] ) ) { $labels[ 'tax_' . DGWT_WCAS()->brands->getBrandTaxonomy() ] = $labels['brand']; unset( $labels['brand'] ); } if ( isset( $labels['brand_plu'] ) ) { $labels[ 'tax_' . DGWT_WCAS()->brands->getBrandTaxonomy() . '_plu' ] = $labels['brand_plu']; unset( $labels['brand_plu'] ); } return $labels; } /** * Populate list of taxonomies that has image support * * @param array $taxonomies * * @return array */ public function taxonomiesWithImages( $taxonomies ) { return array_merge( $taxonomies, $this->getActiveTaxonomies( 'image_support' ) ); } /** * Add search options for custom taxonomies * * @param array $settingsScope * * @return array */ public function addCustomTaxonomiesToSearchSettings( $settingsScope ) { $basePosition = 280; $skippedTaxonomies = array( 'product_cat', 'product_tag' ); if ( DGWT_WCAS()->brands->hasBrands() ) { $skippedTaxonomies[] = DGWT_WCAS()->brands->getBrandTaxonomy(); } foreach ( $this->getTaxonomies() as $taxonomy ) { if ( in_array( $taxonomy['taxonomy'], $skippedTaxonomies ) ) { continue; } $settingsScope[ $basePosition ] = array( 'name' => 'search_in_product_tax_' . $taxonomy['taxonomy'], 'label' => sprintf( __( 'Search in %s', 'ajax-search-for-woocommerce' ), mb_strtolower( $taxonomy['labels']['name'] ) ), 'class' => 'dgwt-wcas-premium-only', 'type' => 'checkbox', 'default' => 'off', ); $basePosition += 2; } if ( in_array( 'product_tag', $this->getActiveTaxonomies( 'image_support' ) ) ) { $settingsScope[1350] = array( 'name' => 'show_product_tax_product_tag_images', 'label' => __( 'show images', 'ajax-search-for-woocommerce' ), 'type' => 'checkbox', 'class' => 'js-dgwt-wcas-adv-settings dgwt-wcas-premium-only', 'default' => 'off', 'desc' => __( 'show images', 'ajax-search-for-woocommerce' ), 'move_dest' => 'show_product_tax_product_tag', ); } return $settingsScope; } /** * Add autocomplete options for custom taxonomies * * @param array $settingsScope * * @return array */ public function addCustomTaxonomiesToAutocompleteSettings( $settingsScope ) { $basePosition = 1350; $skippedTaxonomies = array( 'product_cat', 'product_tag' ); if ( DGWT_WCAS()->brands->hasBrands() ) { $skippedTaxonomies[] = DGWT_WCAS()->brands->getBrandTaxonomy(); } foreach ( $this->getTaxonomies() as $taxonomy ) { if ( in_array( $taxonomy['taxonomy'], $skippedTaxonomies ) ) { continue; } $settingsScope[ $basePosition ] = array( 'name' => 'show_product_tax_' . $taxonomy['taxonomy'], 'label' => sprintf( __( 'Show %s', 'ajax-search-for-woocommerce' ), mb_strtolower( $taxonomy['labels']['name'] ) ), 'class' => 'dgwt-wcas-premium-only' . ( $taxonomy['image_support'] ? ' js-dgwt-wcas-options-toggle-sibling' : '' ), 'type' => 'checkbox', 'default' => 'off', ); $basePosition += 2; if ( $taxonomy['image_support'] ) { $settingsScope[ $basePosition ] = array( 'name' => 'show_product_tax_' . $taxonomy['taxonomy'] . '_images', 'label' => __( 'show images', 'ajax-search-for-woocommerce' ), 'class' => 'dgwt-wcas-premium-only', 'type' => 'checkbox', 'default' => 'off', 'desc' => __( 'show images', 'ajax-search-for-woocommerce' ), 'move_dest' => 'show_product_tax_' . $taxonomy['taxonomy'], ); $basePosition += 2; } } return $settingsScope; } /** * Register all taxonomies * * @return void */ private function registerAllTaxonomies() { $this->taxonomies = array(); $this->registerTaxonomy( array( 'taxonomy' => 'product_cat', 'labels' => array( 'name' => __( 'Categories', 'woocommerce' ), 'singular_name' => __( 'Category', 'woocommerce' ), ), 'image_support' => true, ) ); $this->registerTaxonomy( array( 'taxonomy' => 'product_tag', 'labels' => array( 'name' => __( 'Tags' ), 'singular_name' => __( 'Tag' ), ), 'image_support' => false, ) ); $taxonomies = apply_filters( 'dgwt/wcas/indexer/taxonomies', array() ); if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) { foreach ( $taxonomies as $taxonomy ) { $this->registerTaxonomy( $taxonomy ); } } } /** * Register taxonomy * * @param string|array $taxonomy */ private function registerTaxonomy( $taxonomy ) { // Prepare default data if taxonomy is passed just as string if ( is_string( $taxonomy ) && taxonomy_exists( $taxonomy ) ) { $taxonomyObj = get_taxonomy( $taxonomy ); $taxonomy = array( 'taxonomy' => $taxonomy, 'labels' => array( 'name' => $taxonomyObj->labels->name, 'singular_name' => $taxonomyObj->labels->singular_name, ), 'image_support' => false, ); } if ( ! is_array( $taxonomy ) ) { return; } $taxonomyData = array( 'taxonomy' => '', 'labels' => array( 'name' => '', 'singular_name' => '', ), 'image_support' => false, 'search_direct' => false, 'search_related_products' => false, 'show_images' => false, ); $taxonomy = apply_filters( 'dgwt/wcas/indexer/taxonomies/register-taxonomy', $taxonomy ); // Taxonomy slug if ( empty( $taxonomy['taxonomy'] ) || ! taxonomy_exists( $taxonomy['taxonomy'] ) ) { return; } $taxonomyData['taxonomy'] = $taxonomy['taxonomy']; // Name if ( isset( $taxonomy['labels']['name'] ) && is_string( $taxonomy['labels']['name'] ) ) { $taxonomyData['labels']['name'] = $taxonomy['labels']['name']; } else { $taxonomyObj = get_taxonomy( $taxonomyData['taxonomy'] ); $taxonomyData['labels']['name'] = $taxonomyObj->labels->name; } // Singular name if ( isset( $taxonomy['labels']['singular_name'] ) && is_string( $taxonomy['labels']['singular_name'] ) ) { $taxonomyData['labels']['singular_name'] = $taxonomy['labels']['singular_name']; } else { $taxonomyObj = get_taxonomy( $taxonomyData['taxonomy'] ); $taxonomyData['labels']['singular_name'] = $taxonomyObj->labels->singular_name; } // Image support if ( isset( $taxonomy['image_support'] ) && is_bool( $taxonomy['image_support'] ) ) { $taxonomyData['image_support'] = $taxonomy['image_support']; } $taxonomyData['search_direct'] = DGWT_WCAS()->settings->getOption( 'show_product_tax_' . $taxonomy['taxonomy'] ) === 'on'; $taxonomyData['search_related_products'] = DGWT_WCAS()->settings->getOption( 'search_in_product_tax_' . $taxonomy['taxonomy'] ) === 'on'; $taxonomyData['show_images'] = DGWT_WCAS()->settings->getOption( 'show_product_tax_' . $taxonomy['taxonomy'] . '_images' ) === 'on'; // Ensure we have proper container for taxonomies if ( $this->taxonomies === null ) { $this->taxonomies = array(); } // Prevent to register same taxonomy twice foreach ( $this->taxonomies as $registeredTaxonomy ) { if ( $registeredTaxonomy['taxonomy'] === $taxonomyData['taxonomy'] ) { return; } } $this->taxonomies[] = $taxonomyData; } /** * Clear internal cache after changing values of options related to taxonomies * * @param string $optionKey * * @return void */ public function listenForSettingsChange( $optionKey ) { if ( strpos( $optionKey, 'search_in_product_tax_' ) !== false ) { $this->clearCache(); } } }