| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Traits; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit(); |
| 7 |
} // Exit if accessed directly |
| 8 |
|
| 9 |
trait WordPressHelper { |
| 10 |
|
| 11 |
/** |
| 12 |
* Get all types of post. |
| 13 |
* |
| 14 |
* @param string $post_type |
| 15 |
* |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public function get_post_list($post_type = 'any') |
| 19 |
{ |
| 20 |
return $this->get_query_post_list($post_type); |
| 21 |
} |
| 22 |
|
| 23 |
public function get_query_post_list($post_type = 'any', $limit = -1, $search = '') |
| 24 |
{ |
| 25 |
global $wpdb; |
| 26 |
$where = ''; |
| 27 |
$data = []; |
| 28 |
|
| 29 |
if (-1 == $limit) { |
| 30 |
$limit = ''; |
| 31 |
} elseif (0 == $limit) { |
| 32 |
$limit = "limit 0,1"; |
| 33 |
} else { |
| 34 |
$limit = $wpdb->prepare(" limit 0,%d", esc_sql($limit)); |
| 35 |
} |
| 36 |
|
| 37 |
if ('any' === $post_type) { |
| 38 |
$in_search_post_types = get_post_types(['exclude_from_search' => false]); |
| 39 |
if (empty($in_search_post_types)) { |
| 40 |
$where .= ' AND 1=0 '; |
| 41 |
} else { |
| 42 |
$where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", |
| 43 |
array_map('esc_sql', $in_search_post_types)) . "')"; |
| 44 |
} |
| 45 |
} elseif (!empty($post_type)) { |
| 46 |
$where .= $wpdb->prepare(" AND {$wpdb->posts}.post_type = %s", esc_sql($post_type)); |
| 47 |
} |
| 48 |
|
| 49 |
if (!empty($search)) { |
| 50 |
$where .= $wpdb->prepare(" AND {$wpdb->posts}.post_title LIKE %s", '%' . esc_sql($search) . '%'); |
| 51 |
} |
| 52 |
|
| 53 |
$query = "select post_title,ID from $wpdb->posts where post_status = 'publish' $where $limit"; |
| 54 |
$results = $wpdb->get_results($query); |
| 55 |
if (!empty($results)) { |
| 56 |
foreach ($results as $row) { |
| 57 |
$data[$row->ID] = $row->post_title; |
| 58 |
} |
| 59 |
} |
| 60 |
return $data; |
| 61 |
} |
| 62 |
} |