images
7 years ago
activecampaign.php
7 months ago
hustle-activecampaign-api.php
7 months ago
hustle-activecampaign-form-hooks.php
7 months ago
hustle-activecampaign-form-settings.php
7 months ago
hustle-activecampaign.php
3 years ago
hustle-activecampaign-form-settings.php
408 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Activecampaign_Form_Settings class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'Hustle_Activecampaign_Form_Settings' ) ) : |
| 9 | |
| 10 | /** |
| 11 | * Class Hustle_Activecampaign_Form_Settings |
| 12 | * Form Settings ActiveCampaign Process |
| 13 | */ |
| 14 | class Hustle_Activecampaign_Form_Settings extends Hustle_Provider_Form_Settings_Abstract { |
| 15 | |
| 16 | /** |
| 17 | * Whether the lists are empty. |
| 18 | * |
| 19 | * @since unknown |
| 20 | * @var boolean |
| 21 | */ |
| 22 | private $is_empty_lists = false; |
| 23 | |
| 24 | /** |
| 25 | * Whether to retrieve forms or lists. |
| 26 | * |
| 27 | * @since unknwon |
| 28 | * @var bool|string |
| 29 | */ |
| 30 | public $list_type = false; |
| 31 | |
| 32 | /** |
| 33 | * Options that must be set in order to consider the integration as "connected" to the form. |
| 34 | * |
| 35 | * @since 4.2.0 |
| 36 | * @var array |
| 37 | */ |
| 38 | protected $form_completion_options = array( 'selected_global_multi_id' ); |
| 39 | |
| 40 | /** |
| 41 | * For settings Wizard steps |
| 42 | * |
| 43 | * @since 3.0.5 |
| 44 | * @return array |
| 45 | */ |
| 46 | public function form_settings_wizards() { |
| 47 | // already filtered on Abstract |
| 48 | // numerical array steps. |
| 49 | return array( |
| 50 | // 0 |
| 51 | array( |
| 52 | 'callback' => array( $this, 'first_step_callback' ), |
| 53 | 'is_completed' => array( $this, 'first_step_is_completed' ), |
| 54 | ), |
| 55 | // 1 |
| 56 | array( |
| 57 | 'callback' => array( $this, 'second_step_callback' ), |
| 58 | 'is_completed' => array( $this, 'second_step_is_completed' ), |
| 59 | ), |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check if step is completed |
| 65 | * |
| 66 | * @since 3.0.5 |
| 67 | * @return bool |
| 68 | */ |
| 69 | public function first_step_is_completed() { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check if step is completed |
| 75 | * |
| 76 | * @since 4.0 |
| 77 | * @return bool |
| 78 | */ |
| 79 | public function second_step_is_completed() { |
| 80 | $this->addon_form_settings = $this->get_form_settings_values(); |
| 81 | $is_form = isset( $this->addon_form_settings['sign_up_to'] ) && 'form' === $this->addon_form_settings['sign_up_to']; |
| 82 | |
| 83 | if ( $is_form ) { |
| 84 | if ( empty( $this->addon_form_settings['form_id'] ) ) { |
| 85 | // preliminary value. |
| 86 | $this->addon_form_settings['form_id'] = 0; |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | } elseif ( empty( $this->addon_form_settings['list_id'] ) ) { |
| 91 | // preliminary value. |
| 92 | $this->addon_form_settings['list_id'] = 0; |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns all settings and conditions for 1st step of Activecampaign settings |
| 102 | * |
| 103 | * @since 3.0.5 |
| 104 | * @since 4.0 param $validate removed. |
| 105 | * |
| 106 | * @param array $submitted_data Submitted data. |
| 107 | * @return array |
| 108 | */ |
| 109 | public function first_step_callback( $submitted_data ) { |
| 110 | $this->addon_form_settings = $this->get_form_settings_values(); |
| 111 | $current_data = array( |
| 112 | 'sign_up_to' => '', |
| 113 | ); |
| 114 | $current_data = $this->get_current_data( $current_data, $submitted_data ); |
| 115 | |
| 116 | $is_submit = ! empty( $submitted_data['hustle_is_submit'] ); |
| 117 | $options = $this->get_first_step_options( $current_data ); |
| 118 | |
| 119 | $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'ActiveCampaign Forms or Lists', 'hustle' ), '' ); |
| 120 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 121 | |
| 122 | $buttons = array( |
| 123 | 'disconnect' => array( |
| 124 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 125 | __( 'Disconnect', 'hustle' ), |
| 126 | 'sui-button-ghost', |
| 127 | 'disconnect_form', |
| 128 | true |
| 129 | ), |
| 130 | ), |
| 131 | 'save' => array( |
| 132 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 133 | __( 'Continue', 'hustle' ), |
| 134 | '', |
| 135 | 'next', |
| 136 | true |
| 137 | ), |
| 138 | ), |
| 139 | ); |
| 140 | |
| 141 | $response = array( |
| 142 | 'html' => $step_html, |
| 143 | 'buttons' => $buttons, |
| 144 | 'has_errors' => false, |
| 145 | ); |
| 146 | |
| 147 | // Save only after the step has been validated. |
| 148 | if ( $is_submit ) { |
| 149 | $this->save_form_settings_values( $current_data ); |
| 150 | } |
| 151 | |
| 152 | return $response; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Return an array of options used to display the settings of the 1st step. |
| 157 | * |
| 158 | * @since 4.0 |
| 159 | * |
| 160 | * @param array $submitted_data Submitted data. |
| 161 | * @return array |
| 162 | */ |
| 163 | private function get_first_step_options( $submitted_data ) { |
| 164 | $sign_up_to = $submitted_data['sign_up_to']; |
| 165 | |
| 166 | $options = array( |
| 167 | array( |
| 168 | 'type' => 'wrapper', |
| 169 | 'elements' => array( |
| 170 | 'opt_in' => array( |
| 171 | 'type' => 'checkbox', |
| 172 | 'name' => 'sign_up_to', |
| 173 | 'value' => 'form', |
| 174 | 'id' => 'sign_up_to', |
| 175 | 'class' => 'sui-checkbox-sm', |
| 176 | 'attributes' => array( |
| 177 | 'checked' => 'form' === $sign_up_to ? 'checked' : '', |
| 178 | ), |
| 179 | 'label' => __( 'Enable to choose from your existing forms instead of your existing lists.', 'hustle' ), |
| 180 | ), |
| 181 | ), |
| 182 | ), |
| 183 | array( |
| 184 | 'type' => 'wrapper', |
| 185 | 'style' => 'margin-bottom: 0;', |
| 186 | 'elements' => array( |
| 187 | array( |
| 188 | 'type' => 'notice', |
| 189 | 'icon' => 'info', |
| 190 | 'value' => esc_html__( 'Double opt-in is only available when using forms.', 'hustle' ), |
| 191 | 'class' => 'sui-notice-warning', |
| 192 | ), |
| 193 | ), |
| 194 | ), |
| 195 | ); |
| 196 | |
| 197 | return $options; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * Returns all settings and conditions for 2st step of Activecampaign settings |
| 203 | * |
| 204 | * @since 3.0.5 |
| 205 | * @since 4.0 param $validate removed. |
| 206 | * |
| 207 | * @param array $submitted_data Submitted data. |
| 208 | * @return array |
| 209 | */ |
| 210 | public function second_step_callback( $submitted_data ) { |
| 211 | $this->addon_form_settings = $this->get_form_settings_values( false ); |
| 212 | $sign_up_to = isset( $this->addon_form_settings['sign_up_to'] ) ? $this->addon_form_settings['sign_up_to'] : ''; |
| 213 | $form = 'form' === $sign_up_to; |
| 214 | $list_id = $form ? 'form_id' : 'list_id'; |
| 215 | $list_name = $form ? 'form_name' : 'list_name'; |
| 216 | $current_data = array( |
| 217 | $list_id => '', |
| 218 | ); |
| 219 | |
| 220 | $current_data = $this->get_current_data( $current_data, $submitted_data ); |
| 221 | $is_submit = ! empty( $submitted_data['hustle_is_submit'] ); |
| 222 | |
| 223 | if ( $is_submit && empty( $submitted_data[ $list_id ] ) ) { |
| 224 | $error_message = $form ? __( 'The form is required.', 'hustle' ) : __( 'The email list is required.', 'hustle' ); |
| 225 | } |
| 226 | |
| 227 | $options = $this->get_second_step_options( $current_data, $form ); |
| 228 | |
| 229 | $step_html = $form |
| 230 | ? Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Choose your form', 'hustle' ), __( 'Choose the form you want to send form data to.', 'hustle' ) ) |
| 231 | : Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Choose your list', 'hustle' ), __( 'Choose the list you want to send form data to.', 'hustle' ) ); |
| 232 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 233 | |
| 234 | if ( ! isset( $error_message ) ) { |
| 235 | $has_errors = false; |
| 236 | } else { |
| 237 | $step_html .= '<span class="sui-error-message">' . esc_html( $error_message ) . '</span>'; |
| 238 | $has_errors = true; |
| 239 | } |
| 240 | |
| 241 | $buttons = array( |
| 242 | 'cancel' => array( |
| 243 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 244 | __( 'Back', 'hustle' ), |
| 245 | '', |
| 246 | 'prev', |
| 247 | true |
| 248 | ), |
| 249 | ), |
| 250 | 'save' => array( |
| 251 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 252 | __( 'Save', 'hustle' ), |
| 253 | '', |
| 254 | 'next', |
| 255 | true, |
| 256 | $this->is_empty_lists |
| 257 | ), |
| 258 | ), |
| 259 | ); |
| 260 | |
| 261 | $response = array( |
| 262 | 'html' => $step_html, |
| 263 | 'buttons' => $buttons, |
| 264 | 'has_errors' => $has_errors, |
| 265 | ); |
| 266 | |
| 267 | // Save only after the step has been validated and there are no errors. |
| 268 | if ( $is_submit && ! $has_errors ) { |
| 269 | // Save additional data for submission's entry. |
| 270 | if ( ! empty( $current_data[ $list_id ] ) ) { |
| 271 | $current_data[ $list_name ] = ! empty( $this->lists[ $current_data[ $list_id ] ] ) |
| 272 | ? $this->lists[ $current_data[ $list_id ] ] . ' (' . $current_data[ $list_id ] . ')' : $current_data[ $list_id ]; |
| 273 | } |
| 274 | $this->save_form_settings_values( $current_data ); |
| 275 | } |
| 276 | |
| 277 | return $response; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Refresh list array via API |
| 282 | * |
| 283 | * @param object $provider Provider. |
| 284 | * @param string $global_multi_id Global multi ID. |
| 285 | * @return array |
| 286 | */ |
| 287 | public function refresh_global_multi_lists( $provider, $global_multi_id ) { |
| 288 | $api_url = $provider->get_setting( 'api_url', '', $global_multi_id ); |
| 289 | $api_key = $provider->get_setting( 'api_key', '', $global_multi_id ); |
| 290 | $api = $provider::api( $api_url, $api_key ); |
| 291 | |
| 292 | $lists = array(); |
| 293 | |
| 294 | // Retrieve lists if "sign_up_to" is not set to "forms". |
| 295 | if ( ! $this->list_type || 'forms' !== $this->list_type ) { |
| 296 | $_lists = $api->get_lists(); |
| 297 | } else { |
| 298 | // Retrieve forms otherwise. |
| 299 | $_lists = $api->get_forms(); |
| 300 | } |
| 301 | |
| 302 | if ( ! empty( $_lists ) ) { |
| 303 | $lists = wp_list_pluck( $_lists, 'name', 'id' ); |
| 304 | } |
| 305 | |
| 306 | return $lists; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Return an array of options used to display the settings of the 2st step. |
| 311 | * |
| 312 | * @since 4.0 |
| 313 | * |
| 314 | * @param array $submitted_data Submitted data. |
| 315 | * @param bool $is_form Is Form. |
| 316 | * @return array |
| 317 | */ |
| 318 | private function get_second_step_options( $submitted_data, $is_form ) { |
| 319 | $this->list_type = $is_form ? 'forms' : false; |
| 320 | $id = ! $is_form ? 'list_id' : 'form_id'; |
| 321 | $cache_key = ! $is_form ? 'lists' : 'forms'; |
| 322 | $lists = $this->get_global_multi_lists( false, false, $cache_key ); |
| 323 | $this->lists = $lists; |
| 324 | $selected_list = $this->get_selected_list( $submitted_data, $id ); |
| 325 | |
| 326 | $this->is_empty_lists = empty( $lists ); |
| 327 | |
| 328 | if ( empty( $lists ) ) { |
| 329 | |
| 330 | $empty_list_error = ! $is_form ? esc_html__( "You can't sync this provider because your account doesn't have any email list added. Please, go to your ActiveCampaign account to add one before retrying.", 'hustle' ) |
| 331 | : esc_html__( "You don't have any form added to your account to sync here.", 'hustle' ); |
| 332 | |
| 333 | $options = array( |
| 334 | array( |
| 335 | 'type' => 'wrapper', |
| 336 | 'style' => 'margin-bottom: 0;', |
| 337 | 'elements' => array( |
| 338 | 'options' => array( |
| 339 | 'type' => 'notice', |
| 340 | 'icon' => 'info', |
| 341 | 'class' => 'sui-notice-error', |
| 342 | 'value' => $empty_list_error, |
| 343 | ), |
| 344 | ), |
| 345 | ), |
| 346 | ); |
| 347 | } else { |
| 348 | $options = array( |
| 349 | array( |
| 350 | 'type' => 'wrapper', |
| 351 | 'style' => 'margin-bottom: 0;', |
| 352 | 'elements' => array( |
| 353 | 'label' => array( |
| 354 | 'type' => 'label', |
| 355 | 'for' => $id, |
| 356 | 'value' => ! $is_form ? __( 'Email List', 'hustle' ) : __( 'Choose Form', 'hustle' ), |
| 357 | ), |
| 358 | 'wrapper' => array( |
| 359 | 'type' => 'wrapper', |
| 360 | 'class' => 'hui-select-refresh', |
| 361 | 'is_not_field_wrapper' => true, |
| 362 | 'elements' => array( |
| 363 | 'lists' => array( |
| 364 | 'type' => 'select', |
| 365 | 'id' => $id, |
| 366 | 'class' => 'sui-select', |
| 367 | 'name' => $id, |
| 368 | 'value' => $selected_list, |
| 369 | 'options' => $lists, |
| 370 | 'selected' => $selected_list, |
| 371 | ), |
| 372 | 'refresh' => array( |
| 373 | 'type' => 'raw', |
| 374 | 'value' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Refresh', 'hustle' ), '', 'refresh_list', true ), |
| 375 | ), |
| 376 | ), |
| 377 | ), |
| 378 | ), |
| 379 | ), |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | return $options; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Get the options that should be completed for the form. |
| 388 | * |
| 389 | * @since 4.2.0 |
| 390 | * @param bool $saved_form_settings Form settings. |
| 391 | * @return array |
| 392 | */ |
| 393 | public function get_form_completion_options( $saved_form_settings ) { |
| 394 | |
| 395 | if ( 'form' === $saved_form_settings['sign_up_to'] ) { |
| 396 | $this->form_completion_options[] = 'form_id'; |
| 397 | $this->form_completion_options[] = 'form_name'; |
| 398 | } else { |
| 399 | $this->form_completion_options[] = 'list_id'; |
| 400 | $this->form_completion_options[] = 'list_name'; |
| 401 | } |
| 402 | |
| 403 | return $this->form_completion_options; |
| 404 | } |
| 405 | } // Class end. |
| 406 | |
| 407 | endif; |
| 408 |