2005-11-13 05:21:44 +01:00
< ? php
function get_users_drafts ( $user_id ) {
global $wpdb ;
$user_id = ( int ) $user_id ;
2006-02-14 07:23:03 +01:00
$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 " ;
2005-11-13 05:21:44 +01:00
$query = apply_filters ( 'get_users_drafts' , $query );
return $wpdb -> get_results ( $query );
}
function get_others_drafts ( $user_id ) {
global $wpdb ;
$user = get_userdata ( $user_id );
$level_key = $wpdb -> prefix . 'user_level' ;
$editable = get_editable_user_ids ( $user_id );
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
if ( ! $editable ) {
$other_drafts = '' ;
} else {
$editable = join ( ',' , $editable );
2006-02-14 07:23:03 +01:00
$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 ' " );
2005-11-13 05:21:44 +01:00
}
return apply_filters ( 'get_others_drafts' , $other_drafts );
}
function get_editable_authors ( $user_id ) {
global $wpdb ;
$editable = get_editable_user_ids ( $user_id );
if ( ! $editable ) {
return false ;
} else {
$editable = join ( ',' , $editable );
2006-10-10 06:16:12 +02:00
$authors = $wpdb -> get_results ( " SELECT * FROM $wpdb->users WHERE ID IN ( $editable ) ORDER BY display_name " );
2005-11-13 05:21:44 +01:00
}
return apply_filters ( 'get_editable_authors' , $authors );
}
function get_editable_user_ids ( $user_id , $exclude_zeros = true ) {
global $wpdb ;
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
$user = new WP_User ( $user_id );
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
if ( ! $user -> has_cap ( 'edit_others_posts' ) ) {
if ( $user -> has_cap ( 'edit_posts' ) || $exclude_zeros == false )
return array ( $user -> id );
2006-11-19 08:56:05 +01:00
else
2005-11-13 05:21:44 +01:00
return false ;
}
$level_key = $wpdb -> prefix . 'user_level' ;
$query = " SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ' $level_key ' " ;
if ( $exclude_zeros )
$query .= " AND meta_value != '0' " ;
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
return $wpdb -> get_col ( $query );
}
function get_author_user_ids () {
global $wpdb ;
$level_key = $wpdb -> prefix . 'user_level' ;
$query = " SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ' $level_key ' AND meta_value != '0' " ;
return $wpdb -> get_col ( $query );
}
function get_nonauthor_user_ids () {
global $wpdb ;
$level_key = $wpdb -> prefix . 'user_level' ;
$query = " SELECT user_id FROM $wpdb->usermeta WHERE meta_key = ' $level_key ' AND meta_value = '0' " ;
return $wpdb -> get_col ( $query );
}
function wp_insert_category ( $catarr ) {
global $wpdb ;
extract ( $catarr );
2006-07-20 03:30:37 +02:00
if ( trim ( $cat_name ) == '' )
return 0 ;
2005-11-13 05:21:44 +01:00
$cat_ID = ( int ) $cat_ID ;
// Are we updating or creating?
if ( ! empty ( $cat_ID ))
$update = true ;
else
$update = false ;
2006-05-27 00:14:55 +02:00
$cat_name = apply_filters ( 'pre_category_name' , $cat_name );
2006-11-19 08:56:05 +01:00
2005-11-13 05:21:44 +01:00
if ( empty ( $category_nicename ))
$category_nicename = sanitize_title ( $cat_name );
else
$category_nicename = sanitize_title ( $category_nicename );
2006-05-27 00:47:13 +02:00
$category_nicename = apply_filters ( 'pre_category_nicename' , $category_nicename );
2005-11-13 05:21:44 +01:00
if ( empty ( $category_description ))
$category_description = '' ;
2006-05-27 00:47:13 +02:00
$category_description = apply_filters ( 'pre_category_description' , $category_description );
2005-11-13 05:21:44 +01:00
2006-05-27 00:14:55 +02:00
$category_parent = ( int ) $category_parent ;
2006-11-26 19:00:53 +01:00
if ( empty ( $category_parent ) || ! get_category ( $category_parent ) || ( $cat_ID && cat_is_ancestor_of ( $cat_ID , $category_parent ) ) )
2005-11-13 05:21:44 +01:00
$category_parent = 0 ;
2006-02-27 05:57:30 +01:00
if ( isset ( $posts_private ) )
$posts_private = ( int ) $posts_private ;
else
$posts_private = 0 ;
if ( isset ( $links_private ) )
$links_private = ( int ) $links_private ;
else
$links_private = 0 ;
2005-11-13 05:21:44 +01:00
if ( ! $update ) {
2006-02-27 05:57:30 +01:00
$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 ') " );
2007-03-23 01:59:21 +01:00
$cat_ID = ( int ) $wpdb -> insert_id ;
2005-11-13 05:21:44 +01:00
} else {
2006-02-27 05:57:30 +01:00
$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 ' " );
2005-11-13 05:21:44 +01:00
}
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
if ( $category_nicename == '' ) {
$category_nicename = sanitize_title ( $cat_name , $cat_ID );
$wpdb -> query ( " UPDATE $wpdb->categories SET category_nicename = ' $category_nicename ' WHERE cat_ID = ' $cat_ID ' " );
}
2006-12-10 21:27:20 +01:00
// Keep in mind when using this filter and altering the cat_ID that the two queries above
// have already taken place with the OLD cat_ID
// Also note that you may have post2cat entries with the old cat_ID if this is an update
2005-11-13 05:21:44 +01:00
if ( $update ) {
do_action ( 'edit_category' , $cat_ID );
} else {
do_action ( 'create_category' , $cat_ID );
do_action ( 'add_category' , $cat_ID );
}
2007-01-16 19:15:10 +01:00
$cat_ID = apply_filters ( 'cat_id_filter' , $cat_ID , $update );
clean_category_cache ( $cat_ID );
2005-11-13 05:21:44 +01:00
return $cat_ID ;
}
function wp_update_category ( $catarr ) {
global $wpdb ;
$cat_ID = ( int ) $catarr [ 'cat_ID' ];
2006-11-19 02:12:36 +01:00
if ( $cat_ID == $catarr [ 'category_parent' ] )
return false ;
2005-11-13 05:21:44 +01:00
// First, get all of the original fields
$category = get_category ( $cat_ID , ARRAY_A );
// Escape data pulled from DB.
$category = add_magic_quotes ( $category );
// Merge old and new fields with new fields overwriting old ones.
$catarr = array_merge ( $category , $catarr );
return wp_insert_category ( $catarr );
}
function wp_delete_category ( $cat_ID ) {
global $wpdb ;
$cat_ID = ( int ) $cat_ID ;
2006-12-10 21:53:05 +01:00
$default_cat = get_option ( 'default_category' );
$default_link_cat = get_option ( 'default_link_category' );
2005-11-13 05:21:44 +01:00
2006-12-10 21:53:05 +01:00
// Don't delete either of the default cats
if ( $cat_ID == $default_cat || $cat_ID == $default_link_cat )
2005-11-13 05:21:44 +01:00
return 0 ;
$category = get_category ( $cat_ID );
$parent = $category -> category_parent ;
2006-12-10 21:53:05 +01:00
// Delete the category
2006-03-29 03:51:55 +02:00
if ( ! $wpdb -> query ( " DELETE FROM $wpdb->categories WHERE cat_ID = ' $cat_ID ' " ) )
return 0 ;
2005-11-13 05:21:44 +01:00
2006-12-10 21:53:05 +01:00
// Update children to point to new parent
2005-11-13 05:21:44 +01:00
$wpdb -> query ( " UPDATE $wpdb->categories SET category_parent = ' $parent ' WHERE category_parent = ' $cat_ID ' " );
2006-12-10 21:53:05 +01:00
// Only set posts and links to the default category if they're not in another category already
2006-02-27 05:57:30 +01:00
$posts = $wpdb -> get_col ( " SELECT post_id FROM $wpdb->post2cat WHERE category_id=' $cat_ID ' " );
2006-12-10 21:53:05 +01:00
foreach ( ( array ) $posts as $post_id ) {
2006-06-06 07:04:41 +02:00
$cats = wp_get_post_categories ( $post_id );
2006-02-27 05:57:30 +01:00
if ( 1 == count ( $cats ) )
$cats = array ( $default_cat );
else
$cats = array_diff ( $cats , array ( $cat_ID ));
2006-11-19 08:56:05 +01:00
wp_set_post_categories ( $post_id , $cats );
2006-02-27 05:57:30 +01:00
}
2005-11-13 05:21:44 +01:00
2006-02-27 05:57:30 +01:00
$links = $wpdb -> get_col ( " SELECT link_id FROM $wpdb->link2cat WHERE category_id=' $cat_ID ' " );
2006-12-10 21:53:05 +01:00
foreach ( ( array ) $links as $link_id ) {
2006-02-27 05:57:30 +01:00
$cats = wp_get_link_cats ( $link_id );
if ( 1 == count ( $cats ) )
$cats = array ( $default_link_cat );
else
$cats = array_diff ( $cats , array ( $cat_ID ));
2006-11-19 08:56:05 +01:00
wp_set_link_cats ( $link_id , $cats );
2006-02-27 05:57:30 +01:00
}
2006-11-19 08:56:05 +01:00
2006-12-08 08:35:45 +01:00
clean_category_cache ( $cat_ID );
2005-11-13 05:21:44 +01:00
do_action ( 'delete_category' , $cat_ID );
return 1 ;
}
function wp_create_category ( $cat_name ) {
$cat_array = compact ( 'cat_name' );
return wp_insert_category ( $cat_array );
}
function wp_create_categories ( $categories , $post_id = '' ) {
$cat_ids = array ();
foreach ( $categories as $category ) {
if ( $id = category_exists ( $category ))
$cat_ids [] = $id ;
else
if ( $id = wp_create_category ( $category ))
$cat_ids [] = $id ;
}
if ( $post_id )
2006-06-06 07:04:41 +02:00
wp_set_post_categories ( $post_id , $cat_ids );
2005-11-13 05:21:44 +01:00
return $cat_ids ;
}
function category_exists ( $cat_name ) {
global $wpdb ;
if ( ! $category_nicename = sanitize_title ( $cat_name ))
return 0 ;
return $wpdb -> get_var ( " SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = ' $category_nicename ' " );
}
function wp_delete_user ( $id , $reassign = 'novalue' ) {
global $wpdb ;
$id = ( int ) $id ;
2005-12-01 18:38:37 +01:00
$user = get_userdata ( $id );
2005-11-13 05:21:44 +01:00
if ( $reassign == 'novalue' ) {
$post_ids = $wpdb -> get_col ( " SELECT ID FROM $wpdb->posts WHERE post_author = $id " );
if ( $post_ids ) {
2005-12-24 00:10:59 +01:00
foreach ( $post_ids as $post_id )
wp_delete_post ( $post_id );
2005-11-13 05:21:44 +01:00
}
// Clean links
$wpdb -> query ( " DELETE FROM $wpdb->links WHERE link_owner = $id " );
} else {
$reassign = ( int ) $reassign ;
$wpdb -> query ( " UPDATE $wpdb->posts SET post_author = { $reassign } WHERE post_author = { $id } " );
$wpdb -> query ( " UPDATE $wpdb->links SET link_owner = { $reassign } WHERE link_owner = { $id } " );
}
// FINALLY, delete user
2006-07-17 19:12:37 +02:00
do_action ( 'delete_user' , $id );
2005-11-13 05:21:44 +01:00
$wpdb -> query ( " DELETE FROM $wpdb->users WHERE ID = $id " );
2005-12-05 06:17:28 +01:00
$wpdb -> query ( " DELETE FROM $wpdb->usermeta WHERE user_id = ' $id ' " );
2005-11-13 05:21:44 +01:00
wp_cache_delete ( $id , 'users' );
2005-12-19 20:14:22 +01:00
wp_cache_delete ( $user -> user_login , 'userlogins' );
2005-11-13 05:21:44 +01:00
return true ;
}
2006-04-13 06:49:38 +02:00
function wp_revoke_user ( $id ) {
$id = ( int ) $id ;
2006-11-19 08:56:05 +01:00
2006-04-13 06:49:38 +02:00
$user = new WP_User ( $id );
2006-11-19 08:56:05 +01:00
$user -> remove_all_caps ();
2006-04-13 06:49:38 +02:00
}
2005-11-13 05:21:44 +01:00
function wp_insert_link ( $linkdata ) {
global $wpdb , $current_user ;
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
extract ( $linkdata );
$update = false ;
2006-08-25 00:33:16 +02:00
2005-11-13 05:21:44 +01:00
if ( ! empty ( $link_id ) )
$update = true ;
2006-08-25 00:33:16 +02:00
if ( trim ( $link_name ) == '' )
return 0 ;
$link_name = apply_filters ( 'pre_link_name' , $link_name );
if ( trim ( $link_url ) == '' )
return 0 ;
$link_url = apply_filters ( 'pre_link_url' , $link_url );
2005-11-13 05:21:44 +01:00
if ( empty ( $link_rating ) )
2006-02-12 08:53:23 +01:00
$link_rating = 0 ;
2006-08-25 00:33:16 +02:00
else
$link_rating = ( int ) $link_rating ;
if ( empty ( $link_image ) )
$link_image = '' ;
$link_image = apply_filters ( 'pre_link_image' , $link_image );
2005-11-13 05:21:44 +01:00
if ( empty ( $link_target ) )
2006-02-12 08:53:23 +01:00
$link_target = '' ;
2006-08-25 00:33:16 +02:00
$link_target = apply_filters ( 'pre_link_target' , $link_target );
2005-11-13 05:21:44 +01:00
if ( empty ( $link_visible ) )
$link_visible = 'Y' ;
2006-08-25 00:33:16 +02:00
$link_visibile = preg_replace ( '/[^YNyn]/' , '' , $link_visible );
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
if ( empty ( $link_owner ) )
$link_owner = $current_user -> id ;
2006-08-25 00:33:16 +02:00
else
$link_owner = ( int ) $link_owner ;
2005-12-11 00:22:47 +01:00
if ( empty ( $link_notes ) )
$link_notes = '' ;
2006-08-25 00:33:16 +02:00
$link_notes = apply_filters ( 'pre_link_notes' , $link_notes );
if ( empty ( $link_description ) )
$link_description = '' ;
$link_description = apply_filters ( 'pre_link_description' , $link_description );
if ( empty ( $link_rss ) )
$link_rss = '' ;
$link_rss = apply_filters ( 'pre_link_rss' , $link_rss );
if ( empty ( $link_rel ) )
$link_rel = '' ;
$link_rel = apply_filters ( 'pre_link_rel' , $link_rel );
2005-12-11 00:22:47 +01:00
2006-02-27 05:57:30 +01:00
// Make sure we set a valid category
if ( 0 == count ( $link_category ) || ! is_array ( $link_category )) {
2006-04-08 00:28:57 +02:00
$link_category = array ( get_option ( 'default_link_category' ));
2006-02-27 05:57:30 +01:00
}
2005-11-13 05:21:44 +01:00
if ( $update ) {
$wpdb -> query ( " UPDATE $wpdb->links SET link_url=' $link_url ',
link_name = '$link_name' , link_image = '$link_image' ,
2006-02-27 05:57:30 +01:00
link_target = '$link_target' ,
2005-11-13 05:21:44 +01:00
link_visible = '$link_visible' , link_description = '$link_description' ,
link_rating = '$link_rating' , link_rel = '$link_rel' ,
link_notes = '$link_notes' , link_rss = '$link_rss'
WHERE link_id = '$link_id' " );
} else {
2006-02-27 05:57:30 +01:00
$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 ') " );
2007-03-23 01:59:21 +01:00
$link_id = ( int ) $wpdb -> insert_id ;
2005-11-13 05:21:44 +01:00
}
2006-02-12 08:53:23 +01:00
2006-02-27 05:57:30 +01:00
wp_set_link_cats ( $link_id , $link_category );
2005-11-13 05:21:44 +01:00
if ( $update )
do_action ( 'edit_link' , $link_id );
else
do_action ( 'add_link' , $link_id );
return $link_id ;
}
function wp_update_link ( $linkdata ) {
global $wpdb ;
$link_id = ( int ) $linkdata [ 'link_id' ];
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
$link = get_link ( $link_id , ARRAY_A );
2006-02-12 08:53:23 +01:00
2005-11-13 05:21:44 +01:00
// Escape data pulled from DB.
$link = add_magic_quotes ( $link );
2006-02-12 08:53:23 +01:00
2006-02-27 05:57:30 +01:00
// Passed link category list overwrites existing category list if not empty.
2006-11-19 08:56:05 +01:00
if ( isset ( $linkdata [ 'link_category' ]) && is_array ( $linkdata [ 'link_category' ])
2006-02-27 05:57:30 +01:00
&& 0 != count ( $linkdata [ 'link_category' ]) )
2006-11-19 08:56:05 +01:00
$link_cats = $linkdata [ 'link_category' ];
else
$link_cats = $link [ 'link_category' ];
2006-02-27 05:57:30 +01:00
2005-11-13 05:21:44 +01:00
// Merge old and new fields with new fields overwriting old ones.
$linkdata = array_merge ( $link , $linkdata );
2006-11-19 08:56:05 +01:00
$linkdata [ 'link_category' ] = $link_cats ;
2005-11-13 05:21:44 +01:00
return wp_insert_link ( $linkdata );
}
function wp_delete_link ( $link_id ) {
global $wpdb ;
2005-11-21 09:06:18 +01:00
do_action ( 'delete_link' , $link_id );
2006-11-19 08:56:05 +01:00
2006-02-27 05:57:30 +01:00
$categories = wp_get_link_cats ( $link_id );
if ( is_array ( $categories ) ) {
foreach ( $categories as $category ) {
$wpdb -> query ( " UPDATE $wpdb->categories SET link_count = link_count - 1 WHERE cat_ID = ' $category ' " );
wp_cache_delete ( $category , 'category' );
2006-11-24 21:44:03 +01:00
do_action ( 'edit_category' , $cat_id );
2006-02-27 05:57:30 +01:00
}
}
$wpdb -> query ( " DELETE FROM $wpdb->link2cat WHERE link_id = ' $link_id ' " );
2006-02-12 08:53:23 +01:00
return $wpdb -> query ( " DELETE FROM $wpdb->links WHERE link_id = ' $link_id ' " );
2005-11-13 05:21:44 +01:00
}
2006-02-27 05:57:30 +01:00
function wp_get_link_cats ( $link_ID = 0 ) {
global $wpdb ;
2006-11-19 08:56:05 +01:00
$sql = " SELECT category_id
FROM $wpdb -> link2cat
WHERE link_id = $link_ID
2006-02-27 05:57:30 +01:00
ORDER BY category_id " ;
$result = $wpdb -> get_col ( $sql );
if ( ! $result )
$result = array ();
return array_unique ( $result );
}
function wp_set_link_cats ( $link_ID = 0 , $link_categories = array ()) {
global $wpdb ;
// If $link_categories isn't already an array, make it one:
if ( ! is_array ( $link_categories ) || 0 == count ( $link_categories ))
2006-04-08 00:28:57 +02:00
$link_categories = array ( get_option ( 'default_link_category' ));
2006-02-27 05:57:30 +01:00
$link_categories = array_unique ( $link_categories );
// First the old categories
$old_categories = $wpdb -> get_col ( "
2006-11-19 08:56:05 +01:00
SELECT category_id
FROM $wpdb -> link2cat
2006-02-27 05:57:30 +01:00
WHERE link_id = $link_ID " );
if ( ! $old_categories ) {
$old_categories = array ();
} else {
$old_categories = array_unique ( $old_categories );
}
// Delete any?
$delete_cats = array_diff ( $old_categories , $link_categories );
if ( $delete_cats ) {
foreach ( $delete_cats as $del ) {
$wpdb -> query ( "
2006-11-19 08:56:05 +01:00
DELETE FROM $wpdb -> link2cat
WHERE category_id = $del
AND link_id = $link_ID
2006-02-27 05:57:30 +01:00
" );
}
}
// Add any?
$add_cats = array_diff ( $link_categories , $old_categories );
if ( $add_cats ) {
foreach ( $add_cats as $new_cat ) {
2007-03-12 17:09:44 +01:00
$new_cat = ( int ) $new_cat ;
if ( ! empty ( $new_cat ) )
$wpdb -> query ( "
INSERT INTO $wpdb -> link2cat ( link_id , category_id )
VALUES ( '$link_ID' , '$new_cat' ) " );
2006-02-27 05:57:30 +01:00
}
}
2006-11-19 08:56:05 +01:00
2006-02-27 05:57:30 +01:00
// Update category counts.
$all_affected_cats = array_unique ( array_merge ( $link_categories , $old_categories ));
foreach ( $all_affected_cats as $cat_id ) {
$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 ' " );
$wpdb -> query ( " UPDATE $wpdb->categories SET link_count = ' $count ' WHERE cat_ID = ' $cat_id ' " );
wp_cache_delete ( $cat_id , 'category' );
2006-11-23 21:39:39 +01:00
do_action ( 'edit_category' , $cat_id );
2006-02-27 05:57:30 +01:00
}
2006-11-23 21:39:39 +01:00
2006-02-27 05:57:30 +01:00
} // wp_set_link_cats()
2005-11-13 05:21:44 +01:00
function post_exists ( $title , $content = '' , $post_date = '' ) {
global $wpdb ;
if ( ! empty ( $post_date ))
$post_date = " AND post_date = ' $post_date ' " ;
if ( ! empty ( $title ))
return $wpdb -> get_var ( " SELECT ID FROM $wpdb->posts WHERE post_title = ' $title ' $post_date " );
else
if ( ! empty ( $content ))
return $wpdb -> get_var ( " SELECT ID FROM $wpdb->posts WHERE post_content = ' $content ' $post_date " );
return 0 ;
}
function comment_exists ( $comment_author , $comment_date ) {
global $wpdb ;
return $wpdb -> get_var ( " SELECT comment_post_ID FROM $wpdb->comments
WHERE comment_author = '$comment_author' AND comment_date = '$comment_date' " );
}
2005-12-19 20:14:22 +01:00
?>