From 58cff50db29c8161105df6db70137752819056c0 Mon Sep 17 00:00:00 2001 From: Alexander Mykhailenko Date: Sat, 6 Aug 2022 19:36:13 +0300 Subject: [PATCH] Fixing widget. In progress --- languages/mailjet-for-wordpress-de_DE.po | 4 +- languages/mailjet-for-wordpress-en_US.po | 20 +-- languages/mailjet-for-wordpress-es_ES.po | 4 +- languages/mailjet-for-wordpress-fr_FR.po | 4 +- languages/mailjet-for-wordpress-it_IT.po | 4 +- .../SubscriptionOptionsSettings.php | 31 ++-- src/widget/WP_Mailjet_Subscribe_Widget.php | 16 +- src/widget/js/widget.js | 10 +- src/widget/views/widget.php | 149 +++++++++--------- 9 files changed, 129 insertions(+), 113 deletions(-) diff --git a/languages/mailjet-for-wordpress-de_DE.po b/languages/mailjet-for-wordpress-de_DE.po index f4548b9e..2011c2f4 100644 --- a/languages/mailjet-for-wordpress-de_DE.po +++ b/languages/mailjet-for-wordpress-de_DE.po @@ -1575,13 +1575,13 @@ msgid "email_content_after_button" msgstr "" msgid "contactProperties0" -msgstr "46828" +msgstr "46827" msgid "propertyDataType0" msgstr "0" msgid "EnglishLabel0" -msgstr "" +msgstr "First Name" msgid "FrenchLabel0" msgstr "" diff --git a/languages/mailjet-for-wordpress-en_US.po b/languages/mailjet-for-wordpress-en_US.po index afa1c205..30b9c7f0 100644 --- a/languages/mailjet-for-wordpress-en_US.po +++ b/languages/mailjet-for-wordpress-en_US.po @@ -1119,13 +1119,13 @@ msgid "list" msgstr "21908" msgid "language_mandatory_email" -msgstr "" +msgstr "test@gmail.com" msgid "language_mandatory_button" msgstr "" msgid "confirmation_email_message_input" -msgstr "" +msgstr "test1" msgid "subscription_confirmed_message_input" msgstr "" @@ -1137,34 +1137,34 @@ msgid "already_subscribed_message_input" msgstr "" msgid "invalid_data_format_message_input" -msgstr "" +msgstr "sss" msgid "generic_technical_error_message_input" -msgstr "" +msgstr "ss" msgid "email_subject" -msgstr "" +msgstr "my subject" msgid "email_content_title" -msgstr "" +msgstr "please, please" msgid "email_content_main_text" -msgstr "" +msgstr "main tet" msgid "email_content_confirm_button" -msgstr "" +msgstr "hello moto" msgid "email_content_after_button" msgstr "" msgid "contactProperties0" -msgstr "46828" +msgstr "46827" msgid "propertyDataType0" msgstr "0" msgid "EnglishLabel0" -msgstr "" +msgstr "First Name" msgid "FrenchLabel0" msgstr "" diff --git a/languages/mailjet-for-wordpress-es_ES.po b/languages/mailjet-for-wordpress-es_ES.po index 5e67b5b7..cd22d7de 100644 --- a/languages/mailjet-for-wordpress-es_ES.po +++ b/languages/mailjet-for-wordpress-es_ES.po @@ -1579,13 +1579,13 @@ msgid "email_content_after_button" msgstr "" msgid "contactProperties0" -msgstr "46828" +msgstr "46827" msgid "propertyDataType0" msgstr "0" msgid "EnglishLabel0" -msgstr "" +msgstr "First Name" msgid "FrenchLabel0" msgstr "" diff --git a/languages/mailjet-for-wordpress-fr_FR.po b/languages/mailjet-for-wordpress-fr_FR.po index ebd447e9..ad8dbb8b 100644 --- a/languages/mailjet-for-wordpress-fr_FR.po +++ b/languages/mailjet-for-wordpress-fr_FR.po @@ -1593,13 +1593,13 @@ msgid "email_content_after_button" msgstr "" msgid "contactProperties0" -msgstr "46828" +msgstr "46827" msgid "propertyDataType0" msgstr "0" msgid "EnglishLabel0" -msgstr "" +msgstr "First Name" msgid "FrenchLabel0" msgstr "" diff --git a/languages/mailjet-for-wordpress-it_IT.po b/languages/mailjet-for-wordpress-it_IT.po index 2aed4f67..1f50dcbe 100644 --- a/languages/mailjet-for-wordpress-it_IT.po +++ b/languages/mailjet-for-wordpress-it_IT.po @@ -1567,13 +1567,13 @@ msgid "email_content_after_button" msgstr "" msgid "contactProperties0" -msgstr "46828" +msgstr "46827" msgid "propertyDataType0" msgstr "0" msgid "EnglishLabel0" -msgstr "" +msgstr "First Name" msgid "FrenchLabel0" msgstr "" diff --git a/src/includes/SettingsPages/SubscriptionOptionsSettings.php b/src/includes/SettingsPages/SubscriptionOptionsSettings.php index 7b8c479a..94ba5619 100644 --- a/src/includes/SettingsPages/SubscriptionOptionsSettings.php +++ b/src/includes/SettingsPages/SubscriptionOptionsSettings.php @@ -320,14 +320,22 @@ public function mailjet_subscribe_unsub_user_to_list($subscribe, $user_id) $contactProperties[WooCommerceSettings::WOO_PROP_ACCOUNT_CREATION_DATE] = $user->user_registered; } // Add the user to a contact list - if (\false === self::syncSingleContactEmailToMailjetList(get_option('mailjet_sync_list'), $user->user_email, $action, $contactProperties)) { - return \false; + if (false === self::syncSingleContactEmailToMailjetList(get_option('mailjet_sync_list'), $user->user_email, $action, $contactProperties)) { + return false; } else { - return \true; + return true; } } } - public function mailjet_subscribe_confirmation_from_widget($subscription_email, $instance, $subscription_locale, $widgetId = \false) + + /** + * @param $subscription_email + * @param $instance + * @param $subscription_locale + * @param $widgetId + * @return bool|mixed|void + */ + public function mailjet_subscribe_confirmation_from_widget($subscription_email, $instance, $subscription_locale, $widgetId = false) { $homeUrl = get_home_url(); $language = Mailjeti18n::getCurrentUserLanguage(); @@ -340,9 +348,8 @@ public function mailjet_subscribe_confirmation_from_widget($subscription_email, $email_subject = !empty($instance[$locale]['email_subject']) ? apply_filters('widget_email_subject', $instance[$locale]['email_subject']) : Mailjeti18n::getTranslationsFromFile($locale, 'Subscription Confirmation'); $email_title = !empty($instance[$locale]['email_content_title']) ? apply_filters('widget_email_content_title', $instance[$locale]['email_content_title']) : Mailjeti18n::getTranslationsFromFile($locale, 'Please confirm your subscription'); $email_button_value = !empty($instance[$locale]['email_content_confirm_button']) ? apply_filters('widget_email_content_confirm_button', $instance[$locale]['email_content_confirm_button']) : Mailjeti18n::getTranslationsFromFile($locale, 'Yes, subscribe me to this list'); - $wpUrl = \sprintf('%s', $homeUrl, $homeUrl); - $test = \sprintf(Mailjeti18n::getTranslationsFromFile($locale, 'To receive newsletters from %s please confirm your subscription by clicking the following button:'), $wpUrl); - // $test = sprintf(__('To receive newsletters from %s please confirm your subscription by clicking the following button:', 'mailjet-for-wordpress'), $wpUrl); + $wpUrl = sprintf('%s', $homeUrl, $homeUrl); + $test = sprintf(Mailjeti18n::getTranslationsFromFile($locale, 'To receive newsletters from %s please confirm your subscription by clicking the following button:'), $wpUrl); $email_main_text = !empty($instance[$locale]['email_content_main_text']) ? apply_filters('widget_email_content_main_text', \sprintf($instance[$locale]['email_content_main_text'], get_option('blogname'))) : $test; $email_content_after_button = !empty($instance[$locale]['email_content_after_button']) ? $instance[$locale]['email_content_after_button'] : Mailjeti18n::getTranslationsFromFile($locale, 'If you received this email by mistake or don\'t wish to subscribe anymore, simply ignore this message.'); $properties = isset($_POST['properties']) ? $_POST['properties'] : array(); @@ -350,9 +357,9 @@ public function mailjet_subscribe_confirmation_from_widget($subscription_email, if ($widgetId) { $params['widget_id'] = $widgetId; } - $params = \http_build_query($params); - $subscriptionTemplate = apply_filters('mailjet_confirmation_email_filename', \dirname(\dirname(\dirname(__FILE__))) . '/templates/confirm-subscription-email.php'); - $message = \file_get_contents($subscriptionTemplate); + $params = http_build_query($params); + $subscriptionTemplate = apply_filters('mailjet_confirmation_email_filename', dirname(dirname(dirname(__FILE__))) . '/templates/confirm-subscription-email.php'); + $message = file_get_contents($subscriptionTemplate); $permalinkStructure = get_option('permalink_structure'); if (!$thankYouPageId) { $qm = '?'; @@ -363,7 +370,7 @@ public function mailjet_subscribe_confirmation_from_widget($subscription_email, '__EMAIL_TITLE__' => $email_title, '__EMAIL_HEADER__' => $email_main_text, '__WP_URL__' => $homeUrl, - '__CONFIRM_URL__' => $thankYouURI . $qm . $params . '&mj_sub_token=' . \sha1($params . MailjetSettings::getCryptoHash()), + '__CONFIRM_URL__' => $thankYouURI . $qm . $params . '&mj_sub_token=' . sha1($params . MailjetSettings::getCryptoHash()), '__CLICK_HERE__' => $email_button_value, '__FROM_NAME__' => $homeUrl, //get_option('blogname'), @@ -375,8 +382,6 @@ public function mailjet_subscribe_confirmation_from_widget($subscription_email, } add_filter('wp_mail_content_type', array($this, 'set_html_content_type')); return wp_mail($subscription_email, $email_subject, $message, array('From: ' . get_option('blogname') . ' <' . get_option('admin_email') . '>')); - // echo '

