| @@ -1,0 +1,239 @@ | ||
| 1 | +<?php | |
| 2 | +/* | |
| 3 | + * License: GPLv3 | |
| 4 | + * License URI: https://www.gnu.org/licenses/gpl.txt | |
| 5 | + * Copyright 2012-2026 Jean-Sebastien Morisset (https://surniaulula.com/) | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + | |
| 10 | + die( 'These aren\'t the droids you\'re looking for.' ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if ( ! class_exists( 'SucomPlugin' ) ) { | |
| 14 | + | |
| 15 | + class SucomPlugin { | |
| 16 | + | |
| 17 | + public function __construct() {} | |
| 18 | + | |
| 19 | + /* | |
| 20 | + * GET PLUGINS METHODS: | |
| 21 | + * | |
| 22 | + * get_active_plugins() | |
| 23 | + * get_plugins() | |
| 24 | + * | |
| 25 | + * Returns an associative array of true/false values. | |
| 26 | + */ | |
| 27 | + public static function get_active_plugins( $read_cache = true ) { | |
| 28 | + | |
| 29 | + static $local_cache = null; | |
| 30 | + | |
| 31 | + if ( $read_cache ) { | |
| 32 | + | |
| 33 | + if ( null !== $local_cache ) { | |
| 34 | + | |
| 35 | + return $local_cache; | |
| 36 | + } | |
| 37 | + } | |
| 38 | + | |
| 39 | + $local_cache = array(); // Initialize the cache. | |
| 40 | + | |
| 41 | + $active_plugins = get_option( 'active_plugins', array() ); | |
| 42 | + | |
| 43 | + if ( is_multisite() ) { | |
| 44 | + | |
| 45 | + $active_network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) ); | |
| 46 | + | |
| 47 | + if ( ! empty( $active_network_plugins ) ) { | |
| 48 | + | |
| 49 | + $active_plugins = array_merge( $active_plugins, $active_network_plugins ); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + foreach ( $active_plugins as $plugin_base ) { | |
| 54 | + | |
| 55 | + $local_cache[ $plugin_base ] = true; | |
| 56 | + } | |
| 57 | + | |
| 58 | + return $local_cache; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /* | |
| 62 | + * The WordPress get_plugins() function is very slow, so call it only once and cache its result. | |
| 63 | + * | |
| 64 | + * Used by self::is_plugin_installed(). | |
| 65 | + */ | |
| 66 | + public static function get_plugins( $read_cache = true ) { | |
| 67 | + | |
| 68 | + static $local_cache = null; | |
| 69 | + | |
| 70 | + if ( $read_cache ) { | |
| 71 | + | |
| 72 | + if ( null !== $local_cache ) { | |
| 73 | + | |
| 74 | + return $local_cache; | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + $local_cache = array(); // Initialize the cache. | |
| 79 | + | |
| 80 | + if ( ! function_exists( 'get_plugins' ) ) { // Load the WordPress library if necessary. | |
| 81 | + | |
| 82 | + $plugin_lib = trailingslashit( ABSPATH ) . 'wp-admin/includes/plugin.php'; | |
| 83 | + | |
| 84 | + if ( file_exists( $plugin_lib ) ) { // Just in case. | |
| 85 | + | |
| 86 | + require_once $plugin_lib; | |
| 87 | + | |
| 88 | + } elseif ( method_exists( 'SucomUtil', 'safe_error_log' ) ) { // Just in case. | |
| 89 | + | |
| 90 | + $error_pre = sprintf( '%s error:', __METHOD__ ); | |
| 91 | + $error_msg = sprintf( 'The WordPress %s library file is missing and required.', $plugin_lib ); | |
| 92 | + | |
| 93 | + SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg ); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( function_exists( 'get_plugins' ) ) { | |
| 98 | + | |
| 99 | + $local_cache = get_plugins(); | |
| 100 | + | |
| 101 | + } elseif ( method_exists( 'SucomUtil', 'safe_error_log' ) ) { // Just in case. | |
| 102 | + | |
| 103 | + $error_pre = sprintf( '%s error:', __METHOD__ ); | |
| 104 | + $error_msg = sprintf( 'The WordPress %1$s function is missing and required.', '<code>get_plugins()</code>' ); | |
| 105 | + | |
| 106 | + SucomUtil::safe_error_log( $error_pre . ' ' . $error_msg ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + return $local_cache; | |
| 110 | + } | |
| 111 | + | |
| 112 | + /* | |
| 113 | + * IS CHECK METHODS: | |
| 114 | + * | |
| 115 | + * is_plugin_active() | |
| 116 | + * is_plugin_installed() | |
| 117 | + * | |
| 118 | + * Returns true/false. | |
| 119 | + */ | |
| 120 | + public static function is_plugin_active( $plugin_base, $read_cache = true ) { | |
| 121 | + | |
| 122 | + $active_plugins = self::get_active_plugins( $read_cache ); | |
| 123 | + | |
| 124 | + if ( isset( $active_plugins[ $plugin_base ] ) ) { // Associative array of true/false values. | |
| 125 | + | |
| 126 | + return $active_plugins[ $plugin_base ]; // Return true/false. | |
| 127 | + } | |
| 128 | + | |
| 129 | + return false; | |
| 130 | + } | |
| 131 | + | |
| 132 | + /* | |
| 133 | + * Returns true/false. | |
| 134 | + */ | |
| 135 | + public static function is_plugin_installed( $plugin_base, $read_cache = true ) { | |
| 136 | + | |
| 137 | + static $local_cache = array(); // Associative array of true/false values. | |
| 138 | + | |
| 139 | + if ( $read_cache ) { | |
| 140 | + | |
| 141 | + if ( isset( $local_cache[ $plugin_base ] ) ) { | |
| 142 | + | |
| 143 | + return $local_cache[ $plugin_base ]; | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + if ( empty( $plugin_base ) ) { // Just in case. | |
| 148 | + | |
| 149 | + return $local_cache[ $plugin_base ] = false; | |
| 150 | + | |
| 151 | + } elseif ( validate_file( $plugin_base ) > 0 ) { // Contains invalid characters. | |
| 152 | + | |
| 153 | + return $local_cache[ $plugin_base ] = false; | |
| 154 | + } | |
| 155 | + | |
| 156 | + $wp_plugins = self::get_plugins( $read_cache ); // Front-end safe and uses cache. | |
| 157 | + | |
| 158 | + if ( ! empty( $wp_plugins[ $plugin_base ] ) ) { // Check for a valid plugin header. | |
| 159 | + | |
| 160 | + return $local_cache[ $plugin_base ] = true; | |
| 161 | + } | |
| 162 | + | |
| 163 | + return $local_cache[ $plugin_base ] = false; | |
| 164 | + } | |
| 165 | + | |
| 166 | + /* | |
| 167 | + * PLUGIN UPDATE METHODS: | |
| 168 | + * | |
| 169 | + * get_update_count() | |
| 170 | + * have_plugin_update() | |
| 171 | + * | |
| 172 | + * Get the 'update_plugins' site transient and return the number of pending updates for a given slug prefix. | |
| 173 | + * | |
| 174 | + * Example: $plugin_prefix = 'wpsso' | |
| 175 | + */ | |
| 176 | + public static function get_update_count( $plugin_prefix = '' ) { | |
| 177 | + | |
| 178 | + $updates_count = 0; | |
| 179 | + $update_plugins = get_site_transient( 'update_plugins' ); | |
| 180 | + | |
| 181 | + if ( ! empty( $update_plugins->response ) ) { | |
| 182 | + | |
| 183 | + foreach ( $update_plugins->response as $plugin_base => $data ) { | |
| 184 | + | |
| 185 | + if ( ! empty( $plugin_prefix ) ) { | |
| 186 | + | |
| 187 | + /* | |
| 188 | + * Example: | |
| 189 | + * | |
| 190 | + * $plugin_base = wpsso/wpsso.php | |
| 191 | + * | |
| 192 | + * $data->slug = wpsso | |
| 193 | + */ | |
| 194 | + if ( isset( $data->slug ) && strpos( $data->slug, $plugin_prefix ) === 0 ) { | |
| 195 | + | |
| 196 | + $updates_count++; | |
| 197 | + } | |
| 198 | + | |
| 199 | + } else $updates_count++; | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + return $updates_count; | |
| 204 | + } | |
| 205 | + | |
| 206 | + /* | |
| 207 | + * Returns true/false. | |
| 208 | + */ | |
| 209 | + public static function have_plugin_update( $plugin_base ) { | |
| 210 | + | |
| 211 | + static $local_cache = array(); // Associative array of true/false values. | |
| 212 | + | |
| 213 | + if ( isset( $local_cache[ $plugin_base ] ) ) { | |
| 214 | + | |
| 215 | + return $local_cache[ $plugin_base ]; | |
| 216 | + | |
| 217 | + } elseif ( empty( $plugin_base ) ) { // Just in case. | |
| 218 | + | |
| 219 | + return $local_cache[ $plugin_base ] = false; | |
| 220 | + | |
| 221 | + } elseif ( ! self::is_plugin_installed( $plugin_base ) ) { | |
| 222 | + | |
| 223 | + return $local_cache[ $plugin_base ] = false; | |
| 224 | + } | |
| 225 | + | |
| 226 | + $update_plugins = get_site_transient( 'update_plugins' ); | |
| 227 | + | |
| 228 | + if ( isset( $update_plugins->response ) && is_array( $update_plugins->response ) ) { | |
| 229 | + | |
| 230 | + if ( isset( $update_plugins->response[ $plugin_base ] ) ) { | |
| 231 | + | |
| 232 | + return $local_cache[ $plugin_base ] = true; | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + return $local_cache[ $plugin_base ] = false; | |
| 237 | + } | |
| 238 | + } | |
| 239 | +} | |