function widget($args, $instance)
 {
     global $wp_query;
     $facets = elasticsearch\Faceting::all();
     $async = isset($instance['async']) && $instance['async'];
     $split = isset($instance['splitSpaces']) && $instance['splitSpaces'];
     if ($async) {
         wp_enqueue_script("jquery");
         wp_enqueue_script('elasticsearch', plugins_url('/js/ajax.js', __FILE__), array('jquery'));
         wp_localize_script('elasticsearch', 'esselected', array('showEmpty' => isset($instance['showEmpty']) ? 1 : 0));
     }
     $url = null;
     if (is_category() || is_tax()) {
         $url = get_term_link($wp_query->queried_object);
     } elseif (is_tag()) {
         $url = get_tag_link($wp_query->queried_object->term_id);
     } elseif (is_archive()) {
         $url = get_post_type_archive_link($wp_query->queried_object->query_var);
     } elseif (is_search()) {
         $url = home_url('/');
     }
     if ($url == null) {
         return null;
     }
     if ($async) {
         echo '<aside id="facet-selected" class="widget facets facets-selected">';
         echo '<h3 class="widget-title"><span class="widget-title-inner">Your Selections</span></h3>';
         if ($async) {
             echo '<span class="clear"><a href="#" class="clear-inner esclear">Clear All</a></span>';
         }
         echo '<ul>';
         if (is_search()) {
             if ($split && strpos($wp_query->query_vars['s'], ' ') !== false) {
                 $split = explode(' ', trim($wp_query->query_vars['s']));
                 foreach ($split as $term) {
                     $term = trim($term);
                     if ($term) {
                         echo '<li class="term removable" data-term="' . $term . '"><a href="#search-term-' . $term . '">Search: ' . $term . '</a></li>';
                     }
                 }
             } else {
                 echo '<li data-term="' . $term . '">Search: ' . $wp_query->query_vars['s'] . '</li>';
             }
         }
         foreach ($facets as $type => $facet) {
             $name = $type;
             if (taxonomy_exists($type)) {
                 $name = get_taxonomy($type)->labels->singular_name;
             }
             if (isset($facet['available'])) {
                 foreach ($facet['available'] as $option) {
                     $isremovable = $this->isremoveable($option['slug']);
                     echo '<li id="facet-' . $type . '-' . $option['slug'] . '-selected"';
                     if ($isremovable) {
                         echo ' style="display:none" class="facet-item removable">';
                         echo '<a href="#facet-' . $type . '-' . $option['slug'] . '">' . ($name == 'post_type' ? 'Content Type' : $name) . ': ' . $option['name'] . '</a>';
                     } else {
                         echo 'class="facet-item">' . ($name == 'post_type' ? 'Content Type' : $name) . ': ' . $option['name'];
                     }
                     echo '</li>';
                 }
             }
         }
         echo '</ul>';
         echo '</aside>';
     } else {
         foreach ($facets as $type => $facet) {
             if (count($facet['selected']) > 0) {
                 $name = $type;
                 if (taxonomy_exists($type)) {
                     $name = get_taxonomy($type)->label;
                 }
                 echo '<aside id="facet-' . $type . '-selected" class="widget facets facets-selected">';
                 echo '<h3 class="widget-title"><span class="widget-title-inner">' . $name . '</span></h3>';
                 echo '<ul>';
                 foreach ($facet['selected'] as $option) {
                     $url = elasticsearch\Faceting::urlRemove($url, $type, $option['slug']);
                     $isremovable = $this->isremoveable($option['slug']);
                     echo '<li id="facet-' . $type . '-' . $option['slug'] . '" class="facet-item">';
                     if ($isremovable) {
                         echo '<a href="' . $url . '">' . $option['name'] . '</a>';
                     } else {
                         echo $name . ':' . $option['name'];
                     }
                     echo '</li>';
                 }
                 echo '</ul>';
                 echo '</aside>';
             }
         }
     }
 }
