'dkgfsib-free-admin-style', 'src' => sprintf('%1$s/assets/css/%2$s', DKGFSIB_FREE_URL, defined('WPC_USE_UNMINIFIED_ASSETS') && WPC_USE_UNMINIFIED_ASSETS ? 'admin.css' : 'admin.min.css'), 'version' => $this->_version . '~' . DKGFSIB_FREE_ADDON_CACHE_BUSTER, 'enqueue' => array('admin_page' => array('form_settings', 'plugin_settings')), ), ); return array_merge(parent::styles(), $styles); } public function scripts() { $scripts = array( array( 'handle' => 'dkgfsib-free-admin-script', 'src' => sprintf('%1$s/assets/js/%2$s', DKGFSIB_FREE_URL, defined('WPC_USE_UNMINIFIED_ASSETS') && WPC_USE_UNMINIFIED_ASSETS ? 'admin.js' : 'admin.min.js'), 'version' => $this->_version . '~' . DKGFSIB_FREE_ADDON_CACHE_BUSTER, 'deps' => array('jquery', 'wp-i18n'), 'in_footer' => false, 'callback' => array($this, 'localize_scripts'), 'enqueue' => array('admin_page' => array('form_settings', 'plugin_settings')), ), ); return array_merge(parent::scripts(), $scripts); } /** * The the add-on settings icon. * * @return string */ public function get_menu_icon() { // assets/images/brevo-logo.svg return ' '; } /** * Global plugin settings. * * @return array */ public function plugin_settings_fields() { return array( array( 'title' => esc_html__('Brevo Add-On Settings', 'addon-gravityforms-sendinblue-free'), 'fields' => array( array( 'name' => 'license-key-field', 'label' => esc_html__('License Activation', 'addon-gravityforms-sendinblue-free'), 'type' => 'wpc_license_field', ), array( 'name' => 'sib-api-key-field', 'type' => 'custom_html', 'callback' => [$this, 'settings_sib_api_key_field'], ), ), ), array( 'title' => esc_html__('Help', 'addon-gravityforms-sendinblue-free'), 'fields' => array( array( 'name' => 'help-links', 'label' => esc_html__('Resources', 'addon-gravityforms-sendinblue-free'), 'type' => 'html', 'html' => sprintf( '%2$s | %4$s', esc_url( 'https://wpconnect.co/documentation/gf-to-brevo/user-guide/' ), esc_html__( 'Documentation', 'addon-gravityforms-sendinblue-free' ), esc_url( 'https://wordpress.org/support/plugin/addon-gravityforms-sendinblue-free/' ), esc_html__( 'Support', 'addon-gravityforms-sendinblue-free' ) ), ), ), ), ); } /** * Render the Brevo API Key field and error/success messages. * * @return void */ public function settings_sib_api_key_field() { $api_key = dkgfsib_free_get_addon()->get_plugin_setting('sib-api-key'); $this->validate_sendinblue_api_key_field($api_key); $error = Options\get_plugin_option('sib_api_error'); echo '
%1$s
%1$s
%1$s
', $message), ), ); } else { $attributes = Helpers\get_sendinblue_attributes(); $lists = Helpers\get_sendinblue_contact_lists(); $attributes_error = is_wp_error( $attributes ) ? $attributes : null; $is_enabled_dependency = array( 'live' => true, 'operator' => 'ALL', 'fields' => array( array( 'field' => 'is-enabled', 'values' => array('1', true), ), ), ); $attributes = is_array($attributes) ? $attributes : array(); $attributes_choices = array_map( function ($attribute) { // Allow only boolean (opt-in like field) if (isset($attribute->type) && $attribute->type === 'boolean') { return array( 'label' => sanitize_text_field($attribute->name), 'value' => sanitize_text_field($attribute->name), ); } return array( 'label' => sanitize_text_field($attribute->name) . ' ' . esc_html__('(Pro version)', 'addon-gravityforms-sendinblue-free'), 'value' => '***disabled***', // We can't add disable attribute here so we change the value and make the option disabled in JS (@See admin.js) ); }, $attributes ); uasort($attributes_choices, function ($a, $b) { return $a['value'] !== '***disabled***' ? -1 : 1; }); array_unshift( $attributes_choices, array( 'label' => esc_html__('E-mail address', 'addon-gravityforms-sendinblue-free'), 'value' => 'EMAIL', ) ); $fields = array( array( 'name' => 'is-enabled', 'label' => esc_html__('Enabled', 'addon-gravityforms-sendinblue-free'), 'type' => 'toggle', ), array( 'name' => 'name', 'label' => esc_html__('Feed name', 'addon-gravityforms-sendinblue-free'), 'type' => 'text', 'required' => true, 'class' => 'medium', 'tooltip' => esc_html__('Enter a feed name to uniquely identify it.', 'addon-gravityforms-sendinblue-free'), 'dependency' => $is_enabled_dependency, ), array( 'name' => 'mapped-fields', 'label' => esc_html__('Fields mapping', 'addon-gravityforms-sendinblue-free'), 'tooltip' => esc_html__('Add and select the Brevo attributes for which to send data, then choose the form fields that match.', 'addon-gravityforms-sendinblue-free'), 'type' => 'dynamic_field_map', 'required' => true, 'enable_custom_key' => false, 'value_field' => array('title' => esc_html__('Form fields', 'addon-gravityforms-sendinblue-free')), 'key_field' => array( 'title' => esc_html__('Brevo attributes', 'addon-gravityforms-sendinblue-free'), 'choices' => $attributes_choices, ), 'dependency' => $is_enabled_dependency, 'fields' => array( array( 'name' => 'sib-free-mapping-notice', 'label' => '', 'type' => 'html', 'html' => '' . sprintf( /* translators: %1$s Pro version link */ esc_html__('Free version only includes E-mail and Consent fields ("OPT_IN" boolean in Brevo), while the %1$s is unlimited.', 'addon-gravityforms-sendinblue-free') . '
', sprintf( '%s', 'https://wpconnect.co/gravity-forms-sendinblue-add-on', esc_html__('Pro version', 'addon-gravityforms-sendinblue-free') ) ), ), ), ), ); if ( $attributes_error ) { $fields[] = array( 'name' => 'sib-attributes-api-error', 'label' => '', 'type' => 'html', 'html' => sprintf( '%1$s
', sprintf( /* translators: 1: API error code, 2: API error message */ esc_html__( 'Could not load your Brevo attributes. The Brevo API returned an error (%1$s): %2$s', 'addon-gravityforms-sendinblue-free' ), esc_html( $attributes_error->get_error_code() ), esc_html( $attributes_error->get_error_message() ) ) ), 'dependency' => $is_enabled_dependency, ); } if ( is_wp_error( $lists ) ) { $fields[] = array( 'name' => 'sib-lists-api-error', 'label' => esc_html__( 'Add to list', 'addon-gravityforms-sendinblue-free' ), 'type' => 'html', 'html' => sprintf( '%1$s
', sprintf( /* translators: 1: API error code, 2: API error message */ esc_html__( 'Could not load your Brevo lists. The Brevo API returned an error (%1$s): %2$s', 'addon-gravityforms-sendinblue-free' ), esc_html( $lists->get_error_code() ), esc_html( $lists->get_error_message() ) ) ), 'dependency' => $is_enabled_dependency, ); } elseif ( empty( $lists ) ) { $fields[] = array( 'name' => 'sib-lists-empty', 'label' => esc_html__( 'Add to list', 'addon-gravityforms-sendinblue-free' ), 'type' => 'html', 'html' => sprintf( '%1$s
', esc_html__( 'No contact lists were found in your Brevo account. Create at least one list in Brevo, then reload this page.', 'addon-gravityforms-sendinblue-free' ) ), 'dependency' => $is_enabled_dependency, ); } else { $lists_field = array( 'name' => 'add-to-list', 'label' => esc_html__('Add to list', 'addon-gravityforms-sendinblue-free'), 'tooltip' => esc_html__('All your Brevo lists are displayed below. You can choose which one(s) to save each new contact to.', 'addon-gravityforms-sendinblue-free'), 'type' => 'radio', 'horizontal' => true, 'choices' => array_merge( array( array( 'label' => esc_html__('None', 'addon-gravityforms-sendinblue-free'), 'name' => 'add-to-list-none', 'value' => 0, ), ), array_map( function ($list) { return array( 'label' => sanitize_text_field($list->name), 'name' => sprintf('add-to-list', sanitize_text_field($list->id)), 'value' => sanitize_text_field($list->id), ); }, $lists ) ), 'dependency' => $is_enabled_dependency, ); if (count($lists) > 1) { $lists_field['fields'] = array( array( 'name' => 'sib-free-lists-notice', 'label' => '', 'type' => 'html', 'html' => '' . sprintf( /* translators: %1$s Pro version link */ esc_html__('Free version only allows you to select one list at a time, while the %1$s is unlimited.', 'addon-gravityforms-sendinblue-free') . '
', sprintf( '%s', 'https://wpconnect.co/gravity-forms-sendinblue-add-on', esc_html__('Pro version', 'addon-gravityforms-sendinblue-free') ) ), ), ); } $fields[] = $lists_field; } $fields[] = array( 'name' => 'use-double-optin', 'label' => esc_html__('Double opt-in (Pro version)', 'addon-gravityforms-sendinblue-free'), 'type' => 'checkbox', 'disabled' => true, 'choices' => array( array( 'label' => esc_html__('Enable double opt-in', 'addon-gravityforms-sendinblue-free') . ' ' . esc_html__('(Pro version)', 'addon-gravityforms-sendinblue-free'), 'name' => 'double-optin-enabled', ), ), 'tooltip' => esc_html__('By enabling this, Brevo will send a confirmation email to the user before adding them to your list(s).', 'addon-gravityforms-sendinblue-free'), 'dependency' => $is_enabled_dependency, ); $fields[] = array( 'name' => 'enable-note-mapping', 'label' => esc_html__('Create a note (Pro version)', 'addon-gravityforms-sendinblue-free'), 'type' => 'checkbox', 'tooltip' => esc_html__('By enabling this feature it will be possible to add notes to a created/updated user', 'addon-gravityforms-sendinblue-free'), 'choices' => array( array( 'label' => esc_html__('Enable note creation. (Pro version)', 'addon-gravityforms-sendinblue-free'), 'name' => 'enable-note-mapping-enabled', 'disabled' => true, ), ), 'dependency' => $is_enabled_dependency, ); $fields[] = array( 'name' => 'partial-entries-enabled', 'label' => esc_html__('Use partial entries (Pro version)', 'addon-gravityforms-sendinblue-free'), 'type' => 'checkbox', 'tooltip' => sprintf( esc_html__("Allows sending partial entries to Brevo. Make sure to enable partial entries for this form in the Gravity Forms settings.", 'addon-gravityforms-sendinblue-free'), ), 'choices' => array( array( 'label' => esc_html__('Enable partial entries. (Pro version)', 'addon-gravityforms-sendinblue-free'), 'name' => 'partial-entries-enabled', 'disabled' => true, ), ), 'dependency' => $is_enabled_dependency, ); } return array( array( 'title' => esc_html__('Integration with Brevo', 'addon-gravityforms-sendinblue-free'), 'fields' => $fields, 'class' => 'wpco-sib-integration-settings', 'id' => 'wpco-sib-integration-settings', ), array( 'title' => esc_html__('Conditional logic', 'addon-gravityforms-sendinblue-free'), 'class' => 'wpco-sib-conditional-logic', 'id' => 'wpco-sib-conditional-logic', 'fields' => array( array( 'type' => 'feed_condition', 'name' => 'feed-condition', 'label' => esc_html__('Conditions', 'addon-gravityforms-sendinblue-free'), 'checkbox_label' => esc_html__('Enable conditional processing', 'addon-gravityforms-sendinblue-free'), 'instructions' => esc_html__('Create a contact in Brevo only if', 'addon-gravityforms-sendinblue-free'), ), ), ), ); } // ================================================= // // ##### ## ## ###### ##### ## ## // ## #### ## ## ## ## ## ## // ##### ## ## ## ## ##### #### // ## ## ### ## ## ## ## // ##### ## ## ## ## ## ## // // ================================================= /** * Get the attribute keys the free version is allowed to sync: EMAIL plus * boolean (consent-like) Brevo attributes. * * @return array|null Allowed keys, or null when attributes could not be fetched. */ protected function get_free_allowed_attribute_keys() { $attributes = Helpers\get_sendinblue_attributes(); if (! is_array($attributes)) { return null; } $allowed = array('EMAIL'); foreach ($attributes as $attribute) { if (isset($attribute->type, $attribute->name) && $attribute->type === 'boolean') { $allowed[] = sanitize_text_field($attribute->name); } } return $allowed; } /** * Strip Pro-only attribute mappings before saving a feed. The mapping UI disables * them, but the saved settings are the authoritative layer. * * @param integer $feed_id * @param integer $form_id * @param array $settings * @return integer */ public function save_feed_settings($feed_id, $form_id, $settings) { if (isset($settings['mapped-fields']) && is_array($settings['mapped-fields'])) { $allowed_keys = $this->get_free_allowed_attribute_keys(); // Fail-open: if attributes cannot be fetched, keep the mapping untouched. if (is_array($allowed_keys)) { $settings['mapped-fields'] = array_values( array_filter( $settings['mapped-fields'], function ($mapping) use ($allowed_keys) { return isset($mapping['key']) && in_array($mapping['key'], $allowed_keys, true); } ) ); } } return parent::save_feed_settings($feed_id, $form_id, $settings); } /** * Initiate processing the feed. * * @since 1.0 * * @param array $feed The Feed object to be processed. * @param array $entry The Entry object currently being processed. * @param array $form The Form object currently being processed. */ public function process_feed($feed, $entry, $form) { $settings = $feed['meta']; if ( ! (bool) $feed['is_active'] || ! isset($settings['mapped-fields']) || ! (bool) $settings['is-enabled'] ) { return; } $mapped_fields = array_map( function ($conf) use ($entry, $form) { $value = isset($entry[$conf['value']]) ? sanitize_text_field($entry[$conf['value']]) : null; $key = ($conf['key'] === 'gf_custom') ? sanitize_text_field($conf['custom_key']) : sanitize_text_field($conf['key']); if ($conf['value'] === 'form_title') { $value = sanitize_text_field($form['title']); } return array( 'key' => $key, 'value' => $value, ); }, $settings['mapped-fields'] ); $mapped_fields = $this->get_dynamic_field_map_fields($feed, 'mapped-fields'); $enhanced_mapped_fields = array(); foreach ($mapped_fields as $key => $field_id) { if (rgblank($field_id)) { continue; } $field_value = $this->get_field_value($form, $entry, $field_id); $field = \GFFormsModel::get_field($form, $field_id); if (is_object($field)) { $input_type = $field->get_input_type(); if ($input_type === 'consent' && in_array($field_value, array(esc_html__('Not Checked', 'gravityforms'), esc_html__('Checked', 'gravityforms')))) { $field_value = $field_value === esc_html__('Checked', 'gravityforms'); } } $enhanced_mapped_fields[$key] = (object) array( 'key' => $key, 'value' => $field_value, ); } $list_id = (isset($settings['add-to-list']) ? (int) $settings['add-to-list'] : -1); if ($list_id > 0) { $lists = array($list_id); } else { $lists = array(); } // Filter variables for external modifications. $mapped_fields = apply_filters('dk-gf-sib-free/entry/mapped-fields', $enhanced_mapped_fields, $feed, $entry, $form, $settings, $this); $lists = apply_filters('dk-gf-sib-free/entry/lists', $lists, $feed, $entry, $form, $settings, $this); if (! apply_filters('dk-gf-sib-free/disable-form-entry-processing', false, $feed, $entry, $form, $settings, $this)) { do_action( 'dk-gf-sib-free/process-form-entry', $mapped_fields, $lists, false, null, null, $feed, $entry, $form, $settings, $this ); } } /** * Get all feeds API responses for a specific entry. * * @param integer $entry_id * @return array */ public function get_entry_addon_metadata($entry_id) { $metas = array(); $feeds = $this->get_feeds_by_entry($entry_id); if (! is_array($feeds)) { return $metas; } foreach ($this->get_feeds_by_entry($entry_id) as $feed_id) { $metas[$feed_id] = gform_get_meta($entry_id, sprintf('sib_contact_api_response:%1$d', $feed_id)); } return $metas; } }