| 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 |
$academy_addons = json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME, '{}' ) ); |
| 44 |
wp_send_json_success( $academy_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 |
|
| 53 |
$addon_name = ( isset( $_POST['addon_name'] ) ? sanitize_text_field( $_POST['addon_name'] ) : '' ); |
| 54 |
$addon_slug = ( isset( $_POST['addon_slug'] ) ? sanitize_text_field( $_POST['addon_slug'] ) : '' ); |
| 55 |
$status = (bool) ( isset( $_POST['status'] ) ? \ABlocks\Helper::sanitize_checkbox_field( $_POST['status'] ) : false ); |
| 56 |
|
| 57 |
if ( empty( $addon_slug ) ) { |
| 58 |
wp_send_json_error( __( 'Addon Name missing', 'academy' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( $status ) { |
| 62 |
$required_plugin = ( isset( $_POST['required_plugin'] ) ? json_decode( stripslashes( $_POST['required_plugin'] ), true ) : '' ); |
| 63 |
do_action( 'academy/before_active_addon', $addon_slug, $required_plugin ); |
| 64 |
if ( $required_plugin && is_array( $required_plugin ) ) { |
| 65 |
foreach ( $required_plugin as $plugin ) { |
| 66 |
if ( 'Wishlist Member' === $plugin['plugin_name'] ) { |
| 67 |
$active_plugins = get_option( 'active_plugins', array() ); |
| 68 |
$plugin['plugin_dir_path'] = in_array( $plugin['plugin_dir_path'], $active_plugins, true ) ? $plugin['plugin_dir_path'] : ( in_array( 'wishlist-member-x/wpm.php', $active_plugins, true ) ? 'wishlist-member-x/wpm.php' : '' ); |
| 69 |
} |
| 70 |
if ( ! Helper::is_plugin_active( sanitize_text_field( $plugin['plugin_dir_path'] ) ) ) { |
| 71 |
$error_message = sprintf( '%s Plugin is required to activate %s addon.', sanitize_text_field( $plugin['plugin_name'] ), $addon_name ); |
| 72 |
wp_send_json_error( $error_message ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Saved Data |
| 79 |
$saved_addons = (array) json_decode( get_option( ABLOCKS_ADDONS_SETTINGS_NAME ), true ); |
| 80 |
$saved_addons[ $addon_slug ] = $status; |
| 81 |
update_option( ABLOCKS_ADDONS_SETTINGS_NAME, wp_json_encode( $saved_addons ) ); |
| 82 |
// Fire Addon Action |
| 83 |
if ( $status ) { |
| 84 |
do_action( "ablocks/addons/activated_{$addon_slug}", $status ); |
| 85 |
} else { |
| 86 |
do_action( "ablocks/addons/deactivated_{$addon_slug}", $status ); |
| 87 |
} |
| 88 |
// response |
| 89 |
wp_send_json_success( $saved_addons ); |
| 90 |
} |
| 91 |
} |
| 92 |
|