Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active May 25, 2016 03:21
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/c30424e395eed8e00ba5a24e56c009c1 to your computer and use it in GitHub Desktop.
Save s-hiroshi/c30424e395eed8e00ba5a24e56c009c1 to your computer and use it in GitHub Desktop.
WordPressで汎用的にページ(page)を処理するユーティリティークラスです。
<?php
/**
* InfoTown_Utils_Pagesテスト
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Utils_Pages_Test extends WP_UnitTestCase {
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
require_once( dirname( dirname( dirname( __FILE__ ) ) ) . '/inc/utils/class.pages.php' );
}
/**
* @group page
*/
public function test_get_page_link_by_slug() {
$expected = [];
$post_1st = $this->factory->post->create_and_get( [
'post_name' => '1st',
'post_title' => '1st page',
'post_type' => 'page',
] );
array_push($expected, $post_1st);
$post_2nd = $this->factory->post->create_and_get( [
'post_name' => '2nd',
'post_title' => '2nd page',
'post_type' => 'page',
'post_parent' => $post_1st->ID,
] );
array_push($expected, $post_2nd);
$post_3rd = $this->factory->post->create_and_get( [
'post_name' => '3rd',
'post_title' => '3rd page',
'post_type' => 'page',
'post_parent' => $post_2nd->ID,
] );
array_push($expected, $post_3rd);
$post_4th = $this->factory->post->create_and_get( [
'post_name' => '4th',
'post_title' => '4th page',
'post_type' => 'page',
'post_parent' => $post_3rd->ID,
] );
$actual = InfoTown_Utils_Pages::get_page_link_by_slug($post_4th->post_name);
$this->assertNotEmpty($actual);
}
/**
* @group page
*/
public function test_is_page_children() {
$expected = [];
$post_1st = $this->factory->post->create_and_get( [
'post_name' => '1st',
'post_title' => '1st page',
'post_type' => 'page',
] );
array_push($expected, $post_1st);
$post_2nd = $this->factory->post->create_and_get( [
'post_name' => '2nd',
'post_title' => '2nd page',
'post_type' => 'page',
'post_parent' => $post_1st->ID,
] );
array_push($expected, $post_2nd);
$post_3rd = $this->factory->post->create_and_get( [
'post_name' => '3rd',
'post_title' => '3rd page',
'post_type' => 'page',
'post_parent' => $post_2nd->ID,
] );
array_push($expected, $post_3rd);
$post_4th = $this->factory->post->create_and_get( [
'post_name' => '4th',
'post_title' => '4th page',
'post_type' => 'page',
'post_parent' => $post_3rd->ID,
] );
$actual = InfoTown_Utils_Pages::is_page_children($post_3rd->ID, $post_1st->ID);
$this->assertTrue($actual);
$actual = InfoTown_Utils_Pages::is_page_children($post_2nd->ID, $post_3rd->ID);
$this->assertFalse($actual);
}
}
<?php
/**
* ページ処理
*
* @package InfoTown
* @subpackage Utils
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Utils_Pages {
/**
* 固定ページスラッグからパーマリンク取得
*
* @param string $slug スタッグです。
*
* @link https://firegoby.jp/archives/4285
*
* @return mixed(string | boolean) 引数で指定したスラッグのパーマリングです。
*/
public static function get_page_link_by_slug( $slug ) {
if ( empty( $slug ) ) {
return false;
}
$page = get_posts( [
'post_type' => 'page',
'name' => $slug,
] );
if ( 1 !== count( $page ) ) {
return false;
}
return get_page_link( $page[0]->ID );
}
/**
* 固定ページスラッグからページID取得
*
* @param string $slug スタッグです。
*
* @link https://firegoby.jp/archives/4285
*
* @return mixed(string | boolean) 引数で指定したスラッグのパーマリングです。
*/
public static function get_page_id_by_slug( $slug ) {
if ( empty( $slug ) ) {
return false;
}
$page = get_posts( [
'post_type' => 'page',
'name' => $slug,
] );
if ( 1 !== count( $page ) ) {
return false;
}
return $page[0]->ID;
}
/**
* 子孫ページか検証
*
* 引数で与えられたページが子孫ページか検証します。
*
* @param integer $page_id 判定対象の固定ページIDです。
* @param integer $parent_id 親ページのIDです。
*
* @return boolean 子ページのときはtrue, そうでないときはfalseをかえします。
*/
public static function is_page_children( $page_id, $parent_id ) {
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( [
'post_type' => 'page',
'nopaging' => 'true',
] );
$children = get_page_children( $parent_id, $all_wp_pages );
foreach ( $children as $child ) {
if ( $page_id === $child->ID ) {
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment