2.13 共通関数

PREタグ待避・復元

コード

// PREタグ待避
//   $line_break = true :
//     pre コンテンツを改行でラップ
function hk_escape_pre( $content, &$pre_tags1, &$pre_tags2, $line_break ) {
    // <pre 有無チェック
    if ( strpos( $content, '</pre>' ) === false )            return $content;
    // </pre> で分割
    $content_parts = explode( '</pre>', $content );
    // 退避後の文字列初期化
    $content = '';
    // pre ブロックのカウント初期化
    $pre_i = 0;
    // 分割後文字列処理
    foreach ( $content_parts as $content_part ) {
        // <pre で三分割
        $blocks = preg_split( '/(<pre(?: [^>]*>|>))/i', $content_part, -1, PREG_SPLIT_DELIM_CAPTURE );
        // <pre が無い場合(最終part)
        if ( count( $blocks ) == 1 ) {
            $content .= $content_part;
            continue;
        }
        // preタグより前の文字列 $content に追加
        $content .= $blocks[ 0 ];
        // preタグより後の文字列
        $blocks[ 2 ] = trim( $blocks[ 2 ] );
        // <pre> </pre> 文字列
        if ( $line_break )
            $pre_block = $blocks[ 1 ]."\n".$blocks[ 2 ]."\n</pre>";
        else
            $pre_block = $blocks[ 1 ].$blocks[ 2 ].'</pre>';
        // 退避用代替文字列
        $pre_name = '<pre hk-pre-tag-'.$pre_i.'></pre>';
        // 退避データ保存
        $pre_tags1[ $pre_i ] = $pre_name;
        $pre_tags2[ $pre_i ] = $pre_block;
        // 代替文字列を $content に追加
        $content .= $pre_name;
        $pre_i++;
    }
    unset( $content_part );
    return $content;
}

// PREタグ復元
function hk_restore_pre( $content, $pre_tags1, $pre_tags2 ) {
    if ( empty( $pre_tags1 ) )            return $content;
    return str_replace( $pre_tags1, $pre_tags2, $content );
}

POSTされたデータ1個の取り込み

コード

// POSTされたデータ1個の取り込み
//   $type:1    :1行テキスト
//          1以外:複数行テキスト
//   戻り値:チェック後の文字列
function hk_func_post_read( $key, $type ) {
    if ( ! isset( $_POST[ $key ] ) )        return '';
    // データの取り込み
    $str_reg = $_POST[ $key ];
    // クォートの処理 
    $str_reg = stripslashes( $str_reg );
    // タブのチェック
    if ( preg_match( '/\t/', $str_reg ) )                    die;
    // 改行のチェック
    if ( $type == 1 ) {
        if ( preg_match( '/\r\n|[\r\n]/', $str_reg ) )       die;
    }
    else
        $str_reg = preg_replace( '/\r\n|[\r\n]/', "\n", $str_reg );
    // 先頭・末尾の無効文字削除
    $str_reg = trim( $str_reg );
    // 日本語文字変換
    $str_reg = mb_convert_encoding( $str_reg, "UTF-8", "auto" );
    return $str_reg;
}

POSTされたCSV数値データ取り込み

コード

// POSTされたCSV数値データ取り込み
//   戻り値
//     $csv_data:修正後の文字列
//     $rtc:-1:エラーの場合
function hk_func_post_csv_read( $key, $sp_str ) {
    $rtc = 0;
    // POSTされたデータの取り込み
    $csv_data = hk_func_post_read( $key, 1 );
    if ( $csv_data == $sp_str )       return array( $csv_data, $rtc );
    if ( $csv_data == '' )            return array( $csv_data, $rtc );
    // 半角SPACEを , に書き換え
    $csv_data = preg_replace( '/ /', ',', $csv_data );
    // 先頭・末尾の「,」削除
    $csv_data = trim( $csv_data, ',' );
    // 「,,」の処理
    $csv_data = preg_replace( '/,,+/', ',', $csv_data );
    // 数字とカンマだけで構成されているかチェック
    if (( $csv_data != '' ) && ( ! preg_match( '/^[0-9,]+$/', $csv_data ) ))    $rtc = -1;
    return array( $csv_data, $rtc );
}

