/** * @param $solarium_update_query * @param $post_to_index * @param null $attachment_body * * @return mixed * @internal param $solr_indexing_options */ public function create_solr_document_from_post_or_attachment($solarium_update_query, $post_to_index, $attachment_body = null) { $pid = $post_to_index->ID; $ptitle = $post_to_index->post_title; if (isset($attachment_body)) { // Post is an attachment: we get the document body from the function call $pcontent = $attachment_body; } else { // Post is NOT an attachment: we get the document body from the post object $pcontent = $post_to_index->post_content; } $pexcerpt = $post_to_index->post_excerpt; $pauth_info = get_userdata($post_to_index->post_author); $pauthor = isset($pauth_info) ? $pauth_info->display_name : ''; $pauthor_s = isset($pauth_info) ? get_author_posts_url($pauth_info->ID, $pauth_info->user_nicename) : ''; $ptype = $post_to_index->post_type; $pdate = solr_format_date($post_to_index->post_date_gmt); $pmodified = solr_format_date($post_to_index->post_modified_gmt); $pdisplaydate = $post_to_index->post_date; $pdisplaymodified = $post_to_index->post_modified; $purl = get_permalink($pid); $comments_con = array(); $comm = isset($this->solr_indexing_options[WpSolrSchema::_FIELD_NAME_COMMENTS]) ? $this->solr_indexing_options[WpSolrSchema::_FIELD_NAME_COMMENTS] : ''; $numcomments = 0; if ($comm) { $comments_con = array(); $comments = get_comments("status=approve&post_id={$post_to_index->ID}"); foreach ($comments as $comment) { array_push($comments_con, $comment->comment_content); $numcomments += 1; } } $pcomments = $comments_con; $pnumcomments = $numcomments; /* Get all custom categories selected for indexing, including 'category' */ $cats = array(); $taxo = $this->solr_indexing_options['taxonomies']; $aTaxo = explode(',', $taxo); $newTax = array(); // Add categories by default if (is_array($aTaxo) && count($aTaxo)) { } foreach ($aTaxo as $a) { if (substr($a, strlen($a) - 4, strlen($a)) == "_str") { $a = substr($a, 0, strlen($a) - 4); } // Add only non empty categories if (strlen(trim($a)) > 0) { array_push($newTax, $a); } } // Get all categories ot this post $term_names = wp_get_post_terms($post_to_index->ID, array('category'), array("fields" => "names")); if ($term_names && !is_wp_error($term_names)) { foreach ($term_names as $term_name) { array_push($cats, $term_name); } } // Get all tags of this port $tag_array = array(); $tags = get_the_tags($post_to_index->ID); if (!$tags == null) { foreach ($tags as $tag) { array_push($tag_array, $tag->name); } } $solarium_document_for_update = $solarium_update_query->createDocument(); $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_ID] = $pid; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_PID] = $pid; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TITLE] = $ptitle; if (isset($this->solr_indexing_options['p_excerpt']) && !empty($pexcerpt)) { // Index post excerpt, by adding it to the post content. // Excerpt can therefore be: searched, autocompleted, highlighted. $pcontent .= ' ' . $pexcerpt; } $content_with_shortcodes_expanded_or_stripped = $pcontent; if (isset($this->solr_indexing_options['is_shortcode_expanded']) && strpos($pcontent, '[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_index; $content_with_shortcodes_expanded_or_stripped = do_shortcode($pcontent); } // 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_with_shortcodes_expanded_or_stripped = preg_replace("~(?:\\[/?)[^\\]]+/?\\]~s", '', $content_with_shortcodes_expanded_or_stripped); # strip shortcodes, keep shortcode content; // Remove HTML tags $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CONTENT] = strip_tags($content_with_shortcodes_expanded_or_stripped); $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_AUTHOR] = $pauthor; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_AUTHOR_S] = $pauthor_s; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TYPE] = $ptype; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DATE] = $pdate; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_MODIFIED] = $pmodified; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DISPLAY_DATE] = $pdisplaydate; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DISPLAY_MODIFIED] = $pdisplaymodified; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_PERMALINK] = $purl; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_COMMENTS] = $pcomments; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_NUMBER_OF_COMMENTS] = $pnumcomments; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CATEGORIES] = $cats; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CATEGORIES_STR] = $cats; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TAGS] = $tag_array; $taxonomies = (array) get_taxonomies(array('_builtin' => false), 'names'); foreach ($taxonomies as $parent) { if (in_array($parent, $newTax)) { $terms = get_the_terms($post_to_index->ID, $parent); if ((array) $terms === $terms) { $parent = strtolower(str_replace(' ', '_', $parent)); $nm1 = $parent . '_str'; $nm2 = $parent . '_srch'; $nm1_array = array(); foreach ($terms as $term) { array_push($nm1_array, $term->name); } if (count($nm1_array) > 0) { $solarium_document_for_update->{$nm1} = $nm1_array; $solarium_document_for_update->{$nm2} = $nm1_array; } } } } // Add custom fields to the document $this->set_custom_fields($solarium_document_for_update, $post_to_index); // Last chance to customize the solarium update document $solarium_document_for_update = apply_filters(WpSolrFilters::WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE, $solarium_document_for_update, $this->solr_indexing_options, $post_to_index, $attachment_body); return $solarium_document_for_update; }
/** * @param $solarium_update_query * @param $post_to_index * @param null $attachment_body * * @return mixed * @internal param $solr_indexing_options */ public function create_solr_document_from_post_or_attachment($solarium_update_query, $post_to_index, $attachment_body = '') { $pid = $post_to_index->ID; $ptitle = $post_to_index->post_title; if (!empty($attachment_body)) { // Post is an attachment: we get the document body from the function call $pcontent = $attachment_body; } else { // Post is NOT an attachment: we get the document body from the post object $pcontent = empty($attachment_body) ? $post_to_index->post_content : $post_to_index->post_content . '. ' . $attachment_body; } $pexcerpt = $post_to_index->post_excerpt; $pauth_info = get_userdata($post_to_index->post_author); $pauthor = isset($pauth_info) ? $pauth_info->display_name : ''; $pauthor_s = isset($pauth_info) ? get_author_posts_url($pauth_info->ID, $pauth_info->user_nicename) : ''; // Get the current post language $post_language = apply_filters(WpSolrFilters::WPSOLR_FILTER_POST_LANGUAGE, null, $post_to_index); $ptype = $post_to_index->post_type; $pdate = solr_format_date($post_to_index->post_date_gmt); $pmodified = solr_format_date($post_to_index->post_modified_gmt); $pdisplaydate = $post_to_index->post_date; $pdisplaymodified = $post_to_index->post_modified; $purl = get_permalink($pid); $comments_con = array(); $comm = isset($this->solr_indexing_options[WpSolrSchema::_FIELD_NAME_COMMENTS]) ? $this->solr_indexing_options[WpSolrSchema::_FIELD_NAME_COMMENTS] : ''; $numcomments = 0; if ($comm) { $comments_con = array(); $comments = get_comments("status=approve&post_id={$post_to_index->ID}"); foreach ($comments as $comment) { array_push($comments_con, $comment->comment_content); $numcomments += 1; } } $pcomments = $comments_con; $pnumcomments = $numcomments; /* Get all custom categories selected for indexing, including 'category' */ $cats = array(); $categories_flat_hierarchies = array(); $categories_non_flat_hierarchies = array(); $taxo = $this->solr_indexing_options['taxonomies']; $aTaxo = explode(',', $taxo); $newTax = array(); // Add categories by default if (is_array($aTaxo) && count($aTaxo)) { } foreach ($aTaxo as $a) { if (substr($a, strlen($a) - 4, strlen($a)) == "_str") { $a = substr($a, 0, strlen($a) - 4); } // Add only non empty categories if (strlen(trim($a)) > 0) { array_push($newTax, $a); } } // Get all categories ot this post $terms = wp_get_post_terms($post_to_index->ID, array('category'), array('fields' => 'all_with_object_id')); if ($terms && !is_wp_error($terms)) { foreach ($terms as $term) { // Add category and it's parents $term_parents_names = array(); // Add parents in reverse order ( top-bottom) $term_parents_ids = array_reverse(get_ancestors($term->term_id, 'category')); array_push($term_parents_ids, $term->term_id); foreach ($term_parents_ids as $term_parent_id) { $term_parent = get_term($term_parent_id, 'category'); array_push($term_parents_names, $term_parent->name); // Add the term to the non-flat hierarchy (for filter queries on all the hierarchy levels) array_push($categories_non_flat_hierarchies, $term_parent->name); } // Add the term to the flat hierarchy array_push($categories_flat_hierarchies, implode(WpSolrSchema::FACET_HIERARCHY_SEPARATOR, $term_parents_names)); // Add the term to the categories array_push($cats, $term->name); } } // Get all tags of this port $tag_array = array(); $tags = get_the_tags($post_to_index->ID); if (!$tags == null) { foreach ($tags as $tag) { array_push($tag_array, $tag->name); } } $solarium_document_for_update = $solarium_update_query->createDocument(); if ($this->is_in_galaxy) { $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_BLOG_NAME_STR] = $this->galaxy_slave_filter_value; } $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_ID] = $this->generate_unique_post_id($pid); $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_PID] = $pid; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TITLE] = $ptitle; if (isset($this->solr_indexing_options['p_excerpt']) && !empty($pexcerpt)) { // Index post excerpt, by adding it to the post content. // Excerpt can therefore be: searched, autocompleted, highlighted. $pcontent .= self::CONTENT_SEPARATOR . $pexcerpt; } if (!empty($pcomments)) { // Index post comments, by adding it to the post content. // Excerpt can therefore be: searched, autocompleted, highlighted. //$pcontent .= self::CONTENT_SEPARATOR . implode( self::CONTENT_SEPARATOR, $pcomments ); } $content_with_shortcodes_expanded_or_stripped = $pcontent; if (isset($this->solr_indexing_options['is_shortcode_expanded']) && strpos($pcontent, '[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_index; $content_with_shortcodes_expanded_or_stripped = do_shortcode($pcontent); } // 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_with_shortcodes_expanded_or_stripped = str_replace(array('[', ']'), '', $content_with_shortcodes_expanded_or_stripped); # strip shortcodes, keep shortcode content; // Remove HTML tags $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CONTENT] = strip_tags($content_with_shortcodes_expanded_or_stripped); $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_AUTHOR] = $pauthor; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_AUTHOR_S] = $pauthor_s; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TYPE] = $ptype; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DATE] = $pdate; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_MODIFIED] = $pmodified; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DISPLAY_DATE] = $pdisplaydate; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_DISPLAY_MODIFIED] = $pdisplaymodified; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_PERMALINK] = $purl; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_COMMENTS] = $pcomments; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_NUMBER_OF_COMMENTS] = $pnumcomments; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CATEGORIES_STR] = $cats; // Hierarchy of categories $solarium_document_for_update[sprintf(WpSolrSchema::_FIELD_NAME_FLAT_HIERARCHY, WpSolrSchema::_FIELD_NAME_CATEGORIES_STR)] = $categories_flat_hierarchies; $solarium_document_for_update[sprintf(WpSolrSchema::_FIELD_NAME_NON_FLAT_HIERARCHY, WpSolrSchema::_FIELD_NAME_CATEGORIES_STR)] = $categories_non_flat_hierarchies; $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_TAGS] = $tag_array; // Index post thumbnail $this->index_post_thumbnails($solarium_document_for_update, $pid); // Index post url $this->index_post_url($solarium_document_for_update, $pid); $taxonomies = (array) get_taxonomies(array('_builtin' => false), 'names'); foreach ($taxonomies as $parent) { if (in_array($parent, $newTax)) { $terms = get_the_terms($post_to_index->ID, $parent); if ((array) $terms === $terms) { $parent = strtolower(str_replace(' ', '_', $parent)); $nm1 = $parent . '_str'; $nm2 = $parent . '_srch'; $nm1_array = array(); $taxonomy_non_flat_hierarchies = array(); $taxonomy_flat_hierarchies = array(); foreach ($terms as $term) { // Add taxonomy and it's parents $term_parents_names = array(); // Add parents in reverse order ( top-bottom) $term_parents_ids = array_reverse(get_ancestors($term->term_id, $parent)); array_push($term_parents_ids, $term->term_id); foreach ($term_parents_ids as $term_parent_id) { $term_parent = get_term($term_parent_id, $parent); array_push($term_parents_names, $term_parent->name); // Add the term to the non-flat hierarchy (for filter queries on all the hierarchy levels) array_push($taxonomy_non_flat_hierarchies, $term_parent->name); } // Add the term to the flat hierarchy array_push($taxonomy_flat_hierarchies, implode(WpSolrSchema::FACET_HIERARCHY_SEPARATOR, $term_parents_names)); // Add the term to the taxonomy array_push($nm1_array, $term->name); // Add the term to the categories searchable array_push($cats, $term->name); } if (count($nm1_array) > 0) { $solarium_document_for_update->{$nm1} = $nm1_array; $solarium_document_for_update->{$nm2} = $nm1_array; $solarium_document_for_update[sprintf(WpSolrSchema::_FIELD_NAME_FLAT_HIERARCHY, $nm1)] = $taxonomy_flat_hierarchies; $solarium_document_for_update[sprintf(WpSolrSchema::_FIELD_NAME_NON_FLAT_HIERARCHY, $nm1)] = $taxonomy_non_flat_hierarchies; } } } } // Set categories and custom taxonomies as searchable $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CATEGORIES] = $cats; // Add custom fields to the document $this->set_custom_fields($solarium_document_for_update, $post_to_index); if (isset($this->solr_indexing_options['p_custom_fields']) && isset($solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CUSTOM_FIELDS])) { $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CONTENT] .= self::CONTENT_SEPARATOR . implode(". ", $solarium_document_for_update[WpSolrSchema::_FIELD_NAME_CUSTOM_FIELDS]); } // Last chance to customize the solarium update document $solarium_document_for_update = apply_filters(WpSolrFilters::WPSOLR_FILTER_SOLARIUM_DOCUMENT_FOR_UPDATE, $solarium_document_for_update, $this->solr_indexing_options, $post_to_index, $attachment_body); return $solarium_document_for_update; }