PluginProbe
Redirection / 5.3.8
Redirection v5.3.8
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-settings.php

api-settings.php in Redirection 5.3.8, at api/api-settings.php

147 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @api {get} /redirection/v1/setting Get settings
4 * @apiName GetSettings
5 * @apiDescription Get all settings for Redirection. This includes user-configurable settings, as well as necessary WordPress settings.
6 * @apiGroup Settings
7 *
8 * @apiUse SettingItem
9 * @apiUse 401Error
10 * @apiUse 404Error
11 */
12
13 /**
14 * @api {post} /redirection/v1/setting Update settings
15 * @apiName UpdateSettings
16 * @apiDescription Update Redirection settings. Note you can do partial updates, and only the values specified will be changed.
17 * @apiGroup Settings
18 *
19 * @apiParam {Object} settings An object containing all the settings to update
20 * @apiParamExample {json} settings:
21 * {
22 * "expire_redirect": 14,
23 * "https": false
24 * }
25 *
26 * @apiUse SettingItem
27 * @apiUse 401Error
28 * @apiUse 404Error
29 */
30
31 /**
32 * @apiDefine SettingItem Settings
33 * Redirection settings
34 *
35 * @apiSuccess {Object[]} settings An object containing all settings
36 * @apiSuccess {String} settings.expire_redirect
37 * @apiSuccess {String} settings.token
38 * @apiSuccess {String} settings.monitor_post
39 * @apiSuccess {String[]} settings.monitor_types
40 * @apiSuccess {String} settings.associated_redirect
41 * @apiSuccess {String} settings.auto_target
42 * @apiSuccess {String} settings.expire_redirect
43 * @apiSuccess {String} settings.expire_404
44 * @apiSuccess {String} settings.modules
45 * @apiSuccess {String} settings.newsletter
46 * @apiSuccess {String} settings.redirect_cache
47 * @apiSuccess {String} settings.ip_logging
48 * @apiSuccess {String} settings.last_group_id
49 * @apiSuccess {String} settings.rest_api
50 * @apiSuccess {String} settings.https
51 * @apiSuccess {String} settings.headers
52 * @apiSuccess {String} settings.database
53 * @apiSuccess {String} settings.relocate Relocate this site to the specified domain (and path)
54 * @apiSuccess {String="www","nowww",""} settings.preferred_domain Preferred canonical domain
55 * @apiSuccess {String[]} settings.aliases Array of domains that will be redirected to the current WordPress site
56 * @apiSuccess {Object[]} groups An array of groups
57 * @apiSuccess {String} groups.label Name of the group
58 * @apiSuccess {Integer} groups.value Group ID
59 * @apiSuccess {String} installed The path that WordPress is installed in
60 * @apiSuccess {Boolean} canDelete True if Redirection can be deleted, false otherwise (on multisite, for example)
61 * @apiSuccess {String[]} post_types Array of WordPress post types
62 *
63 * @apiSuccessExample {json} Success-Response:
64 * HTTP/1.1 200 OK
65 * {
66 * "settings": {
67 * "expire_redirect": 7,
68 * "https": true
69 * },
70 * "groups": [
71 * { label: 'My group', value: 5 }
72 * ],
73 * "installed": "/var/html/wordpress",
74 * "canDelete": true,
75 * "post_types": [
76 * "post",
77 * "page"
78 * ]
79 * }
80 */
81
82 class Redirection_Api_Settings extends Redirection_Api_Route {
83 public function __construct( $namespace ) {
84 register_rest_route( $namespace, '/setting', array(
85 $this->get_route( WP_REST_Server::READABLE, 'route_settings', [ $this, 'permission_callback_manage' ] ),
86 $this->get_route( WP_REST_Server::EDITABLE, 'route_save_settings', [ $this, 'permission_callback_manage' ] ),
87 ) );
88 }
89
90 public function route_settings( WP_REST_Request $request ) {
91 if ( ! function_exists( 'get_home_path' ) ) {
92 include_once ABSPATH . '/wp-admin/includes/file.php';
93 }
94
95 return [
96 'settings' => red_get_options(),
97 'groups' => $this->groups_to_json( Red_Group::get_for_select() ),
98 'installed' => get_home_path(),
99 'canDelete' => ! is_multisite(),
100 'post_types' => red_get_post_types(),
101 ];
102 }
103
104 public function permission_callback_manage( WP_REST_Request $request ) {
105 return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_OPTION_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SITE_MANAGE );
106 }
107
108 public function route_save_settings( WP_REST_Request $request ) {
109 $params = $request->get_params();
110 $result = true;
111
112 if ( isset( $params['location'] ) && strlen( $params['location'] ) > 0 ) {
113 $module = Red_Module::get( 2 );
114 $result = $module->can_save( sanitize_text_field( $params['location'] ) );
115 }
116
117 red_set_options( $params );
118
119 $settings = $this->route_settings( $request );
120 if ( is_wp_error( $result ) ) {
121 $settings['warning'] = $result->get_error_message();
122 }
123
124 return $settings;
125 }
126
127 private function groups_to_json( $groups, $depth = 0 ) {
128 $items = array();
129
130 foreach ( $groups as $text => $value ) {
131 if ( is_array( $value ) && $depth === 0 ) {
132 $items[] = (object) array(
133 'label' => $text,
134 'value' => $this->groups_to_json( $value, 1 ),
135 );
136 } else {
137 $items[] = (object) array(
138 'label' => $value,
139 'value' => $text,
140 );
141 }
142 }
143
144 return $items;
145 }
146 }
147