screen = $screen = get_current_screen(); $this->_args = $args+['screen'=>$screen, 'shortcodes'=>['filter', 'row_action']]; array_map([$this, 'component'], ['action', 'view', 'column']); array_map(fn($k)=> add_filter('manage_'.$screen->id.'_'.$k, [$this, 'filter_'.$k]), ['columns', 'sortable_columns']); array_map(fn($k, $s)=> add_filter($k.$s.$screen->id, [$this, 'filter_'.$k]), ['views', 'bulk_actions'], ['_', '-']); array_map(fn($v)=> add_shortcode($v, [$this, $v.'_shortcode']), ['filter', 'row_action']); wpjam_admin([ 'style' => $this->style, 'vars[list_table]' => fn()=> $this->get_setting(), 'vars[page_title_action]' => fn()=> $this->get_action('add', ['class'=>'page-title-action']) ?: '' ]); if($this->builtin){ $this->page_load(); }else{ add_filter('list_table_primary_column', fn()=> $this->primary_column ??= array_key_first(wpjam_except($this->columns, ['no', 'cb']))); parent::__construct($this->_args); } } public function __get($name){ if(in_array($name, $this->compat_fields, true)){ return $this->$name; } if(isset($this->_args[$name])){ return $this->_args[$name]; } if(in_array($name, ['actions', 'views', 'fields'])){ $value = [$this, 'get_'.$name.'_by_model'](); if($name == 'fields'){ return $this->$name = wpjam_fields($value ?: [], true); } return $value ?? ($name == 'actions' ? ($this->builtin ? [] : WPJAM_Model::get_actions()) : ($this->views_by_model() ?: [])); }elseif(in_array($name, ['primary_key', 'filterable_fields', 'searchable_fields'])){ $value = wpjam_trap($this->model.'::get_'.$name, []) ?: ($name == 'primary_key' ? 'id' : []); if($name == 'filterable_fields'){ $fields = wpjam_filter($this->fields, ['filterable'=>true]); $views = array_keys(wpjam_filter($fields, ['type'=>'view'])); $fields = array_map(fn($v)=> wpjam_except($v, ['title', 'before', 'after', 'required', 'show_admin_column'])+[($v['type'] === 'select' ? 'show_option_all' : 'placeholder') => $v['title'] ?? ''], wpjam_except($fields, $views)); $value = $fields+($fields && !$this->builtin && $this->sortable_columns ? [ 'orderby' => ['options'=>[''=>'排序']+array_map('wp_strip_all_tags', array_intersect_key($this->columns, $this->sortable_columns))], 'order' => ['options'=>['desc'=>'降序','asc'=>'升序']] ] : [])+array_fill_keys(array_merge($value, $views), []); } return $this->$name = $value; }elseif(in_array($name, ['form_data', 'params'])){ return $this->validate_by_fields($name); }elseif($name == 'offset'){ return ((int)$this->per_page ?: 50)*($this->get_pagenum()-1); } } public function __set($name, $value){ return in_array($name, $this->compat_fields, true) ? ($this->$name = $value) : ($this->_args[$name] = $value); } public function __isset($name){ return $this->$name !== null; } public function __call($method, $args){ if($method == 'get_arg'){ return wpjam_get($this->_args, ...$args); }elseif($method == 'get_date'){ $type = $args[0] ?? ''; $offset = $type == 'prev' ? -1 : (int)($type == 'next'); [$year, $month] = array_map(fn($k, $f, $r)=> clamp(($type == 'current' ? 0 : (int)$this->get_param($k)) ?: wpjam_date($f), ...$r), ['year', 'month'], ['Y', 'm'], [[1970, 2200], [1, 12]]); $month += $offset; if(in_array($month, [0, 13])){ $year += $offset; $month = abs($month-12); } return in_array('locale', $args) ? sprintf(__('%1$s %2$d', 'wpjam'), $GLOBALS['wp_locale']->get_month($month), $year) : compact('year', 'month'); }elseif($method == 'add'){ $this->_args = wpjam_set($this->_args, array_shift($args).'['.(count($args) >= 2 ? array_shift($args) : '').']', $args[0]); return $args[0]; }elseif($this->mod($method, '_by_fields')){ $name = $args[0] ?? ''; $fields = array_filter($this->{($name === 'left' ? 'left' : 'filterable').'_fields'}); $parts = array_filter([$name === 'left' ? '' : wpjam_chart(), $fields ? wpjam_fields($fields) : '']); $name = ($name === 'left' || !$name) ? 'params' : $name; $data = wp_doing_ajax() ? wp_parse_args($_POST[$name] ?? []) : $_GET; $data = array_filter(array_merge(...array_map(fn($part)=> $method == 'validate' && wp_doing_ajax() ? $part->validate($data) : wpjam_trap([$part, 'validate'], $data, []), $parts)), fn($v)=> isset($v) && $v !== []); return $parts && $method == 'render' ? wpjam_tag('div', ['actions'])->append(array_map(fn($part)=> $part->render(['fields_type'=>'', 'data'=>$data]), $parts)) : $data; }elseif($this->mod($method, '_by_model')){ return ($cb = wpjam_callback([$this->model, $method])) ? wpjam_catch($cb, ...$args) : null; }elseif($this->mod($method, '_shortcode')){ return $method === 'filter' ? $this->get_filter_link($args[0], $args[1], wpjam_pull($args[0], 'class')) : $this->get_row_action($this->row_id, (is_blank($args[1]) ? [] : ['title'=>$args[1]])+$args[0])."\n"; }elseif($this->mod($method, '_row_actions')){ [$value, $id] = $method == 'get' ? [[], $args[0]] : [$args[0], $this->parse_id($args[1])]; $names = array_diff($this->row_actions ?: [], $this->next_actions ?: []); $value += $this->get_action($names, ['id'=>$id]+($this->layout == 'calendar' ? ['wrap'=>''] : [])); $value += $this->builtin ? wpjam_pull($value, ['view', 'delete', 'trash', 'spam', 'remove']) : []; return wpjam_except($value+($this->builtin || $this->primary_key == 'id' ? ['id'=>'ID: '.$id] : []), wpjam_admin('removed_actions[]')); }elseif($this->mod($method, 'ob_get_')){ $result = wpjam_ob([$this, ($this->builtin && $method != 'single_row' ? 'builtin_' : '').$method], ...$args); return $this->builtin && in_array($method, ['single_row', 'display']) ? $this->filter_table($result) : $result; }elseif($this->mod($method, 'builtin_')){ return [$GLOBALS['wp_list_table'] ??= _get_list_table($this->builtin, ['screen'=>$this->screen]), $method](...$args); }elseif($this->mod($method, 'filter_')){ if($method == 'table'){ return wpjam_preg_replace('#]*>.+?#is', fn($m)=> $this->filter_single_row(...$m), $args[0]); }elseif($method == 'single_row'){ $this->row_id = $args[1]; return wpjam_do_shortcode(apply_filters('wpjam_single_row', ...$args), $this->shortcodes); }elseif($method == 'columns'){ return $this->columns = wpjam_except(wpjam_add_at($args[0], $args[0] ? -1 : 0, $this->columns ?: []), wpjam_admin('removed_columns[]')); } return array_merge($args[0], $this->$method ?: []); } return parent::__call($method, $args); } public function __invoke($data){ $type = $data['action_type']; $action = $data['list_action'] ?? ''; $parts = parse_url((wp_get_original_referer() ?: wp_get_referer()) ?: wp_die('非法请求')); if($parts['host'] == $_SERVER['HTTP_HOST']){ $_SERVER['REQUEST_URI'] = $parts['path']; } if($type == 'query_items'){ $data = wpjam_params('data'); if(count($data) == 1 && isset($data['id'])){ return $this->response(['type'=>'add']+$data); } $_REQUEST = $data+$_REQUEST; $result = ['type'=>'list']; }else{ $result = ($this->get_action($action) ?: wp_die('无效的操作'))($type); } if(!in_array($result['type'], ['form', 'append', 'redirect', 'move', 'up', 'down', ''])){ $this->prepare_items(); $result = $this->response($result)+['params'=>$this->params, 'setting'=>$this->get_setting(), 'views'=>$this->ob_get_views()]; $result += $result['type'] == 'list' ? ['table'=>$this->ob_get_display()] : ['tablenav'=>wpjam_fill(['top', 'bottom'], [$this, 'ob_get_display_tablenav'])]; } return $result; } protected function response($data){ if($data['type'] == 'list'){ if($this->layout == 'left' && !isset(wpjam_params('data')[$this->left_key])){ $data['left'] = $this->ob_get_col_left(); } }elseif($data['type'] == 'items'){ if(isset($data['items'])){ $data['items'] = wpjam_map($data['items'], fn($item, $id)=> $this->response(['id'=>$id]+$item)); } }elseif($data['type'] != 'delete'){ if($this->layout == 'calendar'){ if(!empty($data['data'])){ $data['data'] = wpjam_map($data['data'], [$this, 'ob_get_single_date']); } }elseif(!empty($data['bulk'])){ $this->get_by_ids_by_model($ids = array_filter($data['ids'])); $data['data'] = array_map(fn($id)=> ['id'=>$id, 'data'=>$this->ob_get_single_row($id)], $ids); }elseif(!empty($data['id'])){ $data['data'] = $this->ob_get_single_row($data['id']); } } return $data; } protected function component($type, ...$args){ if($args){ return $this->objects[$type][$args[0]] ?? array_find($this->objects[$type], fn($v)=> $v->name == $args[0]); } $args = wpjam_admin('data_type', $this); if($type == 'action'){ if($this->sortable){ $sortable = is_array($this->sortable) ? $this->sortable : []; $action = (wpjam_pull($sortable, 'action') ?: [])+wpjam_except($sortable, ['items']); $this->sortable = ['items'=> $sortable['items'] ?? ' >tr']; $this->actions += wpjam_map([ 'move' => ['page_title'=>'拖动', 'dashicon'=>'move'], 'up' => ['page_title'=>'向上移动', 'dashicon'=>'arrow-up-alt'], 'down' => ['page_title'=>'向下移动', 'dashicon'=>'arrow-down-alt'], ], fn($v)=> $action+$v+['direct'=>true]); } $name = 'update_setting'; $this->actions += $this->$name ? [$name => (is_array($this->$name) ? $this->$name : [])+['page_title'=>'全局设置', 'title'=>'设置', 'class'=>'button-primary', $name=>true]] : []; foreach(wpjam_get_meta_options(wpjam_admin('meta_type'), ['list_table'=>true]+wpjam_except($args, 'data_type')) as $v){ wpjam_register_list_table_action(($v->action_name ?: 'set_'.$v->name), $v->get_args()+[ 'meta_type' => $v->name, 'page_title' => '设置'.$v->title, 'submit_text' => '设置' ]); } }elseif($type == 'column'){ if($this->layout == 'calendar'){ array_map(fn($i)=> $this->add('columns', 'day'.($i%7), $GLOBALS['wp_locale']->get_weekday_abbrev($GLOBALS['wp_locale']->get_weekday($i%7))), array_slice(range(0, get_option('start_of_week')+6), -7)); return array_map(fn($v)=> $this->add('query_args', $v), ['year', 'month']); } $this->bulk_actions && !$this->builtin && $this->add('columns', 'cb', true); ($no = $this->numberable) && $this->add('columns', 'no', $no === true ? 'No.' : $no); } $class = 'WPJAM_List_Table_'.$type; [$class, 'registers']($type == 'column' ? $this->fields : $this->{$type.'s'}); $args = array_map(fn($v)=> ['value'=>$v, 'if_null'=>true, 'callable'=>true], $args); $args += $type == 'action' ? ['calendar'=> $this->layout == 'calendar' ? true : ['compare'=>'!==', 'value'=>'only']] : []; foreach($this->add('objects', $type, [$class, 'get_registereds']($args)) as $object){ $key = $object->name; if($type == 'action'){ if($object->overall){ $this->add('overall_actions', $key); }else{ $object->bulk && $object->is_allowed() && $this->add('bulk_actions', $key, $object); $object->row_action && $this->add('row_actions', $key); } $object->next && $this->add('next_actions', $key, $object->next); }elseif($type == 'view'){ $view = $object(); $view && $this->add('views', $key, is_array($view) ? $this->get_filter_link(...$view) : $view); }else{ $data = array_filter($object->pick(['description', 'sticky', 'format', 'precision', 'conditional_styles', ...($this->nowrap ? [] : ['nowrap'])])); $this->add('columns', $key, $object->title.($data ? wpjam_tag('i', ['data'=>$data]) : '')); $object->sortable && $this->add('sortable_columns', $key, [$key, true]); wpjam_style($object->style); } } } protected function get_setting(){ $fields = $this->searchable_fields; $s = $this->get_param('s'); return wpjam_pick($this, ['sortable', 'layout', 'left_key'])+[ 'search' => $this->builtin || ($this->search ?? $fields) ? [ 'columns' => wpjam_is_assoc_array($fields) ? wpjam_field(['key'=>'search_columns', 'show_option_all'=>'默认', 'value'=>$this->get_param('search_columns'), 'options'=>$fields]) : '' ]+($this->builtin ? ['term'=>$s] : ['box'=>$this->ob_get_search_box('搜索', 'wpjam')]) : false, 'subtitle' => $this->get_subtitle_by_model().($s ? sprintf(__('Search results for: %s', 'wpjam'), ''.esc_html($s).'') : ''), 'summary' => $this->get_summary_by_model(), 'bulk_actions' => array_map(fn($v)=> array_filter($v->get_attr(['bulk'=>true])), $this->bulk_actions ?: []), 'overall_actions' => array_values($this->get_action(array_diff($this->overall_actions ?: [], $this->next_actions ?: []), ['class'=>'button overall-action'])) ]; } public function get_action($name, ...$args){ if(is_array($name)){ return wpjam_fill($name ?: [], fn($n)=> $this->get_action($n, ...$args)); } return ($object = $this->component('action', $name)) && $args ? $object->render($args[0]) : $object; } public function get_row_action($id, $args){ return $this->get_action(wpjam_pull($args, 'name'), $args+['id'=>$id]); } public function get_filter_link($filter, $label, $attr=[]){ return wpjam_tag('a', $attr, $label)->add_class('list-table-filter')->data('filter', $filter+$this->get_param($this->query_args ?: []) ?: new stdClass()); } public function single_row($item){ if($this->layout == 'calendar'){ return wpjam_echo(wpjam_tag('tr')->append(wpjam_map($item, fn($date, $day)=> ['td', ['id'=>'date_'.$date, 'class'=>'column-day'.$day], $this->ob_get_single_date($this->calendar[$date] ?? [], $date)]))); } $item = ($item instanceof WPJAM_Register) ? $item->to_array() : (is_array($item) ? $item : wpjam_trap($this->model.'::get', $item, wp_doing_ajax() ? 'throw' : [])); if(!$item){ return; } $raw = (array)$item; $id = $this->parse_id($item); $attr = $id ? ['id'=>$this->singular.'-'.str_replace('.', '-', $id), 'data'=>['id'=>$id]] : []; $item['row_actions'] = $id ? $this->get_row_actions($id) : ($this->row_actions ? ['error'=>'Primary Key「'.$this->primary_key.'」不存在'] : []); $this->before_single_row_by_model($raw); $method = array_find(['render_row', 'render_item', 'item_callback'], fn($v)=> method_exists($this->model, $v)); $item = $method ? [$this->model, $method]($item, $attr) : $item; $this->numberable && ($this->no ??= $this->offset); echo $item ? $this->filter_single_row(wpjam_tag('tr', $attr, $this->ob_get_single_row_columns($item+($this->numberable ? ['no'=>($this->no += 1)] : [])))->add_class($this->multi_rows ? 'tr-'.$id : ''), $id)."\n" : ''; $this->after_single_row_by_model($item, $raw); } public function single_date($item, $date){ $parts = explode('-', $date); $append = ($item || $parts[1] == $this->get_date()['month']) ? wpjam_tag('div', ['row-actions', 'alignright'])->append($this->get_row_actions($date)) : ''; echo wpjam_tag('div', ['date-meta'])->append('span', ['day', $date == wpjam_date('Y-m-d') ? 'today' : ''], (int)$parts[2])->append($append)->after('div', ['date-content'], $this->render_date_by_model($item, $date) ?? (is_string($item) ? $item : '')); } protected function parse_id($item){ return wpjam_get($item, $this->primary_key); } protected function parse_cell($cell, $id){ $wrap = wpjam_pull($cell, 'wrap'); if(isset($cell['row_action'])){ $cell = $this->get_row_action($id, ['name'=>wpjam_pull($cell, 'row_action')]+$cell); }elseif(isset($cell['filter'])){ $cell = $this->get_filter_link(wpjam_pull($cell, 'filter'), wpjam_pull($cell, 'label'), $cell); }elseif(isset($cell['items'])){ $items = $cell['items']; $args = $cell['args'] ?? []; $type = $args['item_type'] ?? 'image'; $key = $args[$type.'_key'] ?? $type; $max = $args['max_items'] ?? null; $field = $args['field'] ?? ''; $cell = wpjam_tag('div', ['items', $type.'-list'])->data(['field'=>$field, 'max_items'=>$max])->style($args['style'] ?? ''); if($type == 'image'){ $size = wpjam_pick($args, ['width', 'height'])+['width'=>60, 'height'=>60]; $cell->style(wpjam_array($size, fn($k, $v)=> ['--'.$k, $v.'px'])+['--per-row'=>$arg['per_row'] ?? 4]); } $names = $args['actions'] ?? ['add_item', 'edit_item', 'del_item']; $names = !empty($args['sortable']) && $cell->add_class('sortable') ? ['move_item', ...$names] : $names; $args = ['id'=>$id,'data'=>['_field'=>$field]]; $add = in_array('add_item', $names) && (!$max || count($items) <= $max); foreach($items as $i => $item){ $v = $item[$key] ?: ''; $t = wpjam_tag('span', ['truncate-text']); $type == 'image' ? $t->text($item['title'] ?? '')->before('img', ['src'=>wpjam_get_thumbnail($v, array_map(fn($s)=> $s*2, $size))]+$size) : $t->text($v); $args['i'] = $args['data']['i'] = $i; $cell->append(wpjam_tag('div', ['id'=>'item_'.$i, 'data'=>['i'=>$i], 'class'=>'item'])->append([ $this->get_action('move_item', $args+['title'=>$t, 'fallback'=>true])->style(wpjam_pick($item, ['color'])), wpjam_tag('span', ['row-actions'])->append($this->get_action(array_diff($names, ['add_item']), $args+['wrap'=>'', 'item'=>$item])) ])); } unset($args['i'], $args['data']['i']); $add && $cell->append($this->get_action('add_item', $args+['class'=>'add-item item']+($type == 'image' ? ['dashicon'=>'plus-alt2'] : []))); }else{ $cell = $cell['text'] ?? ''; } return (string)wpjam_wrap($cell, $wrap); } public function column_default($item, $name, $id=null){ $id ??= $this->parse_id($item); $object = $this->component('column', $name); $args = $this->value_callback === false ? [] : array_filter(['meta_type'=>wpjam_admin('meta_type'), 'model'=>$this->model]); $value = $object && $id ? $object($args+['data'=>$item, 'id'=>$id]) : (is_array($item) ? ($item[$name] ?? null) : $item); return implode(',', array_map(fn($v)=> is_array($v) ? $this->parse_cell($v, $id) : $v, wp_is_numeric_array($value) ? $value : [$value])); } public function column_cb($item){ if(($id = $this->parse_id($item)) && wpjam_can($this->capability, $id)){ return wpjam_tag('input', ['type'=>'checkbox', 'name'=>'ids[]', 'value'=>$id, 'id'=>'cb-select-'.$id]); } } public function render(){ $form = wpjam_tag('form', ['id'=>'list_table_form'], $this->ob_get_display())->before($this->ob_get_views()); return $this->layout == 'left' ? wpjam_tag('div', ['id'=>'col-container', 'class'=>'wp-clearfix'])->append(array_map(fn($v, $k)=> $v->add_class('col-wrap')->wrap('div', ['id'=>'col-'.$k]), [wpjam_wrap($this->ob_get_col_left(), 'form'), $form], ['left', 'right'])) : $form; } public function col_left($action=''){ $paged = (int)$this->get_param('left_paged') ?: 1; if(($cb = $this->model.'::query_left') && wpjam_callback($cb)){ static $pages, $items; if($action == 'prepare'){ $number = $this->left_per_page ?: 10; $left = array_filter($this->get_param([$this->left_key])); $items = wpjam_try($cb, ['number'=>$number, 'offset'=>($paged-1)*$number]+$this->validate_by_fields('left')+$left); [$pages, $items] = $items ? [ceil($items['total']/$number), $items['items']] : [0, []]; return $items && !$left && wpjam_default($this->left_key, array_first($items)['id']); } $fields = $this->render_by_fields('left'); $head = [[$this->left_title, 'th'], 'tr']; $items = implode(wpjam_map($items ?: ['找不到'.$this->left_title], fn($item)=> wpjam_tag('td')->append(is_array($item) ? [ ['p', ['row-title'], $item['title']], ['span', ['time'], $item['time']], ...(isset($item['count']) ? array_map(fn($v)=> ['span', ['count', 'wp-ui-highlight'], $v], (array)$item['count']) : []) ] : $item)->wrap('tr', is_array($item) ? ['class'=>'left-item', 'id'=>$item['id'], 'data-id'=>$item['id']] : ['no-items']))); echo $fields ? $fields->wrap('div', ['class'=>'tablenav']) : ''; echo wpjam_tag('table', ['widefat striped'])->append([[$head, 'thead'], [$items, 'tbody'], [$head, 'tfoot']]); }else{ $result = $this->col_left_by_model(); $pages = $result && is_array($result) ? ceil($result['total_items']/$result['per_page']) : 0; } $pages > 1 && wpjam_echo(wpjam_tag('span', ['left-pagination-links'])->append([ wpjam_tag('a', ['prev-page'], '‹')->attr('title', __('Previous page', 'wpjam')), wpjam_tag('span', [], $paged.' / '.$pages), wpjam_tag('a', ['next-page'], '›')->attr('title', __('Next page', 'wpjam')), wpjam_tag('input', ['type'=>'number', 'name'=>'left_paged', 'value'=>$paged, 'min'=>1, 'max'=>$pages, 'class'=>'current-page']), wpjam_tag('input', ['type'=>'submit', 'class'=>'button', 'value'=>'➔']) ])->wrap('div', ['tablenav-pages'])->wrap('div', ['tablenav', 'bottom'])); } public function page_load(){ if(wp_doing_ajax()){ return wpjam_ajax('wpjam-list-table-action', [ 'admin' => true, 'callback' => $this, 'nonce_action' => fn($data)=> $data['action_type'] == 'form' ? '' : (($object = $this->get_action($data['list_action'] ?? '')) ? $object->parse_nonce_action($data) : '') ]); } if($action = $_GET['export_action'] ?? ''){ return ($object = $this->get_action($action)) ? wpjam_trap($object, 'export', 'die') : wp_die('无效的导出操作'); } $this->builtin || wpjam_trap([$this, 'prepare_items'], fn($result)=> wpjam_admin('error', $result)); } public function prepare_items(){ $args = array_filter($this->get_param(['orderby', 'order', 's', 'search_columns']), fn($v)=> isset($v)); $_GET = array_merge($_GET, $args); $args += $this->params+wpjam_array($this->filterable_fields, fn($k, $v)=> [$k, $v ? null : $this->get_param($k)], true); if($this->layout == 'calendar'){ $date = $this->get_date(); $start = (int)get_option('start_of_week'); $ts = mktime(0, 0, 0, $date['month'], 1, $date['year']); $pad = calendar_week_mod(date('w', $ts)-$start); $days = date('t', $ts); $days = $days+6-($days%7 ?: 7); $items = wpjam_map(array_chunk(range(0, $days), 7), fn($item)=> wpjam_array($item, fn($k, $v)=> [($v+$start)%7, date('Y-m-d', $ts+($v-$pad)*DAY_IN_SECONDS)])); $this->calendar = wpjam_try($this->model.'::query_calendar', $args+$date); }else{ $this->layout == 'left' && $this->col_left('prepare'); $number = (int)$this->per_page ?: 50; $offset = $this->offset; $cb = $this->model.'::query_items'; $params = wpjam_get_reflection($cb, 'Parameters') ?: []; $items = wpjam_try($cb, ...(count($params) > 1 && $params[0]->name != 'args' ? [$number, $offset] : [compact('number', 'offset')+$args])); if(wpjam_is_assoc_array($items) && isset($items['items'])){ $total = $items['total'] ?? null; $items = $items['items']; } $this->set_pagination_args(['total_items'=>$total ?? ($number = count($items)), 'per_page'=>$number]); } $this->items = $items; } protected function get_table_classes(){ return array_merge(array_diff(parent::get_table_classes(), ($this->fixed ? [] : ['fixed'])), $this->nowrap ? ['nowrap'] : []); } protected function handle_row_actions($item, $column, $primary){ return ($primary === $column && !empty($item['row_actions'])) ? $this->row_actions($item['row_actions']) : ''; } public function get_columns(){ return []; } public function extra_tablenav($which='top'){ if($this->layout == 'calendar'){ echo wpjam_tag('h2', [], $this->get_date('locale')); echo wpjam_tag('span', ['pagination-links'])->append(array_map(fn($v, $k)=> "\n".$this->get_filter_link($this->get_date($k), $v, ['class'=>$k.'-month button', 'title'=>$this->get_date($k, 'locale')]), ['‹', '今日', '›'], ['prev', 'current', 'next']))->wrap('div', ['tablenav-pages']); } if($which == 'top' && ($fields = $this->render_by_fields())){ echo $fields->append(get_submit_button('筛选', '', 'filter_action', false)); } if(!$this->builtin){ $this->extra_tablenav_by_model($which); do_action(wpjam_get_filter_name($this->plural, 'extra_tablenav'), $which); } } public function current_action(){ return $_REQUEST['list_action'] ?? parent::current_action(); } } class WPJAM_Builtin_List_Table extends WPJAM_List_Table{ public function __construct($args){ $data_type = wpjam_admin(wpjam_pick($args, ['data_type', 'meta_type']))['data_type']; if($data_type == 'post_type'){ $echo = true; $builtin = $args['post_type'] == 'attachment' ? 'Media' : 'Posts'; $callback = 'get_post'; $parts = $builtin == 'Media' ? ['media', 'media'] : (($args['hierarchical'] ?? '') ? ['pages', 'page', 'posts'] : ['posts', 'post', 'posts']); }elseif($data_type == 'taxonomy'){ $builtin = 'Terms'; $callback = ['get_term', 'get_term_level']; $parts = [$args['taxonomy'], $args['taxonomy']]; }elseif($data_type == 'user'){ $builtin = 'Users'; $callback = 'get_userdata'; $parts = ['users', 'user', 'users']; }elseif($data_type == 'comment'){ $echo = true; $builtin = 'Comments'; $callback = 'get_comment'; $parts = ['comments', 'comment']; } wpjam_hooks([ ['manage_'.$parts[0].'_custom_column', fn(...$args)=> wpjam_echo($this->column_default(...array_pad($args, -3, [])), $echo ?? false), 10, 3], [$parts[1].'_row_actions', [$this, 'filter_row_actions'], 1, 2], isset($parts[2]) ? ['manage_'.$parts[2].'_extra_tablenav', [$this, 'extra_tablenav']] : [], wp_is_json_request() ? '' : ['wpjam_html', [$this, 'filter_table']] ]); in_array($data_type, ['post_type', 'taxonomy']) && wpjam_once('maybe', 'parse_term_query', function($query){ if(!array_any(debug_backtrace(), fn($v)=> ($v['class'] ?? '') == $this->builtin)){ return false; } $vars = &$query->query_vars; $by = $vars['orderby'] ?? ''; $object = $by && is_string($by) ? $this->component('column', $by) : null; $type = $object ? ($object->sortable === true ? 'meta_value' : $object->sortable) : ''; $vars = array_merge($vars, ['list_table_query'=>true], in_array($type, ['meta_value_num', 'meta_value']) ? ['orderby'=>$type, 'meta_key'=>$by] : []); }, 0); parent::__construct($args+['builtin'=>'WP_'.$builtin.'_List_Table', 'item_callback'=>$callback]); } public function prepare_items(){ if($this->screen->base == 'edit'){ $_GET['post_type'] = $this->post_type; } $_GET = array_merge($_GET, wpjam_params('data')); $_POST = array_merge($_POST, wpjam_params('data')); $this->builtin_prepare_items(); } public function single_row($item){ $cb = (array)$this->item_callback; if($item = is_numeric($item) ? (array_shift($cb))($item) : $item){ if($this->data_type == 'post_type' && $item->post_type == 'attachment'){ $GLOBALS['authordata'] = get_userdata($item->post_author); echo wpjam_tag('tr', ['id'=>'post-'.$item->ID], $this->ob_get_single_row_columns($item))->add_class(['author-'.(get_current_user_id() == $item->post_author ? 'self' : 'other'), 'status-'.$item->post_status]); }else{ $this->builtin_single_row($item, ...array_map(fn($v)=> $v($item), $cb)); } } } public static function load($args){ return new static($args); } } class WPJAM_List_Table_Component extends WPJAM_Register{ public static function registry($method, ...$args){ $registry = wpjam_registry(static::class, ['config'=>['orderby'=>'order']]); if(in_array($method, ['add_object', 'remove_object']) && !is_null($args[1])){ $part = str_replace('wpjam_list_table_', '', $registry->name); $name = $args[0]; if($data_type = wpjam_admin('data_type', $args[1])){ $args[0] .= '__'.md5(wpjam_serialize($data_type)); } if($method == 'add_object'){ if($part == 'action'){ if(!empty($args[1]['update_setting'])){ $model = wpjam_admin('list_table', 'model'); $args[1] += ['overall'=>true, 'callback'=>[$model, 'update_setting'], 'value_callback'=>[$model, 'get_setting']]; } if(!empty($args[1]['overall']) && $args[1]['overall'] !== true){ $registry->$method($name.'_all', ['overall'=>true, 'title'=>wpjam_pull($args[1], 'overall')]+$args[1]); } }elseif($part == 'column'){ $args[1]['_field'] = wpjam_field(wpjam_pick($args[1], ['name', 'options'])+['type'=>'view', 'wrap_tag'=>'', 'key'=>$name]); } $args[1] = new static($name, $args[1]); }else{ if(!static::get($args[0])){ return wpjam_admin('removed_'.$part.'s[]', $name); } } } return $registry->$method(...$args); } } class WPJAM_List_Table_Action extends WPJAM_List_Table_Component{ public function __get($key){ $value = parent::__get($key); if(!is_null($value)){ return $value; } if($key == 'page_title'){ return $this->title ? wp_strip_all_tags($this->title.wpjam_admin('list_table', 'title')) : ''; }elseif($key == 'response'){ return $this->next ? 'form' : ($this->overall && $this->name != 'add' ? 'list' : $this->name); }elseif($key == 'row_action'){ return $this->bulk !== 'only' && $this->name != 'add'; }elseif(in_array($key, ['layout', 'model', 'builtin', 'form_data', 'primary_key', 'data_type', 'capability', 'next_actions']) || ($this->data_type && $this->data_type == $key)){ return wpjam_admin('list_table', $key); } } public function __toString(){ return $this->title; } public function __call($method, $args){ if($this->mod($method, 'prev')){ $prev = self::get($this->prev ?: array_search($this->name, $this->next_actions ?: [])); return $prev ? [$prev, $action](...$args) : []; }elseif($this->mod($method, 'parse_')){ $args = $args[0]; if($method == 'nonce_action'){ return wpjam_join('-', $this->name, empty($args['bulk']) ? ($args['id'] ?? '') : ''); }elseif($method == 'arg' && !$this->overall){ return is_scalar($args) ? $args : ((int)$args['bulk'] === 2 ? (($args['id'] ?? '') ?: $args['ids']) : $args[$args['bulk'] ? 'ids' : 'id']); }elseif($method == 'id' && !$this->overall){ return $args['bulk'] ? null : $args['id']; } } } public function __invoke($type){ $params = $this->get_param(['id', 'ids', 'bulk', 'submit'], $type == 'export' ? 'get' : 'post'); $id = $params['id']; $bulk = $params['bulk']; $ids = wp_parse_args($params['ids'] ?: []); $bulk = is_numeric($bulk) ? (int)$bulk : ($bulk && $bulk !== 'false' ? 1 : 0); $args = ['bulk'=>&$bulk, 'id'=>$id, 'ids'=>$ids]; $data = wpjam_params('data'); if($type == 'submit'){ $args += ['fields'=>$this->get_fields($args, true)]; $data = $args['fields']->validate($data); }elseif(in_array($type, ['direct', 'form'])){ $data += $this->overall ? $this->form_data : []; } $args += ['data'=>$data]; $submit = in_array($type, ['submit', 'export']) ? ($params['submit'] ?: $this->name) : null; $button = $submit ? wpjam_button($this, $submit, $this->parse_arg($args)) : []; if(in_array($type, ['submit', 'direct']) && (($button['export'] ?? $this->export) || ($this->bulk === 'export' && $bulk))){ return ['type'=>'redirect', 'args'=>['submit'=>$submit]+$this->get_attr($args, 'export')]; } $resp = $this->is_allowed($args) ? $this->pick(['page_title', 'last', 'width'])+['bulk'=>&$bulk, 'id'=>&$id, 'ids'=>$ids] : wp_die('access_denied'); if($type == 'form'){ return $resp+['type'=>'form', 'form'=>$this->get_form($args, $type)]; } $resp += ['list_action'=>$this->name, 'type'=>($button['response'] ?? '') ?: $this->response]; $bulk = $bulk === 2 ? 0 : $bulk; if($resp['type'] != 'form'){ $args += wpjam_fill(['callback', 'bulk_callback'], fn($k)=> ($button[$k] ?? '') ?: $this->$k); $result = $this->callback(['data'=>$data, 'submit'=>$submit]+$args); if(is_array($result)){ $err = wpjam_pull($result, ['errcode', 'errmsg']); $resp += empty($err['errcode']) ? ['notice'=>wpjam_pull($result, 'notice') ?: ($err['errmsg'] ?? '')] : $err; $resp += wpjam_pull($result, ['args']); if($this->layout == 'calendar'){ $resp['data'] = $result; }else{ if(wpjam_pick($result, ['type', 'bulk', 'ids', 'id', 'items'])){ [$resp, $result] = [$result+$resp, null]; } } }else{ $resp['notice'] = $button ? $button['text'].'成功' : ''; if(in_array($resp['type'], ['add', 'duplicate']) && !$resp['bulk'] && $this->layout != 'calendar'){ [$id, $result] = [$result, null]; } } if($resp['type'] == 'append'){ return $resp+array_filter(['data'=>$result]); }elseif($resp['type'] == 'redirect'){ return $resp+$this->pick(['target'])+(is_string($result) ? ['url'=>$result] : []); } if($result){ $resp['result'] = $result; } } if($submit){ if(($button['dismiss'] ?? $this->dismiss) || array_find($resp['type'] == 'items' ? $resp['items'] : [$resp], fn($v)=> !empty($v['dismiss']) || $v['type'] == 'delete')){ return ['dismiss'=>true]+$resp; } if($next = self::get($this->next)){ $resp = $next->pick(['page_title'])+$this->pick(['next'])+$resp; } if($resp['type'] == 'form'){ if($next){ $resp['notice'] = ''; } }else{ $data = wpjam_params('defaults'); } $resp['form'] = ($next ?: $this)->get_form(['data'=>$data, 'bulk'=>$bulk, 'id'=>$id, 'ids'=>$ids], $type); } return $resp; } public function callback($args){ $name = $this->name; $bulk = $args['bulk']; $data = $args['data']; if(in_array($name, ['up', 'down'])){ $data = $data+[$name=>true]; $name = 'move'; } $params = [$this->parse_arg($args), $data]; $noid = $this->overall; if($cb = $args[($bulk ? 'bulk_' : '').'callback'] ?? ''){ if(!$noid && !$bulk && ($this->response == 'add' || $name == 'add') && !is_null($data)){ $result = wpjam_get_reflection($cb, 'Parameters') ?: []; $noid = count($result) <= 1 || $result[0]->name == 'data'; } $noid && array_shift($params); return wpjam_trap($cb, ...[...$params, $args['submit'] ?: $this->name, 'throw']) ?? wp_die('「'.$this->title.'」的回调函数无效或没有正确返回'); } if($bulk){ $cb = [$this->model, 'bulk_'.$name]; if(method_exists(...$cb)){ return wpjam_try($cb, ...$params) ?? true; } return array_reduce($args['ids'], fn($c, $id)=> wpjam_merge($c, is_array($r = $this->callback(['id'=>$id, 'bulk'=>false]+$args)) ? $r : []), []) ?: true; } $m = $name == 'duplicate' && !$this->direct ? 'insert' : (['add'=>'insert', 'edit'=>'update'][$name] ?? $name); $cb = [$this->model, $m]; if($noid || $m == 'insert' || $this->response == 'add'){ array_shift($params); }elseif(method_exists(...$cb)){ $this->direct && is_null($data) && array_pop($params); }elseif($this->meta_type || !method_exists($cb[0], '__callStatic')){ $cb[1] = 'update_callback'; if(!method_exists(...$cb)){ $params = [wpjam_admin('meta_type'), ...$params]; $cb = $params[0] ? 'wpjam_update_metadata' : wp_die('「'.$cb[0].'->'.$name.'」未定义'); } $params[] = $args['fields']->get_defaults(); } return wpjam_try($cb, ...$params) ?? true ; } public function is_allowed($args=[]){ return $this->capability == 'read' || array_all($args && !$this->overall ? (array)$this->parse_arg($args) : [null], fn($id)=> wpjam_can($this->capability, $id, $this->name)); } public function get_data($id, $type=''){ $cb = $type ? $this->data_callback : null; $data = $cb ? (is_callable($cb) ? wpjam_try($cb, $id, $this->name) : wp_die($this->title.'的 data_callback 无效')) : null; if($type == 'prev'){ return array_merge($this->get_prev_data($id, 'prev'), ($data ?: [])); } if($id && !$cb){ $data = wpjam_try([$this->model, 'get'], $id); $data = $data instanceof WPJAM_Register ? $data->to_array() : ($data ?: wp_die('无效的 ID「'.$id.'」')); } return $data; } public function get_form($args, $type=''){ $id = $this->parse_id($args); $data = $type == 'submit' && $this->response == 'form' ? [] : $this->get_data($id, 'callback'); $fields = $this->get_fields(wpjam_merge($args, array_filter(['data'=>$data]))); $args = wpjam_merge($args, ['data'=>($id && $type == 'form' ? $this->get_prev_data($id, 'prev') : [])]); return $fields->wrap('form', [ 'id' => 'list_table_action_form', 'data' => $this->get_attr($args, 'form'), 'button' => wpjam_button($this, null, $this->parse_arg($args))->prepend($this->render_prev(['class'=>['button'], 'title'=>'上一步']+$args)) ]); } public function get_fields($args, $prev=false, $output='object'){ $arg = $this->parse_arg($args); $fields = wpjam_try('maybe_callback', $this->fields, $arg, $this->name) ?: wpjam_try($this->model.'::get_fields', $this->name, $arg); $fields = array_merge(is_array($fields) ? $fields : [], ($prev ? $this->get_prev_fields($arg, true, '') : [])); $fields = ($cb = $this->model.'::filter_fields') && wpjam_callback($cb) ? wpjam_try($cb, $fields, $arg, $this->name) : $fields; if(!in_array($this->name, ['add', 'duplicate']) && isset($fields[$this->primary_key])){ $fields[$this->primary_key]['type'] = 'view'; } if($output == 'object'){ $id = $this->parse_id($args); $args = ['id'=>$id]+$args+($id ? array_filter(['meta_type'=>wpjam_admin('meta_type'), 'model'=>$this->model]) : []); return wpjam_fields($fields, array_filter(['value_callback'=>$this->value_callback])+$args); } return $fields; } public function get_attr($args, $type='button'){ $data = wp_parse_args(($args['data'] ?? []), ($this->data ?: [])); $data += $this->layout == 'calendar' ? wpjam_pick($args, ['date']) : []; $keys = $type == 'export' ? ['export_action', '_wpnonce'] : ['action', 'nonce']; $attr = ['data'=>$data]+array_combine($keys, [$this->name, wp_create_nonce($this->parse_nonce_action($args))]); $attr += $this->overall ? [] : wpjam_pick($args, [$args['bulk'] ? 'ids' : 'id'])+($args['bulk'] ? $this->pick(['bulk', 'title']) : []); return $attr+($type == 'export' ? [] : $this->pick($type == 'button' ? ['direct', 'confirm'] : ['next'])); } public function render($args=[]){ $args += ['id'=>0, 'data'=>[], 'bulk'=>false, 'ids'=>[]]; $id = $args['id']; if(is_callable($this->show_if)){ $show_if = wpjam_trap($this->show_if, ...(!empty($args['item']) ? [$args['item'], null] : [$id, $this->name, null])); if(!$show_if){ return; }elseif(is_array($show_if)){ $args += $show_if; } }elseif($this->show_if){ if($id && !wpjam_match(($args['item'] ?? '') ?: $this->get_data($id), $this->show_if)){ return; } } if($this->builtin && $id){ if($this->data_type == 'post_type'){ if(!wpjam_compare(get_post_status($id), ...(array_filter([$this->post_status]) ?: ['!=', 'trash']))){ return; } }elseif($this->data_type == 'user'){ if($this->roles && !array_intersect(get_userdata($id)->roles, (array)$this->roles)){ return; } } } if(!$this->is_allowed($args)){ return ($v = $args['fallback'] ?? null) === true ? ($args['title'] ?? null) : $v; } $attr = wpjam_pick($args, ['class', 'style'])+['title'=>$this->page_title]; $tag = wpjam_tag($args['tag'] ?? 'a', $attr)->add_class($this->class)->style($this->style); if($this->redirect){ $href = maybe_callback($this->redirect, $id, $args); $tag = $href ? $tag->add_class('list-table-redirect')->attr(['href'=>str_replace('%id%', $id, $href)]+$this->pick(['target'])) : ''; }elseif($this->filter || $this->filter === []){ $filter = maybe_callback($this->filter, $id) ?? false; $tag = $filter === false ? '' : $tag->add_class('list-table-filter')->data('filter', array_merge(($this->data ?: []), (wpjam_is_assoc_array($filter) ? $filter : ($this->overall ? [] : wpjam_pick((array)$this->get_data($id), (array)$filter))), $args['data'])); }else{ $tag->add_class('list-table-'.(str_starts_with($this->response, 'move') ? str_replace('_', '-', $this->response).'-' : '').'action')->data($this->get_attr($args)); } $text = wpjam_icon($args) ?? ($args['title'] ?? null); $text ??= (!$tag->has_class('page-title-action') && ($this->layout == 'calendar' || !$this->title)) ? wpjam_icon($this) : null; $text ??= $this->title ?: $this->page_title; return $tag ? $tag->text($text)->wrap($args['wrap'] ?? '', $this->name) : null; } public static function registers($actions){ foreach($actions as $key => $args){ self::register($key, $args+['order'=>10.5]); } } } class WPJAM_List_Table_Column extends WPJAM_List_Table_Component{ public function __get($key){ $value = parent::__get($key); if($key == 'style'){ $value = $this->column_style ?: $value; $value = ($value && !preg_match('/\{([^\}]*)\}/', $value)) ? 'table.wp-list-table .column-'.$this->name.'{'.$value.'}' : $value; }elseif(in_array($key, ['title', 'callback', 'description', 'render'])){ $value = $this->{'column_'.$key} ?? $value; }elseif(in_array($key, ['sortable', 'sticky'])){ $value ??= $this->{$key.'_column'}; } return $value; } public function __invoke($args){ $id = $args['id']; $value = $this->_field->val(null)->value_callback($args) ?? wpjam_value_callback($args, $this->name) ?? $this->default; if(wpjam_is_assoc_array($value)){ return $value; } $cb = is_callable($this->callback) ? $this->callback : null; $value = $cb ? wpjam_call($cb, $id, $this->name, $args['data'], $value) : $value; if($render = $this->render){ if(is_callable($render)){ return $render($value, $args['data'], $this->name, $id); } if($this->type == 'img'){ $size = wpjam_constrain_dimensions($this->size ?: '600x0', [600, 600]); return $value ? '' : ''; }elseif($this->type == 'timestamp'){ return $value ? wpjam_date('Y-m-d H:i:s', $value) : ''; } } return $cb ? $value : $this->filterable($value); } protected function filterable($value){ if(is_array($value)){ return array_map([$this, 'filterable'], $value); } if($value && str_contains($value, '[filter')){ return $value; } $filter = isset(wpjam_admin('list_table', 'filterable_fields')[$this->name]) ? [$this->_field->name => $value] : []; $value = $this->options ? $this->_field->val($value)->render() : $value; return $filter ? ['filter'=>$filter, 'label'=>$value] : $value; } public static function registers($fields){ foreach($fields as $key => $field){ $column = wpjam_pull($field, 'column'); if($field['show_admin_column'] ?? is_array($column)){ self::register($key, ($column ?: [])+wpjam_except(array_diff_key($field, wpjam_admin('data_type', $field)), ['style', 'description', 'render'])+['order'=>10.5]); } } } } class WPJAM_List_Table_View extends WPJAM_List_Table_Component{ public function __invoke(){ if($this->_view){ return $this->_view; } $view = $this; $cb = $this->callback; if($cb && is_callable($cb)){ $view = wpjam_trap($cb, $this->name, null); if(!is_array($view)){ return $view; } } if(!empty($view['label'])){ $filter = $view['filter'] ?? []; return [ $filter, $view['label'].(is_numeric($view['count'] ?? '') ? wpjam_tag('span', ['count'], '('.$view['count'].')') : ''), $view['class'] ?? (array_any($filter, fn($v, $k)=> (((($c = $this->get_param($k)) === null) xor ($v === null)) || $c != $v)) ? '' : 'current') ]; } } public static function registers($views){ foreach(array_filter($views) as $name => $view){ $name = is_numeric($name) ? 'view_'.$name : $name; $view = is_array($view) ? array_diff_key($view, wpjam_admin('data_type', $view)) : $view; $view = (is_string($view) || is_object($view)) ? ['_view'=>$view] : $view; self::register($name, $view); } } }