newsletter
3 months ago
contact-forms.php
3 months ago
fluent-booking.php
3 months ago
iconvert-email-marketer.php
6 days ago
index.php
6 days ago
siteleads.php
3 months ago
siteleads.php
245 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_siteleads_plugin', |
| 15 | array( |
| 16 | 'methods' => 'POST', |
| 17 | 'callback' => 'kubio_api_prepare_siteleads_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_siteleads_widgets', |
| 28 | array( |
| 29 | 'methods' => 'GET', |
| 30 | 'callback' => 'kubio_api_get_siteleads_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_siteleads_widget_data', |
| 41 | array( |
| 42 | 'methods' => 'POST', |
| 43 | 'callback' => 'kubio_api_update_siteleads_widget_data', |
| 44 | 'permission_callback' => function () { |
| 45 | return current_user_can( 'edit_posts' ); |
| 46 | }, |
| 47 | |
| 48 | ) |
| 49 | ); |
| 50 | |
| 51 | register_rest_route( |
| 52 | $namespace, |
| 53 | '/toggle_enable_siteleads_widget', |
| 54 | array( |
| 55 | 'methods' => 'POST', |
| 56 | 'callback' => 'kubio_api_toggle_enable_siteleads_widget', |
| 57 | 'permission_callback' => function () { |
| 58 | return current_user_can( 'edit_posts' ); |
| 59 | }, |
| 60 | |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | ); |
| 65 | |
| 66 | |
| 67 | |
| 68 | function kubio_api_prepare_siteleads_plugin( WP_REST_Request $request ) { |
| 69 | |
| 70 | if(!kubio_is_siteleads_plugin_active()) { |
| 71 | wp_send_json_error(__( 'Required plugin is missing', 'kubio' ), 400); |
| 72 | } |
| 73 | if ( ! class_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager' ) || |
| 74 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'createDefaultWidgetWithOnlyPhoneChannel' ) || |
| 75 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'createDefaultWidgetWithOnlyWhatsappChanel' ) || |
| 76 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'createDefaultWidgetWithPhoneWhatsappAndEmail' ) || |
| 77 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'getDefaultWidgetAlreadyCreated' ) |
| 78 | ) { |
| 79 | wp_send_json_error(__( 'Required class or functions are missing', 'kubio' ), 400); |
| 80 | |
| 81 | } |
| 82 | |
| 83 | $type = sanitize_text_field( $request->get_param( 'type' ) ); |
| 84 | |
| 85 | $allowed = array( 'phone', 'whatsapp', 'phoneWhatsappEmail' ); |
| 86 | |
| 87 | if ( ! in_array( $type, $allowed, true ) ) { |
| 88 | wp_send_json_error(__( 'Invalid widget type.', 'kubio' ), 400); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | //in case of failures only try init once |
| 93 | $already_setup = Flags::getSetting( 'siteleadsInstalled', null ); |
| 94 | |
| 95 | |
| 96 | if ( $already_setup ) { |
| 97 | wp_send_json_success(kubio_get_siteleads_widgets()); |
| 98 | } |
| 99 | Flags::setSetting( "siteleadsInstalled", true ); |
| 100 | try { |
| 101 | |
| 102 | $site_leads_inited = \SiteLeads\Features\Widgets\FCWidgetsManager::getDefaultWidgetAlreadyCreated(); |
| 103 | $widgets = kubio_get_siteleads_widgets(); |
| 104 | //in case the widgets are already created return those |
| 105 | if(!empty($widgets)) { |
| 106 | wp_send_json_success($widgets); |
| 107 | } |
| 108 | $start_source = sanitize_text_field($request->get_param( 'start_source' ) ); |
| 109 | $options = []; |
| 110 | if(!empty($start_source) && is_string($start_source)) { |
| 111 | $options['start_source'] = $start_source; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | //if siteleads was inited already skip this |
| 116 | if(!$site_leads_inited) { |
| 117 | Flags::set('siteLeadsInitedFromKubio', true); |
| 118 | switch ($type) { |
| 119 | case 'phone': |
| 120 | \SiteLeads\Features\Widgets\FCWidgetsManager::createDefaultWidgetWithOnlyPhoneChannel('',$options); |
| 121 | break; |
| 122 | case 'whatsapp': |
| 123 | \SiteLeads\Features\Widgets\FCWidgetsManager::createDefaultWidgetWithOnlyWhatsappChanel('',$options); |
| 124 | break; |
| 125 | case 'phoneWhatsappEmail': |
| 126 | \SiteLeads\Features\Widgets\FCWidgetsManager::createDefaultWidgetWithPhoneWhatsappAndEmail($options); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | $result = kubio_get_siteleads_widgets(); |
| 131 | |
| 132 | wp_send_json_success($result); |
| 133 | |
| 134 | } catch ( Exception $e ) { |
| 135 | wp_send_json_error($e->getMessage(), 400); |
| 136 | |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | function kubio_api_get_siteleads_widgets( WP_REST_Request $request ) { |
| 142 | wp_send_json_success(kubio_get_siteleads_widgets()); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | function kubio_api_update_siteleads_widget_data( WP_REST_Request $request ) { |
| 147 | if(!kubio_is_siteleads_plugin_active()) { |
| 148 | |
| 149 | wp_send_json_error(__( 'Required plugin is missing', 'kubio' ), 400); |
| 150 | } |
| 151 | if ( ! class_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager' ) || |
| 152 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'updateWidgetPhoneNumberAfterCreation' ) || |
| 153 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'updateWidgetWhatsappNumberAfterCreation' ) |
| 154 | ) { |
| 155 | |
| 156 | wp_send_json_error(__( 'Required class or functions are missing', 'kubio' ), 400); |
| 157 | } |
| 158 | |
| 159 | $site_leads_inited_from_kubio = Flags::get('siteLeadsInitedFromKubio'); |
| 160 | |
| 161 | //if SiteLeads was inited from another product don't sync phone data |
| 162 | if(!$site_leads_inited_from_kubio) { |
| 163 | wp_send_json_success( true ); |
| 164 | } |
| 165 | $widgetId = sanitize_text_field( $request->get_param( 'widgetId' ) ); |
| 166 | |
| 167 | if ( empty( $widgetId ) ) { |
| 168 | wp_send_json_error(__( 'Invalid widget ID', 'kubio' ), 400); |
| 169 | } |
| 170 | $params = $request->get_param( 'params' ); |
| 171 | |
| 172 | if ( ! is_array( $params ) ) { |
| 173 | wp_send_json_error(__( 'Invalid params format', 'kubio' ), 400); |
| 174 | } |
| 175 | try { |
| 176 | |
| 177 | |
| 178 | $isPhoneType = LodashBasic::has($params, 'phone'); |
| 179 | |
| 180 | $isWhatsappType = LodashBasic::has($params, 'whatsapp'); |
| 181 | $phoneNr = sanitize_text_field( |
| 182 | LodashBasic::get( $params, 'phone.phoneNr', '' ) |
| 183 | ); |
| 184 | |
| 185 | $whatsappNr = sanitize_text_field( |
| 186 | LodashBasic::get( $params, 'whatsapp.phoneNr', '' ) |
| 187 | ); |
| 188 | |
| 189 | if($isPhoneType && !Flags::get('siteLeadsPhoneUpdatedFromKubio')) { |
| 190 | \SiteLeads\Features\Widgets\FCWidgetsManager::updateWidgetPhoneNumberAfterCreation($widgetId, $phoneNr ); |
| 191 | Flags::set('siteLeadsPhoneUpdatedFromKubio', true); |
| 192 | } |
| 193 | if($isWhatsappType && !Flags::get('siteLeadsWhatsappUpdatedFromKubio')) { |
| 194 | \SiteLeads\Features\Widgets\FCWidgetsManager::updateWidgetWhatsappNumberAfterCreation($widgetId, $whatsappNr ); |
| 195 | Flags::set('siteLeadsWhatsappUpdatedFromKubio', true); |
| 196 | } |
| 197 | |
| 198 | wp_send_json_success( true ); |
| 199 | |
| 200 | } catch ( Exception $e ) { |
| 201 | |
| 202 | wp_send_json_error($e->getMessage(), 400); |
| 203 | |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | |
| 208 | function kubio_api_toggle_enable_siteleads_widget( WP_REST_Request $request ) |
| 209 | { |
| 210 | if (!kubio_is_siteleads_plugin_active()) { |
| 211 | |
| 212 | wp_send_json_error(__( 'Required plugin is missing', 'kubio' ), 400); |
| 213 | } |
| 214 | if ( ! class_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager' ) || |
| 215 | ! method_exists( '\SiteLeads\Features\Widgets\FCWidgetsManager', 'toggleWidgetEnabled' ) |
| 216 | ) { |
| 217 | wp_send_json_error(__( 'Required class or functions are missing', 'kubio' ), 400); |
| 218 | |
| 219 | } |
| 220 | |
| 221 | $widget_id = sanitize_text_field( $request->get_param( 'widgetId' ) ); |
| 222 | |
| 223 | if ( empty( $widget_id ) ) { |
| 224 | |
| 225 | wp_send_json_error(__( 'Missing widgetId parameter.', 'kubio' ), 400); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | $enabled = rest_sanitize_boolean( $request->get_param( 'enabled' ) ); |
| 230 | |
| 231 | if ( ! $request->has_param( 'enabled' ) ) { |
| 232 | wp_send_json_error(__( 'Missing enabled parameter.', 'kubio' ), 400); |
| 233 | } |
| 234 | try { |
| 235 | |
| 236 | \SiteLeads\Features\Widgets\FCWidgetsManager::toggleWidgetEnabled($widget_id, $enabled); |
| 237 | |
| 238 | wp_send_json_success( true ); |
| 239 | } |
| 240 | catch(\Exception $e) { |
| 241 | wp_send_json_error($e->getMessage(), 400); |
| 242 | |
| 243 | } |
| 244 | } |
| 245 |