Make WordPress Core

Ticket #4642: 4642.3.diff

File 4642.3.diff, 2.9 KB (added by Bobcat, 17 years ago)

Set post status based on author's capabilities; includes new function get_userdatabyemail

  • wp-includes/pluggable.php

     
    129129}
    130130endif;
    131131
     132if ( !function_exists('get_userdatabyemail') ) :
     133function get_userdatabyemail($user_email) {
     134        global $wpdb;
     135        $user_email = sanitize_email( $user_email );
     136
     137        if ( !is_email( $user_email ) )
     138                return false;
     139
     140        if ( !$user_ID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_email = %s LIMIT 1", $user_email)) )
     141                return false;
     142
     143        $user = get_userdata($user_ID);
     144        return $user;
     145}
     146endif;
     147
    132148if ( !function_exists( 'wp_mail' ) ) :
    133149function wp_mail( $to, $subject, $message, $headers = '' ) {
    134150        // Compact the input, apply the filters, and extract them back out
  • wp-mail.php

     
    2727        $content_transfer_encoding = '';
    2828        $boundary = '';
    2929        $bodysignal = 0;
     30        $post_author = 1;
     31        $author_found = false;
    3032        $dmonths = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    3133        foreach ($message as $line) :
    3234                if (strlen($line) < 3) $bodysignal = 1;
     
    7072                                $author = sanitize_email($author);
    7173                                if ( is_email($author) ) {
    7274                                        echo "Author = {$author} <p>";
    73                                         $author = $wpdb->escape($author);
    74                                         $result = $wpdb->get_row("SELECT ID FROM $wpdb->users WHERE user_email='$author' LIMIT 1");
    75                                         if (!$result)
     75                                        $userdata = get_userdatabyemail($author);
     76                                        if (!$userdata) {
    7677                                                $post_author = 1;
    77                                         else
    78                                                 $post_author = $result->ID;
    79                                 } else
     78                                                $author_found = false;
     79                                        } else {
     80                                                $post_author = $userdata->ID;
     81                                                $author_found = true;
     82                                        }
     83                                } else {
    8084                                        $post_author = 1;
     85                                        $author_found = false;
     86                                }
    8187                        }
    8288
    8389                        if (preg_match('/Date: /i', $line)) { // of the form '20 Mar 2002 20:32:37'
     
    111117                }
    112118        endforeach;
    113119
     120        // Set $post_status based on $author_found and on author's publish_posts capability
     121        if ($author_found) {
     122                $user = new WP_User($post_author);
     123                if ($user->has_cap('publish_posts'))
     124                        $post_status = 'publish';
     125                else
     126                        $post_status = 'pending';
     127        } else {
     128                // Author not found in DB, set status to pending.  Author already set to admin.
     129                $post_status = 'pending';
     130        }
     131
    114132        $subject = trim($subject);
    115133
    116134        if ($content_type == 'multipart/alternative') {
     
    144162
    145163        $post_category = $post_categories;
    146164
    147         // or maybe we should leave the choice to email drafts? propose a way
    148         $post_status = 'publish';
    149 
    150165        $post_data = compact('post_content','post_title','post_date','post_date_gmt','post_author','post_category', 'post_status');
    151166        $post_data = add_magic_quotes($post_data);
    152167