特殊文字・16進数変換(DB読み出し・書き込み用)

コード

// 特殊文字を16進数に変換 DB書き込み用
function hk_func_spchar2hex( $str ) {
    $str = preg_replace( '/%/', '%25', $str );
    $str = preg_replace( '/&/', '%26', $str );
    $str = preg_replace( '/=/', '%3d', $str );
    $str = preg_replace( '/,/', '%2c', $str );
    return $str;
}

// 16進数を特殊文字に変換 DB読み出し用
function hk_func_hex2spchar( $str ) {
    $str = preg_replace( '/%2c/', ',',  $str );
    $str = preg_replace( '/%3d/', '=',  $str );
    $str = preg_replace( '/%26/', '&',  $str );
    $str = preg_replace( '/%25/', '%',  $str );
    return $str;
}

データ分解

コード

// データ分解 FULL
//   入力例:$db_data : 'aaa=123,456&bbb=789,012'
//   出力例:$key_value['aaa'] : array('123','456')
//           $key_value['bbb'] : array('789','012')
//   DB読み出し用
function hk_func_data_explode_full( $db_data ) {
    // 分解
    $db_data_array = explode( '&', $db_data );
    $key_value = array();
    foreach( $db_data_array as $data ) {
        list( $key, $value ) = explode( '=', $data );
        if ( $value != '' )
            $key_value[ $key ] = explode( ',', $value );
        else
            $key_value[ $key ] = array();
    }
    unset( $data );
    return $key_value;
}

// データ分解 HALF
//   入力例:$db_data : 'aaa=123,456&bbb=789,012'
//   出力例:$key_value['aaa'] : '123,456'
//           $key_value['bbb'] : '789,012'
//   DB読み出し用
function hk_func_data_explode_half( $db_data ) {
    // 分解
    $db_data_array = explode( '&', $db_data );
    $key_value = array();
    foreach( $db_data_array as $data ) {
        list( $key, $value ) = explode( '=', $data );
        $key_value[ $key ] = $value;
    }
    unset( $data );
    return $key_value;
}

サイト名

コード

// サイト名
//   特殊文字を通常文字へ変換
function hk_func_site_name() {
    $site_name = get_bloginfo('name');
    $site_name = htmlspecialchars_decode( $site_name, ENT_QUOTES );
    return $site_name;
}

スパムチェック(メール用)

コード

// スパムチェック(メール用)
// 戻り値:0:OK
//         1:NG http:
//         2:NG <html
function hk_func_spam_check( $content ) {
    if ( preg_match( '/http:/i', $content ) )    return 1;
    if ( preg_match( '/<html/i', $content ) )    return 2;
    return 0;
}

現在日時(メール用)

コード

// 現在日時(メール用)
function hk_func_get_local_time() {
    date_default_timezone_set('Asia/Tokyo');
    list( $sec, $min, $hour, $mday, $mon, $year, $wday ) = localtime();
    $w_j = array('日','月','火','水','木','金','土');
    $date = sprintf("%04d年%02d月%02d日(%s) %02d時%02d分",$year+1900,$mon+1,$mday,$w_j[$wday],$hour,$min);
    return $date;
}

順序項目 SELECT BOX 表示 投稿用

コード

// 順序項目 SELECT BOX 表示 投稿用
function hk_func_orderby_select( $selected ) {
    // 項目
    $selects = array(
        'modified' => '更新日',
        'date'     => '作成日',
        'title'    => 'タイトル',
    );
    // SELECT BOX 表示
    echo '<select name="orderby">'."\n";
    foreach ( $selects as $select => $name ) {
        echo '  <option';
        if ( $select == $selected )
            echo ' selected="selected"';
        echo ' value="'.$select.'">'.$name.'</option>'."\n";
    }
    unset( $select );
    unset( $name );
    echo "</select>\n";
    return;
}

