PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.8.8
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.8.8
7.9.3 7.9.2 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.2.0 4.2.1 4.2.2 4.2.3 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 6.0.0 7.0.0 7.0.1 7.0.2 7.1.0 7.2.0 7.2.1 7.2.10 7.2.11 7.2.2 7.2.3 7.2.4 7.2.5 7.2.6 7.2.7 7.2.8 7.2.9 7.3.0 7.3.1 7.4.0 7.4.1 7.4.2 7.4.3 7.5.0 7.6.0 7.6.1 7.7.0 7.7.1 7.7.10 7.7.11 7.7.12 7.7.13 7.7.14 7.7.15 7.7.16 7.7.17 7.7.18 7.7.19 7.7.2 7.7.20 7.7.21 7.7.22 7.7.3 7.7.4 7.7.5 7.7.6 7.7.7 7.7.8 7.7.9 7.8.0 7.8.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1
the-post-grid / app / Controllers / Hooks / InstallPlugins.php
the-post-grid / app / Controllers / Hooks Last commit date
ActionHooks.php 8 months ago FilterHooks.php 8 months ago InstallPlugins.php 8 months ago
InstallPlugins.php
137 lines
1 <?php
2 /**
3 * Action Hooks class.
4 *
5 * @package RT_TPG
6 */
7
8 namespace RT\ThePostGrid\Controllers\Hooks;
9
10 // Do not allow directly accessing this file.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit( 'This script cannot be accessed directly.' );
13 }
14
15 use Plugin_Upgrader;
16 use RT\ThePostGrid\Helpers\Fns;
17 use WP_Ajax_Upgrader_Skin;
18 use WpOrg\Requests\Exception;
19
20 /**
21 * Action Hooks class.
22 */
23 class InstallPlugins {
24
25 /**
26 * Class init.
27 *
28 * @return void
29 */
30 public static function init() {
31 // Step 4: PHP — AJAX Handlers to Install and Activate Plugin
32
33 // Handle plugin installation via AJAX
34 add_action( 'wp_ajax_install_plugin', function() {
35 // Check permissions and nonce
36 $nonce_action = isset( $_POST['nonceID'] ) ? sanitize_text_field( $_POST['nonceID'] ) : '';
37 $nonce_value = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
38
39 if ( ! current_user_can( 'install_plugins' ) || ! Fns::verifyNonce() ) {
40 wp_send_json_error( [ 'message' => 'Permission denied' ] );
41 }
42
43
44 if ( empty( $_POST['slug'] ) ) {
45 wp_send_json_error( [ 'message' => 'Missing plugin slug' ] );
46 }
47
48 $slug = sanitize_key( $_POST['slug'] );
49
50 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
51 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
52 require_once ABSPATH . 'wp-admin/includes/file.php';
53 require_once ABSPATH . 'wp-admin/includes/misc.php';
54 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skins.php';
55
56 // Get plugin information from WordPress.org API
57 $api = plugins_api( 'plugin_information', [
58 'slug' => $slug,
59 'fields' => [ 'sections' => false ],
60 ] );
61
62 if ( is_wp_error( $api ) ) {
63 wp_send_json_error( [ 'message' => $api->get_error_message() ] );
64 }
65
66 // Ensure filesystem credentials are set up
67 $creds = request_filesystem_credentials( '', '', false, false, [] );
68 if ( ! WP_Filesystem( $creds ) ) {
69 wp_send_json_error( [ 'message' => 'Filesystem credentials error.' ] );
70 }
71
72 // Install the plugin
73 $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
74 $result = $upgrader->install( $api->download_link );
75
76 if ( is_wp_error( $result ) ) {
77 wp_send_json_error( [ 'message' => $result->get_error_message() ] );
78 }
79
80 if ( ! $upgrader->plugin_info() ) {
81 wp_send_json_error( [ 'message' => 'Plugin installation failed.' ] );
82 }
83
84 $plugin_file = $upgrader->plugin_info(); // e.g., classified-listing/classified-listing.php
85 wp_send_json_success( [ 'plugin' => $plugin_file ] );
86 } );
87
88 // Handle plugin activation via AJAX
89 add_action( 'wp_ajax_activate_plugin', function() {
90 $nonce_action = isset( $_POST['nonceID'] ) ? sanitize_text_field( $_POST['nonceID'] ) : '';
91 $nonce_value = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
92
93 if ( ! current_user_can( 'install_plugins' ) || ! Fns::verifyNonce() ) {
94 wp_send_json_error( [ 'message' => 'Permission denied' ] );
95 }
96
97 if ( empty( $_POST['plugin'] ) ) {
98 wp_send_json_error( [ 'message' => 'Missing plugin path' ] );
99 }
100
101 $plugin = sanitize_text_field( $_POST['plugin'] );
102
103 include_once ABSPATH . 'wp-admin/includes/plugin.php';
104
105 $result = activate_plugin( $plugin );
106
107 if ( is_wp_error( $result ) ) {
108 wp_send_json_error( [ 'message' => $result->get_error_message() ] );
109 }
110
111 wp_send_json_success();
112 } );
113
114 add_action( 'admin_head', function() {
115 // Get current screen object
116 $screen = get_current_screen();
117
118 // Check if we are on our desired page
119 if (
120 $screen &&
121 $screen->post_type === 'rttpg' &&
122 isset( $_GET['page'] ) &&
123 $_GET['page'] === 'rttpg_our_plugins'
124 ) {
125 // Remove default WordPress admin notices
126 remove_all_actions( 'admin_notices' );
127 remove_all_actions( 'all_admin_notices' );
128
129 // Optionally also hide them with CSS as fallback
130 echo '<style>.notice, .updated, .error, .is-dismissible { display: none !important; }</style>';
131 }
132 } );
133 }
134
135 }
136
137