first commit

This commit is contained in:
2024-12-23 22:29:39 +01:00
commit 01e021cbac
6943 changed files with 3009416 additions and 0 deletions

View File

@@ -0,0 +1,280 @@
.analyst-action-opt {
cursor: pointer;
}
.analyst-modal {
color: #000000;
display: none;
position: fixed;
z-index: 1000;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.analyst-modal-content {
font-family: Helvetica, serif;
position: relative;
background-color: #fefefe;
margin: auto;
padding: 35px 35px 20px;
border: 1px solid #F2F2F2;
width: 40%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: analyst-animatetop;
-webkit-animation-duration: 0.4s;
animation-name: analyst-animatetop;
animation-duration: 0.4s
}
.analyst-btn-success {
cursor: pointer;
color: #ffffff;
background-color: #00AF5E;
border: none;
width: 100%;
font-size: 18px;
padding: 8px;
font-weight: bold;
}
.analyst-btn-grey {
cursor: pointer;
color: #2D2D2D;
background-color: #D8D8D8;
border: none;
width: 100%;
font-size: 18px;
padding: 8px;
font-weight: bold;
}
.analyst-btn-secondary-ghost {
cursor: pointer;
background: transparent;
border: none;
color: #898686;
font-size: 18px;
}
.analyst-modal-def-top-padding {
padding-top: 20px;
}
.analyst-modal-header {
font-size: 20px;
font-weight: bold;
}
/*INSTALL STYLES*/
.analyst-install-footer {
padding-top: 10px;
text-align: center;
}
.analyst-install-image-block {
width: 140px;
}
.analyst-install-image-block img {
width: inherit;
}
.analyst-install-description-block {
padding-left: 40px;
padding-top: 5px
}
.analyst-install-description-text {
font-size: 16px;
color: #000000;
}
.analyst-install-permissions-list {
list-style: disc inside;
}
.analyst-install-permissions-list li {
padding-left: 15px;
margin-bottom: 2px;
}
.analyst-install-footer span {
color: #8a8787;
padding-right: 10px;
padding-left: 10px;
}
.analyst-install-footer span:not(:last-child) {
border-right: 1px solid #8a8787;
}
/*INSTALL STYLES*/
.reason-answer {
padding: 7px;
margin-left: 23px;
border: 1px solid #F2F2F2;
}
.analyst-link {
color: #00AF5E;
text-decoration: none;
}
.analyst-action-text {
cursor: pointer;
}
.analyst-action-text:hover {
color: #9d9a9a;
}
.analyst-disable-modal-mask {
width: 100%;
height: 100%;
opacity: 0.5;
position: absolute;
background: white;
top: 0;
left: 0;
}
.analyst-smile-image {
vertical-align: middle;
padding-bottom: 3px;
width: 24px;
}
#analyst-deactivation-reasons li {
padding-bottom: 3px;
font-size: 16px;
color: #000000;
}
@-webkit-keyframes analyst-animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes analyst-animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
.analyst-modal-close {
color: #48036F;
font-size: 28px;
font-weight: bold;
top: 12px;
position: absolute;
right: 15px;
}
.analyst-modal-close:hover,
.analyst-modal-close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.analyst-modal-body {padding: 2px 16px;}
.analyst-modal-footer {
padding: 6px 16px;
background-color: #FFE773;
color: white;
}
#analyst-deactivate-modal .question-answer input, textarea {
margin-top: 5px;
width: 100%;
}
.analyst-btn-primary {
cursor: pointer;
border: none;
display:inline-block;
padding:0.7em 1.4em;
margin:0 0.3em 0.3em 0;
border-radius:0.15em;
box-sizing: border-box;
text-decoration:none;
font-family:'Roboto',sans-serif;
text-transform:uppercase;
font-weight:400;
color:#FFFFFF;
background-color:#9F3ED5;
box-shadow:inset 0 -0.6em 0 -0.35em rgba(0,0,0,0.17);
text-align:center;
position:relative;
}
.analyst-btn-primary:disabled {
background-color: #AD66D5;
cursor: not-allowed;
}
.analyst-btn-primary:active{
top:0.1em;
}
@media all and (max-width:30em){
.analyst-btn-primary {
display:block;
margin:0.4em auto;
}
}
.analyst-btn-secondary {
cursor: pointer;
border: none;
display:inline-block;
padding:0.7em 1.4em;
margin:0 0.3em 0.3em 0;
border-radius:0.15em;
box-sizing: border-box;
text-decoration:none;
font-family:'Roboto',sans-serif;
text-transform:uppercase;
font-weight:400;
color:#FFFFFF;
background-color:#6C8CD5;
box-shadow:inset 0 -0.6em 0 -0.35em rgba(0,0,0,0.17);
text-align:center;
position:relative;
}
.analyst-btn-secondary:disabled {
background-color: #6C8CD5;
cursor: not-allowed;
}
.analyst-btn-secondary:active{
top:0.1em;
}
@media all and (max-width:30em){
.analyst-btn-secondary {
display:block;
margin:0.4em auto;
}
}
.analyst-notice {
padding-right: 38px;
position: relative;
margin-bottom: 30px !important;
}
.analyst-notice .analyst-plugin-name {
background-color: #00000024;
padding-left: 7px;
padding-right: 7px;
position: absolute;
top: 100%;
border-radius: 0 0 5px 5px;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@@ -0,0 +1,2 @@
<?php
// Silence is golden.

View File

@@ -0,0 +1,33 @@
(function ($) {
$(document).on('click', '.analyst-notice-dismiss', function () {
var id = $(this).attr('analyst-notice-id');
var self = this;
$.post(ajaxurl, {
action: 'analyst_notification_dismiss',
id: id,
nonce: analyst_opt_localize.nonce
}).done(function () {
$(self).parent().fadeOut()
})
})
var url = new URL(window.location.href)
if (url.searchParams.has('verify')) {
var pluginId = url.searchParams.get('plugin_id')
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'analyst_install_verified_' + pluginId,
nonce: analyst_opt_localize.nonce
},
success: function () {
// Refresh page without query params
window.location.href = window.location.origin + window.location.pathname
}
})
}
})(jQuery)

View File

@@ -0,0 +1,40 @@
<?php
require_once __DIR__ . '/src/helpers.php';
require_once __DIR__ . '/src/Contracts/HttpClientContract.php';
require_once __DIR__ . '/src/Contracts/RequestContract.php';
require_once __DIR__ . '/src/Contracts/RequestorContract.php';
require_once __DIR__ . '/src/Contracts/TrackerContract.php';
require_once __DIR__ . '/src/Contracts/CacheContract.php';
require_once __DIR__ . '/src/Core/AbstractFactory.php';
require_once __DIR__ . '/src/Cache/DatabaseCache.php';
require_once __DIR__ . '/src/Account/Account.php';
require_once __DIR__ . '/src/Account/AccountData.php';
require_once __DIR__ . '/src/Account/AccountDataFactory.php';
require_once __DIR__ . '/src/Contracts/AnalystContract.php';
require_once __DIR__ . '/src/Http/Requests/AbstractLoggerRequest.php';
require_once __DIR__ . '/src/Http/Requests/ActivateRequest.php';
require_once __DIR__ . '/src/Http/Requests/DeactivateRequest.php';
require_once __DIR__ . '/src/Http/Requests/InstallRequest.php';
require_once __DIR__ . '/src/Http/Requests/OptInRequest.php';
require_once __DIR__ . '/src/Http/Requests/OptOutRequest.php';
require_once __DIR__ . '/src/Http/Requests/UninstallRequest.php';
require_once __DIR__ . '/src/Http/CurlHttpClient.php';
require_once __DIR__ . '/src/Http/DummyHttpClient.php';
require_once __DIR__ . '/src/Http/WordPressHttpClient.php';
require_once __DIR__ . '/src/Notices/Notice.php';
require_once __DIR__ . '/src/Notices/NoticeFactory.php';
require_once __DIR__ . '/src/Analyst.php';
require_once __DIR__ . '/src/ApiRequestor.php';
require_once __DIR__ . '/src/ApiResponse.php';
require_once __DIR__ . '/src/Collector.php';
require_once __DIR__ . '/src/Mutator.php';

View File

@@ -0,0 +1,2 @@
<?php
// Silence

View File

@@ -0,0 +1,70 @@
<?php
require_once 'sdk_resolver.php';
/**
* Initialize analyst "private"
*
* @param array $options
*/
if (!function_exists('___analyst_init')) {
function ___analyst_init($options) {
$capabilities = [
'activate_plugins',
'edit_plugins',
'install_plugins',
'update_plugins',
'delete_plugins',
'manage_network_plugins',
'upload_plugins'
];
// Allow if has any of above permissions
$hasPerms = false;
foreach ($capabilities as $i => $cap) {
if (current_user_can($cap)) {
$hasPerms = true;
break;
}
}
if ($hasPerms == false) {
return;
}
// Try resolve latest supported SDK
// In case resolving is failed exit the execution
try {
analyst_resolve_sdk($options['base-dir']);
} catch (Exception $exception) {
// error_log('[ANALYST] Cannot resolve any supported SDK');
return;
}
try {
global /** @var Analyst\Analyst $analyst */
$analyst;
// Set global instance of analyst
if (!$analyst) {
$analyst = Analyst\Analyst::getInstance();
}
$analyst->registerAccount(new Account\Account($options['client-id'], $options['client-secret'], $options['base-dir']));
} catch (Exception $e) {
// error_log('Analyst SDK receive an error: [' . $e->getMessage() . '] Please contact our support at support@analyst.com');
}
}
}
if (!function_exists('analyst_init')) {
function analyst_init($__options) {
if (did_action('init') > 0 && function_exists('current_user_can')) ___analyst_init($__options);
else {
add_action('init', function () use ($__options) {
___analyst_init($__options);
}, -1000);
}
}
}

