Controllers
1 year ago
Integrations
1 year ago
Models
2 years ago
Traits
3 years ago
Loader.php
1 year ago
Loader.php
395 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Loader. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category Loader |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers; |
| 15 | |
| 16 | use DirectoryIterator; |
| 17 | use SureTriggers\Controllers\AuthController; |
| 18 | use SureTriggers\Controllers\AutomationController; |
| 19 | use SureTriggers\Controllers\EventController; |
| 20 | use SureTriggers\Controllers\GlobalSearchController; |
| 21 | use SureTriggers\Controllers\IntegrationsController; |
| 22 | use SureTriggers\Controllers\OptionController; |
| 23 | use SureTriggers\Controllers\RestController; |
| 24 | use SureTriggers\Controllers\RoutesController; |
| 25 | use SureTriggers\Controllers\SettingsController; |
| 26 | use SureTriggers\Traits\SingletonLoader; |
| 27 | use function add_menu_page; |
| 28 | use function add_submenu_page; |
| 29 | |
| 30 | /** |
| 31 | * Loader |
| 32 | * |
| 33 | * @category Loader |
| 34 | * @package SureTriggers |
| 35 | * @author BSF <username@example.com> |
| 36 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 37 | * @link https://www.brainstormforce.com/ |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | class Loader { |
| 41 | |
| 42 | |
| 43 | |
| 44 | use SingletonLoader; |
| 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | * |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | public function __construct() { |
| 52 | register_activation_hook( SURE_TRIGGERS_FILE, [ $this, 'st_activate' ] ); |
| 53 | |
| 54 | $this->define_constants(); |
| 55 | add_action( 'plugins_loaded', [ $this, 'initialize_core' ] ); |
| 56 | // Admin Menu. |
| 57 | add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
| 58 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 59 | add_action( 'admin_init', [ $this, 'reset_plugin' ] ); |
| 60 | |
| 61 | add_filter( 'plugin_action_links_' . plugin_basename( SURE_TRIGGERS_FILE ), [ $this, 'add_settings_link' ] ); |
| 62 | add_action( 'admin_init', [ $this, 'redirect_after_activation' ] ); |
| 63 | |
| 64 | add_action( 'admin_notices', [ $this, 'display_notice' ] ); |
| 65 | |
| 66 | add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Adding dashboard widget. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function add_dashboard_widgets() { |
| 75 | if ( isset( OptionController::$options['secret_key'] ) ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | wp_add_dashboard_widget( |
| 80 | 'suretriggers_dashboard_widget', |
| 81 | 'Please Connect SureTriggers', |
| 82 | [ $this, 'dashboard_widget_display' ], |
| 83 | '', |
| 84 | '', |
| 85 | 'side', |
| 86 | 'high' |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Dashboard widget callback. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function dashboard_widget_display() { ?> |
| 96 | <div> |
| 97 | <p> <?php esc_html_e( 'Please connect to or create your SureTriggers account.', 'suretriggers' ); ?></p> |
| 98 | <p> <?php esc_html_e( 'This will enable you to connect your various plugins, and apps together and automate repetitive tasks.', 'suretriggers' ); ?> </p> |
| 99 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Get Started', 'suretriggers' ); ?> </a> |
| 100 | </div> |
| 101 | <?php |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Display notice. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function display_notice() { |
| 110 | if ( isset( OptionController::$options['secret_key'] ) ) { |
| 111 | return; |
| 112 | } |
| 113 | global $pagenow; |
| 114 | if ( 'index.php' != $pagenow ) { |
| 115 | return; |
| 116 | } |
| 117 | ?> |
| 118 | <div class="notice notice-success" style="padding-bottom: 15px;"> |
| 119 | <p> |
| 120 | <strong> |
| 121 | <?php esc_html_e( 'Connect your plugins and apps together with SureTriggers', 'suretriggers' ); ?> |
| 122 | <span style="transform: rotate(-90deg); font-size: 15px;" class="dashicons dashicons-admin-plugins"></span> |
| 123 | </strong> |
| 124 | </p> |
| 125 | <p> <?php esc_html_e( 'Please connect to or create your SureTriggers account. This will enable you to connect your various plugins and apps together and automate repetitive tasks.', 'suretriggers' ); ?> </p> |
| 126 | |
| 127 | <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Get Started With SureTriggers', 'suretriggers' ); ?> </a> |
| 128 | <a href="https://suretriggers.com/" class="button button-secondary"> <?php esc_html_e( 'Learn More', 'suretriggers' ); ?> </a> |
| 129 | </div> |
| 130 | <?php |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Redirect user after plugin activation. |
| 135 | * |
| 136 | * @return void |
| 137 | */ |
| 138 | public function redirect_after_activation() { |
| 139 | $is_redirect = get_transient( 'st-redirect-after-activation' ); |
| 140 | if ( $is_redirect ) { |
| 141 | delete_transient( 'st-redirect-after-activation' ); |
| 142 | $url = get_admin_url() . 'admin.php?page=suretriggers'; |
| 143 | wp_safe_redirect( $url ); |
| 144 | die; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adding setting link. |
| 150 | * |
| 151 | * @param array $links links. |
| 152 | * @return array |
| 153 | */ |
| 154 | public function add_settings_link( array $links ) { |
| 155 | $url = get_admin_url() . 'admin.php?page=suretriggers'; |
| 156 | $setting_option = get_option( 'suretrigger_options' ); |
| 157 | if ( isset( $setting_option ) && ! empty( $setting_option ) ) { |
| 158 | $settings_link = '<a href="' . $url . '">' . __( 'Dashboard', 'suretriggers' ) . '</a>'; |
| 159 | } else { |
| 160 | $settings_link = '<a href="' . $url . '">' . __( 'Connect', 'suretriggers' ) . '</a>'; |
| 161 | } |
| 162 | $links[] = $settings_link; |
| 163 | return $links; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Define constants |
| 168 | * |
| 169 | * @return void |
| 170 | * @since 1.0.0 |
| 171 | */ |
| 172 | public function define_constants() { |
| 173 | $sass_url = 'https://app.suretriggers.com'; |
| 174 | $api_url = 'https://api.suretriggers.com'; |
| 175 | $webhook_url = 'https://webhook.suretriggers.com'; |
| 176 | |
| 177 | define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) ); |
| 178 | define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) ); |
| 179 | define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) ); |
| 180 | define( 'SURE_TRIGGERS_VER', '1.0.52' ); |
| 181 | define( 'SURE_TRIGGERS_DB_VER', '1.0.52' ); |
| 182 | define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' ); |
| 183 | define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' ); |
| 184 | define( 'SURE_TRIGGERS_SITE_URL', $sass_url ); |
| 185 | define( 'API_SERVER_URL', $api_url ); |
| 186 | define( 'WEBHOOK_SERVER_URL', $webhook_url ); |
| 187 | |
| 188 | define( 'SURE_TRIGGERS_PAGE', 'SureTrigger' ); |
| 189 | define( 'SURE_TRIGGERS_AS_GROUP', 'SureTrigger' ); |
| 190 | |
| 191 | define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Flush permalink rules while plugin activation. |
| 196 | * |
| 197 | * @return void |
| 198 | */ |
| 199 | public function st_activate() { |
| 200 | flush_rewrite_rules(); //phpcs:ignore |
| 201 | |
| 202 | set_transient( 'st-redirect-after-activation', true, 120 ); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add main menu |
| 207 | * |
| 208 | * @since x.x.x |
| 209 | * |
| 210 | * @return void |
| 211 | */ |
| 212 | public function admin_menu() { |
| 213 | $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'SureTriggers', 'suretriggers' ) ); |
| 214 | $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/STLogo.svg' ); |
| 215 | |
| 216 | add_menu_page( |
| 217 | $page_title, |
| 218 | $page_title, |
| 219 | 'manage_options', |
| 220 | 'suretriggers', |
| 221 | [ $this, 'menu_callback' ], |
| 222 | 'data:image/svg+xml;base64,' . base64_encode( $logo ), |
| 223 | 30.6002 |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Enqueue the admin scripts |
| 229 | * |
| 230 | * @param string $hook hook. |
| 231 | * @since x.x.x |
| 232 | * |
| 233 | * @return void |
| 234 | */ |
| 235 | public function enqueue_scripts( $hook = '' ) { |
| 236 | if ( ! in_array( $hook, [ 'toplevel_page_suretriggers' ], true ) ) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | remove_all_actions( 'admin_notices' ); |
| 241 | |
| 242 | $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php'; |
| 243 | if ( ! file_exists( $file ) ) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | $asset = require_once $file; |
| 248 | |
| 249 | if ( ! isset( $asset ) ) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | wp_register_script( |
| 254 | 'sure-trigger-admin', |
| 255 | SURE_TRIGGERS_URL . 'app/build/main.js', |
| 256 | array_merge( $asset['dependencies'] ), |
| 257 | $asset['version'], |
| 258 | true |
| 259 | ); |
| 260 | |
| 261 | wp_localize_script( |
| 262 | 'sure-trigger-admin', |
| 263 | 'sureTriggerData', |
| 264 | $this->get_localized_array() |
| 265 | ); |
| 266 | wp_enqueue_script( 'sure-trigger-admin' ); |
| 267 | wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER ); |
| 268 | wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get localized array for sure triggers. |
| 273 | * |
| 274 | * @return array |
| 275 | */ |
| 276 | private function get_localized_array() { |
| 277 | $current_user = wp_get_current_user(); |
| 278 | |
| 279 | $data = [ |
| 280 | 'siteContent' => [ |
| 281 | 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ), |
| 282 | 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers', |
| 283 | 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ), |
| 284 | 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect', |
| 285 | 'siteTitle' => get_bloginfo( 'name' ), |
| 286 | 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), |
| 287 | ], |
| 288 | 'user' => [ |
| 289 | 'name' => $current_user->display_name, |
| 290 | 'email' => $current_user->user_email, |
| 291 | ], |
| 292 | 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ), |
| 293 | 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ), |
| 294 | 'integrations' => IntegrationsController::get_activated_integrations(), |
| 295 | 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ), |
| 296 | 'settingsPageURL' => admin_url( 'themes.php?page=suretriggers' ), |
| 297 | 'verification_status' => false, |
| 298 | 'projects' => [], |
| 299 | 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE, |
| 300 | 'isElementorEditor' => ( did_action( 'elementorpro/loaded' ) ) ? Elementor\Plugin::instance()->editor->is_edit_mode() : false, |
| 301 | 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ), |
| 302 | ]; |
| 303 | |
| 304 | if ( current_user_can( 'manage_options' ) ) { |
| 305 | $data['siteContent']['accessKey'] = OptionController::get_option( 'secret_key' ); |
| 306 | $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' ); |
| 307 | } |
| 308 | |
| 309 | $settings = OptionController::get_option( 'st_settings' ); |
| 310 | if ( empty( $settings ) ) { |
| 311 | $settings = (object) []; |
| 312 | } |
| 313 | |
| 314 | $data['settingsForm'] = SettingsController::get_fields(); |
| 315 | $data['settings'] = wp_json_encode( $settings ); |
| 316 | $data['nonce'] = wp_create_nonce( 'st-nonce' ); |
| 317 | $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php', 'relative' ) ); |
| 318 | |
| 319 | return apply_filters( 'sure_trigger_control_localize_vars', $data ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Menu callback |
| 324 | * |
| 325 | * @since x.x.x |
| 326 | * |
| 327 | * @return void |
| 328 | */ |
| 329 | public function menu_callback() { |
| 330 | ?> |
| 331 | <div id="sure-triggger-entry" class="st-base"></div> |
| 332 | <?php |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Include all files from the folder. |
| 337 | * |
| 338 | * @param string $folder folder path. |
| 339 | * @return void |
| 340 | */ |
| 341 | public function include_all_files( $folder ) { |
| 342 | $dir = new DirectoryIterator( $folder ); |
| 343 | foreach ( $dir as $file ) { |
| 344 | if ( ! $file->isDot() ) { |
| 345 | if ( $file->isDir() ) { |
| 346 | $this->include_all_files( $file->getPathname() ); |
| 347 | } else { |
| 348 | require_once $file->getPathname(); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Initialize core trigger and actions. |
| 356 | * |
| 357 | * @return void |
| 358 | */ |
| 359 | public function initialize_core() { |
| 360 | /** |
| 361 | * Include only integrations root files |
| 362 | */ |
| 363 | |
| 364 | $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' ); |
| 365 | |
| 366 | IntegrationsController::load_event_files(); |
| 367 | |
| 368 | EventController::get_instance(); |
| 369 | IntegrationsController::get_instance(); |
| 370 | GlobalSearchController::get_instance(); |
| 371 | RestController::get_instance(); |
| 372 | OptionController::get_instance(); |
| 373 | AutomationController::get_instance(); |
| 374 | AuthController::get_instance(); |
| 375 | RoutesController::get_instance(); |
| 376 | SettingsController::get_instance(); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Added option to reset plugin in case of testing. |
| 381 | * |
| 382 | * @return void |
| 383 | */ |
| 384 | public function reset_plugin() { |
| 385 | $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 386 | $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) ); |
| 387 | |
| 388 | if ( $nonce && $is_reset && current_user_can( 'manage_options' ) && wp_verify_nonce( $nonce, 'st-reset-action' ) ) { |
| 389 | delete_option( 'suretrigger_options' ); |
| 390 | wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) ); |
| 391 | exit(); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 |