[PHP]idiorm #10 カラム名取得

public function get_columns($table_name,$ignore_clumns = null)
{
    $result = ORM::for_table('information_schema.columns')
    ->select('column_name')
    ->where('table_name',$table_name ) 
    ->order_by_expr('ordinal_position')
    ->find_array();
    $columns = array_column($result,'column_name');
    if(empty($columns)){
        $result = ORM::raw_execute("show columns from {$table_name}");
        $statement = ORM::get_last_statement();
        $rows = [];
        $columns = [];
        while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
            $columns[] = $row['Field'];
        }
    }
    if(!empty($ignore_clumns)){
        $columns = array_diff($columns,$ignore_clumns);
    }
    return $columns;
}

[PHP]idiorm #9 create

$record = ORM::for_table('posts')->create();
$record->number = $post['number'];
$record->name = $post['name'];
$record->save();

[JavaScript]スクロール位置を保存してリロード

    /**
     * initWithScrollY
     *
     * @static
     */
    static initWithScrollY(){
        let scrollY = localStorage.getItem('scrollKey');
        if(!scrollY){
            return;
        }
        localStorage.removeItem('scrollKey');
        scrollY = Number(scrollY);
        window.scrollTo(0, scrollY);
    }
    /**
     * ローカルストレージ取得
     *
     * @static
     * @param {String} key
     * @param {Boolean} remove
     * @returns {*}
     */
    static getLS(key,remove){
        const value = localStorage.getItem(key);
        if(remove){
            localStorage.removeItem(key);
        }
        return value;
    }
    /**
     * スクロール位置を保存しつつリロード
     *
     * @static
     * @param {*} delay
     */
    static reloadWithScrollY(delay){
        if(delay){
            setTimeout(()=>{
                Utils.reloadWithScrollY();
            },delay);
        }else{
            localStorage.setItem('scrollKey',window.scrollY);
            location.reload();
        }
    }
    /**
     * ローカルストレージ保存
     *
     * @static
     */
    static setLS(key,value){
        localStorage.setItem(key,value);
    }
    /**
     * スクロール位置を保存
     *
     * @static
     */
    static setScrollYToLS(){
        localStorage.setItem('scrollKey',window.scrollY);
    }

[WordPress]カスタム投稿タイプ記事一覧にタクソノミー絞り込み機能を追加

functions.php

/**
 * WP_Termの配列の各要素にterm_ordersを設定する
 *
 * @param array $terms
 * 
 */
function set_parents_term_orders_to_terms(&$terms)
{
    if(empty($terms)){
        return;
    }
    foreach ($terms as $i => $term) {
        $terms[$i]->term_orders = [$term->term_order];
        if(!empty($term->parent)){
            $tmp = $term;
            while(!empty($tmp->parent)){
                $tmp_id = $tmp->term_id;
                foreach ($terms as $j => $term_) {
                    if($tmp->parent === $term_->term_id){
                        array_unshift($terms[$i]->term_orders,$term_->term_order);
                        $tmp = $term_;
                        break;
                    }
                }
                if($tmp_id === $tmp->term_id){
                    break;
                }
            }
        }
    }
}
/**
 * WP_Termの配列をterm_ordersに則ってソートする
 * set_parents_term_orders_to_terms を実行してから使用する
 *
 * @param array $terms
 * 
 */
function sort_terms_by_term_orders(&$terms)
{
    usort($terms,function($a,$b){
        $imax = max(count($a->term_orders),count($b->term_orders));
        for($i=0;$i<$imax;$i++){
            if(count($a->term_orders) <= $i){
                return -1;
            }
            if(count($b->term_orders) <= $i){
                return 1;
            }
            if($a->term_orders[$i] === $b->term_orders[$i]){
                continue;
            }
            return ($a->term_orders[$i] > $b->term_orders[$i]) ? 1 : -1;
        }
    });
}
/**
 * 管理画面記事一覧カテゴリフィルタ追加
 */
function add_post_taxonomy_restrict_filter()
{
global $post_type;
if($post_type === 'カスタム投稿タイプ'){
?>
<select name="タクソノミー">
<option value="">カテゴリーを指定する</option>
<?php
$terms = get_terms('タクソノミー');
/*
プラグイン「Category Order and Taxonomy Terms Order」導入済みの場合
set_parents_term_orders_to_terms($terms);
sort_terms_by_term_orders($terms);
*/
foreach ($terms as $term) { ?>
<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<?php
}
}
add_action('restrict_manage_posts', 'add_post_taxonomy_restrict_filter');

[Shopify]BuyerJourneyでチェックアウト画面の進行をブロックする

import {
  reactExtension,
  useBuyerJourneyIntercept,
} from '@shopify/ui-extensions-react/checkout';

export default reactExtension(
  'purchase.checkout.block.render',
  () => <Extension />,
);

function Extension() {
    useBuyerJourneyIntercept(
        ({ canBlockProgress }) => {
            let errors = [];
            if(address.lastName?.length > 10){
                errors.push({
                    message : '姓を10文字以内で入力してください',
                    target  : '$.cart.deliveryGroups[0].deliveryAddress.lastName'
                });
            }
            /* エラーがあればブロック */
            if(canBlockProgress && errors.length){
                return {
                    behavior : 'block',
                    reason   : 'Invalid length',
                    errors   : errors
                };
            }else{
                return {
                    behavior : 'allow'
                };
            }
        }
    );
}

[JavaScript]useAppMetafields

let j,productId;
const cartLines = useCartLines();
const appMetafields = useAppMetafields();
if(appMetafields.length){
  cartLines.forEach((cartLine,i)=>{
    productId = cartLine.merchandise.product.id.split("/").pop();
    for(j=0;j<appMetafields.length;j++){
      if(appMetafields[j].target.id === productId){
        if(appMetafields[j].metafield.key === "metafieldKey" && appMetafields[j].metafield.value === "metafieldValue"){
          //処理
        }
        break;
      }
    }
  });
}

[JavaScript]オプショナルチェーン備忘録

1

const tmp = {name:"abc"};
console.log(tmp.address?.length);
undefined

 
 

2

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length);
NaN

 
 

3

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length > 4);
false

 
 

4

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length == 0);
false

 
 

5

const tmp = {name:""};
console.log(tmp.name?.length+tmp.city?.length <= 1);
false

 
 

6

const tmp = {name:""};
console.log(tmp.name?.length <= 1);
true