active-campaign-action.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Active_Campaign\Actions; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Action_Handler; |
| 6 | use Jet_Form_Builder\Actions\Types\Base; |
| 7 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 8 | use JFB_Modules\Active_Campaign\Methods\Contact_Modifier; |
| 9 | use JFB_Modules\Active_Campaign\Rest_Api\Editor_Fetch_Endpoint; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | if ( ! defined( 'WPINC' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Define Base_Type class |
| 18 | */ |
| 19 | class Active_Campaign_Action extends Base { |
| 20 | |
| 21 | public $option_name = 'active-campaign-tab'; |
| 22 | |
| 23 | public function get_name() { |
| 24 | return __( 'ActiveCampaign', 'jet-form-builder' ); |
| 25 | } |
| 26 | |
| 27 | public function get_id() { |
| 28 | return 'active_campaign'; |
| 29 | } |
| 30 | |
| 31 | public function action_attributes() { |
| 32 | return array( |
| 33 | 'api_key' => array( |
| 34 | 'default' => '', |
| 35 | 'path' => 'activecampaign/api_key', |
| 36 | ), |
| 37 | 'api_url' => array( |
| 38 | 'default' => '', |
| 39 | 'path' => 'activecampaign/api_url', |
| 40 | ), |
| 41 | 'lists' => array( |
| 42 | 'default' => '', |
| 43 | 'path' => 'activecampaign/lists', |
| 44 | ), |
| 45 | 'fields_map' => array( |
| 46 | 'default' => '', |
| 47 | 'path' => 'activecampaign/fields_map', |
| 48 | ), |
| 49 | 'isValidAPI' => array( |
| 50 | 'default' => '', |
| 51 | 'path' => 'activecampaign/isValidAPI', |
| 52 | ), |
| 53 | 'list_id' => array( |
| 54 | 'default' => '', |
| 55 | 'path' => 'activecampaign/list_id', |
| 56 | ), |
| 57 | 'tags' => array( |
| 58 | 'default' => '', |
| 59 | 'path' => 'activecampaign/tags', |
| 60 | ), |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Run a hook notification |
| 66 | * |
| 67 | * @param array $request |
| 68 | * @param Action_Handler $handler |
| 69 | * |
| 70 | * @return void |
| 71 | * @throws Action_Exception |
| 72 | */ |
| 73 | public function do_action( array $request, Action_Handler $handler ) { |
| 74 | $settings = $this->global_settings( |
| 75 | array( |
| 76 | 'api_url' => '', |
| 77 | 'api_key' => '', |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | $list_id = $this->settings['list_id'] ?? 0; |
| 82 | $tags = $this->settings['tags'] ?? ''; |
| 83 | $fields_map = $this->settings['fields_map'] ?? array(); |
| 84 | |
| 85 | ( new Contact_Modifier() ) |
| 86 | ->set_request( $request ) |
| 87 | ->set_fields_map( $fields_map, true ) |
| 88 | ->set_api_key( $settings['api_key'] ) |
| 89 | ->set_api_url( $settings['api_url'] ) |
| 90 | ->set( '#list', $list_id ) |
| 91 | ->set( '#tags', $tags ) |
| 92 | ->run(); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | public function editor_labels() { |
| 97 | return array( |
| 98 | 'api_data' => __( 'API Data', 'jet-form-builder' ), |
| 99 | 'api_key' => __( 'API Key', 'jet-form-builder' ), |
| 100 | 'api_url' => __( 'API URL', 'jet-form-builder' ), |
| 101 | 'validate_api_key' => __( 'Validate API Key', 'jet-form-builder' ), |
| 102 | 'retry_request' => __( 'Retry request', 'jet-form-builder' ), |
| 103 | 'list_id' => __( 'List Id:', 'jet-form-builder' ), |
| 104 | 'update_list_ids' => __( 'Update List', 'jet-form-builder' ), |
| 105 | 'tags' => __( 'Tags:', 'jet-form-builder' ), |
| 106 | 'fields_map' => __( 'Fields Map:', 'jet-form-builder' ), |
| 107 | 'use_global' => __( 'Use Global Settings', 'jet-form-builder' ), |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function editor_labels_help() { |
| 112 | return array( |
| 113 | 'api_key_link_prefix' => __( 'How to obtain your ActiveCampaign API URL and Key? More info', 'jet-form-builder' ), |
| 114 | 'api_key_link_suffix' => __( 'here', 'jet-form-builder' ), |
| 115 | 'api_key_link' => 'https://help.activecampaign.com/hc/en-us/articles/207317590-Getting-started-with-the-API', |
| 116 | 'tags' => __( 'Add as many tags as you want, comma separated.', 'jet-form-builder' ), |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Register custom action data for the editor |
| 122 | * |
| 123 | * @return array [description] |
| 124 | */ |
| 125 | public function action_data() { |
| 126 | return array( |
| 127 | 'fetch' => array( |
| 128 | 'url' => Editor_Fetch_Endpoint::rest_url(), |
| 129 | 'method' => Editor_Fetch_Endpoint::get_methods(), |
| 130 | ), |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns ActiveCampaign fields |
| 136 | * |
| 137 | * @return array |
| 138 | */ |
| 139 | private function get_fields() { |
| 140 | return apply_filters( |
| 141 | 'jet-form-builder/action/activecampaign/fields', |
| 142 | array( |
| 143 | 'email' => __( 'Email', 'jet-form-builder' ), |
| 144 | 'first_name' => __( 'First Name', 'jet-form-builder' ), |
| 145 | 'last_name' => __( 'Last Name', 'jet-form-builder' ), |
| 146 | 'phone' => __( 'Phone', 'jet-form-builder' ), |
| 147 | 'customer_acct_name' => __( 'Organization', 'jet-form-builder' ), |
| 148 | ) |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | } |
| 154 |