'."\n"; } } public function routes(): void { $read=static fn()=>PostTypes::can_edit(); register_rest_route('digiter-builder/v1','/studio/catalog',['methods'=>'GET','permission_callback'=>$read,'callback'=>[$this,'catalog']]); register_rest_route('digiter-builder/v1','/studio/rules',['methods'=>'POST','permission_callback'=>$read,'callback'=>[$this,'rules']]); register_rest_route('digiter-builder/v1','/studio/reindex',['methods'=>'POST','permission_callback'=>static fn()=>current_user_can('manage_options'),'callback'=>static fn($r)=>self::response((new QueryFields())->reindex(absint($r->get_param('after'))))]); } public static function response($data): \WP_REST_Response { $r=new \WP_REST_Response($data);$r->header('Cache-Control','private, no-store');return $r; } public static function error(string $message,int $status=400): \WP_Error {return new \WP_Error('digibl_studio',$message,['status'=>$status]);} public function catalog(): \WP_REST_Response { $types=[];$taxonomies=[]; foreach(get_post_types(['public'=>true],'objects') as $type){ if($type->name==='attachment'||!is_post_type_viewable($type)){continue;} $types[]=['key'=>$type->name,'label'=>$type->labels->name,'fields'=>array_map(static fn($f)=>array_diff_key($f,['storage'=>true]),array_values(QueryFields::catalog($type->name)))]; } foreach(get_taxonomies(['public'=>true],'objects') as $tax){ $terms=get_terms(['taxonomy'=>$tax->name,'hide_empty'=>false,'number'=>200]); $taxonomies[]=['key'=>$tax->name,'label'=>$tax->labels->name,'types'=>$tax->object_type,'terms'=>is_wp_error($terms)?[]:array_map(static fn($term)=>['id'=>$term->term_id,'name'=>$term->name],$terms)]; } $templates=[]; foreach(get_posts(['post_type'=>'digibl_template','post_status'=>'publish','has_password'=>false,'numberposts'=>100]) as $post){$templates[]=['id'=>$post->ID,'title'=>$post->post_title];} $roles=[];foreach(wp_roles()->roles as $key=>$role){$roles[]=['key'=>$key,'label'=>translate_user_role($role['name'])];} return self::response(['types'=>$types,'taxonomies'=>$taxonomies,'templates'=>$templates,'roles'=>$roles,'indexed'=>get_option('digibl_query_index_version','')==='1']); } public function rules($r) { $context=absint($r->get_param('contextId')); if($context&&!current_user_can('edit_post',$context)){return self::error('You cannot preview this content.',403);} if(strlen($r->get_body())>80000){return self::error('Request is too large.',413);} $styles=$r->get_param('styles'); if(!is_array($styles)||count($styles)>9){return self::error('Invalid display preview.');} $result=['visible'=>true,'ancestors'=>[]]; foreach($styles as $style){if(!is_array($style)){return self::error('Invalid display preview.');}$item=DisplayRules::explain($style,$context);$result['visible']=$result['visible']&&$item['visible'];$result['ancestors'][]=$item;} return self::response($result); } }