| @@ -7,15 +7,24 @@ | ||
| 7 | 7 | * @since 4.04.04 |
| 8 | 8 | */ |
| 9 | 9 | class FrmInstallPlugin { |
| 10 | 10 | |
| 11 | - protected $plugin_file; // format: folder/filename.php | |
| 11 | + /** | |
| 12 | + * Format: folder/filename.php. | |
| 13 | + * | |
| 14 | + * @var string | |
| 15 | + */ | |
| 16 | + protected $plugin_file; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * @var string | |
| 20 | + */ | |
| 12 | 21 | protected $plugin_slug; |
| 13 | 22 | |
| 14 | 23 | public function __construct( $atts ) { |
| 15 | - $this->plugin_file = $atts['plugin_file']; | |
| 24 | + $this->plugin_file = $atts['plugin_file']; | |
| 16 | 25 | list( $slug, $file ) = explode( '/', $this->plugin_file ); |
| 17 | - $this->plugin_slug = $slug; | |
| 26 | + $this->plugin_slug = $slug; | |
| 18 | 27 | } |
| 19 | 28 | |
| 20 | 29 | public function get_activate_link() { |
| 21 | 30 | if ( $this->is_installed() && $this->is_active() ) { |
| @@ -55,6 +64,105 @@ | ||
| 55 | 64 | * @return string |
| 56 | 65 | */ |
| 57 | 66 | protected function activate_url() { |
| 58 | 67 | return wp_nonce_url( self_admin_url( 'plugins.php?action=activate&plugin=' . $this->plugin_file ), 'activate-plugin_' . $this->plugin_file ); |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Handles the AJAX request to install a plugin. | |
| 72 | + * | |
| 73 | + * @since 6.9 | |
| 74 | + * | |
| 75 | + * @return void | |
| 76 | + */ | |
| 77 | + public static function ajax_install_plugin() { | |
| 78 | + // Check permission and nonce. | |
| 79 | + FrmAppHelper::permission_check( 'install_plugins' ); | |
| 80 | + check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 81 | + | |
| 82 | + // Get posted data. | |
| 83 | + $plugin_slug = FrmAppHelper::get_post_param( 'plugin', '', 'sanitize_text_field' ); | |
| 84 | + | |
| 85 | + if ( ! empty( get_plugins()[ $plugin_slug ] ) ) { | |
| 86 | + $activate = activate_plugin( $plugin_slug ); | |
| 87 | + } else { | |
| 88 | + // Include necessary files for plugin installation. | |
| 89 | + require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 90 | + require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; | |
| 91 | + | |
| 92 | + // Get the plugin information. | |
| 93 | + $api = plugins_api( | |
| 94 | + 'plugin_information', | |
| 95 | + array( | |
| 96 | + 'slug' => $plugin_slug, | |
| 97 | + 'fields' => array( | |
| 98 | + 'sections' => false, | |
| 99 | + ), | |
| 100 | + ) | |
| 101 | + ); | |
| 102 | + if ( is_wp_error( $api ) ) { | |
| 103 | + wp_send_json_error( $api->get_error_message() ); | |
| 104 | + } | |
| 105 | + if ( ! FrmAddonsController::url_is_allowed( $api->versions['trunk'] ) ) { | |
| 106 | + wp_send_json_error( 'This download is not allowed' ); | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Set up the Plugin Upgrader. | |
| 110 | + $upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() ); | |
| 111 | + | |
| 112 | + // Install the plugin. | |
| 113 | + $result = $upgrader->install( $api->versions['trunk'] ); | |
| 114 | + if ( is_wp_error( $result ) ) { | |
| 115 | + wp_send_json_error( $result->get_error_message() ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Activate the plugin. | |
| 119 | + $activate = activate_plugin( $upgrader->plugin_info() ); | |
| 120 | + }//end if | |
| 121 | + | |
| 122 | + if ( is_wp_error( $activate ) ) { | |
| 123 | + wp_send_json_error( $activate->get_error_message() ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + if ( 'wp-mail-smtp' === $plugin_slug ) { | |
| 127 | + update_option( 'wp_mail_smtp_activation_prevent_redirect', true ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + // Send a success response. | |
| 131 | + wp_send_json_success( __( 'Plugin installed and activated successfully.', 'formidable' ) ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Checks plugin activation status via AJAX. | |
| 136 | + * | |
| 137 | + * @since 6.9 | |
| 138 | + * | |
| 139 | + * @return void | |
| 140 | + */ | |
| 141 | + public static function ajax_check_plugin_activation() { | |
| 142 | + // Check permission and nonce. | |
| 143 | + FrmAppHelper::permission_check( 'install_plugins' ); | |
| 144 | + check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 145 | + | |
| 146 | + // Retrieve plugin identifier. | |
| 147 | + $plugin_path = FrmAppHelper::get_post_param( 'plugin_path', '', 'sanitize_text_field' ); | |
| 148 | + | |
| 149 | + // Respond based on plugin status. | |
| 150 | + if ( is_plugin_active( $plugin_path ) ) { | |
| 151 | + wp_send_json_success(); | |
| 152 | + } else { | |
| 153 | + wp_send_json_error(); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Check if a plugin is installed. | |
| 159 | + * | |
| 160 | + * @since 6.16 | |
| 161 | + * | |
| 162 | + * @param string $plugin_file | |
| 163 | + * @return bool | |
| 164 | + */ | |
| 165 | + private static function is_plugin_installed( $plugin_file ) { | |
| 166 | + return isset( get_plugins()[ $plugin_file ] ); | |
| 59 | 167 | } |
| 60 | 168 | } |