Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Use Ajax to fetch users for author control, or use Customize Object Selector #240

Open
westonruter opened this issue Sep 2, 2016 · 0 comments

Comments

@westonruter
Copy link
Contributor

If there are a lot of users on a site, the time to generate the full author list upon initial customizer load will not be insignificant. This data should be lazy-loaded once it is needed (i.e. once an author control is visible) or better we should use Customize Object Selector once xwp/wp-customize-object-selector#13 is implemented.

See relevant code:

/**
* Get the author choices array.
*
* @return array
*/
public function get_author_choices() {
$choices = array();
$query_args = array(
'orderby' => 'display_name',
'who' => 'authors',
'fields' => array( 'ID', 'user_login', 'display_name' ),
);
$users = get_users( $query_args );
if ( ! empty( $users ) ) {
foreach ( (array) $users as $user ) {
$choices[] = array(
'value' => (int) $user->ID,
'text' => esc_html( sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'customize-posts' ), $user->display_name, $user->user_login ) ),
);
}
}
return $choices;
}

/**
* Add post author control.
*
* @returns {wp.customize.Control} Added control.
*/
addAuthorControl: function() {
var section = this, control, setting = api( section.id ), postTypeObj, previousValidate;
postTypeObj = api.Posts.data.postTypes[ section.params.post_type ];
control = new api.controlConstructor.dynamic( section.id + '[post_author]', {
params: {
section: section.id,
priority: 70,
label: postTypeObj.labels.author_field ? postTypeObj.labels.author_field : api.Posts.data.l10n.fieldAuthorLabel,
active: true,
settings: {
'default': setting.id
},
field_type: 'select',
setting_property: 'post_author',
choices: api.Posts.data.authorChoices
}
} );
// Ensure selected author is integer, and not a string of digits.
previousValidate = setting.validate;
setting.validate = function ensurePostAuthorInteger( inputData ) {
var data = _.clone( inputData );
data = previousValidate.call( this, data );
data.post_author = parseInt( data.post_author, 10 );
return data;
};
// Override preview trying to de-activate control not present in preview context. See WP Trac #37270.
control.active.validate = function() {
return true;
};
// Register.
section.postFieldControls.post_author = control;
api.control.add( control.id, control );
if ( control.notifications ) {
control.notifications.add = section.addPostFieldControlNotification;
control.notifications.setting_property = control.params.setting_property;
}
return control;
},

@westonruter westonruter added this to the 0.9.0 milestone Sep 2, 2016
@westonruter westonruter modified the milestones: 0.9.0, Next Major Release Aug 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant