Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active June 16, 2016 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/ba7a7768cfd8ff30ce44 to your computer and use it in GitHub Desktop.
Save s-hiroshi/ba7a7768cfd8ff30ce44 to your computer and use it in GitHub Desktop.
WordPressの汎用的なカテゴリー処理をまとめたユーティリティークラスです。
<?php
/**
* InfoTown_Categories_Utilsのテストクラス
*
* @package InfoTown
* @subpackage tests
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
* @version 1.0.0
* @since 1.0.0
*/
class InfoTown_Categories_Utils_Test extends WP_UnitTestCase {
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
require_once( dirname( dirname( __FILE__ ) ) . '/inc/class-categories.php' );
}
public function test_custom_cat_ID() {
$cat_foo = $this->factory->category->create_and_get( [ 'name' => 'foo' ] );
$cat_bar = $this->factory->category->create_and_get( [ 'name' => 'bar' ] );
$actual_foo = InfoTown_Categories_Utils::custom_get_cat_id( 'foo' );
$actual_bar = InfoTown_Categories_Utils::custom_get_cat_id( 'bar' );
$this->assertEquals( $actual_foo, $cat_foo->term_id );
$this->assertEquals( $actual_bar, $cat_bar->term_id );
}
public function test_get_cat_id_by_slug() {
$cat_foo = $this->factory->category->create_and_get( [ 'slug' => 'foo' ] );
$cat_bar = $this->factory->category->create_and_get( [ 'slug' => 'bar' ] );
$actual_foo_id = InfoTown_Categories_Utils::get_cat_id_by_slug( 'foo' );
$actual_bar_id = InfoTown_Categories_Utils::get_cat_id_by_slug( 'bar' );
$this->assertEquals( $actual_foo_id, $cat_foo->term_id );
$this->assertEquals( $actual_bar_id, $cat_bar->term_id );
}
public function test_get_cats_id_by_post_id() {
$cats_id = [ ];
$cat1 = $this->factory->category->create_and_get(
[
'name' => 'cat name 1',
'slug' => 'cat-name-1'
]
);
array_push( $cats_id, $cat1->term_id );
$cat2 = $this->factory->category->create_and_get(
[
'name' => 'cat name 2',
'slug' => 'cat-name-2'
]
);
array_push( $cats_id, $cat2->term_id );
$cat3 = $this->factory->category->create_and_get(
[
'name' => 'cat name 3',
'slug' => 'cat-name-3'
]
);
array_push( $cats_id, $cat3->term_id );
$post = $this->factory->post->create_and_get( [ 'post_category' => $cats_id ] );
$actual = InfoTown_Categories_Utils::get_cats_id_by_post_id( $post->ID );
$this->assertEquals( $cats_id, $actual );
$actual = InfoTown_Categories_Utils::get_cats_id_by_post_id(
$post->ID,
[ $cat1->term_id, $cat2->term_id ]
);
$this->assertEquals( [ $cat3->term_id ], $actual );
}
public function test_get_cats_slug_by_post_id() {
$cats_id = [ ];
$slugs = [ ];
$cat1 = $this->factory->category->create_and_get(
[
'name' => 'cat name 1',
'slug' => 'cat-name-1'
]
);
array_push( $cats_id, $cat1->term_id );
array_push( $slugs, $cat1->slug );
$cat2 = $this->factory->category->create_and_get(
[
'name' => 'cat name 2',
'slug' => 'cat-name-2'
]
);
array_push( $cats_id, $cat2->term_id );
array_push( $slugs, $cat2->slug );
$cat3 = $this->factory->category->create_and_get(
[
'name' => 'cat name 3',
'slug' => 'cat-name-3'
]
);
array_push( $cats_id, $cat3->term_id );
array_push( $slugs, $cat3->slug );
$post = $this->factory->post->create_and_get( [ 'post_category' => $cats_id ] );
$actual = InfoTown_Categories_Utils::get_cats_slug_by_post_id( $post->ID );
$this->assertEquals( $actual, $slugs );
$actual = InfoTown_Categories_Utils::get_cats_slug_by_post_id(
$post->ID,
[ $cat1->slug, $cat2->slug ]
);
$this->assertEquals( [ $cat3->slug ], $actual );
}
public function test_get_categories_value_by_id() {
$cat = $this->factory->category->create_and_get();
$actual_slug = InfoTown_Categories_Utils::get_cat_value_by_id( $cat->term_id, 'slug' );
$actual_description = InfoTown_Categories_Utils::get_cat_value_by_id( $cat->term_id, 'description' );
$actual_taxonomy = InfoTown_Categories_Utils::get_cat_value_by_id( $cat->term_id, 'taxonomy' );
$this->assertEquals( $actual_slug, $cat->slug );
$this->assertEquals( $actual_description, $cat->description );
$this->assertEquals( $actual_taxonomy, $cat->taxonomy );
}
public function test_get_cats_value_by_post_id() {
$desc = [ ];
$cat1 = $this->factory->category->create_and_get( [ 'description' => 'Category 1.' ] );
array_push( $desc, $cat1->description );
$cat2 = $this->factory->category->create_and_get( [ 'description' => 'Category 2.' ] );
array_push( $desc, $cat2->description );
$post_id = $this->factory->post->create( [ 'post_category' => [ $cat1->term_id, $cat2->term_id ] ] );
$actual = InfoTown_Categories_Utils::get_cats_value_by_post_id( $post_id, 'description' );
$this->assertEquals( $desc, $actual );
}
public function test_get_cats_value_exclude_by_post_id() {
$desc = [ ];
$cat1 = $this->factory->category->create_and_get( [ 'description' => 'Category 1.' ] );
array_push( $desc, $cat1->description );
$cat2 = $this->factory->category->create_and_get( [ 'description' => 'Category 2.' ] );
$cat3 = $this->factory->category->create_and_get( [ 'description' => 'Category 3.' ] );
$post_id = $this->factory->post->create( [
'post_category' => [ $cat1->term_id, $cat2->term_id, $cat3->term_id ]
] );
$actual = InfoTown_Categories_Utils::get_cats_value_by_post_id(
$post_id,
'description',
[
$cat2->term_id,
$cat3->term_id
]
);
$this->assertEquals( $desc, $actual );
}
public function test_get_categories_value_by_slug() {
$cat = $this->factory->category->create_and_get( [ 'slug' => 'foo' ] );
$actual_term_id = InfoTown_Categories_Utils::get_cat_value_by_slug( 'foo', 'term_id' );
$actual_description = InfoTown_Categories_Utils::get_cat_value_by_slug( 'foo', 'description' );
$actual_taxonomy = InfoTown_Categories_Utils::get_cat_value_by_slug( 'foo', 'taxonomy' );
$this->assertEquals( $actual_term_id, $cat->term_id );
$this->assertEquals( $actual_description, $cat->description );
$this->assertEquals( $cat->taxonomy, $actual_taxonomy );
}
function test_get_single_cat_value_by_post_id() {
$cats_id = [ ];
for ( $i = 0; $i < 5; $i ++ ) {
$cat = $this->factory->category->create_and_get();
array_push( $cats_id, $cat->term_id );
}
$post = $this->factory->post->create_and_get( [ 'post_category' => $cats_id ] );
$post_id = $post->ID;
$cat_id = InfoTown_Categories_Utils::get_single_cat_value_by_post_id( $post_id, 'term_id' );
$this->assertTrue( in_array( $cat_id, $cats_id ) );
}
function test_get_value_current_cat() {
$cat = $this->factory->category->create_and_get( [
'slug' => 'foo',
'description' => 'This category is foo.'
] );
$this->factory->post->create( [ 'post_category' => [ $cat->term_id ] ] );
$this->go_to( '/?cat=' . $cat->term_id );
$actual = InfoTown_Categories_Utils::get_value_current_cat( 'description' );
$this->assertEquals( $cat->description, $actual );
}
public function test_is_category_id() {
$cat_a = $this->factory->category->create_and_get();
$cat_b = $this->factory->category->create_and_get();
$this->factory->post->create( [ 'post_category' => [ $cat_a->term_id, $cat_b->term_id ] ] );
$this->assertTrue( InfoTown_Categories_Utils::is_category_id( $cat_a->term_id ) );
$this->assertFalse( InfoTown_Categories_Utils::is_category_id( $cat_a->term_id + $cat_b->term_id ) );
}
public function test_sort_cats_by_key() {
$cat_y = $this->factory->category->create_and_get(
[
'name' => 'y',
'slug' => 'y-cat'
]
);
$cat_z = $this->factory->category->create_and_get(
[
'name' => 'z',
'slug' => 'z-cat',
]
);
$cat_a = $this->factory->category->create_and_get(
[
'name' => 'a',
'slug' => 'a-cat',
]
);
$cat_d = $this->factory->category->create_and_get(
[
'name' => 'd',
'slug' => 'd-cat',
]
);
$cats = [
$cat_y,
$cat_z,
$cat_a,
$cat_d,
];
$actual = InfoTown_Categories_Utils::sort_cats_by_key( $cats );
$expected = [
$cat_a,
$cat_d,
$cat_y,
$cat_z,
];
$this->assertEquals( $actual, $expected );
}
public function test_get_top_level_cats_id() {
$cats_id = [ ];
$cat = $this->factory->category->create_and_get( [ 'name' => 'example 1', 'slug' => 'example-1' ] );
array_push( $cats_id, $cat->term_id );
$cat = $this->factory->category->create_and_get( [ 'name' => 'example 2', 'slug' => 'example-2' ] );
array_push( $cats_id, $cat->term_id );
$cat = $this->factory->category->create_and_get( [ 'name' => 'example 3', 'slug' => 'example-3' ] );
array_push( $cats_id, $cat->term_id );
$this->factory->post->create( [ 'post_category' => $cats_id ] );
$actual = InfoTown_Categories_Utils::get_top_level_cats_id();
$this->assertEquals( $actual, $cats_id );
}
public function test_get_children_by_id() {
/*
* parent-1
* |-- child-1
* |-- grandson-1
* |-- child-2
* |-- grandson-2
*/
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_child2 = $this->factory->category->create_and_get(
[
'name' => 'Child 2',
'slug' => 'child-2',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$cat_grandson2 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 2',
'slug' => 'grandson-2',
'parent' => $cat_child2->term_id,
]
);
$this->factory->post->create(
[
'post_category' => [
$cat_parent->term_id,
$cat_child1->term_id,
$cat_child2->term_id,
$cat_grandson1->term_id,
$cat_grandson2->term_id
]
]
);
$cats_id = InfoTown_Categories_Utils::get_children_by_id( $cat_parent->term_id, 'id' );
$this->assertEquals(
$cats_id,
[ $cat_child1->term_id, $cat_child2->term_id ]
);
$slugs = InfoTown_Categories_Utils::get_children_by_id( $cat_parent->term_id, 'slug' );
$this->assertEquals(
$slugs,
[ $cat_child1->slug, $cat_child2->slug ]
);
}
function test_get_children_by_id_false() {
$cat_id = $this->factory->category->create();
$cat_id += 1;
$actual = InfoTown_Categories_Utils::get_children_by_id( $cat_id, 'id' );
$this->assertFalse( $actual );
}
function test_get_end_cat() {
/*
* parent-1
* |-- child-1
* |-- grandson-1
*/
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$cats = [
$cat_grandson1,
$cat_parent,
$cat_child1,
];
$actual = InfoTown_Categories_Utils::get_end_cat( $cats );
$this->assertEquals( $actual->term_id, $cat_grandson1->term_id );
}
function test_get_end_cat_multi() {
/*
* parent-1
* |-- child-1
* |-- grandson-1
* |-- child-2
* |-- grandson-2
*/
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_child2 = $this->factory->category->create_and_get(
[
'name' => 'Child 2',
'slug' => 'child-2',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$cat_grandson2 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 2',
'slug' => 'grandson-2',
'parent' => $cat_child2->term_id,
]
);
$cats = [
$cat_parent,
$cat_child2,
$cat_grandson2,
$cat_child1,
$cat_grandson1,
];
$this->factory->post->create(
[
'post_category' => [
$cat_parent->term_id,
$cat_child1->term_id,
$cat_grandson1->term_id,
$cat_child2->term_id,
$cat_grandson2->term_id,
]
]
);
$actual = InfoTown_Categories_Utils::get_end_cat( $cats );
$this->assertEquals( $actual->term_id, $cat_grandson2->term_id );
}
function test_get_end_cat_by_post_id() {
/*
* parent-1
* |-- child-1
* |-- grandson-1
*/
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$post = $this->factory->post->create_and_get(
[
'post_category' => [
$cat_parent->term_id,
$cat_child1->term_id,
$cat_grandson1->term_id,
]
]
);
$actual = InfoTown_Categories_Utils::get_end_cat_by_post_id( $post->ID );
$this->assertEquals( $actual->term_id, $cat_grandson1->term_id );
}
function test_custom_get_ancestors() {
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$ancestors = InfoTown_Categories_Utils::custom_get_ancestors( $cat_grandson1->term_id );
$this->assertEquals( $ancestors, [ $cat_child1->term_id, $cat_parent->term_id ] );
$ancestors = InfoTown_Categories_Utils::custom_get_ancestors( $cat_grandson1->term_id, 'reverse' );
$this->assertEquals( $ancestors, [ $cat_parent->term_id, $cat_child1->term_id ] );
$ancestors = InfoTown_Categories_Utils::custom_get_ancestors( $cat_parent->term_id );
$this->assertEmpty( $ancestors );
}
function test_get_records_for_cats_tree() {
/*
* parent-1
* |-- child-1
* |-- grandson-1
* |-- child-2
* |-- grandson-2
*/
$cat_parent = $this->factory->category->create_and_get(
[
'name' => 'Parent 1',
'slug' => 'parent-1'
]
);
$cat_child1 = $this->factory->category->create_and_get(
[
'name' => 'Child 1',
'slug' => 'child-1',
'parent' => $cat_parent->term_id,
]
);
$cat_child2 = $this->factory->category->create_and_get(
[
'name' => 'Child 2',
'slug' => 'child-2',
'parent' => $cat_parent->term_id,
]
);
$cat_grandson1 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 1',
'slug' => 'grandson-1',
'parent' => $cat_child1->term_id,
]
);
$cat_grandson2 = $this->factory->category->create_and_get(
[
'name' => 'Grandson 2',
'slug' => 'grandson-2',
'parent' => $cat_child2->term_id,
]
);
$this->factory->post->create(
[
'post_category' => [
$cat_parent->term_id,
$cat_child1->term_id,
$cat_grandson1->term_id,
$cat_child2->term_id,
$cat_grandson2->term_id,
]
]
);
$records = InfoTown_Categories_Utils::get_records_for_cats_tree(
$cat_parent->term_id,
$records = [ ]
);
$data = InfoTown_Categories_Utils::set_records_for_cats_tree( $records );
$actual = InfoTown_Categories_Utils::get_cats_tree( $data[0], [ $data[1][0] ] );
$expected = [
0 => [
'id' => $cat_parent->term_id,
'parent_id' => 0,
'name' => $cat_parent->name,
'children' => [
0 => [
'id' => $cat_child1->term_id,
'parent_id' => $cat_parent->term_id,
'name' => $cat_child1->name,
'children' => [
0 => [
'id' => $cat_grandson1->term_id,
'parent_id' => $cat_child1->term_id,
'name' => $cat_grandson1->name,
]
]
],
1 => [
'id' => $cat_child2->term_id,
'parent_id' => $cat_parent->term_id,
'name' => $cat_child2->name,
'children' => [
0 => [
'id' => $cat_grandson2->term_id,
'parent_id' => $cat_child2->term_id,
'name' => $cat_grandson2->name,
]
]
],
]
],
];
$this->assertEquals( $expected, $actual );
}
}
<?php
/**
* 日付処理
*
* @package InfoTown
* @subpackage Utils
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Utils_Date {
/**
* 日付の経過を検証
*
* 引数のDateTimeオジェジュクトが現在時刻から$diffで渡した期間を経過しているか検証します。
*
* @param \DateTime $past 比較対象のDateTimeオブジェクトです。
* @param string $diff 期間を指定します(P1Dなら1日経過、2週間ならP14D)。
*
* @return boolean 現在時刻から$diffが経過しているときはtrueを返し未経過のときはfalseを返します。
* @throws InvalidArgumentException 第1引数がDateTime型でないときはInvalidArgumentExceptionを返します。
*/
public static function is_day_pass( \DateTime $past, $diff = 'P7D' ) {
if ( ! $past instanceof \DateTime ) {
throw new InvalidArgumentException( 'DateTime型のオブジェクトではありません' );
}
$timezone = $past->getTimezone();
$now = new \DateTime( '', $timezone );
$past->add( new \DateInterval( $diff ) );
if ( $now > $past ) {
return true;
}
return false;
}
/**
* 指定時間を経過しているときオプションをもとにマークアップを構築
*
* @param \DateTime $past 現時刻と比較するDateTimeオブジェクトです。
* @param string $diff 期間を指定します。
* @param array $options 作成するマークアップ情報です。
*
* @return string 引数の\DateTimeオブジェクトが現在時刻から$diffが経過いるときプションにもとづき作成したマークアップをかえします。経過していないときは空文字を返します。
*/
public static function get_the_past( $past, $diff = 'P7D', array $options = array() ) {
$default = array(
'text' => 'Past',
'outer' => 'div',
'class' => '',
);
$options = array_merge( $default, $options );
$is_past = self::is_day_pass( $past, $diff );
$data = '';
if ( $is_past ) {
$class = ( ! empty( $options['class'] ) ) ? ' class="' . $options['class'] . '"' : '';
$data = '<' . $options['outer'] . $class . '>' . $options['text'] . '</' . $options['outer'] . '>';
}
return $data;
}
/**
* 指定時間が未経過のときオプションをもとに作成したマークアップ取得
*
* @param \DateTime $past 現時刻と比較するDateTimeオブジェクトです。
* @param string $diff 期間を指定します。
* @param array $options 作成するマークアップ情報です。
*
* @return string 引数の\DateTimeオブジェクトが現在時刻から$diff経過していないときプションにもとづき作成したマークアップを返し経過しているときは空文字を返します。
*/
public static function get_the_no_past( $past, $diff = 'P7D', array $options = array() ) {
$default = array(
'text' => 'No Past',
'outer' => 'div',
'class' => '',
);
$options = array_merge( $default, $options );
$is_past = self::is_day_pass( $past, $diff );
$data = '';
if ( ! $is_past ) {
$class = ( ! empty( $options['class'] ) ) ? ' class="' . $options['class'] . '"' : '';
$data = '<' . $options['outer'] . $class . '>' . $options['text'] . '</' . $options['outer'] . '>';
}
return $data;
}
/**
* Y/n/j文字列から英語表記の曜日取得
*
* @param string $raw Y/n/j形式の文字列です。
* @param string $timezone タイムゾーンです。
*
* @return string 曜日の英語表記を返します。
*
*/
public static function get_day( $raw, $timezone = '' ) {
$timezone = ( '' === $timezone ) ? date_default_timezone_get() : $timezone;
$datetime = new DateTime( $raw );
$datetime->setTimezone( new DateTimeZone( $timezone ) );
$datetime->format( 'Y/n/j' );
$day = $datetime->format( 'l' );
return $day;
}
/**
* 最も新しい投稿年取得
*
* @param array $args get_postsへ渡す引数です。
*
* @return bool|int 投稿年を返します。投稿がないときはfalseを返します。
*/
public static function get_latest_year( $args ) {
$default = array( 'order' => 'DESC' );
$args = array_merge( $default, $args );
$latest_posts = get_posts( $args );
wp_reset_postdata();
if ( ! empty( $latest_posts ) ) {
$dt = new \DateTime( $latest_posts[0]->post_date );
return (int) $dt->format( 'Y' );
}
return false;
}
/**
* 最も古い投稿年取得
*
* @param array $args get_postsへ渡す引数です。
*
* @return bool|int 投稿年を返します。投稿がないときはfalseを返します。
*/
public static function get_oldest_year( $args ) {
$default = array( 'order' => 'ASC' );
$args = array_merge( $default, $args );
$old_posts = get_posts( $args );
if ( ! empty( $old_posts ) ) {
$dt = new \DateTime( $old_posts[0]->post_date );
return (int) $dt->format( 'Y' );
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment