class-avcf-abilities-ase.php
1 month ago
class-avcf-ase-detector.php
1 month ago
class-avcf-ase-helpers.php
1 month ago
class-avcf-ase-helpers.php
88 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shared helpers for the ASE ability cluster. |
| 4 | * |
| 5 | * ASE stores its definitions as ordinary WordPress post types with config in |
| 6 | * post meta: |
| 7 | * - field groups -> asenha_cfgroup (cfgroup_fields / cfgroup_rules / cfgroup_extras) |
| 8 | * - post types -> asenha_cpt (slug + cpt_label_* + settings) |
| 9 | * - taxonomies -> asenha_ctax (slug + ctax_label_* + settings) |
| 10 | * Because storage is plain wp_insert_post + post meta, reads and the |
| 11 | * field-group writes are reliable; the full label-key schema for CPT/taxonomy |
| 12 | * (ASE auto-generates ~31 cpt_label_* keys) is version-specific, so CPT/tax |
| 13 | * authoring is best-effort. Not tested against a live ASE here. |
| 14 | * |
| 15 | * @package atarim-visual-collaboration |
| 16 | */ |
| 17 | |
| 18 | if ( ! defined('ABSPATH') ) { |
| 19 | exit; |
| 20 | } |
| 21 | |
| 22 | class AVCF_ASE_Helpers { |
| 23 | |
| 24 | public static function list_cpt( $cpt ) { |
| 25 | return get_posts( [ 'post_type' => $cpt, 'post_status' => 'any', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ] ); |
| 26 | } |
| 27 | |
| 28 | public static function find_cpt( $cpt, $key ) { |
| 29 | if ( is_numeric( $key ) ) { |
| 30 | $p = get_post( (int) $key ); |
| 31 | return ( $p && $p->post_type === $cpt ) ? $p : null; |
| 32 | } |
| 33 | // Match by stored slug meta first, then by post_name. |
| 34 | $posts = self::list_cpt( $cpt ); |
| 35 | foreach ( $posts as $p ) { |
| 36 | $slug = get_post_meta( $p->ID, 'cpt_slug', true ); |
| 37 | if ( $slug === '' ) { $slug = get_post_meta( $p->ID, 'ctax_slug', true ); } |
| 38 | if ( $slug === '' ) { $slug = get_post_meta( $p->ID, 'cfgroup_slug', true ); } |
| 39 | if ( (string) $slug === (string) $key || $p->post_name === sanitize_title( (string) $key ) ) { |
| 40 | return $p; |
| 41 | } |
| 42 | } |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | public static function post_meta_flat( $post_id ) { |
| 47 | $raw = get_post_meta( $post_id ); |
| 48 | $out = []; |
| 49 | if ( is_array( $raw ) ) { |
| 50 | foreach ( $raw as $k => $vals ) { |
| 51 | if ( strpos( $k, '_' ) === 0 ) { continue; } |
| 52 | $v = is_array( $vals ) && count( $vals ) === 1 ? $vals[0] : $vals; |
| 53 | $out[ $k ] = is_string( $v ) ? maybe_unserialize( $v ) : $v; |
| 54 | } |
| 55 | } |
| 56 | return $out; |
| 57 | } |
| 58 | |
| 59 | public static function shape_post( $post, $with_meta = false ) { |
| 60 | $out = [ 'id' => (int) $post->ID, 'title' => $post->post_title, 'slug' => $post->post_name, 'status' => $post->post_status ]; |
| 61 | if ( $with_meta ) { $out['settings'] = self::post_meta_flat( $post->ID ); } |
| 62 | return $out; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Insert/update an asenha_* post and write the supplied meta map. |
| 67 | * |
| 68 | * @return int|WP_Error |
| 69 | */ |
| 70 | public static function upsert( $cpt, $title, $meta, $post_id = null ) { |
| 71 | $postarr = [ 'post_type' => $cpt, 'post_title' => $title, 'post_status' => 'publish' ]; |
| 72 | if ( $post_id ) { |
| 73 | $postarr['ID'] = (int) $post_id; |
| 74 | $id = wp_update_post( $postarr, true ); |
| 75 | } else { |
| 76 | $id = wp_insert_post( $postarr, true ); |
| 77 | } |
| 78 | if ( is_wp_error( $id ) ) { return $id; } |
| 79 | if ( is_array( $meta ) ) { |
| 80 | foreach ( $meta as $k => $v ) { |
| 81 | if ( strpos( (string) $k, '_' ) === 0 ) { continue; } |
| 82 | update_post_meta( $id, $k, $v ); |
| 83 | } |
| 84 | } |
| 85 | return (int) $id; |
| 86 | } |
| 87 | } |
| 88 |