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