Plugin Directory

Changeset 3345231


Ignore:
Timestamp:
08/15/2025 04:14:47 PM (8 months ago)
Author:
denishua
Message:

version 6.8.2

Location:
wpjam-basic/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • wpjam-basic/trunk/includes/class-wpjam-api.php

    r3344768 r3345231  
    705705    }
    706706
     707    public function get_schema(){
     708        return $this->get_arg('schema');
     709    }
     710
    707711    public function query_label($id, $field=null){
    708712        if($this->query_label){
     
    723727    public static function get_defaults(){
    724728        return [
    725             'post_type' => ['model'=>'WPJAM_Post',  'meta_type'=>'post',    'label_field'=>'post_title',    'id_field'=>'ID',   'parse_value'=>'wpjam_get_post'],
    726             'taxonomy'  => ['model'=>'WPJAM_Term',  'meta_type'=>'term',    'label_field'=>'name',  'id_field'=>'term_id',  'parse_value'=>'wpjam_get_term'],
    727             'author'    => ['model'=>'WPJAM_User',  'meta_type'=>'user',    'label_field'=>'display_name',  'id_field'=>'ID'],
     729            'post_type' => [
     730                'model'         => 'WPJAM_Post',
     731                'meta_type'     => 'post',
     732                'schema'        => ['type'=>'integer'],
     733                'label_field'   => 'post_title',
     734                'id_field'      => 'ID',
     735                'parse_value'   => 'wpjam_get_post',
     736            ],
     737            'taxonomy'  => [
     738                'model'         => 'WPJAM_Term',
     739                'meta_type'     => 'term',
     740                'schema'        => ['type'=>'integer'],
     741                'label_field'   => 'name',
     742                'id_field'      => 'term_id',
     743                'parse_value'   => 'wpjam_get_term'
     744            ],
     745            'author'    => [
     746                'model'         => 'WPJAM_User',
     747                'meta_type'     => 'user',
     748                'schema'        => ['type'=>'integer'],
     749                'label_field'   => 'display_name',
     750                'id_field'      => 'ID'
     751            ],
    728752            'model'     => [],
    729753            'video'     => ['parse_value'=>'wpjam_get_video_mp4'],
  • wpjam-basic/trunk/includes/class-wpjam-field.php

    r3344768 r3345231  
    289289            }
    290290
    291             $args   = wpjam_except($this->get_args(), ['required', 'show_in_rest']);
     291            $args   = wpjam_except($this->get_args(), 'required');
    292292            $type   = $this->is('mu-text') ? $this->item_type : substr($this->type, 3);
    293293
     
    317317            }, $value, 'options');
    318318        }elseif($key == '_schema'){
    319             $schema = ['type'=>'string'];
    320 
    321319            if($this->is('mu')){
    322320                $schema = $this->schema_by_item();
    323321                $schema = ['type'=>'array', 'items'=>($this->is('mu-fields') ? ['type'=>'object', 'properties'=>$schema] : $schema)];
    324             }elseif($this->is('email')){
    325                 $schema += ['format'=>'email'];
    326             }elseif($this->is('color')){
    327                 $schema += $this->data('alpha-enabled') ? [] : ['format'=>'hex-color'];
    328             }elseif($this->is('url, image, file, img')){
    329                 $schema = (($this->is('img') && $this->item_type != 'url') ? ['type'=>'integer'] : ['format'=>'uri'])+$schema;
    330             }elseif($this->is('number, range')){
    331                 $step   = $this->step ?: '';
    332                 $type   = ($step == 'any' || strpos($step, '.')) ? 'number' : 'integer';
    333                 $schema = ['type'=>$type]+(($type == 'integer' && $step > 1) ? ['multipleOf'=>$step] : [])+$schema;
    334             }elseif($this->is('radio, select, checkbox')){
    335                 if($this->is('checkbox') && !$this->options){
    336                     $schema['type'] = 'boolean';
    337                 }else{
    338                     $schema += $this->_custom ? [] : ['enum'=>array_keys($this->_options)];
    339                     $schema = $this->is('checkbox') ? ['type'=>'array', 'items'=>$schema] : $schema;
    340                 }
     322            }else{
     323                $schema = wpjam_pick((array)$this->show_in_rest, ['type'])+($this->get_schema_by_data_type() ?: []);
     324
     325                if($this->is('email')){
     326                    $schema += ['format'=>'email'];
     327                }elseif($this->is('color')){
     328                    $schema += $this->data('alpha-enabled') ? [] : ['format'=>'hex-color'];
     329                }elseif($this->is('url, image, file, img')){
     330                    $schema += (($this->is('img') && $this->item_type != 'url') ? ['type'=>'integer'] : ['format'=>'uri']);
     331                }elseif($this->is('number, range')){
     332                    $step   = $this->step ?: '';
     333                    $type   = ($step == 'any' || strpos($step, '.')) ? 'number' : 'integer';
     334                    $schema += ['type'=>$type]+(($type == 'integer' && $step > 1) ? ['multipleOf'=>$step] : []);
     335                }elseif($this->is('radio, select, checkbox')){
     336                    if($this->is('checkbox') && !$this->options){
     337                        $schema += ['type'=>'boolean'];
     338                    }else{
     339                        $schema += ['type'=>'string']+($this->_custom ? [] : ['enum'=>array_keys($this->_options)]);
     340                        $schema = $this->is('checkbox') ? ['type'=>'array', 'items'=>$schema] : $schema;
     341                    }
     342                }
     343
     344                $schema += ['type'=>'string'];
     345            }
     346
     347            if($this->required && !$this->show_if){ // todo 以后可能要改成 callback
     348                $schema['required'] = true;
    341349            }
    342350
     
    363371            $schema += wpjam_array($map ?? [], fn($k, $v)=> [$k, $this->$v], true);
    364372
    365             if(is_array($rest = $this->show_in_rest)){
    366                 $schema = (isset($rest['schema']) && is_array($rest['schema'])) ? wpjam_merge($schema, $rest['schema']) : $schema;
    367                 $schema = !empty($rest['type']) ? wpjam_set($schema, ($schema['type'] == 'array' && $rest['type'] != 'array' ? 'items.type' : 'type'), $rest['type']) : $schema;
    368             }
    369 
    370             if($this->required && !$this->show_if){ // todo 以后可能要改成 callback
    371                 $schema['required'] = true;
    372             }
    373 
    374373            return $this->$key = $this->schema('parse', $schema);
    375374        }elseif($key == '_custom'){
     
    388387
    389388    public function __call($method, $args){
    390         if(str_contains($method, '_by_')){
    391             [$method, $type]    = explode('_by', $method);
    392 
    393             $by     = $this->$type;
    394             $value  = $args ? $args[0] : null;
    395 
    396             if($type == '_schema'){
    397                 if(!$by){
    398                     return $method == 'validate' ? true : $value;
    399                 }
    400 
    401                 if($method == 'prepare'){
    402                     return $this->schema($method, $by, $value);
    403                 }
    404 
    405                 $value  = $method == 'sanitize' && $by['type'] == 'string' ? (string)$value : $value;
    406 
    407                 return wpjam_try('rest_'.$method.'_value_from_schema', $value, $by, $this->_title);
    408             }elseif($type == '_custom'){
    409                 if(!$by){
    410                     return $method == 'render' ? '' : $value;
    411                 }
    412 
    413                 $options    = array_map('strval', array_keys($this->_options));
    414 
    415                 if($this->is('checkbox')){
    416                     $value  = array_diff($value ?: [], ['__custom']);
    417                     $diff   = array_diff($value, $options);
    418                     $custom = $diff ? reset($diff) : null;
    419                 }else{
    420                     $custom = isset($value) && !in_array($value, $options) ? $value : null;
    421                 }
    422 
    423                 $is = isset($custom);
    424                 $is && $by->val($custom);
    425 
    426                 if($method == 'render'){
    427                     $this->value    = $is ? ($this->is('checkbox') ? [...$value, '__custom'] : '__custom') : $value;
    428                     $this->options  += ['__custom'=>$by->pull('title')];
    429 
    430                     return $by->attr('name', $this->name)->wrap();
    431                 }elseif($method == 'validate'){
    432                     isset($diff) && count($diff) > 1 && wpjam_throw('too_many_custom_value', $by->_title.'只能传递一个其他选项值');
    433 
    434                     $is && $by->schema(wpjam_get($this->schema(), isset($diff) ? 'items' : null))->validate($custom);
    435                 }
    436 
    437                 return $value;
    438             }
    439 
    440             return $by ? wpjam_try([$by, $method], ...$args) : $value;
    441         }
    442 
    443         trigger_error($method);
     389        [$method, $type]    = explode('_by', $method)+['', ''];
     390
     391        if(!$type){
     392            return;
     393        }
     394
     395        $by     = $this->$type;
     396        $value  = $args ? $args[0] : null;
     397
     398        if($type == '_schema'){
     399            if(!$by){
     400                return $method == 'validate' ? true : $value;
     401            }
     402
     403            if($method == 'prepare'){
     404                return $this->schema($method, $by, $value);
     405            }
     406
     407            $value  = $method == 'sanitize' && $by['type'] == 'string' ? (string)$value : $value;
     408
     409            return wpjam_try('rest_'.$method.'_value_from_schema', $value, $by, $this->_title);
     410        }elseif($type == '_custom'){
     411            if(!$by){
     412                return $method == 'render' ? '' : $value;
     413            }
     414
     415            $options    = array_map('strval', array_keys($this->_options));
     416
     417            if($this->is('checkbox')){
     418                $value  = array_diff($value ?: [], ['__custom']);
     419                $diff   = array_diff($value, $options);
     420                $custom = $diff ? reset($diff) : null;
     421            }else{
     422                $custom = isset($value) && !in_array($value, $options) ? $value : null;
     423            }
     424
     425            $is = isset($custom);
     426            $is && $by->val($custom);
     427
     428            if($method == 'render'){
     429                $this->value    = $is ? ($this->is('checkbox') ? [...$value, '__custom'] : '__custom') : $value;
     430                $this->options  += ['__custom'=>$by->pull('title')];
     431
     432                return $by->attr('name', $this->name)->wrap();
     433            }elseif($method == 'validate'){
     434                isset($diff) && count($diff) > 1 && wpjam_throw('too_many_custom_value', $by->_title.'只能传递一个其他选项值');
     435
     436                $is && $by->schema(wpjam_get($this->schema(), isset($diff) ? 'items' : null))->validate($custom);
     437            }
     438
     439            return $value;
     440        }elseif($type == '_data_type'){
     441            if(!$by){
     442                return $method == 'query_label' ? null : $value;
     443            }
     444
     445            if(is_array($value) && $this->multiple && str_ends_with($method, '_value')){
     446                return array_map(fn($v)=> wpjam_try([$by, $method], $v, $this), $value);
     447            }
     448
     449            array_push($args, $this);
     450        }
     451
     452        return $by ? wpjam_try([$by, $method], ...$args) : $value;
    444453    }
    445454
     
    524533            $value  = $this->is('checkbox') && $this->options ? ($value ?: []) : $value;
    525534
    526             return $this->try('validate_value_by_data_type', $value, $this);
     535            return $this->try('validate_value_by_data_type', $value);
    527536        }
    528537
     
    585594            }
    586595
    587             return $this->prepare_value_by_data_type($args, $this);
     596            return $this->prepare_value_by_data_type($args);
    588597        }
    589598
     
    713722                        $this->direction == 'row' && ($this->class  ??= 'medium-text');
    714723
    715                         $this->_data_type && array_walk($value, fn(&$v)=> ($l = $this->query_label_by_data_type($v, $this)) && ($v = ['value'=>$v, 'label'=>$l]));
     724                        array_walk($value, fn(&$v)=> ($l = $this->query_label_by_data_type($v)) && ($v = ['value'=>$v, 'label'=>$l]));
    716725                    }
    717726
     
    797806            return $this->tag('textarea')->append(esc_textarea($this->value ?: ''));
    798807        }else{
    799             return $this->input()->data(['class'=>$this->class]+($this->_data_type ? ['label'=>$this->query_label_by_data_type($this->value, $this)] : []));
     808            return $this->input()->data(['class'=>$this->class]+array_filter(['label'=>$this->query_label_by_data_type($this->value)]));
    800809        }
    801810    }
     
    854863            $field['propertied']    ??= !empty($field['data_type']) ? true : wpjam_pull($field, 'fieldset_type') == 'array';
    855864        }elseif($type == 'mu-select' || ($type == 'select' && !empty($field['multiple']))){
    856             unset($field['multiple'], $field['data_type']);
    857 
     865            $field['multiple']  = true;
    858866            $field['type']      = 'checkbox';
    859867            $field['_type']     = 'mu-select';
     
    10241032
    10251033            if(!$field->is('fieldset') || ($show && $field->propertied)){
    1026                 if($show){
    1027                     $value  = $field->pack($field->validate($field->unpack($value), $for));
    1028                 }else{
    1029                     if($for == 'value'){
    1030                         continue;
    1031                     }
    1032 
    1033                     $value  = $field->pack(null);
    1034 
    1035                     $if_values[$field->key] = null; // 第一次获取的值都是经过 json schema validate 的,可能存在 show_if 的字段在后面
    1036                 }
     1034                $value  = $show ? $field->unpack($value) : null;
     1035                $value  = is_null($value) && $for == 'value' ? [] : $field->pack($show ? $field->validate($value, $for) : null);
     1036
     1037                $show || $for == 'value' || ($if_values[$field->key] = null); // 第一次获取的值都是经过 json schema validate 的,可能存在 show_if 的字段在后面
    10371038            }
    10381039
     
    10611062
    10621063        foreach($this->fields as $field){
    1063             ($creator && $field->group != $group) && ([$groups[], $fields, $group]  = [[$group, $fields], [], $field->group]);
     1064            $creator && $field->group != $group && ([$groups[], $fields, $group]    = [[$group, $fields], [], $field->group]);
    10641065
    10651066            $fields[]   = $field->sandbox(fn()=> $this->wrap($tag, $args));
     
    10851086
    10861087    public function get_parameter($method='POST', $merge=true){
    1087         $data       = wpjam_get_parameter('', [], $method);
    1088         $validated  = $this->validate($data, 'parameter');
    1089 
    1090         return array_merge($merge ? $data : [], $validated);
     1088        $data   = wpjam_get_parameter('', [], $method);
     1089
     1090        return array_merge($merge ? $data : [], $this->validate($data, 'parameter'));
    10911091    }
    10921092
  • wpjam-basic/trunk/includes/class-wpjam-post.php

    r3344768 r3345231  
    466466            'data_type'     => 'post_type',
    467467            'post_type'     => $post_type,
    468             'placeholder'   => '请输入'.$title.'ID或者输入关键字筛选',
    469             'show_in_rest'  => ['type'=>'integer']
     468            'placeholder'   => '请输入'.$title.'ID或者输入关键字筛选'
    470469        ];
    471470    }
  • wpjam-basic/trunk/includes/class-wpjam-setting.php

    r3344768 r3345231  
    146146
    147147    protected function parse_section($section, $id){
    148         return array_merge($section, ['fields'=> maybe_callback($section['fields'] ?? [], $id, $this->name)]);
     148        return wpjam_set($section, 'fields', maybe_callback($section['fields'] ?? [], $id, $this->name));
    149149    }
    150150
    151151    protected function get_sections($all=false, $filter=true){
    152152        $sections   = $this->get_arg('sections');
    153 
    154         if($sections && is_array($sections)){
    155             $sections   = array_filter($sections, 'is_array');
    156         }else{
    157             $fields     = $this->get_arg('fields', null, false);
    158             $sections   = is_null($fields) ? [] : [($this->sub_name ?: $this->name)=> ['title'=>$this->title ?: '', 'fields'=>$fields]];
    159         }
    160 
    161         $sections   = wpjam_map($sections, fn($v, $k)=> $this->parse_section($v, $k));
     153        $sections   = $sections && is_array($sections) ? $sections : [($this->sub_name ?: $this->name)=> ['fields'=>$this->get_arg('fields', null, false)]];
     154        $sections   = wpjam_array($sections, fn($k, $v)=> is_array($v) && isset($v['fields']) ? [$k, $this->parse_section($v, $k)] : null);
     155        $sections   = count($sections) == 1 ? array_map(fn($s)=> $s+['title'=>$this->title ?: ''], $sections) : $sections;
    162156        $sections   = array_reduce($all ? $this->get_subs() : [], fn($carry, $v)=> array_merge($carry, $v->get_sections(false, false)), $sections);
    163157
  • wpjam-basic/trunk/includes/class-wpjam-term.php

    r3344768 r3345231  
    156156            $validate && wpjam_try([static::class, 'validate'], $term_id);
    157157
    158             $data   = static::prepare_data($data, $post_id);
     158            $data   = static::prepare_data($data, $term_id);
    159159            $tax    = $data['taxonomy'] ?? get_term_field('taxonomy', $term_id);
    160160            $meta   = wpjam_pull($data, 'meta_input');
     
    263263        $type   = $args['type'] ?? '';
    264264        $title  = $args['title'] ??= $object ? $object->title : null;
    265         $args   = array_merge($args, ['data_type'=>'taxonomy', 'show_in_rest'=>['type'=>'integer']]);
     265        $args   += ['data_type'=>'taxonomy'];
    266266
    267267        if($object && ($object->hierarchical || ($type == 'select' || $type == 'mu-select'))){
  • wpjam-basic/trunk/wpjam-basic.php

    r3344768 r3345231  
    44Plugin URI: https://blog.wpjam.com/project/wpjam-basic/
    55Description: WPJAM 常用的函数和接口,屏蔽所有 WordPress 不常用的功能。
    6 Version: 6.8.2
     6Version: 6.8.2.1
    77Requires at least: 6.6
    88Tested up to: 6.8
Note: See TracChangeset for help on using the changeset viewer.