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