PHP

[PHP]zip

public function zip($zipfile,$add_file)
{
    $zip = new ZipArchive();
    $result = $zip->open($zipfile, ZipArchive::CREATE);
    if($result === true)
    {
        $zip->addFile($add_file);
        $result = $zip->close();
    }
    return $result === true;
}

[PHP]echo_json

function echo_json($data)
{
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($data);
}

[PHP]is_https

public function is_https()
{
    if(!empty($_SERVER["HTTPS"]))
        return true;
    //AWS
    if(!empty($_SERVER["HTTP_X_FORWARDED_PROTO"]) && strtolower($_SERVER["HTTP_X_FORWARDED_PROTO"])==="https")
        return true;
    return false;
}

[PHP]array_group_by

SQLのGROUP BYの様な処理を配列に行う

public function array_group_by($arr,$keys = array(array('id','address'),array('name') ),$index = 0)
{
    if(empty($arr))
        return null;
    $results = array();
    $children = array();
    $_vkey = $keys[$index][0];
    $_keys = array_slice($keys,0,$index+1);
    $is_final = count($keys) <= $index + 1;
    $id = null;
    array_push($arr,null);
    foreach ($arr as $i => $data){
        $current_id = $this->get_values_by_keys($data,$_keys,'');
        if($id != $current_id){
            if(!is_null($id)) {
                if(!empty($children)) {
                    $tmp = $this->get_std_by_array($children[0], $keys[$index]);
                    $tmp->value = $children[0]->$_vkey;
                }else{
                    $tmp = new stdClass();
                }
                $tmp->children = $is_final ? $children : $this->array_group_by($children,$keys,$index+1);
                $results[] = $tmp;
                $children = array();
            }
            $id = $current_id;
        }
        $children[] = $data;
    }
    return $results;
}
public function get_std_by_array($arr,$include_keys = null)
{
    $tmp = new stdClass();
    foreach ($arr as $key => $v){
        if(empty($include_keys) || (!empty($include_keys) && in_array($key,$include_keys)))
            $tmp->$key = $v;
    }
    return $tmp;
}
public function get_values_by_keys($data,$keys,$glue = null)
{
    if(empty($data))
        return null;
    $tmp = array();
    foreach ($keys as $key){
        if(is_array($key))
            $key = array_shift(array_values($key));
        $tmp[] = $data->$key;
    }
    return is_null($glue) ? $tmp : implode($glue,$tmp);
}

[PHP,MT]phpでMTのデータ取得

phpでMTのデータ取得

public $basepath = '';
public $cfg_file = 'mt-config.cgi';
public $blog_id = null;
public $mt;

public function __construct($blog_id = null)
{
    $this->basepath = dirname(dirname(dirname(__FILE__))) . '/mt';
    set_include_path(get_include_path() . PATH_SEPARATOR . $this->basepath . '/php' . PATH_SEPARATOR . $this->basepath . '/php/extlib' . PATH_SEPARATOR . $this->basepath . '/MT/php/lib');
    require_once('mt.php');
    $this->init_mt($blog_id);
}

public function init_mt($blog_id = null)
{
    $this->blog_id = $blog_id;
    $this->mt = MT::get_instance($blog_id, $this->cfg_file);
}

public function get_custom_fields_by_entry_id($entry_id)
{
    $sql[] = "SELECT";
    $sql[] = "entry_meta_entry_id as entry_id,";
    $sql[] = "REPLACE(entry_meta_type,'field.','') as name,";
    $sql[] = "(CASE";
    $sql[] = "WHEN entry_meta_vchar IS NOT NULL THEN entry_meta_vchar";
    $sql[] = "WHEN entry_meta_vchar_idx IS NOT NULL THEN entry_meta_vchar_idx";
    $sql[] = "WHEN entry_meta_vdatetime IS NOT NULL THEN entry_meta_vdatetime";
    $sql[] = "WHEN entry_meta_vdatetime_idx IS NOT NULL THEN entry_meta_vdatetime_idx";
    $sql[] = "WHEN entry_meta_vinteger IS NOT NULL THEN entry_meta_vinteger";
    $sql[] = "WHEN entry_meta_vinteger_idx IS NOT NULL THEN entry_meta_vinteger_idx";
    $sql[] = "WHEN entry_meta_vfloat IS NOT NULL THEN entry_meta_vfloat";
    $sql[] = "WHEN entry_meta_vfloat_idx IS NOT NULL THEN entry_meta_vfloat_idx";
    $sql[] = "WHEN entry_meta_vblob IS NOT NULL THEN entry_meta_vblob";
    $sql[] = "WHEN entry_meta_vclob IS NOT NULL THEN entry_meta_vclob";
    $sql[] = "ELSE NULL END) as value";
    $sql[] = "FROM mt_entry_meta WHERE entry_meta_entry_id " . $this->_get_sql_for_in($entry_id,true);
    $sql = implode(' ',$sql);
    $records = $this->mt->db()->execute($sql);
    $records = $this->get_array_by_records($records);
    
    $tmp = array();
    if(!is_array($entry_id)){
        foreach ($records as $rec) {
            $tmp[$rec['name']] = $rec['value'];
        }
        return $tmp;
    }
    
    return $records;
}
protected function _get_sql_for_in($values, $is_number = false)
{
    if ($is_number)
        return is_array($values) && 1 < count($values) ? "IN (" . implode(",", $values) . ")" : (is_array($values) ? "= {$values[0]}" : "= $values");
    return is_array($values) && 1 < count($values) ? "IN ('" . implode("','", $values) . "')" : (is_array($values) ? "= '{$values[0]}'" : "= '$values'");
}
public function get_array_by_records($records)
{
    $result = array();
    if (!$records){
    }else {
        while ($item = $records->fetchRow()) {
            $result[] = $item;
        }
    }
    return $result;
}

[WordPress]メインクエリ停止

//メインクエリ停止
function custom_posts_request( $sql, &$query ) {
    if ( is_admin() || !$query->is_main_query() ){
        return $sql;
    }
    if ( $query->is_main_query() ) {
        /* prevent SELECT FOUND_ROWS() query*/
        $query->query_vars['no_found_rows'] = true;
        /* prevent post term and meta cache update queries */
        $query->query_vars['cache_results'] = false;
        return false;
    }
    return $sql;
}
//posts_request : post 配列を返す SQL クエリを実行する直前に、クエリ全体に対して適用される。
add_filter( 'posts_request', 'custom_posts_request', 10, 2 );

[WordPress]doing_wp_cronが付いてたらリダイレクト

define(‘ALTERNATE_WP_CRON’, true);の時に発生

if(isset($_GET['doing_wp_cron'])){
    header('Location: '.get_current_url(false,array('doing_wp_cron')));
    exit;
}
public function get_current_url($remove_get_query = false,$exclude_keys = array())
{
    $suffix = $_SERVER['REQUEST_URI'];
    if($remove_get_query){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
    }else if(!empty($exclude_keys)){
        $tmp = explode('?',$suffix);
        $suffix = array_shift($tmp);
        $get = get_get_queries($exclude_keys);
        if(!empty($get))
            $suffix .= '?' . http_build_query($get);
    }
    return get_base_url($suffix);
}
public function get_get_queries($exclude_keys = array())
{
    $result = array();
    foreach ($_GET as $key => $value){
        if(!empty($exclude_keys) && in_array($key,$exclude_keys)){
            continue;
        }
        $result[$key] = $value;
    }
    return $result;
}
public function get_base_url($suffix = '')
{
    return (empty($_SERVER["HTTPS"]) ? "http://" : "https://") . $_SERVER["HTTP_HOST"] . $suffix;
}