| 1 |
<?php |
| 2 |
class WPJAM_Admin extends WPJAM_Args{ |
| 3 |
private function __construct(){ |
| 4 |
$this->args = ['prefix'=>is_network_admin() ? 'network_' : (is_user_admin() ? 'user_' : '')]; |
| 5 |
|
| 6 |
add_action('wpjam_admin_init', ['WPJAM_Notice', 'on_admin_init']); |
| 7 |
|
| 8 |
add_action(wp_doing_ajax() ? 'admin_init' : $this->prefix.'admin_menu', [$this, 'init'], 9); |
| 9 |
|
| 10 |
wp_doing_ajax() || add_filter('wpjam_html', fn($html)=> str_replace( |
| 11 |
['dashicons-before dashicons-ri-', '/wp-admin/page='], |
| 12 |
['ri-', '/wp-admin/admin.php?page='], |
| 13 |
$html |
| 14 |
)); |
| 15 |
} |
| 16 |
|
| 17 |
public function __invoke($key, ...$args){ |
| 18 |
if(!$key){ |
| 19 |
return $this; |
| 20 |
} |
| 21 |
|
| 22 |
if(method_exists($this, $key)){ |
| 23 |
return $this->$key(...$args); |
| 24 |
} |
| 25 |
|
| 26 |
$value = $this->get_arg($key); |
| 27 |
|
| 28 |
if(!$args){ |
| 29 |
return $value ?? $this->get_arg('vars['.$key.']'); |
| 30 |
} |
| 31 |
|
| 32 |
if(is_object($value) && !is_object($args[0])){ |
| 33 |
return count($args) >= 2 ? ($value->{$args[0]} = $args[1]) : $value->{$args[0]}; |
| 34 |
} |
| 35 |
|
| 36 |
if(in_array($key, ['script', 'style'])){ |
| 37 |
$key .= '[]'; |
| 38 |
$value = implode("\n", (array)$args[0]); |
| 39 |
}else{ |
| 40 |
$value = $key == 'query_data' ? array_merge($value ?: [], wpjam_map($args[0], fn($v)=> is_array($v) |
| 41 |
? wp_die('query_data 不能为数组') |
| 42 |
: (is_null($v) ? $v : sanitize_textarea_field($v)) |
| 43 |
)) : $args[0]; |
| 44 |
} |
| 45 |
|
| 46 |
$this->process_arg($key, fn()=> $value); |
| 47 |
|
| 48 |
return $value; |
| 49 |
} |
| 50 |
|
| 51 |
public function url($path=''){ |
| 52 |
return wpjam_call($this->prefix.'admin_url', $path); |
| 53 |
} |
| 54 |
|
| 55 |
public function error($msg='', $type=''){ |
| 56 |
if($msg === ''){ |
| 57 |
return wpjam_map($this->error ?: [], fn($e)=> wpjam_echo(wpjam_tag('div', ['is-dismissible', 'notice', 'notice-'.$e[0]], ['p', [], $e[1]]))); |
| 58 |
} |
| 59 |
|
| 60 |
if($args = is_wp_error($msg) ? ['error', $msg->get_error_message()] : ($msg ? [$type ?: 'success', $msg] : '')){ |
| 61 |
$this->update_arg('error[]', $args); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function enqueue(){ |
| 66 |
$ver = get_plugin_data(WPJAM_BASIC_PLUGIN_FILE)['Version']; |
| 67 |
$static = wpjam_url(dirname(__DIR__), 'relative').'/static'; |
| 68 |
|
| 69 |
wp_enqueue_media(wpjam_pick($this, ['post_id'])); // 引� |
| 70 |
� jquery-ui-sortable |
| 71 |
wp_enqueue_script('wpjam-script', $static.'/script.js', ['wp-color-picker', 'jquery-ui-autocomplete', ...(wp_is_mobile() ? ['jquery-touch-punch'] : [])], $ver); |
| 72 |
|
| 73 |
wp_localize_script('wpjam-script', 'wpjam_page_setting', array_map('maybe_closure', $this->vars)+[ |
| 74 |
'upload_files' => current_user_can('upload_files'), |
| 75 |
'admin_url' => $GLOBALS['current_admin_url'], |
| 76 |
'loading' => includes_url('js/thickbox/loadingAnimation.gif'), |
| 77 |
]+$this->pick(['query_data', 'query_url', 'block'])); |
| 78 |
|
| 79 |
$this->block && wp_enqueue_script('wpjam-blocks', $static.'/blocks.js', ['wp-plugins', 'wp-editor', 'wp-element', 'wp-components', 'wp-data'], '1.0', true); |
| 80 |
|
| 81 |
wp_enqueue_style('wpjam-style', $static.'/style.css', ['wp-color-picker', 'editor-buttons'], $ver); |
| 82 |
|
| 83 |
$this->style && wp_add_inline_style('wpjam-style', "\n".implode("\n\n", array_filter($this->style))); |
| 84 |
$this->script && wp_add_inline_script('wpjam-script', "jQuery(function($){".preg_replace('/^/m', "\t", "\n".implode("\n\n", $this->script))."\n});"); |
| 85 |
} |
| 86 |
|
| 87 |
public function data_type(...$args){ |
| 88 |
if(!$args){ |
| 89 |
return $this->data_type; |
| 90 |
} |
| 91 |
|
| 92 |
if(is_scalar($args[0])){ |
| 93 |
return $this->data_type = $args[0]; |
| 94 |
} |
| 95 |
|
| 96 |
$type = wpjam_get($args[0], 'data_type'); |
| 97 |
|
| 98 |
return $type ? (['data_type'=>$type]+(in_array($type, ['post_type', 'taxonomy']) ? [$type => wpjam_get($args[0], $type, '')] : [])) : []; |
| 99 |
} |
| 100 |
|
| 101 |
public function dashboard($action, ...$args){ |
| 102 |
if($action == 'add_widget'){ |
| 103 |
return $this->update_arg('widgets['.array_shift($args).']', ...$args); |
| 104 |
} |
| 105 |
|
| 106 |
if(is_array($action)){ |
| 107 |
$args = [$action]; |
| 108 |
$action = ''; |
| 109 |
} |
| 110 |
|
| 111 |
if($args){ |
| 112 |
$this->dashboard = new WPJAM_Args(...$args); |
| 113 |
} |
| 114 |
|
| 115 |
$object = $this->dashboard; |
| 116 |
|
| 117 |
if(!$action || !$object){ |
| 118 |
return $object; |
| 119 |
} |
| 120 |
|
| 121 |
$name = $object->name; |
| 122 |
|
| 123 |
if($action == 'load'){ |
| 124 |
$name != 'dashboard' && require_once ABSPATH.'wp-admin/includes/dashboard.php'; |
| 125 |
|
| 126 |
wp_enqueue_script('dashboard'); |
| 127 |
|
| 128 |
foreach(array_merge(maybe_callback($object->widgets, $name) ?: [], array_filter($this->widgets ?: [], fn($v)=> ($v['dashboard'] ?? 'dashboard') == $name)) as $id => $w){ |
| 129 |
add_meta_box( |
| 130 |
$w['id'] ?? $id, |
| 131 |
$w['title'], |
| 132 |
$w['callback'] ?? wpjam_get_filter_name($w['id'] ?? $id, 'dashboard_widget_callback'), |
| 133 |
get_current_screen(), |
| 134 |
$w['context'] ?? 'normal', |
| 135 |
$w['priority'] ?? 'core', |
| 136 |
$w['args'] ?? [] |
| 137 |
); |
| 138 |
} |
| 139 |
}elseif($action == 'render'){ |
| 140 |
return wpjam_tag('div', ['id'=>'dashboard-widgets-wrap'], wpjam_ob('wp_dashboard'))->before(($panel = wpjam_ob($object->welcome_panel, $name)) ? wpjam_tag('div', ['id'=>'welcome-panel', 'class'=>'welcome-panel wpjam-welcome-panel'], $panel) : ''); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function load(...$args){ |
| 145 |
if(count($args) >= 2){ |
| 146 |
$map = ['builtin_page'=>'base', 'plugin_page'=>'plugin_page']; |
| 147 |
$type = $args[0] ?? array_find_key($map, fn($v)=> isset($args[1][$v])); |
| 148 |
|
| 149 |
return isset($map[$type]) && $this->update_arg($type.'_load[]', $args[1]); |
| 150 |
} |
| 151 |
|
| 152 |
if($screen = array_shift($args)){ |
| 153 |
$this->screen = $screen; |
| 154 |
$this->vars = ($this->vars ?? [])+['screen_id'=>$screen->id]+array_filter(wpjam_pick($screen, ['post_type', 'taxonomy'])); |
| 155 |
|
| 156 |
$screen->base == 'post' && ($this->post_id = (int)($_GET['post'] ?? ($_POST['post_ID'] ?? 0))); |
| 157 |
} |
| 158 |
|
| 159 |
if($this->plugin_page){ |
| 160 |
$type = 'plugin_page'; |
| 161 |
$object = $this->current_tab ?: $this->plugin_page; |
| 162 |
$args = [$object->menu_slug, $this->current_tab ? $object->tab_slug : '']; |
| 163 |
|
| 164 |
$this->vars += ['plugin_page'=>$args[0]]+($args[1] ? ['current_tab'=>$args[1]] : []); |
| 165 |
|
| 166 |
if(!$args[1] && $screen && str_contains($screen->id, '%')){ |
| 167 |
$screen->id = preg_replace_callback('/^(.*?)(_page_.*)/', fn($m)=> (array_search($m[1], $GLOBALS['admin_page_hooks']) ?: $m[1]).$m[2], $screen->id); |
| 168 |
} |
| 169 |
}else{ |
| 170 |
if($screen->base == 'customize' || !empty($GLOBALS['plugin_page'])){ |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$type = 'builtin_page'; |
| 175 |
$args = [$screen]; |
| 176 |
$page = $this->$type = ($_POST[$type] ?? '') ?: $GLOBALS['pagenow']; |
| 177 |
$url = add_query_arg(array_intersect_key($_REQUEST, wpjam_pick($this->vars, ['taxonomy', 'post_type'])), $this->url($page)); |
| 178 |
|
| 179 |
$GLOBALS['current_admin_url'] = $url; |
| 180 |
|
| 181 |
$this->vars += [$type=>$page]; |
| 182 |
|
| 183 |
if(in_array($screen->base, ['edit', 'upload', 'post'])){ |
| 184 |
if(!($this->type_object = wpjam_get_post_type_object($GLOBALS['typenow']))){ |
| 185 |
return; |
| 186 |
} |
| 187 |
}elseif(in_array($screen->base, ['term', 'edit-tags'])){ |
| 188 |
if(!($this->tax_object = wpjam_get_taxonomy($GLOBALS['taxnow']))){ |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
do_action('wpjam_'.$type.'_load', ...$args); // � |
| 195 |
�容 |
| 196 |
|
| 197 |
foreach(wpjam_sort(array_filter($this->get_arg($type.'_load[]'), function($load){ |
| 198 |
if($this->plugin_page){ |
| 199 |
$page = $this->plugin_page->name; |
| 200 |
$tab = ($tab = $this->current_tab) ? $tab->name : ''; |
| 201 |
$pv = $load['plugin_page'] ?? ''; |
| 202 |
$tv = $load['current_tab'] ?? ''; |
| 203 |
|
| 204 |
if($pv && is_callable($pv)){ |
| 205 |
return $pv($page, $tab); |
| 206 |
} |
| 207 |
|
| 208 |
if($pv && !wpjam_compare($page, $pv)){ |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
return $tv ? ($tab && wpjam_compare($tab, $tv)) : !$tab; |
| 213 |
}else{ |
| 214 |
if(($sv = $load['screen'] ?? '') && is_callable($sv) && !$sv($this->screen)){ |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
return array_all(['base', 'post_type', 'taxonomy'], fn($k) => empty($load[$k]) || wpjam_compare($this->screen->$k, $load[$k])); |
| 219 |
} |
| 220 |
}), 'order', 'desc', 10) as $load){ |
| 221 |
wpjam_map((array)($load['page_file'] ?? []), fn($f)=> is_file($f) && include $f); |
| 222 |
|
| 223 |
if($cb = $load['callback'] ?? ''){ |
| 224 |
wpjam_call($cb, ...$args); |
| 225 |
}elseif($model = $load['model'] ?? ''){ |
| 226 |
if($m = array_find(['load', $type.'_load'], fn($m)=> method_exists($model, $m))){ |
| 227 |
wpjam_call_method($model, $m, ...$args); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
wpjam_trap($this->plugin_page ? [$object, 'load'] : [$this, 'builtin'], $screen, fn($e)=> $this->error($e)); |
| 233 |
|
| 234 |
add_action('admin_enqueue_scripts', [$this, 'enqueue'], 9); |
| 235 |
add_action('all_admin_notices', [$this, 'error'], 9); |
| 236 |
} |
| 237 |
|
| 238 |
public function builtin(){ |
| 239 |
$base = $this->screen->base; |
| 240 |
$type = $GLOBALS['typenow']; |
| 241 |
$tax = $GLOBALS['taxnow']; |
| 242 |
|
| 243 |
if($base == 'edit' |
| 244 |
|| ($base == 'upload' && (($_GET['mode'] ?? '') ?: get_user_option('media_library_mode', get_current_user_id())) === 'list') |
| 245 |
){ |
| 246 |
$object = $this->type_object; |
| 247 |
|
| 248 |
WPJAM_Builtin_List_Table::load(wpjam_pick($object, ['title', 'model', 'hierarchical'])+[ |
| 249 |
'capability' => fn($id)=> $id ? 'edit_post' : $object->cap->edit_posts, |
| 250 |
'primary_key' => 'ID', |
| 251 |
'singular' => 'post', |
| 252 |
'data_type' => 'post_type', |
| 253 |
'meta_type' => 'post', |
| 254 |
'post_type' => $type, |
| 255 |
]); |
| 256 |
}elseif($base == 'post'){ |
| 257 |
$object = $this->type_object; |
| 258 |
$size = $object->thumbnail_size; |
| 259 |
$label = $object->_jam ? $object->labels->name : ''; |
| 260 |
|
| 261 |
$size && wpjam_hook('admin_post_thumbnail_html', '.', wpautop('尺寸:'.$size)); |
| 262 |
$label && wpjam_hook('post_updated_messages', '+', fn($ms)=> [$type=> array_map(fn($m)=> str_replace('文章', $label, $m), $ms['post'])]); |
| 263 |
|
| 264 |
$options = wpjam_get_post_options($type, ['list_table'=>false]); |
| 265 |
|
| 266 |
if(use_block_editor_for_post_type($type) && post_type_supports($type, 'custom-fields')){ |
| 267 |
foreach($options as $name => $option){ |
| 268 |
if(($option->modal || $option->context === 'side') && wpjam_is_assoc_array($option->fields)){ |
| 269 |
$fields = wpjam_fields($option->fields)->to_block(); |
| 270 |
$fields && $this->update_arg('block[panels][]', wpjam_pick($option, ['title', 'modal'])+compact('name', 'fields')); |
| 271 |
|
| 272 |
unset($options[$name]); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
if($options){ |
| 278 |
$this->meta_options = $options; |
| 279 |
|
| 280 |
add_action('add_meta_boxes', [$this, 'meta_options'], 10, 2); |
| 281 |
add_action('wp_after_insert_post', [$this, 'meta_options'], 999); |
| 282 |
} |
| 283 |
}elseif(in_array($base, ['term', 'edit-tags'])){ |
| 284 |
$object = $this->tax_object; |
| 285 |
$label = $object->_jam ? $object->labels->name : ''; |
| 286 |
|
| 287 |
$label && wpjam_hook('term_updated_messages', '+', fn($ms)=> [$tax=> array_map(fn($m)=> str_replace(['项目', 'Item'], [$label, ucfirst($label)], $m), $ms['_item'])]); |
| 288 |
|
| 289 |
$this->meta_options = wpjam_get_term_options($tax, ['list_table'=>false]); |
| 290 |
|
| 291 |
add_action($tax.'_'.($base == 'term' ? 'edit' : 'add').'_form_fields', [$this, 'meta_options'], 10, 2); |
| 292 |
|
| 293 |
if($base == 'edit-tags'){ |
| 294 |
wpjam_map(['slug', 'description'], fn($sup)=> $object->supports($sup) || $this->update_arg('removed_columns[]', $sup)); |
| 295 |
|
| 296 |
$this->update_arg('removed_actions[]', 'inline hide-if-no-js'); |
| 297 |
|
| 298 |
wpjam_hook('pre_insert_term', 'tap', [$this, 'meta_options'], 10, 2); |
| 299 |
|
| 300 |
add_action((wp_doing_ajax() ? 'created' : 'edited').'_term', [$this, 'meta_options']); |
| 301 |
|
| 302 |
WPJAM_Builtin_List_Table::load(wpjam_pick($object, ['title','model', 'hierarchical', 'levels', 'sortable'])+[ |
| 303 |
'capability' => $object->cap->edit_terms, |
| 304 |
'primary_key' => 'term_id', |
| 305 |
'singular' => 'tag', |
| 306 |
'data_type' => 'taxonomy', |
| 307 |
'meta_type' => 'term', |
| 308 |
'taxonomy' => $tax, |
| 309 |
'post_type' => $type, |
| 310 |
]); |
| 311 |
} |
| 312 |
}elseif($base == 'users'){ |
| 313 |
WPJAM_Builtin_List_Table::load([ |
| 314 |
'title' => '用户', |
| 315 |
'model' => 'WPJAM_User', |
| 316 |
'capability' => 'edit_user', |
| 317 |
'primary_key' => 'ID', |
| 318 |
'singular' => 'user', |
| 319 |
'data_type' => 'user', |
| 320 |
'meta_type' => 'user', |
| 321 |
]); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
public function meta_options(...$args){ |
| 326 |
$hook = current_filter(); |
| 327 |
$base = $this->screen->base; |
| 328 |
$options = $this->meta_options; |
| 329 |
|
| 330 |
if($base == 'post'){ |
| 331 |
if($hook === 'add_meta_boxes'){ |
| 332 |
$type = $args[0]; |
| 333 |
$pid = $args[1]->ID; |
| 334 |
$id = $this->screen->action == 'add' ? false : $pid; |
| 335 |
}else{ |
| 336 |
$action = 'callback'; |
| 337 |
$pid = $args[0]; |
| 338 |
$type = get_post_type($pid); |
| 339 |
|
| 340 |
if($_SERVER['REQUEST_METHOD'] != 'POST' || get_post_status($pid) == 'auto-draft' || (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (($_POST['wp-preview'] ?? '') == 'dopreview')){ |
| 341 |
return; |
| 342 |
} |
| 343 |
} |
| 344 |
}else{ |
| 345 |
if($hook == 'pre_insert_term'){ |
| 346 |
$action = 'validate'; |
| 347 |
$tax = $args[0]; |
| 348 |
$args = []; |
| 349 |
}elseif(str_ends_with($hook, '_form_fields')){ |
| 350 |
$action = 'render'; |
| 351 |
$i = str_contains($hook, 'edit') ? 1 : 0; |
| 352 |
$tax = $args[$i]; |
| 353 |
$args = [($i ? $args[0]->term_id : false), ['fields_type'=>($i ? 'tr' : 'div'), 'wrap_class'=>'form-field']]; |
| 354 |
}else{ |
| 355 |
$action = 'callback'; |
| 356 |
$tax = get_term_field('taxonomy', $args[0]); |
| 357 |
} |
| 358 |
|
| 359 |
$options = wpjam_filter($options, ['action'=>['value'=>str_contains($hook, 'edit') ? 'edit' : 'add', 'if_null'=>true]]); |
| 360 |
} |
| 361 |
|
| 362 |
foreach($options as $option){ |
| 363 |
if($base == 'post'){ |
| 364 |
$context = $option->context ?: 'normal'; |
| 365 |
|
| 366 |
if(!($cb = $option->meta_box_cb)){ |
| 367 |
if(!($option->fields = $option->get_fields($pid))){ |
| 368 |
continue; |
| 369 |
} |
| 370 |
|
| 371 |
$cb = fn()=> $option->render($id, ['fields_type'=>($context === 'side' ? 'list' : 'table')]); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if($hook === 'add_meta_boxes'){ |
| 376 |
add_meta_box($option->name, $option->title, $cb, $type, $context, $option->priority); |
| 377 |
}else{ |
| 378 |
wpjam_trap([$option, $action], ...[...$args, 'die']); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
public function init(){ |
| 384 |
if($GLOBALS['pagenow'] == 'admin-post.php'){ |
| 385 |
return; |
| 386 |
} |
| 387 |
|
| 388 |
if(wp_doing_ajax()){ |
| 389 |
wpjam_ajax('wpjam-page-action', [ |
| 390 |
'admin' => true, |
| 391 |
'nonce_action' => fn($data)=> $data['action_type'] == 'form' ? '' : ($data['page_action'] ?? ''), |
| 392 |
'callback' => fn($data)=> ($object = wpjam_get_page_action($data['page_action'] ?? '')) ? $object($data['action_type']) : wpjam_page_action_compat($data), |
| 393 |
]); |
| 394 |
|
| 395 |
wpjam_ajax('wpjam-query', [ |
| 396 |
'admin' => true, |
| 397 |
'fields' => ['data_type'=> ['required'=>true]], |
| 398 |
'callback' => fn($data)=> wpjam_query('data_type', $data), |
| 399 |
'nonce_action' => fn($data)=> wpjam_get_data_type($data['data_type'], $data['query_args'] ?? [])->nonce_action($data['query_args'] ?? []), |
| 400 |
]); |
| 401 |
|
| 402 |
wpjam_ajax('wpjam-upload', [ |
| 403 |
'admin' => true, |
| 404 |
'allow' => fn()=> current_user_can('upload_files'), |
| 405 |
'nonce_action' => fn($data)=> 'upload-'.$data['name'].'-'.$data['accept'], |
| 406 |
'callback' => fn($data)=> wpjam_upload($data['filename'] ?? $data['name'], $data) |
| 407 |
]); |
| 408 |
|
| 409 |
$screen = $_POST['screen_id'] ?? ($_POST['screen'] ?? ''); |
| 410 |
|
| 411 |
if(!$screen){ |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
$page = $GLOBALS['plugin_page'] = $_POST['plugin_page'] ?? ''; |
| 416 |
$part = $page ? wpjam_at($screen, '_page_'.$page, 1) : $screen; |
| 417 |
$type = array_find(['network', 'user'], fn($v)=> str_ends_with($part, '-'.$v)); |
| 418 |
$const = $type ? 'WP_'.strtoupper($type).'_ADMIN' : ''; |
| 419 |
|
| 420 |
$const && !defined($const) && define($const, true); |
| 421 |
|
| 422 |
$screen == 'upload' && ([$GLOBALS['hook_suffix'], $screen] = [$screen, '']); |
| 423 |
} |
| 424 |
|
| 425 |
do_action('wpjam_admin_init'); |
| 426 |
|
| 427 |
add_action('current_screen', [$this, 'load'], 9); |
| 428 |
|
| 429 |
$menu = new WPJAM_Plugin_Page(); |
| 430 |
$pages = apply_filters('wpjam_pages', $this->pages ?: []); |
| 431 |
|
| 432 |
if(wp_doing_ajax()){ |
| 433 |
$page && ($slug = array_find_key($pages, fn($v, $k)=> $page == $k || isset($v['subs'][$page]))) && $menu->parse($pages[$slug], $page, $slug); |
| 434 |
|
| 435 |
set_current_screen($screen); |
| 436 |
}else{ |
| 437 |
wpjam_map($pages, [$menu, 'parse']); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
public static function get_instance(){ |
| 442 |
static $object; |
| 443 |
return $object ??= new self(); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
class WPJAM_Plugin_Page extends WPJAM_Args{ |
| 448 |
private $builtins = []; |
| 449 |
|
| 450 |
public function __get($key){ |
| 451 |
$value = parent::__get($key); |
| 452 |
|
| 453 |
if($key == 'name'){ |
| 454 |
return $this->tab_slug ?: $this->menu_slug; |
| 455 |
}elseif($key == 'type'){ |
| 456 |
return ($value ??= $this->function) == 'list' ? 'list_table' : (in_array($value, ['option', 'list_table', 'form', 'dashboard', 'tab']) ? $value : ''); |
| 457 |
} |
| 458 |
|
| 459 |
return $value; |
| 460 |
} |
| 461 |
|
| 462 |
public function get_sub($slug){ |
| 463 |
return $this->pull('subs['.$slug.']') ?: array_filter(['menu_title'=>$this->sub_title])+wpjam_except($this->args, ['position', 'subs', 'page_title']); |
| 464 |
} |
| 465 |
|
| 466 |
public function parse($args, $slug, $parent=''){ |
| 467 |
if(wp_doing_ajax()){ |
| 468 |
$this->args = $args; |
| 469 |
|
| 470 |
[$args, $parent] = $this->subs ? [$this->get_sub($slug), $parent] : [$args, '']; |
| 471 |
}elseif(!$this->builtins){ |
| 472 |
$map = ['appearance'=>'themes', 'settings'=>'options', 'profile'=>'users']; |
| 473 |
|
| 474 |
$this->builtins = wpjam_reduce($GLOBALS['admin_page_hooks'], fn($c, $v, $k)=> $c+[(str_starts_with($k, 'edit.php?') && $v != 'pages') ? wpjam_get_post_type_setting($v, 'plural') : $v => $k]+(isset($map[$v]) ? [$map[$v]=>$k] : []), []); |
| 475 |
} |
| 476 |
|
| 477 |
$this->args = $args+['menu_slug'=>$slug]; |
| 478 |
$slug = $this->menu_slug; |
| 479 |
$builtin = $parent ? '' : ($this->builtins[$slug] ?? ''); |
| 480 |
|
| 481 |
if(!$builtin){ |
| 482 |
$path = str_contains($parent, '.php') ? $parent : 'admin.php'; |
| 483 |
|
| 484 |
if(!$this->menu_title || !$this->is_available(['network'=>$this->pull('network', ($path == 'admin.php'))])){ |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
$this->page_title ??= $this->menu_title; |
| 489 |
$this->capability ??= 'manage_options'; |
| 490 |
|
| 491 |
if(!str_contains($slug, '.php')){ |
| 492 |
$this->admin_url = add_query_arg(['page'=>$slug], $path); |
| 493 |
|
| 494 |
if(!$this->query_data()){ |
| 495 |
return; |
| 496 |
} |
| 497 |
|
| 498 |
$cb = '__return_true'; |
| 499 |
}else{ |
| 500 |
str_starts_with($slug, $GLOBALS['pagenow']) && array_all(wp_parse_args(parse_url($slug, PHP_URL_QUERY)), fn($v, $k)=> $v == ($_GET[$k] ?? '')) && wpjam_hook('parent_file', $parent ?: $slug); |
| 501 |
} |
| 502 |
|
| 503 |
$object = ($this->is_current() && ($parent || (!$parent && !$this->subs))) ? wpjam_admin('plugin_page', wp_clone($this)) : null; |
| 504 |
$args = [$this->page_title, $this->menu_title, $this->capability, $slug, ($object ? [$object, 'render'] : ($cb ?? null)), $this->position]; |
| 505 |
$icon = $parent ? '' : ($this->icon ? wpjam_prefix($this->icon, 'dashicons-') : ''); |
| 506 |
$hook = $parent ? add_submenu_page($parent, ...$args) : add_menu_page(...wpjam_add_at($args, -1, $icon)); |
| 507 |
|
| 508 |
$object && wpjam_admin('page_hook', $hook); |
| 509 |
} |
| 510 |
|
| 511 |
if(!$parent && ($builtin || $this->subs)){ |
| 512 |
$subs = ($builtin ? [] : [$slug=>$this->get_sub($slug)])+wpjam_sort($this->subs, fn($v)=> ($v['order'] ?? 10) - ($v['position'] ?? 10)*1000); |
| 513 |
|
| 514 |
wpjam_map($subs, fn($sub, $s)=> $this->parse($sub, $s, $builtin ?: $slug)); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
private function is_available($args){ |
| 519 |
return array_all([ |
| 520 |
'network' => fn($v)=> is_network_admin() ? (bool)$v : $v !== 'only', |
| 521 |
'capability' => fn($v)=> current_user_can($v), |
| 522 |
'plugin_page' => fn($v)=> $v == $this->menu_slug |
| 523 |
], fn($cb, $k)=> isset($args[$k]) ? $cb($args[$k]) : true); |
| 524 |
} |
| 525 |
|
| 526 |
public function is_current(){ |
| 527 |
return ($this->tab_slug ? $GLOBALS['current_tab'] : $GLOBALS['plugin_page']) == $this->name; |
| 528 |
} |
| 529 |
|
| 530 |
public function query_data(){ |
| 531 |
if($this->query_args){ |
| 532 |
$query_data = $this->get_param($this->query_args); |
| 533 |
|
| 534 |
if($null_data = array_filter($query_data, fn($v)=> is_null($v))){ |
| 535 |
return $this->is_current() ? wp_die('「'.implode('」,「', array_keys($null_data)).'」参数无法获取') : false; |
| 536 |
} |
| 537 |
|
| 538 |
wpjam_admin('query_url[]', [$this->admin_url, ($this->admin_url = add_query_arg($query_data, $this->admin_url))]); |
| 539 |
|
| 540 |
$this->is_current() && wpjam_admin('query_data', $query_data); |
| 541 |
} |
| 542 |
|
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
private function throw($title){ |
| 547 |
wpjam_throw('error', $title); |
| 548 |
} |
| 549 |
|
| 550 |
private function include(){ |
| 551 |
$cb = $this->pull('load_callback'); |
| 552 |
$cb && !is_callable($cb) && $this->include(); // load_callback 优� |
| 553 |
�文件加载,如不存在,尝试� |
| 554 |
�加载文件 |
| 555 |
$cb && wpjam_call($cb, $this->name); |
| 556 |
|
| 557 |
wpjam_map((array)$this->pull(($this->tab_slug ? 'tab' : 'page').'_file'), fn($f)=> include($f)); |
| 558 |
} |
| 559 |
|
| 560 |
private function defaults($defaults=null){ |
| 561 |
($defaults ??= $this->defaults) && is_array($defaults) && wpjam_default($defaults); |
| 562 |
} |
| 563 |
|
| 564 |
public function load(){ |
| 565 |
$this->defaults(); |
| 566 |
$this->include(); |
| 567 |
|
| 568 |
do_action('wpjam_plugin_page', $this, ($type = $this->type)); |
| 569 |
|
| 570 |
if($type && $type != 'tab'){ |
| 571 |
$name = $this->{$type.'_name'} ?: $this->menu_slug; |
| 572 |
$object = $this->page_object($type, $name, 'preprocess'); |
| 573 |
} |
| 574 |
|
| 575 |
if($this->data_type){ |
| 576 |
$data_type = wpjam_admin('data_type', $this->data_type); |
| 577 |
$dt_object = wpjam_get_data_type($data_type, $this->args); |
| 578 |
|
| 579 |
$dt_object && $dt_object->meta_type && wpjam_admin('meta_type', $dt_object->meta_type); |
| 580 |
|
| 581 |
in_array($data_type, ['post_type', 'taxonomy']) && $this->$data_type && !wpjam_admin('screen', $data_type) && wpjam_admin('screen', $data_type, $this->$data_type); |
| 582 |
} |
| 583 |
|
| 584 |
$this->actions && wpjam_map($this->actions, 'wpjam_register_page_action', 'kv'); |
| 585 |
$this->chart && wpjam_chart(is_array($this->chart) ? $this->chart : []); |
| 586 |
$this->editor && add_action('admin_footer', 'wp_enqueue_editor'); |
| 587 |
|
| 588 |
$GLOBALS['current_admin_url'] = wpjam_admin('url', $this->admin_url); |
| 589 |
|
| 590 |
if($type == 'tab'){ |
| 591 |
$GLOBALS['current_tab'] = wp_doing_ajax() ? ($_POST['current_tab'] ?? null) : ($_GET['tab'] ?? null); |
| 592 |
|
| 593 |
$tabs = $this->get_arg('tabs', [], 'callback'); |
| 594 |
$tabs = wpjam_reduce(wpjam_admin('tabs[]'), fn($c, $v, $k)=> $this->is_available($v) ? $c+[$v['tab_slug']=>$v] : $c, $tabs); |
| 595 |
$tabs = wpjam_array(wpjam_sort($tabs, 'order', 'desc', 10), fn($s, $tab)=> ($tab = new self(['tab_slug'=>$s, 'admin_url'=>$this->admin_url.'&tab='.$s]+$tab+['capability'=>$this->capability]))->query_data() ? [$s, $tab, $GLOBALS['current_tab'] ??= $s] : null); |
| 596 |
|
| 597 |
$object = $tabs ? ($tabs[$GLOBALS['current_tab']] ?? $this->throw('无效的 Tab')) : $this->throw('Tabs 未设置'); |
| 598 |
|
| 599 |
$object->type || $object->function || $this->throw('Tab 未设置 function'); |
| 600 |
$object->type == 'tab' && $this->throw('Tab 不能嵌套 Tab'); |
| 601 |
|
| 602 |
$object->menu_slug = $this->menu_slug; |
| 603 |
|
| 604 |
if(!wp_doing_ajax()){ |
| 605 |
$this->tabs = $tabs; |
| 606 |
$this->render = [$object, 'render']; |
| 607 |
} |
| 608 |
|
| 609 |
wpjam_admin('current_tab', $object); |
| 610 |
wpjam_admin('load'); |
| 611 |
}elseif($type){ |
| 612 |
$object ??= $this->page_object($type, $name); |
| 613 |
|
| 614 |
if($type == 'dashboard'){ |
| 615 |
$load = fn()=> wpjam_dashboard('load'); |
| 616 |
$render = fn()=> wpjam_dashboard('render'); |
| 617 |
}else{ |
| 618 |
$load = wpjam_callback([$object, 'page_load']);; |
| 619 |
$render = [$object, 'render']; |
| 620 |
} |
| 621 |
|
| 622 |
$load && (wp_doing_ajax() ? $load() : add_action('load-'.wpjam_admin('page_hook'), $load)); |
| 623 |
|
| 624 |
$this->render = $render; |
| 625 |
$this->page_title = $object->title ?: $this->page_title; |
| 626 |
$this->summary = $this->summary ?: $object->get_arg('summary'); |
| 627 |
|
| 628 |
$object->query_args && wpjam_admin('query_data', $this->get_param($object->query_args)); |
| 629 |
}else{ |
| 630 |
$function = $this->function ?: wpjam_get_filter_name($this->name, 'page'); |
| 631 |
$this->render = is_callable($function) ? fn()=> wpjam_chart('render').wpjam_ob($function) : $this->throw('页面函数'.'「'.$function.'」未定义。'); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
private function page_object($type, $name, $step=''){ |
| 636 |
$object = null; |
| 637 |
$args = $this->$type; |
| 638 |
|
| 639 |
if($step == 'preprocess'){ |
| 640 |
if($type == 'option'){ |
| 641 |
$object = WPJAM_Option_Setting::get($name); |
| 642 |
$object = $object ? $object->get_current() : null; |
| 643 |
}elseif($type == 'form'){ |
| 644 |
$object = wpjam_get_page_action($name); |
| 645 |
} |
| 646 |
|
| 647 |
if($object){ |
| 648 |
$args = $object->to_array(); |
| 649 |
}else{ |
| 650 |
$model = in_array($type, ['list_table', 'form']) ? ($args ? (is_string($args) ? $args : '') : $this->model) : ''; |
| 651 |
$args = ($cb = wpjam_callback([$model, 'get_'.$type])) ? $cb($this) : $args; |
| 652 |
$args = wpjam_is_assoc_array($args) ? $args+$this->pick(['model']) : $args; |
| 653 |
|
| 654 |
$args && ($args = $this->$type = maybe_callback($args, $this)); |
| 655 |
} |
| 656 |
|
| 657 |
if(is_array($args)){ |
| 658 |
!empty($args['meta_type']) && wpjam_admin('meta_type', $args['meta_type']); |
| 659 |
|
| 660 |
$this->update_args(wpjam_admin('data_type', $args)); |
| 661 |
} |
| 662 |
|
| 663 |
return $object; |
| 664 |
} |
| 665 |
|
| 666 |
if(!$args){ |
| 667 |
array_any(['form'=>['callback'], 'option'=>['sections', 'fields'], 'dashboard'=>['widgets'], 'list_table'=>['model']][$type], fn($k)=> $this->$k) || $this->throw($type.'「'.$name.'」未定义。'); |
| 668 |
|
| 669 |
$args = $type == 'list_table' ? wpjam_except($this->to_array(), 'defaults') : $this->to_array(); |
| 670 |
} |
| 671 |
|
| 672 |
if($type == 'option'){ |
| 673 |
return wpjam_register_option($name, $args)->get_current(); |
| 674 |
}elseif($type == 'form'){ |
| 675 |
return wpjam_register_page_action($name, $args); |
| 676 |
}elseif($type == 'dashboard'){ |
| 677 |
return wpjam_dashboard(['name'=>$name]+$args); |
| 678 |
} |
| 679 |
|
| 680 |
$model = $args['model'] ?? ''; |
| 681 |
|
| 682 |
(!$model || !class_exists($model)) && $this->throw('List Table Model'.'「'.$model.'」未定义。'); |
| 683 |
|
| 684 |
$this->defaults($args['defaults'] ?? []); |
| 685 |
|
| 686 |
wpjam_map(['admin_head', 'admin_footer'], fn($v)=> wpjam_hook($v, wpjam_callback([$model, $v]))); |
| 687 |
|
| 688 |
return new WPJAM_List_Table($args+array_filter([ |
| 689 |
'layout' => 'table', |
| 690 |
'name' => $name, |
| 691 |
'singular' => $name, |
| 692 |
'plural' => $name.'s', |
| 693 |
'capability'=> $this->capability ?: 'manage_options', |
| 694 |
'sortable' => $this->sortable, |
| 695 |
'data_type' => 'model', |
| 696 |
'per_page' => 50 |
| 697 |
])); |
| 698 |
} |
| 699 |
|
| 700 |
public function render(){ |
| 701 |
$sum = maybe_callback($this->summary, $this->menu_slug, $this->tab_slug ?: ''); |
| 702 |
$sum = !$sum || is_array($sum) ? '' : (is_file($sum) ? wpjam_get_file_data($sum, 'summary') : $sum); |
| 703 |
$tag = wpjam_tag('h1', ['wp-heading-inline'], ($this->page_title ?? $this->title))->after([ |
| 704 |
['hr', ['wp-header-end']], |
| 705 |
$sum ? ['p', ['summary'], $sum] : '', |
| 706 |
...($this->type == 'tab' ? [ |
| 707 |
wpjam_ob(wpjam_get_filter_name($this->menu_slug, 'page')) ?: '', // 所有 Tab 页面都执行 |
| 708 |
wpjam_tag('nav', ['nav-tab-wrapper', 'wp-clearfix'])->append(array_map(fn($tab)=> ['a', ['class'=>['nav-tab', $tab->is_current() ? 'nav-tab-active' : ''], 'href'=>$tab->admin_url], ($tab->tab_title ?: $tab->title)], $this->tabs)) |
| 709 |
] : []), |
| 710 |
wpjam_call($this->render, $this) |
| 711 |
]); |
| 712 |
|
| 713 |
return $this->tab_slug ? $tag->tag('h2') : wpjam_echo($tag->wrap('div', ['wrap'])); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
class WPJAM_Page_Action extends WPJAM_Args{ |
| 718 |
public function __invoke($type=''){ |
| 719 |
if($type == 'form'){ |
| 720 |
if(!($title = $_POST['page_title'] ?? '')){ |
| 721 |
if($key = array_find(['page_title', 'button_text', 'submit_text'], fn($k)=> $this->$k && !is_array($this->$k))){ |
| 722 |
$title = $this->$key; |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
return $this->pick(['width', 'modal_id'])+['type'=>'form', 'form'=>$this->get_form(), 'page_title'=>$title]; |
| 727 |
} |
| 728 |
|
| 729 |
$this->is_allowed($type) || wp_die('access_denied'); |
| 730 |
|
| 731 |
$args = $this->validate ? [wpjam_params('data', $this->get_fields())] : []; |
| 732 |
$submit = $type == 'submit' ? (($_POST['submit'] ?? '') ?: $this->name) : ''; |
| 733 |
$button = $submit ? wpjam_button($this, $submit) : []; |
| 734 |
$type = $button['response'] ?? ($this->response ?? $this->name); |
| 735 |
$cb = $button['callback'] ?? $this->callback; |
| 736 |
$result = ($cb ? wpjam_try($cb, ...[...$args, $submit ?: $this->name]) : null) ?? wpjam_throw('invalid_callback'); |
| 737 |
|
| 738 |
if(is_array($result)){ |
| 739 |
if(empty($result['errcode'])){ |
| 740 |
$result['notice'] ??= wpjam_pull($result, 'errmsg') ?: ''; |
| 741 |
} |
| 742 |
}else{ |
| 743 |
$result = is_string($result) ? [($type == 'redirect' ? 'url' : 'data')=>$result] : []; |
| 744 |
} |
| 745 |
|
| 746 |
$result += ['type'=>$type]+$this->pick(['dismiss']); |
| 747 |
$result += $result['type'] == 'redirect' ? $this->pick(['target']) : []; |
| 748 |
|
| 749 |
return apply_filters('wpjam_ajax_response', $result); |
| 750 |
} |
| 751 |
|
| 752 |
public function is_allowed($type=''){ |
| 753 |
return wpjam_can($this->capability ?? ($type ? 'manage_options' : ''), $this->name); |
| 754 |
} |
| 755 |
|
| 756 |
public function render(){ |
| 757 |
return wpjam_trap([$this, 'get_form'], 'die'); |
| 758 |
} |
| 759 |
|
| 760 |
public function get_button($args=[]){ |
| 761 |
if($this->is_allowed()){ |
| 762 |
$text = $this->update_args(wpjam_except($args, 'data'))->button_text ?? '保存'; |
| 763 |
|
| 764 |
return wpjam_tag(($this->tag ?: 'a'), $this->pick(['style', 'class'])+[ |
| 765 |
'class' => 'button-primary large', |
| 766 |
'title' => $this->page_title ?: $text, |
| 767 |
'data' => $this->get_attr($args)+$this->pick(['direct', 'confirm'])+['title'=>$this->page_title ?: $this->button_text] |
| 768 |
], $text)->add_class('wpjam-button'); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
public function get_form(){ |
| 773 |
if($this->is_allowed()){ |
| 774 |
return $this->get_fields()->wrap('form', [ |
| 775 |
'id' => $this->form_id ?: 'wpjam_form', |
| 776 |
'button' => wpjam_button($this), |
| 777 |
'data' => $this->get_attr() |
| 778 |
]); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
public function get_attr($args=[]){ |
| 783 |
return [ |
| 784 |
'action' => $this->name, |
| 785 |
'nonce' => wp_create_nonce($this->name), |
| 786 |
'data' => wp_parse_args($args['data'] ?? [], ($this->data ?: [])), |
| 787 |
]; |
| 788 |
} |
| 789 |
|
| 790 |
public function get_fields(){ |
| 791 |
$fields = wpjam_try('maybe_callback', $this->fields, $this->name) ?: []; |
| 792 |
$data = is_callable($this->data_callback) ? wpjam_try($this->data_callback, $this->name, $fields) : []; |
| 793 |
|
| 794 |
return wpjam_fields($fields, wpjam_merge($this->args, ['data'=>$data ?: []])); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
class WPJAM_Chart extends WPJAM_Args{ |
| 799 |
public function validate($data){ |
| 800 |
$fields = wpjam_fields($this->get_fields(['data'=>$data])); |
| 801 |
|
| 802 |
return $fields->validate($data+$fields->get_defaults()); |
| 803 |
} |
| 804 |
|
| 805 |
public function render($wrap=true){ |
| 806 |
if(!$this->show_form){ |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
$fields = is_array($wrap) ? $this->get_fields() : apply_filters('wpjam_chart_fields', $this->get_fields()); |
| 811 |
$fields += $wrap === true ? ['chart_button'=>['type'=>'submit', 'value'=>'显示', 'class'=>'button button-secondary']] : []; |
| 812 |
$fields = wpjam_fields($fields)->render(is_array($wrap) ? $wrap : ['fields_type'=>'']); |
| 813 |
|
| 814 |
return $wrap === true //.($this->show_compare && $current != -1 ? '&type='.$current : ''); |
| 815 |
? $fields->wrap('form', ['method'=>'POST', 'action'=>$GLOBALS['current_admin_url'], 'id'=>'chart_form', 'class'=>'chart-form']) |
| 816 |
: $fields; |
| 817 |
} |
| 818 |
|
| 819 |
public function get_value($key, $args=[]){ |
| 820 |
if(is_array($key)){ |
| 821 |
return wpjam_fill($key, fn($k)=> $this->get_value($k, $args)); |
| 822 |
} |
| 823 |
|
| 824 |
if(str_contains($key, 'timestamp')){ |
| 825 |
return wpjam_strtotime($this->get_value(str_replace('timestamp', 'date', $key)).' '.(str_starts_with($key, 'end_') ? '23:59:59' : '00:00:00')); |
| 826 |
} |
| 827 |
|
| 828 |
if($value = ($args['data'][$key] ?? '') ?: wpjam_param($key, $args['method'] ?? $this->method)){ |
| 829 |
wpjam_set_cookie($key, $value, HOUR_IN_SECONDS); |
| 830 |
} |
| 831 |
|
| 832 |
if($value = $_COOKIE[$key] ?? ''){ |
| 833 |
return $value; |
| 834 |
} |
| 835 |
|
| 836 |
if(in_array($key, ['date_format', 'date_type'])){ |
| 837 |
return '%Y-%m-%d'; |
| 838 |
}elseif($key == 'compare'){ |
| 839 |
return 0; |
| 840 |
} |
| 841 |
|
| 842 |
if(in_array($key, ['date', 'start_date', 'end_date'])){ |
| 843 |
$ts = time() - (DAY_IN_SECONDS*['date'=>1, 'start_date'=>30, 'end_date'=>0][$key]); |
| 844 |
}elseif(in_array($key, ['start_date_2', 'end_date_2'])){ |
| 845 |
$ts = $this->get_value('start_timestamp') - DAY_IN_SECONDS; |
| 846 |
$ts -= $key == 'start_date_2' ? ($this->get_value('end_timestamp') - $this->get_value('start_timestamp')) : 0; |
| 847 |
} |
| 848 |
|
| 849 |
return isset($ts) ? wpjam_date('Y-m-d', $ts) : null; |
| 850 |
} |
| 851 |
|
| 852 |
public function get_fields($args=[]){ |
| 853 |
$start = $args['show_start_date'] ?? $this->show_start_date; |
| 854 |
$comp = $this->show_compare && $start; |
| 855 |
$fields = ['date_set'=>['title'=>($args['title'] ?? ($comp ? '日期' : '')), 'sep'=>' ', 'fields'=>($start ? [ |
| 856 |
'start_date' => ['type'=>'date'], |
| 857 |
'date_view' => ['type'=>'view', 'value'=>'-'], |
| 858 |
'end_date' => ['type'=>'date'] |
| 859 |
] : [ |
| 860 |
'prev_day' => ['type'=>'button', 'value'=>'‹', 'class'=>'button prev-day'], |
| 861 |
'date' => ['type'=>'date'], |
| 862 |
'next_day' => ['type'=>'button', 'value'=>'›', 'class'=>'button next-day'] |
| 863 |
])]+($start ? ['validate_callback'=>function($data){ |
| 864 |
$span = $this->max_date_span; |
| 865 |
$diff = strtotime($data['end_date']) - strtotime($data['start_date']); |
| 866 |
|
| 867 |
if($msg = $diff < 0 ? '开始日期不能大于结束日期' : ($span && $diff > DAY_IN_SECONDS * $span ? '时长不能� |
| 868 |
过'.$span.'天' : '')){ |
| 869 |
wpjam_throw('error', $msg); |
| 870 |
} |
| 871 |
}] : [])]; |
| 872 |
|
| 873 |
$fields += $this->show_date_type ? ['date_format'=>['type'=>'select', 'options'=>['%Y-%m'=>'按月', '%Y-%m-%d'=>'按天', '%Y-%m-%d %H:00'=>'按小时', '%Y-%m-%d %H:%i'=>'按分钟']]] : []; // '%Y%U'=>'按周' |
| 874 |
|
| 875 |
$fields += $comp && wpjam_param('type', ['default'=>-1]) !=-1 ? ['compare_set'=>['before'=>'对比:', 'sep'=>' ', 'fields'=>[ |
| 876 |
'start_date_2' => ['type'=>'date'], |
| 877 |
'date_view_2' => ['type'=>'view', 'value'=>'-'], |
| 878 |
'end_date_2' => ['type'=>'date'], |
| 879 |
'compare' => ['type'=>'checkbox'], |
| 880 |
]]] : []; |
| 881 |
|
| 882 |
return wpjam_map($fields, fn($v, $k)=> $v+(in_array($v['type'], ['date', 'checkbox']) ? [ |
| 883 |
'value' => $this->get_value($k, $args), |
| 884 |
'value_callback' => fn($k)=> $this->get_value($k, $args) |
| 885 |
] : []), ['deep'=>'fields']); |
| 886 |
} |
| 887 |
|
| 888 |
public static function line($data, $args=[], $type='Line'){ |
| 889 |
foreach(($args['labels'] ?? []) as $k => $v){ |
| 890 |
if(is_array($v)){ |
| 891 |
$columns[$k] = $v['label']; |
| 892 |
|
| 893 |
if($v['show_in_chart'] ?? true){ |
| 894 |
$labels[$k] = $v['label']; |
| 895 |
} |
| 896 |
|
| 897 |
if(!empty($v['callback'])){ |
| 898 |
$cbs[$k] = $v['callback']; |
| 899 |
} |
| 900 |
}else{ |
| 901 |
$columns[$k] = $labels[$k] = $v; |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
$xkey = $args['day_key'] ?? 'day'; |
| 906 |
$ykeys = array_keys($labels); |
| 907 |
$data += ($args['show_table'] ?? true) && $data ? ['total' => array_fill_keys($ykeys, 0)] : []; |
| 908 |
|
| 909 |
foreach($data as $day => &$item){ |
| 910 |
$item = (array)$item; |
| 911 |
$item = array_merge($item, isset($cbs) ? array_map(fn($cb)=> $cb($item), $cbs) : []); |
| 912 |
|
| 913 |
if($day !== 'total'){ |
| 914 |
$item[$xkey] ??= $day; |
| 915 |
|
| 916 |
if(isset($data['total'])){ |
| 917 |
$data['total'] = wpjam_map($data['total'], fn($v, $k)=> $v+(is_numeric($item[$k] ?? null) ? $item[$k] : 0)); |
| 918 |
$rows[$item[$xkey]] = $item; |
| 919 |
} |
| 920 |
|
| 921 |
$item = wpjam_pull($item, [...$ykeys, $xkey]); |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
$total = wpjam_pull($data, 'total'); |
| 926 |
$tag = wpjam_tag(); |
| 927 |
|
| 928 |
if($args['show_chart'] ?? true){ |
| 929 |
$tag->append(wpjam_tag('div', [ |
| 930 |
'id' => $args['chart_id'] ?? 'daily-chart', |
| 931 |
'data' => ['chart'=>['type'=>$type, 'options'=>['data'=>$data, 'xkey'=>$xkey, 'ykeys'=>$ykeys, 'labels'=>array_values($labels)]]] |
| 932 |
])); |
| 933 |
} |
| 934 |
|
| 935 |
if($total && $data){ |
| 936 |
$labels = ($args['day_labels'] ?? [])+['sum'=>'累加', 'avg'=>'平均']; |
| 937 |
$columns = [$xkey => $args['day_label'] ?? '时间']+$columns; |
| 938 |
$th = wpjam_tag('tr')->append(wpjam_map($columns, fn($col, $id)=> wpjam_tag('th', ['scope'=>'col', 'id'=>$id], $col))); |
| 939 |
$rows += ($args['show_sum'] ?? true) ? ['sum'=>$total] : []; |
| 940 |
$rows += ($args['show_avg'] ?? true) ? ['avg'=>array_map(fn($v)=> is_numeric($v) ? round($v/count($data)) : '', $total)] : []; |
| 941 |
$rows = wpjam_map($rows, fn($row, $k)=> wpjam_tag('tr')->append(wpjam_map($columns, fn($col, $id)=> wpjam_tag('td', [ |
| 942 |
'data' => ['colname'=>$col], |
| 943 |
'class' => ['column-'.$id, ($id == $xkey ? 'column-primary' : '')] |
| 944 |
])->append($id == $xkey ? [($labels[$k] ?? $k), wpjam_tag('button', ['class'=>'toggle-row'])] : ($row[$id] ?? ''))))); |
| 945 |
|
| 946 |
$tag->append(wpjam_tag('table', ['class'=>'wp-list-table widefat striped'])->append([ |
| 947 |
wpjam_tag('thead')->append($th), |
| 948 |
wpjam_tag('tbody')->append($rows), |
| 949 |
wpjam_tag('tfoot')->append($th) |
| 950 |
])); |
| 951 |
} |
| 952 |
|
| 953 |
return $tag; |
| 954 |
} |
| 955 |
|
| 956 |
public static function bar($data, $args=[]){ |
| 957 |
return self::line($data, $args, 'Bar'); |
| 958 |
} |
| 959 |
|
| 960 |
public static function donut($data, $args=[]){ |
| 961 |
$tag = wpjam_tag('div', ['class'=>'donut-chart-wrap']); |
| 962 |
$data = wpjam_array($data, fn($k, $v)=> [null, [ |
| 963 |
'label' => !empty($v['label']) ? ($args['labels'][$v['label']] ?? $v['label']) : '/', |
| 964 |
'value' => $v['count'] |
| 965 |
]]); |
| 966 |
|
| 967 |
if($args['show_chart'] ?? true){ |
| 968 |
$tag->append('div', [ |
| 969 |
'id' => $args['chart_id'] ?? 'chart_'.wp_generate_password(6, false, false), |
| 970 |
'data' => ['chart'=>['type'=>'Donut', 'options'=>['data'=>$data]]] |
| 971 |
]); |
| 972 |
} |
| 973 |
|
| 974 |
if(($args['show_table'] ?? true) && $data){ |
| 975 |
$columns = (!empty($args['show_line_num']) ? ['no'=>'排名'] : [])+['label'=>$args['title'] ?? '名称', 'value'=>'数量']; |
| 976 |
|
| 977 |
if($total = $args['total'] ?? false){ |
| 978 |
$total = $total === true ? array_sum(array_column($data, 'value')) : $total; |
| 979 |
$data += ['total'=>['label'=>'所有', 'value'=>$total]]; |
| 980 |
$columns += ['rate'=>'比例']; |
| 981 |
} |
| 982 |
|
| 983 |
$tag->append(wpjam_tag('table', ['wp-list-table', 'widefat', 'striped'])->append([ |
| 984 |
wpjam_tag('thead')->append(wpjam_tag('tr')->append(wpjam_map($columns, fn($col, $id)=> wpjam_tag('th', ['scope'=>'col', 'id'=>$id], $col)))), |
| 985 |
wpjam_tag('tbody')->append(wpjam_map($data, fn($item, $k)=> wpjam_tag('tr')->append(wpjam_map($columns, fn($col, $id)=> wpjam_tag('td', ['data'=>['colname'=>$col]], $item[$id] ?? ($id == 'no' ? ($k === 'total' ? '' : $k+1) : round($item['value']/$total*100, 2).'%')))))) |
| 986 |
])); |
| 987 |
} |
| 988 |
|
| 989 |
return $tag; |
| 990 |
} |
| 991 |
|
| 992 |
public static function create($args=[]){ |
| 993 |
$offset = (int)get_option('gmt_offset'); |
| 994 |
$offset = $offset >= 0 ? '+'.$offset.':00' : $offset.':00'; |
| 995 |
|
| 996 |
$GLOBALS['wpdb']->query("SET time_zone = '{$offset}';"); |
| 997 |
|
| 998 |
wpjam_style('morris', ['cdn'=>true, 'src'=>'/morris.js/0.5.1/morris.css']); |
| 999 |
wpjam_script('raphael', ['cdn'=>true, 'src'=>'/raphael/2.3.0/raphael.min.js']); |
| 1000 |
wpjam_script('morris', ['cdn'=>true, 'src'=>'/morris.js/0.5.1/morris.min.js']); |
| 1001 |
|
| 1002 |
return new self($args+[ |
| 1003 |
'method' => 'POST', |
| 1004 |
'show_form' => true, |
| 1005 |
'show_start_date' => true, |
| 1006 |
'show_date_type' => false, |
| 1007 |
'show_compare' => false |
| 1008 |
]); |
| 1009 |
} |
| 1010 |
} |