';
/**
* Hook suffixes of the two menu entries, to know when the settings page
* itself is on screen.
*
* @var string[]
*/
private $page_hooks = array();
/**
* Boot.
*/
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_widget' ) );
add_action( 'wp_head', array( $this, 'print_tenant_meta' ), 1 );
add_filter( 'script_loader_tag', array( $this, 'add_widget_attributes' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) );
add_action( 'admin_notices', array( $this, 'print_setup_notice' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_notice_script' ) );
add_action( 'wp_ajax_' . self::NOTICE_ACTION, array( $this, 'dismiss_setup_notice' ) );
}
/**
* The configured tenant ID, or '' when the plugin has not been connected yet.
*
* @return string
*/
private function get_tenant_id() {
return (string) get_option( self::OPTION_KEY, '' );
}
/**
* A tenant ID is a short Firestore-style document id. Validating the SHAPE
* (not just sanitising) means a pasted URL, an email, or a stray quote never
* reaches the page as a data attribute.
*
* @param string $value Raw value.
* @return string Valid id, or '' when it does not look like one.
*/
public static function clean_tenant_id( $value ) {
$value = trim( (string) $value );
return preg_match( '/^[A-Za-z0-9_-]{6,64}$/', $value ) ? $value : '';
}
/**
* Settings sanitiser for the Workspace ID. An emptied field disconnects on
* purpose; anything else that is not an id keeps the id already saved and
* says why. Without this a stray paste (a URL, an email) wiped a working id
* under a "Settings saved." banner and the chat silently left the site.
*
* @param mixed $value Raw value from the form.
* @return string
*/
public static function sanitize_tenant_id( $value ) {
$raw = trim( (string) $value );
if ( '' === $raw ) {
return '';
}
$clean = self::clean_tenant_id( $raw );
if ( '' !== $clean ) {
return $clean;
}
// WordPress runs this callback twice when the option is first created
// (update_option, then add_option): add the message once.
foreach ( get_settings_errors( self::OPTION_KEY ) as $error ) {
if ( 'chattoko_invalid_id' === $error['code'] ) {
return (string) get_option( self::OPTION_KEY, '' );
}
}
add_settings_error(
self::OPTION_KEY,
'chattoko_invalid_id',
__( 'That does not look like a Workspace ID, so it was not saved. A Workspace ID is 6 to 64 letters, numbers, dashes or underscores, with no spaces. Copy it from Settings → Installation in your Chattoko dashboard.', 'chattoko' )
);
return (string) get_option( self::OPTION_KEY, '' );
}
/**
* The Workspace ID as a meta tag, read by the widget when its script tag no
* longer carries data-tenant. Optimisation plugins that combine external and
* inline scripts (LiteSpeed Cache "Combine External and Inline JS") copy
* widget.js into their own bundle and drop the attribute with the tag; the
* widget then found no workspace and stayed off. A meta tag survives them.
*/
public function print_tenant_meta() {
$tenant = $this->get_tenant_id();
if ( '' === $tenant ) {
return;
}
printf( '' . "\n", esc_attr( $tenant ) );
}
/**
* Queue the hosted widget. Nothing is loaded until a Workspace ID is saved, so
* an installed-but-unconfigured plugin contacts no external server at all.
*/
public function enqueue_widget() {
if ( '' === $this->get_tenant_id() ) {
return;
}
wp_enqueue_script(
self::SCRIPT_HANDLE,
self::WIDGET_URL,
array(),
self::VERSION,
true // Footer.
);
$this->maybe_add_cart_bridge();
}
/**
* The widget is activated by a data-tenant attribute, which wp_enqueue_script
* cannot express. script_loader_tag (WP 4.1+) is used rather than
* wp_script_attributes (6.3+) so the plugin keeps working on older sites.
*
* The attributes are SPLICED into the existing tag rather than the tag being
* rebuilt. That matters: WP_Scripts::do_item() concatenates any inline
* scripts into $tag before running this filter, so returning a fresh
*