PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 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 All 80 releases
ablocks / includes / ajax / dashboard.php

dashboard.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/ajax/dashboard.php

141 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Helper;
12
13 class Dashboard extends AbstractAjaxHandler {
14 public function __construct() {
15 $this->actions = array(
16 'get_admin_menu_items' => array(
17 'callback' => array( $this, 'get_admin_menu_items' ),
18 ),
19 'regenerate_assets' => array(
20 'callback' => array( $this, 'regenerate_assets' ),
21 ),
22 'clear_demo_transients' => array(
23 'callback' => array( $this, 'clear_demo_transients' ),
24 ),
25 'install_academy_lms' => array(
26 'callback' => array( $this, 'install_academy_lms' ),
27 ),
28 );
29 }
30
31 public function get_admin_menu_items() {
32 $menu_items = wp_json_encode( Helper::get_admin_menu_list() );
33 wp_send_json_success( $menu_items );
34 }
35 public function regenerate_assets() {
36 $FileUpload = new FileUpload();
37 $has_delete = $FileUpload->delete_files();
38 wp_send_json_success( $has_delete );
39 }
40
41 public function clear_demo_transients() {
42 global $wpdb;
43
44 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_ablocks_demo_%' OR option_name LIKE '_transient_timeout_ablocks_demo_%'" );
45
46 if ( ! empty( $wpdb->last_error ) ) {
47 wp_send_json_error( $wpdb->last_error );
48 }
49
50 wp_send_json_success();
51 }
52 public function install_academy_lms() {
53 // Check user permissions
54 if ( ! current_user_can( 'install_plugins' ) ) {
55 wp_send_json_error( __( 'You do not have sufficient permissions to install plugins.', 'ablocks' ) );
56 }
57
58 // Check if the plugin is already installed
59 if ( ! Helper::is_plugin_installed( 'academy/academy.php' ) ) {
60
61 $plugin_status = $this->install_plugin( 'academy', true );
62 if ( $plugin_status ) {
63 wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) );
64 }
65 wp_send_json_error( __( 'Sorry, failed to download.', 'ablocks' ) );
66 }
67
68 // Activate the plugin
69 $activate_status = activate_plugin( 'academy/academy.php' );
70 if ( is_wp_error( $activate_status ) ) {
71 wp_send_json_error( 'Plugin activation failed: ' . $activate_status->get_error_message() );
72 }
73 wp_send_json_success( __( 'Plugin installed and activated successfully!', 'ablocks' ) );
74 }
75
76 public function install_plugin( $slug = '', $active = true ) {
77 if ( empty( $slug ) ) {
78 return new \WP_Error( 'empty_arg', __( 'Argument should not be empty.', 'ablocks' ) );
79 }
80
81 include_once ABSPATH . 'wp-admin/includes/file.php';
82 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
83 include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
84
85 $plugin_data = $this->get_remote_plugin_data( $slug );
86
87 if ( is_wp_error( $plugin_data ) ) {
88 return $plugin_data;
89 }
90
91 $upgrader = new \Plugin_Upgrader( new \Automatic_Upgrader_Skin() );
92
93 // install plugin
94 $install = $upgrader->install( $plugin_data->download_link );
95
96 if ( is_wp_error( $install ) ) {
97 return $install;
98 }
99
100 // activate plugin
101 if ( $install === true && $active ) {
102 $active = activate_plugin( $upgrader->plugin_info(), '', false, true );
103
104 if ( is_wp_error( $active ) ) {
105 return $active;
106 }
107
108 return $active === null;
109 }
110
111 return $install;
112 }
113
114 public function get_remote_plugin_data( $slug = '' ) {
115 if ( empty( $slug ) ) {
116 return new \WP_Error( 'empty_arg', __( 'Argument should not be empty.', 'ablocks' ) );
117 }
118
119 $response = wp_remote_post(
120 'http://api.wordpress.org/plugins/info/1.0/',
121 array(
122 'body' => array(
123 'action' => 'plugin_information',
124 'request' => maybe_serialize( (object) array(
125 'slug' => $slug,
126 'fields' => array(
127 'version' => false,
128 ),
129 )),
130 ),
131 )
132 );
133
134 if ( is_wp_error( $response ) ) {
135 return $response;
136 }
137
138 return maybe_unserialize( wp_remote_retrieve_body( $response ) );
139 }
140 }
141