LLMS_AJAX_Handler::select2_query_posts()
Handle Select2 Search boxes for WordPress Posts by Post Type and Post Status.
Contents
Return Return
(void)
Source Source
File: includes/class.llms.ajax.handler.php
$gateways_html = ob_get_clean();
ob_start();
llms_get_template(
'checkout/form-summary.php',
array(
'coupon' => false,
'plan' => $plan,
'product' => $plan->get_product(),
)
);
$summary_html = ob_get_clean();
return array(
'coupon_html' => $coupon_html,
'gateways_html' => $gateways_html,
'summary_html' => $summary_html,
);
}
/**
* Handle Select2 Search boxes for WordPress Posts by Post Type and Post Status.
*
* @since 3.0.0
* @since 3.32.0 Updated to use llms_filter_input().
* @since 3.32.0 Posts can be queried by post status(es) via the `$_POST['post_statuses']`.
* By default only the published posts will be queried.
* @since 3.37.2 Posts can be 'filtered' by instructor via the `$_POST['instructor_id']`.
* @since 5.5.0 Do not encode quotes when sanitizing search term.
* @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`.
*
* @return void
*/
public static function select2_query_posts() {
global $wpdb;
if ( ! is_user_logged_in() ) {
wp_die();
}
// Grab the search term if it exists.
$term = llms_filter_input_sanitize_string( INPUT_POST, 'term', array( FILTER_FLAG_NO_ENCODE_QUOTES ) );
// Get the page.
$page = llms_filter_input( INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT );
// Get post type(s).
$post_type = sanitize_text_field( llms_filter_input_sanitize_string( INPUT_POST, 'post_type' ) );
$post_types_array = explode( ',', $post_type );
foreach ( $post_types_array as &$str ) {
$str = "'" . esc_sql( trim( $str ) ) . "'";
}
$post_types = implode( ',', $post_types_array );
// Get post status(es).
$post_statuses = llms_filter_input_sanitize_string( INPUT_POST, 'post_statuses' );
$post_statuses = empty( $post_statuses ) ? 'publish' : $post_statuses;
$post_statuses_array = explode( ',', $post_statuses );
foreach ( $post_statuses_array as &$str ) {
$str = "'" . esc_sql( trim( $str ) ) . "'";
}
$post_statuses = implode( ',', $post_statuses_array );
// Filter posts (llms posts) by instructor ID.
$instructor_id = llms_filter_input( INPUT_POST, 'instructor_id', FILTER_SANITIZE_NUMBER_INT );
if ( ! empty( $instructor_id ) ) {
$serialized_iid = serialize(
array(
'id' => absint( $instructor_id ),
)
);
$serialized_iid = str_replace( array( 'a:1:{', '}' ), '', $serialized_iid );
$join = $wpdb->prepare(
" JOIN $wpdb->postmeta AS m ON p.ID = m.post_id AND m.meta_key = '_llms_instructors' AND m.meta_value LIKE %s",
'%' . $wpdb->esc_like( $serialized_iid ) . '%'
);
} else {
$join = '';
}
$limit = 30;
$start = $limit * $page;
if ( $term ) {
$like = " AND post_title LIKE '%s'";
$vars = array( '%' . $term . '%', $start, $limit );
} else {
$like = '';
$vars = array( $start, $limit );
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$posts = $wpdb->get_results(
$wpdb->prepare(
"SELECT p.ID as ID, p.post_title as post_title, p.post_type as post_type
FROM $wpdb->posts as p
$join
WHERE p.post_type IN ( $post_types )
AND p.post_status IN ( $post_statuses )
$like
ORDER BY post_title
LIMIT %d, %d
",
$vars
) // phpcs:ignore -- The number of params is correct, $vars is an array of two elements.
);// no-cache ok.
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$items = array();
$grouping = ( count( $post_types_array ) > 1 );
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 5.9.0 | Stop using deprecated FILTER_SANITIZE_STRING. |
| 5.5.0 | Do not encode quotes when sanitizing search term. |
| 3.37.2 | Posts can be 'filtered' by instructor via the $_POST['instructor_id']. |
| 3.32.0 | Posts can be queried by post status(es) via the $_POST['post_statuses']. By default only the published posts will be queried. |
| 3.0.0 | Introduced. |