'',
'includeAuthorsWoPosts' => false,
'includeContributors' => false,
'includeAdmin' => false,
'includeEditors' => false,
'authorLimit' => 0,
'sort' => null,
'sortReverse' => null,
'unorderedListClass' => null,
'listItemClass' => null,
'spanCountClass' => null,
'spanAuthorClass' => null,
'authorLinkClass' => null,
'showCount' => null,
'countBeforeName' => null,
'moreAuthorsLink' => null,
'moreAuthorsText' => null,
'showAvatarMode' => null,
'showAsDropdown' => false,
'dropdownUnselectedText' => '',
'showAsOrderedList' => false,
'noShowUrlFilter' => null
);
function getAuthorListData(
$aIncludeAuthorsWithoutPosts = true, //affects the SQL
$aIncludeEditors = true, //affects the SQL
$aIncludeAdministrators = true, //affects the SQL
$aAuthorLimit = -1, //affects the SQL
$aIncludeContributors = true, //affects the SQL
$aOrderBy = null, //affects the SQL
$aOrderReverse = false, //affects the SQL
$behaviorShowMoreLink = false, //affects the SQL (returns an extra row to determine if we should show the more link)
$aNoPostCount = 0 //Reserved for future use to perform query optimization
) {
global $wpdb;
$behaviorSqlJoinMode_Post = $aIncludeAuthorsWithoutPosts ? 'LEFT OUTER' : '';
$behaviorSqlFilter_MatchEditors = $aIncludeEditors ? " OR capa.meta_value LIKE '%editor%'" : '';
$behaviorSqlFilter_MatchAdmins = $aIncludeAdministrators ? " OR capa.meta_value LIKE '%admin%'" : '';
$behaviorSqlFilter_MatchContribs = $aIncludeContributors ? " OR capa.meta_value LIKE '%contrib%'" : '';
$behaviorSqlFilter_Limit = $aAuthorLimit > 0 ? "LIMIT {$aAuthorLimit}" : '';
$aAuthorLimitPlusOne = $aAuthorLimit + 1;
$behaviorSqlFilter_LimitPlusOne = $aAuthorLimit > 0 ? "LIMIT {$aAuthorLimitPlusOne}" : '';
//$behaviorShowMoreLink = ($aMoreAuthorsLink != '');
if ($aAuthorLimit > 0 && (!$behaviorShowMoreLink)) $behaviorSqlFilter_LimitPlusOne = $behaviorSqlFilter_Limit;
//Build the ORDER BY query for different sort options
if ('fname' == $aOrderBy || 'lname' == $aOrderBy || 'post_count' == $aOrderBy || 'ID' == $aOrderBy || 'display_name' == $aOrderBy)
$behaviorSqlSort = 'ORDER BY '.$aOrderBy;
else if ('flname' == $aOrderBy)
$behaviorSqlSort = 'ORDER BY fname, lname';
else if ('lfname' == $aOrderBy)
$behaviorSqlSort = 'ORDER BY lname, fname';
else if ('custom' == $aOrderBy)
$behaviorSqlSort = 'ORDER BY usr.knr_author_order, ID';
if ('' != $behaviorSqlSort && 'custom' != $behaviorSqlSort) $behaviorSqlSort .= ($aOrderReverse ? ' DESC' : '');
$userrows = $wpdb->get_results("
SELECT
usr.ID,
usr.user_email,
fnametab.meta_value AS fname,
lnametab.meta_value as lname,
cnt.post_count
FROM {$wpdb->users} usr
JOIN {$wpdb->usermeta} capa ON usr.ID = capa.user_id AND capa.meta_key LIKE '%capabilities'
LEFT OUTER JOIN {$wpdb->usermeta} fnametab ON usr.ID = fnametab.user_id AND fnametab.meta_key = 'first_name'
LEFT OUTER JOIN {$wpdb->usermeta} lnametab ON usr.ID = lnametab.user_id AND lnametab.meta_key = 'last_name'
{$behaviorSqlJoinMode_Post} JOIN (
SELECT post_author, COUNT(1) post_count
FROM {$wpdb->posts}
WHERE post_type='post'
AND post_status='publish'
GROUP BY post_author
) cnt ON usr.ID = cnt.post_author
WHERE capa.meta_value LIKE '%author%'{$behaviorSqlFilter_MatchContribs}{$behaviorSqlFilter_MatchEditors}{$behaviorSqlFilter_MatchAdmins}
{$behaviorSqlSort}
{$behaviorSqlFilter_LimitPlusOne}
");
return (array) $userrows;
}
function getAvatar($aMode, $aId, $aSize) {
if (1 == $aMode) {
return get_avatar($aId, $aSize);
} else {
return '';
}
}
function getAuthorInfo_ddl_knrAuthorList(
$aIncludeAuthorsWithoutPosts = true, //affects the SQL
$aIncludeEditors = true, //affects the SQL
$aIncludeAdministrators = true, //affects the SQL
$aAuthorLimit = -1, //affects the SQL
$aIncludeContributors = true, //affects the SQL
$aOrderBy = null, //affects the SQL
$aOrderReverse = false, //affects the SQL
$aUnorderedListClass = '',
$aListItemClass = '',
$aSpanCountClass = '',
$aSpanAuthorClass = '',
$aAuthorLinkClass = '',
$aShowCount = true,
$aCountBeforeName = false,
$aMoreAuthorsLink = '',
$aMoreAuthorsText = '...',
$aShowAvatarMode = 0,
$aDropdownUnselectedText = '',
$aShowAsOrderedList = false
) {
$behaviorShowCount = $aShowCount;
$behaviorCountBeforeName = $aCountBeforeName;
$userrows_arr = getAuthorListData($aIncludeAuthorsWithoutPosts, $aIncludeEditors, $aIncludeAdministrators, $aAuthorLimit, $aIncludeContributors, $aOrderBy, $aOrderReverse, ($aMoreAuthorsLink != ''), 0);
$blogUrl = get_bloginfo('url');
$knrAuthListDdl_Script = '';
echo $knrAuthListDdl_Script;
}
//Output the list items (LI tags)
function getAuthorInfo_knrAuthorList(
$aIncludeAuthorsWithoutPosts = true, //affects the SQL
$aIncludeEditors = true, //affects the SQL
$aIncludeAdministrators = true, //affects the SQL
$aAuthorLimit = -1, //affects the SQL
$aIncludeContributors = true, //affects the SQL
$aOrderBy = null, //affects the SQL
$aOrderReverse = false, //affects the SQL
$aUnorderedListClass = '',
$aListItemClass = '',
$aSpanCountClass = '',
$aSpanAuthorClass = '',
$aAuthorLinkClass = '',
$aShowCount = true,
$aCountBeforeName = false,
$aMoreAuthorsLink = '',
$aMoreAuthorsText = '...',
$aShowAvatarMode = 0,
$aDropdownUnselectedText = '',
$aShowAsOrderedList = false
) {
if ($aShowAsOrderedList)
echo '
'.CR;
else
echo '
'.CR;
$behaviorShowCount = $aShowCount;
$behaviorCountBeforeName = $aCountBeforeName;
$markupListItemClass = '';
if (isset($aListItemClass) && '' != $aListItemClass)
$markupListItemClass = ' class="' . $aListItemClass . '"';
$markupAuthorLinkClass = '';
if (isset($aAuthorLinkClass) && '' != $aAuthorLinkClass)
$markupAuthorLinkClass = ' class="' . $aAuthorLinkClass . '"';
$markupSpanCountClass = '';
if (isset($aSpanCountClass) && '' != $aSpanCountClass)
$markupSpanCountClass = ' class="' . $aSpanCountClass . '"';
$markupSpanAuthorClass = '';
if (isset($aSpanAuthorClass) && '' != $aSpanAuthorClass)
$markupSpanAuthorClass = ' class="' . $aSpanAuthorClass . '"';
$userrows_arr = getAuthorListData($aIncludeAuthorsWithoutPosts, $aIncludeEditors, $aIncludeAdministrators, $aAuthorLimit, $aIncludeContributors, $aOrderBy, $aOrderReverse, ($aMoreAuthorsLink != ''), 0);
$blogUrl = get_bloginfo('url');
$outerMoreLink = ''; //used for ordered lists
$behaviorShowMoreLink = ($aMoreAuthorsLink != '');
//loop over each of the rows in the result set
for ($i = 0; $i < count($userrows_arr); $i++) {
//We have an author limit set and we've reached it
if ($aAuthorLimit > 0 && $i == $aAuthorLimit) {
//if we're supposed to show the more link
if ($behaviorShowMoreLink) {
$authorNameMarkup = "
$aMoreAuthorsText
";
if ('' != $markupSpanAuthorClass)
$authorNameMarkup = ''.$authorNameMarkup.'';
$countMarkup='';
if ($aShowAsOrderedList)
$outerMoreLink = '
'.$authorNameMarkup.'
';
else
//output the actual 'more' markup
echo '
'.
$authorNameMarkup
.'
'.CR;
}
break;
}
$row = $userrows_arr[$i];
//foreach((array) $userrows as $row) {
$row->post_count=$row->post_count?$row->post_count:0;
//$authorUrl = $blogUrl . '/?author=' . $row->ID;
$authorUrl = get_author_posts_url($row->ID);
//We shouldn't need this since the user must have entered a first name and last name, but just in case
if ($row->lname == null && $row->fname == null) {
$tempUser = get_userdata($row->ID);
if ($tempUser->nickname != null)
$row->fname = $tempUser->nickname;
else
$row->fname = $tempUser->user_login;
}
$authorNameMarkup = "
fname} {$row->lname}\"{$markupAuthorLinkClass}>
{$row->fname} {$row->lname}
";
if ('' != $markupSpanAuthorClass)
$authorNameMarkup = ''.$authorNameMarkup.'';
$countMarkup='';
if ($behaviorShowCount) {
$countMarkup = '('.$row->post_count.')';
if ('' != $markupSpanCountClass)
$countMarkup = ''.$countMarkup.'';
}
if (0 != $aShowAvatarMode)
$avatarPic = getAvatar($aShowAvatarMode, $row->user_email, 16);
else
$avatarPic = '';
echo '
'.CR;
} else
echo ' '.CR;
}
//writes the unordered list tags and passes options to the getAuthorInfo_knrAuthorList function
function writeMarkup_knrAuthorList($options) {
if ($options == null || count($options) == 0) {
$options = getCurrentOptions_knrAuthorList(); //supporting legacy code
}
if (isset($options['showAsDropdown']) && $options['showAsDropdown'])
getAuthorInfo_ddl_knrAuthorList(
$options['includeAuthorsWoPosts'],
$options['includeEditors'],
$options['includeAdmin'],
isset($options['authorLimit'])?$options['authorLimit']:-1,
$options['includeContributors'],
$options['sort'],
$options['sortReverse'],
$options['unorderedListClass'],
$options['listItemClass'],
$options['spanCountClass'],
$options['spanAuthorClass'],
$options['authorLinkClass'],
$options['showCount'],
$options['countBeforeName'],
$options['moreAuthorsLink'],
$options['moreAuthorsText'],
$options['showAvatarMode'],
$options['dropdownUnselectedText'],
$options['showAsOrderedList']
);
else
getAuthorInfo_knrAuthorList(
$options['includeAuthorsWoPosts'],
$options['includeEditors'],
$options['includeAdmin'],
isset($options['authorLimit'])?$options['authorLimit']:-1,
$options['includeContributors'],
$options['sort'],
$options['sortReverse'],
$options['unorderedListClass'],
$options['listItemClass'],
$options['spanCountClass'],
$options['spanAuthorClass'],
$options['authorLinkClass'],
$options['showCount'],
$options['countBeforeName'],
$options['moreAuthorsLink'],
$options['moreAuthorsText'],
$options['showAvatarMode'],
$options['dropdownUnselectedText'],
$options['showAsOrderedList']
);
}
//sets the default options
function getDefaultOptionArray_knrAuthorList() {
$defaultTitle = 'Authors';
$defaultIncludeAuthorsWoPosts = true;
$defaultIncludeContributors = true;
$defaultIncludeEditors = true;
$defaultIncludeAdmin = false;
$defaultAuthorLimit = -1;
$defaultSort = 'none';
$defaultSortReverse = false;
$defaultUnorderedListClass = ''; //CSS class for UL tag below heading tag
$defaultListItemClass = ''; //CSS class for LI tag for each author in the list
$defaultSpanCountClass = ''; //CSS class for SPAN tag for the post-count in the list items
$defaultSpanAuthorClass = ''; //CSS class for SPAN tag for each author in the list items
$defaultAuthorLinkClass = ''; //CSS class for A HREF tag for each author in the list
$defaultShowCount = true;
$defaultCountBeforeName = false;
$defaultMoreAuthorsLink = '';
$defaultShowAvatarMode = 0;
$defaultShowAsDropdown = false;
$defaultDropdownUnselectedText = '';
$defaultShowAsOrderedList = false;
$defaultNoShowUrlFilter = '';
return array('title'=>$defaultTitle,
'includeAuthorsWoPosts'=>$defaultIncludeAuthorsWoPosts,
'includeContributors'=>$defaultIncludeContributors,
'includeAdmin'=>$defaultIncludeAdmin,
'includeEditors'=>$defaultIncludeEditors,
'authorLimit'=>$defaultAuthorLimit,
'sort'=>$defaultSort,
'sortReverse'=>$defaultSortReverse,
'unorderedListClass'=>$defaultUnorderedListClass,
'listItemClass'=>$defaultListItemClass,
'spanCountClass'=>$defaultSpanCountClass,
'spanAuthorClass'=>$defaultSpanAuthorClass,
'authorLinkClass'=>$defaultAuthorLinkClass,
'showCount'=>$defaultShowCount,
'countBeforeName'=>$defaultCountBeforeName,
'moreAuthorsLink'=>$defaultMoreAuthorsLink,
'moreAuthorsText'=>$defaultMoreAuthorsText,
'showAvatarMode'=>$defaultShowAvatarMode,
'showAsDropdown'=>$defaultShowAsDropdown,
'dropdownUnselectedText'=>$defaultDropdownUnselectedText,
'showAsOrderedList'=>$defaultShowAsOrderedList,
'noShowUrlFilter'=>$defaultNoShowUrlFilter
);
}
//reads the plugin options
function getCurrentOptions_knrAuthorList() {
if ($loadedOptions == null) { //prevent reading options more than once in the same request
$options = get_option('widget_knrAuthorList');
if ( !is_array($options) ) {
$options = getDefaultOptionArray_knrAuthorList();
}
$loadedOptions = $options;
}
return $loadedOptions; //return $options;
}
/*
function checkForSkip($pattern, $currUrl) {
if (!$pattern) return false;
return ereg($pattern, $currUrl);
}
*/
/*
//outputs the 'skeleton' code
function widget_knrAuthorList($args) {
$options = getCurrentOptions_knrAuthorList();
$retValSkip = checkForSkip($options['noShowUrlFilter'], $_SERVER['REQUEST_URI']);
if ($retValSkip === 1) {
return;
}
extract($args);
echo $before_widget;
echo $before_title;
echo $options['title'];
echo $after_title;
writeMarkup_knrAuthorList($options);
echo $after_widget;
}
*/
//widget settings in Admin
function control_knrAuthorList() {
$options = getCurrentOptions_knrAuthorList();
if ($_POST["knrAuthorList-submit"]) {
$options['title'] = strip_tags(stripslashes($_POST['knrAuthorList-title']));
$options['includeAuthorsWoPosts'] = isset($_POST['knrAuthorList-incAuthorsWoPosts']);
$options['includeContributors'] = isset($_POST['knrAuthorList-incContributors']);
$options['includeAdmin'] = isset($_POST['knrAuthorList-incAdmin']);
$options['includeEditors'] = isset($_POST['knrAuthorList-incEditors']);
$options['authorLimit'] = strip_tags(stripslashes($_POST['knrAuthorList-authorLimit']));
$options['sort'] = strip_tags(stripslashes($_POST['knrAuthorList-sort']));
$options['sortReverse'] = isset($_POST['knrAuthorList-sortReverse']);
$options['unorderedListClass'] = strip_tags(stripslashes($_POST['knrAuthorList-unorderedListClass']));
$options['listItemClass'] = strip_tags(stripslashes($_POST['knrAuthorList-listItemClass']));
$options['spanCountClass'] = strip_tags(stripslashes($_POST['knrAuthorList-spanCountClass']));
$options['spanAuthorClass'] = strip_tags(stripslashes($_POST['knrAuthorList-spanAuthorClass']));
$options['authorLinkClass'] = strip_tags(stripslashes($_POST['knrAuthorList-authorLinkClass']));
$options['showCount'] = isset($_POST['knrAuthorList-showCount']);
$options['countBeforeName'] = isset($_POST['knrAuthorList-countBeforeName']);
$options['moreAuthorsLink'] = strip_tags(stripslashes($_POST['knrAuthorList-moreAuthorsLink']));
$options['moreAuthorsText'] = strip_tags(stripslashes($_POST['knrAuthorList-moreAuthorsText']));
$options['showAvatarMode'] = strip_tags(stripslashes($_POST['knrAuthorList-showAvatarMode']));
$options['showAsDropdown'] = isset($_POST['knrAuthorList-showAsDropdown']);
$options['dropdownUnselectedText'] = strip_tags(stripslashes($_POST['knrAuthorList-dropdownUnselectedText']));
$options['showAsOrderedList'] = isset($_POST['knrAuthorList-showAsOrderedList']);
$options['noShowUrlFilter'] = $_POST['knrAuthorList-noShowUrlFilter'];
update_option('widget_knrAuthorList', $options);
}
$title = htmlspecialchars($options['title'], ENT_QUOTES);
$includeAuthorsWoPosts = $options['includeAuthorsWoPosts'];
$includeContributors = $options['includeContributors'];
$includeAdmin = $options['includeAdmin'];
$includeEditors = $options['includeEditors'];
$authorLimit = $options['authorLimit'];
$sortOrder = $options['sort'];
$sortOrderReverse = $options['sortReverse'];
$unorderedListClass = $options['unorderedListClass'];
$listItemClass = $options['listItemClass'];
$spanCountClass = $options['spanCountClass'];
$spanAuthorClass = $options['spanAuthorClass'];
$authorLinkClass = $options['authorLinkClass'];
$showCount = $options['showCount'];
$countBeforeName = $options['countBeforeName'];
$moreAuthorsLink = $options['moreAuthorsLink'];
$moreAuthorsText = $options['moreAuthorsText'];
$showAvatarMode = $options['showAvatarMode'];
$showAsDropdown = $options['showAsDropdown'];
$dropdownUnselectedText = $options['dropdownUnselectedText'];
$showAsOrderedList = $options['showAsOrderedList'];
$noShowUrlFilter = $options['noShowUrlFilter'];
echo '
General Options
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
Enter -1 or 0 for no limit
';
echo '
Leave blank for no "more" link
';
echo '
';
echo '
';
echo '
With "Custom" sort order, you have to go to the "Author Order" setting page to manually set the sort order
';
echo '
Reverses the sort order set by "Sort with"
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
Markup Options
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '';
}
/*
//initializes the widget (ensure author ordering column is present)
function knrAuthorList_init() {
global $wpdb;
$wpdb->show_errors();
$query1 = $wpdb->query("SHOW COLUMNS FROM $wpdb->users LIKE 'knr_author_order'");
if ($query1 == 0) {
$wpdb->query("ALTER TABLE $wpdb->users ADD `knr_author_order` INT( 4 ) NULL DEFAULT '0'");
}
register_sidebar_widget(__('KNR Author List'), 'widget_knrAuthorList');
register_widget_control(__('KNR Author List'), 'control_knrAuthorList', 250, 400);
}
*/
//Adds the custom author order page to the settings menu of the Admin
function knrAuthorWidget_menu() {
add_options_page('knrCustomAuthorSort', 'KNR Author List', 'manage_options', __FILE__, 'knrAuthorWidget_menufunc');
}
//Admin menu for custom ordering of authors
function knrAuthorWidget_menufunc() {
/*
* I would like to mention that Wil Linssen's tutorial was really helpful in creating
* the drag-drop interface with jQuery UI Sortable for manually ordering the authors
*/
global $wpdb;
$dirloc=dirname(__FILE__);
$dirloc=str_ireplace('\\', '/', $dirloc);
$dirloc='..'.substr($dirloc,stripos($dirloc,'/wp-content')).'/';
echo <<< EOS
EOS;
$sqlAuthorListQuery = "
SELECT
usr.ID,
fnametab.meta_value AS fname,
lnametab.meta_value as lname
FROM {$wpdb->users} usr
JOIN {$wpdb->usermeta} capa ON usr.ID = capa.user_id AND capa.meta_key LIKE '%capabilities%'
LEFT OUTER JOIN {$wpdb->usermeta} fnametab ON usr.ID = fnametab.user_id AND fnametab.meta_key = 'first_name'
LEFT OUTER JOIN {$wpdb->usermeta} lnametab ON usr.ID = lnametab.user_id AND lnametab.meta_key = 'last_name'
WHERE capa.meta_value NOT LIKE '%subscribe%'
ORDER BY usr.knr_author_order, ID
";
$authorRows = $wpdb->get_results($sqlAuthorListQuery);
echo CR.'
If you require any assistance with this plugin, feel free to contact me on k.nitin.r'.'@gmail.com or k_nitin_r'.'@yahoo.co.in
or leave me a comment on my blog at http://www.nitinkatkam.com .
'.CR;
echo CR.'
Custom Author Ordering
'.CR;
echo 'This page is for only for ordering the authors manually. For other settings (and to set the sort order to "custom"), go to the Widgets page under Appearance, add the KNR Author List widget to your sidebar and click "Edit".'.CR;
echo '
Please order the authors below by dragging & dropping them at the desired position.
'.CR;
echo '
'.CR;
$blogUrl = get_bloginfo('url');
foreach((array) $authorRows as $row) {
$authorUrl = $blogUrl . '/?author=' . $row->ID;
//We shouldn't need this since the user must have entered a first name and last name, but just in case
if ($row->lname == null && $row->fname == null) {
$tempUser = get_userdata($row->ID);
if ($tempUser->nickname != null)
$row->fname = $tempUser->nickname;
else
$row->fname = $tempUser->user_login;
}
echo '