| @@ -1,0 +1,115 @@ | ||
| 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 | + 'link-guard' => 'LinkGuard', | |
| 26 | + ]); | |
| 27 | + | |
| 28 | + foreach ( $addons as $addon_name => $addon_class_name ) { | |
| 29 | + $addon_root_path = ABLOCKS_ADDONS_DIR_PATH . $addon_name . '/'; | |
| 30 | + // Register the addon's root namespace and path. | |
| 31 | + $addon_namespace = 'ABlocks' . $addon_class_name; | |
| 32 | + $Autoload->add_namespace_directory( $addon_namespace, $addon_root_path ); | |
| 33 | + // Initialize the addon's main class. | |
| 34 | + $class = $addon_namespace . '\\' . $addon_class_name; | |
| 35 | + | |
| 36 | + $class::init(); | |
| 37 | + } | |
| 38 | + } | |
| 39 | + | |
| 40 | + public function get_all_addons() { | |
| 41 | + check_ajax_referer( 'ablocks_nonce', 'security' ); | |
| 42 | + if ( ! current_user_can( 'ablocks_manage_addons' ) ) { | |
| 43 | + wp_die(); | |
| 44 | + } | |
| 45 | + $ablocks_addons = json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME, '{}' ) ); | |
| 46 | + wp_send_json_success( $ablocks_addons ); | |
| 47 | + } | |
| 48 | + | |
| 49 | + public function saved_addon_status() { | |
| 50 | + check_ajax_referer( 'ablocks_nonce', 'security' ); | |
| 51 | + // ablocks_manage_addons is only ever granted to somebody who already holds | |
| 52 | + // install_plugins, because turning an add-on on can install one. | |
| 53 | + if ( ! current_user_can( 'ablocks_manage_addons' ) ) { | |
| 54 | + wp_die(); | |
| 55 | + } | |
| 56 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 57 | + $addon_name = ( isset( $_POST['addon_name'] ) ? sanitize_text_field( $_POST['addon_name'] ) : '' ); | |
| 58 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash | |
| 59 | + $addon_slug = ( isset( $_POST['addon_slug'] ) ? sanitize_text_field( $_POST['addon_slug'] ) : '' ); | |
| 60 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 61 | + $status = (bool) ( isset( $_POST['status'] ) ? \ABlocks\Helper::sanitize_checkbox_field( $_POST['status'] ) : false ); | |
| 62 | + | |
| 63 | + if ( empty( $addon_slug ) ) { | |
| 64 | + wp_send_json_error( __( 'Addon Name missing', 'ablocks' ) ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + if ( $status ) { | |
| 68 | + | |
| 69 | + $required_plugin = array(); | |
| 70 | + | |
| 71 | + if ( isset( $_POST['required_plugin'] ) ) { | |
| 72 | + $raw_json = wp_unslash( $_POST['required_plugin'] ); | |
| 73 | + $decoded = json_decode( $raw_json, true ); | |
| 74 | + | |
| 75 | + if ( is_array( $decoded ) ) { | |
| 76 | + $required_plugin = map_deep( $decoded, 'sanitize_text_field' ); | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + do_action( 'ablocks/before_active_addon', $addon_slug, $required_plugin ); | |
| 81 | + | |
| 82 | + if ( ! empty( $required_plugin ) ) { | |
| 83 | + foreach ( $required_plugin as $plugin ) { | |
| 84 | + | |
| 85 | + if ( empty( $plugin['plugin_dir_path'] ) || empty( $plugin['plugin_name'] ) ) { | |
| 86 | + continue; | |
| 87 | + } | |
| 88 | + | |
| 89 | + if ( ! Helper::is_plugin_active( $plugin['plugin_dir_path'] ) ) { | |
| 90 | + $error_message = sprintf( | |
| 91 | + '%s Plugin is required to activate %s addon.', | |
| 92 | + esc_html( $plugin['plugin_name'] ), | |
| 93 | + esc_html( $addon_name ) | |
| 94 | + ); | |
| 95 | + | |
| 96 | + wp_send_json_error( $error_message ); | |
| 97 | + } | |
| 98 | + } | |
| 99 | + } | |
| 100 | + }//end if | |
| 101 | + | |
| 102 | + // Saved Data | |
| 103 | + $saved_addons = (array) json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME ), true ); | |
| 104 | + $saved_addons[ $addon_slug ] = $status; | |
| 105 | + update_option( ABLOCKS_ADDONS_SETTINGS_NAME, wp_json_encode( $saved_addons ) ); | |
| 106 | + // Fire Addon Action | |
| 107 | + if ( $status ) { | |
| 108 | + do_action( "ablocks/addons/activated_{$addon_slug}", $status ); | |
| 109 | + } else { | |
| 110 | + do_action( "ablocks/addons/deactivated_{$addon_slug}", $status ); | |
| 111 | + } | |
| 112 | + // response | |
| 113 | + wp_send_json_success( $saved_addons ); | |
| 114 | + } | |
| 115 | +} | |