順序項目 SELECT BOX 表示 メディア用

コード

// 順序項目 SELECT BOX 表示 メディア用
function hk_func_orderby_select_media( $selected ) {
    // 項目
    $selects = array(
        'title'  => 'ファイル',
        'parent' => 'アップロード先',
        'date'   => '作成日',
    );
    // SELECT BOX 表示
    echo '<select name="orderby">'."\n";
    foreach ( $selects as $select => $name ) {
        echo '  <option';
        if ( $select == $selected )
            echo ' selected="selected"';
        echo ' value="'.$select.'">'.$name.'</option>'."\n";
    }
    unset( $select );
    unset( $name );
    echo "</select>\n";
    return;
}

順序 SELECT BOX 表示

コード

// 順序 SELECT BOX 表示
function hk_func_order_select( $selected ) {
    // 項目
    $selects = array(
        'DESC' => '降順',
        'ASC'  => '昇順',
    );
    // SELECT BOX 表示
    echo '<select name="order">'."\n";
    foreach ( $selects as $select => $name ) {
        echo '  <option';
        if ( $select == $selected )
            echo ' selected="selected"';
        echo ' value="'.$select.'">'.$name.'</option>'."\n";
    }
    unset( $select );
    unset( $name );
    echo "</select>\n";
    return;
}

作成者 SELECT BOX 表示

コード

// 作成者 SELECT BOX 表示
function hk_func_author_select( $selected ) {
    echo '<select name="author">'."\n";
    echo '  <option value="">全作成者</option>'."\n";
    hk_func_author_select_sub( $selected );
    echo '</select>'."\n";
    return;
}

function hk_func_author_select_sub( $selected ) {
    function get_users_by_role( $role, $all_users ) {
        $args = array(
            'role'      => $role,
            'orderby'   => 'ID',
            'order'     => 'ASC',
            'fields'    => 'all',
        );
        $users = get_users( $args );
        foreach ( $users as $user ) {
            $user_id   = $user->ID;
            $user_name = $user->last_name.' '.$user->first_name;
            $all_users[ $user_id ] = $user_name;
        }
        unset( $user );
        return $all_users;
    }
    // 会員情報取得
    $all_users = array();
    //管理者
    $all_users = get_users_by_role( 'administrator', $all_users );
    // 編集者
    $all_users = get_users_by_role( 'editor'       , $all_users );
    // 投稿者
    $all_users = get_users_by_role( 'author'       , $all_users );
    // 寄稿者
    $all_users = get_users_by_role( 'contributor'  , $all_users );
    // OPTION 表示
    foreach ( $all_users as $user_id => $user_name ) {
        if ( $user_id == $selected )
                echo '  <option selected="selected" value="'.$user_id.'">'.$user_name.'</option>'."\n";
        else    echo '  <option value="'.$user_id.'">'.$user_name.'</option>'."\n";
    }
    unset( $user_id );
    unset( $user_name );
    return;
}

状態 SELECT BOX 表示

コード

// 状態 SELECT BOX 表示
function hk_func_status_select( $selected ) {
    // 項目
    $selects = array(
        'any'     => '全状態',
        'publish' => '公開中',
        'pending' => '承認待',
        'draft'   => '下書き',
        'future'  => '公開待',
        'private' => '非公開',
    );
    // SELECT BOX 表示
    echo '<select name="post_status">'."\n";
    foreach ( $selects as $select => $name ) {
        echo '  <option';
        if ( $select == $selected )
            echo ' selected="selected"';
        echo ' value="'.$select.'">'.$name.'</option>'."\n";
    }
    unset( $select );
    unset( $name );
    echo "</select>\n";
    return;
}

カテゴリタイプ 取得

コード

// カテゴリタイプ 取得
function hk_func_get_cat_type( $cat_id ) {
    $cat_obj  = get_category( $cat_id );
    $cat_type = $cat_obj->description;
    if ( $cat_type == 'date' )return $cat_type;
    return '';
}

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

更新日:2016/03/24
掲載日:2015/11/13