| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* 'Plugin' functions for Redirection |
| 5 |
* |
| 6 |
* @phpstan-import-type DatabaseStatus from Red_Database_Status |
| 7 |
* @phpstan-import-type FixerJson from Red_Fixer |
| 8 |
*/ |
| 9 |
class Redirection_Api_Plugin extends Redirection_Api_Route { |
| 10 |
/** |
| 11 |
* Register REST routes for plugin actions |
| 12 |
* |
| 13 |
* @param string $api_namespace REST namespace. |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public function __construct( $api_namespace ) { |
| 17 |
// GET/POST /plugin - Get plugin status or run fixer |
| 18 |
register_rest_route( |
| 19 |
$api_namespace, |
| 20 |
'/plugin', |
| 21 |
[ |
| 22 |
[ |
| 23 |
'methods' => WP_REST_Server::READABLE, |
| 24 |
'callback' => [ $this, 'route_status' ], |
| 25 |
'permission_callback' => [ $this, 'permission_callback_manage' ], |
| 26 |
], |
| 27 |
[ |
| 28 |
'methods' => WP_REST_Server::EDITABLE, |
| 29 |
'callback' => [ $this, 'route_fixit' ], |
| 30 |
'permission_callback' => [ $this, 'permission_callback_manage' ], |
| 31 |
'args' => [ |
| 32 |
'name' => [ |
| 33 |
'description' => 'Name', |
| 34 |
'type' => 'string', |
| 35 |
], |
| 36 |
'value' => [ |
| 37 |
'description' => 'Value', |
| 38 |
'type' => 'string', |
| 39 |
], |
| 40 |
], |
| 41 |
], |
| 42 |
] |
| 43 |
); |
| 44 |
|
| 45 |
// POST /plugin/delete - Delete plugin data |
| 46 |
register_rest_route( |
| 47 |
$api_namespace, |
| 48 |
'/plugin/delete', |
| 49 |
[ |
| 50 |
[ |
| 51 |
'methods' => WP_REST_Server::EDITABLE, |
| 52 |
'callback' => [ $this, 'route_delete' ], |
| 53 |
'permission_callback' => [ $this, 'permission_callback_manage' ], |
| 54 |
], |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
// ALL /plugin/test - Test endpoint |
| 59 |
register_rest_route( |
| 60 |
$api_namespace, |
| 61 |
'/plugin/test', |
| 62 |
[ |
| 63 |
[ |
| 64 |
'methods' => WP_REST_Server::ALLMETHODS, |
| 65 |
'callback' => [ $this, 'route_test' ], |
| 66 |
'permission_callback' => [ $this, 'permission_callback_manage' ], |
| 67 |
], |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
// POST /plugin/data - Database upgrade/status |
| 72 |
register_rest_route( |
| 73 |
$api_namespace, |
| 74 |
'/plugin/data', |
| 75 |
[ |
| 76 |
[ |
| 77 |
'methods' => WP_REST_Server::EDITABLE, |
| 78 |
'callback' => [ $this, 'route_database' ], |
| 79 |
'permission_callback' => [ $this, 'permission_callback_manage' ], |
| 80 |
'args' => [ |
| 81 |
'upgrade' => [ |
| 82 |
'description' => 'Upgrade parameter', |
| 83 |
'type' => 'string', |
| 84 |
'enum' => [ |
| 85 |
'stop', |
| 86 |
'skip', |
| 87 |
'retry', |
| 88 |
], |
| 89 |
], |
| 90 |
], |
| 91 |
], |
| 92 |
] |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Permission callback for manage-level actions |
| 98 |
* |
| 99 |
* @param WP_REST_Request $request REST request. |
| 100 |
* @phpstan-param WP_REST_Request<array<string, mixed>> $request |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 104 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SUPPORT_MANAGE ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get fixer status/debug details |
| 109 |
* |
| 110 |
* @param WP_REST_Request $request REST request. |
| 111 |
* @phpstan-param WP_REST_Request<array<string, mixed>> $request |
| 112 |
* @return FixerJson |
| 113 |
*/ |
| 114 |
public function route_status( WP_REST_Request $request ) { |
| 115 |
include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; |
| 116 |
|
| 117 |
$fixer = new Red_Fixer(); |
| 118 |
return $fixer->get_json(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Run fixer or save a specific debug setting |
| 123 |
* |
| 124 |
* @param WP_REST_Request $request REST request. |
| 125 |
* @phpstan-param WP_REST_Request<array<string, mixed>> $request |
| 126 |
* @return FixerJson|WP_Error |
| 127 |
*/ |
| 128 |
public function route_fixit( WP_REST_Request $request ) { |
| 129 |
include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php'; |
| 130 |
|
| 131 |
$params = $request->get_params(); |
| 132 |
$fixer = new Red_Fixer(); |
| 133 |
|
| 134 |
if ( isset( $params['name'] ) && isset( $params['value'] ) ) { |
| 135 |
global $wpdb; |
| 136 |
|
| 137 |
$fixer->save_debug( sanitize_text_field( $params['name'] ), sanitize_text_field( $params['value'] ) ); |
| 138 |
|
| 139 |
$groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 ); |
| 140 |
if ( $groups === 0 ) { |
| 141 |
Red_Group::create( 'new group', 1 ); |
| 142 |
} |
| 143 |
} else { |
| 144 |
$fixer->fix( $fixer->get_status() ); |
| 145 |
} |
| 146 |
|
| 147 |
return $fixer->get_json(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Delete plugin (single-site only) and redirect back to plugins page |
| 152 |
* |
| 153 |
* @return array{location: string}|WP_Error |
| 154 |
*/ |
| 155 |
public function route_delete() { |
| 156 |
if ( is_multisite() ) { |
| 157 |
return new WP_Error( 'redirect_delete_multi', 'Multisite installations must delete the plugin from the network admin' ); |
| 158 |
} |
| 159 |
|
| 160 |
Redirection_Admin::plugin_uninstall(); |
| 161 |
|
| 162 |
$current = get_option( 'active_plugins' ); |
| 163 |
$plugin_position = array_search( basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), $current, true ); |
| 164 |
if ( $plugin_position !== false ) { |
| 165 |
array_splice( $current, (int) $plugin_position, 1 ); |
| 166 |
update_option( 'active_plugins', $current ); |
| 167 |
} |
| 168 |
|
| 169 |
return array( 'location' => admin_url() . 'plugins.php' ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Simple plugin test endpoint |
| 174 |
* |
| 175 |
* @param WP_REST_Request $request REST request. |
| 176 |
* @phpstan-param WP_REST_Request<array<string, mixed>> $request |
| 177 |
* @return array{success: true} |
| 178 |
*/ |
| 179 |
public function route_test( WP_REST_Request $request ) { |
| 180 |
return array( |
| 181 |
'success' => true, |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Database status/upgrade orchestration |
| 187 |
* |
| 188 |
* @param WP_REST_Request $request REST request. |
| 189 |
* @phpstan-param WP_REST_Request<array<string, mixed>> $request |
| 190 |
* @return DatabaseStatus |
| 191 |
*/ |
| 192 |
public function route_database( WP_REST_Request $request ) { |
| 193 |
$params = $request->get_params(); |
| 194 |
$status = new Red_Database_Status(); |
| 195 |
/** @var string|false $upgrade */ |
| 196 |
$upgrade = false; |
| 197 |
|
| 198 |
if ( isset( $params['upgrade'] ) && in_array( $params['upgrade'], [ 'stop', 'skip' ], true ) ) { |
| 199 |
$upgrade = sanitize_text_field( $params['upgrade'] ); |
| 200 |
} |
| 201 |
|
| 202 |
// Check upgrade |
| 203 |
if ( ! $status->needs_updating() && ! $status->needs_installing() ) { |
| 204 |
/* translators: version number */ |
| 205 |
$status->set_error( sprintf( __( 'Your database does not need updating to %s.', 'redirection' ), REDIRECTION_DB_VERSION ) ); |
| 206 |
|
| 207 |
return $status->get_json(); |
| 208 |
} |
| 209 |
|
| 210 |
if ( $upgrade === 'stop' ) { |
| 211 |
$status->stop_update(); |
| 212 |
} elseif ( $upgrade === 'skip' ) { |
| 213 |
$status->set_next_stage(); |
| 214 |
} |
| 215 |
|
| 216 |
$should_upgrade = $upgrade === false || $status->get_current_stage() !== false; |
| 217 |
if ( $should_upgrade ) { |
| 218 |
$database = new Red_Database(); |
| 219 |
$database->apply_upgrade( $status ); |
| 220 |
} |
| 221 |
|
| 222 |
return $status->get_json(); |
| 223 |
} |
| 224 |
} |
| 225 |
|