images
7 years ago
hustle-sendgrid-api-new.php
3 years ago
hustle-sendgrid-api.php
3 years ago
hustle-sendgrid-form-hooks.php
3 years ago
hustle-sendgrid-form-settings.php
3 years ago
hustle-sendgrid.php
3 years ago
sendgrid.php
3 years ago
hustle-sendgrid-form-settings.php
210 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_SendGrid_Form_Settings class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | if ( ! class_exists( 'Hustle_SendGrid_Form_Settings' ) ) : |
| 9 | |
| 10 | /** |
| 11 | * Class Hustle_SendGrid_Form_Settings |
| 12 | * Form Settings SendGrid Process |
| 13 | */ |
| 14 | class Hustle_SendGrid_Form_Settings extends Hustle_Provider_Form_Settings_Abstract { |
| 15 | |
| 16 | /** |
| 17 | * Options that must be set in order to consider the integration as "connected" to the form. |
| 18 | * |
| 19 | * @since 4.2.0 |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $form_completion_options = array( 'selected_global_multi_id', 'list_id', 'list_name' ); |
| 23 | |
| 24 | /** |
| 25 | * For settings Wizard steps |
| 26 | * |
| 27 | * @since 3.0.5 |
| 28 | * @return array |
| 29 | */ |
| 30 | public function form_settings_wizards() { |
| 31 | // already filtered on Abstract |
| 32 | // numerical array steps. |
| 33 | return array( |
| 34 | // 0 |
| 35 | array( |
| 36 | 'callback' => array( $this, 'first_step_callback' ), |
| 37 | 'is_completed' => array( $this, 'first_step_is_completed' ), |
| 38 | ), |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Check if step is completed |
| 44 | * |
| 45 | * @since 3.0.5 |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function first_step_is_completed() { |
| 49 | $this->addon_form_settings = $this->get_form_settings_values(); |
| 50 | if ( ! isset( $this->addon_form_settings['list_id'] ) ) { |
| 51 | // preliminary value. |
| 52 | $this->addon_form_settings['list_id'] = 0; |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if ( empty( $this->addon_form_settings['list_id'] ) ) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns all settings and conditions for 1st step of SendGrid settings |
| 66 | * |
| 67 | * @since 3.0.5 |
| 68 | * @since 4.0 param $validate removed. |
| 69 | * |
| 70 | * @param array $submitted_data Submitted data. |
| 71 | * @return array |
| 72 | */ |
| 73 | public function first_step_callback( $submitted_data ) { |
| 74 | $this->addon_form_settings = $this->get_form_settings_values(); |
| 75 | $current_data = array( |
| 76 | 'list_id' => '', |
| 77 | ); |
| 78 | $current_data = $this->get_current_data( $current_data, $submitted_data ); |
| 79 | |
| 80 | $is_submit = ! empty( $submitted_data['hustle_is_submit'] ); |
| 81 | if ( $is_submit && empty( $submitted_data['list_id'] ) ) { |
| 82 | $error_message = __( 'The email list is required.', 'hustle' ); |
| 83 | } |
| 84 | |
| 85 | $options = $this->get_first_step_options( $current_data ); |
| 86 | |
| 87 | $step_html = Hustle_Provider_Utils::get_integration_modal_title_markup( __( 'Choose your list', 'hustle' ), __( 'Choose the list you want to send form data to.', 'hustle' ) ); |
| 88 | $step_html .= Hustle_Provider_Utils::get_html_for_options( $options ); |
| 89 | |
| 90 | if ( ! isset( $error_message ) ) { |
| 91 | $has_errors = false; |
| 92 | } else { |
| 93 | $step_html .= '<span class="sui-error-message">' . esc_html( $error_message ) . '</span>'; |
| 94 | $has_errors = true; |
| 95 | } |
| 96 | |
| 97 | $buttons = array( |
| 98 | 'disconnect' => array( |
| 99 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 100 | __( 'Disconnect', 'hustle' ), |
| 101 | 'sui-button-ghost', |
| 102 | 'disconnect_form', |
| 103 | true |
| 104 | ), |
| 105 | ), |
| 106 | 'save' => array( |
| 107 | 'markup' => Hustle_Provider_Utils::get_provider_button_markup( |
| 108 | __( 'Save', 'hustle' ), |
| 109 | '', |
| 110 | 'next', |
| 111 | true |
| 112 | ), |
| 113 | ), |
| 114 | ); |
| 115 | |
| 116 | $response = array( |
| 117 | 'html' => $step_html, |
| 118 | 'buttons' => $buttons, |
| 119 | 'has_errors' => $has_errors, |
| 120 | ); |
| 121 | |
| 122 | // Save only after the step has been validated and there are no errors. |
| 123 | if ( $is_submit && ! $has_errors ) { |
| 124 | // Save additional data for submission's entry. |
| 125 | if ( ! empty( $current_data['list_id'] ) ) { |
| 126 | $current_data['list_name'] = ! empty( $this->lists[ $current_data['list_id'] ] ) |
| 127 | ? $this->lists[ $current_data['list_id'] ] . ' (' . $current_data['list_id'] . ')' : $current_data['list_id']; |
| 128 | } |
| 129 | $this->save_form_settings_values( $current_data ); |
| 130 | } |
| 131 | |
| 132 | return $response; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Refresh list array via API |
| 137 | * |
| 138 | * @param object $provider Provider. |
| 139 | * @param string $global_multi_id Global multi ID. |
| 140 | * @return array |
| 141 | */ |
| 142 | public function refresh_global_multi_lists( $provider, $global_multi_id ) { |
| 143 | $api_key = $provider->get_setting( 'api_key', '', $global_multi_id ); |
| 144 | $new_campaigns = $provider->get_setting( 'new_campaigns', '', $global_multi_id ); |
| 145 | $api = $provider::api( $api_key, $new_campaigns ); |
| 146 | |
| 147 | $lists = array(); |
| 148 | $_lists = $api->get_all_lists(); |
| 149 | |
| 150 | if ( is_array( $_lists ) ) { |
| 151 | $lists += wp_list_pluck( $_lists, 'name', 'id' ); |
| 152 | } |
| 153 | |
| 154 | return $lists; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Return an array of options used to display the settings of the 1st step. |
| 159 | * |
| 160 | * @since 4.0 |
| 161 | * |
| 162 | * @param array $submitted_data Submitted data. |
| 163 | * @return array |
| 164 | */ |
| 165 | private function get_first_step_options( $submitted_data ) { |
| 166 | $lists = $this->get_global_multi_lists(); |
| 167 | $this->lists = $lists; |
| 168 | $selected_list = $this->get_selected_list( $submitted_data ); |
| 169 | |
| 170 | $options = array( |
| 171 | array( |
| 172 | 'type' => 'wrapper', |
| 173 | 'style' => 'margin-bottom: 0;', |
| 174 | 'elements' => array( |
| 175 | array( |
| 176 | 'type' => 'label', |
| 177 | 'for' => 'list_id', |
| 178 | 'value' => __( 'Email List', 'hustle' ), |
| 179 | ), |
| 180 | 'wrapper' => array( |
| 181 | 'type' => 'wrapper', |
| 182 | 'class' => 'hui-select-refresh', |
| 183 | 'is_not_field_wrapper' => true, |
| 184 | 'elements' => array( |
| 185 | 'lists' => array( |
| 186 | 'type' => 'select', |
| 187 | 'id' => 'list_id', |
| 188 | 'name' => 'list_id', |
| 189 | 'class' => 'sui-select', |
| 190 | 'value' => $selected_list, |
| 191 | 'selected' => $selected_list, |
| 192 | 'options' => $lists, |
| 193 | ), |
| 194 | 'refresh' => array( |
| 195 | 'type' => 'raw', |
| 196 | 'value' => Hustle_Provider_Utils::get_provider_button_markup( __( 'Refresh', 'hustle' ), '', 'refresh_list', true ), |
| 197 | ), |
| 198 | ), |
| 199 | ), |
| 200 | ), |
| 201 | ), |
| 202 | ); |
| 203 | |
| 204 | return $options; |
| 205 | } |
| 206 | |
| 207 | } // Class end. |
| 208 | |
| 209 | endif; |
| 210 |