advisor.php
3 months ago
chatbot.php
3 weeks ago
discussions.php
1 week ago
editor-assistant.php
3 months ago
files.php
3 months ago
forms-manager.php
3 months ago
gdpr.php
4 months ago
search.php
3 months ago
security.php
11 months ago
tasks-examples.php
6 months ago
tasks.php
1 month ago
wand.php
3 months ago
forms-manager.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Modules_Forms_Manager { |
| 4 | private $core = null; |
| 5 | |
| 6 | public function __construct( $core ) { |
| 7 | $this->core = $core; |
| 8 | add_action( 'init', [ $this, 'register_post_type' ] ); |
| 9 | add_shortcode( 'mwai_form', [ $this, 'shortcode_render_form' ] ); |
| 10 | } |
| 11 | |
| 12 | public function register_post_type() { |
| 13 | $labels = [ |
| 14 | 'name' => 'AI Forms', |
| 15 | 'singular_name' => 'AI Form', |
| 16 | ]; |
| 17 | |
| 18 | register_post_type( 'mwai_form', [ |
| 19 | 'labels' => $labels, |
| 20 | 'public' => false, |
| 21 | 'show_ui' => true, // Allow native edit screens, but keep it out of the menu |
| 22 | 'show_in_menu' => false, |
| 23 | 'show_in_rest' => true, // Enable Gutenberg data model via REST |
| 24 | 'supports' => [ 'title', 'editor' ], |
| 25 | 'capability_type' => 'post', |
| 26 | 'map_meta_cap' => true, |
| 27 | ] ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * [mwai_form id="123"] |
| 32 | * Render a saved form by ID using the block content and built-in shortcodes. |
| 33 | */ |
| 34 | public function shortcode_render_form( $atts ) { |
| 35 | $atts = shortcode_atts( [ 'id' => 0 ], $atts ); |
| 36 | $post_id = intval( $atts['id'] ); |
| 37 | if ( !$post_id ) { |
| 38 | return ''; |
| 39 | } |
| 40 | |
| 41 | $post = get_post( $post_id ); |
| 42 | if ( !$post || $post->post_type !== 'mwai_form' ) { |
| 43 | return ''; |
| 44 | } |
| 45 | |
| 46 | // Check post status: only published forms are publicly accessible. |
| 47 | // Private/draft forms require the current user to have read permission. |
| 48 | if ( $post->post_status !== 'publish' && !current_user_can( 'read_post', $post_id ) ) { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | // Ensure themes can be enqueued by nested shortcodes if needed |
| 53 | // Actual scripts/styles will be handled by the individual form shortcodes |
| 54 | $content = $post->post_content; |
| 55 | |
| 56 | // Apply blocks and shortcodes |
| 57 | // Let WordPress parse blocks first, then shortcodes within |
| 58 | $content = do_blocks( $content ); |
| 59 | $content = do_shortcode( $content ); |
| 60 | return $content; |
| 61 | } |
| 62 | } |
| 63 |