Skip to content

Commit

Permalink
Break out opt-in-text into its own method
Browse files Browse the repository at this point in the history
We need a way to get the opt-in-text without needing to query through
the list of active plugins so we can send the text with the user
registration.
  • Loading branch information
tarecord committed Sep 26, 2023
1 parent 5c5bc89 commit 37578ab
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
37 changes: 25 additions & 12 deletions src/Telemetry/Opt_In/Opt_In_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,8 @@ public function get_args( string $stellar_slug ) {
__( 'We hope you love %s.', 'stellarwp-telemetry' ),
$optin_args['plugin_name']
);
$optin_args['intro'] = sprintf(
// Translators: The user name and the plugin name.
__(
'Hi, %1$s! This is an invitation to help our StellarWP community.
If you opt-in, some data about your usage of %2$s and future StellarWP Products will be shared with our teams (so they can work their butts off to improve).
We will also share some helpful info on WordPress, and our products from time to time.
And if you skip this, that’s okay! Our products still work just fine.',
'stellarwp-telemetry'
),
$optin_args['user_name'],
$optin_args['plugin_name']
);

$optin_args['intro'] = $this->get_intro( $optin_args['user_name'], $optin_args['plugin_name'] );

/**
* Filters the arguments for rendering the Opt-In modal.
Expand Down Expand Up @@ -230,4 +220,27 @@ public function get_opted_in_plugin_names() {

return $opted_in_plugins;
}

/**
* Gets the primary message displayed on the opt-in modal.
*
* @param string $user_name The display name of the user.
* @param string $plugin_name The name of the plugin.
*
* @return string
*/
public function get_intro( $user_name, $plugin_name ) {
return sprintf(
// Translators: The user name and the plugin name.
esc_html__(
'Hi, %1$s! This is an invitation to help our StellarWP community.
If you opt-in, some data about your usage of %2$s and future StellarWP Products will be shared with our teams (so they can work their butts off to improve).
We will also share some helpful info on WordPress, and our products from time to time.
And if you skip this, that’s okay! Our products still work just fine.',
'stellarwp-telemetry'
),
$user_name,
$plugin_name
);
}
}
7 changes: 3 additions & 4 deletions src/Telemetry/Telemetry/Telemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,14 @@ protected function get_user_details( string $stellar_slug = '' ) {
$stellar_slug = Config::get_stellar_slug();
}

$modal_args = Config::get_container()->get( Opt_In_Template::class )->get_args( $stellar_slug );

$user = wp_get_current_user();
$user = wp_get_current_user();
$template = Config::get_container()->get( Opt_In_Template::class );

$args = [
'name' => $user->display_name,
'email' => $user->user_email,
'plugin_slug' => $stellar_slug,
'opt_in_text' => $modal_args['intro'],
'opt_in_text' => $template->get_intro( $user->display_name, $stellar_slug ),
];

/**
Expand Down
75 changes: 75 additions & 0 deletions tests/wpunit/TemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Codeception\TestCase\WPTestCase;
use StellarWP\Telemetry\Admin\Resources;
use StellarWP\Telemetry\Opt_In\Opt_In_Template;
use StellarWP\Telemetry\Opt_In\Status;
use StellarWP\Telemetry\Tests\Support\Traits\With_Test_Container;
use StellarWP\Telemetry\Tests\Support\Traits\With_Uopz;

class TemplateTest extends WPTestCase {

use With_Test_Container;
use With_Uopz;

public function get_default_template_data() {
return [
'plugin_logo' => Resources::get_asset_path() . 'resources/images/stellar-logo.svg',
'plugin_logo_width' => 151,
'plugin_logo_height' => 32,
'plugin_logo_alt' => 'StellarWP Logo',
'plugin_name' => 'StellarWP',
'plugin_slug' => 'telemetry-library',
'user_name' => 'admin',
'permissions_url' => '#',
'tos_url' => '#',
'privacy_url' => 'https://stellarwp.com/privacy-policy/',
'opted_in_plugins_text' => 'See which plugins you have opted in to tracking for',
'opted_in_plugins' => [],
'heading' => 'We hope you love StellarWP.',
'intro' => 'Hi, admin! This is an invitation to help our StellarWP community.
If you opt-in, some data about your usage of StellarWP and future StellarWP Products will be shared with our teams (so they can work their butts off to improve).
We will also share some helpful info on WordPress, and our products from time to time.
And if you skip this, that’s okay! Our products still work just fine.',
];
}

public function test_should_return_basic_defaults() {
$status = new Status();

$this->set_fn_return(
'wp_get_current_user',
static function () {
return new WP_User( 1, 'admin' );
},
true
);

update_option(
$status->get_option_name(),
[
'plugins' => [
'the-events-calendar' => [
'wp_slug' => 'the-events-calendar/the-events-calendar.php',
'optin' => false,
],
],
'token' => 'abcd1234',
]
);

$expected = $this->get_default_template_data();
$actual = ( new Opt_In_Template( $status ) )->get_args( 'telemetry-library' );

$this->assertIsArray( $actual );
$this->assertEquals( $expected, $actual );
}

public function test_get_intro() {
$expected = $this->get_default_template_data();
$actual = ( new Opt_In_Template( new Status() ) )->get_intro( 'admin', 'StellarWP' );

$this->assertIsString( $actual );
$this->assertEquals( $expected['intro'], $actual );
}
}

0 comments on commit 37578ab

Please sign in to comment.