From 86f4fe86c9804c597b956ffc4c04f8ab90d95c2d Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:00:05 +0300 Subject: [PATCH 1/6] add uninstall hook to cleanup Hellotext options --- hellotext.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/hellotext.php b/hellotext.php index 0d79ead..9190e6b 100644 --- a/hellotext.php +++ b/hellotext.php @@ -26,7 +26,7 @@ $HELLOTEXT_DEV_MODE = $_ENV['APP_ENV'] === 'development'; $HELLOTEXT_API_URL = $HELLOTEXT_DEV_MODE ? $_ENV['HELLOTEXT_API_URL'] ?? '' - : 'https://api.hellotext.com'; + : 'http://api.lvh.me:3000'; session_start(); @@ -88,3 +88,20 @@ function hellotext_load_textdomain() { load_plugin_textdomain( 'hellotext', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } add_action( 'plugins_loaded', 'hellotext_load_textdomain' ); + +function uninstall() { + global $wpdb; + + delete_option('hellotext_business_id'); + delete_option('hellotext_webchat_id'); + delete_option('hellotext_webchat_placement'); + delete_option('hellotext_webchat_behaviour'); + delete_option('hellotext_access_token'); + + $api_keys_table = $wpdb->prefix . 'woocommerce_api_keys'; + if ($wpdb->get_var("SHOW TABLES LIKE '$api_keys_table'") === $api_keys_table) { + $wpdb->delete($api_keys_table, ['description' => 'Hellotext']); + } +} + +register_uninstall_hook(__FILE__, 'uninstall'); From 4269c96cf1666595a5e3992822867db8c6495c8b Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:03:00 +0300 Subject: [PATCH 2/6] localize submit button --- languages/hellotext-en_US.mo | Bin 1524 -> 1569 bytes languages/hellotext-en_US.po | 3 +++ languages/hellotext-es_ES.mo | Bin 1752 -> 1800 bytes languages/hellotext-es_ES.po | 3 +++ languages/hellotext.pot | 3 +++ src/Misc/Settings.php | 8 +++++++- 6 files changed, 16 insertions(+), 1 deletion(-) diff --git a/languages/hellotext-en_US.mo b/languages/hellotext-en_US.mo index 364be782c6c39d4c4363517802e6ed7ea1d5102d..9f8dcd2f513fd2e964e91bc216c21c4950b5a328 100644 GIT binary patch delta 304 zcmYMrF>AtL5XSL~BtcEAR9g^r5R}xdAEH~oK?esdp*pBTG-Dh(ibJu|#YN~)C=TLU za2I!%;@l5Vmky!-1Hpsich9}|{yGbHbjX=Ap|W(De$_y^#@QlchOE}{8t?E5A5j1H z@B$;$y9ws;-OhR0VNxc^+;S@cbV*%G`-lE=}Q2%bI_jk~$1^@s6 diff --git a/languages/hellotext-en_US.po b/languages/hellotext-en_US.po index 538b0f0..18d0a54 100644 --- a/languages/hellotext-en_US.po +++ b/languages/hellotext-en_US.po @@ -68,6 +68,9 @@ msgstr "Top Left" msgid "settings.webchat_behaviour" msgstr "Webchat Behaviour" +msgid "settings.submit" +msgstr "Save changes" + msgid "description.paragraphs.one" msgstr "You can find your Business ID on the Hellotext business settings." diff --git a/languages/hellotext-es_ES.mo b/languages/hellotext-es_ES.mo index fce43c9a2d19c3fa2ab477b78620a438a4a3a85c..bf3ecf8051a869d2b4e613456331b364f7ba10d1 100644 GIT binary patch delta 324 zcmYMvv1`IW6vy!wi2;X1Nlgc=wNt^# z!OrO)(5ZCtkLc9z;eiLo=lza5-n(x*b>=^=xfE87-DKw~u$sonYswb6fp^*m^vDaW z;|o@CgAVRc-7oMA4?6$h71=9^^hD+|py+Tg#xAC)9=zk4%b(aMhb9@{aF3(%v7uuf zz0}^I8eoWOfRWABqoW;YBV#72WTu#u1jtGc4i?JxtC00R!^Y z;jae!KI cj6Tj$i!U*d9E};Y(J`r5>FHuk?WE`2zF{{UT>t<8 diff --git a/languages/hellotext-es_ES.po b/languages/hellotext-es_ES.po index 5053363..105e669 100644 --- a/languages/hellotext-es_ES.po +++ b/languages/hellotext-es_ES.po @@ -68,6 +68,9 @@ msgstr "Arriba a la izquierda" msgid "settings.webchat_behaviour" msgstr "Comportamiento del Webchat" +msgid "settings.submit" +msgstr "Guardar cambios" + msgid "description.paragraphs.one" msgstr "Puedes encontrar tu ID de negocio en la configuraciĆ³n de negocios de Hellotext." diff --git a/languages/hellotext.pot b/languages/hellotext.pot index 8488279..d9c5c5a 100644 --- a/languages/hellotext.pot +++ b/languages/hellotext.pot @@ -65,6 +65,9 @@ msgstr "" msgid "settings.webchat_placement_top-left" msgstr "" +msgid "settings.submit" +msgstr "" + msgid "settings.webchat_behaviour" msgstr "" diff --git a/src/Misc/Settings.php b/src/Misc/Settings.php index 25a6cc4..b919cc9 100644 --- a/src/Misc/Settings.php +++ b/src/Misc/Settings.php @@ -183,7 +183,13 @@ function hellotext_submenu_page_callback () { 'background-color: #FF4C00; color: #FFFFFF; border: none;')); + submit_button( + __('settings.submit', 'hellotext'), + null, + null, + false, + array('style' => 'background-color: #FF4C00; color: #FFFFFF; border: none;') + ); ?> From 29958b03b12a65e32b8c0f516e9348a3f3b971fd Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:03:40 +0300 Subject: [PATCH 3/6] only show respective fields when field is empty --- src/Misc/Settings.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Misc/Settings.php b/src/Misc/Settings.php index b919cc9..fa99a3d 100644 --- a/src/Misc/Settings.php +++ b/src/Misc/Settings.php @@ -68,11 +68,11 @@ function hellotext_description_section_callback() { $business_id = get_option('hellotext_business_id', null); $access_token = get_option('hellotext_access_token', null); - if ($business_id) { + if (!$business_id) { echo '

' . wp_kses( __( 'description.paragraphs.one', 'hellotext' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ) . '

'; } - if ($access_token) { + if (!$access_token) { echo '

' . wp_kses( __( 'description.paragraphs.two', 'hellotext' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ) . '

'; } } From fd2d54b0fd1a2184ce43c2196b68a5f856d0665f Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:04:14 +0300 Subject: [PATCH 4/6] hook for after_update to create the integration once values are set --- src/Events/AppInstalled.php | 44 +++++++++++++++++++++++++------------ src/Events/AppRemoved.php | 27 ++++++++--------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/Events/AppInstalled.php b/src/Events/AppInstalled.php index 80cc307..d5035c3 100644 --- a/src/Events/AppInstalled.php +++ b/src/Events/AppInstalled.php @@ -9,23 +9,14 @@ function hellotext_activate () { return; } - do_action('hellotext_create_profile'); do_action('hellotext_create_integration', $hellotext_business_id); - - // Disbaled for now - // $store_image_id = get_option('woocommerce_email_header_image_id'); - // $store_image_url = wp_get_attachment_image_url($store_image_id, 'full'); - - // (new Event())->track('app.installed', array( - // 'app_parameters' => array( - // 'type' => 'app', - // 'name' => get_bloginfo('name'), - // 'image_url' => $store_image_url, - // ) - // )); } add_action('hellotext_create_integration', function ($business_id) { + if(!$business_id) { + $business_id = get_option('hellotext_business_id'); + } + global $wpdb; $api_keys_table = $wpdb->prefix . 'woocommerce_api_keys'; $api_keys = $wpdb->get_row("SELECT * FROM $api_keys_table WHERE description = 'Hellotext'"); @@ -52,7 +43,6 @@ function hellotext_activate () { Client::with_sufix() ->post('/integrations/woo', [ 'shop' => [ - 'business_id' => $business_id, 'name' => get_bloginfo('name'), 'url' => get_bloginfo('url'), 'email' => get_bloginfo('admin_email'), @@ -62,3 +52,29 @@ function hellotext_activate () { ]); }); +function after_business_id_save($old_value, $new_value) { + if ($old_value !== $new_value) { + maybe_trigger_integration($new_value); + } +} + +function after_business_id_set($value) { + maybe_trigger_integration($value); +} + +function maybe_trigger_integration($business_id) { + $hellotext_access_token = get_option('hellotext_access_token'); + if ($hellotext_access_token && $business_id) { + do_action('hellotext_create_integration'); + } else { + add_action('shutdown', function () { + $hellotext_access_token = get_option('hellotext_access_token'); + if ($hellotext_access_token) { + do_action('hellotext_create_integration'); + } + }); + } +} + +add_action('update_option_hellotext_business_id', 'after_business_id_save', 10, 2); +add_action('add_option_hellotext_business_id', 'after_business_id_set', 10, 1); diff --git a/src/Events/AppRemoved.php b/src/Events/AppRemoved.php index cf2ba22..37c3b56 100644 --- a/src/Events/AppRemoved.php +++ b/src/Events/AppRemoved.php @@ -3,26 +3,17 @@ use Hellotext\Api\Client; use Hellotext\Api\Event; -function hellotext_deactivate () { - $hellotext_business_id = get_option('hellotext_business_id'); - if (!$hellotext_business_id) { - return; - } +function hellotext_deactivate ($hellotext_business_id = null) { + if (!$hellotext_business_id) { + $hellotext_business_id = get_option('hellotext_business_id'); + } - do_action('hellotext_remove_integration', $hellotext_business_id); + if (!$hellotext_business_id) { + return; + } - // Disbaled for now - // $store_image_id = get_option('woocommerce_email_header_image_id'); - // $store_image_url = wp_get_attachment_image_url($store_image_id, 'full'); - - // (new Event())->track('app.removed', array( - // 'app_parameters' => array( - // 'type' => 'app', - // 'name' => get_bloginfo('name'), - // 'image_url' => $store_image_url, - // ) - // )); -} + do_action('hellotext_remove_integration', $hellotext_business_id); + } add_action('hellotext_remove_integration', function ($business_id) { Client::with_sufix() From 9b5228244a180c1b8c33028ea4a035c6151291c3 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:08:27 +0300 Subject: [PATCH 5/6] update plugin endpoint --- hellotext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hellotext.php b/hellotext.php index 9190e6b..db1379a 100644 --- a/hellotext.php +++ b/hellotext.php @@ -26,7 +26,7 @@ $HELLOTEXT_DEV_MODE = $_ENV['APP_ENV'] === 'development'; $HELLOTEXT_API_URL = $HELLOTEXT_DEV_MODE ? $_ENV['HELLOTEXT_API_URL'] ?? '' - : 'http://api.lvh.me:3000'; + : 'https://api.hellotext.com'; session_start(); From 46e0104f3c2cb257368efca59f6c3ea27c83c892 Mon Sep 17 00:00:00 2001 From: rockwellll Date: Thu, 16 Jan 2025 10:11:21 +0300 Subject: [PATCH 6/6] remove installation hook --- hellotext.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/hellotext.php b/hellotext.php index db1379a..7d53b95 100644 --- a/hellotext.php +++ b/hellotext.php @@ -49,9 +49,6 @@ } } -// Function on Events/AppInstalled.php -register_activation_hook( __FILE__, 'hellotext_activate' ); - // Function on Events/AppRemoved.php register_deactivation_hook( __FILE__, 'hellotext_deactivate' );