PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/qa-include/util/external-users-wp.php

http://github.com/q2a/question2answer
PHP | 160 lines | 101 code | 40 blank | 19 comment | 9 complexity | 542a0919ed45a0dbc0afda8a1d8963eb MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. Question2Answer by Gideon Greenspan and contributors
  4. http://www.question2answer.org/
  5. Description: External user functions for WordPress integration
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. More about this license: http://www.question2answer.org/license.php
  15. */
  16. if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
  17. header('Location: ../../');
  18. exit;
  19. }
  20. function qa_get_mysql_user_column_type()
  21. {
  22. return 'BIGINT UNSIGNED';
  23. }
  24. function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
  25. {
  26. return array(
  27. 'login' => wp_login_url(qa_opt('site_url') . $redirect_back_to_url),
  28. 'register' => function_exists('wp_registration_url') ? wp_registration_url() : site_url('wp-login.php?action=register'),
  29. 'logout' => strtr(wp_logout_url(), array('&amp;' => '&')),
  30. );
  31. }
  32. function qa_get_logged_in_user()
  33. {
  34. $wordpressuser = wp_get_current_user();
  35. if ($wordpressuser->ID == 0)
  36. return null;
  37. else {
  38. if (current_user_can('administrator'))
  39. $level = QA_USER_LEVEL_ADMIN;
  40. elseif (current_user_can('editor'))
  41. $level = QA_USER_LEVEL_EDITOR;
  42. elseif (current_user_can('contributor'))
  43. $level = QA_USER_LEVEL_EXPERT;
  44. else
  45. $level = QA_USER_LEVEL_BASIC;
  46. return array(
  47. 'userid' => $wordpressuser->ID,
  48. 'publicusername' => $wordpressuser->user_nicename,
  49. 'email' => $wordpressuser->user_email,
  50. 'level' => $level,
  51. );
  52. }
  53. }
  54. function qa_get_user_email($userid)
  55. {
  56. $user = get_userdata($userid);
  57. return @$user->user_email;
  58. }
  59. function qa_get_userids_from_public($publicusernames)
  60. {
  61. global $wpdb;
  62. if (count($publicusernames))
  63. return qa_db_read_all_assoc(qa_db_query_sub(
  64. 'SELECT user_nicename, ID FROM ' . $wpdb->base_prefix . 'users WHERE user_nicename IN ($)',
  65. $publicusernames
  66. ), 'user_nicename', 'ID');
  67. else
  68. return array();
  69. }
  70. function qa_get_public_from_userids($userids)
  71. {
  72. global $wpdb, $qa_cache_wp_user_emails;
  73. if (count($userids)) {
  74. $useridtopublic = array();
  75. $qa_cache_wp_user_emails = array();
  76. $userfields = qa_db_read_all_assoc(qa_db_query_sub(
  77. 'SELECT ID, user_nicename, user_email FROM ' . $wpdb->base_prefix . 'users WHERE ID IN (#)',
  78. $userids
  79. ), 'ID');
  80. foreach ($userfields as $id => $fields) {
  81. $useridtopublic[$id] = $fields['user_nicename'];
  82. $qa_cache_wp_user_emails[$id] = $fields['user_email'];
  83. }
  84. return $useridtopublic;
  85. } else
  86. return array();
  87. }
  88. function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
  89. {
  90. $publicusername = $logged_in_user['publicusername'];
  91. return '<a href="' . qa_path_html('user/' . $publicusername) . '" class="qa-user-link">' . htmlspecialchars($publicusername) . '</a>';
  92. }
  93. function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
  94. {
  95. $useridtopublic = qa_get_public_from_userids($userids);
  96. $usershtml = array();
  97. foreach ($userids as $userid) {
  98. $publicusername = $useridtopublic[$userid];
  99. $usershtml[$userid] = htmlspecialchars($publicusername);
  100. if ($should_include_link)
  101. $usershtml[$userid] = '<a href="' . qa_path_html('user/' . $publicusername) . '" class="qa-user-link">' . $usershtml[$userid] . '</a>';
  102. }
  103. return $usershtml;
  104. }
  105. function qa_avatar_html_from_userid($userid, $size, $padding)
  106. {
  107. require_once QA_INCLUDE_DIR . 'app/format.php';
  108. global $qa_cache_wp_user_emails;
  109. if (isset($qa_cache_wp_user_emails[$userid]))
  110. return qa_get_gravatar_html($qa_cache_wp_user_emails[$userid], $size);
  111. return null;
  112. }
  113. function qa_user_report_action($userid, $action)
  114. {
  115. }