/** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget($args, $instance) { if (WPSOLR_Global::getOption()->get_search_is_replace_default_wp_search() && WPSOLR_Global::getOption()->get_search_is_use_current_theme_search_template() && WPSOLR_Query_Parameters::is_wp_search()) { echo $args['before_widget']; $results = WPSOLR_Global::getSolrClient()->display_results(WPSOLR_Global::getQuery()); echo '<div id="res_facets">' . WPSOLR_UI_Facets::Build(WPSOLR_Data_Facets::get_data(WPSOLR_Global::getQuery()->get_filter_query_fields_group_by_name(), WPSOLR_Global::getOption()->get_facets_to_display(), $results[1]), OptionLocalization::get_options()) . '</div>'; echo $args['after_widget']; } }
/** * Build facets UI * * @param array $facets * @param $localization_options array * * @return string */ public static function Build($facets, $localization_options) { $html = ''; if (!empty($facets)) { $facet_element = OptionLocalization::get_term($localization_options, 'facets_element'); $facet_title = OptionLocalization::get_term($localization_options, 'facets_title'); foreach ($facets as $facet) { $html .= "<div class='wpsolr_facet_title'>" . sprintf($facet_title, $facet['name']) . "</div>"; self::displayFacetHierarchy($facet_element, $html, $facet, !empty($facet['items']) ? $facet['items'] : array()); } $is_facet_selected = false; $html = sprintf("<div><label class='wdm_label'>%s</label>\n <input type='hidden' name='sel_fac_field' id='sel_fac_field' data-wpsolr-facets-selected=''>\n <div class='wdm_ul' id='wpsolr_section_facets'><div class='select_opt %s' id='wpsolr_remove_facets'>%s</div>", OptionLocalization::get_term($localization_options, 'facets_header'), self::WPSOLR_FACET_CHECKBOX_CLASS . (!$is_facet_selected ? ' checked' : ''), OptionLocalization::get_term($localization_options, 'facets_element_all_results')) . $html; $html .= '</div></div>'; } return $html; }
/** * Build facets UI * * @param array $facets * @param $localization_options array * * @return string */ public static function Build($facets, $localization_options) { $html = ''; if (!empty($facets)) { $wpsolr_facet_checkbox_class = 'wpsolr_facet_checkbox'; $facet_element = OptionLocalization::get_term($localization_options, 'facets_element'); $facet_title = OptionLocalization::get_term($localization_options, 'facets_title'); $is_facet_selected = false; foreach ($facets as $facet) { $html .= "<lh >" . sprintf($facet_title, $facet['name']) . "</lh><br>"; $facet_id = $facet['id']; foreach ($facet['items'] as $item) { $item_name = $item['name']; $item_display_name = $item['name']; $item_count = $item['count']; $item_selected = isset($item['selected']) ? $item['selected'] : false; if (strcasecmp($facet_id, 'type') == null) { //special case type here .. as we can use the post type name for a nicer output $post_type_object = get_post_type_object($item_name); if ($post_type_object != null && $post_type_object->labels != null && $post_type_object->labels->singular_name != null) { $item_display_name = $post_type_object->labels->singular_name; } } // Check if one facet item is selected (once only). if ($item_selected && !$is_facet_selected) { $is_facet_selected = true; } $facet_class = $wpsolr_facet_checkbox_class . ($item_selected ? ' checked' : ''); $html .= "<div class='select_opt {$facet_class}' id='{$facet_id}:{$item_name}'>" . sprintf($facet_element, $item_display_name, $item_count) . "</div>"; } } $html = sprintf("<div><label class='wdm_label'>%s</label>\n <input type='hidden' name='sel_fac_field' id='sel_fac_field' data-wpsolr-facets-selected=''>\n <div class='wdm_ul' id='wpsolr_section_facets'><div class='select_opt %s' id='wpsolr_remove_facets'>%s</div>", OptionLocalization::get_term($localization_options, 'facets_header'), $wpsolr_facet_checkbox_class . (!$is_facet_selected ? ' checked' : ''), OptionLocalization::get_term($localization_options, 'facets_element_all_results')) . $html; $html .= '</div></div>'; } return $html; }
function return_solr_results() { $final_result = WPSOLR_Global::getSolrClient()->display_results(WPSOLR_Global::getQuery()); // Add result rows as html $res1[] = $final_result[3]; // Add pagination html $total = $final_result[2]; $number_of_res = WPSOLR_Global::getOption()->get_search_max_nb_results_by_page(); $paginat_var = ''; if ($total > $number_of_res) { $pages = ceil($total / $number_of_res); $paginat_var .= '<ul id="pagination-flickr"class="wdm_ul">'; for ($k = 1; $k <= $pages; $k++) { $paginat_var .= "<li ><a class='paginate' href='javascript:void(0)' id='{$k}'>{$k}</a></li>"; } $paginat_var .= '</ul>'; } $res1[] = $paginat_var; // Add results infos html ('showing x to y results out of n') $res1[] = $final_result[4]; // Add facets data $res1[] = WPSOLR_UI_Facets::Build(WPSOLR_Data_Facets::get_data(WPSOLR_Global::getQuery()->get_filter_query_fields_group_by_name(), WPSOLR_Global::getOption()->get_facets_to_display(), $final_result[1]), OptionLocalization::get_options()); // Output Json response to Ajax call echo json_encode($res1); die; }
</h4></div> <?php foreach (OptionLocalization::get_section_terms($section) as $term_code => $term_content) { ?> <div class='wdm_row'> <div class='col_left'> <?php echo $term_content[0]; ?> </div> <div class='col_right'> <?php $term_localized = OptionLocalization::get_term($options, $term_code); echo "<textarea id='message_user_without_capabilities_shown_no_results' name='wdm_solr_localization_data[terms][{$term_code}]'\n\t\t\t\t\t\t rows='4' cols='100'>{$term_localized}</textarea >"; ?> </div> </div> <?php } ?> </div> <div style="clear:both"></div> <?php } ?>
function my_enqueue() { if (!WPSOLR_Global::getOption()->get_search_is_prevent_loading_front_end_css()) { wp_enqueue_style('solr_auto_css', plugins_url('css/bootstrap.min.css', __FILE__)); wp_enqueue_style('solr_frontend', plugins_url('css/style.css', __FILE__)); } wp_enqueue_script('solr_auto_js1', plugins_url('js/bootstrap-typeahead.js', __FILE__), array('jquery'), false, true); // Url utilities to manipulate the url parameters wp_enqueue_script('urljs', plugins_url('bower_components/jsurl/url.min.js', __FILE__), array('jquery'), false, true); wp_enqueue_script('autocomplete', plugins_url('js/autocomplete_solr.js', __FILE__), array('solr_auto_js1', 'urljs'), false, true); wp_localize_script('autocomplete', 'wp_localize_script_autocomplete', array('ajax_url' => admin_url('admin-ajax.php'), 'is_show_url_parameters' => WPSOLR_Global::getOption()->get_search_is_ajax_with_url_parameters(), 'is_url_redirect' => WPSOLR_Global::getOption()->get_search_is_use_current_theme_search_template(), 'SEARCH_PARAMETER_SEARCH' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_SEARCH, 'SEARCH_PARAMETER_Q' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_Q, 'SEARCH_PARAMETER_FQ' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_FQ, 'SEARCH_PARAMETER_SORT' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_SORT, 'SEARCH_PARAMETER_PAGE' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_PAGE, 'SORT_CODE_BY_RELEVANCY_DESC' => WPSolrSearchSolrClient::SORT_CODE_BY_RELEVANCY_DESC)); /* * Infinite scroll: load javascript if option is set. */ if (WPSOLR_Global::getOption()->get_search_is_infinitescroll()) { // Get localization options $localization_options = OptionLocalization::get_options(); wp_register_script('infinitescroll', plugins_url('/js/jquery.infinitescroll.js', __FILE__), array('jquery'), '1.0', true); wp_enqueue_script('infinitescroll'); // loadingtext for translation // loadimage custom loading image url wp_localize_script('infinitescroll', 'wp_localize_script_infinitescroll', array('ajax_url' => admin_url('admin-ajax.php'), 'loadimage' => plugins_url('/images/infinitescroll.gif', __FILE__), 'loadingtext' => OptionLocalization::get_term($localization_options, 'infinitescroll_loading'), 'SEARCH_PARAMETER_Q' => WPSOLR_Query_Parameters::SEARCH_PARAMETER_Q)); } }
/** * * @param WPSOLR_Query $wpsolr_query * * @return array Array of html */ public function display_results(WPSOLR_Query $wpsolr_query) { $output = array(); $search_result = array(); // Load options $localization_options = OptionLocalization::get_options(); $resultset = $this->execute_wpsolr_query($wpsolr_query); $found = $resultset->getNumFound(); // No results: try a new query if did you mean is activated if ($found === 0 && WPSOLR_Global::getOption()->get_search_is_did_you_mean()) { // Add spellcheck to current solarium query $spell_check = $this->solarium_query->getSpellcheck(); $spell_check->setCount(10); $spell_check->setCollate(true); $spell_check->setExtendedResults(true); $spell_check->setCollateExtendedResults(true); // Excecute the query modified $resultset = $this->execute_solarium_query(); // Parse spell check results $spell_check_results = $resultset->getSpellcheck(); if ($spell_check_results && !$spell_check_results->getCorrectlySpelled()) { $collations = $spell_check_results->getCollations(); $queryTermsCorrected = $wpsolr_query->get_wpsolr_query(); // original query foreach ($collations as $collation) { foreach ($collation->getCorrections() as $input => $correction) { $queryTermsCorrected = str_replace($input, is_array($correction) ? $correction[0] : $correction, $queryTermsCorrected); } } if ($queryTermsCorrected != $wpsolr_query->get_wpsolr_query()) { $err_msg = sprintf(OptionLocalization::get_term($localization_options, 'results_header_did_you_mean'), $queryTermsCorrected) . '<br/>'; $search_result[] = $err_msg; // Execute query with spelled terms $this->solarium_query->setQuery($queryTermsCorrected); try { $resultset = $this->execute_solarium_query(); $found = $resultset->getNumFound(); } catch (Exception $e) { // Sometimes, the spelling query returns errors // java.lang.StringIndexOutOfBoundsException: String index out of range: 15\n\tat java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:203)\n\tat // java.lang.StringBuilder.charAt(StringBuilder.java:72)\n\tat org.apache.solr.spelling.SpellCheckCollator.getCollation(SpellCheckCollator.java:164)\n\tat $found = 0; } } else { $search_result[] = 0; } } else { $search_result[] = 0; } } else { $search_result[] = 0; } // Retrieve facets from resultset $facets_to_display = WPSOLR_Global::getOption()->get_facets_to_display(); if (count($facets_to_display)) { foreach ($facets_to_display as $facet) { $fact = strtolower($facet); if (WpSolrSchema::_FIELD_NAME_CATEGORIES === $fact) { $fact = WpSolrSchema::_FIELD_NAME_CATEGORIES_STR; } $facet_res = $resultset->getFacetSet()->getFacet("{$fact}"); foreach ($facet_res as $value => $count) { $output[$facet][] = array($value, $count); } } $search_result[] = $output; } else { $search_result[] = 0; } $search_result[] = $found; $results = array(); $highlighting = $resultset->getHighlighting(); $i = 1; $cat_arr = array(); $are_comments_indexed = WPSOLR_Global::getOption()->get_index_are_comments_indexed(); foreach ($resultset as $document) { $id = $document->id; $title = $document->title; $content = ''; $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($id)); $no_comments = $document->numcomments; if ($are_comments_indexed) { $comments = $document->comments; } $date = date('m/d/Y', strtotime($document->displaydate)); if (property_exists($document, 'categories_str')) { $cat_arr = $document->categories_str; } $cat = implode(',', $cat_arr); $auth = $document->author; $url = get_permalink($id); $highlightedDoc = $highlighting->getResult($document->id); $cont_no = 0; $comm_no = 0; if ($highlightedDoc) { foreach ($highlightedDoc as $field => $highlight) { if ($field == WpSolrSchema::_FIELD_NAME_TITLE) { $title = implode(' (...) ', $highlight); } else { if ($field == WpSolrSchema::_FIELD_NAME_CONTENT) { $content = implode(' (...) ', $highlight); } else { if ($field == WpSolrSchema::_FIELD_NAME_COMMENTS) { $comments = implode(' (...) ', $highlight); $comm_no = 1; } } } } } $msg = ''; $msg .= "<div id='res{$i}'><div class='p_title'><a href='{$url}'>{$title}</a></div>"; $image_fragment = ''; // Display first image if (is_array($image_url) && count($image_url) > 0) { $image_fragment .= "<img class='wdm_result_list_thumb' src='{$image_url['0']}' />"; } if (empty($content)) { // Set a default value for content if no highlighting returned. $post_to_show = get_post($id); if (isset($post_to_show)) { // Excerpt first, or content. $content = !empty($post_to_show->post_excerpt) ? $post_to_show->post_excerpt : $post_to_show->post_content; if (isset($ind_opt['is_shortcode_expanded']) && strpos($content, '[solr_search_shortcode]') === false) { // Expand shortcodes which have a plugin active, and are not the search form shortcode (else pb). global $post; $post = $post_to_show; $content = do_shortcode($content); } // Remove shortcodes tags remaining, but not their content. // strip_shortcodes() does nothing, probably because shortcodes from themes are not loaded in admin. // Credit: https://wordpress.org/support/topic/stripping-shortcodes-keeping-the-content. // Modified to enable "/" in attributes $content = preg_replace("~(?:\\[/?)[^\\]]+/?\\]~s", '', $content); # strip shortcodes, keep shortcode content; // Strip HTML and PHP tags $content = strip_tags($content); if (isset($res_opt['highlighting_fragsize']) && is_numeric($res_opt['highlighting_fragsize'])) { // Cut content at the max length defined in options. $content = substr($content, 0, $res_opt['highlighting_fragsize']); } } } // Format content text a little bit $content = str_replace(' ', '', $content); $content = str_replace(' ', ' ', $content); $content = ucfirst(trim($content)); $content .= '...'; //if ( $cont_no == 1 ) { if (false) { $msg .= "<div class='p_content'>{$image_fragment} {$content} - <a href='{$url}'>Content match</a></div>"; } else { $msg .= "<div class='p_content'>{$image_fragment} {$content}</div>"; } if ($comm_no == 1) { $msg .= "<div class='p_comment'>" . $comments . "-<a href='{$url}'>Comment match</a></div>"; } // Groups bloc - Bottom right $wpsolr_groups_message = apply_filters(WpSolrFilters::WPSOLR_FILTER_SOLR_RESULTS_DOCUMENT_GROUPS_INFOS, get_current_user_id(), $document); if (isset($wpsolr_groups_message)) { // Display groups of this user which owns at least one the document capability $message = $wpsolr_groups_message['message']; $msg .= "<div class='p_misc'>{$message}"; $msg .= "</div>"; $msg .= '<br/>'; } // Informative bloc - Bottom right $msg .= "<div class='p_misc'>"; if (WPSOLR_Global::getOption()->get_is_display_author()) { $msg .= "<span class='pauthor'>" . sprintf(OptionLocalization::get_term($localization_options, 'results_row_by_author'), $auth) . "</span>"; } if (WPSOLR_Global::getOption()->get_is_display_category()) { $msg .= empty($cat) ? "" : "<span class='pcat'>" . sprintf(OptionLocalization::get_term($localization_options, 'results_row_in_category'), $cat) . "</span>"; } if (WPSOLR_Global::getOption()->get_is_display_date()) { $msg .= "<span class='pdate'>" . sprintf(OptionLocalization::get_term($localization_options, 'results_row_on_date'), $date) . "</span>"; } if (WPSOLR_Global::getOption()->get_is_display_comments()) { $msg .= empty($no_comments) ? "" : "<span class='pcat'>" . sprintf(OptionLocalization::get_term($localization_options, 'results_row_number_comments'), $no_comments) . "</span>"; } $msg .= "</div>"; // End of snippet bloc $msg .= "</div><hr>"; array_push($results, $msg); $i = $i + 1; } // $msg.='</div>'; if (count($results) < 0) { $search_result[] = 0; } else { $search_result[] = $results; } $fir = $wpsolr_query->get_start() + 1; $last = $wpsolr_query->get_start() + $wpsolr_query->get_nb_results_by_page(); if ($last > $found) { $last = $found; } $search_result[] = "<span class='infor'>" . sprintf(OptionLocalization::get_term($localization_options, 'results_header_pagination_numbers'), $fir, $last, $found) . "</span>"; return $search_result; }