PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / 2.0.43
bBlocks – Essential Gutenberg Blocks & Patterns Collection v2.0.43
2.1.6 2.1.5 2.1.4 2.1.3 2.1.2 2.1.1 2.1.0 2.0.43 2.0.42 2.0.41 2.0.40 2.0.39 2.0.38 trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 106 releases
b-blocks / includes / Dashboard.php

Dashboard.php in bBlocks – Essential Gutenberg Blocks & Patterns Collection 2.0.43, at includes/Dashboard.php

198 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace BBlocks\Inc;
3
4 if( !class_exists( 'BBlocksDashboard' ) ){
5 class BBlocksDashboard{
6 function __construct(){
7 add_action('wp_ajax_bBlocksDisabledBlocks', [$this, 'bBlocksDisabledBlocks']);
8 add_action('wp_ajax_activated_block', [$this, 'activated_block']);
9 add_action('admin_notices', [$this, 'display_activation_notice']);
10 add_action('wp_ajax_get_active_plugins', [$this, 'get_active_plugins']);
11 add_action('wp_ajax_get_popular_plugins', [$this, 'get_popular_plugins']);
12 add_action( 'admin_enqueue_scripts', [$this, 'adminEnqueueScripts'] );
13 add_action( 'admin_menu', [$this, 'adminMenu'] );
14 }
15
16 public function get_popular_plugins () {
17 if ( !function_exists( 'plugins_api' ) ) {
18 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
19 }
20
21 $cached_plugins = plugins_api( 'query_plugins', array(
22 'author' => 'bplugins',
23 'per_page' => 100
24 ) );
25
26 wp_send_json_success($cached_plugins->plugins);
27 }
28
29 public function get_active_plugins() {
30 if (!isset($_GET['nonce']) || !wp_verify_nonce(sanitize_text_field($_GET['nonce']), 'wp_ajax')) {
31 wp_send_json_error(['message' => 'Invalid nonce or request.'], 400);
32 }
33
34 // Get the list of all installed plugins
35 if (!function_exists('get_plugins')) {
36 include_once ABSPATH . '/wp-admin/includes/plugin.php';
37 }
38
39 $installed_plugins = get_plugins();
40
41 // Return the plugin basenames as an array
42 $installed_plugin_slugs = array_keys($installed_plugins);
43
44 wp_send_json_success($installed_plugin_slugs);
45 }
46 public function display_activation_notice() {
47 // Check if transient is set
48 $plugin_slug = get_transient('bblocks_show_activation_notice');
49
50 $first_part = explode("/", $plugin_slug)[0];
51 $cleaned_string = str_replace("-", " ", $first_part);
52
53 if ($plugin_slug) {
54 // Remove transient after displaying the notice
55 delete_transient('bblocks_show_activation_notice');
56
57 // Generate activation URL
58 $activation_url = wp_nonce_url(
59 admin_url('plugins.php?action=activate&plugin=' . $plugin_slug),
60 'activate-plugin_' . $plugin_slug
61 );
62
63 // Display notice with activation button
64 ?>
65 <div class="notice notice-success is-dismissible advScrollbar-notice">
66 <p><?php esc_html_e(" $cleaned_string plugin was successfully installed.", 'advanced-scrollbar'); ?></p>
67 <p>
68 <a href="<?php echo esc_url($activation_url); ?>" class="button button-primary">
69 <?php esc_html_e('Activate Plugin', 'advanced-scrollbar'); ?>
70 </a>
71 </p>
72 </div>
73 <?php
74 }
75 }
76 public function activated_block() {
77 // Verify nonce
78 if (!isset($_GET['nonce']) || !wp_verify_nonce(sanitize_text_field($_GET['nonce']), 'wp_ajax')) {
79 wp_send_json_error(['message' => 'Invalid nonce or request.'], 400);
80 }
81
82 $plugin_name = sanitize_text_field($_GET['plugin_name']) ?? false;
83
84 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
85 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
86
87 try {
88 // Fetch plugin information
89 $api = plugins_api('plugin_information', ['slug' => $plugin_name, 'fields' => ['sections' => false]]);
90 if (is_wp_error($api)) {
91 wp_send_json_error(['message' => 'Failed to fetch plugin information.']);
92 }
93
94 // Suppress unexpected output
95 ob_start();
96 $upgrader = new Plugin_Upgrader();
97 $result = $upgrader->install($api->download_link);
98 ob_end_clean();
99
100 $plugin_slug = "{$plugin_name}/{$plugin_name}.php";
101
102 if ($result) {
103 // Set transient to show notice
104 set_transient('bblocks_show_activation_notice', $plugin_slug, 1000000); // Valid for 60 seconds
105 $redirect_url = admin_url('plugins.php?plugin_status=all');
106 wp_send_json_success(['message' => 'Plugin installed successfully.', 'redirectUrl' => $redirect_url]);
107
108 } else {
109 wp_send_json_error(['message' => 'Plugin installation failed.']);
110 }
111 } catch (Exception $e) {
112 wp_send_json_error(['message' => 'An unexpected error occurred: ' . $e->getMessage()]);
113 }
114 }
115
116 public function bBlocksDisabledBlocks(){
117 $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) ?? '';
118
119 if( !wp_verify_nonce( $nonce, 'wp_ajax' )){
120 wp_send_json_error( 'Invalid Request' );
121 }
122
123 if (!current_user_can('manage_options')) {
124 wp_send_json_error( 'Unauthorized' );
125 }
126
127 $data = json_decode( stripslashes( $_POST['data'] ), true );
128 $db_data = get_option( 'bBlocksDisabledBlocks', [] );
129
130 if( !isset( $data ) && $db_data ){
131 wp_send_json_success( $db_data );
132 }
133 update_option( 'bBlocksDisabledBlocks', $data );
134 wp_send_json_success( $data );
135
136
137 // AI generated
138 // if ( isset( $_POST['data'] ) ) {
139 // $data = json_decode( stripslashes( $_POST['data'] ), true );
140 // update_option( 'bBlocksDisabledBlocks', $data );
141 // wp_send_json_success( $data );
142 // }
143
144 // $db_data = get_option( 'bBlocksDisabledBlocks', [] );
145 // wp_send_json_success( $db_data );
146 }
147
148 function adminEnqueueScripts( $hook ) {
149 if( str_contains( $hook, 'b-blocks' ) ){
150 wp_enqueue_media();
151
152 wp_enqueue_style( 'b-blocks-dashboard-style', B_BLOCKS_BUILD . 'admin-dashboard.css', ['wp-components','wp-edit-blocks','wp-block-editor'], B_BLOCKS_VERSION );
153
154 wp_enqueue_script( 'b-blocks-dashboard-script', B_BLOCKS_BUILD . 'admin-dashboard.js', [ 'react', 'react-dom', 'wp-api', 'wp-util', 'wp-data', 'wp-components', 'wp-i18n', 'lodash', 'wp-media-utils', 'wp-core-data', 'wp-api-request', 'wp-element', 'wp-edit-post', 'wp-block-editor' ], B_BLOCKS_VERSION, true );
155
156 wp_localize_script( 'b-blocks-dashboard-script', 'pluginAction', [
157 'ajaxUrl' => admin_url('admin-ajax.php'),
158 ]);
159 }
160 }
161
162 function adminMenu() {
163 $menuIcon = "<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 500 500' fill='#fff'>
164 <path d='M57.7 50.1c-.4 5.1-.7 25.7-.7 45.9v36.7l11.3.6c6.1.4 16 .7 22 .7H101v230H51v92.1l131.3-.4c141.5-.4 139.3-.3 164.1-5.8 44-9.7 76.7-33.5 88.7-64.4 5.8-14.8 7.2-24.5 6.6-46-.6-21-2.2-30-7.7-44.3-8.2-21.3-25.2-38.7-50.9-52l-11-5.8 6.7-2.9c14.7-6.4 32-20.7 41.1-33.8 12-17.5 17-40.5 14.2-65.9-2.6-23.4-9.8-41-22.6-54.6-20.2-21.4-45.9-31.7-92.8-36.9-11.9-1.3-34.1-1.6-137.3-2l-123.1-.4-.6 9.2zm229.5 87.1c6.6 3.1 13 9.3 16 15.7 1.9 4 2.2 6.3 2.3 17.1 0 11-.3 13.1-2.3 17.5-6.1 13.2-18 18.8-43.6 20.5-7.8.5-16.7 1-19.8 1H234v-75.1l23.8.3c23.3.3 23.8.3 29.4 3zm-10.9 141.4c18.1 4.1 28.4 13.6 31.8 29.6 1.7 7.7.6 24.1-2 30.9-6.3 16.4-22.6 23.6-55.3 24.6l-16.8.6V277h17.6c12.5 0 19.6.5 24.7 1.6z' />
165 </svg>";
166
167 add_menu_page(
168 __( 'bBlocks', 'b-blocks' ),
169 __( 'bBlocks', 'b-blocks' ),
170 'manage_options',
171 'b-blocks',
172 '',
173 'data:image/svg+xml;base64,' . base64_encode( $menuIcon ),
174 20
175 );
176 add_submenu_page(
177 'b-blocks',
178 __( 'Dashboard', 'b-blocks' ),
179 __( 'Dashboard', 'b-blocks' ),
180 'manage_options',
181 'b-blocks',
182 [$this, 'dashboardPage']
183 );
184 }
185
186 function dashboardPage(){ ?>
187 <div id='bBlocksDashboard' data-info=<?php echo esc_attr( wp_json_encode([
188 'version' => B_BLOCKS_VERSION,
189 'nonce' => wp_create_nonce( 'wp_ajax' ),
190 'dirUrl' => B_BLOCKS_DIR_URL,
191 'isPremium' => Utils::isPro(),
192 'hasPro' => B_BLOCKS_HAS_PRO,
193 'licenseActiveNonce' => wp_create_nonce("bBlocksLicenseActivation")
194 ]) ); ?>></div>
195 <?php }
196 }
197 new BBlocksDashboard();
198 }