| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin statistics handling |
| 4 |
* |
| 5 |
* Handles all user plugin statistics and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace POSessions\System; |
| 13 |
|
| 14 |
class Plugin { |
| 15 |
|
| 16 |
/** |
| 17 |
* The details of the plugin. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @var array $details The details of the plugin. |
| 21 |
*/ |
| 22 |
private $details = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* The full slug of the plugin. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* @var string $slug The full slug of the plugin. |
| 29 |
*/ |
| 30 |
private $slug = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initializes the class and set its properties. |
| 34 |
* |
| 35 |
* @param string $slug The slug of the plugin. |
| 36 |
* @param boolean $skip_detection Optional. Skip header parsing. |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
public function __construct( $slug, $skip_detection = true ) { |
| 40 |
$this->slug = $slug . '/' . $slug . '.php'; |
| 41 |
$plugin = WP_PLUGIN_DIR . '/' . $this->slug; |
| 42 |
if ( $skip_detection ) { |
| 43 |
$this->details = [ 'detection' => 'skipped']; |
| 44 |
} else { |
| 45 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 46 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 47 |
} |
| 48 |
if ( file_exists( $plugin ) ) { |
| 49 |
$this->details = \get_plugin_data( $plugin, false ); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get a value. |
| 56 |
* |
| 57 |
* @param string $key The value to retrieve. |
| 58 |
* @return string The value. |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public function get( $key ) { |
| 62 |
if ( is_array( $this->details ) && array_key_exists( $key, $this->details ) ) { |
| 63 |
return $this->details[ $key ]; |
| 64 |
} else { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Verify if the plugin was correctly detected. |
| 71 |
* |
| 72 |
* @return boolean True if the plugin was detected, false otherwise. |
| 73 |
* @since 1.0.0 |
| 74 |
*/ |
| 75 |
public function is_detected() { |
| 76 |
return [] !== $this->details; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Verify the waiting update. |
| 81 |
* |
| 82 |
* @return boolean|string The new version if plugin needs update, false otherwise. |
| 83 |
* @since 1.0.0 |
| 84 |
*/ |
| 85 |
public function waiting_update() { |
| 86 |
if ( ! $this->is_detected() ) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
$update_cache = get_site_transient( 'update_plugins' ); |
| 90 |
$update_cache = is_object( $update_cache ) ? $update_cache : new \stdClass(); |
| 91 |
if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->slug ] ) ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
return $update_cache->response[ $this->slug ]->new_version; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Verify the auto-update status. |
| 99 |
* |
| 100 |
* @return boolean True if plugin is auto-updatable, false otherwise. |
| 101 |
* @since 2.0.0 |
| 102 |
*/ |
| 103 |
public function auto_update() { |
| 104 |
if ( ! $this->is_detected() ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
return in_array( $this->slug, (array) get_site_option( 'auto_update_plugins', [] ), true ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set the auto-update status. |
| 112 |
* |
| 113 |
* @param boolean $status Optional. The status to set. |
| 114 |
* @return boolean True if it is successful, false otherwise. |
| 115 |
* @since 2.0.0 |
| 116 |
*/ |
| 117 |
public function set_auto_update( $status = true ) { |
| 118 |
if ( ! $this->is_detected() ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
$auto_updates = (array) get_site_option( 'auto_update_plugins', [] ); |
| 122 |
if ( $status ) { |
| 123 |
$auto_updates[] = $this->slug; |
| 124 |
$auto_updates = array_unique( $auto_updates ); |
| 125 |
} else { |
| 126 |
$auto_updates = array_diff( $auto_updates, [ $this->slug ] ); |
| 127 |
} |
| 128 |
$all_items = apply_filters( 'all_plugins', get_plugins() ); |
| 129 |
$auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) ); |
| 130 |
return update_site_option( 'auto_update_plugins', $auto_updates ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Switch the auto-update status. |
| 135 |
* |
| 136 |
* @return boolean True if it is successful, false otherwise. |
| 137 |
* @since 2.0.0 |
| 138 |
*/ |
| 139 |
public function switch_auto_update() { |
| 140 |
return $this->set_auto_update( ! $this->auto_update() ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Verify if the WP version is ok. |
| 145 |
* |
| 146 |
* @return boolean True if the WP version is ok, false otherwise. |
| 147 |
* @since 1.0.0 |
| 148 |
*/ |
| 149 |
public function is_required_wp_ok() { |
| 150 |
global $wp_version; |
| 151 |
return ( ! version_compare( $wp_version, $this->get( 'RequiresWP' ), '<' ) ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Verify if the PHP version is ok. |
| 156 |
* |
| 157 |
* @return boolean True if the PHP version is ok, false otherwise. |
| 158 |
* @since 1.0.0 |
| 159 |
*/ |
| 160 |
public function is_required_php_ok() { |
| 161 |
return ( ! version_compare( PHP_VERSION, $this->get( 'RequiresPHP' ), '<' ) ); |
| 162 |
} |
| 163 |
} |
| 164 |
|