PluginProbe
bBlocks – Essential Gutenberg Blocks & Patterns Collection / 2.1.2
bBlocks – Essential Gutenberg Blocks & Patterns Collection v2.1.2
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.1.2, at includes/Dashboard.php

223 lines 9.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 (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'wp_ajax')) {
18 wp_send_json_error(['message' => 'Invalid nonce or request.'], 400);
19 }
20
21 if (!current_user_can('manage_options')) {
22 wp_send_json_error(['message' => 'Insufficient permissions.'], 403);
23 }
24
25 if ( !function_exists( 'plugins_api' ) ) {
26 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
27 }
28
29 $cached_plugins = plugins_api( 'query_plugins', array(
30 'author' => 'bplugins',
31 'per_page' => 100
32 ) );
33
34 wp_send_json_success($cached_plugins->plugins);
35 }
36
37 public function get_active_plugins() {
38 if (!isset($_GET['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), 'wp_ajax')) {
39 wp_send_json_error(['message' => 'Invalid nonce or request.'], 400);
40 }
41
42 if (!current_user_can('manage_options')) {
43 wp_send_json_error(['message' => 'Insufficient permissions.'], 403);
44 }
45
46 // Get the list of all installed plugins
47 if (!function_exists('get_plugins')) {
48 include_once ABSPATH . '/wp-admin/includes/plugin.php';
49 }
50
51 $installed_plugins = get_plugins();
52
53 // Return the plugin basenames as an array
54 $installed_plugin_slugs = array_keys($installed_plugins);
55
56 wp_send_json_success($installed_plugin_slugs);
57 }
58 public function display_activation_notice() {
59 // Check if transient is set
60 $plugin_slug = get_transient('bblocks_show_activation_notice');
61
62 $first_part = explode("/", $plugin_slug)[0];
63 $cleaned_string = str_replace("-", " ", $first_part);
64
65 if ($plugin_slug) {
66 // Remove transient after displaying the notice
67 delete_transient('bblocks_show_activation_notice');
68
69 // Generate activation URL
70 $activation_url = wp_nonce_url(
71 admin_url('plugins.php?action=activate&plugin=' . $plugin_slug),
72 'activate-plugin_' . $plugin_slug
73 );
74
75 // Display notice with activation button
76 ?>
77 <div class="notice notice-success is-dismissible advScrollbar-notice">
78 <p><?php echo esc_html( $cleaned_string . ' ' . __( 'plugin was successfully installed.', 'b-blocks' ) ); ?></p>
79 <p>
80 <a href="<?php echo esc_url($activation_url); ?>" class="button button-primary">
81 <?php esc_html_e('Activate Plugin', 'b-blocks'); ?>
82 </a>
83 </p>
84 </div>
85 <?php
86 }
87 }
88 public function activated_block() {
89 // Verify nonce
90 if (!isset($_GET['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['nonce'])), 'wp_ajax')) {
91 wp_send_json_error(['message' => 'Invalid nonce or request.'], 400);
92 }
93
94 if (!current_user_can('install_plugins')) {
95 wp_send_json_error(['message' => 'Insufficient permissions.'], 403);
96 }
97
98 $plugin_name = sanitize_text_field($_GET['plugin_name'] ?? '');
99
100 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
101 include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
102
103 try {
104 // Fetch plugin information
105 $api = plugins_api('plugin_information', ['slug' => $plugin_name, 'fields' => ['sections' => false]]);
106 if (is_wp_error($api)) {
107 wp_send_json_error(['message' => 'Failed to fetch plugin information.']);
108 }
109
110 // Suppress unexpected output
111 ob_start();
112 $upgrader = new Plugin_Upgrader();
113 $result = $upgrader->install($api->download_link);
114 ob_end_clean();
115
116 $plugin_slug = "{$plugin_name}/{$plugin_name}.php";
117
118 if ($result) {
119 // Set transient to show notice
120 set_transient('bblocks_show_activation_notice', $plugin_slug, 1000000); // Valid for 60 seconds
121 $redirect_url = admin_url('plugins.php?plugin_status=all');
122 wp_send_json_success(['message' => 'Plugin installed successfully.', 'redirectUrl' => $redirect_url]);
123
124 } else {
125 wp_send_json_error(['message' => 'Plugin installation failed.']);
126 }
127 } catch (Exception $e) {
128 wp_send_json_error(['message' => 'An unexpected error occurred: ' . $e->getMessage()]);
129 }
130 }
131
132 public function bBlocksDisabledBlocks(){
133 $nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ?? '' ) );
134
135 if( !wp_verify_nonce( $nonce, 'wp_ajax' )){
136 wp_send_json_error( 'Invalid Request' );
137 }
138
139 if (!current_user_can('manage_options')) {
140 wp_send_json_error( 'Unauthorized' );
141 }
142
143 $raw = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitized below after json_decode
144 $data = is_string( $raw ) ? json_decode( $raw, true ) : null;
145 $db_data = get_option( 'bBlocksDisabledBlocks', [] );
146
147 if ( ! isset( $data ) && $db_data ) {
148 wp_send_json_success( $db_data );
149 }
150
151 // Sanitize: the value is an array of block-name strings (e.g. "b-blocks/alert").
152 if ( is_array( $data ) ) {
153 $data = array_map( 'sanitize_text_field', $data );
154 } else {
155 $data = [];
156 }
157
158 update_option( 'bBlocksDisabledBlocks', $data );
159 wp_send_json_success( $data );
160
161
162 // AI generated
163 // if ( isset( $_POST['data'] ) ) {
164 // $data = json_decode( stripslashes( $_POST['data'] ), true );
165 // update_option( 'bBlocksDisabledBlocks', $data );
166 // wp_send_json_success( $data );
167 // }
168
169 // $db_data = get_option( 'bBlocksDisabledBlocks', [] );
170 // wp_send_json_success( $db_data );
171 }
172
173 function adminEnqueueScripts( $hook ) {
174 if( str_contains( $hook, 'b-blocks' ) ){
175 wp_enqueue_media();
176
177 wp_enqueue_style( 'b-blocks-dashboard-style', B_BLOCKS_BUILD . 'admin-dashboard.css', ['wp-components','wp-edit-blocks','wp-block-editor'], B_BLOCKS_VERSION );
178
179 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 );
180
181 wp_localize_script( 'b-blocks-dashboard-script', 'pluginAction', [
182 'ajaxUrl' => admin_url('admin-ajax.php'),
183 ]);
184 }
185 }
186
187 function adminMenu() {
188 $menuIcon = "<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 500 500' fill='#fff'>
189 <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' />
190 </svg>";
191
192 add_menu_page(
193 __( 'bBlocks', 'b-blocks' ),
194 __( 'bBlocks', 'b-blocks' ),
195 'manage_options',
196 'b-blocks',
197 '',
198 'data:image/svg+xml;base64,' . base64_encode( $menuIcon ),
199 20
200 );
201 add_submenu_page(
202 'b-blocks',
203 __( 'Dashboard', 'b-blocks' ),
204 __( 'Dashboard', 'b-blocks' ),
205 'manage_options',
206 'b-blocks',
207 [$this, 'dashboardPage']
208 );
209 }
210
211 function dashboardPage(){ ?>
212 <div id='bBlocksDashboard' data-info='<?php echo esc_attr( wp_json_encode([
213 'version' => B_BLOCKS_VERSION,
214 'nonce' => wp_create_nonce( 'wp_ajax' ),
215 'dirUrl' => B_BLOCKS_DIR_URL,
216 'isPremium' => Utils::isPro(),
217 'hasPro' => B_BLOCKS_HAS_PRO,
218 'licenseActiveNonce' => wp_create_nonce("bBlocksLicenseActivation")
219 ]) ); ?>'></div>
220 <?php }
221 }
222 new BBlocksDashboard();
223 }