Skip to content

Commit 3d86282

Browse files
committed
Refactor update/delete methods a bit to abstract duplicate functionality
1 parent 7fc1de8 commit 3d86282

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

includes/CMB2_REST_Controller_Fields.php

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,42 +120,17 @@ public function get_item( $request ) {
120120
public function update_field_value( $request ) {
121121
$this->initiate_rest_read_box( $request, 'field_value_update' );
122122

123-
if ( ! $this->request['object_id'] && ! $this->request['object_type'] ) {
124-
return $this->prepare_item( array( 'error' => __( 'CMB2 Field value cannot be updated without the object_id and object_type parameters specified.', 'cmb2' ) ) );
125-
}
126-
127123
if ( ! $this->request['value'] ) {
128124
return $this->prepare_item( array( 'error' => __( 'CMB2 Field value cannot be updated without the value parameter specified.', 'cmb2' ) ) );
129125
}
130126

131-
if ( is_wp_error( $this->rest_box ) ) {
132-
return $this->prepare_item( array( 'error' => $this->rest_box->get_error_message() ) );
133-
}
134-
135127
$field = $this->rest_box->field_can_write( $this->request->get_param( 'field_id' ), true );
136128

137-
if ( ! $field ) {
138-
return new WP_Error( 'cmb2_rest_error', __( 'No field found by that id.', 'cmb2' ) );
139-
}
140-
141-
$field->args['value_updated'] = (bool) $field->save_field( $this->request['value'] );
142-
143-
// If options page, save the updated options
144-
if ( 'options-page' == $this->request['object_type'] ) {
145-
$field->args['value_updated'] = cmb2_options( $this->request['object_id'] )->set();
146-
}
147-
148-
$field_data = $this->get_rest_field( $field );
149-
150-
if ( is_wp_error( $field_data ) ) {
151-
return $this->prepare_item( array( 'error' => $field_data->get_error_message() ) );
152-
}
153-
154-
return $this->prepare_item( $field_data );
129+
return $this->modify_field_value( 'updated', $field );
155130
}
156131

157132
/**
158-
* Update CMB2 field value.
133+
* Delete CMB2 field value.
159134
*
160135
* @since 2.2.4
161136
*
@@ -165,25 +140,41 @@ public function update_field_value( $request ) {
165140
public function delete_field_value( $request ) {
166141
$this->initiate_rest_read_box( $request, 'field_value_delete' );
167142

143+
$field = $this->rest_box->field_can_write( $this->request->get_param( 'field_id' ), true );
144+
145+
return $this->modify_field_value( 'deleted', $field );
146+
}
147+
148+
/**
149+
* Modify CMB2 field value.
150+
*
151+
* @since 2.2.4
152+
*
153+
* @param string $activity The modification activity (updated or deleted).
154+
* @param CMB2_Field $field The field object.
155+
* @return WP_Error|WP_REST_Response
156+
*/
157+
public function modify_field_value( $activity, $field ) {
158+
168159
if ( ! $this->request['object_id'] && ! $this->request['object_type'] ) {
169-
return $this->prepare_item( array( 'error' => __( 'CMB2 Field value cannot be deleted without the object_id and object_type parameters specified.', 'cmb2' ) ) );
160+
return $this->prepare_item( array( 'error' => __( 'CMB2 Field value cannot be modified without the object_id and object_type parameters specified.', 'cmb2' ) ) );
170161
}
171162

172163
if ( is_wp_error( $this->rest_box ) ) {
173164
return $this->prepare_item( array( 'error' => $this->rest_box->get_error_message() ) );
174165
}
175166

176-
$field = $this->rest_box->field_can_write( $this->request->get_param( 'field_id' ), true );
177-
178167
if ( ! $field ) {
179168
return new WP_Error( 'cmb2_rest_error', __( 'No field found by that id.', 'cmb2' ) );
180169
}
181170

182-
$field->args['value_deleted'] = (bool) $field->remove_data();
171+
$field->args["value_{$activity}"] = (bool) 'deleted' === $activity
172+
? $field->remove_data()
173+
: $field->save_field( $this->request['value'] );
183174

184-
// If options page, save the updated options
175+
// If options page, save the $activity options
185176
if ( 'options-page' == $this->request['object_type'] ) {
186-
$field->args['value_deleted'] = cmb2_options( $this->request['object_id'] )->set();
177+
$field->args["value_{$activity}"] = cmb2_options( $this->request['object_id'] )->set();
187178
}
188179

189180
$field_data = $this->get_rest_field( $field );

0 commit comments

Comments
 (0)