大きなコード集

【最終更新日:2011年09月18日】

公開されている投稿記事「ウェブル::初心者でも10秒でできる WordPress プラグインの作り方」を参考に50 行を超えるような大きなコードは独立したプラグインとして管理しています。詳細は、「Portfolio」にある「大きなコードの管理方法」を参照して下さい。

  1. CSVファイルのデータからテーブルを表示させるプラグイン
  2. 固定ページの管理画面に表示順位を表示するためのプラグイン

CSVファイルのデータからテーブルを表示させるプラグイン

本ブログではあまり利用していませんが、もう一つのブログ「俺流!テニス論 2.0」では本当のよく利用させて頂いています。CSV ファイルを作成すれば自動でテーブルに変換してくれます。参考にさせて頂いたのは「 Simple Colors::CSVファイルのデータからテーブルを表示させるショートコード」です。

<?php
/*
Plugin Name: AAA-csv2table
Plugin URI: http://www.warna.info/archives/1481/
Description: CSVファイルのデータからテーブルを表示させるプラグイン
Version: 1.2
Author: Zero Cool
Author URI: http://www.startat50.net/
*/
function table_shortcode( $atts ) {
    $default = array(
        'th'            => 'row',
        'caption'       => '',
        'file'          => '',
        'id'            => false,
        'charset'       => 'sjis-win',
        'table_id'      => '',
        'table_class'   => '',
        'tfoot'         => 'false',
    );
    $args = shortcode_atts( $default, $atts );

    if ( is_numeric( $args['id'] ) ) {
        $post = get_post( $args['id'] );
        if ( $post->post_mime_type == 'text/csv' ) {
            $args['file'] = $post->guid;
        }
    }

    $match_num = 0;
    if ( preg_match( '|^https?://|', $args['file'] ) && strpos( $args['file'], '../' ) === false ) {
        $file = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $args['file'], $match_num );
    } elseif ( preg_match( '|^/|', $args['file'] ) && strpos( $args['file'], '../' ) === false ) {
        $file = $_SERVER['DOCUMENT_ROOT'] . $args['file'];
        $match_num = 1;
    }

    if ( $match_num && file_exists( $file ) && is_readable( $file ) ) {
        $charset = in_array( strtolower( $args['charset'] ), array( 'utf-8', 'euc-jp', 'eucjp-win', 'sjis', 'sjis-win', 'iso-2022-jp' ) ) ? $args['charset'] : $default['charset'];
        $th = in_array( strtolower( $args['th'] ), array( 'col', 'row', 'both' ) ) ? strtolower( $args['th'] ) : $default['th'];
        $fh = fopen( $file, 'r' );
        if ( $fh ) {
            $row_num = $th == 'row' ? 1 : 0;
            $table_id = $args['table_id'] ? ' id="' . esc_attr( $args['table_id'] ) . '"' : '';
            $table_class = $args['table_class'] ? ' class="' . esc_attr( $args['table_class'] ) . '"' : '';
            $output = '<table' . $table_id . $table_class . '>' . "n";
            if ( $args['caption'] ) {
                $output .= '<caption>' . esc_html( $args['caption'] ) . "</caption>n";
            }
            while ( $row = Ps_fgetcsv_reg( $fh, $args['charset'] ) ) {
                if ( $row_num == 0 ) {
                    $row_class = 'head_row';
                } else {
                    $row_class = $row_num % 2 ? 'odd' : 'even';
                }
                if ( $row_num == 0 ) {
                    $output .= '<thead>' . "n";
                } elseif ( $row_num == 1 ) {
                    $output .= '<tbody>' . "n";
                }
                $line = '<tr class="' . $row_class . '">' . "n";

                for ( $i = 0; $i < count( $row ); $i++ ) {
                    $elm = ( $th != 'col' && $i == 0 ) || $row_num == 0 ? 'th' : 'td';
                    $line .= '<' . $elm . '>' . $row[$i] . '</' . $elm . ">n";
                }
                $line .= '</tr>' . "n";
                $output .= $line;
                if ( $row_num == 0 ) {
                    $output .= '</thead>' . "n";
                    if ( in_array( strtolower( $args['tfoot'] ), array( '1', 'true' ) ) ) {
                        $output .= '<tfoot>' . "n";
                        $output .= '<tr class="' . $row_class . '">' . "n";
                        $output .= $line;
                        $output .= '</tr>' . "n";
                        $output .= '</tfoot>' . "n";
                    }
                }
                $row_num++;
            }
            $output .= "</tbody>n";
            $output .= '</table>';
        }
    }
    return $output;
}
add_shortcode( 'csv2table', 'table_shortcode' );

