/admin/wp-admin2/admin-db.php

https://github.com/itspriddle/itt-capstone · PHP · 352 lines · 245 code · 95 blank · 12 comment · 50 complexity · 7c88c81dfe1aa5556c2e0b95f54ae6b5 MD5 · raw file

  1. <?php
  2. function get_users_drafts( $user_id ) {
  3. global $wpdb;
  4. $user_id = (int) $user_id;
  5. $query = "SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author = $user_id ORDER BY ID DESC";
  6. $query = apply_filters('get_users_drafts', $query);
  7. return $wpdb->get_results( $query );
  8. }
  9. function get_others_drafts( $user_id ) {
  10. global $wpdb;
  11. $user = get_userdata( $user_id );
  12. $level_key = $wpdb->prefix . 'user_level';
  13. $editable = get_editable_user_ids( $user_id );
  14. if( !$editable ) {
  15. $other_drafts = '';
  16. } else {
  17. $editable = join(',', $editable);
  18. $other_drafts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'draft' AND post_author IN ($editable) AND post_author != '$user_id' ");
  19. }
  20. return apply_filters('get_others_drafts', $other_drafts);
  21. }
  22. function get_editable_authors( $user_id ) {
  23. global $wpdb;
  24. $editable = get_editable_user_ids( $user_id );
  25. if( !$editable ) {
  26. return false;
  27. } else {
  28. $editable = join(',', $editable);
  29. $authors = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($editable)" );
  30. }
  31. return apply_filters('get_editable_authors', $authors);
  32. }
  33. function get_editable_user_ids( $user_id, $exclude_zeros = true ) {
  34. global $wpdb;
  35. $user = new WP_User( $user_id );
  36. if ( ! $user->has_cap('edit_others_posts') ) {
  37. if ( $user->has_cap('edit_posts') || $exclude_zeros == false )
  38. return array($user->id);
  39. else
  40. return false;
  41. }
  42. $level_key = $wpdb->prefix . 'user_level';
  43. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key'";
  44. if ( $exclude_zeros )
  45. $query .= " AND meta_value != '0'";
  46. return $wpdb->get_col( $query );
  47. }
  48. function get_author_user_ids() {
  49. global $wpdb;
  50. $level_key = $wpdb->prefix . 'user_level';
  51. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value != '0'";
  52. return $wpdb->get_col( $query );
  53. }
  54. function get_nonauthor_user_ids() {
  55. global $wpdb;
  56. $level_key = $wpdb->prefix . 'user_level';
  57. $query = "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '$level_key' AND meta_value = '0'";
  58. return $wpdb->get_col( $query );
  59. }
  60. function wp_insert_category($catarr) {
  61. global $wpdb;
  62. extract($catarr);
  63. $cat_ID = (int) $cat_ID;
  64. // Are we updating or creating?
  65. if (!empty ($cat_ID))
  66. $update = true;
  67. else
  68. $update = false;
  69. $cat_name = apply_filters('pre_category_name', $cat_name);
  70. if (empty ($category_nicename))
  71. $category_nicename = sanitize_title($cat_name);
  72. else
  73. $category_nicename = sanitize_title($category_nicename);
  74. $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
  75. if (empty ($category_description))
  76. $category_description = '';
  77. $category_description = apply_filters('pre_category_description', $category_description);
  78. $category_parent = (int) $category_parent;
  79. if (empty ($category_parent))
  80. $category_parent = 0;
  81. if (!$update) {
  82. $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent')");
  83. $cat_ID = $wpdb->insert_id;
  84. } else {
  85. $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent' WHERE cat_ID = '$cat_ID'");
  86. }
  87. if ( $category_nicename == '' ) {
  88. $category_nicename = sanitize_title($cat_name, $cat_ID );
  89. $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
  90. }
  91. wp_cache_delete($cat_ID, 'category');
  92. if ($update) {
  93. do_action('edit_category', $cat_ID);
  94. } else {
  95. wp_cache_delete('all_category_ids', 'category');
  96. do_action('create_category', $cat_ID);
  97. do_action('add_category', $cat_ID);
  98. }
  99. return $cat_ID;
  100. }
  101. function wp_update_category($catarr) {
  102. global $wpdb;
  103. $cat_ID = (int) $catarr['cat_ID'];
  104. // First, get all of the original fields
  105. $category = get_category($cat_ID, ARRAY_A);
  106. // Escape data pulled from DB.
  107. $category = add_magic_quotes($category);
  108. // Merge old and new fields with new fields overwriting old ones.
  109. $catarr = array_merge($category, $catarr);
  110. return wp_insert_category($catarr);
  111. }
  112. function wp_delete_category($cat_ID) {
  113. global $wpdb;
  114. $cat_ID = (int) $cat_ID;
  115. // Don't delete the default cat.
  116. if (1 == $cat_ID)
  117. return 0;
  118. $category = get_category($cat_ID);
  119. $parent = $category->category_parent;
  120. // Delete the category.
  121. $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
  122. // Update children to point to new parent.
  123. $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
  124. // TODO: Only set categories to general if they're not in another category already
  125. $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
  126. wp_cache_delete($cat_ID, 'category');
  127. wp_cache_delete('all_category_ids', 'category');
  128. do_action('delete_category', $cat_ID);
  129. return 1;
  130. }
  131. function wp_create_category($cat_name) {
  132. $cat_array = compact('cat_name');
  133. return wp_insert_category($cat_array);
  134. }
  135. function wp_create_categories($categories, $post_id = '') {
  136. $cat_ids = array ();
  137. foreach ($categories as $category) {
  138. if ($id = category_exists($category))
  139. $cat_ids[] = $id;
  140. else
  141. if ($id = wp_create_category($category))
  142. $cat_ids[] = $id;
  143. }
  144. if ($post_id)
  145. wp_set_post_cats('', $post_id, $cat_ids);
  146. return $cat_ids;
  147. }
  148. function category_exists($cat_name) {
  149. global $wpdb;
  150. if (!$category_nicename = sanitize_title($cat_name))
  151. return 0;
  152. return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
  153. }
  154. function wp_delete_user($id, $reassign = 'novalue') {
  155. global $wpdb;
  156. $id = (int) $id;
  157. $user = get_userdata($id);
  158. if ($reassign == 'novalue') {
  159. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  160. if ($post_ids) {
  161. foreach ($post_ids as $post_id)
  162. wp_delete_post($post_id);
  163. }
  164. // Clean links
  165. $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  166. } else {
  167. $reassign = (int) $reassign;
  168. $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
  169. $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
  170. }
  171. // FINALLY, delete user
  172. $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  173. $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
  174. wp_cache_delete($id, 'users');
  175. wp_cache_delete($user->user_login, 'userlogins');
  176. do_action('delete_user', $id);
  177. return true;
  178. }
  179. function get_link($link_id, $output = OBJECT) {
  180. global $wpdb;
  181. $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
  182. if ( $output == OBJECT ) {
  183. return $link;
  184. } elseif ( $output == ARRAY_A ) {
  185. return get_object_vars($link);
  186. } elseif ( $output == ARRAY_N ) {
  187. return array_values(get_object_vars($link));
  188. } else {
  189. return $link;
  190. }
  191. }
  192. function wp_insert_link($linkdata) {
  193. global $wpdb, $current_user;
  194. extract($linkdata);
  195. $update = false;
  196. if ( !empty($link_id) )
  197. $update = true;
  198. if ( empty($link_rating) )
  199. $link_rating = 0;
  200. if ( empty($link_target) )
  201. $link_target = '';
  202. if ( empty($link_visible) )
  203. $link_visible = 'Y';
  204. if ( empty($link_owner) )
  205. $link_owner = $current_user->id;
  206. if ( empty($link_notes) )
  207. $link_notes = '';
  208. if ( $update ) {
  209. $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
  210. link_name='$link_name', link_image='$link_image',
  211. link_target='$link_target', link_category='$link_category',
  212. link_visible='$link_visible', link_description='$link_description',
  213. link_rating='$link_rating', link_rel='$link_rel',
  214. link_notes='$link_notes', link_rss = '$link_rss'
  215. WHERE link_id='$link_id'");
  216. } else {
  217. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_category, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_category', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
  218. $link_id = $wpdb->insert_id;
  219. }
  220. if ( $update )
  221. do_action('edit_link', $link_id);
  222. else
  223. do_action('add_link', $link_id);
  224. return $link_id;
  225. }
  226. function wp_update_link($linkdata) {
  227. global $wpdb;
  228. $link_id = (int) $linkdata['link_id'];
  229. $link = get_link($link_id, ARRAY_A);
  230. // Escape data pulled from DB.
  231. $link = add_magic_quotes($link);
  232. // Merge old and new fields with new fields overwriting old ones.
  233. $linkdata = array_merge($link, $linkdata);
  234. return wp_insert_link($linkdata);
  235. }
  236. function wp_delete_link($link_id) {
  237. global $wpdb;
  238. do_action('delete_link', $link_id);
  239. return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
  240. }
  241. function post_exists($title, $content = '', $post_date = '') {
  242. global $wpdb;
  243. if (!empty ($post_date))
  244. $post_date = "AND post_date = '$post_date'";
  245. if (!empty ($title))
  246. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
  247. else
  248. if (!empty ($content))
  249. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
  250. return 0;
  251. }
  252. function comment_exists($comment_author, $comment_date) {
  253. global $wpdb;
  254. return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
  255. WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
  256. }
  257. ?>