Skip to content

Commit

Permalink
Big docs update to simplify and expand
Browse files Browse the repository at this point in the history
  • Loading branch information
zackkatz committed Aug 25, 2024
1 parent 1e161e0 commit cfd8521
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 188 deletions.
151 changes: 5 additions & 146 deletions docs/Client/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,158 +5,17 @@ sidebar_position: 1
---

# TrustedLogin SDK

Easily and securely log in to your customers sites when providing support.

## Our priority: Be secure and [don't crash sites](https://www.bugsnag.com/blog/sdks-should-not-crash-apps) {#our-priority-sdks-should-not-crash-sites}

When you integrate TrustedLogin into your project (theme, plugin, or custom code), you are counting on us not to mess up your customer or clients' sites. We take that extremely seriously.

-------

## Including in your plugin or theme {#including-in-your-plugin-or-theme}

:::info
### When you see `ProBlockBuilder`, make sure to replace with your own namespace!
In the examples below, we're going to pretend your plugin or theme is named "Pro Block Builder" and your business is named Widgets, Co. These should not be the names you use—make sure to update the sample code below to match your business and plugin/theme name!
:::

### Install Strauss & update your composer.json file

1. [Install Strauss](https://github.com/BrianHenryIE/strauss#use). Strauss is used for namespacing the Client to prevent conflicts with other plugins or themes that are using TrustedLogin. We recommend installing via the `strauss.phar` method.
1. `cd` into your plugin or theme directory
1. Run `curl -o strauss.phar -L -C - https://github.com/BrianHenryIE/strauss/releases/latest/download/strauss.phar`
1. Run `composer require trustedlogin/client:dev-main` to install the TrustedLogin Client SDK
1. Run `composer require scssphp/scssphp --dev` to install `scssphp` as a dev dependency. This is used to generate and namespace the CSS used by TrustedLogin. If you already have `scssphp` installed, or are [using an alternative way to namespace the CSS](/Client/css-namespacing.md), skip this step.
1. Update your `composer.json` file to integrate with Strauss. Follow the instructions as detailed in the [Strauss documentation](https://github.com/BrianHenryIE/strauss#configuration) for namespacing your plugin and theme. See example below.

```json
[...]
"require": {
"trustedlogin/client": "dev-main"
},
"require-dev": {
"brianhenryie/strauss": "dev-master",
"scssphp/scssphp": "^v1.11.0"
},
"autoload": {
"classmap": [
"vendor"
]
},
"extra": {
"strauss": {
"target_directory": "vendor-namespaced",
"namespace_prefix": "ProBlockBuilder\\",
"classmap_prefix": "ProBlockBuilder_",
"classmap_output": true,
"packages": [
"trustedlogin/client"
]
}
},
"scripts": {
"strauss": [
"@php strauss.phar"
],
"trustedlogin": [
"@php vendor/bin/build-sass --namespace=ProBlockBuilder"
],
"post-install-cmd": [
"@strauss",
"@trustedlogin"
],
"post-update-cmd": [
"@strauss",
"@trustedlogin"
]
}
[...]
```
4. Run `composer update` to update your dependencies. Strauss should generate a `vendor-namespaced/` directory. If it doesn't, you may need to run `composer install` first.
5. Follow [these directions to configure and instantiate the client](./configuration)

#### To manually include the autoloader

If you chose to set `classmap_output` to `false` in the Strauss configuration, you will need to include the autoloader in your code. If using the sample above, it would be located at `vendor-namepaced/autoload.php`; the code would be something like:

```php
// For a plugin or theme:
include_once trailingslashit( dirname( __FILE__ ) ) . 'vendor-namespaced/autoload.php';
```

#### Vendor directory cleanup

If you find the TrustedLogin directories in your `vendor/` directory to be undesirable for some reason, you may use this configuration for the `trustedlogin` script in Composer.

Replace this:

```json
"trustedlogin": [
"@php vendor/bin/build-sass --namespace=ProBlockBuilder"
],
```

With this:

```json
"trustedlogin": [
"@php vendor/bin/build-sass --namespace=ProBlockBuilder",
"[ -d 'vendor/trustedlogin' ] && rm -rf vendor/trustedlogin || true",
"[ -d 'vendor/scssphp' ] && rm -rf vendor/scssphp || true",
"[ -d 'vendor/bin' ] && rm -rf vendor/bin/build-sass && rm -rf vendor/bin/pscss || true"
],
```

The script modification will now remove the `trustedlogin`, `scssphp`, and TrustedLogin-related files inside `bin`.

### No-Conflict mode {#no-conflict-mode}

Some plugins like Gravity Forms and GravityView have a "no-conflict mode" to limit script and style conflicts. If you see
scripts and styles not loading on your Grant Support Access page, that's what's going on.

The WordPress script and style handles registered by TrustedLogin are formatted as `trustedlogin-{namespace}`.
Here's an example of how GravityView (with a namespace of `gravityview`) allows TrustedLogin scripts:

```php
add_filter( 'gravityview_noconflict_scripts', function ( $allowed_scripts = array() ) {

$allowed_scripts[] = 'trustedlogin-gravityview'; // ⚠️ GravityView's namespace is `gravityview`

return $allowed_scripts;
} );
```

### Testing on local environments {#testing-on-local-environments}

TrustedLogin won't work in local environments unless using a tunnel such as ngrok. Thus, TrustedLogin will display a warning when attempting to generate a login when in a local environment.

To disable the warning, define `TRUSTEDLOGIN_DISABLE_LOCAL_NOTICE` and set it to true:

```php
define( 'TRUSTEDLOGIN_DISABLE_LOCAL_NOTICE', true );
```

## Reference IDs {#reference-ids}

Reference IDs are useful when you want to attach a specific ticket ID or conversation ID to a login.

Reference IDs can be passed via URL like so: `wp-login.php?action=trustedlogin&ns={namespace}&ref=[123]`

When a Reference ID exists, users will see the reference while granting access:

![Reference ID is shown below the footer links in the Grant Access screen](/img/client/reference-id.png)

## Logging {#logging}

We recommend disabling logging by default, but sometimes logs are necessary.
## What is TrustedLogin?

1. TrustedLogin creates a `trustedlogin-logs` directory inside the `wp-content/uploads/` directory.
2. An empty `index.html` file is placed inside the directory to prevent browsing.
3. New log files are created daily for each TrustedLogin namespace. The default log `filename` format is `client-{namespace}-{Y-m-d}-{hash}.log`
- `{namespace}` is the namespace of your business, plugin, or theme name
- `{date}` is `YYYY-MM-DD` format
- The hash is generated using `wp_hash()` using on the `vendor/namespace`, site `home_url()`, and the day of the year (`date('z')`). The point of the hash is to make log names harder to guess (security by obscurity).
TrustedLogin is a secure and easy-to-use SDK that allows you to log in to your customers' WordPress sites without them sharing passwords. It is designed to be easy to integrate into your plugin, theme, or custom code.

### Using your own logging library {#using-your-own-logging-library}
## Why use TrustedLogin?

If you add an action for `trustedlogin/{namespace}/logging/log`, TrustedLogin will let you handle logging. The `trustedlogin-logs` directory and log files will not be created.
Customer support is a critical part of any business. TrustedLogin makes it easy to provide support to your customers without the need for them to share their passwords. This is a huge win for both you and your customers.
155 changes: 155 additions & 0 deletions docs/Client/02-installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
---
title: Installation
sidebar: auto
sidebar_position: 2
---

## Including in your plugin or theme {#including-in-your-plugin-or-theme}

### 1. Install the TrustedLogin SDK using Composer

Run `composer require trustedlogin/client:dev-main` to install the TrustedLogin Client SDK as a dependency.

### 2. Add SCSS as a dev dependency

Run `composer require scssphp/scssphp --dev` to install `scssphp` as a dev dependency.

This is used to generate and namespace the CSS used by TrustedLogin. If you already have `scssphp` installed, or are [using an alternative way to namespace the CSS](/Client/css-namespacing.md), skip this step.

### 3. Namespace the SDK using [Strauss](/Client/namespacing/strauss) or [PHP-Scoper](/Client/namespacing/php-scoper).

In order to prevent conflicts with other plugins or themes that are using TrustedLogin, you must namespace the TrustedLogin Client SDK.

We support two namespacing tools: Strauss and PHP-Scoper. Choose the one that best fits your needs:

- [Strauss](/Client/namespacing/strauss)
- [PHP-Scoper](/Client/namespacing/php-scoper)

### 4. [Namespace the CSS files](/Client/Namespacing/css-namespacing)

TrustedLogin CSS files are namespaced so that they don't conflict with other plugins or themes that are using TrustedLogin.

Follow the [CSS Namespacing](/Client/css-namespacing.md) guide.

### 4. Include the autoloader

Make sure you have the Composer autoloader included in your plugin or theme. If you already have the autoloader included, you can skip this step.

```php
// Include the Composer autoloader.
require_once trailingslashit( dirname( __FILE__ ) ) . 'vendor/autoload.php';
```

Make sure to **include the autoloader on all page loads** to ensure the TrustedLogin SDK is available when needed.

### 5. Customize the [TrustedLogin configuration](/Client/configuration) options

The configuration array is where you set up the TrustedLogin Client SDK to work with your plugin or theme. You can customize the configuration to match your needs.

### 6. Instantiate the TrustedLogin Client

:::info
The TrustedLogin client must be initialized on all page loads, both the front-end and the dashboard.
:::

When instantiating the TrustedLogin `Client` class, you need to pass a valid `Config` object.

The following is a minimal configuration. It has all the _required_ settings, but not all **recommended** settings!

```php
/**
* This is a basic example of how to instantiate the TrustedLogin Client, using the minimum required configuration
* settings and hooked into the `plugins_loaded` action. Adjust the configuration to match your needs.
*/
add_action( 'plugins_loaded', function() {
$config = [
'auth' => [
'api_key' => '1234567890',
],
'vendor' => [
'namespace' => 'pro-block-builder',
'title' => 'Pro Block Builder',
'email' => 'support@example.com',
'website' => 'https://example.com',
'support_url' => 'https://help.example.com',
],
'role' => 'editor',
];

try {
new \ProBlockBuilder\TrustedLogin\Client(
new \ProBlockBuilder\TrustedLogin\Config( $config )
);
} catch ( \Exception $exception ) {
error_log( $exception->getMessage() );
}
} );
```

#### Hooking the TrustedLogin Client instantiation

We recommend instantiating the TrustedLogin Client on the `plugins_loaded` action. This ensures that the TrustedLogin Client is available on all page loads.

TrustedLogin calls the following hooks:
- `init` priority `100`
- `admin_init` priority `100`
- `template_redirect` priority `99`

Instantiating the Client after any of these hooks are called will cause TrustedLogin to not function properly.

:::warning
**Always wrap TrustedLogin Client instantiation in a try/catch block!**
:::

TrustedLogin Client instantiation must be wrapped in a try/catch block. The TrustedLogin Client throws Exceptions when:

- The configuration is invalid.
- TrustedLogin is globally disabled.
- TrustedLogin is disabled for the namespace.
- The current website lacks expected encryption functions (these _should_ be included in all WordPress installations as well as PHP 7.2).

Wrapping the instantiation in a try/catch block ensures that the site won't crash if TrustedLogin fails to initialize.

------

## Advanced

### Vendor directory cleanup

If you find the TrustedLogin directories in your `vendor/` directory to be undesirable for some reason, you may use this configuration for the `trustedlogin` script in Composer.

:::info
### When you see `ProBlockBuilder`, make sure to replace with your own namespace!
In the examples below, we're going to pretend your plugin or theme is named "Pro Block Builder" and your business is named Widgets, Co. These should not be the names you use—make sure to update the sample code below to match your business and plugin/theme name!
:::

Replace this:

```json
"trustedlogin": [
"@php vendor/bin/build-sass --namespace=ProBlockBuilder"
],
```

With this:

```json
"trustedlogin": [
"@php vendor/bin/build-sass --namespace=ProBlockBuilder",
"[ -d 'vendor/trustedlogin' ] && rm -rf vendor/trustedlogin || true",
"[ -d 'vendor/scssphp' ] && rm -rf vendor/scssphp || true",
"[ -d 'vendor/bin' ] && rm -rf vendor/bin/build-sass && rm -rf vendor/bin/pscss || true"
],
```

The script modification will now remove the `trustedlogin`, `scssphp`, and TrustedLogin-related files inside `bin`.

### Testing on local environments {#testing-on-local-environments}

TrustedLogin won't work in local environments unless using a tunnel such as ngrok. Thus, TrustedLogin will display a warning when attempting to generate a login when in a local environment.

To disable the warning, define `TRUSTEDLOGIN_DISABLE_LOCAL_NOTICE` and set it to true:

```php
define( 'TRUSTEDLOGIN_DISABLE_LOCAL_NOTICE', true );
```
Loading

0 comments on commit cfd8521

Please sign in to comment.