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