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-settings.php

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

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