From b500c35e53f37af861a1495047d4bd91d3216c54 Mon Sep 17 00:00:00 2001
From: rockwellll
Date: Sun, 18 Jan 2026 12:31:34 +0300
Subject: [PATCH 01/13] add a constants.php file
---
hellotext.php | 14 ++++++---
src/Api/Client.php | 6 ++--
src/Api/Event.php | 10 +++---
src/Api/Webchat.php | 5 +--
src/Constants.php | 43 +++++++++++++++++++++++++
src/Events/AppInstalled.php | 19 +++++------
src/Events/AppRemoved.php | 5 +--
src/Events/CartUpdates.php | 18 ++++++++---
src/Events/CouponRedeemed.php | 3 +-
src/Events/CustomOptionsUpdated.php | 4 ++-
src/Events/OrderPlaced.php | 9 +++---
src/Events/OrderStatus.php | 9 +++---
src/Events/ProductViewed.php | 3 +-
src/Events/RefundReceived.php | 5 +--
src/Misc/Settings.php | 49 +++++++++++++++++------------
src/Services/CreateProfile.php | 25 ++++++++-------
src/Services/Session.php | 13 ++++----
17 files changed, 162 insertions(+), 78 deletions(-)
create mode 100644 src/Constants.php
diff --git a/hellotext.php b/hellotext.php
index 077a2cd..b5720aa 100644
--- a/hellotext.php
+++ b/hellotext.php
@@ -17,6 +17,10 @@
* Domain Path: /languages
*/
+use Hellotext\Constants;
+
+require_once plugin_dir_path(__FILE__) . 'src/Constants.php';
+
// TODO: Refactor this to use the APP_ENV variable
if (! isset($_ENV['APP_ENV'])) {
$_ENV['APP_ENV'] = 'production';
@@ -89,11 +93,11 @@ function 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');
+ delete_option(Constants::OPTION_BUSINESS_ID);
+ delete_option(Constants::OPTION_WEBCHAT_ID);
+ delete_option(Constants::OPTION_WEBCHAT_PLACEMENT);
+ delete_option(Constants::OPTION_WEBCHAT_BEHAVIOUR);
+ delete_option(Constants::OPTION_ACCESS_TOKEN);
$api_keys_table = $wpdb->prefix . 'woocommerce_api_keys';
if ($wpdb->get_var("SHOW TABLES LIKE '$api_keys_table'") === $api_keys_table) {
diff --git a/src/Api/Client.php b/src/Api/Client.php
index cf1434b..d008cfe 100644
--- a/src/Api/Client.php
+++ b/src/Api/Client.php
@@ -2,8 +2,10 @@
namespace Hellotext\Api;
+use Hellotext\Constants;
+
class Client {
- public static $sufix = '/v1';
+ public static $sufix = '/' . Constants::API_VERSION;
public static function with_sufix ($sufix = '') {
if (0 < strlen($sufix) && '/' !== $sufix[0]) {
@@ -62,7 +64,7 @@ private static function get_api_url () {
}
private static function set_curl_options ($curl, $method) {
- $hellotext_access_token = get_option('hellotext_access_token');
+ $hellotext_access_token = get_option(Constants::OPTION_ACCESS_TOKEN);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
diff --git a/src/Api/Event.php b/src/Api/Event.php
index 99492ad..ef33fe6 100644
--- a/src/Api/Event.php
+++ b/src/Api/Event.php
@@ -2,9 +2,11 @@
namespace Hellotext\Api;
+use Hellotext\Constants;
+
class Event {
public function __construct ($session = null) {
- $this->hellotext_business_id = get_option('hellotext_business_id');
+ $this->hellotext_business_id = get_option(Constants::OPTION_BUSINESS_ID);
$this->session = $session;
$this->curl = curl_init($this->get_api_url());
$this->set_curl_options();
@@ -42,12 +44,12 @@ private function set_curl_options () {
private function get_api_url () {
global $HELLOTEXT_API_URL;
- return $HELLOTEXT_API_URL . '/v1/track/events';
+ return $HELLOTEXT_API_URL . Constants::API_ENDPOINT_TRACK;
}
private function browser_session () {
- if (isset($_COOKIE['hello_session'])) {
- return sanitize_text_field($_COOKIE['hello_session']);
+ if (isset($_COOKIE[Constants::SESSION_COOKIE_NAME])) {
+ return sanitize_text_field($_COOKIE[Constants::SESSION_COOKIE_NAME]);
}
return null;
diff --git a/src/Api/Webchat.php b/src/Api/Webchat.php
index eeda2e8..c5767a5 100644
--- a/src/Api/Webchat.php
+++ b/src/Api/Webchat.php
@@ -3,16 +3,17 @@
namespace Hellotext\Api;
use Hellotext\Api\Client;
+use Hellotext\Constants;
class Webchat {
public static function index() {
- $hellotext_access_token = get_option('hellotext_access_token');
+ $hellotext_access_token = get_option(Constants::OPTION_ACCESS_TOKEN);
if(!$hellotext_access_token) {
return [];
}
- $body = Client::with_sufix()->get('/v1/wordpress/webchats');
+ $body = Client::with_sufix()->get(Constants::API_ENDPOINT_WEBCHATS);
return is_array($body['body']) && isset($body['body']['ids']) ? $body['body']['ids'] : [];
}
}
diff --git a/src/Constants.php b/src/Constants.php
new file mode 100644
index 0000000..9c34446
--- /dev/null
+++ b/src/Constants.php
@@ -0,0 +1,43 @@
+post('/integrations/woo', [
+ ->post(Constants::API_ENDPOINT_INTEGRATIONS_WOO, [
'shop' => [
'name' => get_bloginfo('name'),
'url' => get_bloginfo('url'),
@@ -72,14 +73,14 @@ function maybe_trigger_integration($business_id) {
return;
}
- $hellotext_access_token = get_option('hellotext_access_token');
+ $hellotext_access_token = get_option(Constants::OPTION_ACCESS_TOKEN);
if ($hellotext_access_token && $business_id) {
set_transient('hellotext_integration_triggered', true, 10);
do_action('hellotext_create_integration');
} else {
add_action('shutdown', function () {
$integration_flag = get_transient('hellotext_integration_triggered');
- $hellotext_access_token = get_option('hellotext_access_token');
+ $hellotext_access_token = get_option(Constants::OPTION_ACCESS_TOKEN);
if ($hellotext_access_token && !$integration_flag) {
set_transient('hellotext_integration_triggered', true, 10); // Expires in 10 seconds.
@@ -89,7 +90,7 @@ function maybe_trigger_integration($business_id) {
}
}
-add_action('add_option_hellotext_business_id', 'after_business_id_set', 10, 1);
-add_action('add_option_hellotext_access_token', 'maybe_trigger_integration', 10, 3);
-add_action('update_option_hellotext_business_id', 'after_business_id_save', 10, 3);
-add_action('update_option_hellotext_access_token', 'maybe_trigger_integration', 10, 4);
+add_action('add_option_' . Constants::OPTION_BUSINESS_ID, 'after_business_id_set', 10, 1);
+add_action('add_option_' . Constants::OPTION_ACCESS_TOKEN, 'maybe_trigger_integration', 10, 3);
+add_action('update_option_' . Constants::OPTION_BUSINESS_ID, 'after_business_id_save', 10, 3);
+add_action('update_option_' . Constants::OPTION_ACCESS_TOKEN, 'maybe_trigger_integration', 10, 4);
diff --git a/src/Events/AppRemoved.php b/src/Events/AppRemoved.php
index 37c3b56..f7a4c71 100644
--- a/src/Events/AppRemoved.php
+++ b/src/Events/AppRemoved.php
@@ -2,10 +2,11 @@
use Hellotext\Api\Client;
use Hellotext\Api\Event;
+use Hellotext\Constants;
function hellotext_deactivate ($hellotext_business_id = null) {
if (!$hellotext_business_id) {
- $hellotext_business_id = get_option('hellotext_business_id');
+ $hellotext_business_id = get_option(Constants::OPTION_BUSINESS_ID);
}
if (!$hellotext_business_id) {
@@ -17,7 +18,7 @@ function hellotext_deactivate ($hellotext_business_id = null) {
add_action('hellotext_remove_integration', function ($business_id) {
Client::with_sufix()
- ->delete('/integrations/woo', [
+ ->delete(Constants::API_ENDPOINT_INTEGRATIONS_WOO, [
'shop' => [
'business_id' => $business_id,
]
diff --git a/src/Events/CartUpdates.php b/src/Events/CartUpdates.php
index d56e59b..0ac3fea 100644
--- a/src/Events/CartUpdates.php
+++ b/src/Events/CartUpdates.php
@@ -2,6 +2,7 @@
use Hellotext\Adapters\ProductAdapter;
use Hellotext\Api\Event;
+use Hellotext\Constants;
// We could listen for woocommerce_cart_updated event but this event is
// triggered too many times per cart update. Instead, we listen for the
@@ -27,8 +28,8 @@ function hellotext_cart_updated() {
);
// Set previous cart items and current cart items
- $previous_cart_items = isset($_SESSION['hellotext_cart_items'])
- ? json_decode(sanitize_text_field($_SESSION['hellotext_cart_items']), true)
+ $previous_cart_items = isset($_SESSION[Constants::SESSION_CART_ITEMS])
+ ? json_decode(sanitize_text_field($_SESSION[Constants::SESSION_CART_ITEMS]), true)
: array();
$current_cart_items = WC()->cart->get_cart();
@@ -44,7 +45,7 @@ function hellotext_cart_updated() {
}
// Save current cart items to session
- $_SESSION['hellotext_cart_items'] = json_encode($cart_items);
+ $_SESSION[Constants::SESSION_CART_ITEMS] = json_encode($cart_items);
// Calculate total cart value
$cart_total = WC()->cart->get_cart_contents_total();
@@ -81,11 +82,20 @@ function hellotext_cart_updated() {
}
// Trigger events, one for added and one for removed items
+ $event_map = [
+ 'added' => Constants::EVENT_CART_ADDED,
+ 'removed' => Constants::EVENT_CART_REMOVED,
+ ];
+
foreach ($changes as $event => $items) {
if (0 == count($items)) {
continue;
}
+ if (!isset($event_map[$event])) {
+ continue;
+ }
+
$event_data = array(
'amount' => $cart_total,
'currency' => $currency,
@@ -95,6 +105,6 @@ function hellotext_cart_updated() {
)
);
- (new Event())->track("cart.{$event}", $event_data);
+ (new Event())->track($event_map[$event], $event_data);
}
}
diff --git a/src/Events/CouponRedeemed.php b/src/Events/CouponRedeemed.php
index 47afe87..8c3717d 100644
--- a/src/Events/CouponRedeemed.php
+++ b/src/Events/CouponRedeemed.php
@@ -1,6 +1,7 @@
track('coupon.redeemed', [
+ ( new Event() )->track(Constants::EVENT_COUPON_REDEEMED, [
'object_parameters' => [
'type' => 'coupon',
'reference' => $coupon->get_id(),
diff --git a/src/Events/CustomOptionsUpdated.php b/src/Events/CustomOptionsUpdated.php
index 02cdffc..dc062af 100644
--- a/src/Events/CustomOptionsUpdated.php
+++ b/src/Events/CustomOptionsUpdated.php
@@ -1,8 +1,10 @@
get();
- $session = isset($_COOKIE['hello_session'])
- ? sanitize_text_field($_COOKIE['hello_session'])
+ $session = isset($_COOKIE[Constants::SESSION_COOKIE_NAME])
+ ? sanitize_text_field($_COOKIE[Constants::SESSION_COOKIE_NAME])
: null;
$encrypted_session = Session::encrypt($session);
- add_post_meta($order->get_id(), 'hellotext_session', $encrypted_session);
+ add_post_meta($order->get_id(), Constants::META_SESSION, $encrypted_session);
- $event->track('order.placed', array(
+ $event->track(Constants::EVENT_ORDER_PLACED, array(
'object_parameters' => $parsedOrder,
));
}
diff --git a/src/Events/OrderStatus.php b/src/Events/OrderStatus.php
index 13f975a..c51ad6e 100644
--- a/src/Events/OrderStatus.php
+++ b/src/Events/OrderStatus.php
@@ -3,11 +3,12 @@
use Hellotext\Adapters\OrderAdapter;
use Hellotext\Services\Session;
use Hellotext\Api\Event;
+use Hellotext\Constants;
add_action('woocommerce_order_status_changed', 'track_order_status', 10, 4);
function track_order_status ($order_id, $old_status, $new_status, $order) {
- $encrypted_session = get_post_meta($order_id, 'hellotext_session', true);
+ $encrypted_session = get_post_meta($order_id, Constants::META_SESSION, true);
$session = Session::decrypt($encrypted_session);
$orderAdapter = new OrderAdapter($order);
@@ -17,19 +18,19 @@ function track_order_status ($order_id, $old_status, $new_status, $order) {
switch ($new_status) {
case 'processing':
- $event->track('order.confirmed', array(
+ $event->track(Constants::EVENT_ORDER_CONFIRMED, array(
'object_parameters' => $orderAdapter->get(),
));
break;
case 'cancelled':
- $event->track('order.cancelled', array(
+ $event->track(Constants::EVENT_ORDER_CANCELLED, array(
'object_parameters' => $orderAdapter->get(),
));
break;
case 'completed':
- $event->track('order.delivered', array(
+ $event->track(Constants::EVENT_ORDER_DELIVERED, array(
'object_parameters' => $orderAdapter->get(),
));
break;
diff --git a/src/Events/ProductViewed.php b/src/Events/ProductViewed.php
index ac4cf5c..61f0dfb 100644
--- a/src/Events/ProductViewed.php
+++ b/src/Events/ProductViewed.php
@@ -2,6 +2,7 @@
use Hellotext\Adapters\ProductAdapter;
use Hellotext\Api\Event;
+use Hellotext\Constants;
add_action('woocommerce_after_single_product', 'hellotext_product_viewed');
@@ -10,7 +11,7 @@ function hellotext_product_viewed() {
do_action('hellotext_create_profile');
- ( new Event() )->track('product.viewed', array(
+ ( new Event() )->track(Constants::EVENT_PRODUCT_VIEWED, array(
'object_parameters' => ( new ProductAdapter($product) )->get()
));
}
diff --git a/src/Events/RefundReceived.php b/src/Events/RefundReceived.php
index 116b0c5..d4b908b 100644
--- a/src/Events/RefundReceived.php
+++ b/src/Events/RefundReceived.php
@@ -3,6 +3,7 @@
use Hellotext\Adapters\RefundAdapter;
use Hellotext\Services\Session;
use Hellotext\Api\Event;
+use Hellotext\Constants;
add_action( 'woocommerce_order_refunded', 'hellotext_refund_created', 10, 2 );
@@ -12,10 +13,10 @@ function hellotext_refund_created ($order_id, $refund_id) {
do_action('hellotext_create_profile', $order->get_user_id());
- $encrypted_session = get_post_meta($order_id, 'hellotext_session', true);
+ $encrypted_session = get_post_meta($order_id, Constants::META_SESSION, true);
$session = Session::decrypt($encrypted_session);
- ( new Event($session) )->track('refund.received', array(
+ ( new Event($session) )->track(Constants::EVENT_REFUND_RECEIVED, array(
'object_parameters' => ( new RefundAdapter($refund, $order) )->get(),
));
}
diff --git a/src/Misc/Settings.php b/src/Misc/Settings.php
index fa99a3d..1bcda5a 100644
--- a/src/Misc/Settings.php
+++ b/src/Misc/Settings.php
@@ -1,6 +1,7 @@
' . wp_kses( __( 'description.paragraphs.one', 'hellotext' ), array( 'a' => array( 'href' => array(), 'target' => array(), 'style' => array() ) ) ) . '
';
@@ -79,22 +80,24 @@ function hellotext_description_section_callback() {
function hellotext_business_id_field() {
?>
-
-
+
' . __('webchat_unavailable', 'hellotext') . '';
@@ -102,7 +105,9 @@ function hellotext_webchat_id_field() {
}
?>
-