PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.0.1/wp-admin/admin-db.php

#
PHP | 349 lines | 242 code | 95 blank | 12 comment | 50 complexity | 37861b57aee785e6d6339f2b77a1f709 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  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 = wp_specialchars($cat_name);
  70. if (empty ($category_nicename))
  71. $category_nicename = sanitize_title($cat_name);
  72. else
  73. $category_nicename = sanitize_title($category_nicename);
  74. if (empty ($category_description))
  75. $category_description = '';
  76. if (empty ($category_parent))
  77. $category_parent = 0;
  78. if (!$update) {
  79. $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')");
  80. $cat_ID = $wpdb->insert_id;
  81. } else {
  82. $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'");
  83. }
  84. if ( $category_nicename == '' ) {
  85. $category_nicename = sanitize_title($cat_name, $cat_ID );
  86. $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
  87. }
  88. wp_cache_delete($cat_ID, 'category');
  89. if ($update) {
  90. do_action('edit_category', $cat_ID);
  91. } else {
  92. wp_cache_delete('all_category_ids', 'category');
  93. do_action('create_category', $cat_ID);
  94. do_action('add_category', $cat_ID);
  95. }
  96. return $cat_ID;
  97. }
  98. function wp_update_category($catarr) {
  99. global $wpdb;
  100. $cat_ID = (int) $catarr['cat_ID'];
  101. // First, get all of the original fields
  102. $category = get_category($cat_ID, ARRAY_A);
  103. // Escape data pulled from DB.
  104. $category = add_magic_quotes($category);
  105. // Merge old and new fields with new fields overwriting old ones.
  106. $catarr = array_merge($category, $catarr);
  107. return wp_insert_category($catarr);
  108. }
  109. function wp_delete_category($cat_ID) {
  110. global $wpdb;
  111. $cat_ID = (int) $cat_ID;
  112. // Don't delete the default cat.
  113. if (1 == $cat_ID)
  114. return 0;
  115. $category = get_category($cat_ID);
  116. $parent = $category->category_parent;
  117. // Delete the category.
  118. $wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'");
  119. // Update children to point to new parent.
  120. $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
  121. // TODO: Only set categories to general if they're not in another category already
  122. $wpdb->query("UPDATE $wpdb->post2cat SET category_id='1' WHERE category_id='$cat_ID'");
  123. wp_cache_delete($cat_ID, 'category');
  124. wp_cache_delete('all_category_ids', 'category');
  125. do_action('delete_category', $cat_ID);
  126. return 1;
  127. }
  128. function wp_create_category($cat_name) {
  129. $cat_array = compact('cat_name');
  130. return wp_insert_category($cat_array);
  131. }
  132. function wp_create_categories($categories, $post_id = '') {
  133. $cat_ids = array ();
  134. foreach ($categories as $category) {
  135. if ($id = category_exists($category))
  136. $cat_ids[] = $id;
  137. else
  138. if ($id = wp_create_category($category))
  139. $cat_ids[] = $id;
  140. }
  141. if ($post_id)
  142. wp_set_post_cats('', $post_id, $cat_ids);
  143. return $cat_ids;
  144. }
  145. function category_exists($cat_name) {
  146. global $wpdb;
  147. if (!$category_nicename = sanitize_title($cat_name))
  148. return 0;
  149. return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
  150. }
  151. function wp_delete_user($id, $reassign = 'novalue') {
  152. global $wpdb;
  153. $id = (int) $id;
  154. $user = get_userdata($id);
  155. if ($reassign == 'novalue') {
  156. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  157. if ($post_ids) {
  158. foreach ($post_ids as $post_id)
  159. wp_delete_post($post_id);
  160. }
  161. // Clean links
  162. $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  163. } else {
  164. $reassign = (int) $reassign;
  165. $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
  166. $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
  167. }
  168. // FINALLY, delete user
  169. $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  170. $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
  171. wp_cache_delete($id, 'users');
  172. wp_cache_delete($user->user_login, 'userlogins');
  173. do_action('delete_user', $id);
  174. return true;
  175. }
  176. function get_link($link_id, $output = OBJECT) {
  177. global $wpdb;
  178. $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
  179. if ( $output == OBJECT ) {
  180. return $link;
  181. } elseif ( $output == ARRAY_A ) {
  182. return get_object_vars($link);
  183. } elseif ( $output == ARRAY_N ) {
  184. return array_values(get_object_vars($link));
  185. } else {
  186. return $link;
  187. }
  188. }
  189. function wp_insert_link($linkdata) {
  190. global $wpdb, $current_user;
  191. extract($linkdata);
  192. $update = false;
  193. if ( !empty($link_id) )
  194. $update = true;
  195. if ( empty($link_rating) )
  196. $link_rating = 0;
  197. if ( empty($link_target) )
  198. $link_target = '';
  199. if ( empty($link_visible) )
  200. $link_visible = 'Y';
  201. if ( empty($link_owner) )
  202. $link_owner = $current_user->id;
  203. if ( empty($link_notes) )
  204. $link_notes = '';
  205. if ( $update ) {
  206. $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
  207. link_name='$link_name', link_image='$link_image',
  208. link_target='$link_target', link_category='$link_category',
  209. link_visible='$link_visible', link_description='$link_description',
  210. link_rating='$link_rating', link_rel='$link_rel',
  211. link_notes='$link_notes', link_rss = '$link_rss'
  212. WHERE link_id='$link_id'");
  213. } else {
  214. $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')");
  215. $link_id = $wpdb->insert_id;
  216. }
  217. if ( $update )
  218. do_action('edit_link', $link_id);
  219. else
  220. do_action('add_link', $link_id);
  221. return $link_id;
  222. }
  223. function wp_update_link($linkdata) {
  224. global $wpdb;
  225. $link_id = (int) $linkdata['link_id'];
  226. $link = get_link($link_id, ARRAY_A);
  227. // Escape data pulled from DB.
  228. $link = add_magic_quotes($link);
  229. // Merge old and new fields with new fields overwriting old ones.
  230. $linkdata = array_merge($link, $linkdata);
  231. return wp_insert_link($linkdata);
  232. }
  233. function wp_delete_link($link_id) {
  234. global $wpdb;
  235. do_action('delete_link', $link_id);
  236. return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
  237. }
  238. function post_exists($title, $content = '', $post_date = '') {
  239. global $wpdb;
  240. if (!empty ($post_date))
  241. $post_date = "AND post_date = '$post_date'";
  242. if (!empty ($title))
  243. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
  244. else
  245. if (!empty ($content))
  246. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
  247. return 0;
  248. }
  249. function comment_exists($comment_author, $comment_date) {
  250. global $wpdb;
  251. return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
  252. WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
  253. }
  254. ?>