Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active May 2, 2016 00:26
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/a8cf4947cb10691ff6ed213ce9b7499c to your computer and use it in GitHub Desktop.
Save s-hiroshi/a8cf4947cb10691ff6ed213ce9b7499c to your computer and use it in GitHub Desktop.
WordPressの汎用的なアタッチメント処理をまとめたユーティリティークラスです。
<?php
/**
* InfoTown_Attachments_Utilsのテスト
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Attachments_Utils_Test extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
require_once( dirname( dirname( __FILE__ ) ) . '/inc/class.attachments-utils.php' );
}
/**
* @link https://developer.wordpress.org/reference/functions/wp_insert_attachment/
* @link http://qiita.com/miya0001/items/0f8444948dbe8b256d36
*/
public function test_get_attachment_by_id() {
$post_id = $this->post_ids = $this->factory->post->create();
$attachment_id = $this->factory->attachment->create_object(
'image-' . $post_id . '.jpg',
$post_id,
array(
'post_title' => 'Example attachment',
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_excerpt' => 'Excerpt.',
'post_content' => 'Hello World.',
)
);
update_post_meta( $attachment_id, '_wp_attachment_image_alt', 'Alt Example.' );
$actual = InfoTown_Attachments_Utils::get_attachment_by_id( $attachment_id );
$this->assertEquals( $attachment_id, $actual['id'] );
$this->assertEquals( 'Example attachment', $actual['title'] );
$this->assertEquals( 'Excerpt.', $actual['excerpt'] );
$this->assertEquals( 'Hello World.', $actual['content'] );
$this->assertEquals( 'Alt Example.', $actual['alt'] );
}
}
<?php
/**
* アタッチメント処理
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Attachments_Utils {
/**
* アタッチメントID(投稿ID)からアタッチメント情報取得
*
* @param integer $attachment_id 取得対象のアタッチメントID(投稿ID)です。
*
* @return array アタッチメント情報の配列を返します。
*/
public static function get_attachment_by_id( $attachment_id ) {
$data = array();
$attachment = get_post( (int) $attachment_id );
$data['id'] = $attachment->ID;
$data['url'] = wp_get_attachment_url( $attachment->ID );
// 「メディアを編集」の代替テキスト
$data['alt'] = get_post_meta(
$attachment->ID,
'_wp_attachment_image_alt',
true
);
$data['title'] = $attachment->post_title; // 「メディアを編集」のタイトル
$data['excerpt'] = $attachment->post_excerpt; // 「メディアを編集」のキャプション
$data['content'] = $attachment->post_content; // 「メディアを編集」の説明
return $data;
}
/**
* アイキャッチ画像出力マークアップ取得
*
* 投稿IDに紐付いたアイキャッチ画像のマークアップを取得します。
*
* @link https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/
*
* @param integer $post_id 取得するアイキャッチがひも付いている投稿IDです。
*
* @return integer アタッチメントIDを返します。
*/
public static function get_the_eyecatch_id( $post_id ) {
$attachment_id = get_post_thumbnail_id( $post_id );
return $attachment_id;
}
/**
* アイキャッチ画像出力マークアップ取得
*
* 投稿IDに紐付いたアイキャッチ画像のマークアップを取得します。
*
* @link https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/
*
* @param integer $post_id 取得するアイキャッチがひも付いている投稿IDです。
* @param mixed(string | array) $size 取得サイズです。
*
* @return mixed|string|void アイキャッチ画像のマークアップを返します。
*/
public static function get_the_eyecatch( $post_id, $size = 'post-thumbnail' ) {
$output = get_the_post_thumbnail( $post_id, $size );
return $output;
}
/**
* 画像のリンク切れ判定
*
* Basic認証などの閲覧制限を設定していないことを前提とします。
* WordPressはリンク切れを404.phpで処理するためHTTPステータスコードは404ではなく200が返ります。
* そのため画像のリンク切れはステータスコードではなく'Content-Type'で判断します。
*
* @param integer $id attachment idです。
*
* @return boolean リンク切はtrue、そうでないときはfalseを返します。
*/
function is_image_dead_link( $id ) {
$image = wp_get_attachment_image_src( $id );
$response_header = get_headers( $image[0], 1 );
if ( false === strpos( $response_header['Content-Type'], 'image' ) ) {
return true;
}
return false;
}
/**
* Lazy Load Plugin for jQuery用出力マークアップ取得
*
* @link http://www.appelsiini.net/projects/lazyload
*
* @param integer $attachment_id アタッチメントIDです。
* @param mixed(string | array) $size 取得サイズです。
*
*/
public static function the_lazyload( $attachment_id, $size = 'post-thumbnail' ) {
$image = wp_get_attachment_image_src( $attachment_id, $size );
$html = '<img class="lazy" data-original="' . esc_url( $image[0] ) . '">';
echo( $html );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment