| Server IP : 141.193.213.11 / Your IP : 216.73.216.101 Web Server : nginx System : Linux pod-400555 6.8.0-1055-gke #61-Ubuntu SMP Tue May 26 22:57:42 UTC 2026 x86_64 User : fpm200079 ( 200079) PHP Version : 8.2.36 Disable Function : apache_child_terminate,apache_get_modules,apache_get_version,apache_getenv,apache_note,apache_setenv,disk_free_space,disk_total_space,diskfreespace,dl,exec,fastcgi_finish_request,link,opcache_compile_file,opcache_get_configuration,opcache_invalidate,opcache_is_script_cached,opcache_reset,passthru,pclose,pcntl_exec,popen,posix_getpid,posix_getppid,posix_getpwuid,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_uname,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,realpath_cache_get,shell_exec,show_source,symlink,system MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /nas/content/live/bartislaw/wp-content/plugins/meta-box/inc/fields/ |
Upload File : |
<?php
defined( 'ABSPATH' ) || die;
/**
* The user select field.
*/
class RWMB_User_Field extends RWMB_Object_Choice_Field {
public static function admin_enqueue_scripts( $field = null ): void {
parent::admin_enqueue_scripts( $field );
if ( 'select_advanced' === $field['field_type'] ) {
wp_enqueue_style( 'rwmb-object-thumbnail', RWMB_CSS_URL . 'object-thumbnail.css', [], RWMB_VER );
wp_style_add_data( 'rwmb-object-thumbnail', 'path', RWMB_CSS_DIR . 'object-thumbnail.css' );
}
}
public static function add_actions() {
add_action( 'wp_ajax_rwmb_get_users', [ __CLASS__, 'ajax_get_users' ] );
add_action( 'wp_ajax_nopriv_rwmb_get_users', [ __CLASS__, 'ajax_get_users' ] );
add_action( 'clean_user_cache', [ __CLASS__, 'update_cache' ] );
}
public static function ajax_get_users() {
$request = rwmb_request();
$field = self::get_ajax_field();
// Search.
$term = (string) $request->filter_post( 'term' );
if ( $term ) {
$field['query_args']['search'] = "*{$term}*";
}
// Pagination.
$limit = $field['query_args']['number'] ?? 0;
$limit = (int) $limit;
if ( $limit && 'query:append' === $request->filter_post( '_type' ) ) {
$field['query_args']['paged'] = $request->filter_post( 'page', FILTER_SANITIZE_NUMBER_INT );
}
// Query the database.
$items = self::query( null, $field );
$items = array_values( $items );
$items = apply_filters( 'rwmb_ajax_get_users', $items, $field, $request );
$data = [ 'items' => $items ];
// More items for pagination.
if ( $limit && count( $items ) === $limit ) {
$data['more'] = true;
}
wp_send_json_success( $data );
}
/**
* Update object cache to make sure query method below always get the fresh list of users.
* Unlike posts and terms, WordPress doesn't set 'last_changed' for users.
* So we have to do it ourselves.
*
* @see clean_post_cache()
*/
public static function update_cache() {
wp_cache_set( 'last_changed', microtime(), 'users' );
}
/**
* Normalize parameters for field.
*
* @param array $field Field parameters.
*
* @return array
*/
public static function normalize( $field ) {
// Set default field args.
$field = wp_parse_args( $field, [
'placeholder' => __( 'Select a user', 'meta-box' ),
'query_args' => [],
'display_field' => 'display_name',
'show_thumbnail' => false,
] );
$field = parent::normalize( $field );
// Set default query args.
$limit = $field['ajax'] ? 10 : 0;
$field['query_args'] = wp_parse_args( $field['query_args'], [
'number' => $limit,
] );
if ( 'select_advanced' === $field['field_type'] ) {
$field['show_thumbnail'] = true;
$field['js_options']['show_thumbnail'] = true;
}
parent::set_ajax_params( $field );
return $field;
}
public static function query( $meta, array $field ): array {
$display_field = $field['display_field'];
$args = wp_parse_args( $field['query_args'], [
'orderby' => $display_field,
'order' => 'asc',
// If the search term has "@", WordPress also searches user_email. This lets visitors find emails.
'search_columns' => [ 'user_login', 'user_nicename', 'display_name' ],
] );
$args['fields'] = [
'ID',
'user_login',
'user_nicename',
'user_url',
'user_registered',
'user_status',
'display_name',
];
$meta = wp_parse_id_list( (array) $meta );
// Query only selected items.
if ( ! empty( $field['ajax'] ) && ! empty( $meta ) ) {
$args['include'] = $meta;
$args['number'] = count( $meta );
}
// Get from cache to prevent same queries.
$last_changed = wp_cache_get_last_changed( 'users' );
$show_thumbnail = ! empty( $field['show_thumbnail'] );
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
$key = md5( serialize( $args ) );
$cache_key = "$key:$last_changed:" . (int) $show_thumbnail;
$options = wp_cache_get( $cache_key, 'meta-box-user-field' );
if ( false !== $options ) {
return $options;
}
$users = get_users( $args );
$options = [];
foreach ( $users as $user ) {
$label = $user->$display_field ?? __( '(No title)', 'meta-box' );
$label = self::filter( 'choice_label', $label, $field, $user );
$options[ $user->ID ] = [
'value' => $user->ID,
'label' => $label,
];
if ( $show_thumbnail ) {
$avatar = get_avatar_url( $user->ID, [ 'size' => 40 ] ); // 2x for retina screen
$options[ $user->ID ]['thumbnail'] = $avatar ?: '';
}
}
// Cache the query.
wp_cache_set( $cache_key, $options, 'meta-box-user-field' );
return $options;
}
/**
* Format a single value for the helper functions. Sub-fields should overwrite this method if necessary.
*
* @param array $field Field parameters.
* @param int $value User ID.
* @param array $args Additional arguments. Rarely used. See specific fields for details.
* @param int|null $post_id Post ID. null for current post. Optional.
*
* @return string
*/
public static function format_single_value( $field, $value, $args, $post_id ) {
if ( empty( $value ) ) {
return '';
}
$link = $args['link'] ?? 'view';
$user = get_userdata( $value );
$display_field = $field['display_field'];
$text = $user->$display_field;
if ( false === $link ) {
return $text;
}
$url = get_author_posts_url( $value );
if ( 'edit' === $link ) {
$url = get_edit_user_link( $value );
}
return sprintf( '<a href="%s">%s</a>', esc_url( $url ), esc_html( $text ) );
}
public static function add_new_form( array $field ): string {
if ( ! current_user_can( 'create_users' ) ) {
return '';
}
return sprintf(
'<a href="#" class="rwmb-user-add-button rwmb-modal-add-button" data-url="%s">%s</a>',
admin_url( 'user-new.php' ),
esc_html__( 'Add New User', 'meta-box' )
);
}
}