atarim-visual-collaboration
/
third-party
/
content-model
/
jetengine
/
class-avcf-abilities-jetengine.php
class-avcf-abilities-jetengine.php
4 days ago
class-avcf-jetengine-detector.php
4 days ago
class-avcf-jetengine-helpers.php
4 days ago
class-avcf-abilities-jetengine.php
381 lines
| 1 | <?php |
| 2 | /** |
| 3 | * JetEngine — MCP abilities (schema/authoring + CCT records). |
| 4 | * |
| 5 | * Structure layer (CCT types, meta boxes, options pages) plus CCT RECORDS — |
| 6 | * the latter included because JetEngine CCT records live in dedicated custom |
| 7 | * tables (wp_jet_cct_<slug>) that the generic metadata abilities cannot reach, |
| 8 | * so they are a genuine gap rather than a duplicate. |
| 9 | * |
| 10 | * Confidence (untested against live JetEngine here): |
| 11 | * SOLID — reads (list/get) for CCT types, meta boxes, options pages. |
| 12 | * GOOD — CCT record list/get/create/edit/delete via Factory->get_db(), |
| 13 | * CONTINGENT on resolving the type Factory (intricate internal). |
| 14 | * FLAGGED — authoring of CCT types / meta boxes / options pages goes through |
| 15 | * JetEngine's own data layer (create_item/update_item/delete_item), |
| 16 | * which is architecturally correct but unverified; CCT type |
| 17 | * create/delete also perform table DDL. Validate on a live install. |
| 18 | * |
| 19 | * @package atarim-visual-collaboration |
| 20 | */ |
| 21 | |
| 22 | if ( ! defined('ABSPATH') ) { |
| 23 | exit; |
| 24 | } |
| 25 | |
| 26 | class AVCF_Abilities_JetEngine extends AVCF_Abilities_Base { |
| 27 | |
| 28 | /** @var AVCF_JetEngine_Detector */ |
| 29 | private $detector; |
| 30 | |
| 31 | public function __construct() { |
| 32 | $this->detector = new AVCF_JetEngine_Detector(); |
| 33 | } |
| 34 | |
| 35 | public function register() { |
| 36 | if ( ! $this->detector->avcf_je_is_available() ) { |
| 37 | return; |
| 38 | } |
| 39 | $this->register_check_setup(); |
| 40 | $this->register_ccts(); |
| 41 | $this->register_cct_records(); |
| 42 | $this->register_data_store( 'meta-box', [ 'list' => 'meta-boxes', 'one' => 'meta-box', 'One' => 'Meta Box', 'plural' => 'meta boxes' ], 'meta_boxes_data', 'avcf_je_has_meta_boxes' ); |
| 43 | $this->register_data_store( 'options-page', [ 'list' => 'options-pages', 'one' => 'options-page', 'One' => 'Options Page', 'plural' => 'options pages' ], 'options_pages_data', 'avcf_je_has_options_pages' ); |
| 44 | } |
| 45 | |
| 46 | /* ------------------------------ shared ----------------------------- */ |
| 47 | |
| 48 | private function ro_meta() { |
| 49 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true ] ]; |
| 50 | } |
| 51 | private function write_meta( $destructive = false ) { |
| 52 | return [ 'mcp' => [ 'public' => true, 'type' => 'tool' ], 'annotations' => [ 'readonly' => false, 'destructive' => (bool) $destructive, 'idempotent' => false ] ]; |
| 53 | } |
| 54 | private function std_out() { |
| 55 | return [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ]; |
| 56 | } |
| 57 | |
| 58 | /* --------------------------- check-setup --------------------------- */ |
| 59 | |
| 60 | private function register_check_setup() { |
| 61 | $d = $this->detector; |
| 62 | wp_register_ability( 'atarim/jetengine-check-setup', [ |
| 63 | 'label' => 'Check JetEngine Setup', 'category' => 'atarim', |
| 64 | 'description' => 'Call first. Reports JetEngine presence/version and which modules are active: custom content types, meta boxes, options pages.', |
| 65 | 'input_schema' => [ 'type' => 'object', 'properties' => [], 'additionalProperties' => false ], |
| 66 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'active' => [ 'type' => 'boolean' ], 'version' => [ 'type' => 'string' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'active', 'message' ] ], |
| 67 | 'execute_callback' => function( $input = [] ) use ( $d ) { |
| 68 | return [ 'success' => true, 'active' => $d->avcf_je_is_available(), 'version' => $d->avcf_je_version(), 'modules' => [ |
| 69 | 'custom_content_types' => $d->avcf_je_has_cct(), |
| 70 | 'meta_boxes' => $d->avcf_je_has_meta_boxes(), |
| 71 | 'options_pages' => $d->avcf_je_has_options_pages(), |
| 72 | ], 'message' => 'OK.' ]; |
| 73 | }, |
| 74 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 75 | 'meta' => $this->ro_meta(), |
| 76 | ] ); |
| 77 | } |
| 78 | |
| 79 | /* ------------------------------ CCTs ------------------------------- */ |
| 80 | |
| 81 | private function register_ccts() { |
| 82 | $self = $this; $d = $this->detector; $std = $this->std_out(); |
| 83 | $guard = function() use ( $d ) { |
| 84 | if ( ! $d->avcf_je_has_cct() ) { return [ 'success' => false, 'message' => 'JetEngine Custom Content Types module is not active.' ]; } |
| 85 | return null; |
| 86 | }; |
| 87 | |
| 88 | wp_register_ability( 'atarim/jetengine-list-ccts', [ |
| 89 | 'label' => 'List JetEngine CCTs', 'category' => 'atarim', |
| 90 | 'description' => 'List JetEngine Custom Content Types (slug, name, fields).', |
| 91 | 'input_schema' => [ 'type' => 'object', 'properties' => [], 'additionalProperties' => false ], |
| 92 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'ccts' => [ 'type' => 'array' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 93 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 94 | $g = $guard(); if ( $g ) { return $g; } |
| 95 | $out = []; |
| 96 | foreach ( AVCF_JetEngine_Helpers::store_items( AVCF_JetEngine_Helpers::cct_data() ) as $item ) { $out[] = AVCF_JetEngine_Helpers::shape_cct( $item ); } |
| 97 | return [ 'success' => true, 'ccts' => $out, 'message' => sprintf( '%d CCT(s).', count( $out ) ) ]; |
| 98 | }, |
| 99 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 100 | 'meta' => $this->ro_meta(), |
| 101 | ] ); |
| 102 | |
| 103 | wp_register_ability( 'atarim/jetengine-get-cct', [ |
| 104 | 'label' => 'Get JetEngine CCT', 'category' => 'atarim', |
| 105 | 'description' => 'Get a CCT definition by slug.', |
| 106 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ] ], 'required' => [ 'slug' ], 'additionalProperties' => false ], |
| 107 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'cct' => [ 'type' => 'object' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 108 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 109 | $g = $guard(); if ( $g ) { return $g; } |
| 110 | $slug = (string) $input['slug']; |
| 111 | foreach ( AVCF_JetEngine_Helpers::store_items( AVCF_JetEngine_Helpers::cct_data() ) as $item ) { |
| 112 | $a = (array) $item; |
| 113 | if ( isset( $a['slug'] ) && $a['slug'] === $slug ) { return [ 'success' => true, 'cct' => AVCF_JetEngine_Helpers::shape_cct( $item ), 'message' => 'OK.' ]; } |
| 114 | } |
| 115 | return [ 'success' => false, 'message' => sprintf( 'CCT "%s" not found.', $slug ) ]; |
| 116 | }, |
| 117 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 118 | 'meta' => $this->ro_meta(), |
| 119 | ] ); |
| 120 | |
| 121 | $note = ' BEST-EFFORT: routes through JetEngine\'s CCT data layer (which also performs table DDL); validate on a live install.'; |
| 122 | wp_register_ability( 'atarim/jetengine-create-cct', [ |
| 123 | 'label' => 'Create JetEngine CCT', 'category' => 'atarim', |
| 124 | 'description' => 'Create a Custom Content Type and its table (wp_jet_cct_<slug>).' . $note . ' Provide slug, name, and fields (array of field defs); args is optional extra config.', |
| 125 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'name' => [ 'type' => 'string' ], 'fields' => [ 'type' => 'array' ], 'args' => [ 'type' => 'object' ] ], 'required' => [ 'slug', 'name' ], 'additionalProperties' => false ], |
| 126 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 127 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 128 | $g = $guard(); if ( $g ) { return $g; } |
| 129 | $item = [ 'slug' => sanitize_key( (string) $input['slug'] ), 'name' => (string) $input['name'], 'meta_fields' => isset( $input['fields'] ) ? (array) $input['fields'] : [], 'args' => isset( $input['args'] ) ? (array) $input['args'] : [] ]; |
| 130 | $r = AVCF_JetEngine_Helpers::store_create( AVCF_JetEngine_Helpers::cct_data(), $item ); |
| 131 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 132 | return [ 'success' => true, 'message' => 'CCT created (best-effort — verify the table and type in JetEngine).' ]; |
| 133 | }, |
| 134 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 135 | 'meta' => $this->write_meta( false ), |
| 136 | ] ); |
| 137 | wp_register_ability( 'atarim/jetengine-edit-cct', [ |
| 138 | 'label' => 'Edit JetEngine CCT', 'category' => 'atarim', |
| 139 | 'description' => 'Update a CCT.' . $note . ' Note: JetEngine restricts schema migrations (field rename / SQL-type change / key-field toggle are rejected; add/remove field is allowed).', |
| 140 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'id' => [ 'type' => 'string' ], 'slug' => [ 'type' => 'string' ], 'name' => [ 'type' => 'string' ], 'fields' => [ 'type' => 'array' ], 'args' => [ 'type' => 'object' ] ], 'required' => [ 'id' ], 'additionalProperties' => false ], |
| 141 | 'output_schema'=> $std, |
| 142 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 143 | $g = $guard(); if ( $g ) { return $g; } |
| 144 | $item = [ 'id' => $input['id'] ]; |
| 145 | foreach ( [ 'slug', 'name', 'args' ] as $k ) { if ( isset( $input[ $k ] ) ) { $item[ $k ] = $input[ $k ]; } } |
| 146 | if ( isset( $input['fields'] ) ) { $item['meta_fields'] = (array) $input['fields']; } |
| 147 | $r = AVCF_JetEngine_Helpers::store_update( AVCF_JetEngine_Helpers::cct_data(), $item ); |
| 148 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 149 | return [ 'success' => true, 'message' => 'CCT updated (best-effort).' ]; |
| 150 | }, |
| 151 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 152 | 'meta' => $this->write_meta( false ), |
| 153 | ] ); |
| 154 | wp_register_ability( 'atarim/jetengine-delete-cct', [ |
| 155 | 'label' => 'Delete JetEngine CCT', 'category' => 'atarim', |
| 156 | 'description' => 'Delete a CCT and drop its table. Dry run unless confirm:true. This destroys all records in the type.', |
| 157 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'id' => [ 'type' => 'string' ], 'confirm' => [ 'type' => 'boolean', 'default' => false ] ], 'required' => [ 'id' ], 'additionalProperties' => false ], |
| 158 | 'output_schema'=> $std, |
| 159 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 160 | $g = $guard(); if ( $g ) { return $g; } |
| 161 | if ( empty( $input['confirm'] ) ) { return [ 'success' => true, 'deleted' => false, 'message' => 'Dry run: would delete this CCT and DROP its table (all records lost). Re-call with confirm:true.' ]; } |
| 162 | $r = AVCF_JetEngine_Helpers::store_delete( AVCF_JetEngine_Helpers::cct_data(), $input['id'] ); |
| 163 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 164 | return [ 'success' => true, 'deleted' => true, 'message' => 'CCT deleted (best-effort).' ]; |
| 165 | }, |
| 166 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 167 | 'meta' => $this->write_meta( true ), |
| 168 | ] ); |
| 169 | } |
| 170 | |
| 171 | /* --------------------------- CCT records --------------------------- */ |
| 172 | |
| 173 | private function register_cct_records() { |
| 174 | $self = $this; $d = $this->detector; $std = $this->std_out(); |
| 175 | $guard = function( $slug ) use ( $d ) { |
| 176 | if ( ! $d->avcf_je_has_cct() ) { return [ 'success' => false, 'message' => 'JetEngine CCT module is not active.' ]; } |
| 177 | $db = AVCF_JetEngine_Helpers::cct_db( $slug ); |
| 178 | if ( ! $db ) { return [ 'success' => false, 'message' => sprintf( 'Could not resolve the record table for CCT "%s" (type missing, or Factory not resolvable on this build).', $slug ) ]; } |
| 179 | return $db; |
| 180 | }; |
| 181 | |
| 182 | wp_register_ability( 'atarim/jetengine-list-cct-records', [ |
| 183 | 'label' => 'List JetEngine CCT Records', 'category' => 'atarim', |
| 184 | 'description' => 'List records in a CCT. where is a column=>value filter map; supports limit and offset.', |
| 185 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'where' => [ 'type' => 'object' ], 'limit' => [ 'type' => 'integer', 'minimum' => 1, 'default' => 50 ], 'offset' => [ 'type' => 'integer', 'minimum' => 0, 'default' => 0 ] ], 'required' => [ 'slug' ], 'additionalProperties' => false ], |
| 186 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'records' => [ 'type' => 'array' ], 'total' => [ 'type' => 'integer' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 187 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 188 | $db = $guard( (string) $input['slug'] ); |
| 189 | if ( is_array( $db ) ) { return $db; } |
| 190 | $where = isset( $input['where'] ) && is_array( $input['where'] ) ? $input['where'] : []; |
| 191 | $limit = isset( $input['limit'] ) ? (int) $input['limit'] : 50; |
| 192 | $offset = isset( $input['offset'] ) ? (int) $input['offset'] : 0; |
| 193 | try { |
| 194 | $rows = method_exists( $db, 'query' ) ? $db->query( $where, $limit, $offset ) : []; |
| 195 | $total = method_exists( $db, 'count' ) ? (int) $db->count( $where ) : count( (array) $rows ); |
| 196 | return [ 'success' => true, 'records' => array_map( function( $r ) { return (array) $r; }, (array) $rows ), 'total' => $total, 'message' => 'OK.' ]; |
| 197 | } catch ( \Throwable $e ) { |
| 198 | return [ 'success' => false, 'message' => 'Query failed: ' . $e->getMessage() ]; |
| 199 | } |
| 200 | }, |
| 201 | 'permission_callback' => function() { return current_user_can( 'edit_posts' ); }, |
| 202 | 'meta' => $this->ro_meta(), |
| 203 | ] ); |
| 204 | |
| 205 | wp_register_ability( 'atarim/jetengine-get-cct-record', [ |
| 206 | 'label' => 'Get JetEngine CCT Record', 'category' => 'atarim', |
| 207 | 'description' => 'Get a single CCT record by its _ID.', |
| 208 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'id' => [ 'type' => 'integer' ] ], 'required' => [ 'slug', 'id' ], 'additionalProperties' => false ], |
| 209 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'record' => [ 'type' => 'object' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 210 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 211 | $db = $guard( (string) $input['slug'] ); |
| 212 | if ( is_array( $db ) ) { return $db; } |
| 213 | try { |
| 214 | $rows = method_exists( $db, 'query' ) ? $db->query( [ '_ID' => (int) $input['id'] ], 1, 0 ) : []; |
| 215 | $rows = (array) $rows; |
| 216 | if ( empty( $rows ) ) { return [ 'success' => false, 'message' => sprintf( 'Record %d not found.', (int) $input['id'] ) ]; } |
| 217 | return [ 'success' => true, 'record' => (array) $rows[0], 'message' => 'OK.' ]; |
| 218 | } catch ( \Throwable $e ) { |
| 219 | return [ 'success' => false, 'message' => 'Query failed: ' . $e->getMessage() ]; |
| 220 | } |
| 221 | }, |
| 222 | 'permission_callback' => function() { return current_user_can( 'edit_posts' ); }, |
| 223 | 'meta' => $this->ro_meta(), |
| 224 | ] ); |
| 225 | |
| 226 | wp_register_ability( 'atarim/jetengine-create-cct-record', [ |
| 227 | 'label' => 'Create JetEngine CCT Record', 'category' => 'atarim', |
| 228 | 'description' => 'Insert a record into a CCT. values is a column=>value map matching the type\'s fields.', |
| 229 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'values' => [ 'type' => 'object' ] ], 'required' => [ 'slug', 'values' ], 'additionalProperties' => false ], |
| 230 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'id' => [ 'type' => 'integer' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 231 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 232 | $db = $guard( (string) $input['slug'] ); |
| 233 | if ( is_array( $db ) ) { return $db; } |
| 234 | $values = isset( $input['values'] ) && is_array( $input['values'] ) ? $input['values'] : []; |
| 235 | if ( empty( $values ) ) { return [ 'success' => false, 'message' => 'values must be a non-empty object.' ]; } |
| 236 | try { |
| 237 | $new_id = method_exists( $db, 'insert' ) ? $db->insert( $values ) : null; |
| 238 | return [ 'success' => true, 'id' => (int) $new_id, 'message' => 'Record created.' ]; |
| 239 | } catch ( \Throwable $e ) { |
| 240 | return [ 'success' => false, 'message' => 'Insert failed: ' . $e->getMessage() ]; |
| 241 | } |
| 242 | }, |
| 243 | 'permission_callback' => function() { return current_user_can( 'edit_posts' ); }, |
| 244 | 'meta' => $this->write_meta( false ), |
| 245 | ] ); |
| 246 | |
| 247 | wp_register_ability( 'atarim/jetengine-edit-cct-record', [ |
| 248 | 'label' => 'Edit JetEngine CCT Record', 'category' => 'atarim', |
| 249 | 'description' => 'Update a CCT record by _ID. values is the column=>value map to write.', |
| 250 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'id' => [ 'type' => 'integer' ], 'values' => [ 'type' => 'object' ] ], 'required' => [ 'slug', 'id', 'values' ], 'additionalProperties' => false ], |
| 251 | 'output_schema'=> $std, |
| 252 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 253 | $db = $guard( (string) $input['slug'] ); |
| 254 | if ( is_array( $db ) ) { return $db; } |
| 255 | $values = isset( $input['values'] ) && is_array( $input['values'] ) ? $input['values'] : []; |
| 256 | if ( empty( $values ) ) { return [ 'success' => false, 'message' => 'values must be a non-empty object.' ]; } |
| 257 | try { |
| 258 | method_exists( $db, 'update' ) ? $db->update( $values, [ '_ID' => (int) $input['id'] ] ) : null; |
| 259 | return [ 'success' => true, 'message' => sprintf( 'Record %d updated.', (int) $input['id'] ) ]; |
| 260 | } catch ( \Throwable $e ) { |
| 261 | return [ 'success' => false, 'message' => 'Update failed: ' . $e->getMessage() ]; |
| 262 | } |
| 263 | }, |
| 264 | 'permission_callback' => function() { return current_user_can( 'edit_posts' ); }, |
| 265 | 'meta' => $this->write_meta( false ), |
| 266 | ] ); |
| 267 | |
| 268 | wp_register_ability( 'atarim/jetengine-delete-cct-record', [ |
| 269 | 'label' => 'Delete JetEngine CCT Record', 'category' => 'atarim', |
| 270 | 'description' => 'Delete a CCT record by _ID. Dry run unless confirm:true.', |
| 271 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'slug' => [ 'type' => 'string' ], 'id' => [ 'type' => 'integer' ], 'confirm' => [ 'type' => 'boolean', 'default' => false ] ], 'required' => [ 'slug', 'id' ], 'additionalProperties' => false ], |
| 272 | 'output_schema'=> $std, |
| 273 | 'execute_callback' => function( $input = [] ) use ( $guard ) { |
| 274 | $db = $guard( (string) $input['slug'] ); |
| 275 | if ( is_array( $db ) ) { return $db; } |
| 276 | if ( empty( $input['confirm'] ) ) { return [ 'success' => true, 'deleted' => false, 'message' => sprintf( 'Dry run: would delete record %d. Re-call with confirm:true.', (int) $input['id'] ) ]; } |
| 277 | try { |
| 278 | method_exists( $db, 'delete' ) ? $db->delete( [ '_ID' => (int) $input['id'] ] ) : null; |
| 279 | return [ 'success' => true, 'deleted' => true, 'message' => sprintf( 'Record %d deleted.', (int) $input['id'] ) ]; |
| 280 | } catch ( \Throwable $e ) { |
| 281 | return [ 'success' => false, 'message' => 'Delete failed: ' . $e->getMessage() ]; |
| 282 | } |
| 283 | }, |
| 284 | 'permission_callback' => function() { return current_user_can( 'edit_posts' ); }, |
| 285 | 'meta' => $this->write_meta( true ), |
| 286 | ] ); |
| 287 | } |
| 288 | |
| 289 | /* ---------------- generic data-store registrar (MB/OP) ------------- */ |
| 290 | |
| 291 | private function register_data_store( $key, $labels, $data_method, $detector_check ) { |
| 292 | $self = $this; $d = $this->detector; $std = $this->std_out(); |
| 293 | $store = function() use ( $data_method ) { return call_user_func( [ 'AVCF_JetEngine_Helpers', $data_method ] ); }; |
| 294 | $guard = function() use ( $d, $detector_check, $labels ) { |
| 295 | if ( ! call_user_func( [ $d, $detector_check ] ) ) { return [ 'success' => false, 'message' => sprintf( 'JetEngine %s are not active.', $labels['plural'] ) ]; } |
| 296 | return null; |
| 297 | }; |
| 298 | $note = sprintf( ' BEST-EFFORT: routes through JetEngine\'s %s data layer; validate on a live install.', $labels['one'] ); |
| 299 | |
| 300 | wp_register_ability( 'atarim/jetengine-list-' . $labels['list'], [ |
| 301 | 'label' => 'List JetEngine ' . $labels['One'] . 's', 'category' => 'atarim', |
| 302 | 'description' => sprintf( 'List JetEngine %s.', $labels['plural'] ), |
| 303 | 'input_schema' => [ 'type' => 'object', 'properties' => [], 'additionalProperties' => false ], |
| 304 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'items' => [ 'type' => 'array' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 305 | 'execute_callback' => function( $input = [] ) use ( $guard, $store ) { |
| 306 | $g = $guard(); if ( $g ) { return $g; } |
| 307 | $items = AVCF_JetEngine_Helpers::store_items( $store() ); |
| 308 | return [ 'success' => true, 'items' => array_map( function( $i ) { return (array) $i; }, $items ), 'message' => sprintf( '%d item(s).', count( $items ) ) ]; |
| 309 | }, |
| 310 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 311 | 'meta' => $this->ro_meta(), |
| 312 | ] ); |
| 313 | |
| 314 | wp_register_ability( 'atarim/jetengine-get-' . $labels['one'], [ |
| 315 | 'label' => 'Get JetEngine ' . $labels['One'], 'category' => 'atarim', |
| 316 | 'description' => sprintf( 'Get one JetEngine %s by id.', $labels['one'] ), |
| 317 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'id' => [ 'type' => 'string' ] ], 'required' => [ 'id' ], 'additionalProperties' => false ], |
| 318 | 'output_schema'=> [ 'type' => 'object', 'properties' => [ 'success' => [ 'type' => 'boolean' ], 'item' => [ 'type' => 'object' ], 'message' => [ 'type' => 'string' ] ], 'required' => [ 'success', 'message' ] ], |
| 319 | 'execute_callback' => function( $input = [] ) use ( $guard, $store, $labels ) { |
| 320 | $g = $guard(); if ( $g ) { return $g; } |
| 321 | $id = (string) $input['id']; |
| 322 | foreach ( AVCF_JetEngine_Helpers::store_items( $store() ) as $item ) { |
| 323 | $a = (array) $item; |
| 324 | if ( ( isset( $a['id'] ) && (string) $a['id'] === $id ) || ( isset( $a['slug'] ) && (string) $a['slug'] === $id ) ) { |
| 325 | return [ 'success' => true, 'item' => $a, 'message' => 'OK.' ]; |
| 326 | } |
| 327 | } |
| 328 | return [ 'success' => false, 'message' => sprintf( '%s "%s" not found.', $labels['One'], $id ) ]; |
| 329 | }, |
| 330 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 331 | 'meta' => $this->ro_meta(), |
| 332 | ] ); |
| 333 | |
| 334 | wp_register_ability( 'atarim/jetengine-create-' . $labels['one'], [ |
| 335 | 'label' => 'Create JetEngine ' . $labels['One'], 'category' => 'atarim', |
| 336 | 'description' => sprintf( 'Create a JetEngine %s.%s item is the full definition object as JetEngine stores it.', $labels['one'], $note ), |
| 337 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'item' => [ 'type' => 'object' ] ], 'required' => [ 'item' ], 'additionalProperties' => false ], |
| 338 | 'output_schema'=> $std, |
| 339 | 'execute_callback' => function( $input = [] ) use ( $guard, $store ) { |
| 340 | $g = $guard(); if ( $g ) { return $g; } |
| 341 | $r = AVCF_JetEngine_Helpers::store_create( $store(), (array) $input['item'] ); |
| 342 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 343 | return [ 'success' => true, 'message' => 'Created (best-effort — verify in JetEngine).' ]; |
| 344 | }, |
| 345 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 346 | 'meta' => $this->write_meta( false ), |
| 347 | ] ); |
| 348 | |
| 349 | wp_register_ability( 'atarim/jetengine-edit-' . $labels['one'], [ |
| 350 | 'label' => 'Edit JetEngine ' . $labels['One'], 'category' => 'atarim', |
| 351 | 'description' => sprintf( 'Update a JetEngine %s.%s item must include its id.', $labels['one'], $note ), |
| 352 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'item' => [ 'type' => 'object' ] ], 'required' => [ 'item' ], 'additionalProperties' => false ], |
| 353 | 'output_schema'=> $std, |
| 354 | 'execute_callback' => function( $input = [] ) use ( $guard, $store ) { |
| 355 | $g = $guard(); if ( $g ) { return $g; } |
| 356 | $r = AVCF_JetEngine_Helpers::store_update( $store(), (array) $input['item'] ); |
| 357 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 358 | return [ 'success' => true, 'message' => 'Updated (best-effort).' ]; |
| 359 | }, |
| 360 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 361 | 'meta' => $this->write_meta( false ), |
| 362 | ] ); |
| 363 | |
| 364 | wp_register_ability( 'atarim/jetengine-delete-' . $labels['one'], [ |
| 365 | 'label' => 'Delete JetEngine ' . $labels['One'], 'category' => 'atarim', |
| 366 | 'description' => sprintf( 'Delete a JetEngine %s by id. Dry run unless confirm:true.', $labels['one'] ), |
| 367 | 'input_schema' => [ 'type' => 'object', 'properties' => [ 'id' => [ 'type' => 'string' ], 'confirm' => [ 'type' => 'boolean', 'default' => false ] ], 'required' => [ 'id' ], 'additionalProperties' => false ], |
| 368 | 'output_schema'=> $std, |
| 369 | 'execute_callback' => function( $input = [] ) use ( $guard, $store, $labels ) { |
| 370 | $g = $guard(); if ( $g ) { return $g; } |
| 371 | if ( empty( $input['confirm'] ) ) { return [ 'success' => true, 'deleted' => false, 'message' => sprintf( 'Dry run: would delete %s "%s". Re-call with confirm:true.', $labels['one'], $input['id'] ) ]; } |
| 372 | $r = AVCF_JetEngine_Helpers::store_delete( $store(), $input['id'] ); |
| 373 | if ( is_wp_error( $r ) ) { return [ 'success' => false, 'message' => $r->get_error_message() ]; } |
| 374 | return [ 'success' => true, 'deleted' => true, 'message' => 'Deleted (best-effort).' ]; |
| 375 | }, |
| 376 | 'permission_callback' => function() { return current_user_can( 'manage_options' ); }, |
| 377 | 'meta' => $this->write_meta( true ), |
| 378 | ] ); |
| 379 | } |
| 380 | } |
| 381 |