first commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Exception;
|
||||
|
||||
class DuplicatedFormOptionKeyException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Exception;
|
||||
|
||||
class DuplicatedFormValueKeyException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Exception;
|
||||
|
||||
class ReservedFormOptionKeyException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Exception;
|
||||
|
||||
class SenderRequestFailedException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Exception;
|
||||
|
||||
class UnknownFormOptionKeyException extends \Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal;
|
||||
|
||||
interface Hookable
|
||||
{
|
||||
/**
|
||||
* Init hooks (actions and filters).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormOptions;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormTemplate;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormValues;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Sender\Sender;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Service\AssetsPrinterService;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Service\RequestSenderService;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Service\TemplateGeneratorService;
|
||||
/**
|
||||
* Manages the modal displayed when the plugin is deactivated.
|
||||
*/
|
||||
class Modal
|
||||
{
|
||||
const MODAL_TEMPLATE_PATH = __DIR__ . '/../templates/modal.php';
|
||||
const MODAL_ASSETS_PATH_CSS = __DIR__ . '/../assets/build/css/styles.css';
|
||||
const MODAL_ASSETS_PATH_JS = __DIR__ . '/../assets/build/js/scripts.js';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_slug;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $form_template;
|
||||
/**
|
||||
* @var FormOptions
|
||||
*/
|
||||
private $form_options;
|
||||
/**
|
||||
* @var FormValues
|
||||
*/
|
||||
private $form_values;
|
||||
/**
|
||||
* @var Sender
|
||||
*/
|
||||
private $request_sender;
|
||||
/**
|
||||
* @param string $plugin_slug Example: "plugin-name".
|
||||
* @param FormTemplate $form_template Information about the the deactivation modal template.
|
||||
* @param FormOptions $form_options List of plugin deactivation reasons to choose from.
|
||||
* @param FormValues $form_values Values sent in the request that reports the plugin deactivation.
|
||||
* @param Sender $request_sender Sends a request with a plugin deactivation report.
|
||||
*/
|
||||
public function __construct(string $plugin_slug, \FcfVendor\WPDesk\DeactivationModal\Model\FormTemplate $form_template, \FcfVendor\WPDesk\DeactivationModal\Model\FormOptions $form_options, \FcfVendor\WPDesk\DeactivationModal\Model\FormValues $form_values, \FcfVendor\WPDesk\DeactivationModal\Sender\Sender $request_sender)
|
||||
{
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
$this->form_template = $form_template;
|
||||
$this->form_options = $form_options;
|
||||
$this->form_values = $form_values;
|
||||
$this->request_sender = $request_sender;
|
||||
(new \FcfVendor\WPDesk\DeactivationModal\Service\AssetsPrinterService($this->plugin_slug))->hooks();
|
||||
(new \FcfVendor\WPDesk\DeactivationModal\Service\TemplateGeneratorService($this->plugin_slug, $this->form_template, $this->form_options, $this->form_values))->hooks();
|
||||
(new \FcfVendor\WPDesk\DeactivationModal\Service\RequestSenderService($this->plugin_slug, $this->form_options, $this->form_values, $this->request_sender))->hooks();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\DuplicatedFormOptionKeyException;
|
||||
/**
|
||||
* Default list of plugin deactivation reason for plugins using older libraries.
|
||||
*/
|
||||
class DefaultFormOptions extends \FcfVendor\WPDesk\DeactivationModal\Model\FormOptions
|
||||
{
|
||||
/**
|
||||
* @throws DuplicatedFormOptionKeyException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('plugin_stopped_working', 10, \__('The plugin suddenly stopped working', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('broke_my_site', 20, \__('The plugin broke my site', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('found_better_plugin', 30, \__('I found a better plugin', 'flexible-checkout-fields'), null, \__('What\'s the plugin\'s name?', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('plugin_for_short_period', 40, \__('I only needed the plugin for a short period', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('no_longer_need', 50, \__('I no longer need the plugin', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('temporary_deactivation', 60, \__('It\'s a temporary deactivation (I\'m just debugging an issue)', 'flexible-checkout-fields')));
|
||||
$this->set_option(new \FcfVendor\WPDesk\DeactivationModal\Model\FormOption('other', 70, \__('Other', 'flexible-checkout-fields'), null, \__('Kindly tell us the reason so we can improve', 'flexible-checkout-fields')));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
/**
|
||||
* Stores information about the selectable reason of the plugin deactivation.
|
||||
*/
|
||||
class FormOption
|
||||
{
|
||||
/**
|
||||
* @var string Key of the deactivation reason.
|
||||
*/
|
||||
private $key;
|
||||
/**
|
||||
* @var int Order priority (ascending).
|
||||
*/
|
||||
private $priority;
|
||||
/**
|
||||
* @var string Label of the reason option.
|
||||
*/
|
||||
private $label;
|
||||
/**
|
||||
* @var string|null Message visible after selecting the reason in the form (may contain HTML).
|
||||
*/
|
||||
private $message;
|
||||
/**
|
||||
* @var string|null Label of additional question (visible after selecting the reason in the form).
|
||||
*/
|
||||
private $question;
|
||||
/**
|
||||
* @param string $key Key of the deactivation reason.
|
||||
* @param int $priority Order priority (ascending).
|
||||
* @param string $label Label of the reason option.
|
||||
* @param string|null $message Message visible after selecting the reason in the form (may contain HTML).
|
||||
* @param string|null $question Label of additional question (visible after selecting the reason in the form).
|
||||
*/
|
||||
public function __construct(string $key, int $priority, string $label, string $message = null, string $question = null)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->priority = $priority;
|
||||
$this->label = $label;
|
||||
$this->message = $message;
|
||||
$this->question = $question;
|
||||
}
|
||||
public function get_key() : string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
public function get_priority() : int
|
||||
{
|
||||
return $this->priority;
|
||||
}
|
||||
/**
|
||||
* @param int $priority Order priority (ascending).
|
||||
*/
|
||||
public function set_priority(int $priority) : self
|
||||
{
|
||||
$this->priority = $priority;
|
||||
return $this;
|
||||
}
|
||||
public function get_label() : string
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
/**
|
||||
* @param string $label Label of the reason option.
|
||||
*/
|
||||
public function set_label(string $label) : self
|
||||
{
|
||||
$this->label = $label;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_message()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
/**
|
||||
* @param string|null $message Message visible after selecting the reason in the form (may contain HTML).
|
||||
*/
|
||||
public function set_message(string $message = null) : self
|
||||
{
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_question()
|
||||
{
|
||||
return $this->question;
|
||||
}
|
||||
/**
|
||||
* @param string|null $question Label of additional question (visible after selecting the reason in the form).
|
||||
*/
|
||||
public function set_question(string $question = null) : self
|
||||
{
|
||||
$this->question = $question;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\DuplicatedFormOptionKeyException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\ReservedFormOptionKeyException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\UnknownFormOptionKeyException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Sender\DataWpdeskSender;
|
||||
/**
|
||||
* Manages the list of deactivation reason in the form.
|
||||
*/
|
||||
class FormOptions
|
||||
{
|
||||
/**
|
||||
* @var FormOption[]
|
||||
*/
|
||||
private $options = [];
|
||||
/**
|
||||
* @param FormOption $new_option .
|
||||
*
|
||||
* @throws DuplicatedFormOptionKeyException
|
||||
* @throws ReservedFormOptionKeyException
|
||||
*/
|
||||
public function set_option(\FcfVendor\WPDesk\DeactivationModal\Model\FormOption $new_option) : self
|
||||
{
|
||||
if ($new_option->get_key() === \FcfVendor\WPDesk\DeactivationModal\Sender\DataWpdeskSender::NO_REASON_CHOSEN_KEY) {
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\ReservedFormOptionKeyException(\FcfVendor\WPDesk\DeactivationModal\Sender\DataWpdeskSender::NO_REASON_CHOSEN_KEY);
|
||||
}
|
||||
foreach ($this->options as $option) {
|
||||
if ($option->get_key() === $new_option->get_key()) {
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\DuplicatedFormOptionKeyException($new_option->get_key());
|
||||
}
|
||||
}
|
||||
$this->options[] = $new_option;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param string $option_key .
|
||||
*
|
||||
* @throws UnknownFormOptionKeyException
|
||||
*/
|
||||
public function delete_option(string $option_key) : self
|
||||
{
|
||||
foreach ($this->options as $option_index => $option) {
|
||||
if ($option->get_key() === $option_key) {
|
||||
unset($this->options[$option_index]);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\UnknownFormOptionKeyException($option_key);
|
||||
}
|
||||
/**
|
||||
* @param string $option_key .
|
||||
* @param callable $update_callback Example: "function ( FormOption $option ) { }".
|
||||
*
|
||||
* @throws UnknownFormOptionKeyException
|
||||
*/
|
||||
public function update_option(string $option_key, callable $update_callback) : self
|
||||
{
|
||||
foreach ($this->options as $option) {
|
||||
if ($option->get_key() === $option_key) {
|
||||
\call_user_func($update_callback, $option);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\UnknownFormOptionKeyException($option_key);
|
||||
}
|
||||
/**
|
||||
* @return FormOption[]
|
||||
*/
|
||||
public function get_options() : array
|
||||
{
|
||||
$options = $this->options;
|
||||
\usort($options, function (\FcfVendor\WPDesk\DeactivationModal\Model\FormOption $option_a, \FcfVendor\WPDesk\DeactivationModal\Model\FormOption $option_b) {
|
||||
return $option_a->get_priority() <=> $option_b->get_priority();
|
||||
});
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
/**
|
||||
* Stores information about the the deactivation modal template.
|
||||
*/
|
||||
class FormTemplate
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $form_title;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $form_desc;
|
||||
/**
|
||||
* @param string $plugin_name The full name of the plugin.
|
||||
*/
|
||||
public function __construct(string $plugin_name)
|
||||
{
|
||||
$this->set_form_title(\sprintf(
|
||||
/* translators: %1$s: plugin name */
|
||||
\__('You are deactivating %1$s plugin', 'flexible-checkout-fields'),
|
||||
$plugin_name
|
||||
));
|
||||
$this->set_form_desc(\__('If you have a moment, please let us know why you are deactivating plugin (anonymous feedback):', 'flexible-checkout-fields'));
|
||||
}
|
||||
public function set_form_title(string $form_title) : self
|
||||
{
|
||||
$this->form_title = $form_title;
|
||||
return $this;
|
||||
}
|
||||
public function get_form_title() : string
|
||||
{
|
||||
return $this->form_title;
|
||||
}
|
||||
public function set_form_desc(string $form_desc) : self
|
||||
{
|
||||
$this->form_desc = $form_desc;
|
||||
return $this;
|
||||
}
|
||||
public function get_form_desc() : string
|
||||
{
|
||||
return $this->form_desc;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
/**
|
||||
* Stores information about the additional value sent in the request reporting plugin deactivation.
|
||||
*/
|
||||
class FormValue
|
||||
{
|
||||
/**
|
||||
* @var string Key of the additional value.
|
||||
*/
|
||||
private $key;
|
||||
/**
|
||||
* @var callable A function that returns a text value.
|
||||
*/
|
||||
private $value_callback;
|
||||
/**
|
||||
* @param string $key Key of the additional value.
|
||||
* @param callable $value_callback A function that returns a text value.
|
||||
*/
|
||||
public function __construct(string $key, callable $value_callback)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->value_callback = $value_callback;
|
||||
}
|
||||
public function get_key() : string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
public function get_value_callback() : callable
|
||||
{
|
||||
return $this->value_callback;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\DuplicatedFormValueKeyException;
|
||||
/**
|
||||
* It manages the list of additional information sent in the request reporting plugin deactivation.
|
||||
*/
|
||||
class FormValues
|
||||
{
|
||||
/**
|
||||
* @var FormValue[]
|
||||
*/
|
||||
private $values = [];
|
||||
/**
|
||||
* @param FormValue $new_value .
|
||||
*
|
||||
* @throws DuplicatedFormValueKeyException
|
||||
*/
|
||||
public function set_value(\FcfVendor\WPDesk\DeactivationModal\Model\FormValue $new_value) : self
|
||||
{
|
||||
foreach ($this->values as $value) {
|
||||
if ($value->get_key() === $new_value->get_key()) {
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\DuplicatedFormValueKeyException($new_value->get_key());
|
||||
}
|
||||
}
|
||||
$this->values[] = $new_value;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return FormValue[]
|
||||
*/
|
||||
public function get_values() : array
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Model;
|
||||
|
||||
/**
|
||||
* Stores information containing request data reporting plugin deactivation.
|
||||
*/
|
||||
class RequestData
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_slug;
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $reason_key = null;
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $additional_info = null;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $additional_data = [];
|
||||
/**
|
||||
* @param string $plugin_slug .
|
||||
*/
|
||||
public function __construct(string $plugin_slug)
|
||||
{
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
}
|
||||
public function get_plugin_slug() : string
|
||||
{
|
||||
return $this->plugin_slug;
|
||||
}
|
||||
public function set_reason_key(string $reason_key = null) : self
|
||||
{
|
||||
$this->reason_key = $reason_key;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_reason_key()
|
||||
{
|
||||
return $this->reason_key;
|
||||
}
|
||||
public function set_additional_info(string $additional_info = null) : self
|
||||
{
|
||||
$this->additional_info = $additional_info;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function get_additional_info()
|
||||
{
|
||||
return $this->additional_info;
|
||||
}
|
||||
public function set_additional_data(array $additional_data) : self
|
||||
{
|
||||
$this->additional_data = $additional_data;
|
||||
return $this;
|
||||
}
|
||||
public function set_additional_data_item(string $data_key, string $data_value) : self
|
||||
{
|
||||
$this->additional_data[$data_key] = $data_value;
|
||||
return $this;
|
||||
}
|
||||
public function get_additional_data() : array
|
||||
{
|
||||
return $this->additional_data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Sender;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\SenderRequestFailedException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\RequestData;
|
||||
use WPDesk_Tracker_Sender;
|
||||
use FcfVendor\WPDesk_Tracker_Sender_Wordpress_To_WPDesk;
|
||||
/**
|
||||
* Sends a deactivation report to data.wpdesk.org.
|
||||
*/
|
||||
class DataWpdeskSender implements \FcfVendor\WPDesk\DeactivationModal\Sender\Sender
|
||||
{
|
||||
const HOOK_TRACKER_DEACTIVATION_DATA = 'wpdesk_tracker_deactivation_data';
|
||||
const NO_REASON_CHOSEN_KEY = 'null';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_file_name;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_name;
|
||||
/**
|
||||
* @var WPDesk_Tracker_Sender
|
||||
*/
|
||||
private $tracker_sender;
|
||||
/**
|
||||
* @param string $plugin_file_name Example: "plugin-name/plugin-name.php".
|
||||
* @param string $plugin_name The full name of the plugin.
|
||||
*/
|
||||
public function __construct(string $plugin_file_name, string $plugin_name)
|
||||
{
|
||||
$this->plugin_file_name = $plugin_file_name;
|
||||
$this->plugin_name = $plugin_name;
|
||||
$this->tracker_sender = new \FcfVendor\WPDesk_Tracker_Sender_Wordpress_To_WPDesk();
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function generate_request_data(\FcfVendor\WPDesk\DeactivationModal\Model\RequestData $request_data) : array
|
||||
{
|
||||
$request_body = ['click_action' => 'plugin_deactivation', 'plugin' => $this->plugin_file_name, 'plugin_name' => $this->plugin_name, 'reason' => $request_data->get_reason_key() ?: self::NO_REASON_CHOSEN_KEY, 'additional_data' => $request_data->get_additional_data()];
|
||||
if ($request_data->get_additional_info() !== null && $request_data->get_additional_info() !== '') {
|
||||
$request_body['additional_info'] = $request_data->get_additional_info();
|
||||
}
|
||||
return \apply_filters(self::HOOK_TRACKER_DEACTIVATION_DATA, $request_body, $this->plugin_file_name);
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws SenderRequestFailedException
|
||||
*/
|
||||
public function send_request(\FcfVendor\WPDesk\DeactivationModal\Model\RequestData $request_data) : bool
|
||||
{
|
||||
try {
|
||||
$request_body = $this->generate_request_data($request_data);
|
||||
$response = $this->tracker_sender->send_payload($request_body);
|
||||
return \is_array($response);
|
||||
} catch (\Exception $e) {
|
||||
throw new \FcfVendor\WPDesk\DeactivationModal\Exception\SenderRequestFailedException($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Sender;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\SenderRequestFailedException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\RequestData;
|
||||
interface Sender
|
||||
{
|
||||
/**
|
||||
* @param RequestData $request_data .
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function generate_request_data(\FcfVendor\WPDesk\DeactivationModal\Model\RequestData $request_data) : array;
|
||||
/**
|
||||
* @param RequestData $request_data .
|
||||
*
|
||||
* @return bool Request success status.
|
||||
*
|
||||
* @throws SenderRequestFailedException
|
||||
*/
|
||||
public function send_request(\FcfVendor\WPDesk\DeactivationModal\Model\RequestData $request_data) : bool;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Service;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Hookable;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Modal;
|
||||
/**
|
||||
* Prints the needed contents of CSS and JS files on the plugin list page.
|
||||
*/
|
||||
class AssetsPrinterService implements \FcfVendor\WPDesk\DeactivationModal\Hookable
|
||||
{
|
||||
const PLUGIN_NAME_VARIABLE = '{__PLUGIN_SLUG__}';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_slug;
|
||||
public function __construct(string $plugin_slug)
|
||||
{
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hooks()
|
||||
{
|
||||
\add_action('admin_print_styles-plugins.php', [$this, 'load_styles']);
|
||||
\add_action('admin_print_footer_scripts-plugins.php', [$this, 'load_scripts']);
|
||||
}
|
||||
public function load_styles()
|
||||
{
|
||||
?>
|
||||
<style id="<?php
|
||||
echo \esc_attr($this->plugin_slug);
|
||||
?>-deactivation-modal-css">
|
||||
<?php
|
||||
$plugin_slug = $this->plugin_slug;
|
||||
include_once \FcfVendor\WPDesk\DeactivationModal\Modal::MODAL_ASSETS_PATH_CSS;
|
||||
?>
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
public function load_scripts()
|
||||
{
|
||||
?>
|
||||
<script id="<?php
|
||||
echo \esc_attr($this->plugin_slug);
|
||||
?>-deactivation-modal-js">
|
||||
<?php
|
||||
$plugin_slug = $this->plugin_slug;
|
||||
include_once \FcfVendor\WPDesk\DeactivationModal\Modal::MODAL_ASSETS_PATH_JS;
|
||||
?>
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Service;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Exception\SenderRequestFailedException;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Hookable;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormOptions;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormValues;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\RequestData;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Sender\Sender;
|
||||
/**
|
||||
* Sends the report about the plugin deactivation.
|
||||
*/
|
||||
class RequestSenderService implements \FcfVendor\WPDesk\DeactivationModal\Hookable
|
||||
{
|
||||
const FORM_FIELD_REASON = '_deactivation_reason';
|
||||
const FORM_FIELD_MESSAGE = '_deactivation_message_%s';
|
||||
const ADMIN_AJAX_ACTION = 'wpdesk_deactivation_request_';
|
||||
const ADMIN_AJAX_NONCE = 'wpdesk-deactivation-request';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_slug;
|
||||
/**
|
||||
* @var FormOptions
|
||||
*/
|
||||
private $form_options;
|
||||
/**
|
||||
* @var FormValues
|
||||
*/
|
||||
private $form_values;
|
||||
/**
|
||||
* @var Sender
|
||||
*/
|
||||
private $request_sender;
|
||||
public function __construct(string $plugin_slug, \FcfVendor\WPDesk\DeactivationModal\Model\FormOptions $form_options, \FcfVendor\WPDesk\DeactivationModal\Model\FormValues $form_values, \FcfVendor\WPDesk\DeactivationModal\Sender\Sender $request_sender)
|
||||
{
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
$this->form_options = $form_options;
|
||||
$this->form_values = $form_values;
|
||||
$this->request_sender = $request_sender;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hooks()
|
||||
{
|
||||
\add_action('wp_ajax_' . self::ADMIN_AJAX_ACTION . $this->plugin_slug, [$this, 'handle_ajax_request']);
|
||||
\add_action('wp_ajax_nopriv_' . self::ADMIN_AJAX_ACTION . $this->plugin_slug, [$this, 'handle_ajax_request']);
|
||||
}
|
||||
public static function generate_ajax_url(string $plugin_slug) : string
|
||||
{
|
||||
return \sprintf('admin-ajax.php?action=%1$s&nonce=%2$s', self::ADMIN_AJAX_ACTION . $plugin_slug, \wp_create_nonce(self::ADMIN_AJAX_NONCE));
|
||||
}
|
||||
/**
|
||||
* @param mixed|null $request_params .
|
||||
*
|
||||
* @throws SenderRequestFailedException
|
||||
* @internal
|
||||
*/
|
||||
public function handle_ajax_request($request_params = null)
|
||||
{
|
||||
$params = $request_params && \is_array($request_params) ? $request_params : \wp_unslash($_REQUEST);
|
||||
if (\wp_verify_nonce($params['nonce'] ?? '', self::ADMIN_AJAX_NONCE) !== \false) {
|
||||
$this->send_request($params);
|
||||
\wp_send_json_success();
|
||||
} else {
|
||||
\wp_send_json_error();
|
||||
}
|
||||
\wp_die();
|
||||
}
|
||||
/**
|
||||
* @param array $request_params .
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws SenderRequestFailedException
|
||||
*/
|
||||
private function send_request(array $request_params)
|
||||
{
|
||||
$request_data = $this->get_request_data($request_params);
|
||||
$this->request_sender->send_request($request_data);
|
||||
}
|
||||
private function get_request_data(array $request_params) : \FcfVendor\WPDesk\DeactivationModal\Model\RequestData
|
||||
{
|
||||
$request_data = (new \FcfVendor\WPDesk\DeactivationModal\Model\RequestData($this->plugin_slug))->set_reason_key($this->get_deactivation_reason($request_params));
|
||||
if ($request_data->get_reason_key() !== null) {
|
||||
$value_key = \sprintf(self::FORM_FIELD_MESSAGE, $request_data->get_reason_key());
|
||||
$request_data->set_additional_info(isset($request_params[$value_key]) ? \sanitize_textarea_field($request_params[$value_key]) : null);
|
||||
}
|
||||
foreach ($this->form_values->get_values() as $value) {
|
||||
$request_data->set_additional_data_item($value->get_key(), \call_user_func($value->get_value_callback()));
|
||||
}
|
||||
return $request_data;
|
||||
}
|
||||
/**
|
||||
* @param array $request_params .
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function get_deactivation_reason(array $request_params)
|
||||
{
|
||||
$request_reason = $request_params[self::FORM_FIELD_REASON] ?? '';
|
||||
foreach ($this->form_options->get_options() as $option) {
|
||||
if ($option->get_key() === $request_reason) {
|
||||
return $request_reason;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace FcfVendor\WPDesk\DeactivationModal\Service;
|
||||
|
||||
use FcfVendor\WPDesk\DeactivationModal\Hookable;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Modal;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormOptions;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormTemplate;
|
||||
use FcfVendor\WPDesk\DeactivationModal\Model\FormValues;
|
||||
/**
|
||||
* Prints the deactivation modal template on the plugin list page.
|
||||
*/
|
||||
class TemplateGeneratorService implements \FcfVendor\WPDesk\DeactivationModal\Hookable
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $plugin_slug;
|
||||
/**
|
||||
* @var FormTemplate
|
||||
*/
|
||||
private $form_template;
|
||||
/**
|
||||
* @var FormOptions
|
||||
*/
|
||||
private $form_options;
|
||||
/**
|
||||
* @var FormValues
|
||||
*/
|
||||
private $form_values;
|
||||
public function __construct(string $plugin_slug, \FcfVendor\WPDesk\DeactivationModal\Model\FormTemplate $form_template, \FcfVendor\WPDesk\DeactivationModal\Model\FormOptions $form_options, \FcfVendor\WPDesk\DeactivationModal\Model\FormValues $form_values)
|
||||
{
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
$this->form_template = $form_template;
|
||||
$this->form_options = $form_options;
|
||||
$this->form_values = $form_values;
|
||||
}
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hooks()
|
||||
{
|
||||
\add_action('admin_print_footer_scripts-plugins.php', [$this, 'load_template'], 0);
|
||||
}
|
||||
public function load_template()
|
||||
{
|
||||
$params = ['api_url' => \FcfVendor\WPDesk\DeactivationModal\Service\RequestSenderService::generate_ajax_url($this->plugin_slug), 'plugin_slug' => $this->plugin_slug, 'field_name_reason' => \FcfVendor\WPDesk\DeactivationModal\Service\RequestSenderService::FORM_FIELD_REASON, 'field_name_message' => \FcfVendor\WPDesk\DeactivationModal\Service\RequestSenderService::FORM_FIELD_MESSAGE, 'form_template' => $this->form_template, 'form_options' => $this->form_options, 'form_values' => $this->form_values];
|
||||
\extract($params);
|
||||
// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
|
||||
require_once \FcfVendor\WPDesk\DeactivationModal\Modal::MODAL_TEMPLATE_PATH;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user