View File

@@ -0,0 +1,79 @@
<?php
if (!function_exists('analyst_resolve_sdk')) {
/**
* Resolve supported sdk versions and load latest supported one
* also bootstrap sdk with autoloader
*
* @since 1.1.3
*
* @param null $thisPluginPath
* @return void
* @throws Exception
*/
function analyst_resolve_sdk($thisPluginPath = null) {
static $loaded = false;
// Exit if we already resolved SDK
if ($loaded) return;
$plugins = get_option('active_plugins');
if ($thisPluginPath) {
array_push($plugins, plugin_basename($thisPluginPath));
}
$pluginsFolder = WP_PLUGIN_DIR;
$possibleSDKs = array_map(function ($path) use ($pluginsFolder) {
$sdkFolder = sprintf('%s/%s/analyst/', $pluginsFolder, dirname($path));
$sdkFolder = str_replace('\\', '/', $sdkFolder);
$versionPath = $sdkFolder . 'version.php';
if (file_exists($versionPath)) {
return require $versionPath;
}
return false;
}, $plugins);
global $wp_version;
// Filter out plugins which has no SDK
$SDKs = array_filter($possibleSDKs, function ($s) {return is_array($s);});
// Filter SDKs which is supported by PHP and WP
$supported = array_values(array_filter($SDKs, function ($sdk) use($wp_version) {
$phpSupported = version_compare(PHP_VERSION, $sdk['php']) >= 0;
$wpSupported = version_compare($wp_version, $sdk['wp']) >= 0;
return $phpSupported && $wpSupported;
}));
// Sort SDK by version in descending order
uasort($supported, function ($x, $y) {
return version_compare($y['sdk'], $x['sdk']);
});
// Reset sorted values keys
$supported = array_values($supported);
if (!isset($supported[0])) {
throw new Exception('There is no SDK which is support current PHP version and WP version');
}
// Autoload files for supported SDK
$autoloaderPath = str_replace(
'\\',
'/',
sprintf('%s/autoload.php', $supported[0]['path'])
);
require_once $autoloaderPath;
$loaded = true;
}
}

View File

@@ -0,0 +1,629 @@
<?php
namespace Account;
use Analyst\Analyst;
use Analyst\ApiRequestor;
use Analyst\Cache\DatabaseCache;
use Analyst\Collector;
use Analyst\Http\Requests\ActivateRequest;
use Analyst\Http\Requests\DeactivateRequest;
use Analyst\Http\Requests\InstallRequest;
use Analyst\Http\Requests\OptInRequest;
use Analyst\Http\Requests\OptOutRequest;
use Analyst\Http\Requests\UninstallRequest;
use Analyst\Notices\Notice;
use Analyst\Notices\NoticeFactory;
use Analyst\Contracts\TrackerContract;
use Analyst\Contracts\RequestorContract;
/**
* Class Account
*
* This is plugin's account object
*/
class Account implements TrackerContract
{
/**
* Account id
*
* @var string
*/
protected $id;
/**
* Basename of plugin
*
* @var string
*/
protected $path;
/**
* Whether plugin is active or not
*
* @var bool
*/
protected $isInstalled = false;
/**
* Is user sign in for data tracking
*
* @var bool
*/
protected $isOptedIn = false;
/**
* Is user accepted permissions grant
* for collection site data
*
* @var bool
*/
protected $isSigned = false;
/**
* Is user ever resolved install modal window?
*
* @var bool
*/
protected $isInstallResolved = false;
/**
* Public secret code
*
* @var string
*/
protected $clientSecret;
/**
* @var AccountData
*/
protected $data;
/**
* Base plugin path
*
* @var string
*/
protected $basePluginPath;
/**
* @var RequestorContract
*/
protected $requestor;
/**
* @var Collector
*/
protected $collector;
/**
* Account constructor.
* @param $id
* @param $secret
* @param $baseDir
*/
public function __construct($id, $secret, $baseDir)
{
$this->id = $id;
$this->clientSecret = $secret;
$this->path = $baseDir;
$this->basePluginPath = plugin_basename($baseDir);
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param string $path
*/
public function setPath($path)
{
$this->data->setPath($path);
$this->path = $path;
}
/**
* @return bool
*/
public function isOptedIn()
{
return $this->isOptedIn;
}
/**
* @param bool $isOptedIn
*/
public function setIsOptedIn($isOptedIn)
{
$this->data->setIsOptedIn($isOptedIn);
$this->isOptedIn = $isOptedIn;
}
/**
* Whether plugin is active
*
* @return bool
*/
public function isActive()
{
return is_plugin_active($this->path);
}
/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return bool
*/
public function isInstalled()
{
return $this->isInstalled;
}
/**
* @param bool $isInstalled
*/
public function setIsInstalled($isInstalled)
{
$this->data->setIsInstalled($isInstalled);
$this->isInstalled = $isInstalled;
}
protected function verifyNonceAndPerms() {
$capabilities = [
'activate_plugins',
'edit_plugins',
'install_plugins',
'update_plugins',
'delete_plugins',
'manage_network_plugins',
'upload_plugins'
];
// Allow if has any of above permissions
$hasPerms = false;
foreach ($capabilities as $i => $cap) {
if (current_user_can($cap)) {
$hasPerms = true;
break;
}
}
if ($hasPerms == false) {
wp_send_json_error(['message' => 'no_permissions']);
die;
}
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['nonce']), 'analyst_opt_ajax_nonce')) {
wp_send_json_error(['message' => 'invalid_nonce']);
die;
}
}
/**
* Should register activation and deactivation
* event hooks
*
* @return void
*/
public function registerHooks()
{
register_activation_hook($this->basePluginPath, [&$this, 'onActivePluginListener']);
register_uninstall_hook($this->basePluginPath, ['Account\Account', 'onUninstallPluginListener']);
$this->addFilter('plugin_action_links', [&$this, 'onRenderActionLinksHook']);
$this->addAjax('analyst_opt_in', [&$this, 'onOptInListener']);
$this->addAjax('analyst_opt_out', [&$this, 'onOptOutListener']);
$this->addAjax('analyst_plugin_deactivate', [&$this, 'onDeactivatePluginListener']);
$this->addAjax('analyst_install', [&$this, 'onInstallListener']);
$this->addAjax('analyst_skip_install', [&$this, 'onSkipInstallListener']);
$this->addAjax('analyst_install_verified', [&$this, 'onInstallVerifiedListener']);
}
/**
* Will fire when admin activates plugin
*
* @return void
*/
public function onActivePluginListener()
{
if (!$this->isInstallResolved()) {
DatabaseCache::getInstance()->put('plugin_to_install', $this->id);
}
if (!$this->isAllowingLogging()) return;
ActivateRequest::make($this->collector, $this->id, $this->path)
->execute($this->requestor);
$this->setIsInstalled(true);
AccountDataFactory::syncData();
}
/**
* Will fire when admin deactivates plugin
*
* @return void
*/
public function onDeactivatePluginListener()
{
$this->verifyNonceAndPerms();
if (!$this->isAllowingLogging()) return;
$question = isset($_POST['question']) ? sanitize_text_field(stripslashes($_POST['question'])) : null;
$reason = isset($_POST['reason']) ? sanitize_text_field(stripslashes($_POST['reason'])) : null;
DeactivateRequest::make($this->collector, $this->id, $this->path, $question, $reason)
->execute($this->requestor);
$this->setIsInstalled(false);
AccountDataFactory::syncData();
wp_send_json_success();
}
/**
* Will fire when user opted in
*
* @return void
*/
public function onOptInListener()
{
$this->verifyNonceAndPerms();
OptInRequest::make($this->collector, $this->id, $this->path)->execute($this->requestor);
$this->setIsOptedIn(true);
AccountDataFactory::syncData();
wp_die();
}
/**
* Will fire when user opted out
*
* @return void
*/
public function onOptOutListener()
{
$this->verifyNonceAndPerms();
OptOutRequest::make($this->collector, $this->id, $this->path)->execute($this->requestor);
$this->setIsOptedIn(false);
AccountDataFactory::syncData();
wp_send_json_success();
}
/**
* Will fire when user accept opt-in
* at first time
*
* @return void
*/
public function onInstallListener()
{
$this->verifyNonceAndPerms();
$cache = DatabaseCache::getInstance();
// Set flag to true which indicates that install is resolved
// also remove install plugin id from cache
$this->setIsInstallResolved(true);
$cache->delete('plugin_to_install');
InstallRequest::make($this->collector, $this->id, $this->path)->execute($this->requestor);
$this->setIsSigned(true);
$this->setIsOptedIn(true);
$factory = NoticeFactory::instance();
$message = sprintf('Please confirm your email by clicking on the link we sent to %s. This makes sure youre not a bot.', $this->collector->getGeneralEmailAddress());
$notificationId = uniqid();
$notice = Notice::make(
$notificationId,
$this->getId(),
$message,
$this->collector->getPluginName($this->path)
);
$factory->addNotice($notice);
AccountDataFactory::syncData();
// Set email confirmation notification id to cache
// se we can extract and remove it when user confirmed email
$cache->put(
sprintf('account_email_confirmation_%s', $this->getId()),
$notificationId
);
wp_send_json_success();
}
/**
* Will fire when user skipped installation
*
* @return void
*/
public function onSkipInstallListener()
{
$this->verifyNonceAndPerms();
// Set flag to true which indicates that install is resolved
// also remove install plugin id from cache
$this->setIsInstallResolved(true);
DatabaseCache::getInstance()->delete('plugin_to_install');
}
/**
* Will fire when user delete plugin through admin panel.
* This action will happen if admin at least once
* activated the plugin.
*
* @return void
* @throws \Exception
*/
public static function onUninstallPluginListener()
{
$factory = AccountDataFactory::instance();
$pluginFile = substr(current_filter(), strlen( 'uninstall_' ));
$account = $factory->getAccountDataByBasePath($pluginFile);
// If account somehow is not found, exit the execution
if (!$account) return;
$analyst = Analyst::getInstance();
$collector = new Collector($analyst);
$requestor = new ApiRequestor($account->getId(), $account->getSecret(), $analyst->getApiBase());
// Just send request to log uninstall event not caring about response
UninstallRequest::make($collector, $account->getId(), $account->getPath())->execute($requestor);
$factory->sync();
}
/**
* Fires when used verified his account
*/
public function onInstallVerifiedListener()
{
$this->verifyNonceAndPerms();
$factory = NoticeFactory::instance();
$notice = Notice::make(
uniqid(),
$this->getId(),
'Thank you for confirming your email.',
$this->collector->getPluginName($this->path)
);
$factory->addNotice($notice);
// Remove confirmation notification
$confirmationNotificationId = DatabaseCache::getInstance()->pop(sprintf('account_email_confirmation_%s', $this->getId()));
$factory->remove($confirmationNotificationId);
AccountDataFactory::syncData();
wp_send_json_success();
}
/**
* Will fire when wp renders plugin
* action buttons
*
* @param $defaultLinks
* @return array
*/
public function onRenderActionLinksHook($defaultLinks)
{
$customLinks = [];
$customLinks[] = $this->isOptedIn()
? '<a class="analyst-action-opt analyst-opt-out" analyst-plugin-id="' . $this->getId() . '" analyst-plugin-signed="' . (int) $this->isSigned() . '">Opt Out</a>'
: '<a class="analyst-action-opt analyst-opt-in" analyst-plugin-id="' . $this->getId() . '" analyst-plugin-signed="' . (int) $this->isSigned() . '">Opt In</a>';
// Append anchor to find specific deactivation link
if (isset($defaultLinks['deactivate'])) {
$defaultLinks['deactivate'] .= '<span analyst-plugin-id="' . $this->getId() . '" analyst-plugin-opted-in="' . (int) $this->isOptedIn() . '"></span>';
}
return array_merge($customLinks, $defaultLinks);
}
/**
* @return AccountData
*/
public function getData()
{
return $this->data;
}
/**
* @param AccountData $data
*/
public function setData(AccountData $data)
{
$this->data = $data;
$this->setIsOptedIn($data->isOptedIn());
$this->setIsInstalled($data->isInstalled());
$this->setIsSigned($data->isSigned());
$this->setIsInstallResolved($data->isInstallResolved());
}
/**
* Resolves valid action name
* based on client id
*
* @param $action
* @return string
*/
private function resolveActionName($action)
{
return sprintf('%s_%s', $action, $this->id);
}
/**
* Register action for current plugin
*
* @param $action
* @param $callback
*/
private function addFilter($action, $callback)
{
$validAction = sprintf('%s_%s', $action, $this->basePluginPath);
add_filter($validAction, $callback, 10);
}
/**
* Add ajax action for current plugin
*
* @param $action
* @param $callback
* @param bool $raw Format action ??
*/
private function addAjax($action, $callback, $raw = false)
{
$validAction = $raw ? $action : sprintf('%s%s', 'wp_ajax_', $this->resolveActionName($action));
add_action($validAction, $callback);
}
/**
* @return bool
*/
public function isSigned()
{
return $this->isSigned;
}
/**
* @param bool $isSigned
*/
public function setIsSigned($isSigned)
{
$this->data->setIsSigned($isSigned);
$this->isSigned = $isSigned;
}
/**
* @return RequestorContract
*/
public function getRequestor()
{
return $this->requestor;
}
/**
* @param RequestorContract $requestor
*/
public function setRequestor(RequestorContract $requestor)
{
$this->requestor = $requestor;
}
/**
* @return string
*/
public function getClientSecret()
{
return $this->clientSecret;
}
/**
* @return Collector
*/
public function getCollector()
{
return $this->collector;
}
/**
* @param Collector $collector
*/
public function setCollector(Collector $collector)
{
$this->collector = $collector;
}
/**
* Do we allowing logging
*
* @return bool
*/
public function isAllowingLogging()
{
return $this->isOptedIn;
}
/**
* @return string
*/
public function getBasePluginPath()
{
return $this->basePluginPath;
}
/**
* @return bool
*/
public function isInstallResolved()
{
return $this->isInstallResolved;
}
/**
* @param bool $isInstallResolved
*/
public function setIsInstallResolved($isInstallResolved)
{
$this->data->setIsInstallResolved($isInstallResolved);
$this->isInstallResolved = $isInstallResolved;
}
}

