PluginProbe
OttoKit: All-in-One Automation Platform / 1.0.12
OttoKit: All-in-One Automation Platform v1.0.12
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
419 lines 12.3 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\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 use SingletonLoader;
44
45 /**
46 * Constructor
47 *
48 * @since 1.0.0
49 */
50 public function __construct() {
51 register_activation_hook( SURE_TRIGGERS_FILE, [ $this, 'st_activate' ] );
52
53 $this->define_constants();
54 add_action( 'plugins_loaded', [ $this, 'initialize_core' ] );
55 // Admin Menu.
56 add_action( 'admin_menu', [ $this, 'admin_menu' ] );
57 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
58 add_action( 'admin_init', [ $this, 'detect_saas_env' ] );
59 add_action( 'admin_init', [ $this, 'reset_plugin' ] );
60
61 add_filter( 'plugin_action_links_' . plugin_basename( SURE_TRIGGERS_FILE ), [ $this, 'add_settings_link' ] );
62 add_action( 'admin_init', [ $this, 'redirect_after_activation' ] );
63
64 add_action( 'admin_notices', [ $this, 'display_notice' ] );
65
66 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] );
67 }
68
69 /**
70 * Adding dashboard widget.
71 *
72 * @return void
73 */
74 public function add_dashboard_widgets() {
75 if ( isset( OptionController::$options['secret_key'] ) ) {
76 return;
77 }
78
79 wp_add_dashboard_widget(
80 'suretriggers_dashboard_widget',
81 'Please Connect SureTriggers',
82 [ $this, 'dashboard_widget_display' ],
83 '',
84 '',
85 'side',
86 'high'
87 );
88 }
89
90 /**
91 * Dashboard widget callback.
92 *
93 * @return void
94 */
95 public function dashboard_widget_display() {
96 ?>
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( 'options-general.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( 'options-general.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
166 $sass_url = 'https://app.suretriggers.com';
167 $api_url = 'https://api.suretriggers.com';
168 $webhook_url = 'https://webhook.suretriggers.com';
169
170
171 $saas_env = OptionController::get_option( 'saas_env' );
172 if ( ! empty( $saas_env ) ) {
173 $sass_url = "https://{$saas_env}.suretriggers.com";
174 $api_url = "https://api-{$saas_env}.suretriggers.com";
175 $webhook_url = "https://webhook-{$saas_env}.suretriggers.com";
176 }
177
178 define( 'SURE_TRIGGERS_BASE', plugin_basename( SURE_TRIGGERS_FILE ) );
179 define( 'SURE_TRIGGERS_DIR', plugin_dir_path( SURE_TRIGGERS_FILE ) );
180 define( 'SURE_TRIGGERS_URL', plugins_url( '/', SURE_TRIGGERS_FILE ) );
181 define( 'SURE_TRIGGERS_VER', '1.0.12' );
182 define( 'SURE_TRIGGERS_DB_VER', '1.0.12' );
183 define( 'SURE_TRIGGERS_REST_NAMESPACE', 'sure-triggers/v1' );
184 define( 'SURE_TRIGGERS_SASS_URL', $sass_url . '/wp-json/wp-plugs/v1/' );
185 define( 'SURE_TRIGGERS_SITE_URL', $sass_url );
186 define( 'API_SERVER_URL', $api_url );
187 define( 'WEBHOOK_SERVER_URL', $webhook_url );
188
189 define( 'SURE_TRIGGERS_PAGE', 'SureTrigger' );
190 define( 'SURE_TRIGGERS_AS_GROUP', 'SureTrigger' );
191
192 define( 'SURE_TRIGGERS_ACTION_ERROR_MESSAGE', 'An unexpected error occurred. Something went wrong with the action.' );
193 }
194
195 /**
196 * Flush permalink rules while plugin activation.
197 *
198 * @return void
199 */
200 public function st_activate() {
201 global $wp_rewrite;
202 $wp_rewrite->set_permalink_structure( '/%postname%/' );
203 flush_rewrite_rules(); //phpcs:ignore
204
205 set_transient( 'st-redirect-after-activation', true, 120 );
206 }
207
208 /**
209 * Add main menu
210 *
211 * @since x.x.x
212 *
213 * @return void
214 */
215 public function admin_menu() {
216 $page_title = apply_filters( 'st_menu_page_title', esc_html__( 'SureTriggers', 'suretriggers' ) );
217 $logo = file_get_contents( plugin_dir_path( SURE_TRIGGERS_FILE ) . 'assets/images/STLogo.svg' );
218
219 add_menu_page(
220 $page_title,
221 $page_title,
222 'manage_options',
223 'suretriggers',
224 [ $this, 'menu_callback' ],
225 'data:image/svg+xml;base64,' . base64_encode( $logo ),
226 30.6000
227 );
228 }
229
230 /**
231 * Enqueue the admin scripts
232 *
233 * @param string $hook hook.
234 * @since x.x.x
235 *
236 * @return void
237 */
238 public function enqueue_scripts( $hook = '' ) {
239 if ( ! in_array( $hook, [ 'toplevel_page_suretriggers' ], true ) ) {
240 return;
241 }
242
243 remove_all_actions( 'admin_notices' );
244
245 $file = SURE_TRIGGERS_DIR . 'app/build/main.asset.php';
246 if ( ! file_exists( $file ) ) {
247 return;
248 }
249
250 $asset = require_once $file;
251
252 if ( ! isset( $asset ) ) {
253 return;
254 }
255
256 wp_register_script(
257 'sure-trigger-admin',
258 SURE_TRIGGERS_URL . 'app/build/main.js',
259 array_merge( $asset['dependencies'] ),
260 $asset['version'],
261 true
262 );
263
264 wp_localize_script(
265 'sure-trigger-admin',
266 'sureTriggerData',
267 $this->get_localized_array()
268 );
269 wp_enqueue_script( 'sure-trigger-admin' );
270 wp_enqueue_style( 'sure-trigger-components', SURE_TRIGGERS_URL . 'app/build/style-main.css', [], SURE_TRIGGERS_VER );
271 wp_enqueue_style( 'sure-trigger-css', SURE_TRIGGERS_URL . 'app/build/main.css', [], SURE_TRIGGERS_VER );
272 }
273
274 /**
275 * Get localized array for sure triggers.
276 *
277 * @return array
278 */
279 private function get_localized_array() {
280 $current_user = wp_get_current_user();
281
282 $data = [
283 'siteContent' => [
284 'siteUrl' => str_replace( '/wp-json/', '', get_rest_url() ),
285 'redirectUrl' => get_site_url() . '/wp-admin/themes.php?page=suretriggers',
286 'connectNonce' => wp_create_nonce( 'sure-trigger-connect' ),
287 'connectUrl' => SURE_TRIGGERS_SITE_URL . '/connect-st/connect',
288 'siteTitle' => get_bloginfo( 'name' ),
289 'resetUrl' => base64_encode( wp_nonce_url( admin_url( 'options-general.php?st-reset=true' ), 'st-reset-action' ) ),
290 ],
291 'user' => [
292 'name' => $current_user->display_name,
293 'email' => $current_user->user_email,
294 ],
295 'stSaasURL' => trailingslashit( SURE_TRIGGERS_SITE_URL ),
296 'stPluginURL' => plugin_dir_url( SURE_TRIGGERS_FILE ),
297 'integrations' => IntegrationsController::get_activated_integrations(),
298 'enabledIntegrations' => OptionController::get_option( 'enabled_integrations' ),
299 'settingsPageURL' => admin_url( 'themes.php?page=suretriggers' ),
300 'verification_status' => false,
301 'projects' => [],
302 'apiSlug' => SURE_TRIGGERS_REST_NAMESPACE,
303 'isElementorEditor' => ( did_action( 'elementorpro/loaded' ) ) ? Elementor\Plugin::instance()->editor->is_edit_mode() : false,
304 'reConnectSorryMsg' => (bool) OptionController::get_option( 'st_connect_notice_deprecated' ),
305 ];
306
307 if ( current_user_can( 'manage_options' ) ) {
308 $data['siteContent']['accessKey'] = OptionController::get_option( 'secret_key' );
309 $data['siteContent']['connected_email'] = OptionController::get_option( 'connected_email_key' );
310 }
311
312 $settings = OptionController::get_option( 'st_settings' );
313 if ( empty( $settings ) ) {
314 $settings = (object) [];
315 }
316
317 $data['settingsForm'] = SettingsController::get_fields();
318 $data['settings'] = wp_json_encode( $settings );
319 $data['nonce'] = wp_create_nonce( 'st-nonce' );
320 $data['ajaxurl'] = esc_url( admin_url( 'admin-ajax.php', 'relative' ) );
321
322 return apply_filters( 'sure_trigger_control_localize_vars', $data );
323 }
324
325 /**
326 * Menu callback
327 *
328 * @since x.x.x
329 *
330 * @return void
331 */
332 public function menu_callback() {
333 ?>
334 <div id="sure-triggger-entry" class="st-base"></div>
335 <?php
336 }
337
338 /**
339 * Include all files from the folder.
340 *
341 * @param string $folder folder path.
342 * @return void
343 */
344 public function include_all_files( $folder ) {
345 $dir = new DirectoryIterator( $folder );
346 foreach ( $dir as $file ) {
347 if ( ! $file->isDot() ) {
348 if ( $file->isDir() ) {
349 $this->include_all_files( $file->getPathname() );
350 } else {
351 require_once $file->getPathname();
352 }
353 }
354 }
355 }
356
357 /**
358 * Initialize core trigger and actions.
359 *
360 * @return void
361 */
362 public function initialize_core() {
363 /**
364 * Include only integrations root files
365 */
366
367 $this->include_all_files( SURE_TRIGGERS_DIR . 'src/Integrations/' );
368
369 IntegrationsController::load_event_files();
370
371 EventController::get_instance();
372 EventHelperController::get_instance();
373 IntegrationsController::get_instance();
374 GlobalSearchController::get_instance();
375 RestController::get_instance();
376 OptionController::get_instance();
377 AutomationController::get_instance();
378 AuthController::get_instance();
379 RoutesController::get_instance();
380 SettingsController::get_instance();
381 }
382
383 /**
384 * Added option to reset plugin in case of testing.
385 *
386 * @return void
387 */
388 public function reset_plugin() {
389
390 $is_reset = sanitize_text_field( wp_unslash( isset( $_GET['st-reset'] ) ? $_GET['st-reset'] : false ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
391 $nonce = sanitize_text_field( wp_unslash( isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : false ) );
392
393 if ( $nonce && $is_reset && current_user_can( 'manage_options' ) && wp_verify_nonce( $nonce, 'st-reset-action' ) ) {
394 delete_option( 'suretrigger_options' );
395 wp_safe_redirect( admin_url( 'options-general.php?page=suretriggers' ) );
396 exit();
397 }
398 }
399
400 /**
401 * Switch saas env from URL
402 *
403 * @return void
404 */
405 public function detect_saas_env() {
406 $saas_env = [ 'app', 'stage', 'dev', 'qaing', 'release' ];
407
408 $env = sanitize_text_field( wp_unslash( isset( $_GET['env'] ) ? $_GET['env'] : '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
409
410 if ( ! current_user_can( 'manage_options' ) || ( empty( $env ) || ! in_array( $env, $saas_env, true ) ) ) {
411 return;
412 }
413
414 OptionController::set_option( 'saas_env', $env );
415 wp_safe_redirect( admin_url( 'admin.php?page=suretriggers' ) );
416 exit();
417 }
418 }
419