PluginProbe
Redirection / 4.5.1
Redirection v4.5.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 4.5.1, at api/api-plugin.php

166 lines 4.4 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' ),
10 ) );
11
12 register_rest_route( $namespace, '/plugin', array(
13 $this->get_route( WP_REST_Server::EDITABLE, 'route_fixit' ),
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' ),
28 ) );
29
30 register_rest_route( $namespace, '/plugin/test', array(
31 $this->get_route( WP_REST_Server::ALLMETHODS, 'route_test' ),
32 ) );
33
34 register_rest_route( $namespace, '/plugin/post', array(
35 $this->get_route( WP_REST_Server::READABLE, 'route_match_post' ),
36 'args' => [
37 'text' => [
38 'description' => 'Text to match',
39 'type' => 'string',
40 ],
41 ],
42 ) );
43
44 register_rest_route( $namespace, '/plugin/database', array(
45 $this->get_route( WP_REST_Server::EDITABLE, 'route_database' ),
46 'args' => array(
47 'description' => 'Upgrade parameter',
48 'type' => 'enum',
49 'enum' => array(
50 'stop',
51 'skip',
52 ),
53 ),
54 ) );
55 }
56
57 public function route_match_post( WP_REST_Request $request ) {
58 $params = $request->get_params();
59 $search = isset( $params['text'] ) ? $params['text'] : false;
60 $results = [];
61
62 if ( $search ) {
63 global $wpdb;
64
65 $posts = $wpdb->get_results(
66 $wpdb->prepare(
67 "SELECT ID,post_title,post_name FROM $wpdb->posts WHERE post_status='publish' AND (post_title LIKE %s OR post_name LIKE %s) " .
68 "AND post_type NOT IN ('nav_menu_item','wp_block','oembed_cache')",
69 '%' . $wpdb->esc_like( $search ) . '%', '%' . $wpdb->esc_like( $search ) . '%'
70 )
71 );
72
73 foreach ( (array) $posts as $post ) {
74 $results[] = [
75 'title' => $post->post_title,
76 'slug' => $post->post_name,
77 'url' => get_permalink( $post->ID ),
78 ];
79 }
80 }
81
82 return $results;
83 }
84
85 public function route_status( WP_REST_Request $request ) {
86 include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
87
88 $fixer = new Red_Fixer();
89 return $fixer->get_json();
90 }
91
92 public function route_fixit( WP_REST_Request $request ) {
93 include_once dirname( REDIRECTION_FILE ) . '/models/fixer.php';
94
95 $params = $request->get_params();
96 $fixer = new Red_Fixer();
97
98 if ( isset( $params['name'] ) && isset( $params['value'] ) ) {
99 global $wpdb;
100
101 $fixer->save_debug( $params['name'], $params['value'] );
102
103 $groups = intval( $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" ), 10 );
104 if ( $groups === 0 ) {
105 Red_Group::create( 'new group', 1 );
106 }
107 } else {
108 $fixer->fix( $fixer->get_status() );
109 }
110
111 return $fixer->get_json();
112 }
113
114 public function route_delete() {
115 if ( is_multisite() ) {
116 return $this->getError( 'Multisite installations must delete the plugin from the network admin', __LINE__ );
117 }
118
119 $plugin = Redirection_Admin::init();
120 $plugin->plugin_uninstall();
121
122 $current = get_option( 'active_plugins' );
123 array_splice( $current, array_search( basename( dirname( REDIRECTION_FILE ) ) . '/' . basename( REDIRECTION_FILE ), $current ), 1 );
124 update_option( 'active_plugins', $current );
125
126 return array( 'location' => admin_url() . 'plugins.php' );
127 }
128
129 public function route_test( WP_REST_Request $request ) {
130 return array(
131 'success' => true,
132 );
133 }
134
135 public function route_database( WP_REST_Request $request ) {
136 $params = $request->get_params();
137 $status = new Red_Database_Status();
138 $upgrade = false;
139
140 if ( isset( $params['upgrade'] ) && in_array( $params['upgrade'], [ 'stop', 'skip' ], true ) ) {
141 $upgrade = $params['upgrade'];
142 }
143
144 // Check upgrade
145 if ( ! $status->needs_updating() && ! $status->needs_installing() ) {
146 /* translators: version number */
147 $status->set_error( sprintf( __( 'Your database does not need updating to %s.', 'redirection' ), REDIRECTION_DB_VERSION ) );
148
149 return $status->get_json();
150 }
151
152 if ( $upgrade === 'stop' ) {
153 $status->stop_update();
154 } elseif ( $upgrade === 'skip' ) {
155 $status->set_next_stage();
156 }
157
158 if ( $upgrade === false || $status->get_current_stage() ) {
159 $database = new Red_Database();
160 $database->apply_upgrade( $status );
161 }
162
163 return $status->get_json();
164 }
165 }
166