PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/2.2/wp-admin/admin-db.php

#
PHP | 525 lines | 373 code | 130 blank | 22 comment | 86 complexity | 12a38af24c9710a6d524356845452501 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_type = 'post' AND 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_type = 'post' AND 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, EXTR_SKIP);
  63. if( trim( $cat_name ) == '' )
  64. return 0;
  65. $cat_ID = (int) $cat_ID;
  66. // Are we updating or creating?
  67. if (!empty ($cat_ID))
  68. $update = true;
  69. else
  70. $update = false;
  71. $cat_name = apply_filters('pre_category_name', $cat_name);
  72. if (empty ($category_nicename))
  73. $category_nicename = sanitize_title($cat_name);
  74. else
  75. $category_nicename = sanitize_title($category_nicename);
  76. $category_nicename = apply_filters('pre_category_nicename', $category_nicename);
  77. if (empty ($category_description))
  78. $category_description = '';
  79. $category_description = apply_filters('pre_category_description', $category_description);
  80. $category_parent = (int) $category_parent;
  81. if ( empty($category_parent) || !get_category( $category_parent ) || ($cat_ID && cat_is_ancestor_of($cat_ID, $category_parent) ) )
  82. $category_parent = 0;
  83. if ( isset($posts_private) )
  84. $posts_private = (int) $posts_private;
  85. else
  86. $posts_private = 0;
  87. if ( isset($links_private) )
  88. $links_private = (int) $links_private;
  89. else
  90. $links_private = 0;
  91. if (!$update) {
  92. $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_description, category_parent, links_private, posts_private) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', '$category_parent', '$links_private', '$posts_private')");
  93. $cat_ID = (int) $wpdb->insert_id;
  94. } else {
  95. $wpdb->query ("UPDATE $wpdb->categories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = '$category_parent', links_private = '$links_private', posts_private = '$posts_private' WHERE cat_ID = '$cat_ID'");
  96. }
  97. if ( $category_nicename == '' ) {
  98. $category_nicename = sanitize_title($cat_name, $cat_ID );
  99. $wpdb->query( "UPDATE $wpdb->categories SET category_nicename = '$category_nicename' WHERE cat_ID = '$cat_ID'" );
  100. }
  101. // Keep in mind when using this filter and altering the cat_ID that the two queries above
  102. // have already taken place with the OLD cat_ID
  103. // Also note that you may have post2cat entries with the old cat_ID if this is an update
  104. if ($update) {
  105. do_action('edit_category', $cat_ID);
  106. } else {
  107. do_action('create_category', $cat_ID);
  108. do_action('add_category', $cat_ID);
  109. }
  110. $cat_ID = apply_filters('cat_id_filter', $cat_ID, $update);
  111. clean_category_cache($cat_ID);
  112. if ($update)
  113. do_action('edited_category', $cat_ID);
  114. else
  115. do_action('created_category', $cat_ID);
  116. return $cat_ID;
  117. }
  118. function wp_update_category($catarr) {
  119. global $wpdb;
  120. $cat_ID = (int) $catarr['cat_ID'];
  121. if( $cat_ID == $catarr['category_parent'] )
  122. return false;
  123. // First, get all of the original fields
  124. $category = get_category($cat_ID, ARRAY_A);
  125. // Escape data pulled from DB.
  126. $category = add_magic_quotes($category);
  127. // Merge old and new fields with new fields overwriting old ones.
  128. $catarr = array_merge($category, $catarr);
  129. return wp_insert_category($catarr);
  130. }
  131. function wp_delete_category($cat_ID) {
  132. global $wpdb;
  133. $cat_ID = (int) $cat_ID;
  134. $default_cat = get_option('default_category');
  135. $default_link_cat = get_option('default_link_category');
  136. // Don't delete either of the default cats
  137. if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat )
  138. return 0;
  139. $category = get_category($cat_ID);
  140. $parent = $category->category_parent;
  141. // Delete the category
  142. if ( !$wpdb->query("DELETE FROM $wpdb->categories WHERE cat_ID = '$cat_ID'") )
  143. return 0;
  144. // Update children to point to new parent
  145. $wpdb->query("UPDATE $wpdb->categories SET category_parent = '$parent' WHERE category_parent = '$cat_ID'");
  146. // Only set posts and links to the default category if they're not in another category already
  147. $posts = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id='$cat_ID'");
  148. foreach ( (array) $posts as $post_id ) {
  149. $cats = wp_get_post_categories($post_id);
  150. if ( 1 == count($cats) )
  151. $cats = array($default_cat);
  152. else
  153. $cats = array_diff($cats, array($cat_ID));
  154. wp_set_post_categories($post_id, $cats);
  155. }
  156. $links = $wpdb->get_col("SELECT link_id FROM $wpdb->link2cat WHERE category_id='$cat_ID'");
  157. foreach ( (array) $links as $link_id ) {
  158. $cats = wp_get_link_cats($link_id);
  159. if ( 1 == count($cats) )
  160. $cats = array($default_link_cat);
  161. else
  162. $cats = array_diff($cats, array($cat_ID));
  163. wp_set_link_cats($link_id, $cats);
  164. }
  165. clean_category_cache($cat_ID);
  166. do_action('delete_category', $cat_ID);
  167. return 1;
  168. }
  169. function wp_create_category($cat_name) {
  170. $cat_array = compact('cat_name');
  171. return wp_insert_category($cat_array);
  172. }
  173. function wp_create_categories($categories, $post_id = '') {
  174. $cat_ids = array ();
  175. foreach ($categories as $category) {
  176. if ($id = category_exists($category))
  177. $cat_ids[] = $id;
  178. else
  179. if ($id = wp_create_category($category))
  180. $cat_ids[] = $id;
  181. }
  182. if ($post_id)
  183. wp_set_post_categories($post_id, $cat_ids);
  184. return $cat_ids;
  185. }
  186. function category_exists($cat_name) {
  187. global $wpdb;
  188. if (!$category_nicename = sanitize_title($cat_name))
  189. return 0;
  190. return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$category_nicename'");
  191. }
  192. function wp_delete_user($id, $reassign = 'novalue') {
  193. global $wpdb;
  194. $id = (int) $id;
  195. $user = get_userdata($id);
  196. if ($reassign == 'novalue') {
  197. $post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_author = $id");
  198. if ($post_ids) {
  199. foreach ($post_ids as $post_id)
  200. wp_delete_post($post_id);
  201. }
  202. // Clean links
  203. $wpdb->query("DELETE FROM $wpdb->links WHERE link_owner = $id");
  204. } else {
  205. $reassign = (int) $reassign;
  206. $wpdb->query("UPDATE $wpdb->posts SET post_author = {$reassign} WHERE post_author = {$id}");
  207. $wpdb->query("UPDATE $wpdb->links SET link_owner = {$reassign} WHERE link_owner = {$id}");
  208. }
  209. // FINALLY, delete user
  210. do_action('delete_user', $id);
  211. $wpdb->query("DELETE FROM $wpdb->users WHERE ID = $id");
  212. $wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = '$id'");
  213. wp_cache_delete($id, 'users');
  214. wp_cache_delete($user->user_login, 'userlogins');
  215. return true;
  216. }
  217. function wp_revoke_user($id) {
  218. $id = (int) $id;
  219. $user = new WP_User($id);
  220. $user->remove_all_caps();
  221. }
  222. function wp_insert_link($linkdata) {
  223. global $wpdb, $current_user;
  224. extract($linkdata, EXTR_SKIP);
  225. $update = false;
  226. if ( !empty($link_id) )
  227. $update = true;
  228. $link_id = (int) $link_id;
  229. if( trim( $link_name ) == '' )
  230. return 0;
  231. $link_name = apply_filters('pre_link_name', $link_name);
  232. if( trim( $link_url ) == '' )
  233. return 0;
  234. $link_url = apply_filters('pre_link_url', $link_url);
  235. if ( empty($link_rating) )
  236. $link_rating = 0;
  237. else
  238. $link_rating = (int) $link_rating;
  239. if ( empty($link_image) )
  240. $link_image = '';
  241. $link_image = apply_filters('pre_link_image', $link_image);
  242. if ( empty($link_target) )
  243. $link_target = '';
  244. $link_target = apply_filters('pre_link_target', $link_target);
  245. if ( empty($link_visible) )
  246. $link_visible = 'Y';
  247. $link_visibile = preg_replace('/[^YNyn]/', '', $link_visible);
  248. if ( empty($link_owner) )
  249. $link_owner = $current_user->id;
  250. else
  251. $link_owner = (int) $link_owner;
  252. if ( empty($link_notes) )
  253. $link_notes = '';
  254. $link_notes = apply_filters('pre_link_notes', $link_notes);
  255. if ( empty($link_description) )
  256. $link_description = '';
  257. $link_description = apply_filters('pre_link_description', $link_description);
  258. if ( empty($link_rss) )
  259. $link_rss = '';
  260. $link_rss = apply_filters('pre_link_rss', $link_rss);
  261. if ( empty($link_rel) )
  262. $link_rel = '';
  263. $link_rel = apply_filters('pre_link_rel', $link_rel);
  264. // Make sure we set a valid category
  265. if (0 == count($link_category) || !is_array($link_category)) {
  266. $link_category = array(get_option('default_link_category'));
  267. }
  268. if ( $update ) {
  269. $wpdb->query("UPDATE $wpdb->links SET link_url='$link_url',
  270. link_name='$link_name', link_image='$link_image',
  271. link_target='$link_target',
  272. link_visible='$link_visible', link_description='$link_description',
  273. link_rating='$link_rating', link_rel='$link_rel',
  274. link_notes='$link_notes', link_rss = '$link_rss'
  275. WHERE link_id='$link_id'");
  276. } else {
  277. $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_image, link_target, link_description, link_visible, link_owner, link_rating, link_rel, link_notes, link_rss) VALUES('$link_url','$link_name', '$link_image', '$link_target', '$link_description', '$link_visible', '$link_owner', '$link_rating', '$link_rel', '$link_notes', '$link_rss')");
  278. $link_id = (int) $wpdb->insert_id;
  279. }
  280. wp_set_link_cats($link_id, $link_category);
  281. if ( $update )
  282. do_action('edit_link', $link_id);
  283. else
  284. do_action('add_link', $link_id);
  285. return $link_id;
  286. }
  287. function wp_update_link($linkdata) {
  288. global $wpdb;
  289. $link_id = (int) $linkdata['link_id'];
  290. $link = get_link($link_id, ARRAY_A);
  291. // Escape data pulled from DB.
  292. $link = add_magic_quotes($link);
  293. // Passed link category list overwrites existing category list if not empty.
  294. if ( isset($linkdata['link_category']) && is_array($linkdata['link_category'])
  295. && 0 != count($linkdata['link_category']) )
  296. $link_cats = $linkdata['link_category'];
  297. else
  298. $link_cats = $link['link_category'];
  299. // Merge old and new fields with new fields overwriting old ones.
  300. $linkdata = array_merge($link, $linkdata);
  301. $linkdata['link_category'] = $link_cats;
  302. return wp_insert_link($linkdata);
  303. }
  304. function wp_delete_link($link_id) {
  305. global $wpdb;
  306. do_action('delete_link', $link_id);
  307. $categories = wp_get_link_cats($link_id);
  308. if( is_array( $categories ) ) {
  309. foreach ( $categories as $category ) {
  310. $wpdb->query("UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = '$category'");
  311. wp_cache_delete($category, 'category');
  312. do_action('edit_category', $cat_id);
  313. }
  314. }
  315. $wpdb->query("DELETE FROM $wpdb->link2cat WHERE link_id = '$link_id'");
  316. $wpdb->query("DELETE FROM $wpdb->links WHERE link_id = '$link_id'");
  317. do_action('deleted_link', $link_id);
  318. return true;
  319. }
  320. function wp_get_link_cats($link_ID = 0) {
  321. global $wpdb;
  322. $sql = "SELECT category_id
  323. FROM $wpdb->link2cat
  324. WHERE link_id = $link_ID
  325. ORDER BY category_id";
  326. $result = $wpdb->get_col($sql);
  327. if ( !$result )
  328. $result = array();
  329. return array_unique($result);
  330. }
  331. function wp_set_link_cats($link_ID = 0, $link_categories = array()) {
  332. global $wpdb;
  333. // If $link_categories isn't already an array, make it one:
  334. if (!is_array($link_categories) || 0 == count($link_categories))
  335. $link_categories = array(get_option('default_link_category'));
  336. $link_categories = array_unique($link_categories);
  337. // First the old categories
  338. $old_categories = $wpdb->get_col("
  339. SELECT category_id
  340. FROM $wpdb->link2cat
  341. WHERE link_id = '$link_ID'");
  342. if (!$old_categories) {
  343. $old_categories = array();
  344. } else {
  345. $old_categories = array_unique($old_categories);
  346. }
  347. // Delete any?
  348. $delete_cats = array_diff($old_categories,$link_categories);
  349. if ($delete_cats) {
  350. foreach ($delete_cats as $del) {
  351. $del = (int) $del;
  352. $wpdb->query("
  353. DELETE FROM $wpdb->link2cat
  354. WHERE category_id = '$del'
  355. AND link_id = '$link_ID'
  356. ");
  357. }
  358. }
  359. // Add any?
  360. $add_cats = array_diff($link_categories, $old_categories);
  361. if ($add_cats) {
  362. foreach ($add_cats as $new_cat) {
  363. $new_cat = (int) $new_cat;
  364. if ( !empty($new_cat) )
  365. $wpdb->query("
  366. INSERT INTO $wpdb->link2cat (link_id, category_id)
  367. VALUES ('$link_ID', '$new_cat')");
  368. }
  369. }
  370. // Update category counts.
  371. $all_affected_cats = array_unique(array_merge($link_categories, $old_categories));
  372. foreach ( $all_affected_cats as $cat_id ) {
  373. $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'");
  374. $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'");
  375. wp_cache_delete($cat_id, 'category');
  376. do_action('edit_category', $cat_id);
  377. }
  378. } // wp_set_link_cats()
  379. function post_exists($title, $content = '', $post_date = '') {
  380. global $wpdb;
  381. if (!empty ($post_date))
  382. $post_date = "AND post_date = '$post_date'";
  383. if (!empty ($title))
  384. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title = '$title' $post_date");
  385. else
  386. if (!empty ($content))
  387. return $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_content = '$content' $post_date");
  388. return 0;
  389. }
  390. function comment_exists($comment_author, $comment_date) {
  391. global $wpdb;
  392. return $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments
  393. WHERE comment_author = '$comment_author' AND comment_date = '$comment_date'");
  394. }
  395. ?>