| 1 |
<?php |
| 2 |
/** |
| 3 |
* Addon class. |
| 4 |
* |
| 5 |
* @since 1.3.0 |
| 6 |
* @package QuillForms |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace QuillForms\Addon; |
| 10 |
|
| 11 |
use Exception; |
| 12 |
use QuillForms\Managers\Addons_Manager; |
| 13 |
use QuillForms\Tasks; |
| 14 |
|
| 15 |
/** |
| 16 |
* Abstract class for plugin extensions. |
| 17 |
* |
| 18 |
* @since 1.3.0 |
| 19 |
*/ |
| 20 |
abstract class Addon { |
| 21 |
|
| 22 |
/** |
| 23 |
* Name |
| 24 |
* |
| 25 |
* @since 1.3.0 |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $name; |
| 30 |
|
| 31 |
/** |
| 32 |
* Slug |
| 33 |
* |
| 34 |
* @since 1.3.0 |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $slug; |
| 39 |
|
| 40 |
/** |
| 41 |
* Version |
| 42 |
* |
| 43 |
* @since 1.3.0 |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
public $version; |
| 48 |
|
| 49 |
/** |
| 50 |
* Text domain |
| 51 |
* |
| 52 |
* @since 1.5.0 |
| 53 |
* |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public $textdomain; |
| 57 |
|
| 58 |
/** |
| 59 |
* Plugin file |
| 60 |
* |
| 61 |
* @since 1.6.0 |
| 62 |
* |
| 63 |
* @var string |
| 64 |
*/ |
| 65 |
public $plugin_file; |
| 66 |
|
| 67 |
/** |
| 68 |
* Plugin dir |
| 69 |
* |
| 70 |
* @since 1.5.0 |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
public $plugin_dir; |
| 75 |
|
| 76 |
/** |
| 77 |
* Plugin url |
| 78 |
* |
| 79 |
* @since 1.5.0 |
| 80 |
* |
| 81 |
* @var string |
| 82 |
*/ |
| 83 |
public $plugin_url; |
| 84 |
|
| 85 |
/** |
| 86 |
* Dependencies |
| 87 |
* |
| 88 |
* @since 1.7.1 |
| 89 |
* |
| 90 |
* @var array { |
| 91 |
* An associative array of dependencies. |
| 92 |
* its keys can be quillforms or {other_addon_slug}_addon |
| 93 |
* it also supports ssl, curl |
| 94 |
* |
| 95 |
* @type string $version Dependency version. |
| 96 |
* @type boolean $required For addon dependency only. |
| 97 |
* } |
| 98 |
*/ |
| 99 |
public $dependencies = array(); |
| 100 |
|
| 101 |
/** |
| 102 |
* Settings |
| 103 |
* |
| 104 |
* @since 1.3.0 |
| 105 |
* |
| 106 |
* @var Settings|null |
| 107 |
*/ |
| 108 |
public $settings; |
| 109 |
|
| 110 |
/** |
| 111 |
* Form data |
| 112 |
* |
| 113 |
* @since 1.3.0 |
| 114 |
* |
| 115 |
* @var Form_Data|null |
| 116 |
*/ |
| 117 |
public $form_data; |
| 118 |
|
| 119 |
/** |
| 120 |
* Tasks |
| 121 |
* |
| 122 |
* @var Tasks |
| 123 |
*/ |
| 124 |
public $tasks; |
| 125 |
|
| 126 |
/** |
| 127 |
* Class names |
| 128 |
* |
| 129 |
* @var array |
| 130 |
*/ |
| 131 |
protected static $classes = array( |
| 132 |
// 'scripts' => Scripts::class, |
| 133 |
// 'settings' => Settings::class, |
| 134 |
// 'form_data' => Form_Data::class, |
| 135 |
// 'rest' => REST\REST::class, |
| 136 |
); |
| 137 |
|
| 138 |
/** |
| 139 |
* Subclasses instances. |
| 140 |
* |
| 141 |
* @var array |
| 142 |
* |
| 143 |
* @since 1.3.0 |
| 144 |
*/ |
| 145 |
private static $instances = array(); |
| 146 |
|
| 147 |
/** |
| 148 |
* Addon Instances. |
| 149 |
* |
| 150 |
* Instantiates or reuses an instances of Addon. |
| 151 |
* |
| 152 |
* @since 1.3.0 |
| 153 |
* @static |
| 154 |
* |
| 155 |
* @return static - Single instance |
| 156 |
*/ |
| 157 |
public static function instance() { |
| 158 |
if ( ! isset( self::$instances[ static::class ] ) ) { |
| 159 |
self::$instances[ static::class ] = new static(); |
| 160 |
} |
| 161 |
return self::$instances[ static::class ]; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Constructor. |
| 166 |
* |
| 167 |
* @since 1.3.0 |
| 168 |
*/ |
| 169 |
private function __construct() { |
| 170 |
if ( $this->register() ) { |
| 171 |
$this->init(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Initialize |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
protected function init() { |
| 181 |
add_action('init', function() { |
| 182 |
$this->load_textdomain(); |
| 183 |
}); |
| 184 |
|
| 185 |
if ( ! empty( static::$classes['scripts'] ) ) { |
| 186 |
new static::$classes['scripts']( $this ); |
| 187 |
} |
| 188 |
if ( ! empty( static::$classes['settings'] ) ) { |
| 189 |
$this->settings = new static::$classes['settings']( $this ); |
| 190 |
} |
| 191 |
if ( ! empty( static::$classes['form_data'] ) ) { |
| 192 |
$this->form_data = new static::$classes['form_data']( $this ); |
| 193 |
} |
| 194 |
if ( ! empty( static::$classes['rest'] ) ) { |
| 195 |
new static::$classes['rest']( $this ); |
| 196 |
} |
| 197 |
$this->tasks = new Tasks( "quillforms_{$this->slug}" ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Register |
| 202 |
* |
| 203 |
* @return boolean |
| 204 |
*/ |
| 205 |
private function register() { |
| 206 |
try { |
| 207 |
Addons_Manager::instance()->register( $this ); |
| 208 |
} catch ( Exception $e ) { |
| 209 |
add_action( |
| 210 |
'admin_notices', |
| 211 |
function() use ( $e ) { |
| 212 |
$this->output_registration_error( $e->getMessage() ); |
| 213 |
} |
| 214 |
); |
| 215 |
$deactivation_codes = array(); |
| 216 |
if ( in_array( $e->getCode(), $deactivation_codes, true ) ) { |
| 217 |
deactivate_plugins( $this->plugin_file ); |
| 218 |
quillforms_get_logger()->error( |
| 219 |
esc_html__( 'Addon deactivated due to incompatible dependencies', 'quillforms' ), |
| 220 |
array( |
| 221 |
'source' => static::class . '->' . __FUNCTION__, |
| 222 |
'code' => 'addon_deactivated_due_to_incompatible_dependencies', |
| 223 |
'dependencies' => $this->dependencies, |
| 224 |
) |
| 225 |
); |
| 226 |
} |
| 227 |
return false; |
| 228 |
} |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Output registration error |
| 234 |
* |
| 235 |
* @since 1.6.0 |
| 236 |
* |
| 237 |
* @param string $message Message. |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
protected function output_registration_error( $message ) { |
| 241 |
?> |
| 242 |
<div class="notice notice-error"> |
| 243 |
<p><?php echo esc_html__( 'Cannot register a Quill Forms addon', 'quillforms' ) . ': ' . esc_html( $message ); ?></p> |
| 244 |
</div> |
| 245 |
<?php |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Load plugin text domain |
| 250 |
* |
| 251 |
* Note: For plugins hosted on WordPress.org, translations are loaded automatically |
| 252 |
* since WordPress 4.6. This function is kept for backwards compatibility and |
| 253 |
* for self-hosted installations. |
| 254 |
* |
| 255 |
* @return void |
| 256 |
*/ |
| 257 |
protected function load_textdomain() { |
| 258 |
$plugin_rel_path = substr( $this->plugin_dir, strlen( WP_PLUGIN_DIR ) ) . 'languages'; |
| 259 |
// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- Kept for backwards compatibility with self-hosted installations. |
| 260 |
load_plugin_textdomain( $this->textdomain, false, $plugin_rel_path ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Get main namespace |
| 265 |
* |
| 266 |
* @return string |
| 267 |
*/ |
| 268 |
public function get_namespace() { |
| 269 |
return explode( '\\', static::class )[0]; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|