| 1 |
<?php |
| 2 |
namespace Easyel\EasyElements\Utils; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
class QueryHelper { |
| 6 |
|
| 7 |
/** @var self|null */ |
| 8 |
private static $instance = null; |
| 9 |
|
| 10 |
/** |
| 11 |
* Singleton Instance |
| 12 |
*/ |
| 13 |
public static function get_instance() { |
| 14 |
if ( self::$instance === null ) { |
| 15 |
self::$instance = new self(); |
| 16 |
} |
| 17 |
return self::$instance; |
| 18 |
} |
| 19 |
|
| 20 |
private function __construct() { |
| 21 |
// These query/autocomplete helpers are used inside the editor (admin) |
| 22 |
// context only. They must not be reachable by unauthenticated users, |
| 23 |
// so no nopriv hooks are registered. |
| 24 |
add_action('wp_ajax_easyel_get_posts_by_query', [$this, 'get_posts_by_query']); |
| 25 |
add_action('wp_ajax_easyel_get_posts_title_by_id', [$this, 'get_posts_title_by_id']); |
| 26 |
add_action('wp_ajax_easyel_get_taxonomies_by_query', [$this, 'get_taxonomies_by_query']); |
| 27 |
add_action('wp_ajax_easyel_get_taxonomies_title_by_id', [$this, 'get_taxonomies_title_by_id']); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_posts_by_query() { |
| 31 |
check_ajax_referer('easyel_autocomplete_nonce', 'security'); |
| 32 |
|
| 33 |
$search = isset($_POST['q']) ? sanitize_text_field( wp_unslash($_POST['q']) ) : ''; |
| 34 |
$post_type = isset($_POST['post_type']) ? sanitize_text_field( wp_unslash($_POST['post_type']) ) : 'post'; |
| 35 |
$query_type = isset($_POST['query_type']) ? sanitize_text_field( wp_unslash($_POST['query_type']) ) : ''; |
| 36 |
|
| 37 |
$results = []; |
| 38 |
|
| 39 |
switch($query_type){ |
| 40 |
case 'post_type': |
| 41 |
case 'single_post_type': |
| 42 |
$results = $this->get_public_post_types($search); |
| 43 |
break; |
| 44 |
case 'post_id': |
| 45 |
$results = $this->get_posts_by_id($search); |
| 46 |
break; |
| 47 |
case 'taxonomy': |
| 48 |
$results = $this->get_public_taxonomies($search); |
| 49 |
break; |
| 50 |
case 'term_id': |
| 51 |
case 'single_posts_term_id': |
| 52 |
$results = $this->get_terms_by_search($search); |
| 53 |
break; |
| 54 |
case 'user_id': |
| 55 |
$results = $this->get_users_by_search( $search ); |
| 56 |
break; |
| 57 |
|
| 58 |
case 'user_role': |
| 59 |
$results = $this->get_user_roles_by_search( $search ); |
| 60 |
break; |
| 61 |
default: |
| 62 |
$results = $this->get_posts_by_search($post_type, $search); |
| 63 |
} |
| 64 |
|
| 65 |
wp_send_json($results); |
| 66 |
} |
| 67 |
|
| 68 |
private function get_users_by_search( $search = '' ) { |
| 69 |
|
| 70 |
$results = []; |
| 71 |
|
| 72 |
$args = [ |
| 73 |
'number' => 20, |
| 74 |
'search' => '*' . esc_attr( $search ) . '*', |
| 75 |
'search_columns' => [ 'user_login', 'user_email', 'display_name' ], |
| 76 |
]; |
| 77 |
|
| 78 |
$users = get_users( $args ); |
| 79 |
|
| 80 |
foreach ( $users as $user ) { |
| 81 |
$results[] = [ |
| 82 |
'id' => $user->ID, |
| 83 |
'text' => $user->display_name, |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
return $results; |
| 88 |
} |
| 89 |
|
| 90 |
private function get_user_roles_by_search( $search = '' ) { |
| 91 |
|
| 92 |
global $wp_roles; |
| 93 |
$results = []; |
| 94 |
|
| 95 |
foreach ( $wp_roles->roles as $role_key => $role ) { |
| 96 |
|
| 97 |
if ( $search && stripos( $role['name'], $search ) === false ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$results[] = [ |
| 102 |
'id' => $role_key, |
| 103 |
'text' => $role['name'], |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
return $results; |
| 108 |
} |
| 109 |
|
| 110 |
public function get_posts_title_by_id() { |
| 111 |
|
| 112 |
check_ajax_referer('easyel_autocomplete_nonce', 'security'); |
| 113 |
|
| 114 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 115 |
$raw_ids = $_POST['id'] ?? []; |
| 116 |
$ids = map_deep( wp_parse_args( wp_unslash( $raw_ids ?? [] ), [] ), 'sanitize_text_field' ); |
| 117 |
$post_type = isset($_POST['post_type']) ? sanitize_text_field( wp_unslash($_POST['post_type']) ) : 'post'; |
| 118 |
$query_type = isset($_POST['query_type']) ? sanitize_text_field( wp_unslash($_POST['query_type']) ) : ''; |
| 119 |
$results = []; |
| 120 |
|
| 121 |
switch($query_type){ |
| 122 |
case 'post_type': |
| 123 |
case 'single_post_type': |
| 124 |
foreach($ids as $id) { |
| 125 |
$obj = get_post_type_object($id); |
| 126 |
if($obj) $results[$id]=$obj->label; |
| 127 |
} |
| 128 |
break; |
| 129 |
case 'post_id': |
| 130 |
$query = new \WP_Query([ |
| 131 |
'post_type'=>get_post_types(['public'=>true]), |
| 132 |
'post__in'=>$ids, |
| 133 |
'posts_per_page'=>-1, |
| 134 |
'orderby'=>'post__in' |
| 135 |
]); |
| 136 |
foreach($query->posts as $post) $results[$post->ID]=$post->post_title; |
| 137 |
break; |
| 138 |
case 'taxonomy': |
| 139 |
foreach($ids as $id){ |
| 140 |
$taxonomy=get_taxonomy($id); |
| 141 |
|
| 142 |
if($taxonomy) $results[$id]=$taxonomy->label; |
| 143 |
} |
| 144 |
break; |
| 145 |
case 'term_id': |
| 146 |
case 'single_posts_term_id': |
| 147 |
foreach($ids as $id){ |
| 148 |
$term=get_term($id); |
| 149 |
if($term && !is_wp_error($term)) $results[$id]=$term->name; |
| 150 |
} |
| 151 |
break; |
| 152 |
|
| 153 |
case 'user_id': |
| 154 |
foreach ( $ids as $id ) { |
| 155 |
$user = get_user_by( 'id', $id ); |
| 156 |
if ( $user ) { |
| 157 |
$results[ $id ] = $user->display_name; |
| 158 |
} |
| 159 |
} |
| 160 |
break; |
| 161 |
|
| 162 |
case 'user_role': |
| 163 |
global $wp_roles; |
| 164 |
foreach ( $ids as $id ) { |
| 165 |
if ( isset( $wp_roles->roles[ $id ] ) ) { |
| 166 |
$results[ $id ] = $wp_roles->roles[ $id ]['name']; |
| 167 |
} |
| 168 |
} |
| 169 |
break; |
| 170 |
|
| 171 |
default: |
| 172 |
$query = new \WP_Query(['post_type'=>$post_type,'post__in'=>$ids,'posts_per_page'=>-1,'orderby'=>'post__in']); |
| 173 |
foreach($query->posts as $post) $results[$post->ID]=$post->post_title; |
| 174 |
} |
| 175 |
|
| 176 |
wp_send_json($results); |
| 177 |
} |
| 178 |
|
| 179 |
public function get_taxonomies_by_query() { |
| 180 |
|
| 181 |
check_ajax_referer('easyel_autocomplete_nonce', 'security'); |
| 182 |
$search = isset($_POST['q']) ? sanitize_text_field( wp_unslash($_POST['q']) ) : ''; |
| 183 |
|
| 184 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 185 |
$taxonomy = $_POST['taxonomy'] ?? []; |
| 186 |
$taxonomy = map_deep( wp_parse_args( wp_unslash( $taxonomy ?? [] ), [] ), 'sanitize_text_field' ); |
| 187 |
|
| 188 |
$taxonomy = is_array($taxonomy) ? array_filter($taxonomy, 'taxonomy_exists') : $taxonomy; |
| 189 |
$results=[]; |
| 190 |
|
| 191 |
$terms = get_terms(['taxonomy'=>$taxonomy,'hide_empty'=>false,'search'=>$search]); |
| 192 |
foreach($terms as $term) { |
| 193 |
$results[]=['id'=>$term->term_id,'text'=>$term->name]; |
| 194 |
} |
| 195 |
|
| 196 |
wp_send_json($results); |
| 197 |
} |
| 198 |
|
| 199 |
public function get_taxonomies_title_by_id() { |
| 200 |
|
| 201 |
check_ajax_referer('easyel_autocomplete_nonce', 'security'); |
| 202 |
|
| 203 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 204 |
$raw_ids = $_POST['id'] ?? []; |
| 205 |
$ids = map_deep( wp_parse_args( wp_unslash( $raw_ids ?? [] ), [] ), 'sanitize_text_field' ); |
| 206 |
$terms = get_terms(['include'=>$ids,'hide_empty'=>false]); |
| 207 |
$results=[]; |
| 208 |
foreach($terms as $term) { |
| 209 |
$results[$term->term_id]=$term->name; |
| 210 |
} |
| 211 |
|
| 212 |
wp_send_json($results); |
| 213 |
} |
| 214 |
|
| 215 |
// Helper methods |
| 216 |
private function get_public_post_types($search=''){ |
| 217 |
$results=[]; |
| 218 |
$types = get_post_types(['public'=>true],'objects'); |
| 219 |
foreach($types as $type){ |
| 220 |
if($search && stripos($type->label,$search)===false) continue; |
| 221 |
$results[]=['id'=>$type->name,'text'=>$type->label]; |
| 222 |
} |
| 223 |
return $results; |
| 224 |
} |
| 225 |
|
| 226 |
private function get_posts_by_id($search=''){ |
| 227 |
$results=[]; |
| 228 |
$posts = get_posts(['post_type'=>get_post_types(['public'=>true]),'posts_per_page'=>100,'s'=>$search]); |
| 229 |
foreach($posts as $post) $results[]=['id'=>$post->ID,'text'=>$post->post_title]; |
| 230 |
return $results; |
| 231 |
} |
| 232 |
|
| 233 |
private function get_public_taxonomies($search=''){ |
| 234 |
$results=[]; |
| 235 |
$taxes=get_taxonomies(['public'=>true],'objects'); |
| 236 |
foreach($taxes as $tax){ |
| 237 |
if($search && stripos($tax->label,$search)===false) continue; |
| 238 |
$results[]=['id'=>$tax->name,'text'=>$tax->label]; |
| 239 |
} |
| 240 |
return $results; |
| 241 |
} |
| 242 |
|
| 243 |
private function get_terms_by_search($search=''){ |
| 244 |
$results=[]; |
| 245 |
$taxes = get_taxonomies( ['public' => true] ); |
| 246 |
|
| 247 |
|
| 248 |
foreach($taxes as $tax){ |
| 249 |
$terms=get_terms(['taxonomy'=>$tax,'hide_empty'=>false,'search'=>$search]); |
| 250 |
foreach($terms as $term) $results[]=['id'=>$term->term_id,'text'=>$term->name]; |
| 251 |
} |
| 252 |
return $results; |
| 253 |
} |
| 254 |
|
| 255 |
private function get_posts_by_search($post_type,$search=''){ |
| 256 |
$results=[]; |
| 257 |
$query=new \WP_Query(['post_type'=>$post_type,'post_status'=>'publish','s'=>$search,'posts_per_page'=>-1]); |
| 258 |
foreach($query->posts as $post) $results[]=['id'=>$post->ID,'text'=>$post->post_title]; |
| 259 |
return $results; |
| 260 |
} |
| 261 |
} |