s->get_mapped_field( $record, 'name' ), ]; $subscriber['fields'] = $this->get_mailerlite_custom_fields( $record ); // Allow re-subscribe $allow_resubscribe = $record->get_form_settings( 'allow_resubscribe' ); if ( ! empty( $allow_resubscribe ) && 'yes' === $allow_resubscribe ) { $subscriber['resubscribe'] = true; } return $subscriber; } /** * @param Form_Record $record * * @return array */ private function get_mailerlite_custom_fields( Form_Record $record ) { $custom_fields = []; $form_fields = $record->get( 'fields' ); $field_mapping = $record->get_form_settings( 'mailerlite_fields_map' ); foreach ( $field_mapping as $map_item ) { if ( in_array( $map_item['remote_id'], [ 'email', 'name' ] ) ) { continue; } if ( empty( $map_item['local_id'] ) ) { continue; } foreach ( $form_fields as $id => $field ) { if ( $id !== $map_item['local_id'] ) { continue; } $custom_fields[ $map_item['remote_id'] ] = $field['value']; } } return $custom_fields; } private function get_mapped_field( Form_Record $record, $field_id ) { $fields = $record->get( 'fields' ); foreach ( $record->get_form_settings( 'mailerlite_fields_map' ) as $map_item ) { if ( empty( $fields[ $map_item['local_id'] ]['value'] ) ) { continue; } if ( $field_id === $map_item['remote_id'] ) { return $fields[ $map_item['local_id'] ]['value']; } } return ''; } public function handle_panel_request( array $data ) { if ( ! empty( $data['api_key'] ) && 'default' === $data['api_key'] ) { $api_key = $this->get_global_api_key(); } elseif ( ! empty( $data['custom_api_key'] ) ) { $api_key = $data['custom_api_key']; } if ( empty( $api_key ) ) { throw new \Exception( '`api_key` is required.', 400 ); } $handler = new Mailerlite_Handler( $api_key ); if ( 'groups' === $data['mailerlite_action'] ) { return $handler->get_groups(); } } public function register_admin_fields( Settings $settings ) { $settings->add_section( Settings::TAB_INTEGRATIONS, 'mailerlite', [ 'callback' => function() { echo '

' . esc_html__( 'MailerLite', 'elementor-pro' ) . '

'; }, 'fields' => [ self::OPTION_NAME_API_KEY => [ 'label' => esc_html__( 'API Key', 'elementor-pro' ), 'field_args' => [ 'type' => 'text', 'desc' => sprintf( /* translators: 1: Link opening tag, 2: Link closing tag. */ esc_html__( 'To integrate with our forms you need an %1$sAPI Key%2$s.', 'elementor-pro' ), '', '' ), ], ], 'validate_api_data' => [ 'field_args' => [ 'type' => 'raw_html', 'html' => sprintf( '', self::OPTION_NAME_API_KEY . '_validate', wp_create_nonce( self::OPTION_NAME_API_KEY ), esc_html__( 'Validate API Key', 'elementor-pro' ) ), ], ], ], ] ); } public function ajax_validate_api_key() { check_ajax_referer( self::OPTION_NAME_API_KEY, '_nonce' ); if ( ! isset( $_POST['api_key'] ) ) { wp_send_json_error(); } if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Permission denied' ); } try { new Mailerlite_Handler( $_POST['api_key'] ); // phpcs:ignore -- No need to sanitize to support special characters. } catch ( \Exception $exception ) { wp_send_json_error(); } wp_send_json_success(); } public function __construct() { if ( is_admin() ) { add_action( 'elementor/admin/after_create_settings/' . Settings::PAGE_ID, [ $this, 'register_admin_fields' ], 15 ); } add_action( 'wp_ajax_' . self::OPTION_NAME_API_KEY . '_validate', [ $this, 'ajax_validate_api_key' ] ); } protected function get_fields_map_control_options() { return [ 'condition' => [ 'mailerlite_group!' => '', ], ]; } }