PluginProbe
Core Rollback / trunk
Core Rollback vtrunk
1.4.3 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.4.0 1.4.1 1.4.2
core-rollback / src / Settings.php

Settings.php in Core Rollback trunk, at src/Settings.php

208 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Core Rollback
4 *
5 * @author Andy Fragen
6 * @license MIT
7 * @link https://github.com/afragen/core-rollback
8 * @package core-rollback
9 */
10
11 namespace Fragen\Rollback;
12
13 /*
14 * Exit if called directly.
15 */
16 if ( ! defined( 'WPINC' ) ) {
17 die;
18 }
19
20 /**
21 * Class Settings
22 */
23 class Settings {
24 /**
25 * Load hooks.
26 *
27 * @return void
28 */
29 public function load_hooks() {
30 add_action( 'admin_init', [ $this, 'add_settings' ] );
31 add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'add_plugin_menu' ] );
32 add_action( 'admin_init', [ $this, 'update_settings' ] );
33 add_action( 'network_admin_edit_rollback', [ $this, 'update_settings' ] );
34 }
35
36 /**
37 * Add/register setttings.
38 *
39 * @return void
40 */
41 public function add_settings() {
42 register_setting( 'rollback_settings', 'rollback_settings' );
43 add_settings_section(
44 'core_rollback',
45 null,
46 [ $this, 'print_core_rollback' ],
47 'rollback'
48 );
49 add_settings_field(
50 'core_versions',
51 __( 'Core Releases', 'core-rollback' ),
52 [ $this, 'version_dropdown' ],
53 'rollback',
54 'core_rollback',
55 [ 'core' => new Core() ]
56 );
57 }
58
59 /**
60 * Print settings section blurb.
61 *
62 * @return void
63 */
64 public function print_core_rollback() {
65 global $wp_version;
66
67 echo '<div class="notice notice-warning fade">';
68 echo '<p>' . wp_kses_post( __( '<strong>WARNING:</strong> Downgrading WordPress Core may leave your site in an unusable state.', 'core-rollback' ) ) . '</p>';
69 echo '</div>';
70 echo '<div class="notice notice-info fade">';
71 echo '<p>' . wp_kses_post( __( '<strong>INFO:</strong> Depending upon your current PHP version, some versions of WordPress core will not be available for rollback.', 'core-rollback' ) ) . '</p>';
72 echo '</div>';
73
74 esc_html_e( 'Rollback to latest release or any outdated, secure release version of WordPress Core.', 'core-rollback' );
75
76 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
77 echo '<div class="notice notice-info fade is-dismissible">';
78 echo '<p>' . wp_kses_post( __( '<strong>INFO:</strong> Your site is currently using a block theme. Block themes require at least WordPress 5.9.', 'core-rollback' ) ) . '</p>';
79 echo '</div>';
80 }
81
82 echo '<ol>';
83 echo( '<li>' . esc_html__( 'Select the rollback version from the dropdown menu.', 'core-rollback' ) . '</li>' );
84 echo( '<li>' . esc_html__( 'Click the `Rollback` button. You will be automatically re-directed to the `update-core.php` page.', 'core-rollback' ) . '</li>' );
85 echo( '<li>' . esc_html__( 'Use the `Re-install` button to rollback to the selected version.', 'core-rollback' ) . '</li>' );
86 echo '</ol>';
87 }
88
89 /**
90 * Add options page.
91 */
92 public function add_plugin_menu() {
93 $parent = is_multisite() ? 'settings.php' : 'tools.php';
94 $capability = is_multisite() ? 'manage_network_options' : 'manage_options';
95
96 add_submenu_page(
97 $parent,
98 esc_html__( 'Rollback Core', 'core-rollback' ),
99 esc_html_x( 'Rollback Core', 'Menu item', 'core-rollback' ),
100 $capability,
101 'rollback',
102 [ $this, 'create_admin_page' ]
103 );
104 }
105
106 /**
107 * Options page callback.
108 */
109 public function create_admin_page() {
110 $action = is_multisite() ? 'edit.php?action=rollback' : 'options.php';
111 $form_action = 'update-core.php?action=do-core-reinstall';
112 $submit = __( 'Rollback', 'core-rollback' );
113 $disabled = ! empty( $this->filter_if_block_themes() ) ? '' : 'disabled';
114
115 echo '<div class="wrap">';
116 echo '<h2>' . esc_html__( 'Rollback Core', 'core-rollback' ) . '</h2>';
117 echo '<form method="post" action="' . esc_attr( $form_action ) . '" name="upgrade" class="upgrade">';
118 settings_fields( 'rollback_settings' );
119 do_settings_sections( 'rollback' );
120 submit_button( $submit, $disabled );
121 echo '</form>';
122 echo '</div>';
123 }
124
125 /**
126 * Make version dropdown.
127 *
128 * @param array $args Array of args, [ 'core' => new Core() ].
129 *
130 * @return void
131 */
132 public function version_dropdown( $args ) {
133 $items = array_keys( $args['core']::$core_versions );
134
135 // Filter out WP versions with deprecations and current PHP version.
136 $items = array_filter(
137 $items,
138 function ( $item ) {
139 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
140 return version_compare( $item, '5.9', '>=' );
141 }
142 if ( version_compare( phpversion(), '8.0', '<' ) ) {
143 return version_compare( $item, '4.0', '>=' );
144 } else {
145 return version_compare( $item, '5.3', '>=' );
146 }
147 }
148 );
149
150 if ( ! empty( $this->filter_if_block_themes() ) ) {
151 echo "<select id='core_dropdown' name='versions[core_dropdown]'>";
152 foreach ( $items as $item ) {
153 echo '<option value="' . esc_attr( $item ) . '">' . esc_attr( $item ) . '</option>';
154 }
155 echo '</select>';
156 }
157 }
158
159 /**
160 * Return array of core versions compatible with block themes.
161 *
162 * @return array
163 */
164 private function filter_if_block_themes() {
165 $core = new Core();
166 $items = array_keys( $core::$core_versions );
167 if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
168 $items = array_filter(
169 $items,
170 function ( $item ) {
171 return version_compare( $item, '5.9', '>=' );
172 }
173 );
174 }
175 return $items;
176 }
177
178 /**
179 * Update single site and network settings.
180 * Used when plugin is network activated to save settings.
181 *
182 * @link http://wordpress.stackexchange.com/questions/64968/settings-api-in-multisite-missing-update-message
183 * @link http://benohead.com/wordpress-network-wide-plugin-settings/
184 */
185 public function update_settings() {
186 // Exit if improper privileges.
187 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'rollback_settings-options' ) ) {
188 return;
189 }
190
191 if ( ! isset( $_POST['_wp_http_referer'] ) ) {
192 return false;
193 }
194
195 if ( isset( $_POST['option_page'] ) &&
196 'rollback_settings' === sanitize_title_with_dashes( wp_unslash( $_POST['option_page'] ) )
197 ) {
198 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
199 $options = isset( $_POST['versions'] ) ? wp_unslash( $_POST['versions'] ) : [];
200 // phpcs:enable
201
202 set_site_transient( '_core_rollback', $options, 15 );
203 wp_safe_redirect( \network_admin_url( 'update-core.php?force-check=1' ) );
204 exit;
205 }
206 }
207 }
208