Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
30.91% |
17 / 55 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Plugin | |
30.91% |
17 / 55 |
|
0.00% |
0 / 9 |
151.92 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| register_hooks | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| activated | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| deactivated | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| save_deleted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| deleted | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| installed_or_updated | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
6.29 | |||
| updated | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
5.03 | |||
| installed | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Data\Listeners; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\EventManager; |
| 6 | use NewfoldLabs\WP\Module\Data\Helpers\Transient; |
| 7 | use NewfoldLabs\WP\Module\Data\Helpers\Plugin as PluginHelper; |
| 8 | |
| 9 | /** |
| 10 | * Monitors generic plugin events |
| 11 | */ |
| 12 | class Plugin extends Listener { |
| 13 | |
| 14 | /** |
| 15 | * Functions for gathering plugin data |
| 16 | * |
| 17 | * @var PluginHelper |
| 18 | */ |
| 19 | protected $plugin_helper; |
| 20 | |
| 21 | /** |
| 22 | * Constructor |
| 23 | * |
| 24 | * @param EventManager $manager Event manager instance |
| 25 | * @param ?PluginHelper $plugin_helper Class used to fetch plugin data. |
| 26 | */ |
| 27 | public function __construct( |
| 28 | EventManager $manager, |
| 29 | ?PluginHelper $plugin_helper = null |
| 30 | ) { |
| 31 | parent::__construct( $manager ); |
| 32 | $this->plugin_helper = $plugin_helper ?? new PluginHelper(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Register the hooks for the subscriber |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function register_hooks() { |
| 41 | // Plugin activated/deactivated |
| 42 | add_action( 'activated_plugin', array( $this, 'activated' ), 10, 2 ); |
| 43 | add_action( 'deactivated_plugin', array( $this, 'deactivated' ), 10, 2 ); |
| 44 | add_action( 'delete_plugin', array( $this, 'save_deleted' ), 10, 2 ); |
| 45 | add_action( 'deleted_plugin', array( $this, 'deleted' ), 10, 2 ); |
| 46 | add_action( 'upgrader_process_complete', array( $this, 'installed_or_updated' ), 10, 2 ); |
| 47 | |
| 48 | // transient found - nfd plugin was just activated, send that event |
| 49 | if ( Transient::get( 'nfd_plugin_activated' ) ) { |
| 50 | $this->activated( |
| 51 | Transient::get( 'nfd_plugin_activated' ), |
| 52 | false |
| 53 | ); |
| 54 | Transient::delete( 'nfd_plugin_activated' ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Plugin activated |
| 60 | * |
| 61 | * @param string $plugin Name of the plugin |
| 62 | * @param boolean $network_wide Whether plugin was network activated |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function activated( $plugin, $network_wide ) { |
| 67 | $data = array( |
| 68 | 'plugin' => $this->plugin_helper->collect( $plugin ), |
| 69 | 'network_wide' => $network_wide, |
| 70 | ); |
| 71 | $this->push( 'plugin_activated', $data ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Plugin deactivated |
| 76 | * |
| 77 | * @param string $plugin Name of the plugin |
| 78 | * @param boolean $network_wide Whether plugin was network deactivated |
| 79 | * |
| 80 | * @return void |
| 81 | */ |
| 82 | public function deactivated( $plugin, $network_wide ) { |
| 83 | $data = array( |
| 84 | 'plugin' => $this->plugin_helper->collect( $plugin ), |
| 85 | 'network_wide' => $network_wide, |
| 86 | ); |
| 87 | |
| 88 | // set the active state to false since it is collected while still active |
| 89 | $data['plugin']['active'] = false; |
| 90 | |
| 91 | $this->push( 'plugin_deactivated', $data ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Temporarily store data about the plugin about to be deleted |
| 96 | * |
| 97 | * @param string $plugin Name of the plugin |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public function save_deleted( $plugin ) { |
| 102 | update_option( 'deleted_plugin', $this->plugin_helper->collect( $plugin ) ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Plugin deleted |
| 107 | * |
| 108 | * @param string $plugin Name of the plugin |
| 109 | * @param boolean $deleted Whether the plugin deletion was successful |
| 110 | * |
| 111 | * @return void |
| 112 | */ |
| 113 | public function deleted( $plugin, $deleted ) { |
| 114 | // Only send if it was successfully deleted |
| 115 | if ( $deleted ) { |
| 116 | $data = array( |
| 117 | 'plugin' => get_option( 'deleted_plugin' ), |
| 118 | ); |
| 119 | $this->push( 'plugin_deleted', $data ); |
| 120 | } |
| 121 | // We need to clean up the saved data either way |
| 122 | delete_option( 'plugin_deleted' ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Plugin install or update completed |
| 127 | * |
| 128 | * @param \WP_Upgrader $wp_upgrader Upgrader object from upgrade hook. |
| 129 | * @param array{type:string, action:string, plugins?:array<string>, plugin?:string} $options Options from upgrade hook including type, action & plugins. |
| 130 | * |
| 131 | * @hooked upgrader_process_complete |
| 132 | * @see \Plugin_Upgrader::bulk_upgrade() |
| 133 | */ |
| 134 | public function installed_or_updated( $wp_upgrader, array $options ): void { |
| 135 | // Bail if not a plugin install or update. |
| 136 | if ( 'plugin' !== $options['type'] |
| 137 | // Or if the plugins array is set but empty. |
| 138 | || ( ! empty( $options['plugins'] ) && empty( $options['plugins'][0] ) ) ) { |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | switch ( $options['action'] ) { |
| 143 | case 'install': |
| 144 | $this->installed(); |
| 145 | break; |
| 146 | case 'update': |
| 147 | $this->updated( $options ); |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * One or more plugins were updated |
| 154 | * |
| 155 | * @param array{type:string, action:string, plugins?:array<string>, plugin?:string} $options List of update details |
| 156 | */ |
| 157 | protected function updated( array $options ): void { |
| 158 | $plugins = array(); |
| 159 | |
| 160 | // Manual updates always return array of plugin slugs |
| 161 | if ( isset( $options['plugins'] ) && is_array( $options['plugins'] ) ) { |
| 162 | foreach ( $options['plugins'] as $slug ) { |
| 163 | array_push( $plugins, $this->plugin_helper->collect( $slug ) ); |
| 164 | } |
| 165 | } |
| 166 | // Auto updates always return a single plugin slug |
| 167 | if ( isset( $options['plugin'] ) ) { |
| 168 | array_push( $plugins, $this->plugin_helper->collect( $options['plugin'] ) ); |
| 169 | } |
| 170 | |
| 171 | $data = array( |
| 172 | 'plugins' => $plugins, |
| 173 | ); |
| 174 | |
| 175 | $this->push( 'plugin_updated', $data ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Plugin Installed |
| 180 | * |
| 181 | * @return void |
| 182 | */ |
| 183 | public function installed() { |
| 184 | $data = array( |
| 185 | 'plugins' => $this->plugin_helper->collect_installed(), |
| 186 | ); |
| 187 | $this->push( 'plugin_installed', $data ); |
| 188 | } |
| 189 | } |