diff --git a/hellotext.php b/hellotext.php index 0d79ead..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' ); @@ -88,3 +85,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'); diff --git a/languages/hellotext-en_US.mo b/languages/hellotext-en_US.mo index 364be78..9f8dcd2 100644 Binary files a/languages/hellotext-en_US.mo and b/languages/hellotext-en_US.mo differ 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 fce43c9..bf3ecf8 100644 Binary files a/languages/hellotext-es_ES.mo and b/languages/hellotext-es_ES.mo differ 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/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() diff --git a/src/Misc/Settings.php b/src/Misc/Settings.php index 25a6cc4..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() ) ) ) . '
'; } } @@ -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;') + ); ?>