qa-external-users-wp.php 4.15 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-external-users-wp.php
	Version: See define()s at top of qa-include/qa-base.php
	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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}


	function qa_get_mysql_user_column_type()
	{
		return 'BIGINT UNSIGNED';
	}


	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' => site_url('wp-login.php?action=register'),
			'logout' => strtr(wp_logout_url(), array('&amp;' => '&')),
		);
	}
Scott Vivian committed
47

Gideon Greenspan committed
48 49 50 51

	function qa_get_logged_in_user()
	{
		$wordpressuser=wp_get_current_user();
Scott Vivian committed
52

Gideon Greenspan committed
53 54 55 56 57 58 59 60 61 62 63 64
		if ($wordpressuser->ID==0)
			return null;

		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 Vivian committed
65

Gideon Greenspan committed
66 67 68 69 70 71 72 73 74
			return array(
				'userid' => $wordpressuser->ID,
				'publicusername' => $wordpressuser->user_nicename,
				'email' => $wordpressuser->user_email,
				'level' => $level,
			);
		}
	}

Scott Vivian committed
75

Gideon Greenspan committed
76 77 78
	function qa_get_user_email($userid)
	{
		$user=get_userdata($userid);
Scott Vivian committed
79

Gideon Greenspan committed
80 81
		return @$user->user_email;
	}
Scott Vivian committed
82

Gideon Greenspan committed
83 84 85 86

	function qa_get_userids_from_public($publicusernames)
	{
		global $wpdb;
Scott Vivian committed
87

Gideon Greenspan committed
88 89 90 91 92 93 94 95 96 97 98 99
		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();
	}


	function qa_get_public_from_userids($userids)
	{
Gideon Greenspan committed
100
		global $wpdb, $qa_cache_wp_user_emails;
Scott Vivian committed
101

Gideon Greenspan committed
102 103 104
		if (count($userids)) {
			$useridtopublic=array();
			$qa_cache_wp_user_emails=array();
Scott Vivian committed
105

Gideon Greenspan committed
106 107
			$userfields=qa_db_read_all_assoc(qa_db_query_sub(
				'SELECT ID, user_nicename, user_email FROM '.$wpdb->base_prefix.'users WHERE ID IN (#)',
Gideon Greenspan committed
108
				$userids
Gideon Greenspan committed
109
			), 'ID');
Scott Vivian committed
110

Gideon Greenspan committed
111 112 113 114
			foreach ($userfields as $id => $fields) {
				$useridtopublic[$id]=$fields['user_nicename'];
				$qa_cache_wp_user_emails[$id]=$fields['user_email'];
			}
Scott Vivian committed
115

Gideon Greenspan committed
116 117 118
			return $useridtopublic;

		} else
Gideon Greenspan committed
119 120 121 122 123 124 125
			return array();
	}


	function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
	{
		$publicusername=$logged_in_user['publicusername'];
Scott Vivian committed
126

Gideon Greenspan committed
127
		return '<a href="'.qa_path_html('user/'.$publicusername).'" class="qa-user-link">'.htmlspecialchars($publicusername).'</a>';
Gideon Greenspan committed
128 129 130 131 132 133
	}


	function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
	{
		$useridtopublic=qa_get_public_from_userids($userids);
Scott Vivian committed
134

Gideon Greenspan committed
135 136 137 138
		$usershtml=array();

		foreach ($userids as $userid) {
			$publicusername=$useridtopublic[$userid];
Scott Vivian committed
139

Gideon Greenspan committed
140
			$usershtml[$userid]=htmlspecialchars($publicusername);
Scott Vivian committed
141

Gideon Greenspan committed
142
			if ($should_include_link)
Gideon Greenspan committed
143
				$usershtml[$userid]='<a href="'.qa_path_html('user/'.$publicusername).'" class="qa-user-link">'.$usershtml[$userid].'</a>';
Gideon Greenspan committed
144
		}
Scott Vivian committed
145

Gideon Greenspan committed
146 147
		return $usershtml;
	}
Scott Vivian committed
148 149


Gideon Greenspan committed
150 151 152
	function qa_avatar_html_from_userid($userid, $size, $padding)
	{
		require_once QA_INCLUDE_DIR.'qa-app-format.php';
Scott Vivian committed
153

Gideon Greenspan committed
154
		global $qa_cache_wp_user_emails;
Scott Vivian committed
155

Gideon Greenspan committed
156 157
		if (isset($qa_cache_wp_user_emails[$userid]))
			return qa_get_gravatar_html($qa_cache_wp_user_emails[$userid], $size);
Scott Vivian committed
158

Gideon Greenspan committed
159 160
		return null;
	}
Gideon Greenspan committed
161 162 163 164 165 166 167 168 169 170


	function qa_user_report_action($userid, $action)
	{
	}


/*
	Omit PHP closing tag to help avoid accidental output
*/