PluginProbe
OttoKit: All-in-One Automation Platform / 1.1.20
OttoKit: All-in-One Automation Platform v1.1.20
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.20, at src/Loader.php

945 lines 33.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/?utm_source=wpplugin&utm_medium=dashboard&utm_campaign=top+bar" 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 Get OttoKit Pro link for free and pro users.
303 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
304 $plan_id = is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ? $verification_data['plan_id'] : '';
305 if ( in_array( $plan_id, [ 'free', 'pro' ], true ) ) {
306 $button_text = ( 'free' === $plan_id ) ? __( 'Get OttoKit Pro', 'suretriggers' ) : __( 'Upgrade', 'suretriggers' );
307 $upgrade_link = '<a href="https://ottokit.com/pricing/?utm_source=wpplugin&utm_medium=plugin+list&utm_campaign=plugin+list" target="_blank" style="color: #28a745; font-weight: bold;">' . $button_text . '</a>';
308 $links[] = $upgrade_link;
309 }
310
311 return $links;
312 }
313
314 /**
315 * Define constants
316 *
317 * @return void
318 * @since 1.0.0
319 */
320 public function define_constants() {
321 $sass_url = 'https://app.ottokit.com';
322 $api_url = 'https://api.ottokit.com';
323 $webhook_url = 'https://webhook.ottokit.com';
324
325 define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) );
326 define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) );
327 define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) );
328 define( 'SURE_TRIGGERS_VER', '1.1.20' );
329 define( 'SURE_TRIGGERS_DB_VER', '1.1.20' );
330 define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' );
331 define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' );
332 define( 'SURE_TRIGGERS_SITE_URL', $sass_url );
333 define( 'SURE_TRIGGERS_API_SERVER_URL', $api_url );
334 define( 'SURE_TRIGGERS_WEBHOOK_SERVER_URL', $webhook_url );
335
336 define( 'SURE_TRIGGERS_PAGE', 'OttoKit' );
337 define( 'SURE_TRIGGERS_AS_GROUP', 'OttoKit' );
338
339 define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' );
340 }
341
342 /**
343 * Flush permalink rules while plugin activation.
344 *
345 * @return void
346 */
347 public function st_activate() {
348 flush_rewrite_rules(); //phpcs:ignore
349
350 set_transient( 'st-redirect-after-activation', true, 120 );
351 }
352
353 /**
354 * SureTriggers Access for Users and User Roles.
355 *
356 * @return bool
357 */
358 private function suretriggers_user_permission_check() {
359 $current_user = wp_get_current_user();
360 $current_user_id = $current_user->ID;
361 $current_user_roles = $current_user->roles;
362
363 // Get enabled users and roles with proper default values.
364 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
365 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
366
367 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
368 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
369
370 $is_authorized = in_array( $current_user_id, (array) $enabled_users ) || array_intersect( $current_user_roles, (array) $enabled_user_roles );
371
372 return $is_authorized;
373 }
374
375 /**
376 * Add main menu
377 *
378 * @since x.x.x
379 *
380 * @return void
381 */
382 public function admin_menu() {
383 $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'OttoKit', 'suretriggers' ) );
384 $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/OttoKitLogo.svg' );
385
386 $is_authorized = true;
387 if ( ! current_user_can( 'manage_options' ) ) {
388 $is_authorized = self::suretriggers_user_permission_check();
389 }
390
391 if ( $is_authorized ) {
392 add_menu_page(
393 $page_title,
394 $page_title,
395 'read',
396 'suretriggers',
397 [ $this, 'menu_callback' ],
398 $logo ? 'data:image/svg+xml;base64,' . base64_encode( $logo ) : '',
399 30.6002
400 );
401
402 add_submenu_page(
403 'suretriggers',
404 __( 'OttoKit Status', 'suretriggers' ),
405 __( 'Status', 'suretriggers' ),
406 'read',
407 'suretriggers-status',
408 [ $this, 'suretriggers_status_menu_callback' ]
409 );
410
411 // Add Get OttoKit Pro menu for free and pro users.
412 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
413 $plan_id = is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ? $verification_data['plan_id'] : '';
414 if ( in_array( $plan_id, [ 'free', 'pro' ], true ) ) {
415 $button_text = ( 'free' === $plan_id ) ? __( 'Get OttoKit Pro', 'suretriggers' ) : __( 'Upgrade', 'suretriggers' );
416 add_submenu_page(
417 'suretriggers',
418 $button_text,
419 '<span class="ottokit-upgrade-btn">' . $button_text . '</span>',
420 'read',
421 'suretriggers-upgrade-plan',
422 [ $this, 'suretriggers_upgrade_plan_callback' ]
423 );
424 }
425 }
426
427 if ( isset( OptionController::$options['secret_key'] ) ) {
428 add_options_page(
429 __( 'OttoKit Settings', 'suretriggers' ),
430 __( 'OttoKit Settings', 'suretriggers' ),
431 'manage_options',
432 'ottokit-settings',
433 [ $this, 'suretriggers_render_interface_callback' ]
434 );
435 }
436 }
437
438 /**
439 * Enqueue the admin scripts
440 *
441 * @param string $hook hook.
442 * @since x.x.x
443 *
444 * @return void
445 */
446 public function enqueue_scripts( $hook = '' ) {
447 // Always enqueue admin CSS for upgrade button styling.
448 wp_enqueue_style( 'st-trigger-style', SURE_TRIGGERS_URL . 'assets/admin-css/st-admin-css.css', [], SURE_TRIGGERS_VER );
449
450 if ( ! in_array( $hook, [ 'toplevel_page_suretriggers', 'ottokit_page_suretriggers-status', 'settings_page_ottokit-settings' ], true ) ) {
451 return;
452 }
453
454 remove_all_actions( 'admin_notices' );
455
456 $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php';
457 if ( ! file_exists( $file ) ) {
458 return;
459 }
460
461 $asset = require_once $file;
462
463 if ( ! isset( $asset ) ) {
464 return;
465 }
466
467 wp_register_script(
468 'sure-trigger-admin',
469 SURE_TRIGGERS_URL . 'app/build/main.js',
470 array_merge( $asset['dependencies'], [ 'regenerator-runtime' ], [ 'wp-i18n' ] ),
471 $asset['version'],
472 true
473 );
474
475 wp_localize_script(
476 'sure-trigger-admin',
477 'sureTriggerData',
478 $this->get_localized_array()
479 );
480 wp_enqueue_script( 'sure-trigger-admin' );
481 // Set the script translations.
482 wp_set_script_translations( 'sure-trigger-admin', 'suretriggers', SURE_TRIGGERS_DIR . 'languages' );
483 wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER );
484 wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER );
485 }
486
487 /**
488 * Get localized array for sure triggers.
489 *
490 * @return array
491 */
492 private function get_localized_array() {
493 $settings_nonce = wp_create_nonce( 'suretriggers_settings_nonce_action' );
494 $current_user = wp_get_current_user();
495 global $wp_roles;
496 $is_authorized = true;
497 if ( ! current_user_can( 'manage_options' ) ) {
498 $is_authorized = self::suretriggers_user_permission_check();
499 }
500
501 $source_type = get_option( 'suretriggers_source' );
502
503 // Get enabled users and roles with proper default values.
504 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
505 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
506
507 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
508 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
509
510 $data = [
511 'siteContent' => [
512 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
513 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers',
514 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ),
515 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect',
516 'siteTitle' => get_bloginfo( 'name' ),
517 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ),
518 'sourceType' => $source_type,
519 'adminLanguage' => get_user_locale(),
520 ],
521 'user' => [
522 'name' => $current_user->display_name,
523 'email' => $current_user->user_email,
524 ],
525 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ),
526 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ),
527 'integrations' => IntegrationsController::get_activated_integrations(),
528 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ),
529 'verification_status' => false,
530 'projects' => [],
531 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE,
532 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ),
533 'usersRoles' => array_diff_key( $wp_roles->get_names(), [ 'administrator' => '' ] ),
534 'usersList' => get_users(
535 [
536 'fields' => [
537 'ID',
538 'display_name',
539 ],
540 'role__not_in' => [
541 'administrator',
542 ],
543 ]
544 ),
545 'enabledUsers' => $enabled_users,
546 'enabledUserRoles' => $enabled_user_roles,
547 ];
548
549 if ( $is_authorized ) {
550 $data['siteContent']['accessKey'] = SaasApiToken::get();
551 $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' );
552 }
553
554 $data['settingsNonce'] = esc_js( $settings_nonce );
555 $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php' ) );
556
557 return apply_filters( 'sure_trigger_control_localize_vars', $data );
558 }
559
560 /**
561 * Render OttoKit Interface callback.
562 *
563 * @return void
564 */
565 public function suretriggers_render_interface_callback() {
566 ?>
567 <div id="ottokit-settings-page" class="ottokit-settings"></div>
568 <?php
569 }
570
571 /**
572 * Menu callback.
573 *
574 * @since x.x.x
575 *
576 * @return void
577 */
578 public function menu_callback() {
579 // Check permalink structure first.
580 $permalink_structure = get_option( 'permalink_structure' );
581 $is_plain_permalink = empty( $permalink_structure );
582
583 // If permalink structure is "Plain" and we're trying to connect or already connected, show a prominent warning.
584 if ( $is_plain_permalink ) {
585 ?>
586 <div class="st-permalink-warning-wrapper">
587 <div class="st-permalink-warning-card">
588 <div class="st-permalink-warning-icon">
589 <span class="st-permalink-warning-icon-text">!</span>
590 </div>
591 <div class="st-permalink-warning-content">
592 <p class="st-permalink-warning-title">
593 <?php esc_html_e( '“Plain” Permalink Structure Detected', 'suretriggers' ); ?>
594 </p>
595 <p class="st-permalink-warning-message">
596 <?php esc_html_e( 'Your site is currently using the “Plain” permalink structure, which is not supported by OttoKit. Please visit', 'suretriggers' ); ?>
597 <a href="<?php echo esc_url( admin_url( 'options-permalink.php' ) ); ?>" target="_blank" class="st-permalink-warning-link">
598 <?php esc_html_e( 'Permalinks Settings', 'suretriggers' ); ?>
599 </a>
600 <?php esc_html_e( 'on your WordPress Dashboard and choose any structure other than “Plain”.', 'suretriggers' ); ?>
601 </p>
602 </div>
603 </div>
604 </div>
605 <?php
606 }
607
608 // Verify Token.
609 $response = RestController::verify_user_token();
610 $response_body = wp_remote_retrieve_body( $response );
611 $response_code = wp_remote_retrieve_response_code( $response );
612 if ( ! empty( $response_body ) ) {
613 $response_body = json_decode( $response_body, true );
614 }
615 if ( 200 === $response_code || 401 === $response_code ) {
616 if ( is_array( $response_body ) && isset( $response_body['is_iframe_enabled'] ) && 'NO' === $response_body['is_iframe_enabled'] ) {
617 ?>
618 <div class="suretriggers-nobase">
619 <div>
620 <div>
621 <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>
622 <h2 class="suretriggers-info-title">
623 <?php esc_html_e( 'OttoKit is connected.', 'suretriggers' ); ?>
624 </h2>
625 <p class="suretriggers-info-content">
626 <?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' ); ?>
627 </p>
628 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL . '/apps/WordPress' ); ?>" target="_blank">
629 <?php esc_html_e( 'Access Connection Page', 'suretriggers' ); ?>
630 </a>
631 </div>
632 </div>
633 </div>
634 <?php
635 } else {
636 ?>
637 <div id="sure-triggger-entry" class="st-base"></div>
638 <?php
639 }
640 } elseif ( isset( $response ) && is_wp_error( $response ) || 200 !== $response_code ) {
641 ?>
642 <div class="suretriggers-nobase">
643 <div>
644 <div>
645 <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>
646 <h2 class="suretriggers-info-title">
647 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
648 </h2>
649 <p class="suretriggers-info-content">
650 <?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' ); ?>
651 </p>
652 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL ); ?>" target="_blank">
653 <?php esc_html_e( 'Access Dashboard', 'suretriggers' ); ?>
654 </a>
655 <a class="suretriggers-info-link" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ); ?>">
656 <?php esc_html_e( 'Disconnect OttoKit', 'suretriggers' ); ?>
657 </a>
658 </div>
659 </div>
660 </div>
661 <?php
662 }
663 }
664
665 /**
666 * Status Menu callback.
667 *
668 * @since x.x.x
669 *
670 * @return void
671 */
672 public function suretriggers_status_menu_callback() {
673 if ( ! isset( OptionController::$options['secret_key'] ) ) {
674 ?>
675 <div class="suretriggers-nobase">
676 <div>
677 <div>
678 <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>
679 <h2 class="suretriggers-info-title">
680 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
681 </h2>
682 <p class="suretriggers-info-content">
683 <?php esc_html_e( 'Please connect your OttoKit account to access registered events and outgoing requests.', 'suretriggers' ); ?>
684 </p>
685 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="suretriggers-info-link"> <?php esc_html_e( 'Connect With OttoKit', 'suretriggers' ); ?> </a>
686 </div>
687 </div>
688 </div>
689 <?php
690 return;
691 }
692 ?>
693 <div class="wrap">
694 <?php
695 $tabs = [
696 'st_system_page' => __( 'Status', 'suretriggers' ),
697 'st_outgoing_requests' => __( 'Outgoing Requests', 'suretriggers' ),
698 'st_troubleshooting' => __( 'Troubleshooting', 'suretriggers' ),
699 ];
700 $current_tab = 'st_system_page';
701 if ( isset( $_REQUEST['tab'], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'suretriggers_tab_nonce' ) ) {
702 if ( array_key_exists( sanitize_key( $_REQUEST['tab'] ), $tabs ) ) {
703 $current_tab = sanitize_key( $_REQUEST['tab'] );
704 }
705 }
706 ?>
707 <nav class="suretriggers-nav-tab nav-tab-wrapper">
708 <?php
709 foreach ( $tabs as $name => $label ) {
710 $tab_url = add_query_arg(
711 [
712 'tab' => $name,
713 '_wpnonce' => wp_create_nonce( 'suretriggers_tab_nonce' ),
714 ],
715 admin_url( 'admin.php?page=suretriggers-status' )
716 );
717 echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab ';
718 if ( $current_tab == $name ) {
719 echo 'nav-tab-active';
720 }
721 echo '">' . esc_html( $label ) . '</a>';
722 }
723 ?>
724 </nav>
725 <?php
726 switch ( $current_tab ) {
727 case 'st_system_page':
728 include_once __DIR__ . '/Admin/Views/st-admin-system-page.php';
729 break;
730 case 'st_outgoing_requests':
731 include_once __DIR__ . '/Admin/Views/st-admin-outgoing-req-page.php';
732 break;
733 case 'st_troubleshooting':
734 include_once __DIR__ . '/Admin/Views/st-admin-troubleshooting-page.php';
735 break;
736 }
737 ?>
738 </div>
739 <?php
740 }
741
742 /**
743 * Get OttoKit Pro Menu callback.
744 *
745 * @since x.x.x
746 *
747 * @return void
748 */
749 public function suretriggers_upgrade_plan_callback() {
750 ?>
751 <script type="text/javascript">
752 window.location.href = 'https://ottokit.com/pricing/?utm_source=wpplugin&utm_medium=menu&utm_campaign=left';
753 </script>
754 <?php
755 exit;
756 }
757
758 /**
759 * Add admin menu styles.
760 *
761 * @since x.x.x
762 *
763 * @return void
764 */
765 public function add_admin_menu_styles() {
766 ?>
767 <script>
768 jQuery(document).ready(function($) {
769 // Direct redirect for admin menu upgrade button
770 $('a[href*="suretriggers-lifetime-access"]').on('click', function(e) {
771 e.preventDefault();
772 window.open('https://ottokit.com/pricing/?utm_source=wpplugin&utm_medium=menu&utm_campaign=left', '_blank');
773 return false;
774 });
775 });
776 </script>
777 <?php
778 }
779
780 /**
781 * Include all files from the folder.
782 *
783 * @param string $folder folder path.
784 * @return void
785 */
786 public function include_all_files( $folder ) {
787 $dir = new DirectoryIterator( $folder );
788 foreach ( $dir as $file ) {
789 if ( ! $file->isDot() ) {
790 if ( $file->isDir() ) {
791 $this->include_all_files( $file->getPathname() );
792 } else {
793 require_once $file->getPathname();
794 }
795 }
796 }
797 }
798
799 /**
800 * Initialize core trigger and actions.
801 *
802 * @return void
803 */
804 public function initialize_core() {
805
806 IntegrationsController::load_event_files();
807
808 EventController::get_instance();
809 IntegrationsController::get_instance();
810 GlobalSearchController::get_instance();
811 RestController::get_instance();
812 OptionController::get_instance();
813 AutomationController::get_instance();
814 AuthController::get_instance();
815 RoutesController::get_instance();
816 WebhookRequestsController::get_instance();
817 SettingsController::get_instance();
818
819 // SureTriggers Custom Filter data.
820 add_filter( 'suretriggers_get_iframe_url', [ $this, 'suretriggers_iframe_data' ] );
821 add_filter( 'suretriggers_is_user_connected', [ $this, 'suretriggers_saas_connected_data' ] );
822
823 // Create Webhook Request Log table.
824 WebhookRequestsController::suretriggers_webhook_request_log_table();
825 // Schedule the cron jon to retry failed triggers.
826 WebhookRequestsController::suretriggers_setup_custom_cron();
827
828 $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' );
829 }
830
831 /**
832 * Added option to reset plugin in case of testing.
833 *
834 * @return void
835 */
836 public function reset_plugin() {
837 $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) );
838
839 if ( $nonce && wp_verify_nonce( $nonce, 'st-reset-action' ) ) {
840 $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) );
841 if ( $is_reset && current_user_can( 'manage_options' ) ) {
842 delete_option( 'suretrigger_options' );
843 self::clear_verification_cache();
844 wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) );
845 exit();
846 }
847 }
848 }
849
850 /**
851 * Custom Filter data.
852 *
853 * @param string $site_url Optional. Site URL to include in the iframe data.
854 * @return string
855 */
856 public function suretriggers_iframe_data( $site_url = '' ) {
857 $site_url = esc_url_raw( $site_url );
858 if ( ! current_user_can( 'manage_options' ) ) {
859 return $site_url;
860 }
861 $site_content_data = [
862 'stSaasURL' => $site_url . 'wp-login',
863 'stCode' => SaasApiToken::get(),
864 'baseUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
865 'resetUrl' => rtrim( base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), '=' ), // phpcs:ignore
866 ];
867 $params = [
868 'st-code' => $site_content_data['stCode'],
869 'base_url' => $site_content_data['baseUrl'],
870 'reset_url' => $site_content_data['resetUrl'],
871 'redirect_url' => $site_url . 'embed-login',
872 'is_embedded' => true,
873 ];
874
875 if ( filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
876 $iframe_url = add_query_arg( $params, $site_content_data['stSaasURL'] );
877 } else {
878 $default_url = trailingslashit( SURE_TRIGGERS_SITE_URL ) . '?path=dashboard';
879 $iframe_url = add_query_arg( $params, $default_url );
880 }
881 return esc_url_raw( $iframe_url );
882 }
883
884
885 /**
886 * Custom Filter data to check if user is logged in iframe.
887 *
888 * @return bool
889 */
890 public function suretriggers_saas_connected_data() {
891 if ( ! current_user_can( 'manage_options' ) ) {
892 return false;
893 }
894 $token = SaasApiToken::get();
895
896 if ( '' === $token || null === $token || false === $token || 'connection-denied' === $token ) {
897 $logged_in = false;
898 } else {
899 $logged_in = true;
900 }
901 return $logged_in;
902 }
903
904 /**
905 * Check if user has lifetime plan
906 *
907 * @return bool
908 */
909 public function is_lifetime_plan() {
910 // Check if verification data is already stored in database.
911 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
912 // Return lifetime plan status from cached data.
913 if ( is_array( $verification_data ) && isset( $verification_data['is_lifetime_plan'] ) ) {
914 return (bool) $verification_data['is_lifetime_plan'];
915 }
916
917 return false;
918 }
919
920 /**
921 * Check if user should see upgrade button (free or pro plan)
922 *
923 * @return bool
924 */
925 public function should_show_upgrade_button() {
926 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
927
928 if ( is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ) {
929 $plan_id = $verification_data['plan_id'];
930 return in_array( $plan_id, [ 'free', 'pro' ], true );
931 }
932
933 return false;
934 }
935
936 /**
937 * Clear cached user verification data when token changes
938 *
939 * @return void
940 */
941 public static function clear_verification_cache() {
942 delete_option( 'suretriggers_lifetime_user_plan_data' );
943 }
944 }
945