function build_document(Solarium\QueryType\Update\Query\Document\Document $doc, $post_info, $domain = NULL, $path = NULL) { $plugin_s4wp_settings = solr_options(); $exclude_ids = is_array($plugin_s4wp_settings['s4wp_exclude_pages']) ? $plugin_s4wp_settings['s4wp_exclude_pages'] : explode(',', $plugin_s4wp_settings['s4wp_exclude_pages']); $categoy_as_taxonomy = $plugin_s4wp_settings['s4wp_cat_as_taxo']; $index_comments = $plugin_s4wp_settings['s4wp_index_comments']; $index_custom_fields = is_array($plugin_s4wp_settings['s4wp_index_custom_fields']) ? $plugin_s4wp_settings['s4wp_index_custom_fields'] : explode(',', $plugin_s4wp_settings['s4wp_index_custom_fields']); if ($post_info) { # check if we need to exclude this document if (is_multisite() && in_array(substr(site_url(), 7) . $post_info->ID, (array) $exclude_ids)) { return NULL; } else { if (!is_multisite() && in_array($post_info->ID, (array) $exclude_ids)) { return NULL; } } $auth_info = get_userdata($post_info->post_author); # wpmu specific info if (is_multisite()) { // if we get here we expect that we've "switched" what blog we're running // as global $current_blog; $blogid = get_current_blog_id(); $doc->setField('solr_id', $blogid . '_' . $post_info->ID); $doc->setField('blogid', $blogid); $doc->setField('blogdomain', $domain); $doc->setField('blogpath', $path); $doc->setField('wp', 'multisite'); } else { $doc->setField('solr_id', $post_info->ID); } $doc->setField('ID', $post_info->ID); $doc->setField('permalink', get_permalink($post_info->ID)); $doc->setField('wp', 'wp'); $numcomments = 0; if ($index_comments) { $comments = get_comments("status=approve&post_id={$post_info->ID}"); foreach ($comments as $comment) { $doc->addField('comments', $comment->comment_content); $numcomments += 1; } } $doc->setField('post_name', $post_info->post_name); $doc->setField('post_title', $post_info->post_title); $doc->setField('post_content', strip_tags($post_info->post_content)); $doc->setField('comment_count', $numcomments); if (isset($auth_info->display_name)) { $doc->setField('post_author', $auth_info->display_name); } if (isset($auth_info->user_nicename)) { $doc->setField('author_s', get_author_posts_url($auth_info->ID, $auth_info->user_nicename)); } $doc->setField('post_type', $post_info->post_type); $doc->setField('post_date_gmt', $this->format_date($post_info->post_date_gmt)); $doc->setField('post_modified_gmt', $this->format_date($post_info->post_modified_gmt)); $doc->setField('post_date', $this->format_date($post_info->post_date)); $doc->setField('post_modified', $this->format_date($post_info->post_modified)); $doc->setField('displaydate', $post_info->post_date); $doc->setField('displaymodified', $post_info->post_modified); $doc->setField('post_status', $post_info->post_status); $doc->setField('post_parent', $post_info->post_parent); $doc->setField('post_excerpt', $post_info->post_excerpt); $doc->setField('post_status', $post_info->post_status); $categories = get_the_category($post_info->ID); if (!$categories == NULL) { foreach ($categories as $category) { if ($categoy_as_taxonomy) { $doc->addField('categories', get_category_parents($category->cat_ID, FALSE, '^^')); } else { $doc->addField('categories', $category->cat_name); } } } //get all the taxonomy names used by wp $taxonomies = (array) get_taxonomies(array('_builtin' => FALSE), 'names'); foreach ($taxonomies as $parent) { $terms = get_the_terms($post_info->ID, $parent); if ((array) $terms === $terms) { //we are creating *_taxonomy as dynamic fields using our schema //so lets set up all our taxonomies in that format $parent = $parent . "_taxonomy"; foreach ($terms as $term) { $doc->addField($parent, $term->name); } } } $tags = get_the_tags($post_info->ID); if (!$tags == NULL) { foreach ($tags as $tag) { $doc->addField('tags', $tag->name); } } if (count($index_custom_fields) > 0 && count($custom_fields = get_post_custom($post_info->ID))) { foreach ((array) $index_custom_fields as $field_name) { // test a php error notice. if (isset($custom_fields[$field_name])) { $field = (array) $custom_fields[$field_name]; foreach ($field as $key => $value) { $doc->addField($field_name . '_str', $value); $doc->addField($field_name . '_srch', $value); } } } } } else { // this will fire during blog sign up on multisite, not sure why _e('Post Information is NULL', 'solr4wp'); } return $doc; }
/** * Updates table fields by primary key. * DTO must contain primary key value. * * @param int $id * @return boolean */ public function updateByPK($dto) { //validating input params if ($dto == null) { throw new DebugException("The input param can not be NULL."); } $getPKFunc = $this->getCorrespondingFunctionName($dto->getMapArray(), $this->getPKFieldName(), "get"); $pk = $dto->{$getPKFunc}(); if (!isset($pk)) { throw new DebugException("The primary key is not set."); } $dto_fields = array_values($dto->getMapArray()); $db_fields = array_keys($dto->getMapArray()); $query = $this->getUpdateQuery(); $doc = new \Solarium\QueryType\Update\Query\Document\Document(); //$doc= $query->createDocument(); for ($i = 0; $i < count($dto_fields); $i++) { if ($dto_fields[$i] == $this->getPKFieldName()) { continue; } $functionName = "get" . ucfirst($dto_fields[$i]); $val = $dto->{$functionName}(); if (isset($val)) { $doc->setFieldModifier($db_fields[$i], 'set'); $doc->setField($db_fields[$i], $val); } } $doc->setKey($this->getPKFieldName(), $pk); //add document and commit $query->addDocument($doc)->addCommit(); // this executes the query and returns the result $result = $this->dbms->update($query); if ($result->getStatus() === 0) { return true; } return false; }