PluginProbe
OttoKit: All-in-One Automation Platform / 1.1.10
OttoKit: All-in-One Automation Platform v1.1.10
1.1.38 1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 All 124 releases
suretriggers / src / Loader.php

Loader.php in OttoKit: All-in-One Automation Platform 1.1.10, at src/Loader.php

889 lines 31.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\WebhookRequestsController;
26 use SureTriggers\Controllers\SettingsController;
27 use SureTriggers\Traits\SingletonLoader;
28 use SureTriggers\Models\SaasApiToken;
29 use function add_menu_page;
30 use function add_submenu_page;
31
32 /**
33 * Loader
34 *
35 * @category Loader
36 * @package SureTriggers
37 * @author BSF <username@example.com>
38 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
39 * @link https://www.brainstormforce.com/
40 * @since 1.0.0
41 */
42 class Loader {
43
44
45
46 use SingletonLoader;
47
48 /**
49 * Constructor
50 *
51 * @since 1.0.0
52 */
53 public function __construct() {
54 register_activation_hook( SURE_TRIGGERS_FILE, [ $this, 'st_activate' ] );
55
56 $this->define_constants();
57 add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
58 add_action( 'plugins_loaded', [ $this, 'initialize_core' ] );
59 // Admin Menu.
60 add_action( 'admin_menu', [ $this, 'admin_menu' ] );
61 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
62 add_action( 'admin_head', [ $this, 'add_admin_menu_styles' ] );
63 add_action( 'admin_init', [ $this, 'reset_plugin' ] );
64
65 add_filter( 'plugin_action_links_' . plugin_basename( SURE_TRIGGERS_FILE ), [ $this, 'add_settings_link' ] );
66 add_action( 'admin_init', [ $this, 'redirect_after_activation' ] );
67
68 add_action( 'admin_notices', [ $this, 'display_notice' ] );
69
70 add_action( 'all_admin_notices', [ $this, 'suretriggers_show_api_connection_error' ] );
71
72 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] );
73
74 // Remove Webhook Requests retry cron and requests table.
75 register_uninstall_hook(
76 SURE_TRIGGERS_FILE,
77 [ WebhookRequestsController::class, 'suretriggers_remove_table_retry_cron' ]
78 );
79 }
80
81 /**
82 * Adding dashboard widget.
83 *
84 * @return void
85 */
86 public function add_dashboard_widgets() {
87 if ( isset( OptionController::$options['secret_key'] ) ) {
88 return;
89 }
90
91 wp_add_dashboard_widget(
92 'suretriggers_dashboard_widget',
93 __( 'Stop Doing It Manually!', 'suretriggers' ),
94 [ $this, 'dashboard_widget_display' ],
95 null,
96 null,
97 'side',
98 'high'
99 );
100 }
101
102 /**
103 * Dashboard widget callback.
104 *
105 * @return void
106 */
107 public function dashboard_widget_display() { ?>
108 <div>
109 <p> <?php esc_html_e( 'Automation That’s Easy Enough for Anyone.', 'suretriggers' ); ?></p>
110 <p>
111 <?php
112 esc_html_e(
113 'OttoKit connects all your tools - WooCommerce, Forms, CRM, and more, so your website runs smoothly while you focus on your business.
114 ',
115 'suretriggers'
116 );
117 ?>
118 </p>
119 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Start Automating', 'suretriggers' ); ?> </a>
120 </div>
121 <?php
122 }
123
124 /**
125 * Load Plugin Text Domain.
126 * This will load the translation textdomain depending on the file priorities.
127 * 1. Global Languages /wp-content/languages/suretriggers/ folder
128 * 2. Local directory /wp-content/plugins/suretriggers/languages/ folder
129 *
130 * @return void
131 */
132 public function load_textdomain() {
133 // Default languages directory.
134 $lang_dir = SURE_TRIGGERS_DIR . 'languages/';
135
136 /**
137 * Filters the languages directory path to use for plugin.
138 *
139 * @param string $lang_dir The languages directory path.
140 */
141 $lang_dir = apply_filters( 'suretriggers_languages_directory', $lang_dir );
142
143 // Traditional WordPress plugin locale filter.
144 global $wp_version;
145
146 $get_locale = get_locale();
147
148 if ( $wp_version >= 4.7 ) {
149 $get_locale = get_user_locale();
150 }
151
152 /**
153 * Language Locale for plugin
154 *
155 * Uses get_user_locale()` in WordPress 4.7 or greater,
156 * otherwise uses `get_locale()`.
157 */
158 $locale = apply_filters( 'plugin_locale', $get_locale, 'suretriggers' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook
159 $mofile = sprintf( '%1$s-%2$s.mo', 'suretriggers', $locale );
160
161 // Setup paths to current locale file.
162 $mofile_global = WP_LANG_DIR . '/plugins/' . $mofile;
163 $mofile_local = $lang_dir . $mofile;
164
165 $mofile_global = wp_normalize_path( $mofile_global );
166 $mofile_local = wp_normalize_path( $mofile_local );
167
168 if ( file_exists( $mofile_global ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) ) {
169 // Look in global /wp-content/languages/suretriggers/ folder.
170 load_textdomain( 'suretriggers', $mofile_global );
171 } elseif ( file_exists( $mofile_local ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) && $this->st_valid_file_path( $mofile_local ) ) {
172 // Look in local /wp-content/plugins/suretriggers/languages/ folder.
173 load_textdomain( 'suretriggers', $mofile_local );
174 } else {
175 load_plugin_textdomain( 'suretriggers', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
176 }
177 }
178
179 /**
180 * Check if file is valid.
181 *
182 * @param string $file_path file_path.
183 * @return bool
184 */
185 private function st_valid_file_path( $file_path ) {
186 $allowed_dir = realpath( SURE_TRIGGERS_DIR . 'languages/' );
187 $real_file_path = realpath( $file_path );
188 return false !== $real_file_path && $allowed_dir && 0 === strpos( $real_file_path, $allowed_dir );
189 }
190
191 /**
192 * Display notice.
193 *
194 * @return void
195 */
196 public function display_notice() {
197 global $pagenow;
198 if ( isset( OptionController::$options['secret_key'] ) ) {
199 return;
200 }
201 if ( 'index.php' != $pagenow ) {
202 return;
203 }
204 ?>
205 <div class="notice notice-success" style="padding-bottom: 15px;">
206 <p>
207 <strong>
208 <?php esc_html_e( 'Automate Your WordPress Site. Save Hours. Earn More.', 'suretriggers' ); ?>
209 <span style="transform: rotate(-90deg); font-size: 15px;" class="dashicons dashicons-admin-plugins"></span>
210 </strong>
211 </p>
212 <p> <?php esc_html_e( 'OttoKit connects your plugins and favorite apps so your business runs on autopilot — while you focus on what matters most.', 'suretriggers' ); ?> </p>
213
214 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Start Automating', 'suretriggers' ); ?> </a>
215 <a href="https://ottokit.com/" class="button button-secondary"> <?php esc_html_e( 'See How It Works', 'suretriggers' ); ?> </a>
216 </div>
217 <?php
218 }
219
220 /**
221 * Show Connection Error Admin Notice.
222 *
223 * @return void
224 */
225 public function suretriggers_show_api_connection_error() {
226 global $pagenow;
227 $is_authorized = true;
228 if ( ! current_user_can( 'manage_options' ) ) {
229 $is_authorized = self::suretriggers_user_permission_check();
230 }
231 if ( 'index.php' != $pagenow || ! isset( OptionController::$options['secret_key'] ) || ! $is_authorized ) {
232 return;
233 }
234 $notice = get_option( 'suretriggers_verify_connection' );
235 // If empty option value for connection status, then verify the connection.
236 if ( empty( $notice ) || 'suretriggers_connection_successful' != $notice ) {
237 $connection_status = RestController::suretriggers_verify_wp_connection();
238 $connection_status_code = wp_remote_retrieve_response_code( $connection_status );
239 if ( is_wp_error( $connection_status ) ) {
240 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_wp_error' );
241 } else {
242 if ( 200 !== $connection_status_code ) {
243 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_error' );
244 } else {
245 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_successful' );
246 }
247 }
248 }
249 $notice = get_option( 'suretriggers_verify_connection' );
250 if ( 'suretriggers_connection_successful' != $notice ) {
251 // If connection status is not successful, then show the notice.
252 ?>
253 <div class="notice notice-error is-dismissible">
254 <p>
255 <strong>
256 <?php esc_html_e( 'OttoKit Connection Issue', 'suretriggers' ); ?>
257 <span style="transform: rotate(-180deg); font-size: 20px;" class="dashicons dashicons-warning"></span>
258 </strong>
259 </p>
260 <p>
261 <?php esc_html_e( 'There is an issue with the established connection between WordPress and OttoKit. Please visit the OttoKit dashboard to verify and re-establish the connection if necessary.', 'suretriggers' ); ?>
262 </p>
263 <p>
264 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-secondary"> <?php esc_html_e( 'Go To OttoKit', 'suretriggers' ); ?> </a>
265 </p>
266 </div>
267 <?php
268 }
269 }
270
271 /**
272 * Redirect user after plugin activation.
273 *
274 * @return void
275 */
276 public function redirect_after_activation() {
277 $is_redirect = get_transient( 'st-redirect-after-activation' );
278 if ( $is_redirect ) {
279 delete_transient( 'st-redirect-after-activation' );
280 $url = get_admin_url() . 'admin.php?page=suretriggers';
281 wp_safe_redirect( $url );
282 die;
283 }
284 }
285
286 /**
287 * Adding setting link.
288 *
289 * @param array $links links.
290 * @return array
291 */
292 public function add_settings_link( array $links ) {
293 $url = get_admin_url() . 'admin.php?page=suretriggers';
294 $setting_option = get_option( 'suretrigger_options' );
295 if ( isset( $setting_option ) && ! empty( $setting_option ) ) {
296 $settings_link = '<a href="' . $url . '">' . __( 'Dashboard', 'suretriggers' ) . '</a>';
297 } else {
298 $settings_link = '<a href="' . $url . '">' . __( 'Connect', 'suretriggers' ) . '</a>';
299 }
300 $links[] = $settings_link;
301
302 // Add Unlock Lifetime Access link.
303 $lifetime_access_link = '<a href="https://ottokit.com/pricing/" target="_blank" style="color: #28a745; font-weight: bold;">' . __( 'Unlock Lifetime Access', 'suretriggers' ) . '</a>';
304 $links[] = $lifetime_access_link;
305
306 return $links;
307 }
308
309 /**
310 * Define constants
311 *
312 * @return void
313 * @since 1.0.0
314 */
315 public function define_constants() {
316 $sass_url = 'https://app.ottokit.com';
317 $api_url = 'https://api.ottokit.com';
318 $webhook_url = 'https://webhook.ottokit.com';
319
320 define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) );
321 define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) );
322 define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) );
323 define( 'SURE_TRIGGERS_VER', '1.1.10' );
324 define( 'SURE_TRIGGERS_DB_VER', '1.1.10' );
325 define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' );
326 define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' );
327 define( 'SURE_TRIGGERS_SITE_URL', $sass_url );
328 define( 'SURE_TRIGGERS_API_SERVER_URL', $api_url );
329 define( 'SURE_TRIGGERS_WEBHOOK_SERVER_URL', $webhook_url );
330
331 define( 'SURE_TRIGGERS_PAGE', 'OttoKit' );
332 define( 'SURE_TRIGGERS_AS_GROUP', 'OttoKit' );
333
334 define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' );
335 }
336
337 /**
338 * Flush permalink rules while plugin activation.
339 *
340 * @return void
341 */
342 public function st_activate() {
343 flush_rewrite_rules(); //phpcs:ignore
344
345 set_transient( 'st-redirect-after-activation', true, 120 );
346 }
347
348 /**
349 * SureTriggers Access for Users and User Roles.
350 *
351 * @return bool
352 */
353 private function suretriggers_user_permission_check() {
354 $current_user = wp_get_current_user();
355 $current_user_id = $current_user->ID;
356 $current_user_roles = $current_user->roles;
357
358 // Get enabled users and roles with proper default values.
359 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
360 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
361
362 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
363 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
364
365 $is_authorized = in_array( $current_user_id, (array) $enabled_users ) || array_intersect( $current_user_roles, (array) $enabled_user_roles );
366
367 return $is_authorized;
368 }
369
370 /**
371 * Add main menu
372 *
373 * @since x.x.x
374 *
375 * @return void
376 */
377 public function admin_menu() {
378 $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'OttoKit', 'suretriggers' ) );
379 $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/OttoKitLogo.svg' );
380
381 $is_authorized = true;
382 if ( ! current_user_can( 'manage_options' ) ) {
383 $is_authorized = self::suretriggers_user_permission_check();
384 }
385
386 if ( $is_authorized ) {
387 add_menu_page(
388 $page_title,
389 $page_title,
390 'read',
391 'suretriggers',
392 [ $this, 'menu_callback' ],
393 $logo ? 'data:image/svg+xml;base64,' . base64_encode( $logo ) : '',
394 30.6002
395 );
396
397 add_submenu_page(
398 'suretriggers',
399 __( 'OttoKit Status', 'suretriggers' ),
400 __( 'Status', 'suretriggers' ),
401 'read',
402 'suretriggers-status',
403 [ $this, 'suretriggers_status_menu_callback' ]
404 );
405
406 add_submenu_page(
407 'suretriggers',
408 __( 'Unlock Lifetime', 'suretriggers' ),
409 '<span class="ottokit-upgrade-btn">' . __( 'Unlock Lifetime', 'suretriggers' ) . '</span>',
410 'read',
411 'suretriggers-lifetime-access',
412 [ $this, 'suretriggers_lifetime_access_callback' ]
413 );
414 }
415
416 if ( isset( OptionController::$options['secret_key'] ) ) {
417 add_options_page(
418 __( 'OttoKit Settings', 'suretriggers' ),
419 __( 'OttoKit Settings', 'suretriggers' ),
420 'manage_options',
421 'ottokit-settings',
422 [ $this, 'suretriggers_render_interface_callback' ]
423 );
424 }
425 }
426
427 /**
428 * Enqueue the admin scripts
429 *
430 * @param string $hook hook.
431 * @since x.x.x
432 *
433 * @return void
434 */
435 public function enqueue_scripts( $hook = '' ) {
436 // Always enqueue admin CSS for upgrade button styling.
437 wp_enqueue_style( 'st-trigger-style', SURE_TRIGGERS_URL . 'assets/admin-css/st-admin-css.css', [], SURE_TRIGGERS_VER );
438
439 if ( ! in_array( $hook, [ 'toplevel_page_suretriggers', 'ottokit_page_suretriggers-status', 'settings_page_ottokit-settings' ], true ) ) {
440 return;
441 }
442
443 remove_all_actions( 'admin_notices' );
444
445 $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php';
446 if ( ! file_exists( $file ) ) {
447 return;
448 }
449
450 $asset = require_once $file;
451
452 if ( ! isset( $asset ) ) {
453 return;
454 }
455
456 wp_register_script(
457 'sure-trigger-admin',
458 SURE_TRIGGERS_URL . 'app/build/main.js',
459 array_merge( $asset['dependencies'], [ 'regenerator-runtime' ], [ 'wp-i18n' ] ),
460 $asset['version'],
461 true
462 );
463
464 wp_localize_script(
465 'sure-trigger-admin',
466 'sureTriggerData',
467 $this->get_localized_array()
468 );
469 wp_enqueue_script( 'sure-trigger-admin' );
470 // Set the script translations.
471 wp_set_script_translations( 'sure-trigger-admin', 'suretriggers', SURE_TRIGGERS_DIR . 'languages' );
472 wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER );
473 wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER );
474 }
475
476 /**
477 * Get localized array for sure triggers.
478 *
479 * @return array
480 */
481 private function get_localized_array() {
482 $settings_nonce = wp_create_nonce( 'suretriggers_settings_nonce_action' );
483 $current_user = wp_get_current_user();
484 global $wp_roles;
485 $is_authorized = true;
486 if ( ! current_user_can( 'manage_options' ) ) {
487 $is_authorized = self::suretriggers_user_permission_check();
488 }
489
490 $source_type = get_option( 'suretriggers_source' );
491
492 // Get enabled users and roles with proper default values.
493 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
494 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
495
496 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
497 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
498
499 $data = [
500 'siteContent' => [
501 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
502 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers',
503 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ),
504 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect',
505 'siteTitle' => get_bloginfo( 'name' ),
506 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ),
507 'sourceType' => $source_type,
508 'adminLanguage' => get_user_locale(),
509 ],
510 'user' => [
511 'name' => $current_user->display_name,
512 'email' => $current_user->user_email,
513 ],
514 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ),
515 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ),
516 'integrations' => IntegrationsController::get_activated_integrations(),
517 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ),
518 'verification_status' => false,
519 'projects' => [],
520 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE,
521 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ),
522 'usersRoles' => array_diff_key( $wp_roles->get_names(), [ 'administrator' => '' ] ),
523 'usersList' => get_users(
524 [
525 'fields' => [
526 'ID',
527 'display_name',
528 ],
529 'role__not_in' => [
530 'administrator',
531 ],
532 ]
533 ),
534 'enabledUsers' => $enabled_users,
535 'enabledUserRoles' => $enabled_user_roles,
536 ];
537
538 if ( $is_authorized ) {
539 $data['siteContent']['accessKey'] = SaasApiToken::get();
540 $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' );
541 }
542
543 $data['settingsNonce'] = esc_js( $settings_nonce );
544 $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php' ) );
545
546 return apply_filters( 'sure_trigger_control_localize_vars', $data );
547 }
548
549 /**
550 * Render OttoKit Interface callback.
551 *
552 * @return void
553 */
554 public function suretriggers_render_interface_callback() {
555 ?>
556 <div id="ottokit-settings-page" class="ottokit-settings"></div>
557 <?php
558 }
559
560 /**
561 * Menu callback.
562 *
563 * @since x.x.x
564 *
565 * @return void
566 */
567 public function menu_callback() {
568 // Check permalink structure first.
569 $permalink_structure = get_option( 'permalink_structure' );
570 $is_plain_permalink = empty( $permalink_structure );
571
572 // If permalink structure is "Plain" and we're trying to connect or already connected, show a prominent warning.
573 if ( $is_plain_permalink ) {
574 ?>
575 <div class="st-permalink-warning-wrapper">
576 <div class="st-permalink-warning-card">
577 <div class="st-permalink-warning-icon">
578 <span class="st-permalink-warning-icon-text">!</span>
579 </div>
580 <div class="st-permalink-warning-content">
581 <p class="st-permalink-warning-title">
582 <?php esc_html_e( '“Plain” Permalink Structure Detected', 'suretriggers' ); ?>
583 </p>
584 <p class="st-permalink-warning-message">
585 <?php esc_html_e( 'Your site is currently using the “Plain” permalink structure, which is not supported by OttoKit. Please visit', 'suretriggers' ); ?>
586 <a href="<?php echo esc_url( admin_url( 'options-permalink.php' ) ); ?>" target="_blank" class="st-permalink-warning-link">
587 <?php esc_html_e( 'Permalinks Settings', 'suretriggers' ); ?>
588 </a>
589 <?php esc_html_e( 'on your WordPress Dashboard and choose any structure other than “Plain”.', 'suretriggers' ); ?>
590 </p>
591 </div>
592 </div>
593 </div>
594 <?php
595 }
596
597 // Verify Token.
598 $response = RestController::verify_user_token();
599 $response_body = wp_remote_retrieve_body( $response );
600 $response_code = wp_remote_retrieve_response_code( $response );
601 if ( ! empty( $response_body ) ) {
602 $response_body = json_decode( $response_body, true );
603 }
604 if ( 200 === $response_code || 401 === $response_code ) {
605 if ( is_array( $response_body ) && isset( $response_body['is_iframe_enabled'] ) && 'NO' === $response_body['is_iframe_enabled'] ) {
606 ?>
607 <div class="suretriggers-nobase">
608 <div>
609 <div>
610 <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-circle-check inline-block h-8 w-8 text-green-400 mb-6" aria-hidden="true"><circle cx="12" cy="12" r="10"></circle><path d="m9 12 2 2 4-4"></path></svg>
611 <h2 class="suretriggers-info-title">
612 <?php esc_html_e( 'OttoKit is connected.', 'suretriggers' ); ?>
613 </h2>
614 <p class="suretriggers-info-content">
615 <?php esc_html_e( 'Your WordPress site is successfully connected to the OttoKit SaaS platform. However, the OttoKit interface display is currently disabled. Click below to enable it.', 'suretriggers' ); ?>
616 </p>
617 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL . '/apps/WordPress' ); ?>" target="_blank">
618 <?php esc_html_e( 'Access Connection Page', 'suretriggers' ); ?>
619 </a>
620 </div>
621 </div>
622 </div>
623 <?php
624 } else {
625 ?>
626 <div id="sure-triggger-entry" class="st-base"></div>
627 <?php
628 }
629 } elseif ( isset( $response ) && is_wp_error( $response ) || 200 !== $response_code ) {
630 ?>
631 <div class="suretriggers-nobase">
632 <div>
633 <div>
634 <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="30" height="24" viewBox="0 0 122.88 122.879" enable-background="new 0 0 122.88 122.879" xml:space="preserve" class="lucide lucide-circle-check inline-block h-8 w-8 text-green-400 mb-6"><g><path fill="#FF4141" d="M61.44,0c16.96,0,32.328,6.882,43.453,17.986c11.104,11.125,17.986,26.494,17.986,43.453 c0,16.961-6.883,32.328-17.986,43.453C93.769,115.998,78.4,122.879,61.44,122.879c-16.96,0-32.329-6.881-43.454-17.986 C6.882,93.768,0,78.4,0,61.439C0,44.48,6.882,29.111,17.986,17.986C29.112,6.882,44.48,0,61.44,0L61.44,0z M73.452,39.152 c2.75-2.792,7.221-2.805,9.986-0.026c2.764,2.776,2.775,7.292,0.027,10.083L71.4,61.445l12.077,12.25 c2.728,2.77,2.689,7.256-0.081,10.021c-2.772,2.766-7.229,2.758-9.954-0.012L61.445,71.541L49.428,83.729 c-2.75,2.793-7.22,2.805-9.985,0.025c-2.763-2.775-2.776-7.291-0.026-10.082L51.48,61.435l-12.078-12.25 c-2.726-2.769-2.689-7.256,0.082-10.022c2.772-2.765,7.229-2.758,9.954,0.013L61.435,51.34L73.452,39.152L73.452,39.152z M96.899,25.98C87.826,16.907,75.29,11.296,61.44,11.296c-13.851,0-26.387,5.611-35.46,14.685 c-9.073,9.073-14.684,21.609-14.684,35.459s5.611,26.387,14.684,35.459c9.073,9.074,21.609,14.686,35.46,14.686 c13.85,0,26.386-5.611,35.459-14.686c9.073-9.072,14.684-21.609,14.684-35.459S105.973,35.054,96.899,25.98L96.899,25.98z"></path></g></svg>
635 <h2 class="suretriggers-info-title">
636 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
637 </h2>
638 <p class="suretriggers-info-content">
639 <?php esc_html_e( 'It looks like your WordPress site’s connection with OttoKit has been affected because the URL used for communication has changed. The current link for your site is different from the one OttoKit was originally connected to.', 'suretriggers' ); ?>
640 </p>
641 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL ); ?>" target="_blank">
642 <?php esc_html_e( 'Access Dashboard', 'suretriggers' ); ?>
643 </a>
644 <a class="suretriggers-info-link" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ); ?>">
645 <?php esc_html_e( 'Disconnect OttoKit', 'suretriggers' ); ?>
646 </a>
647 </div>
648 </div>
649 </div>
650 <?php
651 }
652 }
653
654 /**
655 * Status Menu callback.
656 *
657 * @since x.x.x
658 *
659 * @return void
660 */
661 public function suretriggers_status_menu_callback() {
662 if ( ! isset( OptionController::$options['secret_key'] ) ) {
663 ?>
664 <div class="suretriggers-nobase">
665 <div>
666 <div>
667 <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="30" height="24" viewBox="0 0 122.88 122.879" enable-background="new 0 0 122.88 122.879" xml:space="preserve" class="lucide lucide-circle-check inline-block h-8 w-8 text-green-400 mb-6"><g><path fill="#FF4141" d="M61.44,0c16.96,0,32.328,6.882,43.453,17.986c11.104,11.125,17.986,26.494,17.986,43.453 c0,16.961-6.883,32.328-17.986,43.453C93.769,115.998,78.4,122.879,61.44,122.879c-16.96,0-32.329-6.881-43.454-17.986 C6.882,93.768,0,78.4,0,61.439C0,44.48,6.882,29.111,17.986,17.986C29.112,6.882,44.48,0,61.44,0L61.44,0z M73.452,39.152 c2.75-2.792,7.221-2.805,9.986-0.026c2.764,2.776,2.775,7.292,0.027,10.083L71.4,61.445l12.077,12.25 c2.728,2.77,2.689,7.256-0.081,10.021c-2.772,2.766-7.229,2.758-9.954-0.012L61.445,71.541L49.428,83.729 c-2.75,2.793-7.22,2.805-9.985,0.025c-2.763-2.775-2.776-7.291-0.026-10.082L51.48,61.435l-12.078-12.25 c-2.726-2.769-2.689-7.256,0.082-10.022c2.772-2.765,7.229-2.758,9.954,0.013L61.435,51.34L73.452,39.152L73.452,39.152z M96.899,25.98C87.826,16.907,75.29,11.296,61.44,11.296c-13.851,0-26.387,5.611-35.46,14.685 c-9.073,9.073-14.684,21.609-14.684,35.459s5.611,26.387,14.684,35.459c9.073,9.074,21.609,14.686,35.46,14.686 c13.85,0,26.386-5.611,35.459-14.686c9.073-9.072,14.684-21.609,14.684-35.459S105.973,35.054,96.899,25.98L96.899,25.98z"></path></g></svg>
668 <h2 class="suretriggers-info-title">
669 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
670 </h2>
671 <p class="suretriggers-info-content">
672 <?php esc_html_e( 'Please connect your OttoKit (Formerly SureTriggers) account to access registered events and outgoing requests.', 'suretriggers' ); ?>
673 </p>
674 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="suretriggers-info-link"> <?php esc_html_e( 'Connect With OttoKit (Formerly SureTriggers)', 'suretriggers' ); ?> </a>
675 </div>
676 </div>
677 </div>
678 <?php
679 return;
680 }
681 ?>
682 <div class="wrap">
683 <?php
684 $tabs = [
685 'st_system_page' => __( 'Status', 'suretriggers' ),
686 'st_outgoing_requests' => __( 'Outgoing Requests', 'suretriggers' ),
687 'st_troubleshooting' => __( 'Troubleshooting', 'suretriggers' ),
688 ];
689 $current_tab = 'st_system_page';
690 if ( isset( $_REQUEST['tab'], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'suretriggers_tab_nonce' ) ) {
691 if ( array_key_exists( sanitize_key( $_REQUEST['tab'] ), $tabs ) ) {
692 $current_tab = sanitize_key( $_REQUEST['tab'] );
693 }
694 }
695 ?>
696 <nav class="suretriggers-nav-tab nav-tab-wrapper">
697 <?php
698 foreach ( $tabs as $name => $label ) {
699 $tab_url = add_query_arg(
700 [
701 'tab' => $name,
702 '_wpnonce' => wp_create_nonce( 'suretriggers_tab_nonce' ),
703 ],
704 admin_url( 'admin.php?page=suretriggers-status' )
705 );
706 echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab ';
707 if ( $current_tab == $name ) {
708 echo 'nav-tab-active';
709 }
710 echo '">' . esc_html( $label ) . '</a>';
711 }
712 ?>
713 </nav>
714 <?php
715 switch ( $current_tab ) {
716 case 'st_system_page':
717 include_once __DIR__ . '/Admin/Views/st-admin-system-page.php';
718 break;
719 case 'st_outgoing_requests':
720 include_once __DIR__ . '/Admin/Views/st-admin-outgoing-req-page.php';
721 break;
722 case 'st_troubleshooting':
723 include_once __DIR__ . '/Admin/Views/st-admin-troubleshooting-page.php';
724 break;
725 }
726 ?>
727 </div>
728 <?php
729 }
730
731 /**
732 * Lifetime Access Menu callback.
733 *
734 * @since x.x.x
735 *
736 * @return void
737 */
738 public function suretriggers_lifetime_access_callback() {
739 // Redirect to pricing page.
740 wp_safe_redirect( 'https://ottokit.com/pricing/' );
741 exit;
742 }
743
744 /**
745 * Add admin menu styles.
746 *
747 * @since x.x.x
748 *
749 * @return void
750 */
751 public function add_admin_menu_styles() {
752 ?>
753 <script>
754 jQuery(document).ready(function($) {
755 // Direct redirect for admin menu upgrade button
756 $('a[href*="suretriggers-lifetime-access"]').on('click', function(e) {
757 e.preventDefault();
758 window.open('https://ottokit.com/pricing/', '_blank');
759 return false;
760 });
761 });
762 </script>
763 <?php
764 }
765
766 /**
767 * Include all files from the folder.
768 *
769 * @param string $folder folder path.
770 * @return void
771 */
772 public function include_all_files( $folder ) {
773 $dir = new DirectoryIterator( $folder );
774 foreach ( $dir as $file ) {
775 if ( ! $file->isDot() ) {
776 if ( $file->isDir() ) {
777 $this->include_all_files( $file->getPathname() );
778 } else {
779 require_once $file->getPathname();
780 }
781 }
782 }
783 }
784
785 /**
786 * Initialize core trigger and actions.
787 *
788 * @return void
789 */
790 public function initialize_core() {
791
792 IntegrationsController::load_event_files();
793
794 EventController::get_instance();
795 IntegrationsController::get_instance();
796 GlobalSearchController::get_instance();
797 RestController::get_instance();
798 OptionController::get_instance();
799 AutomationController::get_instance();
800 AuthController::get_instance();
801 RoutesController::get_instance();
802 WebhookRequestsController::get_instance();
803 SettingsController::get_instance();
804
805 // SureTriggers Custom Filter data.
806 add_filter( 'suretriggers_get_iframe_url', [ $this, 'suretriggers_iframe_data' ] );
807 add_filter( 'suretriggers_is_user_connected', [ $this, 'suretriggers_saas_connected_data' ] );
808
809 // Create Webhook Request Log table.
810 WebhookRequestsController::suretriggers_webhook_request_log_table();
811 // Schedule the cron jon to retry failed triggers.
812 WebhookRequestsController::suretriggers_setup_custom_cron();
813
814 $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' );
815 }
816
817 /**
818 * Added option to reset plugin in case of testing.
819 *
820 * @return void
821 */
822 public function reset_plugin() {
823 $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) );
824
825 if ( $nonce && wp_verify_nonce( $nonce, 'st-reset-action' ) ) {
826 $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) );
827 if ( $is_reset && current_user_can( 'manage_options' ) ) {
828 delete_option( 'suretrigger_options' );
829 wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) );
830 exit();
831 }
832 }
833 }
834
835 /**
836 * Custom Filter data.
837 *
838 * @param string $site_url Optional. Site URL to include in the iframe data.
839 * @return string
840 */
841 public function suretriggers_iframe_data( $site_url = '' ) {
842 $site_url = esc_url_raw( $site_url );
843 if ( ! current_user_can( 'manage_options' ) ) {
844 return $site_url;
845 }
846 $site_content_data = [
847 'stSaasURL' => $site_url . 'wp-login',
848 'stCode' => SaasApiToken::get(),
849 'baseUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
850 'resetUrl' => rtrim( base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), '=' ), // phpcs:ignore
851 ];
852 $params = [
853 'st-code' => $site_content_data['stCode'],
854 'base_url' => $site_content_data['baseUrl'],
855 'reset_url' => $site_content_data['resetUrl'],
856 'redirect_url' => $site_url . 'embed-login',
857 'is_embedded' => true,
858 ];
859
860 if ( filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
861 $iframe_url = add_query_arg( $params, $site_content_data['stSaasURL'] );
862 } else {
863 $default_url = trailingslashit( SURE_TRIGGERS_SITE_URL ) . '?path=dashboard';
864 $iframe_url = add_query_arg( $params, $default_url );
865 }
866 return esc_url_raw( $iframe_url );
867 }
868
869
870 /**
871 * Custom Filter data to check if user is logged in iframe.
872 *
873 * @return bool
874 */
875 public function suretriggers_saas_connected_data() {
876 if ( ! current_user_can( 'manage_options' ) ) {
877 return false;
878 }
879 $token = SaasApiToken::get();
880
881 if ( '' === $token || null === $token || false === $token || 'connection-denied' === $token ) {
882 $logged_in = false;
883 } else {
884 $logged_in = true;
885 }
886 return $logged_in;
887 }
888 }
889