PluginProbe
Redirection / 5.3.1
Redirection v5.3.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / api / api-plugin.php

api-plugin.php in Redirection 5.3.1, at api/api-plugin.php

138 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * 'Plugin' functions for Redirection
5 */
6 class Redirection_Api_Plugin extends Redirection_Api_Route {
7 public function __construct( $namespace ) {
8 register_rest_route( $namespace, '/plugin', array(
9 $this->get_route( WP_REST_Server::READABLE, 'route_status', [ $this, 'permission_callback_manage' ] ),
10 ) );
11
12 register_rest_route( $namespace, '/plugin', array(
13 $this->get_route( WP_REST_Server::EDITABLE, 'route_fixit', [ $this, 'permission_callback_manage' ] ),
14 'args' => [
15 'name' => array(
16 'description' => 'Name',
17 'type' => 'string',
18 ),
19 'value' => array(
20 'description' => 'Value',
21 'type' => 'string',
22 ),
23 ],
24 ) );
25
26 register_rest_route( $namespace, '/plugin/delete', array(
27 $this->get_route( WP_REST_Server::EDITABLE, 'route_delete', [ $this, 'permission_callback_manage' ] ),
28 ) );
29
30 register_rest_route( $namespace, '/plugin/test', array(
31 $this->get_route( WP_REST_Server::ALLMETHODS, 'route_test', [ $this, 'permission_callback_manage' ] ),
32 ) );
33
34 register_rest_route( $namespace, '/plugin/data', array(
35 $this->get_route( WP_REST_Server::EDITABLE, 'route_database', [ $this, 'permission_callback_manage' ] ),
36 'args' => [
37 'upgrade' => [
38 'description' => 'Upgrade parameter',
39 'type' => 'string',
40 'enum' => array(
41 'stop',
42 'skip',
43 'retry',
44 ),
45 ],
46 ],
47 ) );
48 }
49
50 public function permission_callback_manage( WP_REST_Request $request ) {
51 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SUPPORT_MANAGE );
52 }
53
54 public function route_status( WP_REST_Request $request ) {
55 include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
56
57 $fixer = new Red_Fixer();
58 return $fixer->get_json();
59 }
60
61 public function route_fixit( WP_REST_Request $request ) {
62 include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
63
64 $params = $request->get_params();
65 $fixer = new Red_Fixer();
66
67 if ( isset( $params['name'] ) && isset( $params['value'] ) ) {
68 global $wpdb;
69
70 $fixer->save_debug( $params['name'], $params['value'] );
71
72 $groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 );
73 if ( $groups === 0 ) {
74 Red_Group::create( 'new group', 1 );
75 }
76 } else {
77 $fixer->fix( $fixer->get_status() );
78 }
79
80 return $fixer->get_json();
81 }
82
83 public function route_delete() {
84 if ( is_multisite() ) {
85 return new WP_Error( 'redirect_delete_multi', 'Multisite installations must delete the plugin from the network admin' );
86 }
87
88 $plugin = Redirection_Admin::init();
89 $plugin->plugin_uninstall();
90
91 $current = get_option( 'active_plugins' );
92 $plugin_position = array_search( basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), $current );
93 if ( $plugin_position !== false ) {
94 array_splice( $current, $plugin_position, 1 );
95 update_option( 'active_plugins', $current );
96 }
97
98 return array( 'location' => admin_url() . 'plugins.php' );
99 }
100
101 public function route_test( WP_REST_Request $request ) {
102 return array(
103 'success' => true,
104 );
105 }
106
107 public function route_database( WP_REST_Request $request ) {
108 $params = $request->get_params();
109 $status = new Red_Database_Status();
110 $upgrade = false;
111
112 if ( isset( $params['upgrade'] ) && in_array( $params['upgrade'], [ 'stop', 'skip' ], true ) ) {
113 $upgrade = $params['upgrade'];
114 }
115
116 // Check upgrade
117 if ( ! $status->needs_updating() && ! $status->needs_installing() ) {
118 /* translators: version number */
119 $status->set_error( sprintf( __( 'Your database does not need updating to %s.', 'redirection' ), REDIRECTION_DB_VERSION ) );
120
121 return $status->get_json();
122 }
123
124 if ( $upgrade === 'stop' ) {
125 $status->stop_update();
126 } elseif ( $upgrade === 'skip' ) {
127 $status->set_next_stage();
128 }
129
130 if ( $upgrade === false || $status->get_current_stage() ) {
131 $database = new Red_Database();
132 $database->apply_upgrade( $status );
133 }
134
135 return $status->get_json();
136 }
137 }
138