| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
use Templately\Builder\Managers\ConditionManager; |
| 6 |
use Templately\Builder\Source; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
|
| 10 |
class Conditions extends API { |
| 11 |
/** |
| 12 |
* @var ConditionManager |
| 13 |
*/ |
| 14 |
private $conditions_manager; |
| 15 |
|
| 16 |
public function permission_check( WP_REST_Request $request ) { |
| 17 |
$post_type_object = get_post_type_object( Source::CPT ); |
| 18 |
|
| 19 |
return current_user_can( $post_type_object->cap->edit_posts ); |
| 20 |
} |
| 21 |
|
| 22 |
public function register_routes() { |
| 23 |
$this->get( 'conditions', [ $this, 'get_conditions' ], [ |
| 24 |
'template_id' => [ |
| 25 |
'required' => false, |
| 26 |
'validate_callback' => function ( $param ) { |
| 27 |
return empty( $param ) || is_numeric( $param ); |
| 28 |
} |
| 29 |
] |
| 30 |
] ); |
| 31 |
|
| 32 |
$this->get( 'check-conditions', [ $this, 'check' ], [ |
| 33 |
'template_id' => [ |
| 34 |
'required' => true, |
| 35 |
'validate_callback' => function ( $param ) { |
| 36 |
return is_numeric( $param ); |
| 37 |
} |
| 38 |
] |
| 39 |
] ); |
| 40 |
|
| 41 |
$this->post( 'save-conditions', [ $this, 'save' ], [ |
| 42 |
'template_id' => [ |
| 43 |
'required' => true, |
| 44 |
'validate_callback' => function ( $param ) { |
| 45 |
return is_numeric( $param ); |
| 46 |
} |
| 47 |
], |
| 48 |
'conditions' => [ |
| 49 |
'required' => true, |
| 50 |
'validate_callback' => function ( $param ) { |
| 51 |
return is_array( $param ); |
| 52 |
} |
| 53 |
] |
| 54 |
] ); |
| 55 |
|
| 56 |
$this->get( 'autocomplete-condition', [ $this, 'autocomplete' ], [ |
| 57 |
'payload' => [ |
| 58 |
'required' => true, |
| 59 |
'validate_callback' => function ( $param ) { |
| 60 |
return is_string( $param ); |
| 61 |
} |
| 62 |
], |
| 63 |
'query' => [ |
| 64 |
'required' => true, |
| 65 |
'validate_callback' => function ( $param ) { |
| 66 |
return is_array( $param ) && array_key_exists( 'query_type', $param ); |
| 67 |
} |
| 68 |
] |
| 69 |
] ); |
| 70 |
|
| 71 |
$this->conditions_manager = templately()->theme_builder::$conditions_manager; |
| 72 |
} |
| 73 |
|
| 74 |
public function get_conditions( WP_REST_Request $request ): WP_REST_Response { |
| 75 |
$conditions = $this->conditions_manager->get_conditions_for_display( $request->get_param( 'template_id' ) ); |
| 76 |
|
| 77 |
return $this->success( $conditions ); |
| 78 |
} |
| 79 |
|
| 80 |
public function check( WP_REST_Request $request ): WP_REST_Response { |
| 81 |
return $this->success( [] ); |
| 82 |
} |
| 83 |
|
| 84 |
public function save( WP_REST_Request $request ): WP_REST_Response { |
| 85 |
$conditions = rest_sanitize_array( $request->get_param( 'conditions' ) ); |
| 86 |
$id = (int) $request->get_param( 'template_id' ); |
| 87 |
|
| 88 |
|
| 89 |
$this->conditions_manager->save_conditions( $id, $conditions ); |
| 90 |
|
| 91 |
|
| 92 |
return $this->success( __( 'Successfully saved.', 'templately' ) ); |
| 93 |
} |
| 94 |
|
| 95 |
public function autocomplete( WP_REST_Request $request ): WP_REST_Response { |
| 96 |
$query = $request->get_param( 'query' ); |
| 97 |
$type = $query['query_type'] ?? ''; |
| 98 |
|
| 99 |
if ( empty( $type ) ) { |
| 100 |
// FIXME: need throw error maybe |
| 101 |
return $this->success( [] ); |
| 102 |
} |
| 103 |
|
| 104 |
$by_field = $query['field'] ?? ''; |
| 105 |
|
| 106 |
if ( empty( $by_field ) ) { |
| 107 |
// FIXME: need throw error maybe |
| 108 |
return $this->success( [] ); |
| 109 |
} |
| 110 |
|
| 111 |
$payload = sanitize_text_field( $request->get_param( 'payload' ) ); |
| 112 |
$args = [ 'search' => $payload ]; |
| 113 |
|
| 114 |
if ( isset( $query['query'] ) ) { |
| 115 |
$args = wp_parse_args( $query['query'], $args ); |
| 116 |
} |
| 117 |
|
| 118 |
$results = []; |
| 119 |
|
| 120 |
switch ( $type ) { |
| 121 |
case 'taxonomy': |
| 122 |
$_default = [ 'hide_empty' => false ]; |
| 123 |
$data = get_terms( wp_parse_args( $args, $_default ) ); |
| 124 |
$data_key = 'name'; |
| 125 |
break; |
| 126 |
case 'posts': |
| 127 |
$args['s'] = $args['search']; |
| 128 |
$data = get_posts( $args ); |
| 129 |
$data_key = 'post_title'; |
| 130 |
break; |
| 131 |
case 'authors': |
| 132 |
$args['search_columns'] = [ 'user_nicename', 'user_login' ]; |
| 133 |
$args['search'] = "*{$args['search']}*"; |
| 134 |
$data = get_users( $args ); |
| 135 |
|
| 136 |
$data_key = 'display_name'; |
| 137 |
break; |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! empty( $data ) && is_array( $data ) ) { |
| 141 |
foreach ( $data as $item ) { |
| 142 |
$results[] = [ |
| 143 |
'label' => $item->{$data_key}, |
| 144 |
'value' => $item->{$by_field} |
| 145 |
]; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return $this->success( $results ); |
| 150 |
} |
| 151 |
} |