Account
11 months ago
Cache
11 months ago
Contracts
11 months ago
Core
11 months ago
Http
11 months ago
Notices
11 months ago
Analyst.php
11 months ago
ApiRequestor.php
11 months ago
ApiResponse.php
11 months ago
Collector.php
11 months ago
Mutator.php
11 months ago
helpers.php
11 months ago
Mutator.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Analyst; |
| 4 | |
| 5 | use Analyst\Cache\DatabaseCache; |
| 6 | use Analyst\Contracts\CacheContract; |
| 7 | use Analyst\Notices\NoticeFactory; |
| 8 | |
| 9 | /** |
| 10 | * Class Mutator mutates (modifies) UX with additional |
| 11 | * functional |
| 12 | */ |
| 13 | class Mutator |
| 14 | { |
| 15 | protected $notices = []; |
| 16 | |
| 17 | /** |
| 18 | * @var NoticeFactory |
| 19 | */ |
| 20 | protected $factory; |
| 21 | |
| 22 | /** |
| 23 | * @var CacheContract |
| 24 | */ |
| 25 | protected $cache; |
| 26 | |
| 27 | public function __construct() |
| 28 | { |
| 29 | $this->factory = NoticeFactory::instance(); |
| 30 | |
| 31 | $this->notices = $this->factory->getNotices(); |
| 32 | |
| 33 | $this->cache = DatabaseCache::getInstance(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register filters all necessary stuff. |
| 38 | * Can be invoked only once. |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public function initialize() |
| 43 | { |
| 44 | $this->registerLinks(); |
| 45 | $this->registerAssets(); |
| 46 | $this->registerHooks(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Register all necessary filters and templates |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | protected function registerLinks() |
| 55 | { |
| 56 | add_action('admin_footer', function () { |
| 57 | analyst_require_template('optout.php', [ |
| 58 | 'shieldImage' => analyst_assets_url('img/shield_question.png') |
| 59 | ]); |
| 60 | |
| 61 | analyst_require_template('optin.php'); |
| 62 | |
| 63 | analyst_require_template('forms/deactivate.php', [ |
| 64 | 'pencilImage' => analyst_assets_url('img/pencil.png'), |
| 65 | 'smileImage' => analyst_assets_url('img/smile.png'), |
| 66 | ]); |
| 67 | |
| 68 | analyst_require_template('forms/install.php', [ |
| 69 | 'pluginToInstall' => $this->cache->get('plugin_to_install'), |
| 70 | 'shieldImage' => analyst_assets_url('img/shield_success.png'), |
| 71 | ]); |
| 72 | }); |
| 73 | |
| 74 | add_action('admin_notices',function () { |
| 75 | foreach ($this->notices as $notice) { |
| 76 | analyst_require_template('notice.php', ['notice' => $notice]); |
| 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Register all assets |
| 83 | */ |
| 84 | public function registerAssets() |
| 85 | { |
| 86 | add_action('admin_enqueue_scripts', function () { |
| 87 | wp_enqueue_style('analyst_custom', analyst_assets_url('/css/customize.css')); |
| 88 | wp_enqueue_script('analyst_custom', analyst_assets_url('/js/customize.js')); |
| 89 | wp_localize_script('analyst_custom', 'analyst_opt_localize', array( |
| 90 | 'nonce' => wp_create_nonce('analyst_opt_ajax_nonce') |
| 91 | )); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Register action hooks |
| 97 | */ |
| 98 | public function registerHooks() |
| 99 | { |
| 100 | add_action('wp_ajax_analyst_notification_dismiss', function () { |
| 101 | $capabilities = [ |
| 102 | 'activate_plugins', |
| 103 | 'edit_plugins', |
| 104 | 'install_plugins', |
| 105 | 'update_plugins', |
| 106 | 'delete_plugins', |
| 107 | 'manage_network_plugins', |
| 108 | 'upload_plugins' |
| 109 | ]; |
| 110 | |
| 111 | // Allow if has any of above permissions |
| 112 | $hasPerms = false; |
| 113 | foreach ($capabilities as $i => $cap) { |
| 114 | if (current_user_can($cap)) { |
| 115 | $hasPerms = true; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if ($hasPerms == false) { |
| 121 | wp_send_json_error(['message' => 'no_permissions']); |
| 122 | die; |
| 123 | } |
| 124 | |
| 125 | if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field($_POST['nonce']), 'analyst_opt_ajax_nonce')) { |
| 126 | wp_send_json_error(['message' => 'invalid_nonce']); |
| 127 | die; |
| 128 | } |
| 129 | |
| 130 | $this->factory->remove(sanitize_text_field($_POST['id'])); |
| 131 | |
| 132 | $this->factory->sync(); |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 |