PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.21
OttoKit: All-in-One Automation Platform v1.1.21
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 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Loader.php
suretriggers / src Last commit date
Abilities 3 months ago Admin 3 months ago Controllers 3 months ago Integrations 3 months ago Models 9 months ago Support 1 year ago Traits 3 years ago Loader.php 3 months ago
Loader.php
947 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\WebhookRequestsController;
26 use SureTriggers\Controllers\SettingsController;
27 use SureTriggers\Traits\SingletonLoader;
28 use SureTriggers\Models\SaasApiToken;
29 use SureTriggers\Abilities\AbilitiesController;
30 use function add_menu_page;
31 use function add_submenu_page;
32
33 /**
34 * Loader
35 *
36 * @category Loader
37 * @package SureTriggers
38 * @author BSF <username@example.com>
39 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
40 * @link https://www.brainstormforce.com/
41 * @since 1.0.0
42 */
43 class Loader {
44
45
46
47 use SingletonLoader;
48
49 /**
50 * Constructor
51 *
52 * @since 1.0.0
53 */
54 public function __construct() {
55 register_activation_hook( SURE_TRIGGERS_FILE, [ $this, 'st_activate' ] );
56
57 $this->define_constants();
58 add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] );
59 add_action( 'plugins_loaded', [ $this, 'initialize_core' ] );
60 // Admin Menu.
61 add_action( 'admin_menu', [ $this, 'admin_menu' ] );
62 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
63 add_action( 'admin_head', [ $this, 'add_admin_menu_styles' ] );
64 add_action( 'admin_init', [ $this, 'reset_plugin' ] );
65
66 add_filter( 'plugin_action_links_' . plugin_basename( SURE_TRIGGERS_FILE ), [ $this, 'add_settings_link' ] );
67 add_action( 'admin_init', [ $this, 'redirect_after_activation' ] );
68
69 add_action( 'admin_notices', [ $this, 'display_notice' ] );
70
71 add_action( 'all_admin_notices', [ $this, 'suretriggers_show_api_connection_error' ] );
72
73 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] );
74
75 // Remove Webhook Requests retry cron and requests table.
76 register_uninstall_hook(
77 SURE_TRIGGERS_FILE,
78 [ WebhookRequestsController::class, 'suretriggers_remove_table_retry_cron' ]
79 );
80 }
81
82 /**
83 * Adding dashboard widget.
84 *
85 * @return void
86 */
87 public function add_dashboard_widgets() {
88 if ( isset( OptionController::$options['secret_key'] ) ) {
89 return;
90 }
91
92 wp_add_dashboard_widget(
93 'suretriggers_dashboard_widget',
94 __( 'Stop Doing It Manually!', 'suretriggers' ),
95 [ $this, 'dashboard_widget_display' ],
96 null,
97 null,
98 'side',
99 'high'
100 );
101 }
102
103 /**
104 * Dashboard widget callback.
105 *
106 * @return void
107 */
108 public function dashboard_widget_display() { ?>
109 <div>
110 <p> <?php esc_html_e( 'Automation That’s Easy Enough for Anyone.', 'suretriggers' ); ?></p>
111 <p>
112 <?php
113 esc_html_e(
114 'OttoKit connects all your tools - WooCommerce, Forms, CRM, and more, so your website runs smoothly while you focus on your business.
115 ',
116 'suretriggers'
117 );
118 ?>
119 </p>
120 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Start Automating', 'suretriggers' ); ?> </a>
121 </div>
122 <?php
123 }
124
125 /**
126 * Load Plugin Text Domain.
127 * This will load the translation textdomain depending on the file priorities.
128 * 1. Global Languages /wp-content/languages/suretriggers/ folder
129 * 2. Local directory /wp-content/plugins/suretriggers/languages/ folder
130 *
131 * @return void
132 */
133 public function load_textdomain() {
134 // Default languages directory.
135 $lang_dir = SURE_TRIGGERS_DIR . 'languages/';
136
137 /**
138 * Filters the languages directory path to use for plugin.
139 *
140 * @param string $lang_dir The languages directory path.
141 */
142 $lang_dir = apply_filters( 'suretriggers_languages_directory', $lang_dir );
143
144 // Traditional WordPress plugin locale filter.
145 global $wp_version;
146
147 $get_locale = get_locale();
148
149 if ( $wp_version >= 4.7 ) {
150 $get_locale = get_user_locale();
151 }
152
153 /**
154 * Language Locale for plugin
155 *
156 * Uses get_user_locale()` in WordPress 4.7 or greater,
157 * otherwise uses `get_locale()`.
158 */
159 $locale = apply_filters( 'plugin_locale', $get_locale, 'suretriggers' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook
160 $mofile = sprintf( '%1$s-%2$s.mo', 'suretriggers', $locale );
161
162 // Setup paths to current locale file.
163 $mofile_global = WP_LANG_DIR . '/plugins/' . $mofile;
164 $mofile_local = $lang_dir . $mofile;
165
166 $mofile_global = wp_normalize_path( $mofile_global );
167 $mofile_local = wp_normalize_path( $mofile_local );
168
169 if ( file_exists( $mofile_global ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) ) {
170 // Look in global /wp-content/languages/suretriggers/ folder.
171 load_textdomain( 'suretriggers', $mofile_global );
172 } elseif ( file_exists( $mofile_local ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) && $this->st_valid_file_path( $mofile_local ) ) {
173 // Look in local /wp-content/plugins/suretriggers/languages/ folder.
174 load_textdomain( 'suretriggers', $mofile_local );
175 } else {
176 load_plugin_textdomain( 'suretriggers', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
177 }
178 }
179
180 /**
181 * Check if file is valid.
182 *
183 * @param string $file_path file_path.
184 * @return bool
185 */
186 private function st_valid_file_path( $file_path ) {
187 $allowed_dir = realpath( SURE_TRIGGERS_DIR . 'languages/' );
188 $real_file_path = realpath( $file_path );
189 return false !== $real_file_path && $allowed_dir && 0 === strpos( $real_file_path, $allowed_dir );
190 }
191
192 /**
193 * Display notice.
194 *
195 * @return void
196 */
197 public function display_notice() {
198 global $pagenow;
199 if ( isset( OptionController::$options['secret_key'] ) ) {
200 return;
201 }
202 if ( 'index.php' != $pagenow ) {
203 return;
204 }
205 ?>
206 <div class="notice notice-success" style="padding-bottom: 15px;">
207 <p>
208 <strong>
209 <?php esc_html_e( 'Automate Your WordPress Site. Save Hours. Earn More.', 'suretriggers' ); ?>
210 <span style="transform: rotate(-90deg); font-size: 15px;" class="dashicons dashicons-admin-plugins"></span>
211 </strong>
212 </p>
213 <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>
214
215 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Start Automating', 'suretriggers' ); ?> </a>
216 <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>
217 </div>
218 <?php
219 }
220
221 /**
222 * Show Connection Error Admin Notice.
223 *
224 * @return void
225 */
226 public function suretriggers_show_api_connection_error() {
227 global $pagenow;
228 $is_authorized = true;
229 if ( ! current_user_can( 'manage_options' ) ) {
230 $is_authorized = self::suretriggers_user_permission_check();
231 }
232 if ( 'index.php' != $pagenow || ! isset( OptionController::$options['secret_key'] ) || ! $is_authorized ) {
233 return;
234 }
235 $notice = get_option( 'suretriggers_verify_connection' );
236 // If empty option value for connection status, then verify the connection.
237 if ( empty( $notice ) || 'suretriggers_connection_successful' != $notice ) {
238 $connection_status = RestController::suretriggers_verify_wp_connection();
239 $connection_status_code = wp_remote_retrieve_response_code( $connection_status );
240 if ( is_wp_error( $connection_status ) ) {
241 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_wp_error' );
242 } else {
243 if ( 200 !== $connection_status_code ) {
244 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_error' );
245 } else {
246 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_successful' );
247 }
248 }
249 }
250 $notice = get_option( 'suretriggers_verify_connection' );
251 if ( 'suretriggers_connection_successful' != $notice ) {
252 // If connection status is not successful, then show the notice.
253 ?>
254 <div class="notice notice-error is-dismissible">
255 <p>
256 <strong>
257 <?php esc_html_e( 'OttoKit Connection Issue', 'suretriggers' ); ?>
258 <span style="transform: rotate(-180deg); font-size: 20px;" class="dashicons dashicons-warning"></span>
259 </strong>
260 </p>
261 <p>
262 <?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' ); ?>
263 </p>
264 <p>
265 <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>
266 </p>
267 </div>
268 <?php
269 }
270 }
271
272 /**
273 * Redirect user after plugin activation.
274 *
275 * @return void
276 */
277 public function redirect_after_activation() {
278 $is_redirect = get_transient( 'st-redirect-after-activation' );
279 if ( $is_redirect ) {
280 delete_transient( 'st-redirect-after-activation' );
281 $url = get_admin_url() . 'admin.php?page=suretriggers';
282 wp_safe_redirect( $url );
283 die;
284 }
285 }
286
287 /**
288 * Adding setting link.
289 *
290 * @param array $links links.
291 * @return array
292 */
293 public function add_settings_link( array $links ) {
294 $url = get_admin_url() . 'admin.php?page=suretriggers';
295 $setting_option = get_option( 'suretrigger_options' );
296 if ( isset( $setting_option ) && ! empty( $setting_option ) ) {
297 $settings_link = '<a href="' . $url . '">' . __( 'Dashboard', 'suretriggers' ) . '</a>';
298 } else {
299 $settings_link = '<a href="' . $url . '">' . __( 'Connect', 'suretriggers' ) . '</a>';
300 }
301 $links[] = $settings_link;
302
303 // Add Get OttoKit Pro link for free and pro users.
304 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
305 $plan_id = is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ? $verification_data['plan_id'] : '';
306 if ( in_array( $plan_id, [ 'free', 'pro' ], true ) ) {
307 $button_text = ( 'free' === $plan_id ) ? __( 'Get OttoKit Pro', 'suretriggers' ) : __( 'Upgrade', 'suretriggers' );
308 $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>';
309 $links[] = $upgrade_link;
310 }
311
312 return $links;
313 }
314
315 /**
316 * Define constants
317 *
318 * @return void
319 * @since 1.0.0
320 */
321 public function define_constants() {
322 $sass_url = 'https://app.ottokit.com';
323 $api_url = 'https://api.ottokit.com';
324 $webhook_url = 'https://webhook.ottokit.com';
325
326 define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) );
327 define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) );
328 define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) );
329 define( 'SURE_TRIGGERS_VER', '1.1.21' );
330 define( 'SURE_TRIGGERS_DB_VER', '1.1.21' );
331 define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' );
332 define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' );
333 define( 'SURE_TRIGGERS_SITE_URL', $sass_url );
334 define( 'SURE_TRIGGERS_API_SERVER_URL', $api_url );
335 define( 'SURE_TRIGGERS_WEBHOOK_SERVER_URL', $webhook_url );
336
337 define( 'SURE_TRIGGERS_PAGE', 'OttoKit' );
338 define( 'SURE_TRIGGERS_AS_GROUP', 'OttoKit' );
339
340 define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' );
341 }
342
343 /**
344 * Flush permalink rules while plugin activation.
345 *
346 * @return void
347 */
348 public function st_activate() {
349 flush_rewrite_rules(); //phpcs:ignore
350
351 set_transient( 'st-redirect-after-activation', true, 120 );
352 }
353
354 /**
355 * SureTriggers Access for Users and User Roles.
356 *
357 * @return bool
358 */
359 private function suretriggers_user_permission_check() {
360 $current_user = wp_get_current_user();
361 $current_user_id = $current_user->ID;
362 $current_user_roles = $current_user->roles;
363
364 // Get enabled users and roles with proper default values.
365 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
366 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
367
368 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
369 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
370
371 $is_authorized = in_array( $current_user_id, (array) $enabled_users ) || array_intersect( $current_user_roles, (array) $enabled_user_roles );
372
373 return $is_authorized;
374 }
375
376 /**
377 * Add main menu
378 *
379 * @since x.x.x
380 *
381 * @return void
382 */
383 public function admin_menu() {
384 $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'OttoKit', 'suretriggers' ) );
385 $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/OttoKitLogo.svg' );
386
387 $is_authorized = true;
388 if ( ! current_user_can( 'manage_options' ) ) {
389 $is_authorized = self::suretriggers_user_permission_check();
390 }
391
392 if ( $is_authorized ) {
393 add_menu_page(
394 $page_title,
395 $page_title,
396 'read',
397 'suretriggers',
398 [ $this, 'menu_callback' ],
399 $logo ? 'data:image/svg+xml;base64,' . base64_encode( $logo ) : '',
400 30.6002
401 );
402
403 add_submenu_page(
404 'suretriggers',
405 __( 'OttoKit Status', 'suretriggers' ),
406 __( 'Status', 'suretriggers' ),
407 'read',
408 'suretriggers-status',
409 [ $this, 'suretriggers_status_menu_callback' ]
410 );
411
412 // Add Get OttoKit Pro menu for free and pro users.
413 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
414 $plan_id = is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ? $verification_data['plan_id'] : '';
415 if ( in_array( $plan_id, [ 'free', 'pro' ], true ) ) {
416 $button_text = ( 'free' === $plan_id ) ? __( 'Get OttoKit Pro', 'suretriggers' ) : __( 'Upgrade', 'suretriggers' );
417 add_submenu_page(
418 'suretriggers',
419 $button_text,
420 '<span class="ottokit-upgrade-btn">' . $button_text . '</span>',
421 'read',
422 'suretriggers-upgrade-plan',
423 [ $this, 'suretriggers_upgrade_plan_callback' ]
424 );
425 }
426 }
427
428 if ( isset( OptionController::$options['secret_key'] ) ) {
429 add_options_page(
430 __( 'OttoKit Settings', 'suretriggers' ),
431 __( 'OttoKit Settings', 'suretriggers' ),
432 'manage_options',
433 'ottokit-settings',
434 [ $this, 'suretriggers_render_interface_callback' ]
435 );
436 }
437 }
438
439 /**
440 * Enqueue the admin scripts
441 *
442 * @param string $hook hook.
443 * @since x.x.x
444 *
445 * @return void
446 */
447 public function enqueue_scripts( $hook = '' ) {
448 // Always enqueue admin CSS for upgrade button styling.
449 wp_enqueue_style( 'st-trigger-style', SURE_TRIGGERS_URL . 'assets/admin-css/st-admin-css.css', [], SURE_TRIGGERS_VER );
450
451 if ( ! in_array( $hook, [ 'toplevel_page_suretriggers', 'ottokit_page_suretriggers-status', 'settings_page_ottokit-settings' ], true ) ) {
452 return;
453 }
454
455 remove_all_actions( 'admin_notices' );
456
457 $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php';
458 if ( ! file_exists( $file ) ) {
459 return;
460 }
461
462 $asset = require_once $file;
463
464 if ( ! isset( $asset ) ) {
465 return;
466 }
467
468 wp_register_script(
469 'sure-trigger-admin',
470 SURE_TRIGGERS_URL . 'app/build/main.js',
471 array_merge( $asset['dependencies'], [ 'regenerator-runtime' ], [ 'wp-i18n' ] ),
472 $asset['version'],
473 true
474 );
475
476 wp_localize_script(
477 'sure-trigger-admin',
478 'sureTriggerData',
479 $this->get_localized_array()
480 );
481 wp_enqueue_script( 'sure-trigger-admin' );
482 // Set the script translations.
483 wp_set_script_translations( 'sure-trigger-admin', 'suretriggers', SURE_TRIGGERS_DIR . 'languages' );
484 wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER );
485 wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER );
486 }
487
488 /**
489 * Get localized array for sure triggers.
490 *
491 * @return array
492 */
493 private function get_localized_array() {
494 $settings_nonce = wp_create_nonce( 'suretriggers_settings_nonce_action' );
495 $current_user = wp_get_current_user();
496 global $wp_roles;
497 $is_authorized = true;
498 if ( ! current_user_can( 'manage_options' ) ) {
499 $is_authorized = self::suretriggers_user_permission_check();
500 }
501
502 $source_type = get_option( 'suretriggers_source' );
503
504 // Get enabled users and roles with proper default values.
505 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
506 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
507
508 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
509 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
510
511 $data = [
512 'siteContent' => [
513 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
514 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers',
515 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ),
516 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect',
517 'siteTitle' => get_bloginfo( 'name' ),
518 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ),
519 'sourceType' => $source_type,
520 'adminLanguage' => get_user_locale(),
521 ],
522 'user' => [
523 'name' => $current_user->display_name,
524 'email' => $current_user->user_email,
525 ],
526 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ),
527 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ),
528 'integrations' => IntegrationsController::get_activated_integrations(),
529 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ),
530 'verification_status' => false,
531 'projects' => [],
532 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE,
533 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ),
534 'usersRoles' => array_diff_key( $wp_roles->get_names(), [ 'administrator' => '' ] ),
535 'usersList' => get_users(
536 [
537 'fields' => [
538 'ID',
539 'display_name',
540 ],
541 'role__not_in' => [
542 'administrator',
543 ],
544 ]
545 ),
546 'enabledUsers' => $enabled_users,
547 'enabledUserRoles' => $enabled_user_roles,
548 ];
549
550 if ( $is_authorized ) {
551 $data['siteContent']['accessKey'] = SaasApiToken::get();
552 $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' );
553 }
554
555 $data['settingsNonce'] = esc_js( $settings_nonce );
556 $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php' ) );
557
558 return apply_filters( 'sure_trigger_control_localize_vars', $data );
559 }
560
561 /**
562 * Render OttoKit Interface callback.
563 *
564 * @return void
565 */
566 public function suretriggers_render_interface_callback() {
567 ?>
568 <div id="ottokit-settings-page" class="ottokit-settings"></div>
569 <?php
570 }
571
572 /**
573 * Menu callback.
574 *
575 * @since x.x.x
576 *
577 * @return void
578 */
579 public function menu_callback() {
580 // Check permalink structure first.
581 $permalink_structure = get_option( 'permalink_structure' );
582 $is_plain_permalink = empty( $permalink_structure );
583
584 // If permalink structure is "Plain" and we're trying to connect or already connected, show a prominent warning.
585 if ( $is_plain_permalink ) {
586 ?>
587 <div class="st-permalink-warning-wrapper">
588 <div class="st-permalink-warning-card">
589 <div class="st-permalink-warning-icon">
590 <span class="st-permalink-warning-icon-text">!</span>
591 </div>
592 <div class="st-permalink-warning-content">
593 <p class="st-permalink-warning-title">
594 <?php esc_html_e( '“Plain” Permalink Structure Detected', 'suretriggers' ); ?>
595 </p>
596 <p class="st-permalink-warning-message">
597 <?php esc_html_e( 'Your site is currently using the “Plain” permalink structure, which is not supported by OttoKit. Please visit', 'suretriggers' ); ?>
598 <a href="<?php echo esc_url( admin_url( 'options-permalink.php' ) ); ?>" target="_blank" class="st-permalink-warning-link">
599 <?php esc_html_e( 'Permalinks Settings', 'suretriggers' ); ?>
600 </a>
601 <?php esc_html_e( 'on your WordPress Dashboard and choose any structure other than “Plain”.', 'suretriggers' ); ?>
602 </p>
603 </div>
604 </div>
605 </div>
606 <?php
607 }
608
609 // Verify Token.
610 $response = RestController::verify_user_token();
611 $response_body = wp_remote_retrieve_body( $response );
612 $response_code = wp_remote_retrieve_response_code( $response );
613 if ( ! empty( $response_body ) ) {
614 $response_body = json_decode( $response_body, true );
615 }
616 if ( 200 === $response_code || 401 === $response_code ) {
617 if ( is_array( $response_body ) && isset( $response_body['is_iframe_enabled'] ) && 'NO' === $response_body['is_iframe_enabled'] ) {
618 ?>
619 <div class="suretriggers-nobase">
620 <div>
621 <div>
622 <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>
623 <h2 class="suretriggers-info-title">
624 <?php esc_html_e( 'OttoKit is connected.', 'suretriggers' ); ?>
625 </h2>
626 <p class="suretriggers-info-content">
627 <?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' ); ?>
628 </p>
629 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL . '/apps/WordPress' ); ?>" target="_blank">
630 <?php esc_html_e( 'Access Connection Page', 'suretriggers' ); ?>
631 </a>
632 </div>
633 </div>
634 </div>
635 <?php
636 } else {
637 ?>
638 <div id="sure-triggger-entry" class="st-base"></div>
639 <?php
640 }
641 } elseif ( isset( $response ) && is_wp_error( $response ) || 200 !== $response_code ) {
642 ?>
643 <div class="suretriggers-nobase">
644 <div>
645 <div>
646 <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>
647 <h2 class="suretriggers-info-title">
648 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
649 </h2>
650 <p class="suretriggers-info-content">
651 <?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' ); ?>
652 </p>
653 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL ); ?>" target="_blank">
654 <?php esc_html_e( 'Access Dashboard', 'suretriggers' ); ?>
655 </a>
656 <a class="suretriggers-info-link" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ); ?>">
657 <?php esc_html_e( 'Disconnect OttoKit', 'suretriggers' ); ?>
658 </a>
659 </div>
660 </div>
661 </div>
662 <?php
663 }
664 }
665
666 /**
667 * Status Menu callback.
668 *
669 * @since x.x.x
670 *
671 * @return void
672 */
673 public function suretriggers_status_menu_callback() {
674 if ( ! isset( OptionController::$options['secret_key'] ) ) {
675 ?>
676 <div class="suretriggers-nobase">
677 <div>
678 <div>
679 <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>
680 <h2 class="suretriggers-info-title">
681 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
682 </h2>
683 <p class="suretriggers-info-content">
684 <?php esc_html_e( 'Please connect your OttoKit account to access registered events and outgoing requests.', 'suretriggers' ); ?>
685 </p>
686 <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>
687 </div>
688 </div>
689 </div>
690 <?php
691 return;
692 }
693 ?>
694 <div class="wrap">
695 <?php
696 $tabs = [
697 'st_system_page' => __( 'Status', 'suretriggers' ),
698 'st_outgoing_requests' => __( 'Outgoing Requests', 'suretriggers' ),
699 'st_troubleshooting' => __( 'Troubleshooting', 'suretriggers' ),
700 ];
701 $current_tab = 'st_system_page';
702 if ( isset( $_REQUEST['tab'], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'suretriggers_tab_nonce' ) ) {
703 if ( array_key_exists( sanitize_key( $_REQUEST['tab'] ), $tabs ) ) {
704 $current_tab = sanitize_key( $_REQUEST['tab'] );
705 }
706 }
707 ?>
708 <nav class="suretriggers-nav-tab nav-tab-wrapper">
709 <?php
710 foreach ( $tabs as $name => $label ) {
711 $tab_url = add_query_arg(
712 [
713 'tab' => $name,
714 '_wpnonce' => wp_create_nonce( 'suretriggers_tab_nonce' ),
715 ],
716 admin_url( 'admin.php?page=suretriggers-status' )
717 );
718 echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab ';
719 if ( $current_tab == $name ) {
720 echo 'nav-tab-active';
721 }
722 echo '">' . esc_html( $label ) . '</a>';
723 }
724 ?>
725 </nav>
726 <?php
727 switch ( $current_tab ) {
728 case 'st_system_page':
729 include_once __DIR__ . '/Admin/Views/st-admin-system-page.php';
730 break;
731 case 'st_outgoing_requests':
732 include_once __DIR__ . '/Admin/Views/st-admin-outgoing-req-page.php';
733 break;
734 case 'st_troubleshooting':
735 include_once __DIR__ . '/Admin/Views/st-admin-troubleshooting-page.php';
736 break;
737 }
738 ?>
739 </div>
740 <?php
741 }
742
743 /**
744 * Get OttoKit Pro Menu callback.
745 *
746 * @since x.x.x
747 *
748 * @return void
749 */
750 public function suretriggers_upgrade_plan_callback() {
751 ?>
752 <script type="text/javascript">
753 window.location.href = 'https://ottokit.com/pricing/?utm_source=wpplugin&utm_medium=menu&utm_campaign=left';
754 </script>
755 <?php
756 exit;
757 }
758
759 /**
760 * Add admin menu styles.
761 *
762 * @since x.x.x
763 *
764 * @return void
765 */
766 public function add_admin_menu_styles() {
767 ?>
768 <script>
769 jQuery(document).ready(function($) {
770 // Direct redirect for admin menu upgrade button
771 $('a[href*="suretriggers-lifetime-access"]').on('click', function(e) {
772 e.preventDefault();
773 window.open('https://ottokit.com/pricing/?utm_source=wpplugin&utm_medium=menu&utm_campaign=left', '_blank');
774 return false;
775 });
776 });
777 </script>
778 <?php
779 }
780
781 /**
782 * Include all files from the folder.
783 *
784 * @param string $folder folder path.
785 * @return void
786 */
787 public function include_all_files( $folder ) {
788 $dir = new DirectoryIterator( $folder );
789 foreach ( $dir as $file ) {
790 if ( ! $file->isDot() ) {
791 if ( $file->isDir() ) {
792 $this->include_all_files( $file->getPathname() );
793 } else {
794 require_once $file->getPathname();
795 }
796 }
797 }
798 }
799
800 /**
801 * Initialize core trigger and actions.
802 *
803 * @return void
804 */
805 public function initialize_core() {
806
807 IntegrationsController::load_event_files();
808
809 EventController::get_instance();
810 IntegrationsController::get_instance();
811 GlobalSearchController::get_instance();
812 RestController::get_instance();
813 OptionController::get_instance();
814 AutomationController::get_instance();
815 AuthController::get_instance();
816 RoutesController::get_instance();
817 WebhookRequestsController::get_instance();
818 SettingsController::get_instance();
819 AbilitiesController::get_instance();
820
821 // SureTriggers Custom Filter data.
822 add_filter( 'suretriggers_get_iframe_url', [ $this, 'suretriggers_iframe_data' ] );
823 add_filter( 'suretriggers_is_user_connected', [ $this, 'suretriggers_saas_connected_data' ] );
824
825 // Create Webhook Request Log table.
826 WebhookRequestsController::suretriggers_webhook_request_log_table();
827 // Schedule the cron jon to retry failed triggers.
828 WebhookRequestsController::suretriggers_setup_custom_cron();
829
830 $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' );
831 }
832
833 /**
834 * Added option to reset plugin in case of testing.
835 *
836 * @return void
837 */
838 public function reset_plugin() {
839 $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) );
840
841 if ( $nonce && wp_verify_nonce( $nonce, 'st-reset-action' ) ) {
842 $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) );
843 if ( $is_reset && current_user_can( 'manage_options' ) ) {
844 delete_option( 'suretrigger_options' );
845 self::clear_verification_cache();
846 wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) );
847 exit();
848 }
849 }
850 }
851
852 /**
853 * Custom Filter data.
854 *
855 * @param string $site_url Optional. Site URL to include in the iframe data.
856 * @return string
857 */
858 public function suretriggers_iframe_data( $site_url = '' ) {
859 $site_url = esc_url_raw( $site_url );
860 if ( ! current_user_can( 'manage_options' ) ) {
861 return $site_url;
862 }
863 $site_content_data = [
864 'stSaasURL' => $site_url . 'wp-login',
865 'stCode' => SaasApiToken::get(),
866 'baseUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
867 'resetUrl' => rtrim( base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), '=' ), // phpcs:ignore
868 ];
869 $params = [
870 'st-code' => $site_content_data['stCode'],
871 'base_url' => $site_content_data['baseUrl'],
872 'reset_url' => $site_content_data['resetUrl'],
873 'redirect_url' => $site_url . 'embed-login',
874 'is_embedded' => true,
875 ];
876
877 if ( filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
878 $iframe_url = add_query_arg( $params, $site_content_data['stSaasURL'] );
879 } else {
880 $default_url = trailingslashit( SURE_TRIGGERS_SITE_URL ) . '?path=dashboard';
881 $iframe_url = add_query_arg( $params, $default_url );
882 }
883 return esc_url_raw( $iframe_url );
884 }
885
886
887 /**
888 * Custom Filter data to check if user is logged in iframe.
889 *
890 * @return bool
891 */
892 public function suretriggers_saas_connected_data() {
893 if ( ! current_user_can( 'manage_options' ) ) {
894 return false;
895 }
896 $token = SaasApiToken::get();
897
898 if ( '' === $token || null === $token || false === $token || 'connection-denied' === $token ) {
899 $logged_in = false;
900 } else {
901 $logged_in = true;
902 }
903 return $logged_in;
904 }
905
906 /**
907 * Check if user has lifetime plan
908 *
909 * @return bool
910 */
911 public function is_lifetime_plan() {
912 // Check if verification data is already stored in database.
913 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
914 // Return lifetime plan status from cached data.
915 if ( is_array( $verification_data ) && isset( $verification_data['is_lifetime_plan'] ) ) {
916 return (bool) $verification_data['is_lifetime_plan'];
917 }
918
919 return false;
920 }
921
922 /**
923 * Check if user should see upgrade button (free or pro plan)
924 *
925 * @return bool
926 */
927 public function should_show_upgrade_button() {
928 $verification_data = get_option( 'suretriggers_lifetime_user_plan_data' );
929
930 if ( is_array( $verification_data ) && isset( $verification_data['plan_id'] ) ) {
931 $plan_id = $verification_data['plan_id'];
932 return in_array( $plan_id, [ 'free', 'pro' ], true );
933 }
934
935 return false;
936 }
937
938 /**
939 * Clear cached user verification data when token changes
940 *
941 * @return void
942 */
943 public static function clear_verification_cache() {
944 delete_option( 'suretriggers_lifetime_user_plan_data' );
945 }
946 }
947