PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.5
OttoKit: All-in-One Automation Platform v1.1.5
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
Admin 10 months ago Controllers 9 months ago Integrations 9 months ago Models 9 months ago Support 1 year ago Traits 3 years ago Loader.php 9 months ago
Loader.php
829 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 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_init', [ $this, 'reset_plugin' ] );
63
64 add_filter( 'plugin_action_links_' . plugin_basename( SURE_TRIGGERS_FILE ), [ $this, 'add_settings_link' ] );
65 add_action( 'admin_init', [ $this, 'redirect_after_activation' ] );
66
67 add_action( 'admin_notices', [ $this, 'display_notice' ] );
68
69 add_action( 'all_admin_notices', [ $this, 'suretriggers_show_api_connection_error' ] );
70
71 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] );
72
73 // Remove Webhook Requests retry cron and requests table.
74 register_uninstall_hook(
75 SURE_TRIGGERS_FILE,
76 [ WebhookRequestsController::class, 'suretriggers_remove_table_retry_cron' ]
77 );
78 }
79
80 /**
81 * Adding dashboard widget.
82 *
83 * @return void
84 */
85 public function add_dashboard_widgets() {
86 if ( isset( OptionController::$options['secret_key'] ) ) {
87 return;
88 }
89
90 wp_add_dashboard_widget(
91 'suretriggers_dashboard_widget',
92 __( 'Please Connect OttoKit (Formerly SureTriggers)', 'suretriggers' ),
93 [ $this, 'dashboard_widget_display' ],
94 null,
95 null,
96 'side',
97 'high'
98 );
99 }
100
101 /**
102 * Dashboard widget callback.
103 *
104 * @return void
105 */
106 public function dashboard_widget_display() { ?>
107 <div>
108 <p> <?php esc_html_e( 'Please connect to or create your OttoKit (Formerly SureTriggers) account.', 'suretriggers' ); ?></p>
109 <p> <?php esc_html_e( 'This will enable you to connect your various plugins, and apps together and automate repetitive tasks.', 'suretriggers' ); ?> </p>
110 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Get Started', 'suretriggers' ); ?> </a>
111 </div>
112 <?php
113 }
114
115 /**
116 * Load Plugin Text Domain.
117 * This will load the translation textdomain depending on the file priorities.
118 * 1. Global Languages /wp-content/languages/suretriggers/ folder
119 * 2. Local directory /wp-content/plugins/suretriggers/languages/ folder
120 *
121 * @return void
122 */
123 public function load_textdomain() {
124 // Default languages directory.
125 $lang_dir = SURE_TRIGGERS_DIR . 'languages/';
126
127 /**
128 * Filters the languages directory path to use for plugin.
129 *
130 * @param string $lang_dir The languages directory path.
131 */
132 $lang_dir = apply_filters( 'suretriggers_languages_directory', $lang_dir );
133
134 // Traditional WordPress plugin locale filter.
135 global $wp_version;
136
137 $get_locale = get_locale();
138
139 if ( $wp_version >= 4.7 ) {
140 $get_locale = get_user_locale();
141 }
142
143 /**
144 * Language Locale for plugin
145 *
146 * Uses get_user_locale()` in WordPress 4.7 or greater,
147 * otherwise uses `get_locale()`.
148 */
149 $locale = apply_filters( 'plugin_locale', $get_locale, 'suretriggers' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook
150 $mofile = sprintf( '%1$s-%2$s.mo', 'suretriggers', $locale );
151
152 // Setup paths to current locale file.
153 $mofile_global = WP_LANG_DIR . '/plugins/' . $mofile;
154 $mofile_local = $lang_dir . $mofile;
155
156 $mofile_global = wp_normalize_path( $mofile_global );
157 $mofile_local = wp_normalize_path( $mofile_local );
158
159 if ( file_exists( $mofile_global ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) ) {
160 // Look in global /wp-content/languages/suretriggers/ folder.
161 load_textdomain( 'suretriggers', $mofile_global );
162 } elseif ( file_exists( $mofile_local ) && wp_is_file_mod_allowed( 'load_textdomain_mofile' ) && $this->st_valid_file_path( $mofile_local ) ) {
163 // Look in local /wp-content/plugins/suretriggers/languages/ folder.
164 load_textdomain( 'suretriggers', $mofile_local );
165 } else {
166 load_plugin_textdomain( 'suretriggers', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
167 }
168 }
169
170 /**
171 * Check if file is valid.
172 *
173 * @param string $file_path file_path.
174 * @return bool
175 */
176 private function st_valid_file_path( $file_path ) {
177 $allowed_dir = realpath( SURE_TRIGGERS_DIR . 'languages/' );
178 $real_file_path = realpath( $file_path );
179 return false !== $real_file_path && $allowed_dir && 0 === strpos( $real_file_path, $allowed_dir );
180 }
181
182 /**
183 * Display notice.
184 *
185 * @return void
186 */
187 public function display_notice() {
188 global $pagenow;
189 if ( isset( OptionController::$options['secret_key'] ) ) {
190 return;
191 }
192 if ( 'index.php' != $pagenow ) {
193 return;
194 }
195 ?>
196 <div class="notice notice-success" style="padding-bottom: 15px;">
197 <p>
198 <strong>
199 <?php esc_html_e( 'Connect your plugins and apps together with OttoKit (Formerly SureTriggers)', 'suretriggers' ); ?>
200 <span style="transform: rotate(-90deg); font-size: 15px;" class="dashicons dashicons-admin-plugins"></span>
201 </strong>
202 </p>
203 <p> <?php esc_html_e( 'Please connect to or create your OttoKit (Formerly SureTriggers) account. This will enable you to connect your various plugins and apps together and automate repetitive tasks.', 'suretriggers' ); ?> </p>
204
205 <a href="<?php echo esc_url( admin_url( 'admin.php?page=suretriggers' ) ); ?>" class="button button-primary"> <?php esc_html_e( 'Get Started With OttoKit (Formerly SureTriggers)', 'suretriggers' ); ?> </a>
206 <a href="https://ottokit.com/" class="button button-secondary"> <?php esc_html_e( 'Learn More', 'suretriggers' ); ?> </a>
207 </div>
208 <?php
209 }
210
211 /**
212 * Show Connection Error Admin Notice.
213 *
214 * @return void
215 */
216 public function suretriggers_show_api_connection_error() {
217 global $pagenow;
218 $is_authorized = true;
219 if ( ! current_user_can( 'manage_options' ) ) {
220 $is_authorized = self::suretriggers_user_permission_check();
221 }
222 if ( 'index.php' != $pagenow || ! isset( OptionController::$options['secret_key'] ) || ! $is_authorized ) {
223 return;
224 }
225 $notice = get_option( 'suretriggers_verify_connection' );
226 // If empty option value for connection status, then verify the connection.
227 if ( empty( $notice ) || 'suretriggers_connection_successful' != $notice ) {
228 $connection_status = RestController::suretriggers_verify_wp_connection();
229 $connection_status_code = wp_remote_retrieve_response_code( $connection_status );
230 if ( is_wp_error( $connection_status ) ) {
231 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_wp_error' );
232 } else {
233 if ( 200 !== $connection_status_code ) {
234 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_error' );
235 } else {
236 update_option( 'suretriggers_verify_connection', 'suretriggers_connection_successful' );
237 }
238 }
239 }
240 $notice = get_option( 'suretriggers_verify_connection' );
241 if ( 'suretriggers_connection_successful' != $notice ) {
242 // If connection status is not successful, then show the notice.
243 ?>
244 <div class="notice notice-error is-dismissible">
245 <p>
246 <strong>
247 <?php esc_html_e( 'OttoKit Connection Issue', 'suretriggers' ); ?>
248 <span style="transform: rotate(-180deg); font-size: 20px;" class="dashicons dashicons-warning"></span>
249 </strong>
250 </p>
251 <p>
252 <?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' ); ?>
253 </p>
254 <p>
255 <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>
256 </p>
257 </div>
258 <?php
259 }
260 }
261
262 /**
263 * Redirect user after plugin activation.
264 *
265 * @return void
266 */
267 public function redirect_after_activation() {
268 $is_redirect = get_transient( 'st-redirect-after-activation' );
269 if ( $is_redirect ) {
270 delete_transient( 'st-redirect-after-activation' );
271 $url = get_admin_url() . 'admin.php?page=suretriggers';
272 wp_safe_redirect( $url );
273 die;
274 }
275 }
276
277 /**
278 * Adding setting link.
279 *
280 * @param array $links links.
281 * @return array
282 */
283 public function add_settings_link( array $links ) {
284 $url = get_admin_url() . 'admin.php?page=suretriggers';
285 $setting_option = get_option( 'suretrigger_options' );
286 if ( isset( $setting_option ) && ! empty( $setting_option ) ) {
287 $settings_link = '<a href="' . $url . '">' . __( 'Dashboard', 'suretriggers' ) . '</a>';
288 } else {
289 $settings_link = '<a href="' . $url . '">' . __( 'Connect', 'suretriggers' ) . '</a>';
290 }
291 $links[] = $settings_link;
292 return $links;
293 }
294
295 /**
296 * Define constants
297 *
298 * @return void
299 * @since 1.0.0
300 */
301 public function define_constants() {
302 $sass_url = 'https://app.ottokit.com';
303 $api_url = 'https://api.ottokit.com';
304 $webhook_url = 'https://webhook.ottokit.com';
305
306 define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) );
307 define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) );
308 define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) );
309 define( 'SURE_TRIGGERS_VER', '1.1.5' );
310 define( 'SURE_TRIGGERS_DB_VER', '1.1.5' );
311 define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' );
312 define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' );
313 define( 'SURE_TRIGGERS_SITE_URL', $sass_url );
314 define( 'SURE_TRIGGERS_API_SERVER_URL', $api_url );
315 define( 'SURE_TRIGGERS_WEBHOOK_SERVER_URL', $webhook_url );
316
317 define( 'SURE_TRIGGERS_PAGE', 'OttoKit' );
318 define( 'SURE_TRIGGERS_AS_GROUP', 'OttoKit' );
319
320 define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' );
321 }
322
323 /**
324 * Flush permalink rules while plugin activation.
325 *
326 * @return void
327 */
328 public function st_activate() {
329 flush_rewrite_rules(); //phpcs:ignore
330
331 set_transient( 'st-redirect-after-activation', true, 120 );
332 }
333
334 /**
335 * SureTriggers Access for Users and User Roles.
336 *
337 * @return bool
338 */
339 private function suretriggers_user_permission_check() {
340 $current_user = wp_get_current_user();
341 $current_user_id = $current_user->ID;
342 $current_user_roles = $current_user->roles;
343
344 // Get enabled users and roles with proper default values.
345 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
346 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
347
348 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
349 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
350
351 $is_authorized = in_array( $current_user_id, (array) $enabled_users ) || array_intersect( $current_user_roles, (array) $enabled_user_roles );
352
353 return $is_authorized;
354 }
355
356 /**
357 * Add main menu
358 *
359 * @since x.x.x
360 *
361 * @return void
362 */
363 public function admin_menu() {
364 $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'OttoKit', 'suretriggers' ) );
365 $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/OttoKitLogo.svg' );
366
367 $is_authorized = true;
368 if ( ! current_user_can( 'manage_options' ) ) {
369 $is_authorized = self::suretriggers_user_permission_check();
370 }
371
372 if ( $is_authorized ) {
373 add_menu_page(
374 $page_title,
375 $page_title,
376 'read',
377 'suretriggers',
378 [ $this, 'menu_callback' ],
379 $logo ? 'data:image/svg+xml;base64,' . base64_encode( $logo ) : '',
380 30.6002
381 );
382
383 add_submenu_page(
384 'suretriggers',
385 __( 'OttoKit Status', 'suretriggers' ),
386 __( 'Status', 'suretriggers' ),
387 'read',
388 'suretriggers-status',
389 [ $this, 'suretriggers_status_menu_callback' ]
390 );
391 }
392
393 if ( isset( OptionController::$options['secret_key'] ) ) {
394 add_options_page(
395 __( 'OttoKit Settings', 'suretriggers' ),
396 __( 'OttoKit Settings', 'suretriggers' ),
397 'manage_options',
398 'ottokit-settings',
399 [ $this, 'suretriggers_render_interface_callback' ]
400 );
401 }
402 }
403
404 /**
405 * Enqueue the admin scripts
406 *
407 * @param string $hook hook.
408 * @since x.x.x
409 *
410 * @return void
411 */
412 public function enqueue_scripts( $hook = '' ) {
413 if ( ! in_array( $hook, [ 'toplevel_page_suretriggers', 'ottokit_page_suretriggers-status', 'settings_page_ottokit-settings' ], true ) ) {
414 return;
415 }
416
417 remove_all_actions( 'admin_notices' );
418
419 $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php';
420 if ( ! file_exists( $file ) ) {
421 return;
422 }
423
424 $asset = require_once $file;
425
426 if ( ! isset( $asset ) ) {
427 return;
428 }
429
430 wp_register_script(
431 'sure-trigger-admin',
432 SURE_TRIGGERS_URL . 'app/build/main.js',
433 array_merge( $asset['dependencies'], [ 'regenerator-runtime' ], [ 'wp-i18n' ] ),
434 $asset['version'],
435 true
436 );
437
438 wp_localize_script(
439 'sure-trigger-admin',
440 'sureTriggerData',
441 $this->get_localized_array()
442 );
443 wp_enqueue_script( 'sure-trigger-admin' );
444 // Set the script translations.
445 wp_set_script_translations( 'sure-trigger-admin', 'suretriggers', SURE_TRIGGERS_DIR . 'languages' );
446 wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER );
447 wp_enqueue_style( 'st-trigger-style', SURE_TRIGGERS_URL . 'assets/admin-css/st-admin-css.css', [], SURE_TRIGGERS_VER );
448 wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER );
449 }
450
451 /**
452 * Get localized array for sure triggers.
453 *
454 * @return array
455 */
456 private function get_localized_array() {
457 $settings_nonce = wp_create_nonce( 'suretriggers_settings_nonce_action' );
458 $current_user = wp_get_current_user();
459 global $wp_roles;
460 $is_authorized = true;
461 if ( ! current_user_can( 'manage_options' ) ) {
462 $is_authorized = self::suretriggers_user_permission_check();
463 }
464
465 $source_type = get_option( 'suretriggers_source' );
466
467 // Get enabled users and roles with proper default values.
468 $enabled_users = get_option( 'suretriggers_enabled_users', [] );
469 $enabled_user_roles = get_option( 'suretriggers_enabled_user_roles', [] );
470
471 $enabled_users = is_array( $enabled_users ) ? $enabled_users : [];
472 $enabled_user_roles = is_array( $enabled_user_roles ) ? $enabled_user_roles : [];
473
474 $data = [
475 'siteContent' => [
476 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
477 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers',
478 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ),
479 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect',
480 'siteTitle' => get_bloginfo( 'name' ),
481 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ),
482 'sourceType' => $source_type,
483 'adminLanguage' => get_user_locale(),
484 ],
485 'user' => [
486 'name' => $current_user->display_name,
487 'email' => $current_user->user_email,
488 ],
489 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ),
490 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ),
491 'integrations' => IntegrationsController::get_activated_integrations(),
492 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ),
493 'verification_status' => false,
494 'projects' => [],
495 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE,
496 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ),
497 'usersRoles' => array_diff_key( $wp_roles->get_names(), [ 'administrator' => '' ] ),
498 'usersList' => get_users(
499 [
500 'fields' => [
501 'ID',
502 'display_name',
503 ],
504 'role__not_in' => [
505 'administrator',
506 ],
507 ]
508 ),
509 'enabledUsers' => $enabled_users,
510 'enabledUserRoles' => $enabled_user_roles,
511 ];
512
513 if ( $is_authorized ) {
514 $data['siteContent']['accessKey'] = SaasApiToken::get();
515 $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' );
516 }
517
518 $data['settingsNonce'] = esc_js( $settings_nonce );
519 $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php' ) );
520
521 return apply_filters( 'sure_trigger_control_localize_vars', $data );
522 }
523
524 /**
525 * Render OttoKit Interface callback.
526 *
527 * @return void
528 */
529 public function suretriggers_render_interface_callback() {
530 ?>
531 <div id="ottokit-settings-page" class="ottokit-settings"></div>
532 <?php
533 }
534
535 /**
536 * Menu callback.
537 *
538 * @since x.x.x
539 *
540 * @return void
541 */
542 public function menu_callback() {
543 // Check permalink structure first.
544 $permalink_structure = get_option( 'permalink_structure' );
545 $is_plain_permalink = empty( $permalink_structure );
546
547 // If permalink structure is "Plain" and we're trying to connect or already connected, show a prominent warning.
548 if ( $is_plain_permalink ) {
549 ?>
550 <div class="st-permalink-warning-wrapper">
551 <div class="st-permalink-warning-card">
552 <div class="st-permalink-warning-icon">
553 <span class="st-permalink-warning-icon-text">!</span>
554 </div>
555 <div class="st-permalink-warning-content">
556 <p class="st-permalink-warning-title">
557 <?php esc_html_e( '“Plain” Permalink Structure Detected', 'suretriggers' ); ?>
558 </p>
559 <p class="st-permalink-warning-message">
560 <?php esc_html_e( 'Your site is currently using the “Plain” permalink structure, which is not supported by OttoKit. Please visit', 'suretriggers' ); ?>
561 <a href="<?php echo esc_url( admin_url( 'options-permalink.php' ) ); ?>" target="_blank" class="st-permalink-warning-link">
562 <?php esc_html_e( 'Permalinks Settings', 'suretriggers' ); ?>
563 </a>
564 <?php esc_html_e( 'on your WordPress Dashboard and choose any structure other than “Plain”.', 'suretriggers' ); ?>
565 </p>
566 </div>
567 </div>
568 </div>
569 <?php
570 }
571
572 // Verify Token.
573 $response = RestController::verify_user_token();
574 $response_body = wp_remote_retrieve_body( $response );
575 $response_code = wp_remote_retrieve_response_code( $response );
576 if ( ! empty( $response_body ) ) {
577 $response_body = json_decode( $response_body, true );
578 }
579 if ( 200 === $response_code || 401 === $response_code ) {
580 if ( is_array( $response_body ) && isset( $response_body['is_iframe_enabled'] ) && 'NO' === $response_body['is_iframe_enabled'] ) {
581 ?>
582 <div class="suretriggers-nobase">
583 <div>
584 <div>
585 <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>
586 <h2 class="suretriggers-info-title">
587 <?php esc_html_e( 'OttoKit is connected.', 'suretriggers' ); ?>
588 </h2>
589 <p class="suretriggers-info-content">
590 <?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' ); ?>
591 </p>
592 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL . '/apps/WordPress' ); ?>" target="_blank">
593 <?php esc_html_e( 'Access Connection Page', 'suretriggers' ); ?>
594 </a>
595 </div>
596 </div>
597 </div>
598 <?php
599 } else {
600 ?>
601 <div id="sure-triggger-entry" class="st-base"></div>
602 <?php
603 }
604 } elseif ( isset( $response ) && is_wp_error( $response ) || 200 !== $response_code ) {
605 ?>
606 <div class="suretriggers-nobase">
607 <div>
608 <div>
609 <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>
610 <h2 class="suretriggers-info-title">
611 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
612 </h2>
613 <p class="suretriggers-info-content">
614 <?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' ); ?>
615 </p>
616 <a class="suretriggers-info-link" href="<?php echo esc_url( SURE_TRIGGERS_SITE_URL ); ?>" target="_blank">
617 <?php esc_html_e( 'Access Dashboard', 'suretriggers' ); ?>
618 </a>
619 <a class="suretriggers-info-link" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ); ?>">
620 <?php esc_html_e( 'Disconnect OttoKit', 'suretriggers' ); ?>
621 </a>
622 </div>
623 </div>
624 </div>
625 <?php
626 }
627 }
628
629 /**
630 * Status Menu callback.
631 *
632 * @since x.x.x
633 *
634 * @return void
635 */
636 public function suretriggers_status_menu_callback() {
637 if ( ! isset( OptionController::$options['secret_key'] ) ) {
638 ?>
639 <div class="suretriggers-nobase">
640 <div>
641 <div>
642 <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>
643 <h2 class="suretriggers-info-title">
644 <?php esc_html_e( 'OttoKit Not Connected.', 'suretriggers' ); ?>
645 </h2>
646 <p class="suretriggers-info-content">
647 <?php esc_html_e( 'Please connect your OttoKit (Formerly SureTriggers) account to access registered events and outgoing requests.', 'suretriggers' ); ?>
648 </p>
649 <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>
650 </div>
651 </div>
652 </div>
653 <?php
654 return;
655 }
656 ?>
657 <div class="wrap">
658 <?php
659 $tabs = [
660 'st_system_page' => __( 'Status', 'suretriggers' ),
661 'st_outgoing_requests' => __( 'Outgoing Requests', 'suretriggers' ),
662 'st_troubleshooting' => __( 'Troubleshooting', 'suretriggers' ),
663 ];
664 $current_tab = 'st_system_page';
665 if ( isset( $_REQUEST['tab'], $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'suretriggers_tab_nonce' ) ) {
666 if ( array_key_exists( sanitize_key( $_REQUEST['tab'] ), $tabs ) ) {
667 $current_tab = sanitize_key( $_REQUEST['tab'] );
668 }
669 }
670 ?>
671 <nav class="suretriggers-nav-tab nav-tab-wrapper">
672 <?php
673 foreach ( $tabs as $name => $label ) {
674 $tab_url = add_query_arg(
675 [
676 'tab' => $name,
677 '_wpnonce' => wp_create_nonce( 'suretriggers_tab_nonce' ),
678 ],
679 admin_url( 'admin.php?page=suretriggers-status' )
680 );
681 echo '<a href="' . esc_url( $tab_url ) . '" class="nav-tab ';
682 if ( $current_tab == $name ) {
683 echo 'nav-tab-active';
684 }
685 echo '">' . esc_html( $label ) . '</a>';
686 }
687 ?>
688 </nav>
689 <?php
690 switch ( $current_tab ) {
691 case 'st_system_page':
692 include_once __DIR__ . '/Admin/Views/st-admin-system-page.php';
693 break;
694 case 'st_outgoing_requests':
695 include_once __DIR__ . '/Admin/Views/st-admin-outgoing-req-page.php';
696 break;
697 case 'st_troubleshooting':
698 include_once __DIR__ . '/Admin/Views/st-admin-troubleshooting-page.php';
699 break;
700 }
701 ?>
702 </div>
703 <?php
704 }
705
706 /**
707 * Include all files from the folder.
708 *
709 * @param string $folder folder path.
710 * @return void
711 */
712 public function include_all_files( $folder ) {
713 $dir = new DirectoryIterator( $folder );
714 foreach ( $dir as $file ) {
715 if ( ! $file->isDot() ) {
716 if ( $file->isDir() ) {
717 $this->include_all_files( $file->getPathname() );
718 } else {
719 require_once $file->getPathname();
720 }
721 }
722 }
723 }
724
725 /**
726 * Initialize core trigger and actions.
727 *
728 * @return void
729 */
730 public function initialize_core() {
731
732 IntegrationsController::load_event_files();
733
734 EventController::get_instance();
735 IntegrationsController::get_instance();
736 GlobalSearchController::get_instance();
737 RestController::get_instance();
738 OptionController::get_instance();
739 AutomationController::get_instance();
740 AuthController::get_instance();
741 RoutesController::get_instance();
742 WebhookRequestsController::get_instance();
743 SettingsController::get_instance();
744
745 // SureTriggers Custom Filter data.
746 add_filter( 'suretriggers_get_iframe_url', [ $this, 'suretriggers_iframe_data' ] );
747 add_filter( 'suretriggers_is_user_connected', [ $this, 'suretriggers_saas_connected_data' ] );
748
749 // Create Webhook Request Log table.
750 WebhookRequestsController::suretriggers_webhook_request_log_table();
751 // Schedule the cron jon to retry failed triggers.
752 WebhookRequestsController::suretriggers_setup_custom_cron();
753
754 $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' );
755 }
756
757 /**
758 * Added option to reset plugin in case of testing.
759 *
760 * @return void
761 */
762 public function reset_plugin() {
763 $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) );
764
765 if ( $nonce && wp_verify_nonce( $nonce, 'st-reset-action' ) ) {
766 $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) );
767 if ( $is_reset && current_user_can( 'manage_options' ) ) {
768 delete_option( 'suretrigger_options' );
769 wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) );
770 exit();
771 }
772 }
773 }
774
775 /**
776 * Custom Filter data.
777 *
778 * @param string $site_url Optional. Site URL to include in the iframe data.
779 * @return string
780 */
781 public function suretriggers_iframe_data( $site_url = '' ) {
782 $site_url = esc_url_raw( $site_url );
783 if ( ! current_user_can( 'manage_options' ) ) {
784 return $site_url;
785 }
786 $site_content_data = [
787 'stSaasURL' => $site_url . 'wp-login',
788 'stCode' => SaasApiToken::get(),
789 'baseUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
790 'resetUrl' => rtrim( base64_encode( wp_nonce_url( admin_url( 'admin.php?st-reset=true' ), 'st-reset-action' ) ), '=' ), // phpcs:ignore
791 ];
792 $params = [
793 'st-code' => $site_content_data['stCode'],
794 'base_url' => $site_content_data['baseUrl'],
795 'reset_url' => $site_content_data['resetUrl'],
796 'redirect_url' => $site_url . 'embed-login',
797 'is_embedded' => true,
798 ];
799
800 if ( filter_var( $site_url, FILTER_VALIDATE_URL ) ) {
801 $iframe_url = add_query_arg( $params, $site_content_data['stSaasURL'] );
802 } else {
803 $default_url = trailingslashit( SURE_TRIGGERS_SITE_URL ) . '?path=dashboard';
804 $iframe_url = add_query_arg( $params, $default_url );
805 }
806 return esc_url_raw( $iframe_url );
807 }
808
809
810 /**
811 * Custom Filter data to check if user is logged in iframe.
812 *
813 * @return bool
814 */
815 public function suretriggers_saas_connected_data() {
816 if ( ! current_user_can( 'manage_options' ) ) {
817 return false;
818 }
819 $token = SaasApiToken::get();
820
821 if ( '' === $token || null === $token || false === $token || 'connection-denied' === $token ) {
822 $logged_in = false;
823 } else {
824 $logged_in = true;
825 }
826 return $logged_in;
827 }
828 }
829