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