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