/** * print_r() alternative * * @param mixed $value Value to debug */ function fw_print($value) { static $first_time = true; if ($first_time) { ob_start(); echo '<style type="text/css"> div.fw_print_r { max-height: 500px; overflow-y: scroll; background: #23282d; margin: 10px 30px; padding: 0; border: 1px solid #F5F5F5; border-radius: 3px; position: relative; z-index: 11111; } div.fw_print_r pre { color: #78FF5B; background: #23282d; text-shadow: 1px 1px 0 #000; font-family: Consolas, monospace; font-size: 12px; margin: 0; padding: 5px; display: block; line-height: 16px; text-align: left; } div.fw_print_r_group { background: #f1f1f1; margin: 10px 30px; padding: 1px; border-radius: 5px; } div.fw_print_r_group div.fw_print_r { margin: 9px; border-width: 0; } </style>'; echo str_replace(array(' ', "\n"), '', ob_get_clean()); $first_time = false; } if (func_num_args() == 1) { echo '<div class="fw_print_r"><pre>'; echo fw_htmlspecialchars(FW_Dumper::dump($value)); echo '</pre></div>'; } else { echo '<div class="fw_print_r_group">'; foreach (func_get_args() as $param) { fw_print($param); } echo '</div>'; } }
/** * @internal */ public function _action_update_extensions() { $nonce_name = '_nonce_fw_ext_update_extensions'; if (!isset($_POST[$nonce_name]) || !wp_verify_nonce($_POST[$nonce_name])) { wp_die(__('Invalid nonce.', 'fw')); } require_once ABSPATH . 'wp-admin/admin-header.php'; fw_print('Hello'); require_once ABSPATH . 'wp-admin/admin-footer.php'; }
/** * Remove extensions * @param array $extensions {'ext_1' => array(), 'ext_2' => array(), ...} * @param array $opts * @return WP_Error|bool|array * true: when all extensions succeeded * array: when some/all failed */ public function uninstall_extensions(array $extensions, $opts = array()) { $opts = array_merge(array('cancel_on_error' => false, 'verbose' => false), $opts); $cancel_on_error = $opts['cancel_on_error']; // fixme: install back successfully removed extensions before error? $verbose = $opts['verbose']; unset($opts); if (!$this->can_install()) { return new WP_Error('access_denied', __('You have no permissions to uninstall extensions', 'fw')); } if (empty($extensions)) { return new WP_Error('no_extensions', __('No extensions provided', 'fw')); } /** @var WP_Filesystem_Base $wp_filesystem */ global $wp_filesystem; if (!$wp_filesystem || is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { return new WP_Error('fs_not_initialized', __('WP Filesystem is not initialized', 'fw')); } $installed_extensions = $this->get_installed_extensions(); $extensions_before_uninstall = array_fill_keys(array_keys($installed_extensions), array()); $result = $uninstalled_extensions = array(); $has_errors = false; while (!empty($extensions)) { $not_used_var = reset($extensions); $extension_name = key($extensions); unset($extensions[$extension_name]); $extension_title = $this->get_extension_title($extension_name); if (!isset($installed_extensions[$extension_name])) { // already deleted $result[$extension_name] = true; continue; } if (!isset($installed_extensions[$extension_name]['path']) || empty($installed_extensions[$extension_name]['path'])) { /** * This happens sometimes, but I don't know why * If the script will continue, it will delete the root folder */ fw_print('Please report this to https://github.com/ThemeFuse/Unyson/issues', $extension_name, $installed_extensions); die; } $wp_fs_extension_path = FW_WP_Filesystem::real_path_to_filesystem_path($installed_extensions[$extension_name]['path']); if (!$wp_filesystem->exists($wp_fs_extension_path)) { // already deleted, maybe because it was a sub-extension of an deleted extension $result[$extension_name] = true; continue; } if ($verbose) { $verbose_message = sprintf(__('Deleting the "%s" extension...', 'fw'), $extension_title); if (is_subclass_of($verbose, 'WP_Upgrader_Skin')) { $verbose->feedback($verbose_message); } else { echo fw_html_tag('p', array(), $verbose_message); } } if (!$wp_filesystem->delete($wp_fs_extension_path, true, 'd')) { $result[$extension_name] = new WP_Error('cannot_delete_directory', sprintf(__('Cannot delete the "%s" extension.', 'fw'), $extension_title)); $has_errors = true; if ($cancel_on_error) { break; } else { continue; } } else { if ($verbose) { $verbose_message = sprintf(__('The %s extension has been successfully deleted.', 'fw'), $extension_title); if (is_subclass_of($verbose, 'WP_Upgrader_Skin')) { $verbose->feedback($verbose_message); } else { echo fw_html_tag('p', array(), $verbose_message); } } $result[$extension_name] = true; } unset($installed_extensions); $installed_extensions = $this->get_installed_extensions(true); $not_used_extensions = array_fill_keys(array_keys(array_diff_key($installed_extensions, $this->get_used_extensions($extensions, array_keys($installed_extensions)))), array()); $extensions = array_merge($extensions, $not_used_extensions); } do_action('fw_extensions_uninstall', $result); if ($cancel_on_error && $has_errors) { if (($last_result = end($result)) && is_wp_error($last_result)) { return $last_result; } else { // this should not happen, but just to be sure (for the future, if the code above will be changed) return new WP_Error('uninstall_failed', _n('Cannot uninstall extension', 'Cannot uninstall extensions', count($extensions), 'fw')); } } update_option(fw()->extensions->_get_active_extensions_db_option_name(), array_diff_key(fw()->extensions->_get_db_active_extensions(), array_diff_key($extensions_before_uninstall, $installed_extensions))); if ($has_errors) { return $result; } else { return true; } }
private function display_delete_page() { $flash_id = 'fw_extensions_delete'; if (!$this->can_install()) { FW_Flash_Messages::add($flash_id, __('You are not allowed to delete extensions.', 'fw'), 'error'); $this->js_redirect(); return; } $installed_extensions = $this->get_installed_extensions(); $extensions = array_fill_keys(array_map('trim', explode(',', FW_Request::GET('extension', ''))), array()); $error = ''; do { foreach ($extensions as $extension_name => $x) { if (empty($extension_name)) { unset($extensions[$extension_name]); continue; } if (!isset($installed_extensions[$extension_name])) { $error = sprintf(__('Extension "%s" is not installed.', 'fw'), $this->get_extension_title($extension_name)); break 2; } } if (empty($extensions)) { $error = __('No extensions to delete.', 'fw'); break; } } while (false); if ($error) { FW_Flash_Messages::add($flash_id, $error, 'error'); $this->js_redirect(); return; } if (!class_exists('_FW_Extensions_Delete_Upgrader_Skin')) { fw_include_file_isolated(dirname(__FILE__) . '/includes/class--fw-extensions-delete-upgrader-skin.php'); } $skin = new _FW_Extensions_Delete_Upgrader_Skin(array('title' => _n('Delete Extension', 'Delete Extensions', count($extensions), 'fw'))); $skin->header(); do { $nonce = $this->get_nonce('delete'); if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST[$nonce['name']]) || !wp_verify_nonce($_POST[$nonce['name']], $nonce['action'])) { $skin->error(__('Invalid nonce.', 'fw')); } if (!FW_WP_Filesystem::request_access(fw_get_framework_directory('/extensions'), fw_current_url(), array($nonce['name']))) { break; } // add sub-extensions for deletion foreach (array_keys($extensions) as $extension_name) { foreach ($this->collect_sub_extensions($extension_name, $installed_extensions) as $sub_extension_name => $sub_extension_data) { $extensions[$sub_extension_name] = array(); } } $not_used_extensions = array_fill_keys(array_keys(array_diff_key($installed_extensions, $this->get_used_extensions($extensions, array_keys($installed_extensions)))), array()); $extensions = array_merge($extensions, $not_used_extensions); /** @var WP_Filesystem_Base $wp_filesystem */ global $wp_filesystem; foreach ($extensions as $extension_name => $x) { if (!isset($installed_extensions[$extension_name])) { $skin->error(sprintf(__('Extension "%s" is not installed.', 'fw'), $this->get_extension_title($extension_name))); continue; } if (!isset($installed_extensions[$extension_name]['path']) || empty($installed_extensions[$extension_name]['path'])) { // this happens sometimes, but I don't know why fw_print($extension_name, $installed_extensions); die; } $extension_title = $this->get_extension_title($extension_name); $wp_fs_extension_path = FW_WP_Filesystem::real_path_to_filesystem_path($installed_extensions[$extension_name]['path']); if (!$wp_filesystem->exists($wp_fs_extension_path)) { // already deleted, maybe because it was a sub-extension of an deleted extension continue; } $skin->feedback(sprintf(__('Deleting the "%s" extension...', 'fw'), $extension_title)); if (!$wp_filesystem->delete($wp_fs_extension_path, true, 'd')) { $skin->error(sprintf(__('Cannot delete the "%s" extension.', 'fw'), $extension_title)); } else { $skin->feedback(sprintf(__('%s extension successfully delete.', 'fw'), $extension_title)); $skin->set_result(true); } } $db_active_extensions = fw()->extensions->_get_db_active_extensions(); $db_active_extensions = array_diff_key($db_active_extensions, $extensions); update_option(fw()->extensions->_get_active_extensions_db_option_name(), $db_active_extensions); unset($db_active_extensions); $skin->after(array('extensions_page_link' => $this->get_link())); } else { echo '<form method="post">'; wp_nonce_field($nonce['action'], $nonce['name']); fw_render_view(dirname(__FILE__) . '/views/delete-form.php', array('extension_names' => array_keys($extensions), 'installed_extensions' => $installed_extensions, 'list_page_link' => $this->get_link()), false); echo '</form>'; } } while (false); $skin->footer(); }