| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2020-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( 'SucomAbstractAddOn' ) ) { |
| 14 |
|
| 15 |
abstract class SucomAbstractAddOn { |
| 16 |
|
| 17 |
protected $p; // Plugin class object. |
| 18 |
protected $ext = ''; // Add-on lowercase classname. |
| 19 |
protected $p_ext = ''; // Add-on lowercase acronym. |
| 20 |
protected $cf = array(); // Add-on config array. |
| 21 |
|
| 22 |
protected $did_plugin_notices = false; // True when $this->init_plugin_notices() has run. |
| 23 |
|
| 24 |
public function __construct() {} |
| 25 |
|
| 26 |
public function get_ext() { |
| 27 |
|
| 28 |
return $this->ext; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_p_ext() { |
| 32 |
|
| 33 |
return $this->p_ext; |
| 34 |
} |
| 35 |
|
| 36 |
public function get_config( array $config ) { |
| 37 |
|
| 38 |
if ( $this->get_missing_requirements() ) { // Returns false or an array of missing requirements. |
| 39 |
|
| 40 |
return $config; // Stop here. |
| 41 |
} |
| 42 |
|
| 43 |
return SucomUtil::array_merge_recursive_distinct( $config, $this->cf ); |
| 44 |
} |
| 45 |
|
| 46 |
public function get_avail( array $avail ) { |
| 47 |
|
| 48 |
if ( $this->get_missing_requirements() ) { // Returns false or an array of missing requirements. |
| 49 |
|
| 50 |
$avail[ 'p_ext' ][ $this->p_ext ] = false; // Signal that this extension / add-on is not available. |
| 51 |
|
| 52 |
return $avail; |
| 53 |
} |
| 54 |
|
| 55 |
$avail[ 'p_ext' ][ $this->p_ext ] = true; // Signal that this extension / add-on is available. |
| 56 |
|
| 57 |
return $avail; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
* All WPSSO Core objects are instantiated and configured. |
| 62 |
*/ |
| 63 |
public function init_plugin_notices() { |
| 64 |
|
| 65 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 66 |
|
| 67 |
$this->p->debug->mark(); |
| 68 |
} |
| 69 |
|
| 70 |
if ( empty( $this->p->notice ) ) { // Just in case. |
| 71 |
|
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$is_admin = is_admin(); |
| 76 |
$doing_ajax = SucomUtilWP::doing_ajax(); |
| 77 |
$missing_reqs = $this->get_missing_requirements(); // Returns false or an array of missing requirements. |
| 78 |
|
| 79 |
$this->did_plugin_notices = true; // Signal that $this->init_plugin_notices() has run. |
| 80 |
|
| 81 |
if ( ! $doing_ajax && $missing_reqs ) { |
| 82 |
|
| 83 |
foreach ( $missing_reqs as $key => $req_info ) { |
| 84 |
|
| 85 |
if ( ! empty( $req_info[ 'notice' ] ) ) { |
| 86 |
|
| 87 |
if ( $is_admin ) { |
| 88 |
|
| 89 |
$this->p->notice->err( $req_info[ 'notice' ] ); |
| 90 |
|
| 91 |
SucomUtil::safe_error_log( __METHOD__ . ' error: ' . $req_info[ 'notice' ], $strip_html = true ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 95 |
|
| 96 |
$this->p->debug->log( strtolower( $req_info[ 'notice' ] ) ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
public function show_admin_notices() { |
| 104 |
|
| 105 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 106 |
|
| 107 |
$this->p->debug->mark(); |
| 108 |
} |
| 109 |
|
| 110 |
if ( $this->did_plugin_notices ) { // True when $this->init_plugin_notices() has run. |
| 111 |
|
| 112 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 113 |
|
| 114 |
$this->p->debug->log( 'admin notices already done' ); |
| 115 |
} |
| 116 |
|
| 117 |
return; // Stop here. |
| 118 |
} |
| 119 |
|
| 120 |
$missing_reqs = $this->get_missing_requirements(); // Returns false or an array of missing requirements. |
| 121 |
|
| 122 |
if ( $missing_reqs ) { |
| 123 |
|
| 124 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 125 |
|
| 126 |
$this->p->debug->log( 'have missing requirements' ); |
| 127 |
} |
| 128 |
|
| 129 |
foreach ( $missing_reqs as $key => $req_info ) { |
| 130 |
|
| 131 |
if ( ! empty( $req_info[ 'notice' ] ) ) { |
| 132 |
|
| 133 |
echo wp_kses_post( '<div class="notice notice-error error"><p>' . $req_info[ 'notice' ] . '</p></div>' ); |
| 134 |
|
| 135 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 136 |
|
| 137 |
$this->p->debug->log( strtolower( $req_info[ 'notice' ] ) ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Returns false or an array of missing requirements. |
| 146 |
*/ |
| 147 |
protected function get_missing_requirements() { |
| 148 |
|
| 149 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 150 |
|
| 151 |
$this->p->debug->mark(); |
| 152 |
} |
| 153 |
|
| 154 |
static $local_cache = null; |
| 155 |
|
| 156 |
if ( null !== $local_cache ) return $local_cache; |
| 157 |
|
| 158 |
$local_cache = array(); // Also prevents recursion. |
| 159 |
|
| 160 |
$info = $this->cf[ 'plugin' ][ $this->ext ]; |
| 161 |
|
| 162 |
if ( empty( $info[ 'req' ] ) ) return $local_cache = false; |
| 163 |
|
| 164 |
$addon_name = $info[ 'name' ]; |
| 165 |
$text_domain = $info[ 'text_domain' ]; |
| 166 |
|
| 167 |
foreach ( $info[ 'req' ] as $key => $req_info ) { |
| 168 |
|
| 169 |
if ( ! empty( $req_info[ 'home' ] ) ) { |
| 170 |
|
| 171 |
$req_name = '<a href="' . $req_info[ 'home' ] . '">' . $req_info[ 'name' ] . '</a>'; |
| 172 |
|
| 173 |
} else $req_name = $req_info[ 'name' ]; |
| 174 |
|
| 175 |
/* |
| 176 |
* Optimize and check for plugin version first, then check for plugin existence. |
| 177 |
*/ |
| 178 |
if ( ! empty( $req_info[ 'version_const' ] ) && defined( $req_info[ 'version_const' ] ) ) { |
| 179 |
|
| 180 |
$req_info[ 'version' ] = constant( $req_info[ 'version_const' ] ); |
| 181 |
|
| 182 |
} elseif ( ! empty( $req_info[ 'version_global' ] ) && isset( $GLOBALS[ $req_info[ 'version_global' ] ] ) ) { |
| 183 |
|
| 184 |
$req_info[ 'version' ] = $GLOBALS[ $req_info[ 'version_global' ] ]; |
| 185 |
|
| 186 |
} elseif ( ! empty( $req_info[ 'plugin_class' ] ) && ! class_exists( $req_info[ 'plugin_class' ] ) ) { |
| 187 |
|
| 188 |
$req_info[ 'notice' ] = $this->get_requires_plugin_notice( $info, $req_info ); |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* A version value from a global variable or constant. |
| 193 |
*/ |
| 194 |
if ( ! empty( $req_info[ 'version' ] ) ) { |
| 195 |
|
| 196 |
if ( ! empty( $req_info[ 'min_version' ] ) ) { |
| 197 |
|
| 198 |
if ( version_compare( $req_info[ 'version' ], $req_info[ 'min_version' ], '<' ) ) { |
| 199 |
|
| 200 |
$req_info[ 'notice' ] = $this->get_requires_version_notice( $info, $req_info ); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* Possible notice for wordpress version, plugin version, or missing plugin. |
| 207 |
*/ |
| 208 |
if ( ! empty( $req_info[ 'notice' ] ) ) { |
| 209 |
|
| 210 |
$local_cache[ $key ] = $req_info; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if ( empty( $local_cache ) ) { |
| 215 |
|
| 216 |
$local_cache = false; |
| 217 |
} |
| 218 |
|
| 219 |
return $local_cache; |
| 220 |
} |
| 221 |
|
| 222 |
protected function get_requires_plugin_notice( array $info, array $req_info ) { |
| 223 |
|
| 224 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 225 |
|
| 226 |
$this->p->debug->mark(); |
| 227 |
} |
| 228 |
|
| 229 |
$this->init_textdomain(); // If not already loaded, load the textdomain now. |
| 230 |
|
| 231 |
$text_domain = $info[ 'text_domain' ]; |
| 232 |
$addon_name = _x( $info[ 'name' ], 'plugin name', $text_domain ); |
| 233 |
$req_name = _x( $req_info[ 'name' ], 'plugin name', $text_domain ); |
| 234 |
$req_name = empty( $req_info[ 'home' ] ) ? $req_name : '<a href="' . $req_info[ 'home' ] . '">' . $req_name . '</a>'; |
| 235 |
$notice_html = __( 'The %1$s add-on requires the %2$s plugin.', $text_domain ); |
| 236 |
$notice_html = sprintf( $notice_html, $addon_name, $req_name ); |
| 237 |
|
| 238 |
return $notice_html; |
| 239 |
} |
| 240 |
|
| 241 |
protected function get_requires_version_notice( array $info, array $req_info ) { |
| 242 |
|
| 243 |
if ( ! empty( $this->p->debug->enabled ) ) { |
| 244 |
|
| 245 |
$this->p->debug->mark(); |
| 246 |
} |
| 247 |
|
| 248 |
$this->init_textdomain(); // If not already loaded, load the textdomain now. |
| 249 |
|
| 250 |
$text_domain = $info[ 'text_domain' ]; |
| 251 |
$addon_name = _x( $info[ 'name' ], 'plugin name', $text_domain ); |
| 252 |
$req_name = _x( $req_info[ 'name' ], 'plugin name', $text_domain ); |
| 253 |
$req_name = empty( $req_info[ 'home' ] ) ? $req_name : '<a href="' . $req_info[ 'home' ] . '">' . $req_name . '</a>'; |
| 254 |
$notice_html = __( 'The %1$s add-on requires %2$s version %3$s or newer (version %4$s is currently installed).', $text_domain ); |
| 255 |
$notice_html = sprintf( $notice_html, $addon_name, $req_name, $req_info[ 'min_version' ], $req_info[ 'version' ] ); |
| 256 |
|
| 257 |
return $notice_html; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|