コード例 #1
0
ファイル: relevanssi.php プロジェクト: realfluid/umbaugh
function relevanssi_index_doc($indexpost, $remove_first = false, $custom_fields = false) {
	global $wpdb, $relevanssi_table, $post;
    $post = $indexpost;
// END modified by renaissancehack
	if (!is_object($post)) {
// BEGIN modified by renaissancehack
//  modified query to get child records that inherit their post_status
		get_option('relevanssi_index_attachments') == 'on' ? $attachments = '' : $attachments = "AND post.post_type!='attachment'";
		$post = $wpdb->get_row("SELECT *,parent.post_status
			FROM $wpdb->posts parent, $wpdb->posts post WHERE
            (parent.post_status='publish' OR parent.post_status='private')
			AND post.ID=$post
            AND (
                (post.post_status='inherit'
                AND post.post_parent=parent.ID)
                OR
                (parent.ID=post.ID)
            )
            AND post.post_type!='nav_menu_item' AND post.post_type!='revision' $attachments");
// END modified by renaissancehack
		if (!$post) {
			// the post isn't public
			return;
		}
	}
	
	$index_type = get_option('relevanssi_index_type');
	$custom_types = explode(",", get_option('relevanssi_custom_types'));
	
	$index_this_post = false;
	switch ($index_type) {
		case 'posts':
			if ("post" == $post->post_type) $index_this_post = true;
			if (in_array($post->post_type, $custom_types)) $index_this_post = true;
			break;
		case 'pages':
			if ("page" == $post->post_type) $index_this_post = true;
			if (in_array($post->post_type, $custom_types)) $index_this_post = true;
			break;
		case 'public';
			if (function_exists('get_post_types')) {
				$public_types = get_post_types(array('exclude_from_search' => false));
				if (in_array($post->post_type, $public_types)) $index_this_post = true;
			}
			else {
				$index_this_post = true;
			}
			break;
		case 'both':
			$index_this_post = true;
			break;
	}
	
	if ($remove_first) {
		// we are updating a post, so remove the old stuff first
		relevanssi_remove_doc($post->ID);
	}

	// This needs to be here, after the call to relevanssi_remove_doc(), because otherwise
	// a post that's in the index but shouldn't be there won't get removed. A remote chance,
	// I mean who ever flips exclude_from_search between true and false once it's set, but
	// I'd like to cover all bases.
	if (!$index_this_post) return;

	$n = 0;	
	$titles = relevanssi_tokenize($post->post_title);
	

	//Added by OdditY - INDEX COMMENTS of the POST ->
	if ("none" != get_option("relevanssi_index_comments")) {
		$pcoms = relevanssi_get_comments($post->ID);
		if( $pcoms != "" ){
			$pcoms = relevanssi_strip_invisibles($pcoms);
			$pcoms = strip_tags($pcoms);
			$pcoms = relevanssi_tokenize($pcoms);		
			if (count($pcoms) > 0) {
				foreach ($pcoms as $pcom => $count) {
					if (strlen($pcom) < 2) continue;
					$n++;
					$wpdb->query("INSERT INTO $relevanssi_table (doc, term, tf, title)
					VALUES ($post->ID, '$pcom', $count, 3)");
				}
			}				
		}
	} //Added by OdditY END <-


	$taxonomies = array();
	//Added by OdditY - INDEX TAGs of the POST ->
	if ("on" == get_option("relevanssi_include_tags")) {
		array_push($taxonomies, "post_tag");
	} // Added by OdditY END <- 

	$custom_taxos = get_option("relevanssi_custom_taxonomies");
	if ("" != $custom_taxos) {
		$cts = explode(",", $custom_taxos);
		foreach ($cts as $taxon) {
			$taxon = trim($taxon);
			array_push($taxonomies, $taxon);
		}
	}

	// Then process all taxonomies, if any.
	foreach ($taxonomies as $taxonomy) {
		$n += index_taxonomy_terms($post, $taxonomy);
	}
	
	// index categories
	if ("on" == get_option("relevanssi_include_cats")) {
		$post_categories = get_the_category($post->ID);
		if (is_array($post_categories)) {
			foreach ($post_categories as $p_cat) {
				$cat_name = apply_filters("single_cat_title", $p_cat->cat_name);
				$cat_tokens = relevanssi_tokenize($cat_name);
				foreach ($cat_tokens as $pcat => $count) {
					if (strlen($pcat) < 2) continue;
					$n++;
					$wpdb->query("INSERT INTO $relevanssi_table (doc, term, tf, title)
					VALUES ($post->ID, '$pcat', $count, 4)");
				}
			}
		}
	}

	// index author
	if ("on" == get_option("relevanssi_index_author")) {
		$auth = $post->post_author;
		$display_name = $wpdb->get_var("SELECT display_name FROM $wpdb->users WHERE ID=$auth");
		$wpdb->query("INSERT INTO $relevanssi_table (doc, term, tf, title)
			VALUES ($post->ID, '$display_name', 1, 5)");
	}

	if ($custom_fields) {
		foreach ($custom_fields as $field) {
			$values = get_post_meta($post->ID, $field, false);
			if ("" == $values) continue;
			foreach ($values as $value) {
				// Custom field values are simply tacked to the end of the post content
				$post->post_content .= ' ' . $value;
			}
		}
	}

	if (isset($post->post_excerpt) && ("on" == get_option("relevanssi_index_excerpt") || "attachment" == $post->post_type)) { // include excerpt for attachments which use post_excerpt for captions - modified by renaissancehack
		$post->post_content .= ' ' . $post->post_excerpt;
	}
	
	$contents = relevanssi_strip_invisibles($post->post_content);

	if ('on' == get_option('relevanssi_expand_shortcodes')) {
		if (function_exists("do_shortcode")) {
			$contents = do_shortcode($contents);
		}
	}
	else {
		if (function_exists("strip_shortcodes")) {
			// WP 2.5 doesn't have the function
			$contents = strip_shortcodes($contents);
		}
	}
	
	$contents = strip_tags($contents);
	$contents = relevanssi_tokenize($contents);
	
	if (count($titles) > 0) {
		foreach ($titles as $title => $count) {
			if (strlen($title) < 2) continue;
			$n++;
			
			$wpdb->query("INSERT INTO $relevanssi_table (doc, term, tf, title)
			VALUES ($post->ID, '$title', $count, 1)");
			
			// a slightly clumsy way to handle titles, I'll try to come up with something better
		}
	}
	if (count($contents) > 0) {
		foreach ($contents as $content => $count) {
			if (strlen($content) < 2) continue;
			$n++;
			$wpdb->query("INSERT INTO $relevanssi_table (doc, term, tf, title)
			VALUES ($post->ID, '$content', $count, 0)");
		}
	}
	
	return $n;
}
コード例 #2
0
ファイル: indexing.php プロジェクト: WP-Panda/allergenics
function relevanssi_insert_edit($post_id)
{
    global $wpdb;
    $post_status = get_post_status($post_id);
    if ('auto-draft' == $post_status) {
        return;
    }
    if ($post_status == 'inherit') {
        $post_type = $wpdb->get_var("SELECT post_type FROM {$wpdb->posts} WHERE ID={$post_id}");
        $post_status = $wpdb->get_var("SELECT p.post_status FROM {$wpdb->posts} p, {$wpdb->posts} c WHERE c.ID={$post_id} AND c.post_parent=p.ID");
    }
    $index_statuses = apply_filters('relevanssi_valid_status', array('publish', 'private', 'draft', 'future', 'pending'));
    if (!in_array($post_status, $index_statuses)) {
        // The post isn't supposed to be indexed anymore, remove it from index
        relevanssi_remove_doc($post_id);
    } else {
        $bypassglobalpost = true;
        relevanssi_publish($post_id, $bypassglobalpost);
    }
}
コード例 #3
0
ファイル: relevanssi.php プロジェクト: voku/Relevanssi
function relevanssi_index_doc($indexpost, $remove_first = false, $custom_fields = false, $bypassglobalpost = false)
{
    global $wpdb, $relevanssi_table, $post;
    $post_was_null = false;
    $previous_post = NULL;
    if ($bypassglobalpost) {
        // if $bypassglobalpost is set, relevanssi_index_doc() will index the post object or post
        // ID as specified in $indexpost
        isset($post) ? $previous_post = $post : ($post_was_null = true);
        is_object($indexpost) ? $post = $indexpost : ($post = get_post($indexpost));
    } else {
        // Quick edit has an array in the global $post, so fetch the post ID for the post to edit.
        if (is_array($post)) {
            $post = $post['ID'];
        }
        if (!isset($post)) {
            // No $post set, so we need to use $indexpost, if it's a post object
            $post_was_null = true;
            if (is_object($indexpost)) {
                $post = $indexpost;
            }
        } else {
            // $post was set, let's grab the previous value in case we need it
            $previous_post = $post;
        }
        // At this point we should have something in $post; if not, quit.
        if ($post == NULL) {
            if ($post_was_null) {
                $post = null;
            }
            if ($previous_post) {
                $post = $previous_post;
            }
            return;
        }
        is_object($post) ? $ID = $post->ID : ($ID = $post);
    }
    // Finally fetch the post again by ID. Complicated, yes, but unless we do this, we might end
    // up indexing the post before the updates come in.
    $post = get_post($ID);
    $index_type = get_option('relevanssi_index_type');
    $custom_types = explode(",", get_option('relevanssi_custom_types'));
    $index_this_post = false;
    switch ($index_type) {
        case 'posts':
            if ("post" == $post->post_type) {
                $index_this_post = true;
            }
            if (in_array($post->post_type, $custom_types)) {
                $index_this_post = true;
            }
            break;
        case 'pages':
            if ("page" == $post->post_type) {
                $index_this_post = true;
            }
            if (in_array($post->post_type, $custom_types)) {
                $index_this_post = true;
            }
            break;
        case 'public':
            if (function_exists('get_post_types')) {
                $pt_1 = get_post_types(array('exclude_from_search' => '0'));
                $pt_2 = get_post_types(array('exclude_from_search' => false));
                $public_types = array_merge($pt_1, $pt_2);
                if (in_array($post->post_type, $public_types)) {
                    $index_this_post = true;
                }
            } else {
                $index_this_post = true;
            }
            break;
        case 'custom':
            if (in_array($post->post_type, $custom_types)) {
                $index_this_post = true;
            }
            break;
        case 'both':
            $index_this_post = true;
            break;
    }
    if ($post->post_type == 'attachment') {
        get_option('relevanssi_index_attachments') == 'on' ? $index_this_post = true : ($index_this_post = false);
    }
    if ($remove_first) {
        // we are updating a post, so remove the old stuff first
        relevanssi_remove_doc($post->ID);
        relevanssi_purge_excerpt_cache($post->ID);
    }
    // This needs to be here, after the call to relevanssi_remove_doc(), because otherwise
    // a post that's in the index but shouldn't be there won't get removed. A remote chance,
    // I mean who ever flips exclude_from_search between true and false once it's set, but
    // I'd like to cover all bases.
    if (!$index_this_post) {
        if ($post_was_null) {
            $post = null;
        }
        if ($previous_post) {
            $post = $previous_post;
        }
        return;
    }
    $n = 0;
    $min_word_length = get_option('relevanssi_min_word_length', 3);
    $titles = relevanssi_tokenize($post->post_title);
    //Added by OdditY - INDEX COMMENTS of the POST ->
    if ("none" != get_option("relevanssi_index_comments")) {
        $pcoms = relevanssi_get_comments($post->ID);
        if ($pcoms != "") {
            $pcoms = relevanssi_strip_invisibles($pcoms);
            $pcoms = strip_tags($pcoms);
            $pcoms = relevanssi_tokenize($pcoms);
            if (count($pcoms) > 0) {
                foreach ($pcoms as $pcom => $count) {
                    if (strlen($pcom) < $min_word_length) {
                        continue;
                    }
                    $n++;
                    $wpdb->query("INSERT INTO {$relevanssi_table} (doc, term, tf, title)\n\t\t\t\t\tVALUES ({$post->ID}, '{$pcom}', {$count}, 3)");
                }
            }
        }
    }
    //Added by OdditY END <-
    $taxonomies = array();
    //Added by OdditY - INDEX TAGs of the POST ->
    if ("on" == get_option("relevanssi_include_tags")) {
        array_push($taxonomies, "post_tag");
    }
    // Added by OdditY END <-
    $custom_taxos = get_option("relevanssi_custom_taxonomies");
    if ("" != $custom_taxos) {
        $cts = explode(",", $custom_taxos);
        foreach ($cts as $taxon) {
            $taxon = trim($taxon);
            array_push($taxonomies, $taxon);
        }
    }
    // Then process all taxonomies, if any.
    foreach ($taxonomies as $taxonomy) {
        $n += index_taxonomy_terms($post, $taxonomy);
    }
    // index categories
    if ("on" == get_option("relevanssi_include_cats")) {
        $post_categories = get_the_category($post->ID);
        if (is_array($post_categories)) {
            foreach ($post_categories as $p_cat) {
                $cat_name = apply_filters("single_cat_title", $p_cat->cat_name);
                $cat_tokens = relevanssi_tokenize($cat_name);
                foreach ($cat_tokens as $pcat => $count) {
                    if (strlen($pcat) < $min_word_length) {
                        continue;
                    }
                    $n++;
                    $wpdb->query("INSERT INTO {$relevanssi_table} (doc, term, tf, title)\n\t\t\t\t\tVALUES ({$post->ID}, '{$pcat}', {$count}, 4)");
                }
            }
        }
    }
    // index author
    if ("on" == get_option("relevanssi_index_author")) {
        $auth = $post->post_author;
        $display_name = $wpdb->get_var("SELECT display_name FROM {$wpdb->users} WHERE ID={$auth}");
        $names = relevanssi_tokenize($display_name, false);
        $names = apply_filters('relevanssi_index_author', $names, $post);
        $names = apply_filters('relevanssi_index_author-', $names, $post);
        foreach ($names as $name => $count) {
            $wpdb->query("INSERT INTO {$relevanssi_table} (doc, term, tf, title)\n\t\t\t\tVALUES ({$post->ID}, '{$name}', {$count}, 5)");
        }
    }
    if ($custom_fields) {
        foreach ($custom_fields as $field) {
            $values = get_post_meta($post->ID, $field, false);
            if ("" == $values) {
                continue;
            }
            foreach ($values as $value) {
                // Custom field values are simply tacked to the end of the post content
                $post->post_content .= ' ' . (is_array($value) ? implode(' ', $value) : $value);
            }
        }
    }
    if (isset($post->post_excerpt) && ("on" == get_option("relevanssi_index_excerpt") || "attachment" == $post->post_type)) {
        // include excerpt for attachments which use post_excerpt for captions - modified by renaissancehack
        $post->post_content .= ' ' . $post->post_excerpt;
    }
    $contents = $post->post_content;
    $contents = apply_filters('relevanssi_index_content', $contents, $post);
    $contents = apply_filters('relevanssi_index_content-' . $post->post_type, $contents, $post);
    if ('on' == get_option('relevanssi_expand_shortcodes')) {
        if (function_exists("do_shortcode")) {
            $contents = do_shortcode($contents);
        }
    } else {
        if (function_exists("strip_shortcodes")) {
            // WP 2.5 doesn't have the function
            $contents = strip_shortcodes($contents);
        }
    }
    $contents = relevanssi_strip_invisibles($contents);
    $contents = strip_tags($contents);
    $contents = relevanssi_tokenize($contents);
    if (count($titles) > 0) {
        foreach ($titles as $title => $count) {
            if (strlen($title) < $min_word_length) {
                continue;
            }
            $n++;
            $wpdb->query("INSERT INTO {$relevanssi_table} (doc, term, tf, title)\n\t\t\tVALUES ({$post->ID}, '{$title}', {$count}, 1)");
            // a slightly clumsy way to handle titles, I'll try to come up with something better
        }
    }
    if (count($contents) > 0) {
        foreach ($contents as $content => $count) {
            if (strlen($content) < $min_word_length) {
                continue;
            }
            $n++;
            $wpdb->query("INSERT INTO {$relevanssi_table} (doc, term, tf, title)\n\t\t\tVALUES ({$post->ID}, '{$content}', {$count}, 0)");
        }
    }
    // Restore the global $post to whatever it was.
    if ($post_was_null) {
        $post = null;
    }
    if ($previous_post) {
        $post = $previous_post;
    }
    return $n;
}