$result,
)
);
die();
}
/**
* Get the user id through ajax, used in 'select2'.
*
* @param string $search_term Search term.
*
* @return array
*
* @since 4.5.1
*/
public static function get_users( $search_term = null ) {
$result = array();
$query_params = array();
if ( ! is_null( $search_term ) ) {
$query_params['search'] = '*' . $search_term . '*';
$query_params['search_columns'] = array( 'user_login', 'user_email' );
}
if ( WP_Helper::is_multisite() ) {
$query_params['blog_id'] = 0;
}
$users = get_users( $query_params );
if ( empty( $users ) ) {
return $result;
}
return array_map( array( __CLASS__, 'convert_object_to_select2_data' ), $users );
}
/**
* Converts given object to data that can be consumed by select2 library.
*
* @param object $object User, post or other supported object.
*
* @return array|void
*
* @since 4.5.1
*/
private static function convert_object_to_select2_data( $object ) {
if ( $object instanceof \WP_User ) {
return array(
'id' => $object->ID,
'text' => $object->user_login . ' (' . $object->user_email . ')',
);
}
if ( $object instanceof \WP_Post ) {
$post_title = $object->post_title;
if ( strlen( $post_title ) > 50 ) {
$post_title = substr( $post_title, 0, 50 ) . '...';
}
$post_id = $object->ID;
return array(
'id' => $post_id,
'text' => $post_title . ' (' . $post_id . ')',
);
}
}
/**
* Handles AJAX calls to retrieve post data to be used in 'select2'.
*
* @param string $search_term Search term.
*
* @return array
*
* @since 4.5.1
*/
public static function get_posts( $search_term ) {
$result = array();
$args = array(
'search_post_title' => $search_term, // Search post title only.
'suppress_filters' => false,
'post_status' => 'publish',
'post_type' => 'any',
);
add_filter( 'posts_where', array( __CLASS__, 'search_post_title' ), 10, 2 );
$posts = get_posts( $args );
remove_filter( 'posts_where', array( __CLASS__, 'search_post_title' ), 10 );
if ( empty( $posts ) ) {
return $result;
}
return array_map( array( __CLASS__, 'convert_object_to_select2_data' ), $posts );
}
/**
* Renders all the HTML, CSS and JS code necessary to display select2 form control configure using given parameters.
*
* @param array $args Select form control parameters.
*
* @since 4.5.1
*/
public static function insert( $args ) {
// name - string
// data-type - user, role, post
// placeholder - string
// width - int (pixels)
// id - string
// multiple - bool
// min_chars - int (minimum number of characters to type before searching)
// selected - selected value or an array of values
// extra_js_callback - function to call after the select2 init function is called, it should print additional JS if necessary, using "s2" as reference to the current select2 instance.
if ( ! isset( self::$base_url ) ) {
// Library not initialized correctly.
return;
}
if ( ! array_key_exists( 'name', $args ) ) {
// Field name is missing.
return;
}
if ( ! array_key_exists( 'data-type', $args ) && ! array_key_exists( 'data', $args ) ) {
// No data source defined.
return;
}
// Enqueue scripts.
if ( ! self::$scripts_queued ) {
self::enqueue_scripts();
}
$attributes = array(
'name' => $args['name'],
);
if ( ! array_key_exists( 'id', $args ) ) {
$args['id'] = self::$id_prefix . self::$id_counter++;
}
$attributes['id'] = $args['id'];
if ( array_key_exists( 'width', $args ) ) {
$attributes['style'] = 'width: ' . $args['width'] . 'px;';
}
array_walk(
$attributes,
function ( &$value, $key ) {
$value = $key . '="' . \esc_attr( $value ) . '"';
}
);
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '';
}
/**
* Enqueues necessary JS scripts and stylesheets.
*
* @since 4.5.1
* @since 5.0.0 - this method is no longer private.
*/
public static function enqueue_scripts() {
if ( self::$scripts_queued ) {
return;
}
if ( ! self::$initiated ) {
return;
}
wp_enqueue_style(
'wpw-select2',
self::$base_url . '/assets/css/select2.min.css',
array(),
'4.0.13'
);
wp_enqueue_style(
'wpw-select2-overrides',
self::$base_url . '/assets/css/wp-overrides.css',
array( 'wpw-select2' ),
'4.0.13'
);
wp_enqueue_script(
'wpw-select2',
self::$base_url . '/assets/js/select2.full.min.js',
array( 'jquery' ),
'4.0.13',
true
);
self::$scripts_queued = true;
}
/**
* Alters WordPress query to search only by post title.
*
* @param string $where SQL WHERE statement.
* @param WP_Query $wp_query WordPress query object.
*
* @return string
*
* @since 4.5.1
*/
public static function search_post_title( $where, $wp_query ) {
$search_term = $wp_query->get( 'search_post_title' );
if ( $search_term ) {
global $wpdb;
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . $wpdb->esc_like( $search_term ) . '%\'';
}
return $where;
}
}
}