-
-
Save inc2734/8db479bca8e3db6c86bbcdf8d6d086a2 to your computer and use it in GitHub Desktop.
Smart Custom Fields リレーションフィールドの速度調査
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Smart_Custom_Fields_Test_Sample extends WP_UnitTestCase { | |
/** | |
* @var int | |
*/ | |
protected $post_id; | |
/** | |
* setUp | |
*/ | |
public function setUp() { | |
parent::setUp(); | |
$this->post_ids = $this->factory->post->create_many( 5000, array( | |
'post_type' => 'post', | |
) ); | |
$this->post_id = $this->post_ids[0]; | |
$this->news_ids = $this->factory->post->create_many( 5000, array( | |
'post_type' => 'news', | |
) ); | |
$this->news_id = $this->news_ids[0]; | |
add_filter( 'smart-cf-register-fields', array( $this, '_register' ), 10, 4 ); | |
SCF::clear_all_cache(); | |
} | |
/** | |
* tearDown | |
*/ | |
public function tearDown() { | |
parent::tearDown(); | |
SCF::clear_all_cache(); | |
} | |
/** | |
* @group temp | |
*/ | |
public function test_sample() { | |
var_dump( 'Start' ); | |
foreach ( $this->post_ids as $post_id ) { | |
$rand_key = array_rand( $this->news_ids ); | |
$rand_news_id = $this->news_ids[$rand_key]; | |
add_post_meta( $post_id, 'relation', $rand_news_id ); | |
} | |
$time_start = microtime(true); | |
$news = get_posts( array( | |
'post_type' => 'news', | |
'posts_per_page' => -1, | |
'post__in' => SCF::get( 'relation', $this->post_id ), | |
) ); | |
var_dump( '投稿ID ' . $this->post_id . ' が関連付けしたニュース: ' . count( $news ) ); | |
$time_end = microtime(true); | |
echo "\n" . $time_end - $time_start . " seconds\n"; | |
$time_start = microtime(true); | |
$posts = get_posts( array( | |
'post_type' => 'post', | |
'posts_per_page' => -1, | |
'meta_query' => array( | |
array( | |
'key' => 'relation', | |
'value' => $rand_news_id, | |
'type' => 'NUMERIC', | |
'compare' => '=', | |
), | |
), | |
) ); | |
var_dump( 'ニュースID ' . $rand_news_id . ' を関連付けしている投稿: ' . count( $posts ) ); | |
$time_end = microtime(true); | |
echo "\n" . $time_end - $time_start . " seconds\n"; | |
} | |
/** | |
* フック経由でカスタムフィールドを設定 | |
* | |
* @param array $settings 管理画面で設定された Smart_Custom_Fields_Setting の配列 | |
* @param string $type 投稿タイプ or ロール | |
* @param int $id 投稿ID or ユーザーID | |
* @param string $meta_type メタデータのタイプ。post or user | |
* @return array | |
*/ | |
public function _register( $settings, $type, $id, $meta_type ) { | |
// SCF::add_setting( 'ユニークなID', 'メタボックスのタイトル' ); | |
if ( $type === 'post' && $id === $this->post_id ) { | |
$Setting = SCF::add_setting( 'id-1', 'Register Test' ); | |
// $Setting->add_group( 'ユニークなID', 繰り返し可能か, カスタムフィールドの配列 ); | |
$Setting->add_group( 0, false, array( | |
array( | |
'name' => 'relation', | |
'label' => 'relation', | |
'type' => 'relation', | |
'post-type' => array( 'news' ), | |
), | |
) ); | |
$settings['id-1'] = $Setting; | |
} | |
return $settings; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment