Access
3 years ago
Admin
3 years ago
Emails
3 years ago
Forms
3 years ago
Frontend
3 years ago
Helpers
3 years ago
Integrations
3 years ago
Lite
3 years ago
Logger
3 years ago
Migrations
3 years ago
Providers
3 years ago
SmartTags
3 years ago
Tasks
3 years ago
Loader.php
3 years ago
WPForms.php
3 years ago
WPForms.php
416 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPForms { |
| 4 | |
| 5 | use AllowDynamicProperties; |
| 6 | use stdClass; |
| 7 | |
| 8 | /** |
| 9 | * Main WPForms class. |
| 10 | * |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | #[AllowDynamicProperties] |
| 14 | final class WPForms { |
| 15 | |
| 16 | /** |
| 17 | * One is the loneliest number that you'll ever do. |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | * |
| 21 | * @var \WPForms\WPForms |
| 22 | */ |
| 23 | private static $instance; |
| 24 | |
| 25 | /** |
| 26 | * Plugin version for enqueueing, etc. |
| 27 | * The value is got from WPFORMS_VERSION constant. |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | public $version = ''; |
| 34 | |
| 35 | /** |
| 36 | * Classes registry. |
| 37 | * |
| 38 | * @since 1.5.7 |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | private $registry = []; |
| 43 | |
| 44 | /** |
| 45 | * List of legacy public properties. |
| 46 | * |
| 47 | * @since 1.6.8 |
| 48 | * |
| 49 | * @var string[] |
| 50 | */ |
| 51 | private $legacy_properties = [ |
| 52 | 'form', |
| 53 | 'entry', |
| 54 | 'entry_fields', |
| 55 | 'entry_meta', |
| 56 | 'frontend', |
| 57 | 'process', |
| 58 | 'smart_tags', |
| 59 | 'license', |
| 60 | ]; |
| 61 | |
| 62 | /** |
| 63 | * Paid returns true, free (Lite) returns false. |
| 64 | * |
| 65 | * @since 1.3.9 |
| 66 | * @since 1.7.3 changed to private. |
| 67 | * |
| 68 | * @var bool |
| 69 | */ |
| 70 | private $pro = false; |
| 71 | |
| 72 | /** |
| 73 | * Backward compatibility method for accessing the class registry in an old way, |
| 74 | * e.g. 'wpforms()->form' or 'wpforms()->entry'. |
| 75 | * |
| 76 | * @since 1.5.7 |
| 77 | * |
| 78 | * @param string $name Name of the object to get. |
| 79 | * |
| 80 | * @return mixed|null |
| 81 | */ |
| 82 | public function __get( $name ) { |
| 83 | |
| 84 | if ( $name === 'smart_tags' ) { |
| 85 | _deprecated_argument( |
| 86 | 'wpforms()->smart_tags', |
| 87 | '1.6.7 of the WPForms plugin', |
| 88 | "Please use `wpforms()->get( 'smart_tags' )` instead." |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | if ( $name === 'pro' ) { |
| 93 | return wpforms()->is_pro(); |
| 94 | } |
| 95 | |
| 96 | return $this->get( $name ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Main WPForms Instance. |
| 101 | * |
| 102 | * Only one instance of WPForms exists in memory at any one time. |
| 103 | * Also prevent the need to define globals all over the place. |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | * |
| 107 | * @return WPForms |
| 108 | */ |
| 109 | public static function instance() { |
| 110 | |
| 111 | if ( |
| 112 | self::$instance === null || |
| 113 | ! self::$instance instanceof self |
| 114 | ) { |
| 115 | |
| 116 | self::$instance = new self(); |
| 117 | |
| 118 | self::$instance->constants(); |
| 119 | self::$instance->includes(); |
| 120 | |
| 121 | // Load Pro or Lite specific files. |
| 122 | if ( self::$instance->is_pro() ) { |
| 123 | self::$instance->registry['pro'] = require_once WPFORMS_PLUGIN_DIR . 'pro/wpforms-pro.php'; |
| 124 | } else { |
| 125 | require_once WPFORMS_PLUGIN_DIR . 'lite/wpforms-lite.php'; |
| 126 | } |
| 127 | |
| 128 | add_action( 'plugins_loaded', [ self::$instance, 'objects' ], 10 ); |
| 129 | } |
| 130 | |
| 131 | return self::$instance; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Setup plugin constants. |
| 136 | * All the path/URL related constants are defined in main plugin file. |
| 137 | * |
| 138 | * @since 1.0.0 |
| 139 | */ |
| 140 | private function constants() { |
| 141 | |
| 142 | $this->version = WPFORMS_VERSION; |
| 143 | |
| 144 | // Plugin Slug - Determine plugin type and set slug accordingly. |
| 145 | // This filter is documented in \WPForms\WPForms::is_pro. |
| 146 | if ( apply_filters( 'wpforms_allow_pro_version', file_exists( WPFORMS_PLUGIN_DIR . 'pro/wpforms-pro.php' ) ) ) { |
| 147 | $this->pro = true; |
| 148 | |
| 149 | define( 'WPFORMS_PLUGIN_SLUG', 'wpforms' ); |
| 150 | } else { |
| 151 | define( 'WPFORMS_PLUGIN_SLUG', 'wpforms-lite' ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Include files. |
| 157 | * |
| 158 | * @since 1.0.0 |
| 159 | */ |
| 160 | private function includes() { |
| 161 | |
| 162 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-db.php'; |
| 163 | require_once WPFORMS_PLUGIN_DIR . 'includes/functions.php'; |
| 164 | require_once WPFORMS_PLUGIN_DIR . 'includes/compat.php'; |
| 165 | |
| 166 | $this->includes_magic(); |
| 167 | |
| 168 | // Global includes. |
| 169 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-install.php'; |
| 170 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-form.php'; |
| 171 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-fields.php'; |
| 172 | // TODO: class-templates.php should be loaded in admin area only. |
| 173 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-templates.php'; |
| 174 | // TODO: class-providers.php should be loaded in admin area only. |
| 175 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-providers.php'; |
| 176 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-process.php'; |
| 177 | require_once WPFORMS_PLUGIN_DIR . 'includes/class-widget.php'; |
| 178 | require_once WPFORMS_PLUGIN_DIR . 'includes/emails/class-emails.php'; |
| 179 | require_once WPFORMS_PLUGIN_DIR . 'includes/integrations.php'; |
| 180 | require_once WPFORMS_PLUGIN_DIR . 'includes/deprecated.php'; |
| 181 | |
| 182 | // Admin/Dashboard only includes, also in ajax. |
| 183 | if ( is_admin() ) { |
| 184 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/admin.php'; |
| 185 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-notices.php'; |
| 186 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-menu.php'; |
| 187 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/overview/class-overview.php'; |
| 188 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/builder/class-builder.php'; |
| 189 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/builder/functions.php'; |
| 190 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-settings.php'; |
| 191 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-welcome.php'; |
| 192 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-editor.php'; |
| 193 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-review.php'; |
| 194 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/class-about.php'; |
| 195 | require_once WPFORMS_PLUGIN_DIR . 'includes/admin/ajax-actions.php'; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Including the new files with PHP 5.3 style. |
| 201 | * |
| 202 | * @since 1.4.7 |
| 203 | */ |
| 204 | private function includes_magic() { |
| 205 | |
| 206 | // Action Scheduler requires a special loading procedure. |
| 207 | require_once WPFORMS_PLUGIN_DIR . 'vendor/woocommerce/action-scheduler/action-scheduler.php'; |
| 208 | |
| 209 | // Autoload Composer packages. |
| 210 | require_once WPFORMS_PLUGIN_DIR . 'vendor/autoload.php'; |
| 211 | |
| 212 | // Load the class loader. |
| 213 | $this->register( |
| 214 | [ |
| 215 | 'name' => 'Loader', |
| 216 | 'hook' => false, |
| 217 | ] |
| 218 | ); |
| 219 | |
| 220 | /* |
| 221 | * Load email subsystem. |
| 222 | */ |
| 223 | add_action( 'wpforms_loaded', [ '\WPForms\Emails\Summaries', 'get_instance' ] ); |
| 224 | |
| 225 | /* |
| 226 | * Load admin components. Exclude from frontend. |
| 227 | */ |
| 228 | if ( is_admin() ) { |
| 229 | add_action( 'wpforms_loaded', [ '\WPForms\Admin\Loader', 'get_instance' ] ); |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Properly init the providers loader, that will handle all the related logic and further loading. |
| 234 | */ |
| 235 | add_action( 'wpforms_loaded', [ '\WPForms\Providers\Providers', 'get_instance' ] ); |
| 236 | |
| 237 | /* |
| 238 | * Properly init the integrations loader, that will handle all the related logic and further loading. |
| 239 | */ |
| 240 | add_action( 'wpforms_loaded', [ '\WPForms\Integrations\Loader', 'get_instance' ] ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Setup objects. |
| 245 | * |
| 246 | * @since 1.0.0 |
| 247 | */ |
| 248 | public function objects() { |
| 249 | |
| 250 | // Global objects. |
| 251 | $this->form = new \WPForms_Form_Handler(); |
| 252 | $this->process = new \WPForms_Process(); |
| 253 | |
| 254 | /** |
| 255 | * Executes when all the WPForms stuff was loaded. |
| 256 | * |
| 257 | * @since 1.4.0 |
| 258 | */ |
| 259 | do_action( 'wpforms_loaded' ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Register a class. |
| 264 | * |
| 265 | * @since 1.5.7 |
| 266 | * |
| 267 | * @param array $class Class registration info. |
| 268 | */ |
| 269 | public function register( $class ) { |
| 270 | |
| 271 | if ( empty( $class['name'] ) || ! is_string( $class['name'] ) ) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | if ( isset( $class['condition'] ) && empty( $class['condition'] ) ) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | $full_name = $this->is_pro() ? '\WPForms\Pro\\' . $class['name'] : '\WPForms\Lite\\' . $class['name']; |
| 280 | $full_name = class_exists( $full_name ) ? $full_name : '\WPForms\\' . $class['name']; |
| 281 | |
| 282 | if ( ! class_exists( $full_name ) ) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | $id = isset( $class['id'] ) ? $class['id'] : ''; |
| 287 | $id = $id ? preg_replace( '/[^a-z_]/', '', (string) $id ) : $id; |
| 288 | $hook = isset( $class['hook'] ) ? (string) $class['hook'] : 'wpforms_loaded'; |
| 289 | $run = isset( $class['run'] ) ? $class['run'] : 'init'; |
| 290 | $priority = isset( $class['priority'] ) && is_int( $class['priority'] ) ? $class['priority'] : 10; |
| 291 | |
| 292 | $callback = function () use ( $full_name, $id, $run ) { |
| 293 | |
| 294 | $instance = new $full_name(); |
| 295 | |
| 296 | if ( $id && ! array_key_exists( $id, $this->registry ) ) { |
| 297 | $this->registry[ $id ] = $instance; |
| 298 | } |
| 299 | if ( $run && method_exists( $instance, $run ) ) { |
| 300 | $instance->{$run}(); |
| 301 | } |
| 302 | }; |
| 303 | |
| 304 | if ( $hook ) { |
| 305 | add_action( $hook, $callback, $priority ); |
| 306 | } else { |
| 307 | $callback(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Register classes in bulk. |
| 313 | * |
| 314 | * @since 1.5.7 |
| 315 | * |
| 316 | * @param array $classes Classes to register. |
| 317 | */ |
| 318 | public function register_bulk( $classes ) { |
| 319 | |
| 320 | if ( ! is_array( $classes ) ) { |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | foreach ( $classes as $class ) { |
| 325 | $this->register( $class ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Get a class instance from a registry. |
| 331 | * |
| 332 | * @since 1.5.7 |
| 333 | * |
| 334 | * @param string $name Class name or an alias. |
| 335 | * |
| 336 | * @return mixed|stdClass|null |
| 337 | */ |
| 338 | public function get( $name ) { |
| 339 | |
| 340 | if ( ! empty( $this->registry[ $name ] ) ) { |
| 341 | return $this->registry[ $name ]; |
| 342 | } |
| 343 | |
| 344 | // Backward compatibility for old public properties. |
| 345 | // Return null to save old condition for these properties. |
| 346 | if ( in_array( $name, $this->legacy_properties, true ) ) { |
| 347 | return isset( $this->{$name} ) ? $this->{$name} : null; |
| 348 | } |
| 349 | |
| 350 | return new stdClass(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Get the list of all custom tables starting with `wpforms_*`. |
| 355 | * |
| 356 | * @since 1.6.3 |
| 357 | * |
| 358 | * @return array List of table names. |
| 359 | */ |
| 360 | public function get_existing_custom_tables() { |
| 361 | |
| 362 | global $wpdb; |
| 363 | |
| 364 | $tables = $wpdb->get_results( "SHOW TABLES LIKE '" . $wpdb->prefix . "wpforms_%'", 'ARRAY_N' ); // phpcs:ignore |
| 365 | |
| 366 | return ! empty( $tables ) ? wp_list_pluck( $tables, 0 ) : []; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Whether the current instance of the plugin is a paid version, or free. |
| 371 | * |
| 372 | * @since 1.7.3 |
| 373 | * |
| 374 | * @return bool |
| 375 | */ |
| 376 | public function is_pro() { |
| 377 | |
| 378 | /** |
| 379 | * Filters whether the current plugin version is pro. |
| 380 | * |
| 381 | * @since 1.7.3 |
| 382 | * |
| 383 | * @param bool $pro Whether the current plugin version is pro. |
| 384 | */ |
| 385 | return (bool) apply_filters( 'wpforms_allow_pro_version', $this->pro ); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | namespace { |
| 391 | |
| 392 | /** |
| 393 | * The function which returns the one WPForms instance. |
| 394 | * |
| 395 | * @since 1.0.0 |
| 396 | * |
| 397 | * @return WPForms\WPForms |
| 398 | */ |
| 399 | function wpforms() { |
| 400 | |
| 401 | return WPForms\WPForms::instance(); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Adding an alias for backward-compatibility with plugins |
| 406 | * that still use class_exists( 'WPForms' ) |
| 407 | * instead of function_exists( 'wpforms' ), which is preferred. |
| 408 | * |
| 409 | * In 1.5.0 we removed support for PHP 5.2 |
| 410 | * and moved former WPForms class to a namespace: WPForms\WPForms. |
| 411 | * |
| 412 | * @since 1.5.1 |
| 413 | */ |
| 414 | class_alias( 'WPForms\WPForms', 'WPForms' ); |
| 415 | } |
| 416 |