| 1 |
<?php |
| 2 |
|
| 3 |
namespace ABlocks\Ajax; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use ABlocks\Classes\AbstractAjaxHandler; |
| 10 |
use ABlocks\Classes\FileUpload; |
| 11 |
use ABlocks\Classes\FontLoadLocally; |
| 12 |
use ABlocks\Helper; |
| 13 |
|
| 14 |
class Dashboard extends AbstractAjaxHandler { |
| 15 |
public function __construct() { |
| 16 |
$this->actions = array( |
| 17 |
'get_admin_menu_items' => array( |
| 18 |
'callback' => array( $this, 'get_admin_menu_items' ), |
| 19 |
'capability' => 'manage_options', |
| 20 |
), |
| 21 |
'regenerate_assets' => array( |
| 22 |
'callback' => array( $this, 'regenerate_assets' ), |
| 23 |
'capability' => 'manage_options', |
| 24 |
), |
| 25 |
'clear_demo_transients' => array( |
| 26 |
'callback' => array( $this, 'clear_demo_transients' ), |
| 27 |
'capability' => 'manage_options', |
| 28 |
), |
| 29 |
'install_academy_lms' => array( |
| 30 |
'callback' => array( $this, 'install_academy_lms' ), |
| 31 |
'capability' => 'manage_options', |
| 32 |
), |
| 33 |
'install_storeengine' => array( |
| 34 |
'callback' => array( $this, 'install_storeengine' ), |
| 35 |
'capability' => 'manage_options', |
| 36 |
), |
| 37 |
'download_google_fonts' => array( |
| 38 |
'callback' => array( $this, 'download_google_fonts' ), |
| 39 |
'capability' => 'manage_options', |
| 40 |
), |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function get_admin_menu_items() { |
| 45 |
$menu_items = wp_json_encode( Helper::get_admin_menu_list() ); |
| 46 |
wp_send_json_success( $menu_items ); |
| 47 |
} |
| 48 |
public function regenerate_assets() { |
| 49 |
$FileUpload = new FileUpload(); |
| 50 |
$has_delete = $FileUpload->delete_files(); |
| 51 |
Helper::clear_third_party_plugin_cache(); |
| 52 |
update_option( ABLOCKS_FONTS_SETTINGS_NAME, '{}' ); |
| 53 |
wp_send_json_success( $has_delete ); |
| 54 |
} |
| 55 |
|
| 56 |
public function clear_demo_transients() { |
| 57 |
global $wpdb; |
| 58 |
|
| 59 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 60 |
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_ablocks_demo_%' OR option_name LIKE '_transient_timeout_ablocks_demo_%'" ); |
| 61 |
|
| 62 |
if ( ! empty( $wpdb->last_error ) ) { |
| 63 |
wp_send_json_error( $wpdb->last_error ); |
| 64 |
} |
| 65 |
|
| 66 |
wp_send_json_success(); |
| 67 |
} |
| 68 |
public function install_academy_lms() { |
| 69 |
// Check user permissions |
| 70 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 71 |
wp_send_json_error( __( 'You do not have sufficient permissions to install plugins.', 'ablocks' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
// Check if the plugin is already installed |
| 75 |
if ( ! Helper::is_plugin_installed( 'academy/academy.php' ) ) { |
| 76 |
|
| 77 |
$plugin_status = $this->install_plugin( 'academy', true ); |
| 78 |
if ( $plugin_status ) { |
| 79 |
wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) ); |
| 80 |
} |
| 81 |
wp_send_json_error( __( 'Sorry, failed to download.', 'ablocks' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
// Activate the plugin |
| 85 |
$activate_status = activate_plugin( 'academy/academy.php' ); |
| 86 |
if ( is_wp_error( $activate_status ) ) { |
| 87 |
wp_send_json_error( 'Plugin activation failed: ' . $activate_status->get_error_message() ); |
| 88 |
} |
| 89 |
wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) ); |
| 90 |
} |
| 91 |
public function install_storeengine() { |
| 92 |
// Check user permissions |
| 93 |
if ( ! current_user_can( 'install_plugins' ) ) { |
| 94 |
wp_send_json_error( __( 'You do not have sufficient permissions to install plugins.', 'ablocks' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
// Check if the plugin is already installed |
| 98 |
if ( ! Helper::is_plugin_installed( 'storeengine/storeengine.php' ) ) { |
| 99 |
|
| 100 |
$plugin_status = $this->install_plugin( 'storeengine', true ); |
| 101 |
if ( $plugin_status ) { |
| 102 |
wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) ); |
| 103 |
} |
| 104 |
wp_send_json_error( __( 'Sorry, failed to download.', 'ablocks' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
// Activate the plugin |
| 108 |
$activate_status = activate_plugin( 'storeengine/storeengine.php' ); |
| 109 |
if ( is_wp_error( $activate_status ) ) { |
| 110 |
wp_send_json_error( 'Plugin activation failed: ' . $activate_status->get_error_message() ); |
| 111 |
} |
| 112 |
wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
public function install_plugin( $slug = '', $active = true ) { |
| 116 |
if ( empty( $slug ) ) { |
| 117 |
return new \WP_Error( 'empty_arg', __( 'Argument should not be empty.', 'ablocks' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 121 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 122 |
include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php'; |
| 123 |
|
| 124 |
$plugin_data = $this->get_remote_plugin_data( $slug ); |
| 125 |
|
| 126 |
if ( is_wp_error( $plugin_data ) ) { |
| 127 |
return $plugin_data; |
| 128 |
} |
| 129 |
|
| 130 |
$upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() ); |
| 131 |
|
| 132 |
// install plugin |
| 133 |
$install = $upgrader->install( $plugin_data->download_link ); |
| 134 |
|
| 135 |
if ( is_wp_error( $install ) ) { |
| 136 |
return $install; |
| 137 |
} |
| 138 |
|
| 139 |
// activate plugin |
| 140 |
if ( $install === true && $active ) { |
| 141 |
$active = activate_plugin( $upgrader->plugin_info(), '', false, true ); |
| 142 |
|
| 143 |
if ( is_wp_error( $active ) ) { |
| 144 |
return $active; |
| 145 |
} |
| 146 |
|
| 147 |
return $active === null; |
| 148 |
} |
| 149 |
|
| 150 |
return $install; |
| 151 |
} |
| 152 |
|
| 153 |
public function get_remote_plugin_data( $slug = '' ) { |
| 154 |
if ( empty( $slug ) ) { |
| 155 |
return new \WP_Error( 'empty_arg', __( 'Argument should not be empty.', 'ablocks' ) ); |
| 156 |
} |
| 157 |
|
| 158 |
$response = wp_remote_post( |
| 159 |
'http://api.wordpress.org/plugins/info/1.0/', |
| 160 |
array( |
| 161 |
'body' => array( |
| 162 |
'action' => 'plugin_information', |
| 163 |
'request' => maybe_serialize( (object) array( |
| 164 |
'slug' => $slug, |
| 165 |
'fields' => array( |
| 166 |
'version' => false, |
| 167 |
), |
| 168 |
)), |
| 169 |
), |
| 170 |
) |
| 171 |
); |
| 172 |
|
| 173 |
if ( is_wp_error( $response ) ) { |
| 174 |
return $response; |
| 175 |
} |
| 176 |
|
| 177 |
return maybe_unserialize( wp_remote_retrieve_body( $response ) ); |
| 178 |
} |
| 179 |
|
| 180 |
public function download_google_fonts() { |
| 181 |
global $ablocks_fonts; |
| 182 |
$fontDownloader = new FontLoadLocally(); |
| 183 |
$fontDownloader->process_font_queue( $ablocks_fonts ); |
| 184 |
} |
| 185 |
} |
| 186 |
|