true, 'strategy' => 'defer', ) ); // phpcs:enable wp_add_inline_script( self::HANDLE, self::inline_script( $s ), 'before' ); } // embed.js reads its agent from its own tag's data-agent-id. public static function agent_attribute( $tag, $handle ) { if ( self::HANDLE !== $handle ) { return $tag; } $key = ReplyBash_Options::connection()['key']; return str_replace( ' src=', ' data-agent-id="' . esc_attr( $key ) . '" src=', $tag ); } /** Should the chat show on this request's page? */ public static function shows_here( $s ) { if ( $s['hide_staff'] && current_user_can( 'edit_posts' ) ) { return false; } if ( 'all' === $s['pages_mode'] ) { return true; } $path = self::current_path(); $matched = false; foreach ( array_filter( explode( "\n", $s['pages'] ) ) as $pattern ) { if ( self::path_matches( $path, $pattern ) ) { $matched = true; break; } } return 'only' === $s['pages_mode'] ? $matched : ! $matched; } /** This request's path inside the site ("/pricing"), whatever folder WordPress is installed in. */ public static function current_path() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; $path = strtolower( (string) wp_parse_url( $uri, PHP_URL_PATH ) ); $base = strtolower( untrailingslashit( (string) wp_parse_url( home_url(), PHP_URL_PATH ) ) ); if ( '' !== $base && 0 === strpos( $path, $base ) ) { $path = substr( $path, strlen( $base ) ); } return '/' . trim( $path, '/' ); } /** "/blog/*" matches /blog and everything under it; "/*" matches every page; others match exactly. */ public static function path_matches( $path, $pattern ) { if ( '/*' === $pattern ) { return true; } if ( '/*' === substr( $pattern, -2 ) ) { $prefix = substr( $pattern, 0, -2 ); return $path === $prefix || 0 === strpos( $path, $prefix . '/' ); } return $path === $pattern; } public static function mark_logout() { if ( ReplyBash_Options::settings()['identify'] ) { setcookie( self::RESET_COOKIE, '1', time() + HOUR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), false ); } } private static function inline_script( $s ) { $js = 'window.replybash=window.replybash||function(){(replybash.q=replybash.q||[]).push(arguments)};'; if ( ! $s['identify'] ) { return $js; } // After logging out, forget the user and their open chat once, so the next person starts fresh. // Read in the browser, so it works on cached pages too. $js .= 'if(document.cookie.indexOf("' . self::RESET_COOKIE . '=1")>-1){replybash("reset");document.cookie="' . self::RESET_COOKIE . '=; Max-Age=0; path=/"}'; $user = wp_get_current_user(); if ( $user->exists() && '' !== $s['identity_secret'] ) { $id = 'wp-' . $user->ID; $data = array( 'userId' => $id, 'hash' => hash_hmac( 'sha256', $id, $s['identity_secret'] ), 'name' => $user->display_name, 'email' => $user->user_email, ); $js .= 'replybash("identify",' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) . ');'; } return $js; } }