' . __('Subscription confirmation email sent. Please check your inbox and confirm the subscription.', 'mailjet-for-wordpress') . '

'; - // die; } public function getContactListsMenu() { diff --git a/src/widget/WP_Mailjet_Subscribe_Widget.php b/src/widget/WP_Mailjet_Subscribe_Widget.php index f11bd955..a143aa44 100644 --- a/src/widget/WP_Mailjet_Subscribe_Widget.php +++ b/src/widget/WP_Mailjet_Subscribe_Widget.php @@ -2,6 +2,7 @@ namespace MailjetWp\MailjetPlugin\Widget; +use Exception; use MailjetWp\MailjetPlugin\Includes\MailjetApi; use MailjetWp\MailjetPlugin\Includes\Mailjeti18n; use MailjetWp\MailjetPlugin\Includes\MailjetLogger; @@ -35,17 +36,17 @@ public function __construct() parent::__construct($this->get_widget_slug(), __('Mailjet Subscription Widget', 'mailjet-for-wordpress'), $widget_options); // Register site styles and scripts add_action('admin_enqueue_scripts', array($this, 'register_widget_scripts')); - if (is_active_widget(\false, \false, $this->id_base)) { - add_action('wp_enqueue_scripts', array($this, 'register_widget_front_styles')); - add_action('wp_enqueue_scripts', array($this, 'register_widget_front_scripts')); - } + // Refreshing the widget's cached output with each new post add_action('save_post', array($this, 'flush_widget_cache')); add_action('deleted_post', array($this, 'flush_widget_cache')); add_action('switch_theme', array($this, 'flush_widget_cache')); add_action('wp_ajax_mailjet_add_contact_property', array($this, 'wp_ajax_mailjet_add_contact_property')); + add_action('wp_enqueue_scripts', array($this, 'register_widget_front_styles')); + add_action('wp_enqueue_scripts', array($this, 'register_widget_front_scripts')); add_action('wp_ajax_send_mailjet_subscription_form', array($this, 'sendSubscriptionEmail')); add_action('wp_ajax_nopriv_send_mailjet_subscription_form', array($this, 'sendSubscriptionEmail')); + $this->propertyData = get_site_transient('propertyData'); // Subscribe user $this->activateConfirmSubscriptionUrl(); } @@ -55,9 +56,9 @@ public function __construct() * * @since 5.0.0 * - * @return Plugin slug variable. + * @return string */ - public function get_widget_slug() + public function get_widget_slug(): string { return $this->widget_slug; } @@ -68,6 +69,7 @@ public function get_widget_slug() */ public function sendSubscriptionEmail() { + file_put_contents(__DIR__ . '/test.txt', print_r($_POST, true)); $subscriptionOptionsSettings = $this->getSubscriptionOptionsSettings(); $instances = get_option(self::WIDGETS_OPTIONS_NAME); $locale = Mailjeti18n::getLocale(); @@ -594,7 +596,7 @@ private function getMailjetContactProperties() try { $this->mailjetContactProperties = MailjetApi::getContactProperties(); } catch (Exception $ex) { - return \false; + return false; } } return $this->mailjetContactProperties; diff --git a/src/widget/js/widget.js b/src/widget/js/widget.js index 26d3a894..41be28cc 100644 --- a/src/widget/js/widget.js +++ b/src/widget/js/widget.js @@ -2,6 +2,8 @@ 'use strict'; $(function () { + var mjInitTabs = false; + $(document).on('widget-updated', function (event, widget) { showCheckedLanguages(); eventOpenAdvancedFormModal(); @@ -30,7 +32,6 @@ }); $(document).on('change', '.language-select-list', function (event) { - console.log('TEST 3'); var languageListId = event.target.value; if (languageListId === '0') { // No contact list is selected @@ -189,7 +190,9 @@ if (dataAttr !== null && typeof dataAttr !== null) { $('#modal-' + dataAttr).slideToggle('slow'); } - eventChangeAdvancedFormTab(); + if (!mjInitTabs) { + eventChangeAdvancedFormTab(); + } }); // Fires when property value is changed @@ -277,6 +280,9 @@ function eventChangeAdvancedFormTab() { const tabsTriggersContainers = document.querySelectorAll('.mj-nav-tabs'); + if (typeof tabsTriggersContainers !== 'undefined' && tabsTriggersContainers.length > 0) { + mjInitTabs = true; + } tabsTriggersContainers.forEach(function (tabsTriggersContainer) { const modalId = '#modal-' + tabsTriggersContainer.getAttribute('data-target'); const tabsTriggers = tabsTriggersContainer.querySelectorAll('.mj-tab'); diff --git a/src/widget/views/widget.php b/src/widget/views/widget.php index 9172202c..2cc0f9ff 100644 --- a/src/widget/views/widget.php +++ b/src/widget/views/widget.php @@ -7,8 +7,9 @@
-
propertyData[$contactPropertyId])) { - continue; - } - $propertyDataType = $this->propertyData[$contactPropertyId]['Datatype']; - // Mailjet property type - $labelValue = $instance[$locale][$language . 'Label' . $i]; - $propertyType = (int) $instance[$locale]['propertyDataType' . $i]; - // '0' - optional, '1' - mandatory, '2' - hidden - $isHidden = $propertyType === 2; - $isMandatory = $propertyType === 1; - $inputProperties = $this->getInputProperties($propertyDataType, $labelValue, $isHidden, $isMandatory); - if ('bool' === $inputProperties['type']) { - $required = isset($inputProperties['required']) ? $inputProperties['required'] : ''; - ?> -
- name="properties[]" id="mailjet_property_" /> - -
- $propValue) { - $inputPropertiesString .= "{$propKey}=\"{$propValue}\""; - } - $additionalDivClass = 'date' === $inputProperties['type'] ? 'mailjet-widget-form-date' : ''; - ?> -
- - - propertyData[$contactPropertyId])) { + continue; + } + + $propertyDataType = $this->propertyData[$contactPropertyId]['Datatype']; + // Mailjet property type + $labelValue = $instance[$locale][$language . 'Label' . $i]; + $propertyType = (int)$instance[$locale]['propertyDataType' . $i]; + // '0' - optional, '1' - mandatory, '2' - hidden + $isHidden = $propertyType === 2; + $isMandatory = $propertyType === 1; + $inputProperties = $this->getInputProperties($propertyDataType, $labelValue, $isHidden, $isMandatory); + if ('bool' === $inputProperties['type']) { + $required = isset($inputProperties['required']) ? $inputProperties['required'] : ''; + ?> +
+ name="properties[]" id="mailjet_property_"/> + +
+ $propValue) { + $inputPropertiesString .= "{$propKey}=\"{$propValue}\""; + } + $additionalDivClass = 'date' === $inputProperties['type'] ? 'mailjet-widget-form-date' : ''; + ?> +
+ + + + > +
+ - > -
-