| 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 |
$allowed_fields = [ |
| 100 |
'authors' => [ 'ID', 'user_nicename', 'display_name' ], |
| 101 |
'posts' => [ 'ID', 'post_title', 'post_name' ], |
| 102 |
'taxonomy' => [ 'term_id', 'slug', 'name' ], |
| 103 |
]; |
| 104 |
|
| 105 |
if ( empty( $type ) || ! isset( $allowed_fields[ $type ] ) ) { |
| 106 |
return $this->success( [] ); |
| 107 |
} |
| 108 |
|
| 109 |
$by_field = $query['field'] ?? ''; |
| 110 |
|
| 111 |
if ( empty( $by_field ) || ! in_array( $by_field, $allowed_fields[ $type ], true ) ) { |
| 112 |
return $this->success( [] ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( 'authors' === $type && ! current_user_can( 'list_users' ) ) { |
| 116 |
return $this->success( [] ); |
| 117 |
} |
| 118 |
|
| 119 |
$payload = sanitize_text_field( $request->get_param( 'payload' ) ); |
| 120 |
$args = [ 'search' => $payload ]; |
| 121 |
if ( is_numeric( $payload ) ) { |
| 122 |
$args = [ 'post__in' => [ (int) $payload ] ]; |
| 123 |
} |
| 124 |
|
| 125 |
if ( isset( $query['query'] ) && is_array( $query['query'] ) ) { |
| 126 |
$safe_query_keys = [ |
| 127 |
'post_type', 'posts_per_page', 'number', 'orderby', 'order', |
| 128 |
'taxonomy', 'parent', 'hide_empty', |
| 129 |
]; |
| 130 |
$safe_query = array_intersect_key( $query['query'], array_flip( $safe_query_keys ) ); |
| 131 |
$args = wp_parse_args( $safe_query, $args ); |
| 132 |
} |
| 133 |
|
| 134 |
$results = []; |
| 135 |
$data = []; |
| 136 |
$data_key = ''; |
| 137 |
|
| 138 |
switch ( $type ) { |
| 139 |
case 'taxonomy': |
| 140 |
$_default = [ 'hide_empty' => false ]; |
| 141 |
$data = get_terms( wp_parse_args( $args, $_default ) ); |
| 142 |
$data_key = 'name'; |
| 143 |
break; |
| 144 |
case 'posts': |
| 145 |
$args['s'] = $args['search']; |
| 146 |
$args['post_status'] = 'publish'; |
| 147 |
$args['perm'] = 'readable'; |
| 148 |
$data = get_posts( $args ); |
| 149 |
$data_key = 'post_title'; |
| 150 |
break; |
| 151 |
case 'authors': |
| 152 |
$args['search_columns'] = [ 'user_nicename', 'user_login' ]; |
| 153 |
$args['search'] = "*{$args['search']}*"; |
| 154 |
$data = get_users( $args ); |
| 155 |
|
| 156 |
$data_key = 'display_name'; |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
if ( ! empty( $data ) && is_array( $data ) ) { |
| 161 |
foreach ( $data as $item ) { |
| 162 |
$results[] = [ |
| 163 |
'label' => $item->{$data_key}, |
| 164 |
'value' => $item->{$by_field} |
| 165 |
]; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $this->success( $results ); |
| 170 |
} |
| 171 |
} |