| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| final class Spr_Jetpack_Instant_Search_Wholesale_Visibility {
|
|
|
|
|
|
|
|
|
|
|
|
|
| private static $instance = null;
|
|
|
|
|
|
|
|
|
|
|
| private const WHOLESALE_ROLE = 'wholesale_customer';
|
| private const WHOLESALE_SLUG = 'wholesale';
|
|
|
|
|
|
|
|
|
|
|
|
|
| public static function spr_instance() {
|
| if ( null === self::$instance ) {
|
| self::$instance = new self();
|
| }
|
| return self::$instance;
|
| }
|
|
|
|
|
|
|
|
|
| private function __construct() {
|
| add_filter( 'jetpack_instant_search_options', array( $this, 'spr_filter_instant_search_options' ), 999 );
|
|
|
|
|
| add_action( 'wp_enqueue_scripts', array( $this, 'spr_enqueue_overlay_fallback_assets' ) );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| private function spr_user_can_view_wholesale() {
|
|
|
| if ( current_user_can( 'manage_woocommerce' ) || current_user_can( 'manage_options' ) ) {
|
| return true;
|
| }
|
|
|
| $user = wp_get_current_user();
|
| if ( ! $user || empty( $user->roles ) ) {
|
| return false;
|
| }
|
|
|
| $role = (string) apply_filters( 'spr_wholesale_role_key', self::WHOLESALE_ROLE );
|
| return in_array( $role, (array) $user->roles, true );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function spr_filter_instant_search_options( $options ) {
|
| if ( $this->spr_user_can_view_wholesale() ) {
|
| return $options;
|
| }
|
|
|
| $wholesale_slug = sanitize_title( (string) apply_filters( 'spr_wholesale_category_slug', self::WHOLESALE_SLUG ) );
|
|
|
|
|
| $not_product = array(
|
| 'bool' => array(
|
| 'must_not' => array(
|
| array( 'term' => array( 'post_type' => 'product' ) ),
|
| ),
|
| ),
|
| );
|
|
|
|
|
| $product_not_wholesale = array(
|
| 'bool' => array(
|
| 'must' => array(
|
| array( 'term' => array( 'post_type' => 'product' ) ),
|
| ),
|
| 'must_not' => array(
|
| array( 'term' => array( 'taxonomy.product_cat.slug' => $wholesale_slug ) ),
|
| ),
|
| ),
|
| );
|
|
|
| $options['adminQueryFilter'] = array(
|
| 'bool' => array(
|
| 'should' => array( $not_product, $product_not_wholesale ),
|
| 'minimum_should_match' => 1,
|
| ),
|
| );
|
|
|
| return $options;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function spr_enqueue_overlay_fallback_assets() {
|
| if ( $this->spr_user_can_view_wholesale() ) {
|
| return;
|
| }
|
|
|
| $wholesale_slug = sanitize_title( (string) apply_filters( 'spr_wholesale_category_slug', self::WHOLESALE_SLUG ) );
|
|
|
|
|
| $css = <<<CSS
|
| /* Accessibility: ensure the facet option is not perceivable or focusable for non-wholesale users. */
|
| .jetpack-instant-search__overlay a.jetpack-search-filter__link[data-filter-type="taxonomy"][data-taxonomy="product_cat"][data-val="{$wholesale_slug}"] {
|
| display: none !important;
|
| }
|
| CSS;
|
|
|
|
|
| wp_register_style( 'spr-jetpack-wholesale-css', false, array(), '1.0.1' );
|
| wp_enqueue_style( 'spr-jetpack-wholesale-css' );
|
| wp_add_inline_style( 'spr-jetpack-wholesale-css', $css );
|
|
|
|
|
| $js = <<<JS
|
| (function() {
|
| 'use strict';
|
| var slug = '{$wholesale_slug}';
|
|
|
| function hideWholesaleFacet(root) {
|
| if (!root) { return; }
|
| var sel = 'a.jetpack-search-filter__link[data-filter-type="taxonomy"][data-taxonomy="product_cat"][data-val="' + slug + '"]';
|
| var nodes = root.querySelectorAll(sel);
|
| nodes.forEach(function(node) {
|
| node.setAttribute('aria-hidden', 'true');
|
| node.setAttribute('hidden', '');
|
| node.setAttribute('tabindex', '-1');
|
| node.style.display = 'none';
|
| });
|
| }
|
|
|
| // On load and whenever the overlay updates (Jetpack mutates DOM on search).
|
| document.addEventListener('DOMContentLoaded', function() {
|
| hideWholesaleFacet(document);
|
| var target = document.body;
|
| if (!('MutationObserver' in window) || !target) { return; }
|
| var mo = new MutationObserver(function(mutations) {
|
| for (var i = 0; i < mutations.length; i++) {
|
| var m = mutations[i];
|
| if (m.addedNodes && m.addedNodes.length) {
|
| hideWholesaleFacet(document);
|
| }
|
| }
|
| });
|
| mo.observe(target, { childList: true, subtree: true });
|
| });
|
| })();
|
| JS;
|
|
|
| wp_register_script( 'spr-jetpack-wholesale-js', '', array(), '1.0.1', true );
|
| wp_enqueue_script( 'spr-jetpack-wholesale-js' );
|
| wp_add_inline_script( 'spr-jetpack-wholesale-js', $js, 'after' );
|
| }
|
| }
|
|
|
| Spr_Jetpack_Instant_Search_Wholesale_Visibility::spr_instance();
|
| |
| |
Comments