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