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 |