5, 'headers' => array( 'Content-Type' => 'application/json' ) + $headers, ); if ( null !== $body ) { $args['body'] = wp_json_encode( $body ); } if ( '' !== $api_key ) { $args['headers']['X-TruSend-Api-Key'] = $api_key; } $response = wp_remote_post( TRUSEND_API_URL . $path, $args ); if ( is_wp_error( $response ) ) { return array( 0, array() ); } $decoded = json_decode( wp_remote_retrieve_body( $response ), true ); return array( (int) wp_remote_retrieve_response_code( $response ), is_array( $decoded ) ? $decoded : array() ); } /** * `appId` is null while the integration is turned off in TruSend: no chat on the site, but the key stays. */ function trusend_app_id_from( $config ) { return is_array( $config ) && isset( $config['appId'] ) && is_string( $config['appId'] ) ? $config['appId'] : ''; } /** * chat-api ties the workspace to this address and takes an ASCII host only, so an IDN host goes as punycode. */ function trusend_site_url() { $url = home_url(); $host = (string) wp_parse_url( $url, PHP_URL_HOST ); if ( function_exists( 'idn_to_ascii' ) && preg_match( '/[^\x00-\x7F]/', $host ) ) { $ascii = idn_to_ascii( $host, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46 ); if ( $ascii ) { $url = str_replace( $host, $ascii, $url ); } } return $url; } function trusend_settings_url() { return admin_url( 'options-general.php?page=trusend' ); } function trusend_forget() { delete_option( 'trusend_api_key' ); delete_option( 'trusend_app_id' ); delete_option( 'trusend_orders_key_id' ); wp_clear_scheduled_hook( 'trusend_refresh' ); } /** * Re-reads the widget config. The portal promises the chat leaves the site once the key is * revoked (Disconnect, Rotate API key) or the integration is turned off. */ function trusend_refresh() { trusend_sweep_nonces(); trusend_check( trusend_orders_ready() ); } add_action( 'trusend_refresh', 'trusend_refresh' ); /** * The same check also tells chat-api whether it may look up orders here (CHA-1920). The capability * belongs to this exact key: after a re-pair or a rotated key chat-api waits for the new key to say so. * * @param bool $orders Whether the orders route below can answer. */ function trusend_check( $orders ) { $api_key = get_option( 'trusend_api_key' ); if ( ! $api_key ) { return; } list( $status, $config ) = trusend_api( '/integrations/wp/widget-config', null, $api_key, array( 'X-TruSend-Orders-Api' => $orders ? '1' : '0' ) ); if ( 401 === $status ) { trusend_forget(); update_option( 'trusend_revoked', 1 ); } elseif ( 200 === $status ) { update_option( 'trusend_app_id', trusend_app_id_from( $config ), false ); // The decimal id chat-api signs order lookups with; empty — it does not ask. $key_id = $orders && isset( $config['ordersKeyId'] ) && is_string( $config['ordersKeyId'] ) ? $config['ordersKeyId'] : ''; update_option( 'trusend_orders_key_id', $key_id, false ); } // Anything else — chat-api down, mid-deploy — keeps the last good app id. } add_action( 'init', function () { if ( get_option( 'trusend_api_key' ) && ! wp_next_scheduled( 'trusend_refresh' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'hourly', 'trusend_refresh' ); } } ); /** * WooCommerce switched on or off, or the permalinks changed: the orders capability goes to chat-api * on the next request, where the plugins have settled — in this one a deactivated WooCommerce is * still loaded. */ function trusend_refresh_soon() { if ( get_option( 'trusend_api_key' ) ) { wp_schedule_single_event( time(), 'trusend_refresh' ); } } add_action( 'activated_plugin', 'trusend_refresh_soon' ); add_action( 'deactivated_plugin', 'trusend_refresh_soon' ); add_action( 'permalink_structure_changed', 'trusend_refresh_soon' ); register_deactivation_hook( __FILE__, function () { wp_clear_scheduled_hook( 'trusend_refresh' ); // With the plugin off nothing answers chat-api's order lookups. trusend_check( false ); } ); // ── Storefront ─────────────────────────────────────────────────────────────── /** * What kind of page this is, in the widget's words — Shopify's `request.page_type` (CHA-1916). * Null leaves it to the widget, which reads the address. The conditional tags answer false, * silently, before the `wp` hook; `wp_enqueue_scripts` runs well after it. */ function trusend_page_type() { // A shop set as the front page is still the home page. if ( is_front_page() ) { return 'index'; } if ( function_exists( 'is_product' ) ) { if ( is_product() ) { return 'product'; } if ( is_shop() || is_product_taxonomy() ) { return 'collection'; } if ( is_cart() ) { return 'cart'; } if ( is_checkout() ) { return 'checkout'; } // Before is_account_page(), which is true on every endpoint of My account. if ( is_wc_endpoint_url( 'orders' ) || is_wc_endpoint_url( 'view-order' ) ) { return 'customers/order'; } if ( is_account_page() ) { return 'customers/account'; } } if ( is_search() ) { return 'search'; } if ( is_404() ) { return '404'; } if ( is_singular( 'post' ) ) { return 'article'; } if ( is_page() ) { return 'page'; } if ( is_home() || is_archive() ) { return 'blog'; } return null; } function trusend_page() { $type = trusend_page_type(); $page = null === $type ? null : array( 'type' => $type ); if ( 'product' === $type ) { $product = wc_get_product( get_queried_object_id() ); if ( $product ) { $page['product'] = array( 'handle' => $product->get_slug(), 'title' => $product->get_name(), ); } } /** * Filters what the chat is told about the current page. * * @param array|null $page `type` and, on a product page, `product` with `handle` and `title`. * Null lets the widget work it out from the address. */ return apply_filters( 'trusend_page', $page ); } add_action( 'wp_enqueue_scripts', function () { $app_id = get_option( 'trusend_app_id' ); /** * Filters whether the TruSend chat shows on the current page. * * @param bool $show Default true. */ if ( ! $app_id || ! apply_filters( 'trusend_show_widget', true ) ) { return; } $signed_in = is_user_logged_in(); wp_enqueue_script( 'trusend-chat', plugins_url( 'assets/boot.js', __FILE__ ), array(), TRUSEND_VERSION, true ); wp_add_inline_script( 'trusend-chat', 'var trusendChat = ' . wp_json_encode( array( 'appId' => $app_id, 'page' => trusend_page(), 'widgetUrl' => TRUSEND_WIDGET_URL, 'apiUrl' => TRUSEND_API_URL, 'cookiePath' => COOKIEPATH, // Only a signed-in user asks for a token; everyone else chats anonymously. 'tokenUrl' => $signed_in ? rest_url( 'trusend/v1/visitor-token' ) : '', 'nonce' => $signed_in ? wp_create_nonce( 'wp_rest' ) : '', ), // A product title is shop-manager text inside a