View File

@@ -0,0 +1,176 @@
<?php
namespace Account;
/**
* Class AccountData is the data holder
* for Analyst\Account\Account class
* which is unserialized from database
*/
class AccountData
{
/**
* Account id
*
* @var string
*/
protected $id;
/**
* Account secret key
*
* @var string
*/
protected $secret;
/**
* Basename of plugin
*
* @var string
*/
protected $path;
/**
* Whether admin accepted opt in
* terms and permissions
*
* @var bool
*/
protected $isInstalled = false;
/**
* Is user sign in for data tracking
*
* @var bool
*/
protected $isOptedIn = false;
/**
* Is user accepted permissions grant
* for collection site data
*
* @var bool
*/
protected $isSigned = false;
/**
* Is user ever resolved install modal window?
*
* @var bool
*/
protected $isInstallResolved;
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @param string $path
* @return AccountData
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* @return bool
*/
public function isInstalled()
{
return $this->isInstalled;
}
/**
* @param bool $isInstalled
*/
public function setIsInstalled($isInstalled)
{
$this->isInstalled = $isInstalled;
}
/**
* @return bool
*/
public function isOptedIn()
{
return $this->isOptedIn;
}
/**
* @param bool $isOptedIn
*/
public function setIsOptedIn($isOptedIn)
{
$this->isOptedIn = $isOptedIn;
}
/**
* @return bool
*/
public function isSigned()
{
return $this->isSigned;
}
/**
* @param bool $isSigned
*/
public function setIsSigned($isSigned)
{
$this->isSigned = $isSigned;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return string
*/
public function getSecret()
{
return $this->secret;
}
/**
* @param string $secret
*/
public function setSecret($secret)
{
$this->secret = $secret;
}
/**
* @return bool
*/
public function isInstallResolved()
{
return $this->isInstallResolved;
}
/**
* @param bool $isInstallResolved
*/
public function setIsInstallResolved($isInstallResolved)
{
$this->isInstallResolved = $isInstallResolved;
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace Account;
use Analyst\Core\AbstractFactory;
/**
* Class AccountDataFactory
*
* Holds information about this
* wordpress project plugins accounts
*
*/
class AccountDataFactory extends AbstractFactory
{
private static $instance;
CONST OPTIONS_KEY = 'analyst_accounts_data';
/**
* @var AccountData[]
*/
protected $accounts = [];
/**
* Read factory from options or make fresh instance
*
* @return static
*/
public static function instance()
{
if (!static::$instance) {
$raw = get_option(self::OPTIONS_KEY);
// In case object is already unserialized
// and instance of AccountDataFactory we
// return it, in other case deal with
// serialized string data
if ($raw instanceof self) {
static::$instance = $raw;
} else {
static::$instance = is_string($raw) ? static::unserialize($raw) : new self();
}
}
return static::$instance;
}
/**
* Sync this object data with cache
*/
public function sync()
{
update_option(self::OPTIONS_KEY, serialize($this));
}
/**
* Sync this instance data with cache
*/
public static function syncData()
{
static::instance()->sync();
}
/**
* Find plugin account data or create fresh one
*
* @param Account $account
* @return AccountData|null
*/
public function resolvePluginAccountData(Account $account)
{
$accountData = $this->findAccountDataById($account->getId());
if (!$accountData) {
$accountData = new AccountData();
// Set proper default values
$accountData->setPath($account->getPath());
$accountData->setId($account->getId());
$accountData->setSecret($account->getClientSecret());
array_push($this->accounts, $accountData);
}
return $accountData;
}
/**
* Should return account data by base path
*
* @param $basePath
* @return AccountData
*/
public function getAccountDataByBasePath($basePath)
{
foreach ($this->accounts as $iterable) {
$iterableBasePath = plugin_basename($iterable->getPath());
if ($iterableBasePath === $basePath) {
return $iterable;
}
}
return null;
}
/**
* Return account by id
*
* @param $id
* @return AccountData|null
*/
private function findAccountDataById($id)
{
foreach ($this->accounts as &$iterable) {
if ($iterable->getId() === $id) {
return $iterable;
}
}
return null;
}
}

View File

@@ -0,0 +1,167 @@
<?php
namespace Analyst;
use Account\Account;
use Account\AccountDataFactory;
use Analyst\Contracts\AnalystContract;
use Analyst\Contracts\RequestorContract;
class Analyst implements AnalystContract
{
/**
* All plugin's accounts
*
* @var array
*/
protected $accounts = array();
/**
* @var Mutator
*/
protected $mutator;
/**
* @var AccountDataFactory
*/
protected $accountDataFactory;
/**
* Base url to api
*
* @var string
*/
protected $apiBase = 'https://feedback.sellcodes.com/api/v1';
/**
* @var Collector
*/
protected $collector;
/**
* Singleton instance
*
* @var static
*/
protected static $instance;
/**
* Get instance of analyst
*
* @return Analyst
* @throws \Exception
*/
public static function getInstance()
{
if (!static::$instance) {
static::$instance = new Analyst();
}
return static::$instance;
}
protected function __construct()
{
$this->mutator = new Mutator();
$this->accountDataFactory = AccountDataFactory::instance();
$this->mutator->initialize();
$this->collector = new Collector($this);
$this->initialize();
}
/**
* Initialize rest of application
*/
public function initialize()
{
add_action('init', function () {
$this->collector->loadCurrentUser();
});
}
/**
* Register new account
*
* @param Account $account
* @return Analyst
* @throws \Exception
*/
public function registerAccount($account)
{
// Stop propagation when account is already registered
if ($this->isAccountRegistered($account)) {
return $this;
}
// Resolve account data from factory
$accountData = $this->accountDataFactory->resolvePluginAccountData($account);
$account->setData($accountData);
$account->setRequestor(
$this->resolveRequestorForAccount($account)
);
$account->setCollector($this->collector);
$account->registerHooks();
$this->accounts[$account->getId()] = $account;
return $this;
}
/**
* Must return version of analyst
*
* @return string
*/
public static function version()
{
$version = require __DIR__ . '/../version.php';
return $version['sdk'];
}
/**
* Is this account registered
*
* @param Account $account
* @return bool
*/
protected function isAccountRegistered($account)
{
return isset($this->accounts[$account->getId()]);
}
/**
* Resolves requestor for account
*
* @param Account $account
* @return RequestorContract
* @throws \Exception
*/
protected function resolveRequestorForAccount(Account $account)
{
$requestor = new ApiRequestor($account->getId(), $account->getClientSecret(), $this->apiBase);
// Set SDK version
$requestor->setDefaultHeader(
'x-analyst-client-user-agent',
sprintf('Analyst/%s', $this->version())
);
return $requestor;
}
/**
* @return string
*/
public function getApiBase()
{
return $this->apiBase;
}
}

View File

@@ -0,0 +1,257 @@
<?php
namespace Analyst;
use Exception;
use Analyst\Contracts\HttpClientContract;
use Analyst\Contracts\RequestorContract;
class ApiRequestor implements RequestorContract
{
/**
* Supported http client
*
* @var HttpClientContract
*/
protected $httpClient;
/**
* @var string
*/
protected $clientId;
/**
* @var string
*/
protected $clientSecret;
/**
* @var string
*/
protected $apiBase;
/**
* Default headers to be sent
*
* @var array
*/
protected $defaultHeaders = [
'accept' => 'application/json',
'content-type' => 'application/json'
];
/**
* Prioritized http clients
*
* @var array
*/
protected $availableClients = [
'Analyst\Http\WordPressHttpClient',
'Analyst\Http\CurlHttpClient',
'Analyst\Http\DummyHttpClient',
];
/**
* ApiRequestor constructor.
* @param $id
* @param $secret
* @param $apiBase
* @throws \Exception
*/
public function __construct($id, $secret, $apiBase)
{
$this->clientId = $id;
$this->clientSecret = $secret;
$this->setApiBase($apiBase);
$this->httpClient = $this->resolveHttpClient();
}
/**
* Set api base url
*
* @param $url
*/
public function setApiBase($url)
{
$this->apiBase = $url;
}
/**
* Get request
*
* @param $url
* @param array $headers
* @return mixed
*/
public function get($url, $headers = [])
{
return $this->request('GET', $url, null, $headers);
}
/**
* Post request
*
* @param $url
* @param $body
* @param array $headers
* @return mixed
*/
public function post($url, $body = [], $headers = [])
{
return $this->request('POST', $url, $body, $headers);
}
/**
* Put request
*
* @param $url
* @param $body
* @param array $headers
* @return mixed
*/
public function put($url, $body = [], $headers = [])
{
return $this->request('PUT', $url, $body, $headers);
}
/**
* Delete request
*
* @param $url
* @param array $headers
* @return mixed
*/
public function delete($url, $headers = [])
{
return $this->request('DELETE', $url, null, $headers);
}
/**
* Make request to api
*
* @param $method
* @param $url
* @param array $body
* @param array $headers
* @return mixed
*/
protected function request($method, $url, $body = [], $headers = [])
{
$fullUrl = $this->resolveFullUrl($url);
$date = date('r', time());
$headers['date'] = $date;
$headers['signature'] = $this->resolveSignature($this->clientSecret, $method, $fullUrl, $body, $date);
// Lowercase header names
$headers = $this->prepareHeaders(
array_merge($headers, $this->defaultHeaders)
);
$response = $this->httpClient->request($method, $fullUrl, $body, $headers);
// TODO: Check response code and take actions
return $response;
}
/**
* Set one default header
*
* @param $header
* @param $value
*/
public function setDefaultHeader($header, $value)
{
$this->defaultHeaders[
$this->resolveValidHeaderName($header)
] = $value;
}
/**
* Resolves supported http client
*
* @return HttpClientContract
* @throws Exception
*/
protected function resolveHttpClient()
{
$clients = array_filter($this->availableClients, $this->guessClientSupportEnvironment());
if (!isset($clients[0])) {
throw new Exception('There is no http client which this application can support');
}
// Instantiate first supported http client
return new $clients[0];
}
/**
* This will filter out clients which is not supported
* by the current environment
*
* @return \Closure
*/
protected function guessClientSupportEnvironment()
{
return function ($client) {
return forward_static_call([$client, 'hasSupport']);
};
}
/**
* Resolves valid header name
*
* @param $headerName
* @return string
*/
private function resolveValidHeaderName($headerName)
{
return strtolower($headerName);
}
/**
* Lowercase header names
*
* @param $headers
* @return array
*/
private function prepareHeaders($headers)
{
return array_change_key_case($headers, CASE_LOWER);
}
/**
* Sign request
*
* @param $key
* @param $method
* @param $url
* @param $body
* @param $date
*
* @return false|string
*/
private function resolveSignature($key, $method, $url, $body, $date)
{
$string = implode('\n', [$method, $url, md5(json_encode($body)), $date]);
$contentSecret = hash_hmac('sha256', $string, $key);
return sprintf('%s:%s', $this->clientId, $contentSecret);
}
/**
* Compose full url
*
* @param $url
* @return string
*/
private function resolveFullUrl($url)
{
return sprintf('%s/%s', $this->apiBase, trim($url, '/'));
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Analyst;
class ApiResponse
{
/**
* Response headers
*
* @var array
*/
public $headers;
/**
* Response body
*
* @var mixed
*/
public $body;
/**
* Status code
*
* @var string
*/
public $code;
public function __construct($body, $code, $headers)
{
$this->body = $body;
$this->code = $code;
$this->headers = $headers;
}
/**
* Whether status code is successful
*
* @return bool
*/
public function isSuccess()
{
return $this->code >= 200 && $this->code < 300;
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Analyst\Cache;
use Analyst\Contracts\CacheContract;
/**
* Class DatabaseCache
*
* @since 1.1.5
*/
class DatabaseCache implements CacheContract
{
const OPTION_KEY = 'analyst_cache';
protected static $instance;
/**
* Get instance of db cache
*
* @return DatabaseCache
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new DatabaseCache();
}
return self::$instance;
}
/**
* Key value pair
*
* @var array[]
*/
protected $values = [];
/**
* DatabaseCache constructor.
*/
public function __construct()
{
$raw = get_option(self::OPTION_KEY, serialize([]));
// Raw data may be an array already
$this->values = is_array($raw) ? $raw : @unserialize($raw);
// In case serialization is failed
// make sure values is an array
if (!is_array($this->values)) {
$this->values = [];
}
}
/**
* Save value with given key
*
* @param string $key
* @param string $value
*
* @return static
*/
public function put($key, $value)
{
$this->values[$key] = $value;
$this->sync();
return $this;
}
/**
* Get value by given key
*
* @param $key
*
* @param null $default
* @return string
*/
public function get($key, $default = null)
{
$value = isset($this->values[$key]) ? $this->values[$key] : $default;
return $value;
}
/**
* @param $key
*
* @return static
*/
public function delete($key)
{
if (isset($this->values[$key])) {
unset($this->values[$key]);
$this->sync();
}
return $this;
}
/**
* Update cache in DB
*/
protected function sync()
{
update_option(self::OPTION_KEY, serialize($this->values));
}
/**
* Should get value and remove it from cache
*
* @param $key
* @param null $default
* @return mixed
*/
public function pop($key, $default = null)
{
$value = $this->get($key);
$this->delete($key);
return $value;
}
}

View File

@@ -0,0 +1,221 @@
<?php
namespace Analyst;
use Analyst\Contracts\AnalystContract;
/**
* Class Collector is a set of getters
* to retrieve some data from wp site
*/
class Collector
{
/**
* @var AnalystContract
*/
protected $sdk;
/**
* @var \WP_User
*/
protected $user;
public function __construct(AnalystContract $sdk)
{
$this->sdk = $sdk;
}
/**
* Load current user into memory
*/
public function loadCurrentUser()
{
$this->user = wp_get_current_user();
}
/**
* Get site url
*
* @return string
*/
public function getSiteUrl()
{
return get_option('siteurl');
}
/**
* Get current user email
*
* @return string
*/
public function getCurrentUserEmail()
{
return $this->user->user_email;
}
/**
* Get's email from general settings
*
* @return string
*/
public function getGeneralEmailAddress()
{
return get_option('admin_email');
}
/**
* Is this user administrator
*
* @return bool
*/
public function isUserAdministrator()
{
return in_array('administrator', $this->user->roles);
}
/**
* User name
*
* @return string
*/
public function getCurrentUserName()
{
return $this->user ? $this->user->user_nicename : 'unknown';
}
/**
* WP version
*
* @return string
*/
public function getWordPressVersion()
{
global $wp_version;
return $wp_version;
}
/**
* PHP version
*
* @return string
*/
public function getPHPVersion()
{
return phpversion();
}
/**
* Resolves plugin information
*
* @param string $path Absolute path to plugin
* @return array
*/
public function resolvePluginData($path)
{
if( !function_exists('get_plugin_data') ){
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
return get_plugin_data($path);
}
/**
* Get plugin name by path
*
* @param $path
* @return string
*/
public function getPluginName($path)
{
$data = $this->resolvePluginData($path);
return $data['Name'];
}
/**
* Get plugin version
*
* @param $path
* @return string
*/
public function getPluginVersion($path)
{
$data = $this->resolvePluginData($path);
return $data['Version'] ? $data['Version'] : null;
}
/**
* Get server ip
*
* @return string
*/
public function getServerIp()
{
return sanitize_text_field($_SERVER['SERVER_ADDR']);
}
/**
* @return string
*/
public function getSDKVersion()
{
return $this->sdk->version();
}
/**
* @return string
*/
public function getMysqlVersion()
{
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn) {
$version = mysqli_get_server_info($conn);
} else {
return 'unknown';
}
return $version ? $version : 'unknown';
}
/**
* @return string
*/
public function getSiteLanguage()
{
return get_locale();
}
/**
* Current WP theme
*
* @return false|string
*/
public function getCurrentThemeName()
{
return wp_get_theme()->get('Name');
}
/**
* Get active plugins list
*
* @return array
*/
public function getActivePluginsList()
{
if (!function_exists('get_plugins')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$allPlugins = get_plugins();
$activePluginsNames = array_map(function ($path) use ($allPlugins) {
return $allPlugins[$path]['Name'];
}, get_option('active_plugins'));
return $activePluginsNames;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Analyst\Contracts;
interface AnalystContract
{
/**
* Must return version of analyst
*
* @return string
*/
public static function version();
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Analyst\Contracts;
/**
* Interface CacheContract
*
* @since 1.1.5
*/
interface CacheContract
{
/**
* Save value with given key
*
* @param string $key
* @param string $value
*
* @return static
*/
public function put($key, $value);
/**
* Get value by given key
*
* @param $key
*
* @param null $default
* @return string
*/
public function get($key, $default = null);
/**
* @param $key
*
* @return static
*/
public function delete($key);
/**
* Should get value and remove it from cache
*
* @param $key
* @param null $default
* @return mixed
*/
public function pop($key, $default = null);
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Analyst\Contracts;
use Analyst\ApiResponse;
interface HttpClientContract
{
/**
* Make an http request
*
* @param $method
* @param $url
* @param $body
* @param $headers
* @return ApiResponse
*/
public function request($method, $url, $body, $headers);
/**
* Must return `true` if client is supported
*
* @return bool
*/
public static function hasSupport();
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Analyst\Contracts;
use Analyst\ApiResponse;
interface RequestContract
{
/**
* Cast request data to array
*
* @return array
*/
public function toArray();
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor);
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Analyst\Contracts;
interface RequestorContract
{
/**
* Get request
*
* @param $url
* @param array $headers
* @return mixed
*/
public function get($url, $headers = []);
/**
* Post request
*
* @param $url
* @param $body
* @param array $headers
* @return mixed
*/
public function post($url, $body = [], $headers = []);
/**
* Put request
*
* @param $url
* @param $body
* @param array $headers
* @return mixed
*/
public function put($url, $body = [], $headers = []);
/**
* Delete request
*
* @param $url
* @param array $headers
* @return mixed
*/
public function delete($url, $headers = []);
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Analyst\Contracts;
interface TrackerContract
{
/**
* Should register activation and deactivation
* event hooks
*
* @return void
*/
public function registerHooks();
/**
* Will fire when admin activates plugin
*
* @return void
*/
public function onActivePluginListener();
/**
* Will fire when admin deactivates plugin
*
* @return void
*/
public function onDeactivatePluginListener();
/**
* Will fire when user opted in
*
* @return void
*/
public function onOptInListener();
/**
* Will fire when user opted out
*
* @return void
*/
public function onOptOutListener();
/**
* Will fire when user accept opt/in at first time
*
* @return void
*/
public function onInstallListener();
/**
* Will fire when user skipped installation
*
* @return void
*/
public function onSkipInstallListener();
/**
* Will fire when user delete plugin through admin panel.
* This action will happen if admin at least once
* activated the plugin.
*
* The register_uninstall_hook function accepts only static
* function or global function to be executed, so this is
* why this method is static
*
* @return void
*/
public static function onUninstallPluginListener();
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Analyst\Core;
abstract class AbstractFactory
{
/**
* Unserialize to static::class instance
*
* @param $raw
* @return static
*/
protected static function unserialize($raw)
{
$instance = maybe_unserialize($raw);
$isProperObject = is_object($instance) && $instance instanceof static;
// In case for some reason unserialized object is not
// static::class we make sure it is static::class
if (!$isProperObject) {
$instance = new static();
}
return $instance;
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Analyst\Http;
use Analyst\ApiResponse;
use Analyst\Contracts\HttpClientContract;
class CurlHttpClient implements HttpClientContract
{
/**
* Make an http request
*
* @param $method
* @param $url
* @param array $body
* @param $headers
* @return mixed
*/
public function request($method, $url, $body, $headers)
{
$method = strtoupper($method);
$options = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => $this->prepareRequestHeaders($headers),
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_FAILONERROR => true,
CURLOPT_HEADER => true,
CURLOPT_TIMEOUT => 30,
];
if ($method === 'POST') {
$options[CURLOPT_POST] = 1;
$options[CURLOPT_POSTFIELDS] = json_encode($body);
}
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
list($rawHeaders, $rawBody) = explode("\r\n\r\n", $response, 2);
$info = curl_getinfo($curl);
curl_close($curl);
$responseHeaders = $this->resolveResponseHeaders($rawHeaders);
$responseBody = json_decode($rawBody, true);
return new ApiResponse($responseBody, $info['http_code'], $responseHeaders);
}
/**
* Must return `true` if client is supported
*
* @return bool
*/
public static function hasSupport()
{
return function_exists('curl_version');
}
/**
* Modify request headers from key value pair
* to vector array
*
* @param array $headers
* @return array
*/
protected function prepareRequestHeaders ($headers)
{
return array_map(function ($key, $value) {
return sprintf('%s:%s', $key, $value);
}, array_keys($headers), $headers);
}
/**
* Resolve raw response headers as
* associative array
*
* @param $rawHeaders
* @return array
*/
private function resolveResponseHeaders($rawHeaders)
{
$headers = [];
foreach (explode("\r\n", $rawHeaders) as $i => $line) {
$parts = explode(': ', $line);
if (count($parts) === 1) {
continue;
}
$headers[$parts[0]] = $parts[1];
}
return $headers;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Analyst\Http;
use Analyst\ApiResponse;
use Analyst\Contracts\HttpClientContract;
class DummyHttpClient implements HttpClientContract
{
/**
* Make an http request
*
* @param $method
* @param $url
* @param $body
* @param $headers
* @return ApiResponse
*/
public function request($method, $url, $body, $headers)
{
return new ApiResponse('Dummy response', 200, []);
}
/**
* Must return `true` if client is supported
*
* @return bool
*/
public static function hasSupport()
{
return true;
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestContract;
use Analyst\Contracts\RequestorContract;
abstract class AbstractLoggerRequest implements RequestContract
{
/**
* @var Collector
*/
protected $collector;
/**
* @var integer
*/
protected $id;
/**
* @var string
*/
protected $path;
public function __construct(Collector $collector, $pluginId, $path)
{
$this->collector = $collector;
$this->id = $pluginId;
$this->path = $path;
}
/**
* Cast request data to array
*
* @return array
*/
public function toArray()
{
return [
'plugin_id' => $this->id,
'php_version' => $this->collector->getPHPVersion(),
'wp_version' => $this->collector->getWordPressVersion(),
'plugin_version' => $this->collector->getPluginVersion($this->path),
'url' => $this->collector->getSiteUrl(),
'sdk_version' => $this->collector->getSDKVersion(),
'ip' => $this->collector->getServerIp(),
'mysql_version' => $this->collector->getMysqlVersion(),
'locale' => $this->collector->getSiteLanguage(),
'current_theme' => $this->collector->getCurrentThemeName(),
'active_plugins_list' => implode(', ', $this->collector->getActivePluginsList()),
'email' => $this->collector->getGeneralEmailAddress(),
'name' => $this->collector->getCurrentUserName()
];
}
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public abstract function execute(RequestorContract $requestor);
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestContract;
use Analyst\Contracts\RequestorContract;
/**
* Class ActivateRequest
*
* Is is very similar to install request
* but with different path
*
* @since 0.9.12
*/
class ActivateRequest extends AbstractLoggerRequest
{
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/activate', $this->toArray());
}
/**
* Make request instance
*
* @param Collector $collector
* @param $pluginId
* @param $path
* @return static
*/
public static function make(Collector $collector, $pluginId, $path)
{
return new static($collector, $pluginId, $path);
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestorContract;
/**
* Class DeactivateRequest
*
* @since 0.9.10
*/
class DeactivateRequest extends AbstractLoggerRequest
{
/**
* @var string
*/
protected $question;
/**
* @var string
*/
protected $answer;
/**
* @param Collector $collector
* @param $pluginId
* @param $path
* @param $question
* @param $answer
* @return static
*/
public static function make(Collector $collector, $pluginId, $path, $question, $answer)
{
return new static($collector, $pluginId, $path, $question, $answer);
}
public function __construct(Collector $collector, $pluginId, $path, $question, $answer)
{
parent::__construct($collector, $pluginId, $path);
$this->question = $question;
$this->answer = $answer;
}
public function toArray()
{
return array_merge(parent::toArray(), [
'question' => $this->question,
'answer' => $this->answer,
]);
}
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/deactivate', $this->toArray());
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestorContract;
/**
* Class InstallRequest
*
* @since 0.9.4
*/
class InstallRequest extends AbstractLoggerRequest
{
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/install', $this->toArray());
}
/**
* Make request instance
*
* @param Collector $collector
* @param $pluginId
* @param $path
* @return static
*/
public static function make(Collector $collector, $pluginId, $path)
{
return new static($collector, $pluginId, $path);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestContract;
use Analyst\Contracts\RequestorContract;
/**
* Class OptInRequest
*
* Is is very similar to install request
* but with different path
*
* @since 0.9.5
*/
class OptInRequest extends AbstractLoggerRequest
{
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/opt-in', $this->toArray());
}
/**
* Make request instance
*
* @param Collector $collector
* @param $pluginId
* @param $path
* @return static
*/
public static function make(Collector $collector, $pluginId, $path)
{
return new static($collector, $pluginId, $path);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestContract;
use Analyst\Contracts\RequestorContract;
/**
* Class OptOutRequest
*
* Is is very similar to install request
* but with different path
*
* @since 0.9.9
*/
class OptOutRequest extends AbstractLoggerRequest
{
/**
* @param Collector $collector
* @param $pluginId
* @param $path
* @return static
*/
public static function make(Collector $collector, $pluginId, $path)
{
return new static($collector, $pluginId, $path);
}
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/opt-out', $this->toArray());
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Analyst\Http\Requests;
use Analyst\ApiResponse;
use Analyst\Collector;
use Analyst\Contracts\RequestorContract;
/**
* Class DeactivateRequest
*
* @since 0.9.13
*/
class UninstallRequest extends AbstractLoggerRequest
{
/**
* @param Collector $collector
* @param $pluginId
* @param $path
* @return static
*/
public static function make(Collector $collector, $pluginId, $path)
{
return new static($collector, $pluginId, $path);
}
/**
* Execute the request
* @param RequestorContract $requestor
* @return ApiResponse
*/
public function execute(RequestorContract $requestor)
{
return $requestor->post('logger/uninstall', $this->toArray());
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Analyst\Http;
use WP_Error;
use Analyst\ApiResponse;
use Analyst\Contracts\HttpClientContract;
use Requests_Utility_CaseInsensitiveDictionary;
class WordPressHttpClient implements HttpClientContract
{
/**
* Make an http request
*
* @param $method
* @param $url
* @param $body
* @param $headers
* @return ApiResponse
*/
public function request($method, $url, $body, $headers)
{
$options = [
'body' => json_encode($body),
'headers' => $headers,
'method' => $method,
'timeout' => 30,
];
$response = wp_remote_request($url, $options);
$body = [];
$responseHeaders = [];
if ($response instanceof WP_Error) {
$code = $response->get_error_code();
} else {
/** @var Requests_Utility_CaseInsensitiveDictionary $headers */
$responseHeaders = $response['headers']->getAll();
$body = json_decode($response['body'], true);
$code = $response['response']['code'];
}
return new ApiResponse(
$body,
$code,
$responseHeaders
);
}
/**
* Must return `true` if client is supported
*
* @return bool
*/
public static function hasSupport()
{
return function_exists('wp_remote_request');
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace Analyst;
use Analyst\Cache\DatabaseCache;
use Analyst\Contracts\CacheContract;
use Analyst\Notices\NoticeFactory;
/**
* Class Mutator mutates (modifies) UX with additional
* functional
*/
class Mutator
{
protected $notices = [];
/**
* @var NoticeFactory
*/
protected $factory;
/**
* @var CacheContract
*/
protected $cache;
public function __construct()
{
$this->factory = NoticeFactory::instance();
$this->notices = $this->factory->getNotices();
$this->cache = DatabaseCache::getInstance();
}
/**
* Register filters all necessary stuff.
* Can be invoked only once.
*
* @return void
*/
public function initialize()
{
$this->registerLinks();
$this->registerAssets();
$this->registerHooks();
}
/**
* Register all necessary filters and templates
*
* @return void
*/
protected function registerLinks()
{
add_action('admin_footer', function () {
analyst_require_template('optout.php', [
'shieldImage' => analyst_assets_url('img/shield_question.png')
]);
analyst_require_template('optin.php');
analyst_require_template('forms/deactivate.php', [
'pencilImage' => analyst_assets_url('img/pencil.png'),
'smileImage' => analyst_assets_url('img/smile.png'),
]);
analyst_require_template('forms/install.php', [
'pluginToInstall' => $this->cache->get('plugin_to_install'),
'shieldImage' => analyst_assets_url('img/shield_success.png'),
]);
});
add_action('admin_notices',function () {
foreach ($this->notices as $notice) {
analyst_require_template('notice.php', ['notice' => $notice]);
}
});
}
/**
* Register all assets
*/
public function registerAssets()
{
add_action('admin_enqueue_scripts', function () {
wp_enqueue_style('analyst_custom', analyst_assets_url('/css/customize.css'));
wp_enqueue_script('analyst_custom', analyst_assets_url('/js/customize.js'));
wp_localize_script('analyst_custom', 'analyst_opt_localize', array(
'nonce' => wp_create_nonce('analyst_opt_ajax_nonce')
));
});
}
/**
* Register action hooks
*/
public function registerHooks()
{
add_action('wp_ajax_analyst_notification_dismiss', function () {
$capabilities = [
'activate_plugins',
'edit_plugins',
'install_plugins',
'update_plugins',
'delete_plugins',
'manage_network_plugins',
'upload_plugins'
];
// Allow if has any of above permissions
$hasPerms = false;
foreach ($capabilities as $i => $cap) {
if (current_user_can($cap)) {
$hasPerms = true;
break;
}
}
if ($hasPerms == false) {
wp_send_json_error(['message' => 'no_permissions']);
die;
}
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['nonce']), 'analyst_opt_ajax_nonce')) {
wp_send_json_error(['message' => 'invalid_nonce']);
die;
}
$this->factory->remove(sanitize_text_field($_POST['id']));
$this->factory->sync();
});
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace Analyst\Notices;
class Notice
{
/**
* Id of notice
*
* @var string
*/
protected $id;
/**
* Body of notice
*
* @var string
*/
protected $body;
/**
* Account id
*
* @var string
*/
protected $accountId;
/**
* The plugin name
*
* @var string
*/
protected $pluginName;
/**
* New notice
*
* @param $id
* @param $accountId
* @param $body
* @param null $pluginName
*
* @return Notice
*/
public static function make($id, $accountId, $body, $pluginName = null)
{
return new Notice($id, $accountId, $body, $pluginName);
}
public function __construct($id, $accountId, $body, $pluginName)
{
$this->setId($id);
$this->setBody($body);
$this->setAccountId($accountId);
$this->setPluginName($pluginName);
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getBody()
{
return $this->body;
}
/**
* @param string $body
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* @return string
*/
public function getAccountId()
{
return $this->accountId;
}
/**
* @param string $accountId
*/
public function setAccountId($accountId)
{
$this->accountId = $accountId;
}
/**
* @return string|null
*/
public function getPluginName()
{
return $this->pluginName;
}
/**
* @param string $pluginName
*/
public function setPluginName($pluginName)
{
$this->pluginName = $pluginName;
}
}

View File

@@ -0,0 +1,130 @@
<?php
namespace Analyst\Notices;
use Analyst\Core\AbstractFactory;
class NoticeFactory extends AbstractFactory
{
private static $instance;
CONST OPTIONS_KEY = 'analyst_notices';
/**
* Application notifications
*
* @var array
*/
protected $notices = [];
/**
* Read factory from options or make fresh instance
*
* @return NoticeFactory
*/
public static function instance()
{
if (!static::$instance) {
$raw = get_option(self::OPTIONS_KEY);
// In case object is already unserialized
// and instance of AccountDataFactory we
// return it, in other case deal with
// serialized string data
if ($raw instanceof self) {
static::$instance = $raw;
} else {
static::$instance = is_string($raw) ? static::unserialize($raw) : new self();
}
}
return static::$instance;
}
/**
* Sync this object data with cache
*/
public function sync()
{
update_option(self::OPTIONS_KEY, serialize($this));
}
/**
* Sync this instance data with cache
*/
public static function syncData()
{
static::instance()->sync();
}
/**
* @return array
*/
public function getNotices()
{
return $this->notices;
}
/**
* Filter out notices for certain account
*
* @param $accountId
* @return array
*/
public function getNoticesForAccount($accountId)
{
return array_filter($this->notices, function (Notice $notice) use ($accountId) {
return $notice->getAccountId() === $accountId;
});
}
/**
* Add new notice
*
* @param $notice
*
* @return $this
*/
public function addNotice($notice)
{
array_push($this->notices, $notice);
$this->sync();
return $this;
}
/**
* Find notice by id
*
* @param $id
* @return Notice|null
*/
public function find($id)
{
$notices = array_filter($this->notices, function (Notice $notice) use ($id) {
return $notice->getId() === $id;
});
return array_pop($notices);
}
/**
* Remove notice by it's id
*
* @param $id
*/
public function remove($id)
{
// Get key of notice to remove
$key = array_search(
$this->find($id),
$this->notices
);
// Unset notice with key
unset($this->notices[$key]);
$this->sync();
}
}

View File

@@ -0,0 +1,73 @@
<?php
if (! function_exists('analyst_assets_path')) {
/**
* Generates path to file in assets folder
*
* @param $file
* @return string
*/
function analyst_assets_path($file)
{
$path = sprintf('%s/assets/%s', realpath(__DIR__ . '/..'), trim($file, '/'));
return wp_normalize_path($path);
}
}
if (! function_exists('analyst_assets_url')) {
/**
* Generates url to file in assets folder
*
* @param $file
* @return string
*/
function analyst_assets_url($file)
{
$absolutePath = analyst_assets_path($file);
// We can always rely on WP_PLUGIN_DIR, because that's where
// wordpress install it's plugin's. So we remove last segment
// of that path to get the content dir AKA directly where
// plugins are installed and make the magic...
$contentDir = is_link(WP_PLUGIN_DIR) ?
dirname(wp_normalize_path(readlink(WP_PLUGIN_DIR))) :
dirname(wp_normalize_path(WP_PLUGIN_DIR));
$relativePath = str_replace( $contentDir, '', $absolutePath);
return content_url(wp_normalize_path($relativePath));
}
}
if (! function_exists('analyst_templates_path')) {
/**
* Generates path to file in templates folder
*
* @param $file
* @return string
*/
function analyst_templates_path($file)
{
$path = sprintf('%s/templates/%s', realpath(__DIR__ . '/..'), trim($file, '/'));
return wp_normalize_path($path);
}
}
if (! function_exists('analyst_require_template')) {
/**
* Require certain template with data
*
* @param $file
* @param array $data
*/
function analyst_require_template($file, $data = [])
{
// Extract data to current scope table
extract($data);
require analyst_templates_path($file);
}
}

View File

@@ -0,0 +1,157 @@
<div id="analyst-deactivate-modal" class="analyst-modal" style="display: none">
<div class="analyst-modal-content" style="width: 500px">
<div class="analyst-disable-modal-mask" id="analyst-disable-deactivate-modal-mask" style="display: none"></div>
<div style="display: flex">
<div class="analyst-install-image-block" style="width: 80px">
<img src="<?php echo $pencilImage; ?>"/>
</div>
<div class="analyst-install-description-block" style="padding-left: 20px">
<strong class="analyst-modal-header">Why do you deactivate?</strong>
<div class="analyst-install-description-text" style="padding-top: 2px">
Please let us know, so we can improve it! Thank you <img class="analyst-smile-image" src="<?php echo $smileImage; ?>" alt="">
</div>
</div>
</div>
<div>
<ul id="analyst-deactivation-reasons">
<li>
<label>
<span>
<input type="radio" name="deactivation-reason">
</span>
<span class="question" data-question="I couldn't understand how to make it work">I couldn't understand how to make it work</span>
</label>
</li>
<li data-input-type="textarea" data-input-placeholder="What should have worked, but didnt?">
<label>
<span>
<input type="radio" name="deactivation-reason">
</span>
<span class="question" data-question="The plugin didn't work as expected">The plugin didn't work as expected</span>
</label>
<div class="question-answer"></div>
</li>
<li data-input-type="input" data-input-placeholder="What is the plugin name?">
<label>
<span>
<input type="radio" name="deactivation-reason">
</span>
<span class="question" data-question="I found a better plugin">I found a better plugin</span>
</label>
<div class="question-answer"></div>
</li>
<li>
<label>
<span>
<input type="radio" name="deactivation-reason">
</span>
<span class="question" data-question="It's a temporary deactivation">It's a temporary deactivation</span>
</label>
<div class="question-answer"></div>
</li>
<li data-input-type="textarea" data-input-placeholder="Please provide the reason of deactivation">
<label>
<span>
<input type="radio" name="deactivation-reason">
</span>
<span class="question" data-question="Other">Other</span>
</label>
<div class="question-answer"></div>
</li>
</ul>
<p id="analyst-deactivation-error" style="color: #dc3232; font-size: 16px; display: none">Please let us know the reason for de-activation. Thank you!</p>
</div>
<div>
<button class="analyst-btn-grey" id="analyst-disabled-plugin-action">Deactivate</button>
</div>
<div class="" style="text-align: center; font-size: 18px; padding-top: 10px">
<button class="analyst-btn-secondary-ghost analyst-deactivate-modal-close" style="color: #cccccc">Cancel</button>
</div>
</div>
</div>
<script type="text/javascript">
(function ($) {
$('.deactivate').click(function (e) {
var anchor = $(this).find('[analyst-plugin-id]')
var pluginId = anchor.attr('analyst-plugin-id')
var isOptedIn = anchor.attr('analyst-plugin-opted-in') === '1'
// Do not ask for reason if not opted in
if (!isOptedIn) {
return
}
e.preventDefault()
$('#analyst-deactivate-modal')
.attr({
'analyst-plugin-id': pluginId,
'analyst-redirect-url': $(this).find('a').attr('href')
})
.show()
})
$('.analyst-deactivate-modal-close').click(function () {
$('#analyst-deactivate-modal').hide()
})
$('#analyst-deactivation-reasons input[name="deactivation-reason"]').change(function () {
$('.question-answer').empty()
var root = $('#analyst-deactivation-reasons input[name="deactivation-reason"]:checked').parents('li')
$('#analyst-deactivation-error').hide()
if (!root.attr('data-input-type')) return
var reasonInput = $('<' + root.attr('data-input-type') + '/>').attr({placeholder: root.attr('data-input-placeholder'), class: 'reason-answer'})
root.find('.question-answer').append(reasonInput)
})
$('#analyst-disabled-plugin-action').click(function () {
var pluginId = $('#analyst-deactivate-modal').attr('analyst-plugin-id')
var pluginDeactivationUrl = $('#analyst-deactivate-modal').attr('analyst-redirect-url')
var root = $('#analyst-deactivation-reasons input[name="deactivation-reason"]:checked').parents('li');
var reason = root.find('.question-answer .reason-answer').val();
var question = root.find('.question').attr('data-question').trim()
var $errorBlock = $('#analyst-deactivation-error')
if (!question) {
return $errorBlock.show()
}
$errorBlock.hide()
var data = {
action: 'analyst_plugin_deactivate_' + pluginId,
question: question,
nonce: analyst_opt_localize.nonce
}
if (reason) {
data['reason'] = reason.trim();
}
$(this).attr('disabled', true).text('Deactivating...');
$('#analyst-disable-deactivate-modal-mask').show();
$.ajax({
url: ajaxurl,
method: 'POST',
data: data
}).done(function () {
window.location.href = pluginDeactivationUrl
$('#analyst-disable-deactivate-modal-mask').hide();
})
})
})(jQuery)
</script>

View File

@@ -0,0 +1,117 @@
<div id="analyst-install-modal" class="analyst-modal" style="display: none" analyst-plugin-id="<?php echo $pluginToInstall; ?>">
<div class="analyst-modal-content" style="width: 450px">
<div class="analyst-disable-modal-mask" id="analyst-disable-install-modal-mask" style="display: none"></div>
<div style="display: flex">
<div class="analyst-install-image-block">
<img src="<?php echo $shieldImage; ?>"/>
</div>
<div class="analyst-install-description-block">
<strong class="analyst-modal-header">Stay on the safe side</strong>
<p class="analyst-install-description-text">Receive our plugins alerts in
case of <strong>critical security</strong>, feature & special deal
updates and allow non-sensitive
diagnostic tracking.</p>
</div>
</div>
<div class="analyst-modal-def-top-padding">
<button class="analyst-btn-success" id="analyst-install-action">Allow & Continue &gt;</button>
</div>
<div class="analyst-modal-def-top-padding" id="analyst-permissions-block" style="display: none">
<span>Youre granting these permissions:</span>
<ul class="analyst-install-permissions-list">
<li><strong>Your profile information</strong> (name and email) </li>
<li><strong>Your site information</strong> (URL, WP version, PHP info, plugins & themes)</li>
<li><strong>Plugin notices</strong> (updates, announcements, marketing, no spam)</li>
<li><strong>Plugin events</strong> (activation, deactivation and uninstall)</li>
</ul>
</div>
<div class="analyst-install-footer analyst-modal-def-top-padding">
<span class="analyst-action-text" id="analyst-permissions-toggle">Learn more</span>
<span id="analyst-powered-by" style="display: none;">Powered by <a href="https://sellcodes.com/blog/wordpress-feedback-system-for-plugin-creators/?utm_source=optin_screen" target="_blank" class="analyst-link">Sellcodes.com</a></span>
<span class="analyst-action-text analyst-install-modal-close" id="analyst-install-skip">Skip</span>
</div>
<div id="analyst-install-error" class="analyst-modal-def-top-padding" style="display: none; text-align: center">
<span style="color: #dc3232; font-size: 16px">Service unavailable. Please try again later</span>
</div>
</div>
</div>
<script type="text/javascript">
(function ($) {
var installPlugin = function (pluginId) {
var $error = $('#analyst-install-error')
$error.hide()
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'analyst_install_' + pluginId,
nonce: analyst_opt_localize.nonce
},
success: function (data) {
if (data && !data.success) {
//error
$('#analyst-install-modal').hide()
return
}
window.location.reload()
},
error: function () {
$('#analyst-install-modal').hide()
}
}).done(function () {
$('#analyst-disable-install-modal-mask').hide()
$('#analyst-install-action')
.attr('disabled', false)
.text('Allow & Continue >')
})
}
if ($('#analyst-install-modal').attr('analyst-plugin-id')) {
$('#analyst-install-modal').show()
}
$('.analyst-install-modal-close').click(function () {
$('#analyst-install-modal').hide()
})
$('#analyst-install-action').click(function () {
var pluginId = $('#analyst-install-modal').attr('analyst-plugin-id')
$('#analyst-install-action')
.attr('disabled', true)
.text('Please wait...')
$('#analyst-disable-install-modal-mask').show()
installPlugin(pluginId)
})
$('#analyst-permissions-toggle').click(function () {
var isVisible = $('#analyst-permissions-block').toggle().is(':visible')
isVisible ? $(this).text('Close section') : $(this).text('Learn more')
var poweredBy = $('#analyst-powered-by')
isVisible ? poweredBy.show() : poweredBy.hide()
})
$('#analyst-install-skip').click(function () {
var pluginId = $('#analyst-install-modal').attr('analyst-plugin-id')
$.post(ajaxurl, {
action: 'analyst_skip_install_' + pluginId,
nonce: analyst_opt_localize.nonce
}).done(function () {
$('#analyst-install-modal').hide()
})
})
})(jQuery)
</script>

View File

@@ -0,0 +1,10 @@
<div class="notice notice-success analyst-notice">
<p>
<strong class="analyst-plugin-name"><?php echo $notice->getPluginName(); ?></strong>
<?php echo $notice->getBody(); ?>
</p>
<button type="button" class="analyst-notice-dismiss notice-dismiss" analyst-notice-id="<?php echo $notice->getId(); ?>">
<span class="screen-reader-text">Dismiss this notice.</span>
</button>
</div>

View File

@@ -0,0 +1,61 @@
<script type="text/javascript">
(function ($) {
var isOptingIn = false
$('#analyst-opt-in-modal').appendTo($('body'))
var makeOptIn = function (pluginId) {
if (isOptingIn) return
isOptingIn = true
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'analyst_opt_in_' + pluginId,
nonce: analyst_opt_localize.nonce
},
success: function () {
$('#analyst-opt-in-modal').hide()
isOptingIn = false
var optOutAction = $('<a />').attr({
class: 'analyst-action-opt analyst-opt-out',
'analyst-plugin-id': pluginId,
'analyst-plugin-signed': '1'
})
.text('Opt Out')
$('.analyst-opt-in[analyst-plugin-id="'+ pluginId +'"').replaceWith(optOutAction)
$('[analyst-plugin-id="' + pluginId + '"').attr('analyst-plugin-opted-in', 1)
}
})
}
$(document).on('click', '.analyst-opt-in:not([loading])', function() {
var pluginId = $(this).attr('analyst-plugin-id')
var isSigned = $(this).attr('analyst-plugin-signed') === '1'
if (!isSigned) {
$('#analyst-install-modal')
.attr('analyst-plugin-id', pluginId)
.show()
return;
}
$('#analyst-install-modal').attr({'analyst-plugin-id': pluginId})
$(this).attr('loading', true).text('Opting In...')
makeOptIn(pluginId);
})
$('.opt-in-modal-close').click(function () {
$('#analyst-opt-in-modal').hide()
})
})(jQuery)
</script>

View File

@@ -0,0 +1,110 @@
<div id="analyst-opt-out-modal" class="analyst-modal" style="display: none">
<div class="analyst-modal-content" style="width: 600px">
<div class="analyst-disable-modal-mask" id="analyst-disable-opt-out-modal-mask" style="display: none"></div>
<div style="display: flex">
<div class="analyst-install-image-block" style="width: 120px">
<img src="<?php echo $shieldImage; ?>"/>
</div>
<div class="analyst-install-description-block">
<strong class="analyst-modal-header">By opting out, we cannot alert you anymore in case of important security updates.</strong>
<p class="analyst-install-description-text">
In addition, we wont get pointers how to further improve the plugin based on your integration with our plugin.
</p>
</div>
</div>
<div class="analyst-modal-def-top-padding">
<button class="analyst-btn-success opt-out-modal-close">Ok, don't opt out</button>
</div>
<div class="analyst-modal-def-top-padding" style="text-align: center;">
<button class="analyst-btn-secondary-ghost" id="opt-out-action">Opt out</button>
</div>
<div id="analyst-opt-out-error" class="analyst-modal-def-top-padding" style="display: none;">
<span style="color: #dc3232; font-size: 16px">Service unavailable. Please try again later</span>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function ($) {
var isOptingOut = false
$('#analyst-opt-out-modal').appendTo($('body'))
$(document).on('click', '.analyst-opt-out', function() {
var pluginId = $(this).attr('analyst-plugin-id')
$('#analyst-opt-out-modal')
.attr({'analyst-plugin-id': pluginId})
.show()
})
$('.opt-out-modal-close').click(function () {
$('#analyst-opt-out-modal').hide()
})
$('#opt-out-action').click(function () {
if (isOptingOut) return
var $mask = $('#analyst-disable-opt-out-modal-mask')
var $error = $('#analyst-opt-out-error')
var pluginId = $('#analyst-opt-out-modal').attr('analyst-plugin-id')
$mask.show()
$error.hide()
var self = this
isOptingOut = true
$(self).text('Opting out...')
$.ajax({
url: ajaxurl,
method: 'POST',
data: {
action: 'analyst_opt_out_' + pluginId,
nonce: analyst_opt_localize.nonce
},
success: function (data) {
$(self).text('Opt out')
if (data && !data.success) {
$('#analyst-opt-out-modal').hide()
return
}
$error.hide()
$('#analyst-opt-out-modal').hide()
isOptingOut = false
var optInAction = $('<a />').attr({
class: 'analyst-action-opt analyst-opt-in',
'analyst-plugin-id': pluginId,
'analyst-plugin-signed': '1'
})
.text('Opt In')
$('.analyst-opt-out[analyst-plugin-id="'+ pluginId +'"').replaceWith(optInAction)
$('[analyst-plugin-id="' + pluginId + '"').attr('analyst-plugin-opted-in', 0)
$mask.hide()
},
error: function () {
$('#analyst-opt-out-error').show()
$(self).text('Opt out')
}
}).done(function () {
$mask.hide()
isOptingOut = false
})
})
})(jQuery)
</script>

View File

@@ -0,0 +1,15 @@
<?php
return array(
// The sdk version
'sdk' => '1.3.30',
// Minimum supported WordPress version
'wp' => '4.7',
// Supported PHP version
'php' => '5.4',
// Path to current SDK$
'path' => __DIR__,
);