/wordpress_consumer/xenforo-api-consumer/includes/dashboard/options.php

https://gitlab.com/billyprice1/bdApi · PHP · 215 lines · 179 code · 29 blank · 7 comment · 28 complexity · ff1b3a004c41cb77facc2e079dd8de57 MD5 · raw file

  1. <?php
  2. // Exit if accessed directly
  3. if (!defined('ABSPATH')) {
  4. exit();
  5. }
  6. function xfac_options_init()
  7. {
  8. if (!empty($_REQUEST['do'])) {
  9. switch ($_REQUEST['do']) {
  10. case 'xfac_xf_guest_account':
  11. require(xfac_template_locateTemplate('dashboard_xfac_xf_guest_account.php'));
  12. return;
  13. }
  14. }
  15. // prepare common data
  16. $config = xfac_option_getConfig();
  17. $meta = array();
  18. if (!empty($config)) {
  19. $meta = xfac_option_getMeta($config);
  20. }
  21. $currentWpUser = wp_get_current_user();
  22. $currentWpUserRecords = xfac_user_getRecordsByUserId($currentWpUser->ID);
  23. $adminAccessToken = xfac_user_getAdminAccessToken($config);
  24. // setup sections
  25. $sections = array(array(
  26. 'id' => 'xfac_api',
  27. 'title' => __('API Configuration', 'xenforo-api-consumer'),
  28. ));
  29. if (!empty($meta['linkIndex'])) {
  30. $sections = array_merge($sections, array(
  31. array(
  32. 'id' => 'xfac_post_comment',
  33. 'title' => __('Post & Comment', 'xenforo-api-consumer'),
  34. ),
  35. array(
  36. 'id' => 'xfac_user_role',
  37. 'title' => __('User & Role', 'xenforo-api-consumer'),
  38. ),
  39. array(
  40. 'id' => 'xfac_ui',
  41. 'title' => __('Appearances', 'xenforo-api-consumer'),
  42. ),
  43. ));
  44. }
  45. // always show advanced sections
  46. $sections[] = array(
  47. 'id' => 'xfac_advanced',
  48. 'title' => __('Advanced', 'xenforo-api-consumer'),
  49. );
  50. // setup tabs
  51. $tab = 'xfac_api';
  52. if (!empty($_REQUEST['tab'])) {
  53. $tab = 'xfac_' . $_GET['tab'];
  54. }
  55. $sectionFound = false;
  56. foreach ($sections as $section) {
  57. if ($section['id'] === $tab) {
  58. $sectionFound = true;
  59. }
  60. }
  61. if (!$sectionFound) {
  62. $firstSection = reset($sections);
  63. $tab = $firstSection['id'];
  64. }
  65. // prepare section's data
  66. switch ($tab) {
  67. case 'xfac_api':
  68. $xfGuestRecords = xfac_user_getRecordsByUserId(0);
  69. $xfAdminRecords = $currentWpUserRecords;
  70. $configuredAdminRecord = null;
  71. $xfAdminAccountOption = intval(get_option('xfac_xf_admin_account'));
  72. if ($xfAdminAccountOption > 0) {
  73. $configuredAdminRecord = xfac_user_getRecordById($xfAdminAccountOption);
  74. if (!empty($configuredAdminRecord)) {
  75. $found = false;
  76. foreach ($xfAdminRecords as $xfAdminRecord) {
  77. if ($xfAdminRecord->id == $configuredAdminRecord->id) {
  78. $found = true;
  79. }
  80. }
  81. if (!$found) {
  82. $xfAdminRecords[] = $configuredAdminRecord;
  83. }
  84. }
  85. }
  86. break;
  87. case 'xfac_post_comment':
  88. $hourlyNext = wp_next_scheduled('xfac_cron_hourly');
  89. $tagForumMappings = get_option('xfac_tag_forum_mappings');
  90. if (!is_array($tagForumMappings)) {
  91. $tagForumMappings = array();
  92. }
  93. $tags = get_terms('post_tag', array('hide_empty' => false));
  94. break;
  95. case 'xfac_user_role':
  96. $syncRoleOption = get_option('xfac_sync_role');
  97. if (!is_array($syncRoleOption)) {
  98. $syncRoleOption = array();
  99. }
  100. break;
  101. case 'xfac_ui':
  102. $optionTopBarForums = get_option('xfac_top_bar_forums');
  103. if (!is_array($optionTopBarForums)) {
  104. $optionTopBarForums = array();
  105. }
  106. break;
  107. }
  108. require(xfac_template_locateTemplate('dashboard_options.php'));
  109. }
  110. function xfac_wpmu_options()
  111. {
  112. $config = xfac_option_getConfig();
  113. $meta = xfac_option_getMeta($config);
  114. require(xfac_template_locateTemplate('dashboard_wpmu_options.php'));
  115. }
  116. add_action('wpmu_options', 'xfac_wpmu_options');
  117. function xfac_update_wpmu_options()
  118. {
  119. $options = array(
  120. 'xfac_root',
  121. 'xfac_client_id',
  122. 'xfac_client_secret',
  123. );
  124. foreach ($options as $optionName) {
  125. if (!isset($_POST[$optionName])) {
  126. continue;
  127. }
  128. $optionValue = wp_unslash($_POST[$optionName]);
  129. update_site_option($optionName, $optionValue);
  130. }
  131. }
  132. add_action('update_wpmu_options', 'xfac_update_wpmu_options');
  133. function xfac_dashboardOptions_admin_init()
  134. {
  135. if (empty($_REQUEST['page'])) {
  136. return;
  137. }
  138. if ($_REQUEST['page'] !== 'xfac') {
  139. return;
  140. }
  141. if (!empty($_REQUEST['cron'])) {
  142. switch ($_REQUEST['cron']) {
  143. case 'hourly':
  144. do_action('xfac_cron_hourly');
  145. wp_redirect(admin_url('options-general.php?page=xfac&ran=hourly'));
  146. exit;
  147. }
  148. } elseif (!empty($_REQUEST['do'])) {
  149. switch ($_REQUEST['do']) {
  150. case 'xfac_meta':
  151. update_option('xfac_meta', array());
  152. wp_redirect(admin_url('options-general.php?page=xfac&done=xfac_meta'));
  153. break;
  154. case 'xfac_xf_guest_account_submit':
  155. $config = xfac_option_getConfig();
  156. if (empty($config)) {
  157. wp_die('no_config');
  158. }
  159. $username = $_REQUEST['xfac_guest_username'];
  160. if (empty($username)) {
  161. wp_die('no_username');
  162. }
  163. $password = $_REQUEST['xfac_guest_password'];
  164. if (empty($password)) {
  165. wp_die('no_password');
  166. }
  167. $token = xfac_api_getAccessTokenFromUsernamePassword($config, $username, $password);
  168. if (empty($token)) {
  169. wp_die('no_token');
  170. }
  171. $guest = xfac_api_getUsersMe($config, $token['access_token'], false);
  172. if (empty($guest['user'])) {
  173. wp_die('no_xf_user');
  174. }
  175. xfac_user_updateRecord(0, $config['root'], $guest['user']['user_id'], $guest['user'], $token);
  176. $records = xfac_user_getRecordsByUserId(0);
  177. $record = reset($records);
  178. update_option('xfac_xf_guest_account', $record->id);
  179. // force meta rebuild
  180. update_option('xfac_meta', array());
  181. wp_redirect(admin_url('options-general.php?page=xfac&done=xfac_xf_guest_account'));
  182. break;
  183. }
  184. }
  185. }
  186. add_action('admin_init', 'xfac_dashboardOptions_admin_init');