function Ps_fgetcsv_reg ( &$handle, $charset = 'sjis-win', $length = null, $d = ',', $e = '"' ) {
    $d = preg_quote( $d );
    $e = preg_quote( $e );
    $_line = "";
    $eof = false;
    while ( ( $eof != true ) and ( ! feof( $handle ) ) ) {
        $_line .= ( empty( $length ) ? fgets( $handle ) : fgets( $handle, $length ) );
        $itemcnt = preg_match_all( '/'.$e.'/', $_line, $dummy );
        if ( $itemcnt % 2 == 0 ) $eof = true;
    }
    if ( strtolower( $charset ) != 'utf-8' ) {
        $_line = mb_convert_encoding( $_line, 'UTF-8', $charset );
    }
    $_csv_line = preg_replace( '/(?:rn|[rn])?$/', $d, trim( $_line ) );
    $_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/';
    preg_match_all( $_csv_pattern, $_csv_line, $_csv_matches );
    $_csv_data = $_csv_matches[1];
    for( $_csv_i = 0; $_csv_i < count( $_csv_data ); $_csv_i++ ) {
        $_csv_data[$_csv_i] = preg_replace( '/^'.$e.'(.*)'.$e.'$/s', '$1', $_csv_data[$_csv_i] );
        $_csv_data[$_csv_i] = str_replace( $e.$e, $e, $_csv_data[$_csv_i] );
    }
    return empty( $_line ) ? false : $_csv_data;
}
?>

固定ページの管理画面に表示順位を表示するためのプラグイン

「Simple Colors::WordPressの固定ページ一覧に順序の項目を追加する方法」を参考にしているプラグインです。管理画面から固定ページへ移動するとページの表示順序を表示してくれます。

<?php
/*
Plugin Name: AAA-Page Numbering
Plugin URI: http://www.warna.info/archives/1661/
Description: 固定ページの管理画面に表示順位を表示するためのプラグイン
Version: 1.0
Author: Zero Cool
Author URI: http://www.startat50.net
*/

function check_post_type_support_page_attr() {
    $all_post_types = get_post_types( array('show_ui' => true ), false );

    if ( !isset( $_REQUEST['post_type'] ) ) {
        $edit_post_type = 'post';
    } elseif ( in_array( $_REQUEST['post_type'], array_keys( $all_post_types ) ) ) {
        $edit_post_type = $_REQUEST['post_type'];
    } else {
        wp_die( __('Invalid post type') );
    }

    if ( post_type_supports( $edit_post_type, 'page-attributes' ) ) {
        add_filter( 'manage_' . $edit_post_type . '_posts_columns', 'add_menu_order_column' );
        add_action( 'admin_print_styles-edit.php', 'add_menu_order_column_styles' );
        add_filter( 'manage_edit-' . $edit_post_type . '_sortable_columns', 'add_menu_order_sortable_column' );
    }
}
add_action( 'load-edit.php' , 'check_post_type_support_page_attr' );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    add_action( 'admin_init', 'check_post_type_support_page_attr' );
}

function add_menu_order_column( $posts_columns ) {
    $new_columns = array();
    foreach ( $posts_columns as $column_name => $column_display_name ) {
        if ( $column_name == 'date' ) {
            $new_columns['order'] = __( 'Order' );
            add_action( 'manage_pages_custom_column', 'display_menu_order_column', 10, 2 );
            add_action( 'manage_posts_custom_column', 'display_menu_order_column', 10, 2 );
        }
        $new_columns[$column_name] = $column_display_name;
    }
    return $new_columns;
}

function add_menu_order_sortable_column( $sortable_column ) {
    $sortable_column['order'] = 'menu_order';
    return $sortable_column;
}

function display_menu_order_column( $column_name, $post_id ) {
    if ( $column_name == 'order' ) {
        $post_id = (int)$post_id;
        $post = get_post( $post_id );
        echo $post->menu_order;
    }
}

function add_menu_order_column_styles() {
?>
<style type="text/css" charset="utf-8">
.fixed .column-order {
    width: 7%;
    text-align: center;
}
</style>
<?php
}
?>

【公開日:2011年09月18日】