newsletter
11 months ago
bubble-chat.php
11 months ago
contact-forms.php
11 months ago
fluent-booking.php
11 months ago
index.php
11 months ago
contact-forms.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Flags; |
| 4 | |
| 5 | add_action( |
| 6 | 'rest_api_init', |
| 7 | function () { |
| 8 | $namespace = 'kubio/v1'; |
| 9 | |
| 10 | register_rest_route( |
| 11 | $namespace, |
| 12 | '/prepare_contact_form_plugin', |
| 13 | array( |
| 14 | 'methods' => 'GET', |
| 15 | 'callback' => 'kubio_api_prepare_contact_form_plugin', |
| 16 | 'permission_callback' => function () { |
| 17 | return current_user_can( 'edit_posts' ); |
| 18 | }, |
| 19 | |
| 20 | ) |
| 21 | ); |
| 22 | |
| 23 | register_rest_route( |
| 24 | $namespace, |
| 25 | '/get_contact_forms', |
| 26 | array( |
| 27 | 'methods' => 'GET', |
| 28 | 'callback' => 'kubio_api_get_contact_forms', |
| 29 | 'permission_callback' => function () { |
| 30 | return current_user_can( 'edit_posts' ); |
| 31 | }, |
| 32 | |
| 33 | ) |
| 34 | ); |
| 35 | } |
| 36 | ); |
| 37 | |
| 38 | |
| 39 | function kubio_api_prepare_contact_form_plugin( WP_REST_Request $request ) { |
| 40 | //in case of failures only try init once |
| 41 | $already_setup = Flags::getSetting( 'contactFormsInstalled', null ); |
| 42 | if ( empty( $already_setup ) ) { |
| 43 | Flags::setSetting( 'contactFormsInstalled', true ); |
| 44 | } |
| 45 | |
| 46 | if ( ! kubio_is_recommendation_contact_form_plugin_active() ) { |
| 47 | wp_send_json_error( __( 'Contact Form plugin is not active!', 'kubio' ), 400 ); |
| 48 | } |
| 49 | |
| 50 | $default_form = null; |
| 51 | |
| 52 | if ( $already_setup ) { |
| 53 | wp_send_json_success( kubio_get_recommendation_contact_forms() ); |
| 54 | } else { |
| 55 | // Create a default contact form |
| 56 | $default_form = kubio_recommendations_create_contact_form(); |
| 57 | if ( ! $default_form ) { |
| 58 | wp_send_json_error( __( 'Failed to create default contact form!', 'kubio' ), 500 ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | ob_start(); |
| 63 | try { |
| 64 | $forms = kubio_get_recommendation_contact_forms(); |
| 65 | |
| 66 | if ( empty( $forms ) ) { |
| 67 | $forms = array(); |
| 68 | } |
| 69 | |
| 70 | $errors = ob_get_clean(); |
| 71 | if ( ! empty( $errors ) ) { |
| 72 | error_log( $errors ); |
| 73 | } |
| 74 | |
| 75 | wp_send_json_success( $forms ); |
| 76 | |
| 77 | } catch ( Exception $e ) { |
| 78 | $errors = ob_get_clean(); |
| 79 | if ( ! empty( $errors ) ) { |
| 80 | error_log( $errors ); |
| 81 | } |
| 82 | wp_send_json_error( $e->getMessage(), 400 ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | function kubio_api_get_contact_forms( WP_REST_Request $request ) { |
| 87 | wp_send_json_success( kubio_get_recommendation_contact_forms() ); |
| 88 | } |
| 89 |