atarim-visual-collaboration
/
third-party
/
content-model
/
jetengine
/
class-avcf-jetengine-helpers.php
class-avcf-abilities-jetengine.php
4 weeks ago
class-avcf-jetengine-detector.php
4 weeks ago
class-avcf-jetengine-helpers.php
4 weeks ago
class-avcf-jetengine-helpers.php
169 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shared helpers for the JetEngine ability cluster. |
| 4 | * |
| 5 | * JetEngine keeps its definitions in "data" objects that share a common base |
| 6 | * (get_items / create_item / update_item / delete_item): |
| 7 | * - CCT types -> CCT module manager's data store (also handles table DDL) |
| 8 | * - meta boxes -> jet_engine()->meta_boxes->data |
| 9 | * - options pages -> jet_engine()->options_pages->data |
| 10 | * CCT *records* live in per-type custom tables (wp_jet_cct_<slug>), reached |
| 11 | * through the type's Factory->get_db() handler. |
| 12 | * |
| 13 | * None of this could be tested against a live JetEngine here. Reads are sound; |
| 14 | * authoring goes through JetEngine's own data layer (architecturally correct |
| 15 | * but unverified); record CRUD depends on resolving the Factory, which is |
| 16 | * intricate — treat as best-effort pending live validation. |
| 17 | * |
| 18 | * @package atarim-visual-collaboration |
| 19 | */ |
| 20 | |
| 21 | if ( ! defined('ABSPATH') ) { |
| 22 | exit; |
| 23 | } |
| 24 | |
| 25 | class AVCF_JetEngine_Helpers { |
| 26 | |
| 27 | /** jet_engine() instance or null. */ |
| 28 | public static function je() { |
| 29 | return function_exists( 'jet_engine' ) ? jet_engine() : null; |
| 30 | } |
| 31 | |
| 32 | /** The CCT module object, or null. */ |
| 33 | public static function cct_module() { |
| 34 | $je = self::je(); |
| 35 | if ( ! $je || ! isset( $je->modules ) || ! method_exists( $je->modules, 'get_module' ) ) { |
| 36 | return null; |
| 37 | } |
| 38 | return $je->modules->get_module( 'custom-content-types' ); |
| 39 | } |
| 40 | |
| 41 | /** CCT types data store (get_items / create_item / update_item / delete_item). */ |
| 42 | public static function cct_data() { |
| 43 | $module = self::cct_module(); |
| 44 | if ( ! $module ) { |
| 45 | return null; |
| 46 | } |
| 47 | $manager = isset( $module->manager ) ? $module->manager : ( isset( $module->instance->manager ) ? $module->instance->manager : null ); |
| 48 | if ( $manager && isset( $manager->data ) ) { |
| 49 | return $manager->data; |
| 50 | } |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | public static function meta_boxes_data() { |
| 55 | $je = self::je(); |
| 56 | return ( $je && isset( $je->meta_boxes ) && isset( $je->meta_boxes->data ) ) ? $je->meta_boxes->data : null; |
| 57 | } |
| 58 | |
| 59 | public static function options_pages_data() { |
| 60 | $je = self::je(); |
| 61 | return ( $je && isset( $je->options_pages ) && isset( $je->options_pages->data ) ) ? $je->options_pages->data : null; |
| 62 | } |
| 63 | |
| 64 | /** Read items from a JetEngine data store as an array. */ |
| 65 | public static function store_items( $store ) { |
| 66 | if ( ! $store || ! method_exists( $store, 'get_items' ) ) { |
| 67 | return []; |
| 68 | } |
| 69 | $items = $store->get_items(); |
| 70 | return is_array( $items ) ? $items : (array) $items; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Create/update/delete via a JetEngine data store. The base data class |
| 75 | * reads from the request by default; passing $from_request=false uses the |
| 76 | * provided item array. Returns the store result or a WP_Error. |
| 77 | */ |
| 78 | public static function store_create( $store, $item ) { |
| 79 | if ( ! $store || ! method_exists( $store, 'create_item' ) ) { |
| 80 | return new WP_Error( 'no_create', 'This JetEngine data store does not expose create_item().' ); |
| 81 | } |
| 82 | try { |
| 83 | return $store->create_item( false, $item ); |
| 84 | } catch ( \Throwable $e ) { |
| 85 | return new WP_Error( 'create_failed', $e->getMessage() ); |
| 86 | } |
| 87 | } |
| 88 | public static function store_update( $store, $item ) { |
| 89 | if ( ! $store || ! method_exists( $store, 'update_item' ) ) { |
| 90 | return new WP_Error( 'no_update', 'This JetEngine data store does not expose update_item().' ); |
| 91 | } |
| 92 | try { |
| 93 | return $store->update_item( false, $item ); |
| 94 | } catch ( \Throwable $e ) { |
| 95 | return new WP_Error( 'update_failed', $e->getMessage() ); |
| 96 | } |
| 97 | } |
| 98 | public static function store_delete( $store, $id ) { |
| 99 | if ( ! $store || ! method_exists( $store, 'delete_item' ) ) { |
| 100 | return new WP_Error( 'no_delete', 'This JetEngine data store does not expose delete_item().' ); |
| 101 | } |
| 102 | try { |
| 103 | return $store->delete_item( false, $id ); |
| 104 | } catch ( \Throwable $e ) { |
| 105 | return new WP_Error( 'delete_failed', $e->getMessage() ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Resolve a CCT Factory by slug (best-effort across known manager APIs). |
| 111 | * Record CRUD depends on this. Returns object|null. |
| 112 | */ |
| 113 | public static function cct_factory( $slug ) { |
| 114 | $module = self::cct_module(); |
| 115 | if ( ! $module ) { |
| 116 | return null; |
| 117 | } |
| 118 | $manager = isset( $module->manager ) ? $module->manager : null; |
| 119 | if ( ! $manager ) { |
| 120 | return null; |
| 121 | } |
| 122 | try { |
| 123 | if ( method_exists( $manager, 'get_content_types' ) ) { |
| 124 | $types = $manager->get_content_types(); |
| 125 | if ( is_array( $types ) && isset( $types[ $slug ] ) ) { |
| 126 | return $types[ $slug ]; |
| 127 | } |
| 128 | } |
| 129 | if ( method_exists( $manager, 'get_factory_instances' ) ) { |
| 130 | $factories = $manager->get_factory_instances(); |
| 131 | if ( is_array( $factories ) && isset( $factories[ $slug ] ) ) { |
| 132 | return $factories[ $slug ]; |
| 133 | } |
| 134 | } |
| 135 | if ( method_exists( $manager, 'get_content_type_for_slug' ) ) { |
| 136 | return $manager->get_content_type_for_slug( $slug ); |
| 137 | } |
| 138 | } catch ( \Throwable $e ) { |
| 139 | return null; |
| 140 | } |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | /** Get the record DB handler for a CCT slug, or null. */ |
| 145 | public static function cct_db( $slug ) { |
| 146 | $factory = self::cct_factory( $slug ); |
| 147 | if ( $factory && method_exists( $factory, 'get_db' ) ) { |
| 148 | try { |
| 149 | return $factory->get_db(); |
| 150 | } catch ( \Throwable $e ) { |
| 151 | return null; |
| 152 | } |
| 153 | } |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | /** Shape a CCT type item for output. */ |
| 158 | public static function shape_cct( $item ) { |
| 159 | $a = (array) $item; |
| 160 | return [ |
| 161 | 'id' => isset( $a['id'] ) ? $a['id'] : null, |
| 162 | 'slug' => isset( $a['slug'] ) ? $a['slug'] : '', |
| 163 | 'name' => isset( $a['name'] ) ? $a['name'] : '', |
| 164 | 'args' => isset( $a['args'] ) ? $a['args'] : null, |
| 165 | 'fields' => isset( $a['meta_fields'] ) ? $a['meta_fields'] : ( isset( $a['fields'] ) ? $a['fields'] : null ), |
| 166 | ]; |
| 167 | } |
| 168 | } |
| 169 |