Advertisement
Guest User

Untitled

a guest
Nov 9th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. function butc_categorySavePre($in) {
  2. $cat = butc_getCategory();
  3. if ($cat) {
  4. return array($cat);
  5. }
  6. return $in;
  7. }
  8.  
  9. function butc_getCategory() {
  10. global $user_ID;
  11. $opts = get_option("bindusertocat");
  12. $keys = array_keys($opts);
  13. $sid = (string)$user_ID;
  14. if (in_array($sid, $keys)) {
  15. return $opts[$sid];
  16. }
  17. return false;
  18. }
  19.  
  20. function butc_removeCategorySelection($page) {
  21. return preg_replace('#<fieldset id="categorydiv".*?</fieldset>#sim', '', $page);
  22. }
  23.  
  24. function butc_adminHead($in) {
  25. global $user_level;
  26. get_currentuserinfo();
  27. $cat = butc_getCategory();
  28. if ($cat && $user_level < 10) {
  29. if(
  30. preg_match('#/wp-admin/post\.php#', $_SERVER['REQUEST_URI'])
  31. || preg_match('#/wp-admin/post-new\.php#', $_SERVER['REQUEST_URI'])
  32. ) {
  33. ob_start(butc_removeCategorySelection);
  34. }
  35. }
  36. return $in;
  37. }
  38.  
  39. function butc_menu() {
  40. add_management_page(__('Bind user to category'),
  41. __('Bind user to category'),
  42. 10, basename(__FILE__), "butc_form");
  43.  
  44. }
  45.  
  46. function butc_form() {
  47. global $wpdb;
  48. if (isset($_POST['info_update'])) {
  49. $updated = butc_saveForm($_POST);
  50. if ($updated) {
  51. echo '<div class="updated"><p><strong>' . __('Binding successful.', 'bindusertocat') .'</strong></p></div>';
  52. } else {
  53. echo '<div class="error"><p><strong>' . __('Error while saving binding.', 'bindusertocat') .'</strong></p></div>';
  54. }
  55. }
  56. echo '<div class="wrap"><form method="post" action="">';
  57. echo '<h2>Bind user to cat settings</h2>';
  58. $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;");
  59. $users = array();
  60. foreach ($userids as $userid) {
  61. $tmp_user = new WP_User($userid);
  62. if ($tmp_user->wp_user_level > 7) continue;
  63. $users[$userid] = $tmp_user;
  64. }
  65.  
  66. $wp23 = butc_wp23orbetter();
  67.  
  68. if ($wp23) {
  69. $cats = $wpdb->get_results("SELECT * FROM $wpdb->terms JOIN $wpdb->term_taxonomy USING (term_id) WHERE taxonomy='category' ORDER BY name");
  70. }
  71. else {
  72. $cats = $wpdb->get_results("SELECT * FROM $wpdb->categories ORDER BY cat_name");
  73. }
  74.  
  75. $opts = get_option("bindusertocat");
  76.  
  77. $t = "<tr><td>%s</td><td>%s</td></tr>";
  78.  
  79. echo "<table id='bindusertocat'>";
  80.  
  81. $field = $wp23 ? 'term_id' : 'cat_ID';
  82. $name = $wp23 ? 'name' : 'cat_name';
  83.  
  84. foreach ($opts as $k => $v) {
  85. printf($t, butc_select('user[]', $users, 'ID', 'user_login', $k), butc_select('cat[]', $cats, $field, $name, $v));
  86. }
  87.  
  88. printf($t, butc_select('user[]', $users, 'ID', 'user_login'), butc_select('cat[]', $cats, $field, $name));
  89.  
  90. echo "</table>";
  91.  
  92. echo '<div class="submit"><input type="submit" name="info_update" value="' . __('Update settings', 'bindusertocat') . '" /></div></form></div>';
  93.  
  94. }
  95.  
  96. function butc_select($n, $a = array(), $v, $t, $s = '') {
  97. $h = '<select name="' . $n . '">';
  98. $h .= '<option value=""' . ($s === "" ? ' selected="selected"' : '') . '> -- </option>';
  99. foreach ($a as $it) {
  100. $h .= '<option value="' . $it->$v . '"' . ($it->$v == $s ? ' selected="selected"' : '') . '>' . $it->$t . '</option>';
  101. }
  102. $h .= '</select>';
  103. return $h;
  104. }
  105.  
  106. function butc_saveForm() {
  107. $len = count($_POST["user"]);
  108. $opts = array();
  109. for ($i = 0; $i < $len; $i++) {
  110. if ($_POST["user"][$i] && $_POST["cat"][$i]) {
  111. $opts[$_POST["user"][$i]] = (int)$_POST["cat"][$i];
  112. }
  113. }
  114. update_option("bindusertocat", $opts);
  115. return true;
  116. }
  117.  
  118.  
  119. function butc_wp23orbetter() {
  120. static $ret = null;
  121. if (isset($ret)) {
  122. return $ret;
  123. }
  124. $version = get_bloginfo('version');
  125. $parts = explode('.', $version);
  126. if ((int)$parts[0] > 2) {
  127. $ret = true;
  128. return $ret;
  129. }
  130. if ((int)$parts[0] == 2) {
  131. $ret = ((int)$parts[1] >= 3);
  132. return $ret;
  133. }
  134. $ret = false;
  135. return $ret;
  136. }
  137.  
  138. add_option("bindusertocat", array(), "", false);
  139. add_action('admin_menu', "butc_menu");
  140. add_filter("category_save_pre", "butc_categorySavePre");
  141. add_action("admin_head", "butc_adminHead");
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. *********************************************
  150. *********************************************
  151. JS File:
  152. *********************************************
  153. *********************************************
  154.  
  155.  
  156.  
  157.  
  158. [CODE](function() {
  159. function init() {
  160. var t = document.getElementById("bindusertocat");
  161. var d = document.createElement("div");
  162. d.id = "addRow";
  163. var b = document.createElement("input");
  164. b.type = "button";
  165. b.value = "+";
  166. t.parentNode.appendChild(d).appendChild(b);
  167. addDeleteButtons(t);
  168. b.onclick = addRow;
  169. //document.createElement("input").type
  170. }
  171.  
  172. function addRow() {
  173. var t = document.getElementById("bindusertocat");
  174. var r = t.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
  175. var clone = r[r.length - 1].cloneNode(true);
  176. var s = clone.getElementsByTagName("select");
  177. for (var i = 0; i < s.length; i++) {
  178. s[i].selectedIndex = 0;
  179. }
  180. assignDeleteEvent(clone);
  181. t.getElementsByTagName("tbody")[0].appendChild(clone);
  182. }
  183.  
  184. function addDeleteButtons(t) {
  185. var r = t.getElementsByTagName("tr");
  186. var td = document.createElement("td");
  187. var b = document.createElement("input");
  188. b.type = "button";
  189. b.value = "-";
  190. var it;
  191. td.appendChild(b);
  192. for (var i = 0, el; (el = r[i]); i++) {
  193. if (el.parentNode.tagName.toLowerCase() == "tbody") {
  194. el.appendChild(td.cloneNode(true));
  195. assignDeleteEvent(el);
  196. } else {
  197. el.appendChild(document.createElement("td"));
  198. }
  199. }
  200. }
  201.  
  202. function deleteRow() {
  203. if (this.parentNode.parentNode.parentNode.getElementsByTagName("tr").length > 1) {
  204. this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
  205. } else {
  206. var s = this.parentNode.parentNode.getElementsByTagName("select");
  207. for (var i = 0; i < s.length; i++) {
  208. s[i].selectedIndex = 0;
  209. }
  210. }
  211. }
  212.  
  213. function assignDeleteEvent(el) {
  214. var it = el.getElementsByTagName("input")[0];
  215. it.onclick = deleteRow;
  216. }
  217.  
  218.  
  219. addLoadEvent(init);
  220.  
  221. })();
  222.  
  223.  
  224.  
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement