| 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 array( |
| 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 |
red_set_options( $request->get_params() ); |
| 27 |
|
| 28 |
return $this->route_settings( $request ); |
| 29 |
} |
| 30 |
|
| 31 |
private function groups_to_json( $groups, $depth = 0 ) { |
| 32 |
$items = array(); |
| 33 |
|
| 34 |
foreach ( $groups as $text => $value ) { |
| 35 |
if ( is_array( $value ) && $depth === 0 ) { |
| 36 |
$items[] = (object) array( |
| 37 |
'text' => $text, |
| 38 |
'value' => $this->groups_to_json( $value, 1 ), |
| 39 |
); |
| 40 |
} else { |
| 41 |
$items[] = (object) array( |
| 42 |
'text' => $value, |
| 43 |
'value' => $text, |
| 44 |
); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $items; |
| 49 |
} |
| 50 |
} |
| 51 |
|