コード例 #2
0
ファイル: widget.php プロジェクト: pivotlearning/wpsite
 function widget($args, $instance)
 {
     global $wp_query;
     $facets = elasticsearch\Faceting::all();
     $url = null;
     if (is_category() || is_tax()) {
         $url = get_term_link($wp_query->queried_object);
     } elseif (is_tag()) {
         $url = get_tag_link($wp_query->queried_object->term_id);
     } elseif (is_archive()) {
         $url = get_post_type_archive_link($wp_query->queried_object->query_var);
     } elseif (is_search()) {
         $url = home_url('/');
     }
     foreach ($facets as $type => $facet) {
         if (count($facet['selected']) > 0) {
             $name = $type;
             if (taxonomy_exists($type)) {
                 $name = get_taxonomy($type)->label;
             }
             echo '<aside id="facet-' . $type . '-selected" class="widget facets facets-selected">';
             echo '<h3 class="widget-title">' . $name . '</h3>';
             echo '<ul>';
             foreach ($facet['selected'] as $option) {
                 $url = elasticsearch\Faceting::urlRemove($url, $type, $option['slug']);
                 echo '<li id="facet-' . $type . '-' . $option['slug'] . '" class="facet-item">';
                 echo '<a href="' . $url . '">' . $option['name'] . '</a>';
                 echo '</li>';
             }
             echo '</ul>';
             echo '</aside>';
         }
     }
 }
 function widget($args, $instance)
 {
     global $wp_query;
     $async = isset($instance['async']) && $instance['async'] && isset($instance['asyncReplace']);
     $offset = isset($instance['cssOffset']) ? $instance['cssOffset'] : null;
     if ($async) {
         wp_enqueue_script("jquery");
         wp_enqueue_script('elasticsearch', plugins_url('/js/ajax.js', __FILE__), array('jquery'));
         elasticsearch\Theme::setSelector($instance['asyncReplace']);
         wp_localize_script('elasticsearch', 'esfaceting', array('replace' => $instance['asyncReplace'], 'offset' => $offset));
     }
     $facets = elasticsearch\Faceting::all();
     $prep = array();
     $url = null;
     $selected = null;
     if (is_category() || is_tax()) {
         $url = get_term_link($wp_query->queried_object);
     } elseif (is_tag()) {
         $url = get_tag_link($wp_query->queried_object->term_id);
     } elseif (is_archive()) {
         $url = get_post_type_archive_link($wp_query->queried_object->query_var);
     } elseif (is_search()) {
         $url = home_url('/');
     }
     if ($url == null) {
         return null;
     }
     foreach ($facets as $type => $facet) {
         if ($facet['total'] > 0 || $async) {
             if (isset($facet['available'])) {
                 foreach ($facet['available'] as $option) {
                     if ($option['count'] != $wp_query->found_posts) {
                         if (!isset($prep[$type])) {
                             $name = $type;
                             if (taxonomy_exists($type)) {
                                 $name = get_taxonomy($type)->label;
                             }
                             $prep[$type] = array('type' => $type, 'name' => $name, 'avail' => array(), 'show' => false);
                         }
                         $prep[$type]['show'] = true;
                         $prep[$type]['avail'][] = array('url' => elasticsearch\Faceting::urlAdd($url, $type, $option['slug']), 'option' => $option);
                     }
                 }
             }
         }
     }
     uksort($prep, function ($a, $b) {
         if ($a == $b) {
             return 0;
         }
         // prioritize these
         $priority = array('post_type', 'category', 'post_tag');
         $pa = array_search($a, $priority);
         $pb = array_search($b, $priority);
         if ($pa !== false && $pb !== false) {
             return $pa < $pb ? 0 : 1;
         }
         if ($pa !== false || $pb !== false) {
             return $pa === false;
         }
         // then by alpha
         return $a < $b ? 0 : 1;
     });
     if (count($prep) > 0) {
         echo '<form action="' . $url . '" method="GET" id="esajaxform">';
         foreach ($prep as $type => $settings) {
             $style = $settings['show'] ? '' : 'style="display:none"';
             echo '<aside id="facet-' . $type . '-available" class="widget facets facets-available" ' . $style . '>';
             echo '<h3 class="widget-title"><span class="widget-title-inner">' . ($settings['name'] == 'post_type' ? 'Content Type' : $settings['name']) . '</span></h3>';
             if ($async) {
                 echo '<p class="facet-empty" style="display:none">You can not filter the results anymore.</p>';
             }
             echo '<ul>';
             foreach ($settings['avail'] as $avail) {
                 $style = $avail['option']['count'] < $wp_query->found_posts ? '' : 'style="display:none"';
                 echo '<li id="facet-' . $type . '-' . $avail['option']['slug'] . '" class="facet-item" ' . $style . '>';
                 if ($async) {
                     printf('<input type="checkbox" name="es[%s][and][]" value="%s" />%s <span class="count">(%d)</span>', $type, $avail['option']['slug'], $avail['option']['name'], $avail['option']['count']);
                 } else {
                     echo '<a href="' . $avail['url'] . '">' . $avail['option']['name'] . ' (' . $avail['option']['count'] . ')</a>';
                 }
                 echo '</li>';
             }
             echo '</ul>';
             echo '</aside>';
         }
         if ($async) {
             echo '<span class="clear"><a href="#" class="clear-inner esclear">Clear All</a></span>';
         }
         echo '</form>';
     }
 }