/wordpress_consumer/xenforo-api-consumer/includes/ui/top_bar.php

https://gitlab.com/billyprice1/bdApi · PHP · 278 lines · 214 code · 46 blank · 18 comment · 34 complexity · 136b9b173b51f14569e663771cbd0fec MD5 · raw file

  1. <?php
  2. // Exit if accessed directly
  3. if (!defined('ABSPATH')) {
  4. exit();
  5. }
  6. function xfac_admin_bar_forums_menu($wpAdminBar)
  7. {
  8. if (is_admin()) {
  9. // don't add menu in Dashboard
  10. return;
  11. }
  12. $config = xfac_option_getConfig();
  13. if (empty($config)) {
  14. // don't add menu for site without configuration
  15. return;
  16. }
  17. $optionTopBarForums = get_option('xfac_top_bar_forums');
  18. if (!is_array($optionTopBarForums)) {
  19. $optionTopBarForums = array();
  20. }
  21. $meta = xfac_option_getMeta($config);
  22. if (in_array(0, $optionTopBarForums)) {
  23. $wpAdminBar->add_menu(array(
  24. 'id' => 'xfac-forums',
  25. 'title' => __('Forums', 'xenforo-api-consumer'),
  26. 'href' => $meta['linkIndex'],
  27. ));
  28. foreach ($optionTopBarForums as $forumId) {
  29. $forum = false;
  30. if (empty($meta['forums'])) {
  31. continue;
  32. }
  33. foreach ($meta['forums'] as $_forum) {
  34. if ($_forum['forum_id'] == $forumId) {
  35. $forum = $_forum;
  36. }
  37. }
  38. if (empty($forum)) {
  39. continue;
  40. }
  41. $wpAdminBar->add_menu(array(
  42. 'parent' => 'xfac-forums',
  43. 'id' => 'xfac-forum-' . $forum['forum_id'],
  44. 'title' => $forum['forum_title'],
  45. 'href' => $forum['links']['permalink'],
  46. ));
  47. }
  48. }
  49. }
  50. function xfac_admin_bar_jscount_menu($wpAdminBar)
  51. {
  52. if (is_admin()) {
  53. // don't add menu in Dashboard
  54. return;
  55. }
  56. $wpUser = wp_get_current_user();
  57. if (empty($wpUser->ID)) {
  58. // don't add menu for guests
  59. return;
  60. }
  61. $config = xfac_option_getConfig();
  62. if (empty($config)) {
  63. // don't add menu for site without configuration
  64. return;
  65. }
  66. $records = xfac_user_getRecordsByUserId($wpUser->ID);
  67. if (empty($records)) {
  68. // don't add menu for not-connected users
  69. return;
  70. }
  71. $record = reset($records);
  72. $meta = xfac_option_getMeta($config);
  73. $doNotifications = (!!get_option('xfac_top_bar_notifications') AND !empty($meta['linkAlerts']));
  74. $doConversations = (!!get_option('xfac_top_bar_conversations') AND !empty($meta['linkConversations']));
  75. $script = 'window.xfacClientId = ' . json_encode($config['clientId']) . ';';
  76. $script .= 'window.xfacXenForoUserId = ' . json_encode(intval($record->identifier)) . ';';
  77. $script .= 'window.xfacDoNotifications = ' . json_encode($doNotifications ? 1 : 0) . ';';
  78. $script .= 'window.xfacDoConversations = ' . json_encode($doConversations ? 1 : 0) . ';';
  79. $html = sprintf('<script>%s</script>', $script);
  80. if ($doNotifications) {
  81. $notificationsTitle = __('Alerts', 'xenforo-api-consumer');
  82. if (!isset($_COOKIE['notificationCount'])) {
  83. $notificationsTitle .= ' <span id="xfacNotificationCount" class="xfacJsCount"></span>';
  84. } else {
  85. $notificationsTitle .= call_user_func_array('sprintf', array(
  86. ' <span id="xfacNotificationCount" class="xfacJsCount updated%s">%d</span>',
  87. $_COOKIE['notificationCount'] > 0 ? ' unread' : '',
  88. $_COOKIE['notificationCount'],
  89. ));
  90. }
  91. $wpAdminBar->add_menu(array(
  92. 'id' => 'xfac-notifications',
  93. 'title' => $notificationsTitle,
  94. 'parent' => (!!get_option('xfac_top_bar_replace') ? 'top-secondary' : ''),
  95. 'href' => $meta['linkAlerts'],
  96. 'meta' => array('html' => $html),
  97. ));
  98. // reset html
  99. $html = '';
  100. }
  101. if ($doConversations) {
  102. $conversationTitle = __('Conversations', 'xenforo-api-consumer');
  103. if (!isset($_COOKIE['conversationCount'])) {
  104. $conversationTitle .= ' <span id="xfacConversationCount" class="xfacJsCount"></span>';
  105. } else {
  106. $conversationTitle .= call_user_func_array('sprintf', array(
  107. ' <span id="xfacConversationCount" class="xfacJsCount updated%s">%d</span>',
  108. $_COOKIE['conversationCount'] > 0 ? ' unread' : '',
  109. $_COOKIE['conversationCount'],
  110. ));
  111. }
  112. $wpAdminBar->add_menu(array(
  113. 'id' => 'xfac-conversations',
  114. 'title' => $conversationTitle,
  115. 'parent' => (!!get_option('xfac_top_bar_replace') ? 'top-secondary' : ''),
  116. 'href' => $meta['linkConversations'],
  117. 'meta' => array('html' => $html),
  118. ));
  119. // reset html
  120. $html = '';
  121. }
  122. }
  123. function xfac_admin_bar_login_menu($wpAdminBar)
  124. {
  125. $wpUser = wp_get_current_user();
  126. if ($wpUser->ID > 0) {
  127. // don't add menu for users
  128. return;
  129. }
  130. $config = xfac_option_getConfig();
  131. if (empty($config)) {
  132. // don't add menu for site without configuration
  133. return;
  134. }
  135. $meta = xfac_option_getMeta($config);
  136. if (!empty($meta['linkRegister'])) {
  137. $wpAdminBar->add_menu(array(
  138. 'id' => 'xfac-register',
  139. 'parent' => 'top-secondary',
  140. 'title' => __('Register', 'xenforo-api-consumer'),
  141. 'href' => $meta['linkRegister'],
  142. ));
  143. }
  144. if (!empty($meta['linkLogin'])) {
  145. $wpAdminBar->add_menu(array(
  146. 'id' => 'xfac-login',
  147. 'parent' => 'top-secondary',
  148. 'title' => __('Log in', 'xenforo-api-consumer'),
  149. 'href' => $meta['linkLogin'],
  150. ));
  151. if (!empty($meta['linkLoginLogin'])) {
  152. $loginFormAction = $meta['linkLoginLogin'];
  153. $redirect = site_url('wp-login.php?xfac=1');
  154. ob_start();
  155. require(xfac_template_locateTemplate('top_bar_login_form.php'));
  156. $loginForm = ob_get_clean();
  157. $wpAdminBar->add_menu(array(
  158. 'id' => 'xfac-loginForm',
  159. 'parent' => 'xfac-login',
  160. 'title' => $loginForm,
  161. ));
  162. }
  163. }
  164. }
  165. function xfac_admin_bar_remove_menus($wpAdminBar)
  166. {
  167. $nodes = $wpAdminBar->get_nodes();
  168. $nodeIds = array_keys($nodes);
  169. foreach ($nodeIds as $nodeId) {
  170. if ($nodes[$nodeId]->group) {
  171. // keep groups
  172. continue;
  173. }
  174. if (strpos($nodeId, 'xfac-') === 0) {
  175. // keep ours
  176. continue;
  177. } elseif ($nodeId === 'top-secondary' OR $nodeId === 'my-account' OR $nodes[$nodeId]->parent === 'user-actions') {
  178. // keep user related nodes
  179. continue;
  180. } elseif ($nodeId === 'logo') {
  181. // keep logo
  182. continue;
  183. } else {
  184. // remove others
  185. $wpAdminBar->remove_node($nodeId);
  186. }
  187. }
  188. }
  189. function xfac_add_admin_bar_menus()
  190. {
  191. $config = xfac_option_getConfig();
  192. if (empty($config)) {
  193. // don't add menu for site without configuration
  194. return;
  195. }
  196. add_action('admin_bar_menu', 'xfac_admin_bar_forums_menu', 30);
  197. if (!!get_option('xfac_top_bar_notifications') OR !!get_option('xfac_top_bar_conversations')) {
  198. add_action('admin_bar_menu', 'xfac_admin_bar_jscount_menu', !!get_option('xfac_top_bar_replace') ? 0 : 30);
  199. }
  200. if (!!get_option('xfac_top_bar_replace')) {
  201. wp_enqueue_style('xfac-top_bar.css', XFAC_PLUGIN_URL . '/css/top_bar.css');
  202. add_action('admin_bar_menu', 'xfac_admin_bar_login_menu', 7);
  203. add_action('admin_bar_menu', 'xfac_admin_bar_remove_menus', PHP_INT_MAX - 1);
  204. }
  205. }
  206. add_action('add_admin_bar_menus', 'xfac_add_admin_bar_menus');
  207. function xfac_show_admin_bar($showAdminBar)
  208. {
  209. if (!!get_option('xfac_top_bar_always')) {
  210. return true;
  211. }
  212. return $showAdminBar;
  213. }
  214. add_filter('show_admin_bar', 'xfac_show_admin_bar');
  215. if (!!get_option('xfac_top_bar_notifications') OR !!get_option('xfac_top_bar_conversations')) {
  216. function xfac_topBar_wp_enqueue_scripts()
  217. {
  218. $config = xfac_option_getConfig();
  219. if (empty($config)) {
  220. // do nothing
  221. return;
  222. }
  223. $wpUser = wp_get_current_user();
  224. if ($wpUser->ID > 0) {
  225. wp_enqueue_script('jquery');
  226. wp_enqueue_script('xfac-sdk', xfac_api_getSdkJsUrl($config));
  227. wp_enqueue_script('xfac-script.js', XFAC_PLUGIN_URL . '/js/script.js');
  228. wp_enqueue_style('xfac-top_bar.css', XFAC_PLUGIN_URL . '/css/top_bar.css');
  229. }
  230. }
  231. add_action('wp_enqueue_scripts', 'xfac_topBar_wp_enqueue_scripts');
  232. }