added custom information to HS Beacon

This commit is contained in:
Jonathan Labreuille
2016-11-04 14:11:37 +01:00
parent ac8d1d808f
commit b8cb1da777
5 changed files with 192 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ class Renderer {
$this->setupTranslations();
$this->setupFunctions();
$this->setupHandlebars();
$this->setupHelpscout();
$this->setupGlobalVariables();
$this->setupSyntax();
@@ -43,6 +44,10 @@ class Renderer {
$this->renderer->addExtension(new Twig\Handlebars());
}
function setupHelpscout() {
$this->renderer->addExtension(new Twig\Helpscout());
}
function setupGlobalVariables() {
$this->renderer->addExtension(new Twig\Assets(array(
'assets_url' => Env::$assets_url,

50
lib/Helpscout/Beacon.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
namespace MailPoet\Helpscout;
use MailPoet\Models\Subscriber;
use MailPoet\Models\Setting;
if(!defined('ABSPATH')) exit;
class Beacon {
static function getData() {
$current_theme = wp_get_theme();
$mta = Setting::getValue('mta');
global $wpdb;
$db_version = $wpdb->get_var('SELECT @@VERSION');
// curent user
$current_user = wp_get_current_user();
return array(
'name' => $current_user->display_name,
'email' => $current_user->user_email,
'PHP version' => PHP_VERSION,
'MailPoet version' => MAILPOET_VERSION,
'WordPress version' => get_bloginfo('version'),
'Database version' => $db_version,
'WP_MEMORY_LIMIT' => WP_MEMORY_LIMIT,
'WP_MAX_MEMORY_LIMIT' => WP_MAX_MEMORY_LIMIT,
'WP_DEBUG' => WP_DEBUG,
'PHP max_execution_time' => ini_get('max_execution_time'),
'PHP memory_limit' => ini_get('memory_limit'),
'PHP upload_max_filesize' => ini_get('upload_max_filesize'),
'PHP post_max_size' => ini_get('post_max_size'),
'WordPress language' => get_locale(),
'Multisite environment?' => (is_multisite() ? 'Yes' : 'No'),
'Current Theme' => $current_theme->get('Name').
' (version '.$current_theme->get('Version').')',
'Active Plugin names' => join(", ", get_option('active_plugins')),
'Sending Method' => $mta['method'],
'Sending Frequency' => sprintf('%d emails every %d minutes',
$mta['frequency']['emails'],
$mta['frequency']['interval']
),
'Task Scheduler method' => Setting::getValue('cron_trigger.method'),
'Default FROM address' => Setting::getValue('sender.address'),
'Default Reply-To address' => Setting::getValue('reply_to.address'),
'Bounce Email Address' => Setting::getValue('bounce.address'),
'Total number of subscribers' => Subscriber::getTotalSubscribers()
);
}
}

21
lib/Twig/Helpscout.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace MailPoet\Twig;
if(!defined('ABSPATH')) exit;
class Helpscout extends \Twig_Extension {
public function getName() {
return 'helpscout';
}
public function getFunctions() {
return array(
new \Twig_SimpleFunction(
'get_helpscout_data',
'\MailPoet\Helpscout\Beacon::getData',
array('is_safe' => array('all'))
)
);
}
}

View File

@@ -0,0 +1,107 @@
<?php
use \MailPoet\Helpscout\Beacon;
use \MailPoet\Models\Setting;
use \MailPoet\Models\Subscriber;
class BeaconTest extends MailPoetTest {
function _before() {
// create 3 users (1 confirmed, 1 subscribed, 1 unsubscribed)
Subscriber::createOrUpdate(array(
'email' => 'user1@mailpoet.com',
'status' => Subscriber::STATUS_SUBSCRIBED
));
Subscriber::createOrUpdate(array(
'email' => 'user2@mailpoet.com',
'status' => Subscriber::STATUS_UNCONFIRMED
));
Subscriber::createOrUpdate(array(
'email' => 'user3@mailpoet.com',
'status' => Subscriber::STATUS_UNSUBSCRIBED
));
$this->beacon_data = Beacon::getData();
}
function testItReturnsPhpVersion() {
expect($this->beacon_data['PHP version'])->equals(PHP_VERSION);
}
function testItReturnsMailpoetVersion() {
expect($this->beacon_data['MailPoet version'])->equals(MAILPOET_VERSION);
}
function testItReturnsWordpressVersion() {
expect($this->beacon_data['WordPress version'])->equals(get_bloginfo('version'));
}
function testItReturnsDatabaseVersion() {
global $wpdb;
$db_version = $wpdb->get_var('SELECT @@VERSION');
expect($this->beacon_data['Database version'])->equals($db_version);
}
function testItReturnsWpMemoryLimit() {
expect($this->beacon_data['WP_MEMORY_LIMIT'])->equals(WP_MEMORY_LIMIT);
}
function testItReturnsWpMaxMemoryLimit() {
expect($this->beacon_data['WP_MAX_MEMORY_LIMIT'])->equals(WP_MAX_MEMORY_LIMIT);
}
function testItReturnsWpDebugValue() {
expect($this->beacon_data['WP_DEBUG'])->equals(WP_DEBUG);
}
function testItReturnsPhpMaxExecutionTime() {
expect($this->beacon_data['PHP max_execution_time'])->equals(ini_get('max_execution_time'));
}
function testItReturnsPhpMemoryLimit() {
expect($this->beacon_data['PHP memory_limit'])->equals(ini_get('memory_limit'));
}
function testItReturnsPhpUploadMaxFilesize() {
expect($this->beacon_data['PHP upload_max_filesize'])->equals(ini_get('upload_max_filesize'));
}
function testItReturnsPhpPostMaxSize() {
expect($this->beacon_data['PHP post_max_size'])->equals(ini_get('post_max_size'));
}
function testItReturnsWpLanguage() {
expect($this->beacon_data['WordPress language'])->equals(get_locale());
}
function testItReturnsIfWpIsMultisite() {
expect($this->beacon_data['Multisite environment?'])->equals(is_multisite() ? 'Yes' : 'No');
}
function testItReturnsCurrentThemeNameAndVersion() {
$current_theme = wp_get_theme();
expect($this->beacon_data['Current Theme'])->contains($current_theme->get('Name'));
expect($this->beacon_data['Current Theme'])->contains($current_theme->get('Version'));
}
function testItReturnsActivePlugins() {
expect($this->beacon_data['Active Plugin names'])->equals(join(", ", get_option('active_plugins')));
}
function testItReturnsSendingMethodDetails() {
$mta = Setting::getValue('mta');
expect($this->beacon_data['Sending Method'])->equals($mta['method']);
expect($this->beacon_data['Sending Frequency'])->contains($mta['frequency']['emails'].' emails');
expect($this->beacon_data['Sending Frequency'])->contains($mta['frequency']['interval'].' minutes');
}
function testItReturnsSomeSettings() {
expect($this->beacon_data['Task Scheduler method'])->equals(Setting::getValue('cron_trigger.method'));
expect($this->beacon_data['Default FROM address'])->equals(Setting::getValue('sender.address'));
expect($this->beacon_data['Default Reply-To address'])->equals(Setting::getValue('reply_to.address'));
expect($this->beacon_data['Bounce Email Address'])->equals(Setting::getValue('bounce.address'));
}
function testItReturnsTotalNumberOfSubscribers() {
// unsubscribed users are not taken into account
expect($this->beacon_data['Total number of subscribers'])->equals(2);
}
}

View File

@@ -57,10 +57,18 @@ jQuery('.toplevel_page_mailpoet-newsletters.menu-top-last')
<script type="text/javascript">
if(window['HS'] !== undefined) {
// HelpScout Beacon: Configuration
HS.beacon.config({
icon: 'message',
zIndex: 50000,
instructions: '<%= __('Want to give feedback to the MailPoet team? Contact us here. Please provide as much information as possible!') %>',
instructions: "<%= __('Want to give feedback to the MailPoet team? Contact us here. Please provide as much information as possible!') %>"
});
// HelpScout Beacon: Custom information
HS.beacon.ready(function() {
HS.beacon.identify(
<%= json_encode(get_helpscout_data()) %>
);
});
}
</script>