| 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.redirect_cache |
| 46 |
* @apiSuccess {String} settings.ip_logging |
| 47 |
* @apiSuccess {String} settings.last_group_id |
| 48 |
* @apiSuccess {String} settings.rest_api |
| 49 |
* @apiSuccess {String} settings.https |
| 50 |
* @apiSuccess {String} settings.headers |
| 51 |
* @apiSuccess {String} settings.database |
| 52 |
* @apiSuccess {String} settings.relocate Relocate this site to the specified domain (and path) |
| 53 |
* @apiSuccess {String="www","nowww",""} settings.preferred_domain Preferred canonical domain |
| 54 |
* @apiSuccess {String[]} settings.aliases Array of domains that will be redirected to the current WordPress site |
| 55 |
* @apiSuccess {Object[]} groups An array of groups |
| 56 |
* @apiSuccess {String} groups.label Name of the group |
| 57 |
* @apiSuccess {Integer} groups.value Group ID |
| 58 |
* @apiSuccess {String} installed The path that WordPress is installed in |
| 59 |
* @apiSuccess {Boolean} canDelete True if Redirection can be deleted, false otherwise (on multisite, for example) |
| 60 |
* @apiSuccess {String[]} post_types Array of WordPress post types |
| 61 |
* |
| 62 |
* @apiSuccessExample {json} Success-Response: |
| 63 |
* HTTP/1.1 200 OK |
| 64 |
* { |
| 65 |
* "settings": { |
| 66 |
* "expire_redirect": 7, |
| 67 |
* "https": true |
| 68 |
* }, |
| 69 |
* "groups": [ |
| 70 |
* { label: 'My group', value: 5 } |
| 71 |
* ], |
| 72 |
* "installed": "/var/html/wordpress", |
| 73 |
* "canDelete": true, |
| 74 |
* "post_types": [ |
| 75 |
* "post", |
| 76 |
* "page" |
| 77 |
* ] |
| 78 |
* } |
| 79 |
*/ |
| 80 |
|
| 81 |
class Redirection_Api_Settings extends Redirection_Api_Route { |
| 82 |
public function __construct( $namespace ) { |
| 83 |
register_rest_route( $namespace, '/setting', array( |
| 84 |
$this->get_route( WP_REST_Server::READABLE, 'route_settings', [ $this, 'permission_callback_manage' ] ), |
| 85 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_save_settings', [ $this, 'permission_callback_manage' ] ), |
| 86 |
) ); |
| 87 |
} |
| 88 |
|
| 89 |
public function route_settings( WP_REST_Request $request ) { |
| 90 |
if ( ! function_exists( 'get_home_path' ) ) { |
| 91 |
include_once ABSPATH . '/wp-admin/includes/file.php'; |
| 92 |
} |
| 93 |
|
| 94 |
return [ |
| 95 |
'settings' => red_get_options(), |
| 96 |
'groups' => $this->groups_to_json( Red_Group::get_for_select() ), |
| 97 |
'installed' => get_home_path(), |
| 98 |
'canDelete' => ! is_multisite(), |
| 99 |
'post_types' => red_get_post_types(), |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function permission_callback_manage( WP_REST_Request $request ) { |
| 104 |
return Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_OPTION_MANAGE ) || Redirection_Capabilities::has_access( Redirection_Capabilities::CAP_SITE_MANAGE ); |
| 105 |
} |
| 106 |
|
| 107 |
public function route_save_settings( WP_REST_Request $request ) { |
| 108 |
$params = $request->get_params(); |
| 109 |
$result = true; |
| 110 |
|
| 111 |
if ( isset( $params['location'] ) && strlen( $params['location'] ) > 0 ) { |
| 112 |
$module = Red_Module::get( 2 ); |
| 113 |
$result = $module->can_save( sanitize_text_field( $params['location'] ) ); |
| 114 |
} |
| 115 |
|
| 116 |
red_set_options( $params ); |
| 117 |
|
| 118 |
$settings = $this->route_settings( $request ); |
| 119 |
if ( is_wp_error( $result ) ) { |
| 120 |
$settings['warning'] = $result->get_error_message(); |
| 121 |
} |
| 122 |
|
| 123 |
return $settings; |
| 124 |
} |
| 125 |
|
| 126 |
private function groups_to_json( $groups, $depth = 0 ) { |
| 127 |
$items = array(); |
| 128 |
|
| 129 |
foreach ( $groups as $text => $value ) { |
| 130 |
if ( is_array( $value ) && $depth === 0 ) { |
| 131 |
$items[] = (object) array( |
| 132 |
'label' => $text, |
| 133 |
'value' => $this->groups_to_json( $value, 1 ), |
| 134 |
); |
| 135 |
} else { |
| 136 |
$items[] = (object) array( |
| 137 |
'label' => $value, |
| 138 |
'value' => $text, |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return $items; |
| 144 |
} |
| 145 |
} |
| 146 |
|