| 1 |
<?php |
| 2 |
|
| 3 |
class Redirection_Api_Settings extends Redirection_Api_Route { |
| 4 |
public function __construct( $namespace ) { |
| 5 |
register_rest_route( $namespace, '/setting', array( |
| 6 |
$this->get_route( WP_REST_Server::READABLE, 'route_settings' ), |
| 7 |
$this->get_route( WP_REST_Server::EDITABLE, 'route_save_settings' ), |
| 8 |
) ); |
| 9 |
} |
| 10 |
|
| 11 |
public function route_settings( WP_REST_Request $request ) { |
| 12 |
if ( ! function_exists( 'get_home_path' ) ) { |
| 13 |
include_once ABSPATH . '/wp-admin/includes/file.php'; |
| 14 |
} |
| 15 |
|
| 16 |
return [ |
| 17 |
'settings' => red_get_options(), |
| 18 |
'groups' => $this->groups_to_json( Red_Group::get_for_select() ), |
| 19 |
'installed' => get_home_path(), |
| 20 |
'canDelete' => ! is_multisite(), |
| 21 |
'post_types' => red_get_post_types(), |
| 22 |
]; |
| 23 |
} |
| 24 |
|
| 25 |
public function route_save_settings( WP_REST_Request $request ) { |
| 26 |
$params = $request->get_params(); |
| 27 |
$result = true; |
| 28 |
|
| 29 |
if ( isset( $params['location'] ) && strlen( $params['location'] ) > 0 ) { |
| 30 |
$module = Red_Module::get( 2 ); |
| 31 |
$result = $module->can_save( $params['location'] ); |
| 32 |
} |
| 33 |
|
| 34 |
red_set_options( $params ); |
| 35 |
|
| 36 |
$settings = $this->route_settings( $request ); |
| 37 |
if ( is_wp_error( $result ) ) { |
| 38 |
$settings['warning'] = $result->get_error_message(); |
| 39 |
} |
| 40 |
|
| 41 |
return $settings; |
| 42 |
} |
| 43 |
|
| 44 |
private function groups_to_json( $groups, $depth = 0 ) { |
| 45 |
$items = array(); |
| 46 |
|
| 47 |
foreach ( $groups as $text => $value ) { |
| 48 |
if ( is_array( $value ) && $depth === 0 ) { |
| 49 |
$items[] = (object) array( |
| 50 |
'text' => $text, |
| 51 |
'value' => $this->groups_to_json( $value, 1 ), |
| 52 |
); |
| 53 |
} else { |
| 54 |
$items[] = (object) array( |
| 55 |
'text' => $value, |
| 56 |
'value' => $text, |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return $items; |
| 62 |
} |
| 63 |
} |
| 64 |
|