install-addons.php
231 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Ajax plugin configuration |
| 4 | * |
| 5 | * @version 1.0 |
| 6 | */ |
| 7 | |
| 8 | // Exit if accessed directly |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | */ |
| 15 | function wfactory_600_install_components( $plugin_instance ) { |
| 16 | check_ajax_referer( 'updates' ); |
| 17 | |
| 18 | $slug = $plugin_instance->request->post( 'plugin', null, true ); |
| 19 | $action = $plugin_instance->request->post( 'plugin_action', null, true ); |
| 20 | $storage = $plugin_instance->request->post( 'storage', null, true ); |
| 21 | |
| 22 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 23 | wp_die( __( 'You don\'t have permission to perform this action.', 'robin-image-optimizer' ), __( 'Something went wrong. Please refresh and try again.', 'robin-image-optimizer' ), 403 ); |
| 24 | } |
| 25 | |
| 26 | if ( empty( $slug ) || empty( $action ) ) { |
| 27 | wp_send_json_error( [ 'error_message' => __( 'Required attributes are not passed or empty.', 'robin-image-optimizer' ) ] ); |
| 28 | } |
| 29 | $success = false; |
| 30 | $send_data = []; |
| 31 | |
| 32 | if ( $storage == 'internal' ) { |
| 33 | if ( $action == 'activate' ) { |
| 34 | if ( $plugin_instance->activateComponent( $slug ) ) { |
| 35 | $success = true; |
| 36 | } |
| 37 | } elseif ( $action == 'deactivate' ) { |
| 38 | |
| 39 | if ( $plugin_instance->deactivateComponent( $slug ) ) { |
| 40 | $success = true; |
| 41 | } |
| 42 | } else { |
| 43 | wp_send_json_error( [ 'error_message' => __( 'You are trying to perform an invalid action.', 'robin-image-optimizer' ) ] ); |
| 44 | } |
| 45 | } elseif ( $storage == 'WordPress' || $storage == 'creativemotion' ) { |
| 46 | if ( ! empty( $slug ) ) { |
| 47 | $network_wide = $plugin_instance->isNetworkActive(); |
| 48 | |
| 49 | if ( $action == 'activate' ) { |
| 50 | $result = activate_plugin( $slug, '', $network_wide ); |
| 51 | |
| 52 | if ( is_wp_error( $result ) ) { |
| 53 | wp_send_json_error( [ 'error_message' => $result->get_error_message() ] ); |
| 54 | } |
| 55 | } elseif ( $action == 'deactivate' ) { |
| 56 | deactivate_plugins( $slug, false, $network_wide ); |
| 57 | } |
| 58 | |
| 59 | $success = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if ( $action == 'install' || $action == 'deactivate' ) { |
| 64 | try { |
| 65 | // Delete button |
| 66 | $delete_button = $plugin_instance->get_delete_component_button( $storage, $slug ); |
| 67 | $send_data['delete_button'] = $delete_button->get_button(); |
| 68 | } catch ( Exception $e ) { |
| 69 | wp_send_json_error( [ 'error_message' => $e->getMessage() ] ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Если требуется обновить постоянные ссылки, даем сигнал, что пользователю, нужно показать |
| 74 | // всплывающее уведомление. |
| 75 | // todo: сделать более красивое решение с передачей текстовы� |
| 76 | сообщений |
| 77 | /* |
| 78 | if ( $action == 'deactivate' ) { |
| 79 | $is_need_rewrite_rules = $plugin_instance->getPopulateOption( 'need_rewrite_rules' ); |
| 80 | if ( $is_need_rewrite_rules ) { |
| 81 | $send_data['need_rewrite_rules'] = sprintf( '<span class="wbcr-clr-need-rewrite-rules-message">' . __( 'When you deactivate some components, permanent links may work incorrectly. If this happens, please, <a href="%s">update the permalinks</a>, so you could complete the deactivation.', 'wbcr_factory_600' ), admin_url( 'options-permalink.php' ) . '</span>' ); |
| 82 | } |
| 83 | }*/ |
| 84 | |
| 85 | if ( $success ) { |
| 86 | if ( 'wbcr_clearfy' === $plugin_instance->getPluginName() ) { |
| 87 | do_action( 'wbcr_clearfy_update_component', $slug, $action, $storage ); |
| 88 | } |
| 89 | do_action( "wfactory/updated_{$plugin_instance->getPluginName()}_component", $slug, $action, $storage ); |
| 90 | |
| 91 | wp_send_json_success( $send_data ); |
| 92 | } |
| 93 | |
| 94 | wp_send_json_error( [ 'error_message' => __( 'An unknown error occurred during the activation of the component.', 'robin-image-optimizer' ) ] ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Ajax event that calls the wbcr/clearfy/activated_component action, |
| 99 | * to get the component to work. Usually this is a call to the installation functions, |
| 100 | * but in some cases, overwriting permanent references or compatibility checks. |
| 101 | */ |
| 102 | function wfactory_600_prepare_component( $plugin_instance ) { |
| 103 | check_ajax_referer( 'updates' ); |
| 104 | |
| 105 | $component_name = $plugin_instance->request->post( 'plugin', null, true ); |
| 106 | |
| 107 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 108 | wp_send_json_error( [ 'error_message' => __( 'You don\'t have permission to perform this action.', 'robin-image-optimizer' ) ], 403 ); |
| 109 | } |
| 110 | |
| 111 | if ( empty( $component_name ) ) { |
| 112 | wp_send_json_error( [ 'error_message' => __( 'Required attribute [component_name] is empty.', 'robin-image-optimizer' ) ] ); |
| 113 | } |
| 114 | if ( 'wbcr_clearfy' === $plugin_instance->getPluginName() ) { |
| 115 | do_action( 'wbcr/clearfy/activated_component', $component_name ); |
| 116 | } |
| 117 | do_action( "wfactory/activated_{$plugin_instance->getPluginName()}_component", $component_name ); |
| 118 | |
| 119 | wp_send_json_success(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Ajax handler for installing a plugin. |
| 124 | * |
| 125 | * @since 4.6.0 |
| 126 | * |
| 127 | * @see Plugin_Upgrader |
| 128 | * |
| 129 | * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. |
| 130 | */ |
| 131 | function wfactory_600_creativemotion_install_plugin( $plugin_instance ) { |
| 132 | check_ajax_referer( 'updates' ); |
| 133 | |
| 134 | if ( empty( $_POST['slug'] ) ) { |
| 135 | wp_send_json_error( |
| 136 | [ |
| 137 | 'slug' => '', |
| 138 | 'errorCode' => 'no_plugin_specified', |
| 139 | 'errorMessage' => __( 'No plugin specified.', 'robin-image-optimizer' ), |
| 140 | ] |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | $status = [ |
| 145 | 'install' => 'plugin', |
| 146 | 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), |
| 147 | ]; |
| 148 | |
| 149 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 150 | $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.', 'robin-image-optimizer' ); |
| 151 | wp_send_json_error( $status ); |
| 152 | } |
| 153 | |
| 154 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 155 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 156 | |
| 157 | $api = plugins_api( |
| 158 | 'plugin_information', |
| 159 | [ |
| 160 | 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), |
| 161 | 'fields' => [ |
| 162 | 'sections' => false, |
| 163 | ], |
| 164 | ] |
| 165 | ); |
| 166 | |
| 167 | if ( is_wp_error( $api ) ) { |
| 168 | $status['errorMessage'] = $api->get_error_message(); |
| 169 | wp_send_json_error( $status ); |
| 170 | } |
| 171 | |
| 172 | $status['pluginName'] = $api->name; |
| 173 | |
| 174 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 175 | $upgrader = new Plugin_Upgrader( $skin ); |
| 176 | // $result = $upgrader->install($api->download_link); |
| 177 | $result = $upgrader->install( 'https://clearfy.pro/components/download.php' ); |
| 178 | |
| 179 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 180 | $status['debug'] = $skin->get_upgrade_messages(); |
| 181 | } |
| 182 | |
| 183 | if ( is_wp_error( $result ) ) { |
| 184 | $status['errorCode'] = $result->get_error_code(); |
| 185 | $status['errorMessage'] = $result->get_error_message(); |
| 186 | wp_send_json_error( $status ); |
| 187 | } elseif ( is_wp_error( $skin->result ) ) { |
| 188 | $status['errorCode'] = $skin->result->get_error_code(); |
| 189 | $status['errorMessage'] = $skin->result->get_error_message(); |
| 190 | wp_send_json_error( $status ); |
| 191 | } elseif ( $skin->get_errors()->has_errors() ) { |
| 192 | $status['errorMessage'] = $skin->get_error_messages(); |
| 193 | wp_send_json_error( $status ); |
| 194 | } elseif ( is_null( $result ) ) { |
| 195 | global $wp_filesystem; |
| 196 | |
| 197 | $status['errorCode'] = 'unable_to_connect_to_filesystem'; |
| 198 | $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.', 'robin-image-optimizer' ); |
| 199 | |
| 200 | // Pass through the error from WP_Filesystem if one was raised. |
| 201 | if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { |
| 202 | $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); |
| 203 | } |
| 204 | |
| 205 | wp_send_json_error( $status ); |
| 206 | } |
| 207 | |
| 208 | $install_status = install_plugin_install_status( $api ); |
| 209 | $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; |
| 210 | |
| 211 | // If installation request is coming from import page, do not return network activation link. |
| 212 | $plugins_url = ( 'import' === $pagenow ) ? admin_url( 'plugins.php' ) : network_admin_url( 'plugins.php' ); |
| 213 | |
| 214 | if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) { |
| 215 | $status['activateUrl'] = add_query_arg( |
| 216 | [ |
| 217 | '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ), |
| 218 | 'action' => 'activate', |
| 219 | 'plugin' => $install_status['file'], |
| 220 | ], |
| 221 | $plugins_url |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | if ( is_multisite() && current_user_can( 'manage_network_plugins' ) && 'import' !== $pagenow ) { |
| 226 | $status['activateUrl'] = add_query_arg( [ 'networkwide' => 1 ], $status['activateUrl'] ); |
| 227 | } |
| 228 | |
| 229 | wp_send_json_success( $status ); |
| 230 | } |
| 231 |