Skip to content

Commit

Permalink
Clarify docs about toggling opt-in status via a Settings page.
Browse files Browse the repository at this point in the history
Fixes #109.
  • Loading branch information
TimothyBJacobs committed Jan 26, 2024
1 parent a4bc84f commit 953bd72
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ do_action( 'stellarwp/telemetry/optin', '{stellar_slug}', [ 'plugin_slug' => 'th
## Saving Opt-In Status on a Settings Page
When implementing the library, settings should be available for site administrators to change their opt-in status at any time. The value passed to `set_status()` should be a boolean.

When a user want's to opt in, call the `Opt_In_Subscriber::opt_in` method instead of `Status::set_status` to ensure that the site is registered with the telemetry server.

```php
add_action( 'admin_init', 'save_opt_in_setting_field' );

Expand All @@ -168,11 +170,16 @@ public function save_opt_in_setting_field() {

// Get an instance of the Status class.
$Status = Config::get_container()->get( Status::class );
$opt_in = Config::get_container()->get( Opt_In_Subscriber::class );

// Get the value submitted on the settings page as a boolean.
$value = filter_input( INPUT_POST, 'opt-in-status', FILTER_VALIDATE_BOOL );

$Status->set_status( $value );
if ( $value ) {
$opt_in->opt_in( '{stellar_slug}' );
} else {
$Status->set_status( $value );
}
}
```

Expand Down

0 comments on commit 953bd72

Please sign in to comment.