Changeset 3385082
- Timestamp:
- 10/27/2025 09:26:46 AM (5 months ago)
- Location:
- wpjam-basic/trunk
- Files:
-
- 8 edited
-
includes/class-wpjam-admin.php (modified) (1 diff)
-
includes/class-wpjam-api.php (modified) (12 diffs)
-
includes/class-wpjam-args.php (modified) (7 diffs)
-
public/wpjam-compat.php (modified) (1 diff)
-
public/wpjam-functions.php (modified) (6 diffs)
-
public/wpjam-route.php (modified) (10 diffs)
-
public/wpjam-utils.php (modified) (3 diffs)
-
wpjam-basic.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
wpjam-basic/trunk/includes/class-wpjam-admin.php
r3383874 r3385082 703 703 }else{ 704 704 $context = use_block_editor_for_post_type($post_type) ? 'normal' : 'wpjam'; 705 706 705 $context == 'wpjam' && add_action(($post_type == 'page' ? 'edit_page_form' : 'edit_form_advanced'), [self::class, 'on_edit_form'], 99); 707 706 } 708 707 709 708 foreach(wpjam_get_post_options($post_type, ['list_table'=>false]) as $object){ 710 if( $object->meta_box_cb|| ($object->fields = $object->get_fields($post_id, ''))){709 if(($cb = $object->meta_box_cb) || ($object->fields = $object->get_fields($post_id, ''))){ 711 710 if($method == 'callback'){ 712 711 wpjam_trap([$object, 'callback'], $post_id, 'die'); 713 712 }else{ 714 add_meta_box($object->name, $object->title, [$object, 'render'], $post_type, ($object->context ?: $context), $object->priority); 713 $id = $GLOBALS['current_screen']->action == 'add' ? false : $post_id; 714 $args = ['fields_type'=>$object->context === 'side' ? 'list' : 'table']; 715 716 add_meta_box($object->name, $object->title, $cb ?: fn()=> $object->render($id, $args), $post_type, ($object->context ?: $context), $object->priority); 715 717 } 716 718 } -
wpjam-basic/trunk/includes/class-wpjam-api.php
r3385081 r3385082 42 42 43 43 public function set($field, $key, ...$args){ 44 $exists = $this->get($field); 45 46 $this->data[$field] = is_array($key) ? (($args && $args[0]) ? $key : array_merge($exists, $key)) : wpjam_set($exists, $key, ...$args); 44 $this->data[$field] = is_array($key) ? array_merge(($args && $args[0]) ? $this->get($field) : [], $key) : wpjam_set($this->get($field), $key, ...$args); 47 45 48 46 return is_array($key) ? $key : $args[0]; … … 58 56 59 57 public function get($field, ...$args){ 60 return wpjam_get(($this->data[$field] ?? []), ($args ? $args[0] : null));58 return $args ? wpjam_get($this->get($field), ...$args) : ($this->data[$field] ?? []); 61 59 } 62 60 … … 76 74 [$module, $action] = [$module['module'], $module['action'] ?? '']; 77 75 78 remove_action('template_redirect', 'redirect_canonical'); 79 } 80 81 $item = $this->get('route', $module); 82 83 if($item){ 76 remove_action('template_redirect', 'redirect_canonical'); 77 } 78 79 if($item = $this->get('route', $module)){ 84 80 !empty($item['query_var']) && $GLOBALS['wp']->set_query_var($module, $action); 85 81 !empty($item['callback']) && wpjam_call($item['callback'], $action, $module); … … 161 157 } 162 158 159 public static function die_handler($msg, $title='', $args=[]){ 160 wpjam_if_error($msg, 'send'); 161 162 $code = $args['code'] ?? ''; 163 $data = $code && $title ? ['modal'=>['title'=>$title, 'content'=>$msg]] : []; 164 $code = $code ?: $title; 165 $item = !$code && is_string($msg) ? wpjam_get_error_setting($msg) : []; 166 $item = $item ?: ['errcode'=>($code ?: 'error'), 'errmsg'=>$msg]+$data; 167 168 wpjam_send_json($item); 169 } 170 163 171 public static function redirect($name){ 164 172 header('X-Content-Type-Options: nosniff'); … … 171 179 } 172 180 173 add_filter('wp_die_'.(array_find(['jsonp_', 'json_'], fn($v)=> call_user_func('wp_is_'.$v.'request')) ?: '').'handler', fn()=> [ 'WPJAM_Error', 'wp_die_handler']);181 add_filter('wp_die_'.(array_find(['jsonp_', 'json_'], fn($v)=> call_user_func('wp_is_'.$v.'request')) ?: '').'handler', fn()=> [self::class, 'die_handler']); 174 182 175 183 if(!try_remove_prefix($name, 'mag.')){ … … 220 228 } 221 229 230 class WPJAM_AJAX extends WPJAM_Args{ 231 public function __invoke(){ 232 add_filter('wp_die_ajax_handler', fn()=> ['WPJAM_JSON', 'die_handler']); 233 234 $cb = $this->callback; 235 236 (!$cb || !is_callable($cb)) && wp_die('invalid_callback'); 237 238 if($this->admin){ 239 $data = $this->fields ? wpjam_fields($this->fields)->get_parameter('POST') : wpjam_get_post_parameter(); 240 $verify = wpjam_get($data, 'action_type') !== 'form'; 241 }else{ 242 $data = array_merge(wpjam_get_data_parameter(), wpjam_except(wpjam_get_post_parameter(), ['action', 'defaults', 'data', '_ajax_nonce'])); 243 $data = array_merge($data, wpjam_fields($this->fields)->validate($data, 'parameter')); 244 $verify = $this->verify !== false; 245 } 246 247 $action = $verify ? $this->parse_nonce_action($this->name, $data) : ''; 248 $action && !check_ajax_referer($action, false, false) && wpjam_send_json(['errcode'=>'invalid_nonce', 'errmsg'=>'验证失败,请刷新重试。']); 249 250 $this->allow && !wpjam_call($this->allow, $data) && wp_die('access_denied'); 251 252 return $cb($data, $this->name); 253 } 254 255 public static function parse_nonce_action($name, $data=[]){ 256 return ($nonce = wpjam('ajax', $name.'[nonce_action]')) ? $nonce($data) : (wpjam('ajax', $name.'[admin]') ? '' : $name.wpjam_join(':', wpjam_pick($data, wpjam('ajax', $name.'[nonce_keys]') ?: []))); 257 } 258 259 public static function parse_submit_button($button, $submit=null){ 260 $cb = fn($item)=> (is_array($item) ? $item : ['text'=>$item])+['class'=>'primary']; 261 262 if($submit){ 263 return isset($button[$submit]) ? $cb($button[$submit]) : wp_die('无效的提交按钮'); 264 } 265 266 foreach(array_filter($button) as $key => $item){ 267 $item = $cb($item); 268 $parsed[] = get_submit_button($item['text'], $item['class'], $key, false); 269 } 270 271 return isset($parsed) ? wpjam_tag('p', ['submit'], implode($parsed)) : wpjam_tag(); 272 } 273 274 public static function get_attr($name, $data=[]){ 275 if(wpjam('ajax', $name)){ 276 return ['action'=>$name, 'data'=>$data]+(($action = self::parse_nonce_action($name, $data)) ? ['nonce'=>wp_create_nonce($action)] : []); 277 } 278 } 279 280 public static function create($name, $args){ 281 if(!is_admin() && !wpjam('ajax')){ 282 wpjam_script('wpjam-ajax', [ 283 'for' => 'wp, login', 284 'src' => wpjam_url(dirname(__DIR__).'/static/ajax.js'), 285 'deps' => ['jquery'], 286 'data' => 'var ajaxurl = "'.admin_url('admin-ajax.php').'";', 287 'position' => 'before', 288 'priority' => 1 289 ]); 290 291 if(!is_login()){ 292 add_filter('script_loader_tag', fn($tag, $handle)=> $handle == 'wpjam-ajax' && current_theme_supports('script', $handle) ? '' : $tag, 10, 2); 293 } 294 } 295 296 if(wp_doing_ajax() && wpjam_get($_REQUEST, 'action') == $name && (is_user_logged_in() || !empty($args['nopriv']))){ 297 add_action('wp_ajax_'.(is_user_logged_in() ? '' : 'nopriv_').$name, fn()=> wpjam_send_json(wpjam_catch(new static(['name'=>$name]+$args)))); 298 } 299 300 return wpjam('ajax', $name, $args); 301 } 302 } 303 222 304 class WPJAM_Platforms{ 223 305 private $platforms = []; 224 private $cache = [];225 306 226 307 protected function __construct($platforms){ … … 236 317 $key = 'page_key'.$suffix; 237 318 $paths = WPJAM_Path::get_by($args); 238 $fields_key = md5(serialize($prepend+['suffix'=>$suffix, 'strict'=>$strict, 'page_keys'=>array_keys($paths)])); 239 $fields = $this->cache[$fields_key] ?? []; 319 $platforms = $this->platforms; 320 $fields_key = md5(serialize($prepend+['suffix'=>$suffix, 'strict'=>$strict, 'platforms'=>implode('-', array_keys($platforms)), 'page_keys'=>array_keys($paths)])); 321 $fields = wpjam('path_fields', $fields_key); 240 322 241 323 if(!$fields){ 242 $backup = count($ this->platforms) > 1 && !$strict;324 $backup = count($platforms) > 1 && !$strict; 243 325 $pks = [$key=>['OR', $suffix]]+($backup ? [$key.'_backup'=>['AND', $suffix.'_backup']] : []); 244 326 $fields = array_fill_keys(array_keys($pks), ['tabbar'=>['title'=>'菜单栏/常用', 'options'=>($strict ? [] : ['none'=>'只展示不跳转'])]]+wpjam('path_group')+['others'=>['title'=>'其他页面']])+($backup ? ['show_if'=>[]] : []); … … 250 332 251 333 foreach($pks as $pk => [$op, $fix]){ 252 if(wpjam_array($ this->platforms, fn($pf)=> $pf->has_path($name, $strict && $op == 'OR'), $op)){334 if(wpjam_array($platforms, fn($pf)=> $pf->has_path($name, $strict && $op == 'OR'), $op)){ 253 335 $i++; 254 336 255 337 $fields = wpjam_set($fields, $pk.'['.$group.'][options]['.$name.']', [ 256 338 'label' => $path->title, 257 'fields' => wpjam_array($path->get_fields($ this->platforms), fn($k, $v)=> [$k.$fix, wpjam_except($v, 'title')+$prepend])339 'fields' => wpjam_array($path->get_fields($platforms), fn($k, $v)=> [$k.$fix, wpjam_except($v, 'title')+$prepend]) 258 340 ]); 259 341 } … … 265 347 } 266 348 267 $this->cache[$fields_key] = $fields;349 wpjam('path_fields', $fields_key, $fields); 268 350 } 269 351 … … 1042 1124 1043 1125 public function load(){ 1044 $this->dir = maybe_callback($this->dir);1045 1046 if(!is_dir($ this->dir)){1126 $this->dir = $dir = maybe_callback($this->dir); 1127 1128 if(!is_dir($dir)){ 1047 1129 return; 1048 1130 } … … 1053 1135 $extends = array_keys(array_merge($this->get_option(), $this->get_option(true))); 1054 1136 }else{ 1055 $extends = array_diff(scandir($ this->dir), ['.', '..']);1137 $extends = array_diff(scandir($dir), ['.', '..']); 1056 1138 $plugins = get_option('active_plugins') ?: []; 1057 $extends = array_filter($extends, fn($v)=> !in_array($v.(is_dir($ this->dir.'/'.$v) ? '/'.$v : '').'.php', $plugins));1139 $extends = array_filter($extends, fn($v)=> !in_array($v.(is_dir($dir.'/'.$v) ? '/'.$v : '').'.php', $plugins)); 1058 1140 } 1059 1141 … … 1160 1242 } 1161 1243 1162 class WPJAM_Http{1163 public static function request($url, $args=[], $err=[]){1164 $args += ['body'=>[], 'headers'=>[], 'sslverify'=>false, 'stream'=>false];1165 $headers = &$args['headers'];1166 $headers = wpjam_array($headers, fn($k)=> strtolower($k));1167 $method = strtoupper(wpjam_pull($args, 'method', '')) ?: ($args['body'] ? 'POST' : 'GET');1168 1169 if($method == 'GET'){1170 $response = wp_remote_get($url, $args);1171 }elseif($method == 'FILE'){1172 $response = (new WP_Http_Curl())->request($url, $args+[1173 'method' => $args['body'] ? 'POST' : 'GET',1174 'sslcertificates' => ABSPATH.WPINC.'/certificates/ca-bundle.crt',1175 'user-agent' => $headers['user-agent'] ?? 'WordPress',1176 'decompress' => true,1177 ]);1178 }else{1179 if(str_contains(($headers['content-type'] ?? ''), 'application/json') || wpjam_at(wpjam_pull($args, ['json_encode', 'need_json_encode']), 0)){1180 if(is_array($args['body'])){1181 $args['body'] = wpjam_json_encode($args['body'] ?: new stdClass);1182 }1183 1184 if(empty($headers['content-type'])){1185 $headers['content-type'] = 'application/json';1186 }1187 }1188 1189 $response = wp_remote_request($url, $args+['method'=>$method]);1190 }1191 1192 if(is_wp_error($response)){1193 return self::trigger($response, $url, $args['body']);1194 }1195 1196 $code = $response['response']['code'] ?? 0;1197 1198 if($code && ($code < 200 || $code >= 300)){1199 return new WP_Error($code, '远程服务器错误:'.$code.' - '.$response['response']['message'].'-'.var_export($response['body'], true));1200 }1201 1202 $body = &$response['body'];1203 1204 if($body && !$args['stream']){1205 if(str_contains(wp_remote_retrieve_header($response, 'content-disposition'), 'attachment;')){1206 $body = wpjam_bits($body);1207 }elseif(wpjam_pull($args, 'json_decode') !== false && str_starts_with($body, '{') && str_ends_with($body, '}')){1208 $result = wpjam_json_decode($body);1209 1210 if(!is_wp_error($result)){1211 $err += [1212 'errcode' => 'errcode',1213 'errmsg' => 'errmsg',1214 'detail' => 'detail',1215 'success' => '0',1216 ];1217 1218 $code = wpjam_pull($result, $err['errcode']);1219 1220 if($code && $code != $err['success']){1221 $msg = wpjam_pull($result, $err['errmsg']);1222 $detail = wpjam_pull($result, $err['detail']);1223 $detail = is_null($detail) ? array_filter($result) : $detail;1224 1225 return self::trigger(new WP_Error($code, $msg, $detail), $url, $args['body']);1226 }1227 1228 $body = $result;1229 }1230 }1231 }1232 1233 return $response;1234 }1235 1236 private static function trigger($error, $url, $body){1237 $code = $error->get_error_code();1238 $msg = $error->get_error_message();1239 $detail = $error->get_error_data();1240 1241 if(apply_filters('wpjam_http_response_error_debug', true, $code, $msg)){1242 trigger_error($url."\n".$code.' : '.$msg."\n".($detail ? var_export($detail, true)."\n" : '').var_export($body, true));1243 }1244 1245 return $error;1246 }1247 }1248 1249 class WPJAM_Error{1250 public static function parse($data){1251 if(is_wp_error($data)){1252 $err = $data->get_error_data();1253 $data = ['errcode'=>$data->get_error_code(), 'errmsg'=>$data->get_error_message()]+($err ? (is_array($err) ? $err : ['errdata'=>$err]) : []);1254 }1255 1256 if(wpjam_is_assoc_array($data)){1257 $data += ['errcode'=>0];1258 $data = array_merge($data, $data['errcode'] ? array_filter(self::parse_setting($data['errcode'], $data['errmsg'] ?? '')) : []);1259 }1260 1261 return $data;1262 }1263 1264 public static function parse_message($code, $args=null){1265 $fn = fn($map, $code)=> $map[$code] ?? ucwords($code);1266 1267 if(try_remove_prefix($code, 'invalid_')){1268 if($code == 'parameter'){1269 return $args ? '无效的参数:'.$args[0].'。' : '参数错误。';1270 }elseif($code == 'callback'){1271 return '无效的回调函数'.($args ? ':'.$args[0] : '').'。';1272 }elseif($code == 'name'){1273 return $args ? $args[0].'不能为纯数字。' : '无效的名称';1274 }else{1275 return [1276 'code' => '验证码错误。',1277 'password' => '两次输入的密码不一致。'1278 ][$code] ?? '无效的'.$fn([1279 'id' => ' ID',1280 'post_type' => '文章类型',1281 'taxonomy' => '分类模式',1282 'post' => '文章',1283 'term' => '分类',1284 'user' => '用户',1285 'comment_type' => '评论类型',1286 'comment_id' => '评论 ID',1287 'comment' => '评论',1288 'type' => '类型',1289 'signup_type' => '登录方式',1290 'email' => '邮箱地址',1291 'data_type' => '数据类型',1292 'qrcode' => '二维码',1293 ], $code);1294 }1295 }elseif(try_remove_prefix($code, 'illegal_')){1296 return $fn([1297 'access_token' => 'Access Token ',1298 'refresh_token' => 'Refresh Token ',1299 'verify_code' => '验证码',1300 ], $code).'无效或已过期。';1301 }elseif(try_remove_suffix($code, '_required')){1302 return $args ? sprintf(($code == 'parameter' ? '参数%s' : '%s的值').'为空或无效。', ...$args) : '参数或者值无效';1303 }elseif(try_remove_suffix($code, '_occupied')){1304 return $fn([1305 'phone' => '手机号码',1306 'email' => '邮箱地址',1307 'nickname' => '昵称',1308 ], $code).'已被其他账号使用。';1309 }1310 1311 return '';1312 }1313 1314 public static function parse_setting($code, $args=null){1315 if($item = wpjam_get_setting('wpjam_errors', $code)){1316 $modal = array_all(['show_modal', 'modal.title', 'modal.content'], fn($k)=> wpjam_get($item, $k));1317 $item = ['errmsg'=>$item['errmsg']]+($modal ? ['modal'=>$item['modal']] : []);1318 }elseif($item = (!$args || is_array($args)) ? self::get_setting($code) : []){1319 $item['errmsg'] = maybe_closure($item['errmsg'], $args ?: []);1320 }1321 1322 if($item && $args && is_array($args)){1323 $item['errmsg'] = sprintf($item['errmsg'], ...$args);1324 }1325 1326 return $item ?: [];1327 }1328 1329 public static function get_setting(...$args){1330 return wpjam('error', ...$args);1331 }1332 1333 public static function add_setting($code, $errmsg, $modal=[]){1334 self::get_setting() || add_action('wp_error_added', [self::class, 'on_wp_error_added'], 10, 4);1335 1336 return wpjam('error', $code, compact('errmsg', 'modal'));1337 }1338 1339 public static function wp_die_handler($message, $title='', $args=[]){1340 $errmsg = wpjam_if_error($message, 'send');1341 $errcode = $args['code'] ?? '';1342 1343 if($errcode){1344 $detail = $title ? ['modal'=>['title'=>$title, 'content'=>$errmsg]] : [];1345 }elseif($title){1346 $errcode = $title;1347 }elseif(is_string($errmsg)){1348 if($item = self::parse_setting($errmsg)){1349 wpjam_send_json($item);1350 }1351 1352 if($parsed = self::parse_message($errmsg)){1353 [$errcode, $errmsg] = [$errmsg, $parsed];1354 }1355 }1356 1357 wpjam_send_json(['errcode'=>($errcode ?: 'error'), 'errmsg'=>$errmsg]+($detail ?? []));1358 }1359 1360 public static function on_wp_error_added($code, $message, $data, $error){1361 if($code && (!$message || is_array($message)) && count($error->get_error_messages($code)) <= 1){1362 $args = $message ?: [];1363 $item = self::parse_setting($code, $args);1364 $parsed = $item ? $item['errmsg'] : self::parse_message($code, $args);1365 $data = $item && !empty($item['modal']) ? array_merge((is_array($data) ? $data : []), ['modal'=>$item['modal']]) : $data;1366 1367 $error->remove($code);1368 $error->add($code, ($parsed ?: $code), $data);1369 }1370 }1371 }1372 1373 1244 class WPJAM_Exception extends Exception{ 1374 1245 private $errcode = ''; -
wpjam-basic/trunk/includes/class-wpjam-args.php
r3383874 r3385082 713 713 } 714 714 715 /**716 * @config orderby717 **/718 #[config('orderby')]719 715 class WPJAM_Meta_Type extends WPJAM_Register{ 720 716 public function __call($method, $args){ 721 if(try_remove_suffix($method, '_option')){ // get_option register_option unregister_option 722 $name = array_shift($args); 717 if(try_remove_suffix($method, '_option')){ 718 $name = $method == 'get' && is_array($args[0]) ? '' : array_shift($args); 719 $key = 'options['.$name.']'; 723 720 724 721 if($method == 'register'){ 725 $args = ['meta_type'=>$this->name]+array_shift($args);722 $args = array_shift($args); 726 723 727 724 if($this->name == 'post'){ … … 738 735 } 739 736 740 $args = [new WPJAM_Meta_Option($name, $args)]; 741 } 742 743 $args = [$this->name.':'.$name, ...$args]; 744 $cb = ['WPJAM_Meta_Option', $method]; 737 return $this->update_arg($key, new WPJAM_Meta_Option(['name'=>$name, 'meta_type'=>$this->name]+$args)); 738 }elseif($method == 'unregister'){ 739 return $this->delete_arg($key); 740 } 741 742 if($name){ 743 return $this->get_arg($key); 744 } 745 746 $args = $args[0]; 747 $cb = fn($v)=> ['value'=>$v, 'if_null'=>true, 'callable'=>true]; 748 749 if($this->name == 'post'){ 750 if(isset($args['post_type'])){ 751 $object = wpjam_get_post_type_object($args['post_type']); 752 $object && $object->register_option(); 753 754 $args['post_type'] = $cb($args['post_type']); 755 } 756 }elseif($this->name == 'term'){ 757 if(isset($args['taxonomy'])){ 758 $object = wpjam_get_taxonomy_object($args['taxonomy']); 759 $object && $object->register_option(); 760 761 $args['taxonomy'] = $cb($args['taxonomy']); 762 } 763 764 if(isset($args['action'])){ 765 $args['action'] = $cb($args['action']); 766 } 767 } 768 769 if(isset($args['list_table'])){ 770 $args['title'] = true; 771 $args['list_table'] = $args['list_table'] ? true : ['compare'=>'!==', 'value'=>'only']; 772 } 773 774 return wpjam_sort(wpjam_filter($this->get_arg($key), $args), 'order', 'DESC', 10); 745 775 }elseif(in_array($method, ['get_data', 'add_data', 'update_data', 'delete_data', 'data_exists'])){ 746 776 $args = [$this->name, ...$args]; … … 791 821 } 792 822 793 public function get_options($args=[]){794 if($this->name == 'post'){795 if(isset($args['post_type'])){796 $object = wpjam_get_post_type_object($args['post_type']);797 $object && $object->register_option();798 799 $args['post_type'] = ['value'=>$args['post_type'], 'if_null'=>true, 'callable'=>true];800 }801 }elseif($this->name == 'term'){802 if(isset($args['taxonomy'])){803 $object = wpjam_get_taxonomy_object($args['taxonomy']);804 $object && $object->register_option();805 806 $args['taxonomy'] = ['value'=>$args['taxonomy'], 'if_null'=>true, 'callable'=>true];807 }808 809 if(isset($args['action'])){810 $args['action'] = ['value'=>$args['action'], 'if_null'=>true, 'callable'=>true];811 }812 }813 814 if(isset($args['list_table'])){815 $args['title'] = true;816 $args['list_table'] = $args['list_table'] ? true : ['compare'=>'!==', 'value'=>'only'];817 }818 819 return wpjam_array(WPJAM_Meta_Option::get_by(['meta_type'=>$this->name]+$args), fn($k, $v)=> $v->name);820 }821 822 823 public function register_actions($args=[]){ 823 foreach($this->get_option s(['list_table'=>true]+$args) as $v){824 wpjam_register_list_table_action( $v->action_name, $v->get_args()+[825 'meta_type' => $ v->name,824 foreach($this->get_option(['list_table'=>true]+$args) as $v){ 825 wpjam_register_list_table_action(($v->action_name ?: 'set_'.$v->name), $v->get_args()+[ 826 'meta_type' => $this->name, 826 827 'page_title' => '设置'.$v->title, 827 828 'submit_text' => '设置' … … 955 956 public function cleanup(){ 956 957 $wpdb = $GLOBALS['wpdb']; 957 958 if($this->object_key){ 959 $object_key = $this->object_key; 960 $object_table = $wpdb->{$this->name.'s'}; 961 }else{ 962 $object_model = $this->object_model; 963 964 if(!$object_model || !is_callable([$object_model, 'get_table'])){ 958 $key = $this->object_key; 959 $table = $key ? $wpdb->{$this->name.'s'} : ''; 960 961 if(!$key){ 962 $model = $this->object_model; 963 964 if(!$model || !is_callable([$model, 'get_table'])){ 965 965 return; 966 966 } 967 967 968 $ object_table = $object_model::get_table();969 $ object_key = $object_model::get_primary_key();968 $table = $model::get_table(); 969 $key = $model::get_primary_key(); 970 970 } 971 971 … … 974 974 } 975 975 976 $mids = $wpdb->get_col("SELECT m.".$this->get_column('id')." FROM ".$this->get_table()." m LEFT JOIN ".$ object_table." t ON t.".$object_key." = m.".$this->get_column('object')." WHERE t.".$object_key." IS NULL") ?: [];976 $mids = $wpdb->get_col("SELECT m.".$this->get_column('id')." FROM ".$this->get_table()." m LEFT JOIN ".$table." t ON t.".$key." = m.".$this->get_column('object')." WHERE t.".$key." IS NULL") ?: []; 977 977 978 978 array_walk($mids, [$this, 'delete_by_mid']); … … 980 980 981 981 public function create_table(){ 982 $table = $this->get_table(); 983 984 if($GLOBALS['wpdb']->get_var("show tables like '{$table}'") != $table){ 982 if(($table = $this->get_table()) != $GLOBALS['wpdb']->get_var("show tables like '{$table}'")){ 985 983 $column = $this->name.'_id'; 986 984 … … 999 997 public static function get_defaults(){ 1000 998 return array_merge([ 1001 'post' => ['o rder'=>50, 'object_model'=>'WPJAM_Post', 'object_column'=>'title', 'object_key'=>'ID'],1002 'term' => ['o rder'=>40, 'object_model'=>'WPJAM_Term', 'object_column'=>'name', 'object_key'=>'term_id'],1003 'user' => ['o rder'=>30, 'object_model'=>'WPJAM_User', 'object_column'=>'display_name','object_key'=>'ID'],999 'post' => ['object_model'=>'WPJAM_Post', 'object_column'=>'title', 'object_key'=>'ID'], 1000 'term' => ['object_model'=>'WPJAM_Term', 'object_column'=>'name', 'object_key'=>'term_id'], 1001 'user' => ['object_model'=>'WPJAM_User', 'object_column'=>'display_name','object_key'=>'ID'], 1004 1002 ], (is_multisite() ? [ 1005 'blog' => ['o rder'=>5, 'object_key'=>'blog_id'],1006 'site' => [ 'order'=>5],1003 'blog' => ['object_key'=>'blog_id'], 1004 'site' => [], 1007 1005 ] : [])); 1008 1006 } 1009 1007 } 1010 1008 1011 /** 1012 * @config orderby 1013 **/ 1014 #[config('orderby')] 1015 class WPJAM_Meta_Option extends WPJAM_Register{ 1009 class WPJAM_Meta_Option extends WPJAM_Args{ 1016 1010 public function __get($key){ 1017 1011 $value = parent::__get($key); 1018 1012 1019 if($key == 'list_table'){ 1020 return $value ?? (did_action('current_screen') && !empty($GLOBALS['plugin_page'])); 1021 }elseif($key == 'callback'){ 1022 return $value ?: $this->update_callback; 1023 }elseif($key == 'action_name'){ 1024 return $value ?: 'set_'.$this->name; 1013 if(isset($value)){ 1014 return $value; 1015 }elseif($key == 'list_table'){ 1016 return did_action('current_screen') && !empty($GLOBALS['plugin_page']); 1025 1017 }elseif($key == 'show_in_rest'){ 1026 return $value ??true;1018 return true; 1027 1019 }elseif($key == 'show_in_posts_rest'){ 1028 return $value ?? $this->show_in_rest; 1029 } 1030 1031 return $value; 1032 } 1033 1034 public function get_fields($id=null, $output='object'){ 1020 return $this->show_in_rest; 1021 } 1022 } 1023 1024 public function __call($method, $args){ 1025 if($method == 'prepare' && ($this->callback || $this->update_callback)){ 1026 return []; 1027 } 1028 1029 $id = array_shift($args); 1035 1030 $fields = maybe_callback($this->fields, $id, $this->name); 1036 1031 1037 return $output == 'object' ? WPJAM_Fields::create($fields, array_merge($this->get_args(), ['id'=>$id])) : $fields; 1038 } 1039 1040 public function prepare($id=null){ 1041 return $this->callback ? [] : $this->get_fields($id)->prepare(); 1042 } 1043 1044 public function validate($id=null, $data=null){ 1045 return $this->get_fields($id)->validate($data); 1046 } 1047 1048 public function render($id, $args=[]){ 1049 if($this->meta_type == 'post' && isset($GLOBALS['current_screen'])){ 1050 if($this->meta_box_cb){ 1051 return call_user_func($this->meta_box_cb, $id, $args); 1052 } 1053 1054 $args = ['fields_type'=>$this->context == 'side' ? 'list' : 'table']; 1055 $id = $GLOBALS['current_screen']->action == 'add' ? false : $id->ID; 1056 1057 echo $this->summary ? wpautop($this->summary) : ''; 1058 } 1059 1060 echo $this->get_fields($id)->render($args); 1061 } 1062 1063 public function callback($id, $data=null){ 1064 $fields = $this->get_fields($id); 1065 $data = wpjam_catch([$fields, 'validate'], $data); 1066 1067 if(is_wp_error($data) || !$data){ 1068 return $data ?: true; 1069 } 1070 1071 if($this->callback){ 1072 $result = is_callable($this->callback) ? call_user_func($this->callback, $id, $data, $this->get_fields($id, '')) : false; 1073 1074 return $result === false ? new WP_Error('invalid_callback') : $result; 1075 } 1076 1077 return wpjam_update_metadata($this->meta_type, $id, $data, $fields->get_defaults()); 1078 } 1079 } 1080 1081 class WPJAM_AJAX extends WPJAM_Args{ 1082 public function __invoke(){ 1083 add_filter('wp_die_ajax_handler', fn()=> ['WPJAM_Error', 'wp_die_handler']); 1084 1085 $cb = $this->callback; 1086 1087 (!$cb || !is_callable($cb)) && wp_die('invalid_callback'); 1088 1089 if($this->admin){ 1090 $data = $this->fields ? wpjam_fields($this->fields)->get_parameter('POST') : wpjam_get_post_parameter(); 1091 $verify = wpjam_get($data, 'action_type') !== 'form'; 1032 if($method == 'get_fields'){ 1033 return $fields; 1034 } 1035 1036 $object = WPJAM_Fields::create($fields, array_merge($this->get_args(), ['id'=>$id])); 1037 1038 if($method == 'callback'){ 1039 $data = wpjam_catch([$object, 'validate'], ...$args); 1040 1041 if(is_wp_error($data) || !$data){ 1042 return $data ?: true; 1043 } 1044 1045 if($callback = $this->callback ?: $this->update_callback){ 1046 $result = is_callable($callback) ? call_user_func($callback, $id, $data, $fields) : false; 1047 1048 return $result === false ? new WP_Error('invalid_callback') : $result; 1049 } 1050 1051 return wpjam_update_metadata($this->meta_type, $id, $data, $object->get_defaults()); 1052 }elseif($method == 'render'){ 1053 echo wpautop($this->summary ?: '').$object->render(...$args); 1092 1054 }else{ 1093 $data = array_merge(wpjam_get_data_parameter(), wpjam_except(wpjam_get_post_parameter(), ['action', 'defaults', 'data', '_ajax_nonce'])); 1094 $data = array_merge($data, wpjam_fields($this->fields)->validate($data, 'parameter')); 1095 $verify = $this->verify !== false; 1096 } 1097 1098 $action = $verify ? $this->parse_nonce_action($this->name, $data) : ''; 1099 $action && !check_ajax_referer($action, false, false) && wpjam_send_json(['errcode'=>'invalid_nonce', 'errmsg'=>'验证失败,请刷新重试。']); 1100 1101 $this->allow && !wpjam_call($this->allow, $data) && wp_die('access_denied'); 1102 1103 return $cb($data, $this->name); 1104 } 1105 1106 public static function parse_nonce_action($name, $data=[]){ 1107 return ($nonce = wpjam('ajax', $name.'[nonce_action]')) ? $nonce($data) : (wpjam('ajax', $name.'[admin]') ? '' : $name.wpjam_join(':', wpjam_pick($data, wpjam('ajax', $name.'[nonce_keys]') ?: []))); 1108 } 1109 1110 public static function parse_submit_button($button, $submit=null){ 1111 $cb = fn($item)=> (is_array($item) ? $item : ['text'=>$item])+['class'=>'primary']; 1112 1113 if($submit){ 1114 return isset($button[$submit]) ? $cb($button[$submit]) : wp_die('无效的提交按钮'); 1115 } 1116 1117 foreach(array_filter($button) as $key => $item){ 1118 $item = $cb($item); 1119 $parsed[] = get_submit_button($item['text'], $item['class'], $key, false); 1120 } 1121 1122 return isset($parsed) ? wpjam_tag('p', ['submit'], implode($parsed)) : wpjam_tag(); 1123 } 1124 1125 public static function get_attr($name, $data=[]){ 1126 if(wpjam('ajax', $name)){ 1127 return ['action'=>$name, 'data'=>$data]+(($action = self::parse_nonce_action($name, $data)) ? ['nonce'=>wp_create_nonce($action)] : []); 1128 } 1129 } 1130 1131 public static function create($name, $args){ 1132 if(!is_admin() && !wpjam('ajax')){ 1133 wpjam_script('wpjam-ajax', [ 1134 'for' => 'wp, login', 1135 'src' => wpjam_url(dirname(__DIR__).'/static/ajax.js'), 1136 'deps' => ['jquery'], 1137 'data' => 'var ajaxurl = "'.admin_url('admin-ajax.php').'";', 1138 'position' => 'before', 1139 'priority' => 1 1140 ]); 1141 1142 if(!is_login()){ 1143 add_filter('script_loader_tag', fn($tag, $handle)=> $handle == 'wpjam-ajax' && current_theme_supports('script', $handle) ? '' : $tag, 10, 2); 1144 } 1145 } 1146 1147 if(wp_doing_ajax() && wpjam_get($_REQUEST, 'action') == $name && (is_user_logged_in() || !empty($args['nopriv']))){ 1148 add_action('wp_ajax_'.(is_user_logged_in() ? '' : 'nopriv_').$name, fn()=> wpjam_send_json(wpjam_catch(new static(['name'=>$name]+$args)))); 1149 } 1150 1151 return wpjam('ajax', $name, $args); 1055 return $object->$method(...$args); 1056 } 1152 1057 } 1153 1058 } -
wpjam-basic/trunk/public/wpjam-compat.php
r3383874 r3385082 1056 1056 // }); 1057 1057 1058 class WPJAM_Error{ 1059 public static function parse($data){ 1060 return wpjam_parse_error($data); 1061 } 1062 } 1063 1064 class WPJAM_Http{ 1065 public static function request($url, $args=[], $err=[]){ 1066 return wpjam_remote_request($url, $args+['field'=>''], $err); 1067 } 1068 } 1069 1058 1070 class WPJAM_Crypt extends WPJAM_Args{ 1059 1071 public function __construct(...$args){ -
wpjam-basic/trunk/public/wpjam-functions.php
r3383874 r3385082 191 191 } 192 192 193 function wpjam_register_meta_option($ meta_type, $name, $args){194 return ($object = WPJAM_Meta_Type::get($ meta_type)) ? $object->register_option($name, $args) : null;195 } 196 197 function wpjam_unregister_meta_option($ meta_type, $name){198 return ($object = WPJAM_Meta_Type::get($ meta_type)) ? $object->unregister_option($name) : null;199 } 200 201 function wpjam_get_meta_options($ meta_type, $args=[]){202 return ($object = WPJAM_Meta_Type::get($ meta_type)) ? $object->get_options($args) : [];203 } 204 205 function wpjam_get_meta_option($ meta_type, $name, $output='object'){206 $option = ($object = WPJAM_Meta_Type::get($ meta_type)) ? $object->get_option($name) : null;193 function wpjam_register_meta_option($type, $name, $args){ 194 return ($object = WPJAM_Meta_Type::get($type)) ? $object->register_option($name, $args) : null; 195 } 196 197 function wpjam_unregister_meta_option($type, $name){ 198 return ($object = WPJAM_Meta_Type::get($type)) ? $object->unregister_option($name) : null; 199 } 200 201 function wpjam_get_meta_options($type, $args=[]){ 202 return ($object = WPJAM_Meta_Type::get($type)) ? $object->get_option($args) : []; 203 } 204 205 function wpjam_get_meta_option($type, $name, $output='object'){ 206 $option = ($object = WPJAM_Meta_Type::get($type)) ? $object->get_option($name) : null; 207 207 208 208 return $output == 'object' ? $option : ($option ? $option->to_array() : []); 209 209 } 210 210 211 function wpjam_get_by_meta($meta_type, ...$args){ 212 return ($object = WPJAM_Meta_Type::get($meta_type)) ? $object->get_by_key(...$args) : []; 213 } 214 215 function wpjam_get_metadata($meta_type, $object_id, ...$args){ 216 return ($object = WPJAM_Meta_Type::get($meta_type)) ? $object->get_data_with_default($object_id, ...$args) : null; 217 } 218 219 function wpjam_update_metadata($meta_type, $object_id, $key, ...$args){ 220 return ($object = WPJAM_Meta_Type::get($meta_type)) ? $object->update_data_with_default($object_id, $key, ...$args) : null; 221 } 222 223 function wpjam_delete_metadata($meta_type, $object_id, $key){ 224 ($object = WPJAM_Meta_Type::get($meta_type)) && $key && array_map(fn($k)=> $object->delete_data($object_id, $k), (array)$key); 225 226 return true; 211 function wpjam_get_by_meta($type, ...$args){ 212 return ($object = WPJAM_Meta_Type::get($type)) ? $object->get_by_key(...$args) : []; 213 } 214 215 function wpjam_get_metadata($type, $object_id, ...$args){ 216 return ($object = WPJAM_Meta_Type::get($type)) ? $object->get_data_with_default($object_id, ...$args) : null; 217 } 218 219 function wpjam_update_metadata($type, $object_id, $key, ...$args){ 220 return ($object = WPJAM_Meta_Type::get($type)) ? $object->update_data_with_default($object_id, $key, ...$args) : null; 221 } 222 223 function wpjam_delete_metadata($type, $object_id, $key){ 224 return ($object = WPJAM_Meta_Type::get($type)) && $key && array_map(fn($k)=> $object->delete_data($object_id, $k), (array)$key) || true; 227 225 } 228 226 … … 396 394 397 395 function wpjam_get_post_excerpt($post=null, $length=0, $more=null){ 398 $post = get_post($post); 399 400 if(!$post){ 401 return ''; 402 } 403 404 if($post->post_excerpt){ 405 return wp_strip_all_tags($post->post_excerpt, true); 396 if(!($post = get_post($post)) || $post->post_excerpt){ 397 return $post ? wp_strip_all_tags($post->post_excerpt, true) : ''; 406 398 } 407 399 … … 518 510 foreach($images as $image){ 519 511 $parsed = array_map(fn($s)=> wpjam_get_thumbnail($image, $s), $sizes); 520 521 if($query = wpjam_parse_image_query($image)){ 522 $parsed = array_merge($parsed, wpjam_pick($query, ['orientation', 'width', 'height'])+['width'=>0, 'height'=>0]); 523 } 512 $parsed = array_merge($parsed, ($query = wpjam_parse_image_query($image)) ? wpjam_pick($query, ['orientation', 'width', 'height'])+['width'=>0, 'height'=>0] : []); 524 513 525 514 if(isset($sizes['thumbnail'])){ … … 939 928 $args = is_array($name) ? $name : compact('name', 'media', 'post_id'); 940 929 $media = $args['media'] ?? false; 941 $field = wpjam_get($args, 'field') ?: ($media ? 'id' : 'file');930 $field = ($args['field'] ?? '') ?: ($media ? 'id' : 'file'); 942 931 $id = wpjam_get_by_meta('post', 'source_url', $url, 'object_id'); 943 932 … … 945 934 try{ 946 935 $tmp = wpjam_try('download_url', $url); 947 $upload = ['name'=> wpjam_get($args, 'name') ?: md5($url).'.'.wpjam_at(wp_get_image_mime($tmp), '/', 1), 'tmp_name'=>$tmp];936 $upload = ['name'=>($args['name'] ?? '') ?: md5($url).'.'.wpjam_at(wp_get_image_mime($tmp), '/', 1), 'tmp_name'=>$tmp]; 948 937 949 938 if(!$media){ … … 1147 1136 function wpjam_icon($icon){ 1148 1137 if(is_array($icon) || is_object($icon)){ 1149 $k = wpjam_find(['dashicon', 'remixicon'], fn($k)=> wpjam_get($icon, $k)); 1150 1151 return $k ? wpjam_icon(($k == 'remixicon' ? '' : 'dashicons-').wpjam_get($icon, $k)) : null; 1152 } 1153 1154 if(str_starts_with($icon, 'ri-')){ 1155 return wpjam_tag('i', $icon); 1156 } 1157 1158 return wpjam_tag('span', ['dashicons', (str_starts_with($icon, 'dashicons-') ? '' : 'dashicons-').$icon]); 1138 return ($k = wpjam_find(['dashicon', 'remixicon'], fn($k)=> wpjam_get($icon, $k))) ? wpjam_icon(wpjam_get($icon, $k)) : null; 1139 } 1140 1141 return str_starts_with($icon, 'ri-') ? wpjam_tag('i', $icon) : wpjam_tag('span', ['dashicons', (str_starts_with($icon, 'dashicons-') ? '' : 'dashicons-').$icon]); 1159 1142 } 1160 1143 -
wpjam-basic/trunk/public/wpjam-route.php
r3383874 r3385082 39 39 40 40 function wpjam_add_filter($name, $args=[], $priority=10, $accepted_args=1){ 41 if(is_callable( wpjam_get($args, 'callback'))){41 if(is_callable($args['callback'] ?? '')){ 42 42 $cb = function(...$params) use($name, $args, $priority, &$cb){ 43 43 if(!empty($args['check']) && !$args['check'](...$params)){ … … 61 61 62 62 function wpjam_remove_filter($name, $cb, ...$args){ 63 $priority = $args ? $args[0] : has_filter($name, $cb); 64 65 return $priority !== false ? remove_filter($name, $cb, $priority) : false; 63 return ($priority = $args ? $args[0] : has_filter($name, $cb)) !== false ? remove_filter($name, $cb, $priority) : false; 66 64 } 67 65 … … 373 371 $group = $fix ? '' : ($group ?: 'default'); 374 372 $args = is_numeric($arg) ? ['expire'=>$arg] : (array)$arg; 375 $expire = wpjam_get($args, 'expire') ?: 86400;373 $expire = ($args['expire'] ?? '') ?: 86400; 376 374 377 375 if($expire === -1 || $cb === false){ … … 379 377 } 380 378 381 $force = wpjam_get($args, 'force');379 $force = $args['force'] ?? false; 382 380 $value = $fix ? ('get_'.$fix)($key) : wp_cache_get($key, $group, ($force === 'get' || $force === true)); 383 381 … … 463 461 464 462 function wpjam_parse_options($field, $args=[]){ 465 $type = wpjam_get($args, 'type')?? (is_array($field) ? '' : 'select');466 $title = wpjam_get($args, 'title_field') ?: 'title';467 $name = wpjam_get($args, 'name_field') ?: 'name';463 $type = $args['type'] ?? (is_array($field) ? '' : 'select'); 464 $title = ($args['title_field'] ?? '') ?: 'title'; 465 $name = ($args['name_field'] ?? '') ?: 'name'; 468 466 $items = wpjam_filter(is_array($field) ? $field : (wpjam($field) ?: []), $args['filter'] ?? []); 469 467 … … 471 469 if(!is_array($item) && !is_object($item)){ 472 470 $carry[$opt] = $item; 473 }elseif( is_null(wpjam_get($item, 'options'))){474 $opt = wpjam_get($item, $name) ?: $opt;475 $carry = wpjam_set($carry, $opt, wpjam_pick($item, ['label', 'image', 'description', 'alias', 'fields', 'show_if'])+($type == 'select' ? wpjam_pick($item, [$title]) : ( wpjam_get($item, 'field') ?: [])+['label'=>wpjam_get($item, $title)]));471 }elseif(!isset($item['options'])){ 472 $opt = ($item[$name] ?? '') ?: $opt; 473 $carry = wpjam_set($carry, $opt, wpjam_pick($item, ['label', 'image', 'description', 'alias', 'fields', 'show_if'])+($type == 'select' ? wpjam_pick($item, [$title]) : (($item['field'] ?? '') ?: [])+['label'=>($item[$title] ?? '')])); 476 474 } 477 475 … … 564 562 // Request 565 563 function wpjam_remote_request($url='', $args=[], $err=[]){ 566 $throw = wpjam_pull($args, 'throw'); 567 $field = wpjam_pull($args, 'field') ?? 'body'; 568 $result = WPJAM_HTTP::request($url, $args, $err); 569 570 if(is_wp_error($result)){ 571 return $throw ? wpjam_throw($result) : $result; 572 } 573 574 return $field ? wpjam_get($result, $field) : $result; 564 $throw = wpjam_pull($args, 'throw'); 565 $field = wpjam_pull($args, 'field') ?? 'body'; 566 $args += ['body'=>[], 'headers'=>[], 'sslverify'=>false, 'stream'=>false]; 567 $headers = &$args['headers']; 568 $headers = wpjam_array($headers, fn($k)=> strtolower($k)); 569 $method = strtoupper(wpjam_pull($args, 'method', '')) ?: ($args['body'] ? 'POST' : 'GET'); 570 571 if($method == 'GET'){ 572 $response = wp_remote_get($url, $args); 573 }elseif($method == 'FILE'){ 574 $response = (new WP_Http_Curl())->request($url, $args+[ 575 'method' => $args['body'] ? 'POST' : 'GET', 576 'sslcertificates' => ABSPATH.WPINC.'/certificates/ca-bundle.crt', 577 'user-agent' => $headers['user-agent'] ?? 'WordPress', 578 'decompress' => true, 579 ]); 580 }else{ 581 if(str_contains((wpjam_pull($headers, 'content-type') ?: ''), 'application/json') 582 || wpjam_at(wpjam_pull($args, ['json_encode', 'need_json_encode']), 0) 583 ){ 584 $headers += ['content-type'=>'application/json']; 585 $args = (is_array($args['body']) ? ['body'=> wpjam_json_encode($args['body'] ?: new stdClass)] : [])+$args; 586 } 587 588 $response = wp_remote_request($url, $args+['method'=>$method]); 589 } 590 591 if(is_wp_error($response)){ 592 wpjam_trigger_error($response, $url, $args['body']); 593 594 return $throw ? wpjam_throw($response) : $result; 595 } 596 597 $code = $response['response']['code'] ?? 0; 598 599 if($code && ($code < 200 || $code >= 300)){ 600 $error = new WP_Error($code, '远程服务器错误:'.$code.' - '.$response['response']['message'].'-'.var_export($response['body'], true)); 601 602 return $throw ? wpjam_throw($error) : $error; 603 } 604 605 $body = &$response['body']; 606 607 if($body && !$args['stream']){ 608 if(str_contains(wp_remote_retrieve_header($response, 'content-disposition'), 'attachment;')){ 609 $body = wpjam_bits($body); 610 }elseif(wpjam_pull($args, 'json_decode') !== false && str_starts_with($body, '{') && str_ends_with($body, '}')){ 611 $result = wpjam_json_decode($body); 612 613 if(!is_wp_error($result)){ 614 $err += [ 615 'errcode' => 'errcode', 616 'errmsg' => 'errmsg', 617 'detail' => 'detail', 618 'success' => '0', 619 ]; 620 621 $code = wpjam_pull($result, $err['errcode']); 622 623 if($code && $code != $err['success']){ 624 $msg = wpjam_pull($result, $err['errmsg']); 625 $detail = wpjam_pull($result, $err['detail']); 626 $detail = is_null($detail) ? array_filter($result) : $detail; 627 $error = new WP_Error($code, $msg, $detail); 628 629 wpjam_trigger_error($error, $url, $args['body']); 630 631 return $throw ? wpjam_throw($error) : $error; 632 } 633 634 $body = $result; 635 } 636 } 637 } 638 639 return $field ? wpjam_get($response, $field) : $response; 575 640 } 576 641 577 642 // Error 578 function wpjam_parse_error($data){ 579 return WPJAM_Error::parse($data); 580 } 581 582 function wpjam_add_error_setting($code, $message, $modal=[]){ 583 return WPJAM_Error::add_setting($code, $message, $modal); 643 function wpjam_trigger_error($error, $url='', $body=[]){ 644 $error = array_filter(wpjam_fill(['code', 'message', 'data'], fn($k)=> [$error, 'get_error_'.$k]())); 645 646 if(!$url || apply_filters('wpjam_http_response_error_debug', true, $error['code'], $error['message'])){ 647 trigger_error(var_export($url ? array_filter(compact('url', 'error', 'body')) : $error, true)); 648 } 649 } 650 651 function wpjam_parse_error($data, $args=[]){ 652 if(is_wp_error($data)){ 653 $err = $data->get_error_data(); 654 $data = ['errcode'=>$data->get_error_code(), 'errmsg'=>$data->get_error_message()]+array_filter(is_array($err) ? $err : ['errdata'=>$err]); 655 } 656 657 if(wpjam_is_assoc_array($data)){ 658 $data += ['errcode'=>0]; 659 $data = array_merge($data, $data['errcode'] ? wpjam_get_error_setting($data['errcode'], $data['errmsg'] ?? []) : []); 660 } 661 662 return $data; 663 } 664 665 function wpjam_add_error_setting($code, $msg, $modal=[]){ 666 wpjam('error') || add_action('wp_error_added', function($code, $msg, $data, $error){ 667 if($code && count($error->get_error_messages($code)) <= 1 && ($item = wpjam_get_error_setting($code, $msg))){ 668 $error->remove($code); 669 $error->add($code, $item['errmsg'], !empty($item['modal']) ? array_merge((is_array($data) ? $data : []), ['modal'=>$item['modal']]) : $data); 670 } 671 }, 10, 4); 672 673 return wpjam('error', $code, ['errmsg'=>$msg, 'modal'=>$modal]); 674 } 675 676 function wpjam_get_error_setting($code, $args=[]){ 677 if($args && !is_array($args)){ 678 return []; 679 } 680 681 $args = $args ?: []; 682 $item = wpjam('error', $code) ?: []; 683 684 if($item){ 685 $msg = maybe_closure($item['errmsg'], $args); 686 }else{ 687 if(try_remove_suffix($code, '_required')){ 688 $msg = $args ? ($code == 'parameter' ? '参数%s' : '%s的值').'为空或无效。' : '参数或者值无效'; 689 }elseif(try_remove_suffix($code, '_occupied')){ 690 $msg = __($code, 'wpjam-basic').'已被其他账号使用。'; 691 }elseif(try_remove_prefix($code, 'invalid_')){ 692 if($code == 'parameter'){ 693 $msg = $args ? '无效的参数:%s。' : '参数错误。'; 694 }elseif($code == 'callback'){ 695 $msg = '无效的回调函数'.($args ? ':%s' : '').'。'; 696 }elseif($code == 'name'){ 697 $msg = $args ? '%s不能为纯数字。' : '无效的名称'; 698 }elseif(in_array($code, ['code', 'password'])){ 699 $msg = $code == 'code' ? '验证码错误。' : '两次输入的密码不一致。'; 700 }else{ 701 $prefix = '无效的'; 702 $map = [ 703 'id' => ' ID', 704 'post_type' => '文章类型', 705 'taxonomy' => '分类模式', 706 'post' => '文章', 707 'term' => '分类', 708 'user' => '用户', 709 'comment_type' => '评论类型', 710 'comment_id' => '评论 ID', 711 'comment' => '评论', 712 'type' => '类型', 713 'signup_type' => '登录方式', 714 'email' => '邮箱地址', 715 'data_type' => '数据类型', 716 'qrcode' => '二维码', 717 ]; 718 } 719 }elseif(try_remove_prefix($code, 'illegal_')){ 720 $suffix = '无效或已过期。'; 721 $map = ['verify_code' => '验证码']; 722 } 723 724 $msg ??= isset($map) ? ($prefix ?? '').($map[$code] ?? ucwords(str_replace('_', ' ', $code))).($suffix ?? '') : ''; 725 } 726 727 return $msg ? ['errcode'=>$code, 'errmsg'=>($args && str_contains($msg, '%') ? sprintf($msg, ...$args) : $msg)]+$item : []; 584 728 } 585 729 … … 656 800 return array_reduce(wpjam(wpjam_join(':', 'config', $group)), function($carry, $item){ 657 801 if(!empty($item['callback'])){ 658 $name = wpjam_get($item, 'name');659 $value = $item['callback'](...( wpjam_get($item, 'args') ?: ($name ? [$name] : [])));802 $name = $item['name'] ?? ''; 803 $value = $item['callback'](...(($item['args'] ?? []) ?: ($name ? [$name] : []))); 660 804 $item = $name ? [$name=> $value] : (is_array($value) ? $value : []); 661 805 } … … 756 900 $args = array_merge($args[1], [$key => $slug]); 757 901 758 if(!is_admin() && wpjam_get($args, 'function') == 'option' && (!empty($args['sections']) || !empty($args['fields']))){902 if(!is_admin() && ($args['function'] ?? '') == 'option' && (!empty($args['sections']) || !empty($args['fields']))){ 759 903 wpjam_register_option(($args['option_name'] ?? $slug), $args); 760 904 } … … 955 1099 add_action('plugins_loaded', fn()=> is_admin() && wpjam_admin(), 0); 956 1100 1101 add_action('init', fn()=> get_locale() == 'zh_CN' && $GLOBALS['wp_textdomain_registry']->set('wpjam-basic', 'zh_CN', dirname(__DIR__).'/languages')); 1102 957 1103 if(wpjam_is_json_request()){ 958 1104 ini_set('display_errors', 0); -
wpjam-basic/trunk/public/wpjam-utils.php
r3383874 r3385082 34 34 //nbf 时间之前不接收处理该Token 35 35 //exp 过期时间不能小于当前时间 36 if( wpjam_get($header, 'alg') == 'HS256' &&36 if(($header['alg'] ?? '') == 'HS256' && 37 37 !array_any(['iat'=>'>', 'nbf'=>'>', 'exp'=>'<'], fn($v, $k)=> isset($payload[$k]) && wpjam_compare($payload[$k], $v, time())) 38 38 ){ … … 60 60 foreach($types as $type){ 61 61 if($type == 'pkcs7'){ 62 if( wpjam_get($args, 'options') == OPENSSL_ZERO_PADDING && !empty($args['block_size'])){62 if(($args['options'] ?? '') == OPENSSL_ZERO_PADDING && !empty($args['block_size'])){ 63 63 $text = $cb($text, $type, $args['block_size']); 64 64 } 65 65 }elseif($type == 'weixin'){ 66 if( wpjam_get($args, 'pad') == 'weixin' && !empty($args['appid'])){66 if(($args['pad'] ?? '') == 'weixin' && !empty($args['appid'])){ 67 67 $text = $cb($text, $type, trim($args['appid'])); 68 68 } … … 179 179 $data = ['errcode'=>'-1', 'errmsg'=>'系统数据错误或者回调函数返回错误']; 180 180 }else{ 181 $data = WPJAM_Error::parse($data);181 $data = wpjam_parse_error($data); 182 182 } 183 183 -
wpjam-basic/trunk/wpjam-basic.php
r3383874 r3385082 4 4 Plugin URI: https://blog.wpjam.com/project/wpjam-basic/ 5 5 Description: WPJAM 常用的函数和接口,屏蔽所有 WordPress 不常用的功能。 6 Version: 6.8.5 6 Version: 6.8.5.1 7 7 Requires at least: 6.6 8 8 Tested up to: 6.8
Note: See TracChangeset
for help on using the changeset viewer.