external-users-wp.php 3.87 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: External user functions for WordPress integration


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../');
Scott committed
24 25
	exit;
}
Scott committed
26 27


Scott committed
28 29 30 31
function qa_get_mysql_user_column_type()
{
	return 'BIGINT UNSIGNED';
}
Scott committed
32 33


Scott committed
34 35 36 37 38 39 40 41
function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
{
	return array(
		'login' => wp_login_url(qa_opt('site_url') . $redirect_back_to_url),
		'register' => function_exists('wp_registration_url') ? wp_registration_url() : site_url('wp-login.php?action=register'),
		'logout' => strtr(wp_logout_url(), array('&amp;' => '&')),
	);
}
Scott committed
42 43


Scott committed
44 45 46
function qa_get_logged_in_user()
{
	$wordpressuser = wp_get_current_user();
Scott committed
47

Scott committed
48 49
	if ($wordpressuser->ID == 0)
		return null;
Scott committed
50

Scott committed
51 52 53 54 55 56 57 58 59
	else {
		if (current_user_can('administrator'))
			$level = QA_USER_LEVEL_ADMIN;
		elseif (current_user_can('editor'))
			$level = QA_USER_LEVEL_EDITOR;
		elseif (current_user_can('contributor'))
			$level = QA_USER_LEVEL_EXPERT;
		else
			$level = QA_USER_LEVEL_BASIC;
Scott committed
60

Scott committed
61 62 63 64 65 66
		return array(
			'userid' => $wordpressuser->ID,
			'publicusername' => $wordpressuser->user_nicename,
			'email' => $wordpressuser->user_email,
			'level' => $level,
		);
Scott committed
67
	}
Scott committed
68
}
Scott committed
69 70


Scott committed
71 72 73
function qa_get_user_email($userid)
{
	$user = get_userdata($userid);
Scott committed
74

Scott committed
75 76
	return @$user->user_email;
}
Scott committed
77 78


Scott committed
79 80 81
function qa_get_userids_from_public($publicusernames)
{
	global $wpdb;
Scott committed
82

Scott committed
83 84 85 86 87 88 89 90
	if (count($publicusernames))
		return qa_db_read_all_assoc(qa_db_query_sub(
			'SELECT user_nicename, ID FROM ' . $wpdb->base_prefix . 'users WHERE user_nicename IN ($)',
			$publicusernames
		), 'user_nicename', 'ID');
	else
		return array();
}
Scott committed
91 92


Scott committed
93 94 95
function qa_get_public_from_userids($userids)
{
	global $wpdb, $qa_cache_wp_user_emails;
Scott committed
96

Scott committed
97 98 99
	if (count($userids)) {
		$useridtopublic = array();
		$qa_cache_wp_user_emails = array();
Scott committed
100

Scott committed
101 102 103 104
		$userfields = qa_db_read_all_assoc(qa_db_query_sub(
			'SELECT ID, user_nicename, user_email FROM ' . $wpdb->base_prefix . 'users WHERE ID IN (#)',
			$userids
		), 'ID');
Scott committed
105

Scott committed
106 107 108 109
		foreach ($userfields as $id => $fields) {
			$useridtopublic[$id] = $fields['user_nicename'];
			$qa_cache_wp_user_emails[$id] = $fields['user_email'];
		}
Scott committed
110

Scott committed
111
		return $useridtopublic;
Scott committed
112

Scott committed
113 114 115
	} else
		return array();
}
Scott committed
116 117


Scott committed
118 119 120
function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
{
	$publicusername = $logged_in_user['publicusername'];
Scott committed
121

Scott committed
122 123
	return '<a href="' . qa_path_html('user/' . $publicusername) . '" class="qa-user-link">' . htmlspecialchars($publicusername) . '</a>';
}
Scott committed
124 125


Scott committed
126 127 128
function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
{
	$useridtopublic = qa_get_public_from_userids($userids);
Scott committed
129

Scott committed
130
	$usershtml = array();
Scott committed
131

Scott committed
132 133 134 135
	foreach ($userids as $userid) {
		$publicusername = $useridtopublic[$userid];

		$usershtml[$userid] = htmlspecialchars($publicusername);
Scott committed
136

Scott committed
137 138 139
		if ($should_include_link)
			$usershtml[$userid] = '<a href="' . qa_path_html('user/' . $publicusername) . '" class="qa-user-link">' . $usershtml[$userid] . '</a>';
	}
Scott committed
140

Scott committed
141 142
	return $usershtml;
}
Scott committed
143 144


Scott committed
145 146 147
function qa_avatar_html_from_userid($userid, $size, $padding)
{
	require_once QA_INCLUDE_DIR . 'app/format.php';
Scott committed
148

Scott committed
149
	global $qa_cache_wp_user_emails;
Scott committed
150

Scott committed
151 152
	if (isset($qa_cache_wp_user_emails[$userid]))
		return qa_get_gravatar_html($qa_cache_wp_user_emails[$userid], $size);
Scott committed
153

Scott committed
154 155
	return null;
}
Scott committed
156 157


Scott committed
158 159 160
function qa_user_report_action($userid, $action)
{
}