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