| 1 |
<?php |
| 2 |
/** |
| 3 |
* Attribute controller. |
| 4 |
* |
| 5 |
* @package HivePress\Controllers |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace HivePress\Controllers; |
| 9 |
|
| 10 |
use HivePress\Helpers as hp; |
| 11 |
use HivePress\Models; |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Manages model attributes. |
| 18 |
*/ |
| 19 |
final class Attribute extends Controller { |
| 20 |
|
| 21 |
/** |
| 22 |
* Class constructor. |
| 23 |
* |
| 24 |
* @param array $args Controller arguments. |
| 25 |
*/ |
| 26 |
public function __construct( $args = [] ) { |
| 27 |
$args = hp\merge_arrays( |
| 28 |
[ |
| 29 |
'routes' => [ |
| 30 |
'models_resource' => [ |
| 31 |
'path' => '/models', |
| 32 |
'rest' => true, |
| 33 |
], |
| 34 |
|
| 35 |
'model_resource' => [ |
| 36 |
'base' => 'models_resource', |
| 37 |
'path' => '/(?P<model_name>[a-z0-9_]+)', |
| 38 |
'rest' => true, |
| 39 |
], |
| 40 |
|
| 41 |
'attributes_resource' => [ |
| 42 |
'base' => 'model_resource', |
| 43 |
'path' => '/attributes', |
| 44 |
'rest' => true, |
| 45 |
], |
| 46 |
|
| 47 |
'attribute_resource' => [ |
| 48 |
'base' => 'attributes_resource', |
| 49 |
'path' => '/(?P<attribute_name>[a-z0-9_]+)', |
| 50 |
'rest' => true, |
| 51 |
], |
| 52 |
|
| 53 |
'attribute_options_resource' => [ |
| 54 |
'base' => 'attribute_resource', |
| 55 |
'path' => '/options', |
| 56 |
'method' => 'GET', |
| 57 |
'action' => [ $this, 'get_attribute_options' ], |
| 58 |
'rest' => true, |
| 59 |
], |
| 60 |
|
| 61 |
'forms_resource' => [ |
| 62 |
'path' => '/forms', |
| 63 |
'rest' => true, |
| 64 |
], |
| 65 |
|
| 66 |
'form_resource' => [ |
| 67 |
'base' => 'forms_resource', |
| 68 |
'path' => '/(?P<form_name>[a-z0-9_]+)', |
| 69 |
'method' => 'POST', |
| 70 |
'action' => [ $this, 'get_form' ], |
| 71 |
'rest' => true, |
| 72 |
], |
| 73 |
|
| 74 |
'meta_boxes_resource' => [ |
| 75 |
'path' => '/meta-boxes', |
| 76 |
'rest' => true, |
| 77 |
], |
| 78 |
|
| 79 |
'meta_box_resource' => [ |
| 80 |
'base' => 'meta_boxes_resource', |
| 81 |
'path' => '/(?P<meta_box_name>[a-z0-9_]+)', |
| 82 |
'method' => 'POST', |
| 83 |
'action' => [ $this, 'get_meta_box' ], |
| 84 |
'rest' => true, |
| 85 |
], |
| 86 |
], |
| 87 |
], |
| 88 |
$args |
| 89 |
); |
| 90 |
|
| 91 |
parent::__construct( $args ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Gets attribute options. |
| 96 |
* |
| 97 |
* @param WP_REST_Request $request API request. |
| 98 |
* @return WP_Rest_Response |
| 99 |
*/ |
| 100 |
public function get_attribute_options( $request ) { |
| 101 |
|
| 102 |
// Get search query. |
| 103 |
$query = sanitize_text_field( $request->get_param( 'search' ) ); |
| 104 |
|
| 105 |
if ( strlen( $query ) < 3 ) { |
| 106 |
return hp\rest_error( 400 ); |
| 107 |
} |
| 108 |
|
| 109 |
// Get model name. |
| 110 |
$model = sanitize_key( $request->get_param( 'model_name' ) ); |
| 111 |
|
| 112 |
if ( ! in_array( $model, hivepress()->attribute->get_models() ) ) { |
| 113 |
return hp\rest_error( 400 ); |
| 114 |
} |
| 115 |
|
| 116 |
// Get attribute name. |
| 117 |
$attribute_name = sanitize_key( $request->get_param( 'attribute_name' ) ); |
| 118 |
|
| 119 |
if ( ! $attribute_name ) { |
| 120 |
return hp\rest_error( 400 ); |
| 121 |
} |
| 122 |
|
| 123 |
// Get attribute. |
| 124 |
$attribute = hp\get_array_value( hivepress()->attribute->get_attributes( $model ), $attribute_name ); |
| 125 |
|
| 126 |
if ( ! $attribute || ! isset( $attribute['edit_field']['source'] ) ) { |
| 127 |
return hp\rest_error( 404 ); |
| 128 |
} |
| 129 |
|
| 130 |
// Get taxonomy. |
| 131 |
$taxonomy = hp\get_array_value( hp\get_array_value( $attribute['edit_field'], 'option_args' ), 'taxonomy' ); |
| 132 |
|
| 133 |
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) { |
| 134 |
return hp\rest_error( 404 ); |
| 135 |
} |
| 136 |
|
| 137 |
// Get terms. |
| 138 |
$terms = get_terms( |
| 139 |
[ |
| 140 |
'taxonomy' => $taxonomy, |
| 141 |
'search' => $query, |
| 142 |
'number' => 20, |
| 143 |
'fields' => 'id=>name', |
| 144 |
'hide_empty' => false, |
| 145 |
] |
| 146 |
); |
| 147 |
|
| 148 |
// Get results. |
| 149 |
$results = []; |
| 150 |
|
| 151 |
if ( $request->get_param( 'context' ) === 'list' ) { |
| 152 |
foreach ( $terms as $id => $name ) { |
| 153 |
$results[] = [ |
| 154 |
'id' => $id, |
| 155 |
'text' => html_entity_decode( $name ), |
| 156 |
]; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return hp\rest_response( 200, $results ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Gets form. |
| 165 |
* |
| 166 |
* @param WP_REST_Request $request API request. |
| 167 |
* @return WP_Rest_Response |
| 168 |
*/ |
| 169 |
public function get_form( $request ) { |
| 170 |
|
| 171 |
// Check authentication. |
| 172 |
if ( ! is_user_logged_in() ) { |
| 173 |
return hp\rest_error( 401 ); |
| 174 |
} |
| 175 |
|
| 176 |
// Check form model. |
| 177 |
$form_name = sanitize_key( $request->get_param( 'form_name' ) ); |
| 178 |
|
| 179 |
$model_name = sanitize_key( $request->get_param( '_model' ) ); |
| 180 |
$model_id = absint( $request->get_param( '_id' ) ); |
| 181 |
|
| 182 |
if ( 'vendor_submit' === $form_name && 'user' === $model_name ) { |
| 183 |
$model_name = 'vendor'; |
| 184 |
$model_id = Models\Vendor::query()->filter( |
| 185 |
[ |
| 186 |
'status' => [ 'auto-draft', 'draft', 'publish' ], |
| 187 |
'user' => $model_id, |
| 188 |
] |
| 189 |
)->get_first_id(); |
| 190 |
} |
| 191 |
|
| 192 |
if ( ! in_array( $model_name, hivepress()->attribute->get_models() ) || $form_name !== $model_name . '_submit' ) { |
| 193 |
return hp\rest_error( 400 ); |
| 194 |
} |
| 195 |
|
| 196 |
// Get model. |
| 197 |
$model = hivepress()->model->get_model_object( $model_name, $model_id ); |
| 198 |
|
| 199 |
if ( ! $model ) { |
| 200 |
return hp\rest_error( 404 ); |
| 201 |
} |
| 202 |
|
| 203 |
// Check permissions. |
| 204 |
if ( ! current_user_can( 'edit_others_posts' ) && ( get_current_user_id() !== $model->get_user__id() || ! in_array( $model->get_status(), [ 'auto-draft', 'draft', 'publish' ] ) ) ) { |
| 205 |
return hp\rest_error( 403 ); |
| 206 |
} |
| 207 |
|
| 208 |
// Update categories. |
| 209 |
$model->set_categories( $request->get_param( 'categories' ) )->save_categories(); |
| 210 |
|
| 211 |
// Refresh model. |
| 212 |
$model = hivepress()->model->get_model_object( $model_name, $model_id ); |
| 213 |
|
| 214 |
// Create form. |
| 215 |
$form = null; |
| 216 |
|
| 217 |
if ( 'vendor_submit' === $form_name ) { |
| 218 |
$form = hp\create_class_instance( '\HivePress\Forms\User_Update_Profile', [ [ 'model' => $model->get_user() ] ] ); |
| 219 |
} else { |
| 220 |
$form = hp\create_class_instance( '\HivePress\Forms\\' . $form_name, [ [ 'model' => $model ] ] ); |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! $form || ! in_array( $form::get_meta( 'model' ), [ $model_name, 'user' ] ) ) { |
| 224 |
return hp\rest_error( 404 ); |
| 225 |
} |
| 226 |
|
| 227 |
// Render form. |
| 228 |
$output = $form->set_values( $request->get_params(), true )->render(); |
| 229 |
|
| 230 |
return hp\rest_response( |
| 231 |
200, |
| 232 |
[ |
| 233 |
'html' => $output, |
| 234 |
] |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Gets meta box. |
| 240 |
* |
| 241 |
* @param WP_REST_Request $request API request. |
| 242 |
* @return WP_Rest_Response |
| 243 |
*/ |
| 244 |
public function get_meta_box( $request ) { |
| 245 |
|
| 246 |
// Check authentication. |
| 247 |
if ( ! is_user_logged_in() ) { |
| 248 |
return hp\rest_error( 401 ); |
| 249 |
} |
| 250 |
|
| 251 |
// Check meta box model. |
| 252 |
$meta_box = sanitize_key( $request->get_param( 'meta_box_name' ) ); |
| 253 |
$model_name = sanitize_key( $request->get_param( '_model' ) ); |
| 254 |
|
| 255 |
$model_names = array_merge( |
| 256 |
array_map( |
| 257 |
function( $name ) { |
| 258 |
return $name . '_attribute'; |
| 259 |
}, |
| 260 |
hivepress()->attribute->get_models() |
| 261 |
), |
| 262 |
hivepress()->attribute->get_models( 'post' ) |
| 263 |
); |
| 264 |
|
| 265 |
if ( ! in_array( $model_name, $model_names ) || ! in_array( $meta_box, [ $model_name . '_attributes', $model_name . '_edit', $model_name . '_search' ] ) ) { |
| 266 |
return hp\rest_error( 400 ); |
| 267 |
} |
| 268 |
|
| 269 |
// Get post. |
| 270 |
global $post; |
| 271 |
|
| 272 |
$post = get_post( absint( $request->get_param( '_id' ) ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 273 |
|
| 274 |
if ( ! $post || hp\prefix( $model_name ) !== $post->post_type ) { |
| 275 |
return hp\rest_error( 404 ); |
| 276 |
} |
| 277 |
|
| 278 |
// Check permissions. |
| 279 |
if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
| 280 |
return hp\rest_error( 403 ); |
| 281 |
} |
| 282 |
|
| 283 |
if ( $model_name . '_attributes' === $meta_box ) { |
| 284 |
|
| 285 |
// Update category. |
| 286 |
$taxonomy = hp\prefix( $model_name . '_category' ); |
| 287 |
|
| 288 |
if ( taxonomy_exists( $taxonomy ) ) { |
| 289 |
wp_set_post_terms( $post->ID, array_map( 'absint', (array) $request->get_param( 'hp_categories' ) ), $taxonomy ); |
| 290 |
} |
| 291 |
} else { |
| 292 |
|
| 293 |
// Update field types. |
| 294 |
foreach ( [ 'edit', 'search' ] as $field_context ) { |
| 295 |
$field_name = hp\prefix( $field_context . '_field_type' ); |
| 296 |
|
| 297 |
update_post_meta( $post->ID, $field_name, sanitize_key( $request->get_param( $field_name ) ) ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// Render meta box. |
| 302 |
$output = hivepress()->admin->render_meta_box( |
| 303 |
$post, |
| 304 |
[ |
| 305 |
'id' => hp\prefix( $meta_box ), |
| 306 |
'defaults' => $request->get_params(), |
| 307 |
'echo' => false, |
| 308 |
] |
| 309 |
); |
| 310 |
|
| 311 |
return hp\rest_response( |
| 312 |
200, |
| 313 |
[ |
| 314 |
'html' => $output, |
| 315 |
] |
| 316 |
); |
| 317 |
} |
| 318 |
} |
| 319 |
|