plugin-loader.php
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Loader. |
| 4 | * |
| 5 | * @package sureforms |
| 6 | * @since 0.0.1 |
| 7 | */ |
| 8 | |
| 9 | namespace SRFM; |
| 10 | |
| 11 | use SRFM\Admin\Admin; |
| 12 | use SRFM\Admin\Analytics; |
| 13 | use SRFM\API\Block_Patterns; |
| 14 | use SRFM\Inc\Activator; |
| 15 | use SRFM\Inc\Admin_Ajax; |
| 16 | use SRFM\Inc\AI_Form_Builder\AI_Auth; |
| 17 | use SRFM\Inc\AI_Form_Builder\AI_Form_Builder; |
| 18 | use SRFM\Inc\AI_Form_Builder\AI_Helper; |
| 19 | use SRFM\Inc\AI_Form_Builder\Field_Mapping; |
| 20 | use SRFM\Inc\Background_Process; |
| 21 | use SRFM\Inc\Blocks\Register; |
| 22 | use SRFM\Inc\Compatibility\Themes\Astra; |
| 23 | use SRFM\Inc\Create_New_Form; |
| 24 | use SRFM\Inc\Database\Register as DatabaseRegister; |
| 25 | use SRFM\Inc\Events_Scheduler; |
| 26 | use SRFM\Inc\Export; |
| 27 | use SRFM\Inc\Form_Restriction; |
| 28 | use SRFM\Inc\Form_Submit; |
| 29 | use SRFM\Inc\Forms_Data; |
| 30 | use SRFM\Inc\Frontend_Assets; |
| 31 | use SRFM\Inc\Generate_Form_Markup; |
| 32 | use SRFM\Inc\Global_Settings\Email_Summary; |
| 33 | use SRFM\Inc\Global_Settings\Global_Settings; |
| 34 | use SRFM\Inc\Gutenberg_Hooks; |
| 35 | use SRFM\Inc\Helper; |
| 36 | use SRFM\Inc\Lib\SRFM_Nps_Survey; |
| 37 | use SRFM\Inc\Nps_Notice; |
| 38 | use SRFM\Inc\Onboarding; |
| 39 | use SRFM\Inc\Page_Builders\Page_Builders; |
| 40 | use SRFM\Inc\Post_Types; |
| 41 | use SRFM\Inc\Rest_Api; |
| 42 | use SRFM\Inc\Single_Form_Settings\Compliance_Settings; |
| 43 | use SRFM\Inc\Smart_Tags; |
| 44 | use SRFM\Inc\Updater; |
| 45 | |
| 46 | if ( ! defined( 'ABSPATH' ) ) { |
| 47 | exit; // Exit if accessed directly. |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Plugin_Loader |
| 52 | * |
| 53 | * @since 0.0.1 |
| 54 | */ |
| 55 | class Plugin_Loader { |
| 56 | /** |
| 57 | * Instance |
| 58 | * |
| 59 | * @access private |
| 60 | * @var object Class Instance. |
| 61 | * @since 0.0.1 |
| 62 | */ |
| 63 | private static $instance = null; |
| 64 | |
| 65 | /** |
| 66 | * Constructor |
| 67 | * |
| 68 | * @since 0.0.1 |
| 69 | */ |
| 70 | public function __construct() { |
| 71 | if ( ! defined( 'SRFM_DIR' ) || ! defined( 'SRFM_FILE' ) ) { |
| 72 | return; |
| 73 | } |
| 74 | // Load the action scheduler before plugin loads. |
| 75 | require_once SRFM_DIR . 'inc/lib/action-scheduler/action-scheduler.php'; |
| 76 | |
| 77 | spl_autoload_register( [ $this, 'autoload' ] ); |
| 78 | |
| 79 | add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] ); |
| 80 | add_action( 'plugins_loaded', [ $this, 'load_plugin' ], 99 ); |
| 81 | add_action( 'init', [ $this, 'load_classes' ] ); |
| 82 | add_action( 'admin_init', [ $this, 'activation_redirect' ] ); |
| 83 | Analytics::get_instance(); |
| 84 | /** |
| 85 | * The code that runs during plugin activation |
| 86 | */ |
| 87 | register_activation_hook( |
| 88 | SRFM_FILE, |
| 89 | static function () { |
| 90 | Activator::activate(); |
| 91 | } |
| 92 | ); |
| 93 | |
| 94 | register_deactivation_hook( |
| 95 | SRFM_FILE, |
| 96 | static function () { |
| 97 | update_option( '__sureforms_do_redirect', false ); |
| 98 | Events_Scheduler::unschedule_events( 'srfm_weekly_scheduled_events' ); |
| 99 | Events_Scheduler::unschedule_events( 'srfm_daily_scheduled_action' ); |
| 100 | } |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Initiator |
| 106 | * |
| 107 | * @since 0.0.1 |
| 108 | * @return object initialized object of class. |
| 109 | */ |
| 110 | public static function get_instance() { |
| 111 | if ( null === self::$instance ) { |
| 112 | self::$instance = new self(); |
| 113 | |
| 114 | /** |
| 115 | * SureForms loaded. |
| 116 | * |
| 117 | * Fires when SureForms was fully loaded and instantiated. |
| 118 | * |
| 119 | * @since 0.0.1 |
| 120 | */ |
| 121 | do_action( 'srfm_core_loaded' ); |
| 122 | } |
| 123 | return self::$instance; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Autoload classes. |
| 128 | * |
| 129 | * @param string $class class name. |
| 130 | * @return void |
| 131 | */ |
| 132 | public function autoload( $class ) { |
| 133 | if ( 0 !== strpos( $class, __NAMESPACE__ ) ) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | $filename = preg_replace( |
| 138 | [ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ], |
| 139 | [ '', '$1-$2', '-', DIRECTORY_SEPARATOR ], |
| 140 | $class |
| 141 | ); |
| 142 | |
| 143 | if ( ! is_string( $filename ) ) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // Check for directory traversal. |
| 148 | if ( strpos( $filename, '..' ) !== false ) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $filename = strtolower( $filename ); |
| 153 | |
| 154 | $file = SRFM_DIR . $filename . '.php'; |
| 155 | |
| 156 | // if the file is readable, include it. |
| 157 | if ( is_readable( $file ) ) { |
| 158 | require_once $file; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Activation Reset |
| 164 | * |
| 165 | * @return void |
| 166 | * @since 0.0.1 |
| 167 | */ |
| 168 | public function activation_redirect() { |
| 169 | // Avoid redirection in case of WP_CLI calls. |
| 170 | if ( defined( 'WP_CLI' ) && \WP_CLI ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | // Avoid redirection in case of ajax calls. |
| 175 | if ( wp_doing_ajax() ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | $do_redirect = apply_filters( 'srfm_enable_redirect_activation', get_option( '__srfm_do_redirect' ) ); |
| 180 | |
| 181 | if ( $do_redirect ) { |
| 182 | |
| 183 | update_option( '__srfm_do_redirect', false ); |
| 184 | |
| 185 | if ( ! is_multisite() ) { |
| 186 | wp_safe_redirect( |
| 187 | add_query_arg( |
| 188 | [ |
| 189 | 'page' => 'sureforms_menu', |
| 190 | 'srfm-activation-redirect' => true, |
| 191 | ], |
| 192 | admin_url( 'admin.php' ) |
| 193 | ) |
| 194 | ); |
| 195 | exit; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Load Classes. |
| 202 | * |
| 203 | * @return void |
| 204 | * @since 0.0.1 |
| 205 | */ |
| 206 | public function load_classes() { |
| 207 | |
| 208 | Register::get_instance(); |
| 209 | if ( is_admin() ) { |
| 210 | Admin::get_instance(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Load Plugin Text Domain. |
| 216 | * This will load the translation textdomain depending on the file priorities. |
| 217 | * 1. Global Languages /wp-content/languages/sureforms/ folder |
| 218 | * 2. Local directory /wp-content/plugins/sureforms/languages/ folder |
| 219 | * |
| 220 | * @since 0.0.1 |
| 221 | * @return void |
| 222 | */ |
| 223 | public function load_textdomain() { |
| 224 | // Default languages directory. |
| 225 | $lang_dir = SRFM_DIR . 'languages/'; |
| 226 | |
| 227 | /** |
| 228 | * Filters the languages directory path to use for plugin. |
| 229 | * |
| 230 | * @param string $lang_dir The languages directory path. |
| 231 | */ |
| 232 | $lang_dir = apply_filters( 'srfm_languages_directory', $lang_dir ); |
| 233 | |
| 234 | // Traditional WordPress plugin locale filter. |
| 235 | global $wp_version; |
| 236 | |
| 237 | $get_locale = get_locale(); |
| 238 | |
| 239 | if ( $wp_version >= 4.7 ) { |
| 240 | $get_locale = get_user_locale(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Language Locale for plugin |
| 245 | * |
| 246 | * Uses get_user_locale()` in WordPress 4.7 or greater, |
| 247 | * otherwise uses `get_locale()`. |
| 248 | */ |
| 249 | $locale = apply_filters( 'plugin_locale', $get_locale, 'sureforms' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook |
| 250 | $mofile = sprintf( '%1$s-%2$s.mo', 'sureforms', $locale ); |
| 251 | |
| 252 | // Setup paths to current locale file. |
| 253 | $mofile_global = WP_LANG_DIR . '/plugins/' . $mofile; |
| 254 | $mofile_local = $lang_dir . $mofile; |
| 255 | |
| 256 | if ( file_exists( $mofile_global ) ) { |
| 257 | // Look in global /wp-content/languages/sureforms/ folder. |
| 258 | load_textdomain( 'sureforms', $mofile_global ); |
| 259 | } elseif ( file_exists( $mofile_local ) ) { |
| 260 | // Look in local /wp-content/plugins/sureforms/languages/ folder. |
| 261 | load_textdomain( 'sureforms', $mofile_local ); |
| 262 | } else { |
| 263 | // Load the default language files. |
| 264 | load_plugin_textdomain( 'sureforms', false, $lang_dir ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Loads plugin files. |
| 270 | * |
| 271 | * @since 0.0.1 |
| 272 | * |
| 273 | * @return void |
| 274 | */ |
| 275 | public function load_plugin() { |
| 276 | Post_Types::get_instance(); |
| 277 | Form_Submit::get_instance(); |
| 278 | Block_Patterns::get_instance(); |
| 279 | Gutenberg_Hooks::get_instance(); |
| 280 | Frontend_Assets::get_instance(); |
| 281 | Helper::get_instance(); |
| 282 | Activator::get_instance(); |
| 283 | Admin_Ajax::get_instance(); |
| 284 | Forms_Data::get_instance(); |
| 285 | Export::get_instance(); |
| 286 | Smart_Tags::get_instance(); |
| 287 | Generate_Form_Markup::get_instance(); |
| 288 | Create_New_Form::get_instance(); |
| 289 | Global_Settings::get_instance(); |
| 290 | Email_Summary::get_instance(); |
| 291 | Compliance_Settings::get_instance(); |
| 292 | Events_Scheduler::get_instance(); |
| 293 | AI_Form_Builder::get_instance(); |
| 294 | Field_Mapping::get_instance(); |
| 295 | Background_Process::get_instance(); |
| 296 | Page_Builders::get_instance(); |
| 297 | Rest_Api::get_instance(); |
| 298 | AI_Helper::get_instance(); |
| 299 | AI_Auth::get_instance(); |
| 300 | Updater::get_instance(); |
| 301 | Onboarding::get_instance(); |
| 302 | DatabaseRegister::init(); |
| 303 | Form_Restriction::get_instance(); |
| 304 | // Initializing Compatibilities. |
| 305 | Astra::get_instance(); |
| 306 | /** |
| 307 | * Required to add the if check for the class existence to resolve phpstan error, |
| 308 | * as the phpstan configuration ignores the inc/lib directory which gives error |
| 309 | * unknown class. |
| 310 | */ |
| 311 | if ( class_exists( 'SRFM\Inc\Lib\SRFM_Nps_Survey' ) && ! apply_filters( 'srfm_disable_nps_survey', false ) ) { |
| 312 | SRFM_Nps_Survey::get_instance(); // Inits the NPS Survey class for which inits the NPS Survey plugin. |
| 313 | Nps_Notice::get_instance(); // Responsible for displaying the NPS Survey: keeping the line out of the check will also work. |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Load core files necessary for the Spectra block. |
| 318 | * This method is called in the Spectra block loader to ensure core files are loaded during the 'plugins_loaded' action. |
| 319 | * |
| 320 | * Note: This code is added at the bottom to ensure the form block is loaded first, |
| 321 | * followed by the Spectra blocks such as heading, image, and icon blocks. |
| 322 | */ |
| 323 | $this->load_core_files(); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Load Core Files. |
| 328 | * |
| 329 | * @since 0.0.1 |
| 330 | * |
| 331 | * @return void |
| 332 | */ |
| 333 | public function load_core_files() { |
| 334 | include_once SRFM_DIR . 'modules/gutenberg/classes/class-spec-block-loader.php'; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Kicking this off by calling 'get_instance()' method |
| 340 | */ |
| 341 | Plugin_Loader::get_instance(); |
| 342 |