Last active
April 28, 2016 23:46
-
-
Save s-hiroshi/25504d7672e8d51c8e76 to your computer and use it in GitHub Desktop.
WordPressの汎用的なリンク処理をまとめたユーティリティークラスです。
This file contains hidden or 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 | |
| /** | |
| * リンク処理 | |
| * | |
| * @package InfoTown | |
| * @author Hiroshi Sawai <[email protected]> | |
| * @copyright Hiroshi Sawai | |
| */ | |
| class InfoTown_Links_Utils { | |
| /** | |
| * ページの上へを出力 | |
| * | |
| * @param array $options リンクのオプションです。 | |
| * | |
| * @return ページの上へのリンクを出力します。 | |
| */ | |
| public static function the_pagetop( $options = array() ) { | |
| $defaults = array( | |
| 'dest' => 'header', | |
| 'classname' => 'pagetop', | |
| 'text' => 'ページの上へ', | |
| ); | |
| $output = array_merge( $defaults, $options ); | |
| echo '<div class="' . esc_attr( $output['classname'] ) . '">' . | |
| '<a href="#' . esc_attr( $output['dest'] ) . '">' . esc_html( $output['text'] ) . '</a></div>'; | |
| return; | |
| } | |
| /** | |
| * 画像のリンク切れ判定 | |
| * | |
| * Basic認証などの閲覧制限を設定していないことを前提とします。 | |
| * WordPressはリンク切れを404.phpで処理するためHTTPステータスコードは404ではなく200が返ります。 | |
| * そのため画像のリンク切れはステータスコードではなく'Content-Type'で判断します。 | |
| * | |
| * @param integer $id attachment id. | |
| */ | |
| 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; | |
| } | |
| /** | |
| * guid(Global Unique Identifier )取得 | |
| * | |
| * 引数に該当する投稿のguidを返します。 | |
| * | |
| * @param $args get_postsの引数です。 | |
| * | |
| * @return array 渡された$argsで取得した投稿のguidの配列 | |
| */ | |
| function get_guids( $args ) { | |
| $posts_obj = get_posts( $args ); | |
| $guids = array(); | |
| foreach ( $posts_obj as $post_obj ) { | |
| array_push( $guids, $post_obj->guid ); | |
| } | |
| wp_reset_postdata(); | |
| return $guids; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment