PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / trunk
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder vtrunk
2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 All 78 releases
ablocks / includes / addons.php

addons.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder trunk, at includes/addons.php

115 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 use \ABlocks\Helper;
9
10 class Addons {
11 public static function init() {
12 $self = new self();
13 // Load all addons
14 $self->addons_loader();
15 // Addons Ajax
16 add_action( 'wp_ajax_ablocks/addons/get_all_addons', array( $self, 'get_all_addons' ) );
17 add_action( 'wp_ajax_ablocks/addons/saved_addon_status', array( $self, 'saved_addon_status' ) );
18 }
19
20 private function addons_loader() {
21 $Autoload = Autoload::get_instance();
22 $addons = apply_filters('ablocks/addons/loader_args', [
23 'theme-builder' => 'ThemeBuilder',
24 'cookie-consent' => 'CookieConsent',
25 ]);
26
27 foreach ( $addons as $addon_name => $addon_class_name ) {
28 $addon_root_path = ABLOCKS_ADDONS_DIR_PATH . $addon_name . '/';
29 // Register the addon's root namespace and path.
30 $addon_namespace = 'ABlocks' . $addon_class_name;
31 $Autoload->add_namespace_directory( $addon_namespace, $addon_root_path );
32 // Initialize the addon's main class.
33 $class = $addon_namespace . '\\' . $addon_class_name;
34
35 $class::init();
36 }
37 }
38
39 public function get_all_addons() {
40 check_ajax_referer( 'ablocks_nonce', 'security' );
41 if ( ! current_user_can( 'ablocks_manage_addons' ) ) {
42 wp_die();
43 }
44 $ablocks_addons = json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME, '{}' ) );
45 wp_send_json_success( $ablocks_addons );
46 }
47
48 public function saved_addon_status() {
49 check_ajax_referer( 'ablocks_nonce', 'security' );
50 // ablocks_manage_addons is only ever granted to somebody who already holds
51 // install_plugins, because turning an add-on on can install one.
52 if ( ! current_user_can( 'ablocks_manage_addons' ) ) {
53 wp_die();
54 }
55 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
56 $addon_name = ( isset( $_POST['addon_name'] ) ? sanitize_text_field( $_POST['addon_name'] ) : '' );
57 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
58 $addon_slug = ( isset( $_POST['addon_slug'] ) ? sanitize_text_field( $_POST['addon_slug'] ) : '' );
59 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
60 $status = (bool) ( isset( $_POST['status'] ) ? \ABlocks\Helper::sanitize_checkbox_field( $_POST['status'] ) : false );
61
62 if ( empty( $addon_slug ) ) {
63 wp_send_json_error( __( 'Addon Name missing', 'ablocks' ) );
64 }
65
66 if ( $status ) {
67
68 $required_plugin = array();
69
70 if ( isset( $_POST['required_plugin'] ) ) {
71 $raw_json = wp_unslash( $_POST['required_plugin'] );
72 $decoded = json_decode( $raw_json, true );
73
74 if ( is_array( $decoded ) ) {
75 $required_plugin = map_deep( $decoded, 'sanitize_text_field' );
76 }
77 }
78
79 do_action( 'ablocks/before_active_addon', $addon_slug, $required_plugin );
80
81 if ( ! empty( $required_plugin ) ) {
82 foreach ( $required_plugin as $plugin ) {
83
84 if ( empty( $plugin['plugin_dir_path'] ) || empty( $plugin['plugin_name'] ) ) {
85 continue;
86 }
87
88 if ( ! Helper::is_plugin_active( $plugin['plugin_dir_path'] ) ) {
89 $error_message = sprintf(
90 '%s Plugin is required to activate %s addon.',
91 esc_html( $plugin['plugin_name'] ),
92 esc_html( $addon_name )
93 );
94
95 wp_send_json_error( $error_message );
96 }
97 }
98 }
99 }//end if
100
101 // Saved Data
102 $saved_addons = (array) json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME ), true );
103 $saved_addons[ $addon_slug ] = $status;
104 update_option( ABLOCKS_ADDONS_SETTINGS_NAME, wp_json_encode( $saved_addons ) );
105 // Fire Addon Action
106 if ( $status ) {
107 do_action( "ablocks/addons/activated_{$addon_slug}", $status );
108 } else {
109 do_action( "ablocks/addons/deactivated_{$addon_slug}", $status );
110 }
111 // response
112 wp_send_json_success( $saved_addons );
113 }
114 }
115