PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 385 lines | 271 code | 102 blank | 12 comment | 58 complexity | b2af7c9945b7c04885b7e359a0b82655 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) ORDER BY display_name" );
  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 ($cat_ID == get_option('default_category'))
  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. $default_cat = get_option('default_category');
  126. $wpdb->query("UPDATE $wpdb->post2cat SET category_id='$default_cat' WHERE category_id='$cat_ID'");
  127. wp_cache_delete($cat_ID, 'category');
  128. wp_cache_delete('all_category_ids', 'category');
  129. do_action('delete_category', $cat_ID);
  130. return 1;
  131. }
  132. function wp_create_category($cat_name) {
  133. $cat_array = compact('cat_name');
  134. return wp_insert_category($cat_array);
  135. }
  136. function wp_create_categories($categories, $post_id = '') {
  137. $cat_ids = array ();
  138. foreach ($categories as $category) {
  139. if ($id = category_exists($category))
  140. $cat_ids[] = $id;
  141. else
  142. if ($id = wp_create_category($category))
  143. $cat_ids[] = $id;
  144. }
  145. if ($post_id)
  146. wp_set_post_cats('', $post_id, $cat_ids);
  147. return $cat_ids;
  148. }
  149. function category_exists($cat_name) {
  150. global $wpdb;
  151. if (!$category_nicename = sanitize_title($cat_name))
  152. return 0;
  153. return $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
  154. }
  155. function wp_delete_user($id, $reassign = 'novalue') {
  156. global $wpdb;
  157. $id = (int) $id;
  158. $user = get_userdata($id);
  159. if ($reassign == 'novalue') {
  160. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  161. if ($post_ids) {
  162. foreach ($post_ids as $post_id)
  163. wp_delete_post($post_id);
  164. }
  165. // Clean links
  166. $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  167. } else {
  168. $reassign = (int) $reassign;
  169. $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
  170. $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
  171. }
  172. // FINALLY, delete user
  173. $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  174. $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
  175. wp_cache_delete($id, 'users');
  176. wp_cache_delete($user->user_login, 'userlogins');
  177. do_action('delete_user', $id);
  178. return true;
  179. }
  180. function get_link($link_id, $output = OBJECT) {
  181. global $wpdb;
  182. $link = $wpdb->get_row("SELECT * FROM $wpdb->links WHERE link_id = '$link_id'");
  183. if ( $output == OBJECT ) {
  184. return $link;
  185. } elseif ( $output == ARRAY_A ) {
  186. return get_object_vars($link);
  187. } elseif ( $output == ARRAY_N ) {
  188. return array_values(get_object_vars($link));
  189. } else {
  190. return $link;
  191. }
  192. }
  193. function wp_insert_link($linkdata) {
  194. global $wpdb, $current_user;
  195. extract($linkdata);
  196. $update = false;
  197. if ( !empty($link_id) )
  198. $update = true;
  199. if( trim( $link_name ) == '' )
  200. return 0;
  201. $link_name = apply_filters('pre_link_name', $link_name);
  202. if( trim( $link_url ) == '' )
  203. return 0;
  204. $link_url = apply_filters('pre_link_url', $link_url);
  205. if ( empty($link_rating) )
  206. $link_rating = 0;
  207. else
  208. $link_rating = (int) $link_rating;
  209. if ( empty($link_image) )
  210. $link_image = '';
  211. $link_image = apply_filters('pre_link_image', $link_image);
  212. if ( empty($link_target) )
  213. $link_target = '';
  214. $link_target = apply_filters('pre_link_target', $link_target);
  215. if ( empty($link_visible) )
  216. $link_visible = 'Y';
  217. $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
  218. if ( empty($link_owner) )
  219. $link_owner = $current_user->id;
  220. else
  221. $link_owner = (int) $link_owner;
  222. if ( empty($link_notes) )
  223. $link_notes = '';
  224. $link_notes = apply_filters('pre_link_notes', $link_notes);
  225. if ( empty($link_description) )
  226. $link_description = '';
  227. $link_description = apply_filters('pre_link_description', $link_description);
  228. if ( empty($link_rss) )
  229. $link_rss = '';
  230. $link_rss = apply_filters('pre_link_rss', $link_rss);
  231. if ( empty($link_rel) )
  232. $link_rel = '';
  233. $link_rel = apply_filters('pre_link_rel', $link_rel);
  234. if ( $update ) {
  235. $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
  236. link_name='$link_name', link_image='$link_image',
  237. link_target='$link_target', link_category='$link_category',
  238. link_visible='$link_visible', link_description='$link_description',
  239. link_rating='$link_rating', link_rel='$link_rel',
  240. link_notes='$link_notes', link_rss = '$link_rss'
  241. WHERE link_id='$link_id'");
  242. } else {
  243. $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')");
  244. $link_id = $wpdb->insert_id;
  245. }
  246. if ( $update )
  247. do_action('edit_link', $link_id);
  248. else
  249. do_action('add_link', $link_id);
  250. return $link_id;
  251. }
  252. function wp_update_link($linkdata) {
  253. global $wpdb;
  254. $link_id = (int) $linkdata['link_id'];
  255. $link = get_link($link_id, ARRAY_A);
  256. // Escape data pulled from DB.
  257. $link = add_magic_quotes($link);
  258. // Merge old and new fields with new fields overwriting old ones.
  259. $linkdata = array_merge($link, $linkdata);
  260. return wp_insert_link($linkdata);
  261. }
  262. function wp_delete_link($link_id) {
  263. global $wpdb;
  264. do_action('delete_link', $link_id);
  265. return $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
  266. }
  267. function post_exists($title, $content = '', $post_date = '') {
  268. global $wpdb;
  269. if (!empty ($post_date))
  270. $post_date = "AND post_date = '$post_date'";
  271. if (!empty ($title))
  272. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
  273. else
  274. if (!empty ($content))
  275. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
  276. return 0;
  277. }
  278. function comment_exists($comment_author, $comment_date) {
  279. global $wpdb;
  280. return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
  281. WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
  282. }
  283. ?>