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
bubble-chat.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | use \Kubio\Flags; |
| 5 | use Kubio\Core\LodashBasic; |
| 6 | |
| 7 | add_action( |
| 8 | 'rest_api_init', |
| 9 | function () { |
| 10 | $namespace = 'kubio/v1'; |
| 11 | |
| 12 | register_rest_route( |
| 13 | $namespace, |
| 14 | '/prepare_bubble_chat_plugin', |
| 15 | array( |
| 16 | 'methods' => 'GET', |
| 17 | 'callback' => 'kubio_api_prepare_bubble_chat_plugin', |
| 18 | 'permission_callback' => function () { |
| 19 | return current_user_can( 'edit_posts' ); |
| 20 | }, |
| 21 | |
| 22 | ) |
| 23 | ); |
| 24 | |
| 25 | register_rest_route( |
| 26 | $namespace, |
| 27 | '/get_bubble_chat_widgets', |
| 28 | array( |
| 29 | 'methods' => 'GET', |
| 30 | 'callback' => 'kubio_api_get_bubble_chat_widgets', |
| 31 | 'permission_callback' => function () { |
| 32 | return current_user_can( 'edit_posts' ); |
| 33 | }, |
| 34 | |
| 35 | ) |
| 36 | ); |
| 37 | |
| 38 | register_rest_route( |
| 39 | $namespace, |
| 40 | '/update_bubble_chat_widget_data', |
| 41 | array( |
| 42 | 'methods' => 'POST', |
| 43 | 'callback' => 'kubio_api_update_bubble_chat_widget_data', |
| 44 | 'permission_callback' => function () { |
| 45 | return current_user_can( 'edit_posts' ); |
| 46 | }, |
| 47 | |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | ); |
| 52 | |
| 53 | |
| 54 | |
| 55 | function kubio_api_prepare_bubble_chat_plugin( WP_REST_Request $request ) { |
| 56 | |
| 57 | //in case of failures only try init once |
| 58 | $already_setup = Flags::getSetting( 'bubbleChatInstalled', null ); |
| 59 | if(empty($already_setup)) { |
| 60 | Flags::setSetting( "bubbleChatInstalled", true ); |
| 61 | } |
| 62 | |
| 63 | if(!kubio_is_bubble_chat_plugin_active()) { |
| 64 | wp_send_json_error(__('Required plugin is missing', 'kubio'), 400); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | if ( ! class_exists( '\FloatingContact\Features\Widgets\FCWidget' ) |
| 69 | || ! method_exists( '\FloatingContact\Features\Widgets\FCWidget', 'createFromTemplate' )) { |
| 70 | wp_send_json_error(__('Required class or functions are missing', 'kubio'), 400); |
| 71 | } |
| 72 | |
| 73 | if ( $already_setup ) { |
| 74 | wp_send_json_success(kubio_get_bubble_chat_widgets()); |
| 75 | } |
| 76 | |
| 77 | ob_start(); |
| 78 | |
| 79 | |
| 80 | try { |
| 81 | $widgets = kubio_get_bubble_chat_widgets(); |
| 82 | |
| 83 | //in case the widgets are already created |
| 84 | if(!empty($widgets)) { |
| 85 | |
| 86 | $errors = ob_get_clean(); |
| 87 | if(!empty($errors)) { |
| 88 | error_log($errors); |
| 89 | } |
| 90 | |
| 91 | return $widgets; |
| 92 | } |
| 93 | |
| 94 | \FloatingContact\Features\Widgets\FCWidget::createFromTemplate([ |
| 95 | 'name' => __('Kubio Widget', 'kubio') |
| 96 | ]); |
| 97 | |
| 98 | $result = kubio_get_bubble_chat_widgets(); |
| 99 | |
| 100 | $errors = ob_get_clean(); |
| 101 | if(!empty($errors)) { |
| 102 | error_log($errors); |
| 103 | } |
| 104 | |
| 105 | wp_send_json_success($result); |
| 106 | |
| 107 | } catch ( Exception $e ) { |
| 108 | $errors = ob_get_clean(); |
| 109 | if(!empty($errors)) { |
| 110 | error_log($errors); |
| 111 | } |
| 112 | wp_send_json_error($e->getMessage(), 400); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | function kubio_api_get_bubble_chat_widgets( WP_REST_Request $request ) { |
| 118 | wp_send_json_success(kubio_get_bubble_chat_widgets()); |
| 119 | } |
| 120 | function kubio_api_update_bubble_chat_widget_data( WP_REST_Request $request ) { |
| 121 | if(!kubio_is_bubble_chat_plugin_active()) { |
| 122 | wp_send_json_error(__('Required plugin is missing', 'kubio'), 400); |
| 123 | } |
| 124 | if ( ! class_exists( '\FloatingContact\Features\Widgets\FCWidget' ) || |
| 125 | ! method_exists( '\FloatingContact\Features\Widgets\FCWidget', 'findById' ) || |
| 126 | ! method_exists( '\FloatingContact\Features\Widgets\FCWidget', 'updateParams' ) |
| 127 | ) { |
| 128 | wp_send_json_error(__('Required class or functions are missing', 'kubio'), 400); |
| 129 | } |
| 130 | |
| 131 | try { |
| 132 | $id = $request->get_param( 'widgetId' ); |
| 133 | $params = $request->get_param( 'params' ); |
| 134 | $phoneNr = LodashBasic::get($params, 'phone.phoneNr'); |
| 135 | $whatsappNr = LodashBasic::get($params, 'whatsapp.phoneNr'); |
| 136 | $widgetParams = [ |
| 137 | 'phone' => [ |
| 138 | 'phoneNr' => $phoneNr |
| 139 | ], |
| 140 | 'whatsapp' => [ |
| 141 | 'phoneNr' => $whatsappNr |
| 142 | ] |
| 143 | ]; |
| 144 | ob_start(); |
| 145 | try { |
| 146 | $widget = \FloatingContact\Features\Widgets\FCWidget::findById($id); |
| 147 | |
| 148 | if(!$widget) { |
| 149 | wp_send_json_error(__('Widget not found'), 404); |
| 150 | } |
| 151 | $widget->updateParams($widgetParams); |
| 152 | |
| 153 | wp_send_json_success(); |
| 154 | } catch(\Exception $e) { |
| 155 | wp_send_json_error($e->getMessage(), 400); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | } catch ( Exception $e ) { |
| 161 | wp_send_json_error($e->getMessage(), 400); |
| 162 | } |
| 163 | } |
| 164 |