| 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 |
return array( |
| 13 |
'settings' => red_get_options(), |
| 14 |
'groups' => $this->groups_to_json( Red_Group::get_for_select() ), |
| 15 |
'installed' => get_home_path(), |
| 16 |
'canDelete' => ! is_multisite(), |
| 17 |
'post_types' => red_get_post_types(), |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
public function route_save_settings( WP_REST_Request $request ) { |
| 22 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 23 |
include_once ABSPATH.'/wp-admin/includes/plugin.php'; |
| 24 |
} |
| 25 |
|
| 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( 'text' => $text, 'value' => $this->groups_to_json( $value, 1 ) ); |
| 37 |
} else { |
| 38 |
$items[] = (object)array( 'text' => $value, 'value' => $text ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $items; |
| 43 |
} |
| 44 |
} |
| 45 |
|