Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
38 / 40 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Plugin | |
95.00% |
38 / 40 |
|
80.00% |
4 / 5 |
17 | |
0.00% |
0 / 1 |
| collect | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| collect_installed | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 | |||
| get_data | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 | |||
| does_it_autoupdate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_admin_users | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Data\Helpers; |
| 4 | |
| 5 | /** |
| 6 | * Helper class for gathering and formatting plugin data |
| 7 | * |
| 8 | * @phpstan-type plugin-array array{slug:string, version:string, title:string, url:string, active:bool, mu:bool, auto_updates:bool, users?:array<array{id:int, email:string}>} |
| 9 | */ |
| 10 | class Plugin { |
| 11 | /** |
| 12 | * Prepare plugin data for a single plugin |
| 13 | * |
| 14 | * @param string $basename The plugin basename (filename relative to WP_PLUGINS_DIR). |
| 15 | * |
| 16 | * @return plugin-array Hiive relevant plugin details |
| 17 | */ |
| 18 | public function collect( $basename ): array { |
| 19 | |
| 20 | if ( ! function_exists( 'get_plugin_data' ) ) { |
| 21 | require wp_normalize_path( constant( 'ABSPATH' ) . '/wp-admin/includes/plugin.php' ); |
| 22 | } |
| 23 | |
| 24 | return $this->get_data( $basename, get_plugin_data( constant( 'WP_PLUGIN_DIR' ) . '/' . $basename ) ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Prepare plugin data for all plugins |
| 29 | * |
| 30 | * @return array<plugin-array> of plugins |
| 31 | */ |
| 32 | public function collect_installed(): array { |
| 33 | if ( ! function_exists( 'get_plugins' ) ) { |
| 34 | require wp_normalize_path( constant( 'ABSPATH' ) . '/wp-admin/includes/plugin.php' ); |
| 35 | } |
| 36 | |
| 37 | $plugins = array(); |
| 38 | |
| 39 | // Collect standard plugins |
| 40 | foreach ( get_plugins() as $slug => $data ) { |
| 41 | array_push( $plugins, $this->get_data( $slug, $data ) ); |
| 42 | } |
| 43 | |
| 44 | // Collect mu plugins |
| 45 | foreach ( get_mu_plugins() as $slug => $data ) { |
| 46 | array_push( $plugins, $this->get_data( $slug, $data, true ) ); |
| 47 | } |
| 48 | |
| 49 | return $plugins; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Grab relevant data from plugin data - and only what we want |
| 54 | * |
| 55 | * @param string $basename The plugin basename (filename relative to WP_PLUGINS_DIR). |
| 56 | * @param array $data The plugin meta-data from its header. |
| 57 | * @param bool $mu Whether the plugin is installed as a must-use plugin. |
| 58 | * |
| 59 | * @return plugin-array Hiive relevant plugin details |
| 60 | */ |
| 61 | public function get_data( string $basename, array $data, bool $mu = false ): array { |
| 62 | $plugin = array(); |
| 63 | $plugin['slug'] = $basename; |
| 64 | $plugin['version'] = isset( $data['Version'] ) ? $data['Version'] : '0.0'; |
| 65 | $plugin['title'] = isset( $data['Name'] ) ? $data['Name'] : ''; |
| 66 | $plugin['url'] = isset( $data['PluginURI'] ) ? $data['PluginURI'] : ''; |
| 67 | $plugin['active'] = is_plugin_active( $basename ); |
| 68 | $plugin['mu'] = $mu; |
| 69 | $plugin['auto_updates'] = ( ! $mu && $this->does_it_autoupdate( $basename ) ); |
| 70 | |
| 71 | if ( strpos( $basename, 'jetpack/jetpack.php' ) !== false ) { |
| 72 | $plugin['users'] = $this->get_admin_users(); |
| 73 | } |
| 74 | |
| 75 | return $plugin; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Whether the plugin is set to auto update |
| 80 | * |
| 81 | * @param string $slug Name of the plugin |
| 82 | */ |
| 83 | protected function does_it_autoupdate( string $slug ): bool { |
| 84 | // Check plugin setting for auto updates on all plugins |
| 85 | if ( 'true' === get_site_option( 'auto_update_plugin', 'true' ) ) { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // check core setting for auto updates on this plugin |
| 90 | $wp_auto_updates = (array) get_site_option( 'auto_update_plugins', array() ); |
| 91 | |
| 92 | return in_array( $slug, $wp_auto_updates, true ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get Admin user accounts |
| 97 | * |
| 98 | * @return array<array{id:int, email:string}> $users Array of Admin user. |
| 99 | */ |
| 100 | protected function get_admin_users(): array { |
| 101 | // Get all admin users |
| 102 | $admin_users = get_users( |
| 103 | array( |
| 104 | 'role' => 'administrator', |
| 105 | ) |
| 106 | ); |
| 107 | $users = array(); |
| 108 | |
| 109 | /** |
| 110 | * Add administrators to the $users array, filtering out those with an email address. |
| 111 | * |
| 112 | * @var \WP_User $user |
| 113 | */ |
| 114 | foreach ( $admin_users as $user ) { |
| 115 | if ( empty( $user->user_email ) ) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | $users[] = array( |
| 120 | 'id' => $user->ID, |
| 121 | 'email' => $user->user_email, |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | return $users; |
| 126 | } |
| 127 | } |