/** * Check if a tag exists on a published post, to see if we should match this rewrite rule. * * @return Boolean Whether the tag exists on a published post. */ public static function rewrite_tag_exists($rule, $slug, $parameters) { $tags = explode(' ', $rule->named_arg_values['tag']); $tags = array_map('trim', $tags, array_fill(0, count($tags), '-')); $tags = array_map(array('Tags', 'get_one'), $tags); $initial_tag_count = count($tags); $tags = array_filter($tags); // Are all of the tags we asked for actual tags on this site? if (count($tags) != $initial_tag_count) { return false; } $tag_params = array(); foreach ($tags as $tag) { $tag_params[] = $tag->term_display; } return $tag instanceof Term && Posts::count_by_tag($tag_params, Post::status('published')) > 0; }
/** * Tag Archives * * Handle tag archive block output * * @param Block $block The block instance to be configured * @param Theme $theme The active theme */ public function action_block_content_tag_archives($block, $theme) { $tags = array(); $results = Tags::vocabulary()->get_tree(); foreach ($results as $result) { $count = ''; if ($block->show_counts) { $count = " (" . Posts::count_by_tag($result->term_display, "published") . ")"; } $url = URL::get('display_entries_by_tag', array('tag' => $result->term)); $tags[] = array('tag' => $result->term_display, 'count' => $count, 'url' => $url); } $block->tags = $tags; }
echo "<li><a href=\"{$page->permalink}\" title=\"{$page->title}\">{$page->title}</a></li>"; } ?> </ul> <?php } if ($tags) { ?> <h2><?php _e('Tags'); ?> </h2> <ul> <?php foreach ($tags as $tag) { $tag_count = Posts::count_by_tag($tag->tag, 'published'); echo "<li><a href=\"" . URL::get('display_entries_by_tag', 'tag=' . $tag->slug) . "\">{$tag->tag}</a></li>"; } ?> </ul> <?php } ?> <h2><?php _e('Meta'); ?> </h2> <ul> <?php if ($loggedin) {
/** * Get posts by tag */ public function test_get_posts_by_tag() { // setup $tags = array(); if (Tags::vocabulary()->get_term("laser")) { Tags::vocabulary()->delete_term("laser"); } if (Tags::vocabulary()->get_term("dog")) { Tags::vocabulary()->delete_term("dog"); } if (Tags::vocabulary()->get_term("name")) { Tags::vocabulary()->delete_term("name"); } $tags[] = Tags::vocabulary()->add_term("laser"); $five_tags = array("mattress", "freeze", "DOG", "Name", "hash"); foreach ($five_tags as $tag) { $tags[] = Tags::vocabulary()->add_term($tag); $count_before[$tag] = Posts::get(array('vocabulary' => array('tags:term' => $tag), 'count' => 'DISTINCT {posts}.id', 'ignore_permissions' => true, 'nolimit' => 1)); } for ($i = 1; $i <= 15; $i++) { $post_tags = array(); for ($j = 0; $j < 5; $j++) { if ($i % ($j + 2) == 0) { $post_tags[] = $five_tags[$j]; } } $post = Post::create(array('title' => "Test post {$i}", 'content' => count($post_tags) . " tags: " . implode(', ', $post_tags), 'user_id' => $this->user->id, 'status' => Post::status('published'), 'tags' => $post_tags, 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $post->info->testing_tag = 1; $post->info->commit(); } /** * At this point, these are the posts and their tags: * 1 (no tags) * 2: mattress * 3: freeze * 4: mattress, DOG * 5: Name * 6: mattress, freeze, hash * 7 (no tags) * 8: mattress, DOG * 9: freeze * 10: mattress, Name * 11 (no tags) * 12: mattress, freeze, DOG, hash * 13 (no tags) * 14: mattress * 15: freeze, Name */ $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n LEFT JOIN {object_terms} o ON p.id = o.object_id\n\t\t\t\tWHERE o.term_id IN (\n\t\t\t\t\tSELECT id FROM {terms} WHERE term_display = 'DOG'\n\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM vocabularies WHERE name = 'tags' )\n\t\t\t\t)\n\t\t\t"); // tags:term_display $post_count = Posts::get(array('vocabulary' => array('tags:term_display' => 'DOG'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $post_count = Posts::count_by_tag('DOG', Post::status('published')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Posts::count_by_tag(): {$post_count}"); // tags:term $post_count = Posts::get(array('vocabulary' => array('tags:term' => 'dog'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n LEFT JOIN {object_terms} o ON p.id = o.object_id\n\t\t\t\tWHERE o.term_id IN (\n\t\t\t\t\tSELECT id FROM {terms} WHERE term = 'name'\n\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM vocabularies WHERE name = 'tags' )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:term' => 'name'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n LEFT JOIN {object_terms} o ON\n p.id = o.object_id\n\t\t\t\tWHERE o.term_id IN (\n\t\t\t\t\tSELECT id FROM {terms} WHERE term in ( 'mattress', 'freeze' )\n\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:term' => array('mattress', 'freeze')), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); // tags:all:term $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id IN (\n\t\t\t\t\tSELECT o1.object_id FROM {object_terms} o1\n\t\t\t\t\t\tLEFT JOIN {object_terms} o2 ON\n\t\t\t\t\t\t\to1.object_id = o2.object_id AND\n\t\t\t\t\t\t\to1.term_id != o2.term_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\to1.term_id = ( SELECT id FROM {terms} WHERE term = 'mattress'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) ) AND\n\t\t\t\t\t\to2.term_id = ( SELECT id FROM {terms} WHERE term = 'freeze'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $any_count = $post_count; $post_count = count(Posts::get(array('vocabulary' => array('tags:all:term' => array('mattress', 'freeze')), 'ignore_permissions' => true, 'nolimit' => 1))); $this->assert_not_equal($any_count, $post_count, "Any: {$any_count} All: {$post_count}"); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); // tags:all:term_display $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id IN (\n\t\t\t\t\tSELECT o1.object_id FROM {object_terms} o1\n\t\t\t\t\t\tLEFT JOIN {object_terms} o2 ON\n\t\t\t\t\t\t\to1.object_id = o2.object_id AND\n\t\t\t\t\t\t\to1.term_id != o2.term_id\n\t\t\t\t\tWHERE\n\t\t\t\t\t\to1.term_id = ( SELECT id FROM {terms} WHERE term_display = 'Name'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) ) AND\n\t\t\t\t\t\to2.term_id = ( SELECT id FROM {terms} WHERE term_display = 'DOG'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $any_count = $post_count; $post_count = Posts::get(array('vocabulary' => array('tags:all:term' => array('Name', 'DOG')), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_not_equal($any_count, $post_count, "Any: {$any_count} All: {$post_count}"); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); // tags:not:term $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'laser'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:not:term' => 'laser'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'mattress'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t\tAND id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'freeze'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:not:term' => array('mattress', 'freeze')), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'laser'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t\tAND id IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'mattress'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:not:term' => 'laser', 'tags:term' => 'mattress'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); // tags:not:term_display $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term_display = 'DOG'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t\tAND id IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'mattress'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:not:term_display' => 'DOG', 'tags:term' => 'mattress'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); $sql_count = DB::get_value("SELECT COUNT(DISTINCT id) FROM {posts} p\n\t\t\t\tWHERE id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term_display = 'DOG'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t\tAND id NOT IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'freeze'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t\tAND id IN (\n\t\t\t\t\tSELECT object_id FROM {object_terms}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tterm_id in ( SELECT id FROM {terms} WHERE term = 'mattress'\n\t\t\t\t\t\t\tAND vocabulary_id = ( SELECT id FROM {vocabularies} WHERE name = 'tags' ) )\n\t\t\t\t)\n\t\t\t"); $post_count = Posts::get(array('vocabulary' => array('tags:not:term_display' => array('DOG', 'freeze'), 'tags:term' => 'mattress'), 'ignore_permissions' => true, 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_equal($sql_count, $post_count, "SQL: {$sql_count} Post: {$post_count}"); // teardown Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_tag', 'nolimit' => 1))->delete(); foreach ($tags as $tag) { Tags::vocabulary()->delete_term($tag); } }
private function add_count(&$tag) { if (in_array($tag->tag_text[0], $this->prefix)) { return false; } $tag->count = Posts::count_by_tag($tag->slug, "published"); $tag->step = ceil($tag->count / $this->step); return $tag; }