jetformbuilder
/
includes
/
blocks
/
conditional-block
/
rest-api
/
add-render-state-endpoint-option.php
add-render-state-endpoint-option.php
3 years ago
delete-render-states-endpoint.php
3 years ago
rest-api-controller.php
3 years ago
add-render-state-endpoint-option.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Blocks\Conditional_Block\Rest_Api; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Conditional_Block\Render_State; |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Rest_Api\Rest_Api_Endpoint_Base; |
| 9 | |
| 10 | class Add_Render_State_Endpoint_Option extends Rest_Api_Endpoint_Base { |
| 11 | |
| 12 | /** |
| 13 | * @return mixed |
| 14 | */ |
| 15 | public static function get_rest_base() { |
| 16 | return 'render-states/add/option'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return mixed |
| 21 | */ |
| 22 | public static function get_methods() { |
| 23 | return \WP_REST_Server::CREATABLE; |
| 24 | } |
| 25 | |
| 26 | public function check_permission(): bool { |
| 27 | return current_user_can( 'manage_options' ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param \WP_REST_Request $request |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public function run_callback( \WP_REST_Request $request ) { |
| 36 | $body = $request->get_json_params(); |
| 37 | $label = $body['value'] ?? ''; |
| 38 | $value = $this->format_name( $label ); |
| 39 | |
| 40 | if ( ! $value ) { |
| 41 | return new \WP_REST_Response( |
| 42 | array( |
| 43 | 'message' => __( 'Empty render state name', 'jet-form-builder' ), |
| 44 | ), |
| 45 | 400 |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | $saved = Tools::decode_json( get_option( Render_State::OPTION_KEY, '[]' ) ); |
| 50 | |
| 51 | $find = array_filter( |
| 52 | $saved, |
| 53 | function ( $item ) use ( $value ) { |
| 54 | return $item['value'] === $value; |
| 55 | } |
| 56 | ); |
| 57 | |
| 58 | if ( count( $find ) ) { |
| 59 | return new \WP_REST_Response( |
| 60 | array( |
| 61 | 'message' => sprintf( |
| 62 | /* translators: %s - name of render state */ |
| 63 | __( 'Render state with name "%s" is already exist', 'jet-form-builder' ), |
| 64 | $value |
| 65 | ), |
| 66 | ), |
| 67 | 400 |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | $new_state = array( |
| 72 | 'label' => $label, |
| 73 | 'value' => $value, |
| 74 | ); |
| 75 | |
| 76 | $saved[] = $new_state; |
| 77 | |
| 78 | $result = Render_State::update_states( $saved ); |
| 79 | |
| 80 | if ( ! $result ) { |
| 81 | return new \WP_REST_Response( |
| 82 | array( |
| 83 | 'message' => __( 'Something went wrong', 'jet-form-builder' ), |
| 84 | ), |
| 85 | 400 |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | $new_state['is_custom'] = true; |
| 90 | |
| 91 | return new \WP_REST_Response( |
| 92 | array( |
| 93 | 'message' => __( 'Successful added render state', 'jet-form-builder' ), |
| 94 | 'state' => $new_state, |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | private function format_name( string $title ): string { |
| 100 | $name = preg_replace( '#[\s\-\_]+#', '.', $title ); |
| 101 | $name = preg_replace( '#[^\w\.]+#', '', $name ); |
| 102 | $name = trim( $name, '.' ); |
| 103 | |
| 104 | return strtoupper( $name ); |
| 105 | } |
| 106 | } |
| 107 |