SyndicatedPost::author_id PHP Méthode

author_id() public méthode

SyndicatedPost::author_id (): get the ID for an author name from the feed. Create the author if necessary.
public author_id ( string $unfamiliar_author = 'create' ) : null | integer
$unfamiliar_author string
Résultat null | integer The numeric ID of the author to attribute the post to NULL if the post should be filtered out.
    function author_id($unfamiliar_author = 'create')
    {
        global $wpdb;
        $a = $this->named['author'];
        $source = $this->source();
        $forbidden = apply_filters('feedwordpress_forbidden_author_names', array('admin', 'administrator', 'www', 'root'));
        // Prepare the list of candidates to try for author name: name from
        // feed, original source title (if any), immediate source title live
        // from feed, subscription title, prettied version of feed homepage URL,
        // prettied version of feed URL, or, failing all, use "unknown author"
        // as last resort
        $candidates = array();
        $candidates[] = $a['name'];
        if (!is_null($source)) {
            $candidates[] = $source['title'];
        }
        $candidates[] = $this->link->name(true);
        $candidates[] = $this->link->name(false);
        if (strlen($this->link->homepage()) > 0) {
            $candidates[] = feedwordpress_display_url($this->link->homepage());
        }
        $candidates[] = feedwordpress_display_url($this->link->uri());
        $candidates[] = 'unknown author';
        // Pick the first one that works from the list, screening against empty
        // or forbidden names.
        $author = NULL;
        while (is_null($author) and $candidate = each($candidates)) {
            if (!is_null($candidate['value']) and strlen(trim($candidate['value'])) > 0 and !in_array(strtolower(trim($candidate['value'])), $forbidden)) {
                $author = $candidate['value'];
            }
        }
        $email = isset($a['email']) ? $a['email'] : NULL;
        $authorUrl = isset($a['uri']) ? $a['uri'] : NULL;
        $hostUrl = $this->link->homepage();
        if (is_null($hostUrl) or strlen($hostUrl) < 0) {
            $hostUrl = $this->link->uri();
        }
        $match_author_by_email = !('yes' == get_option("feedwordpress_do_not_match_author_by_email"));
        if ($match_author_by_email and !FeedWordPress::is_null_email($email)) {
            $test_email = $email;
        } else {
            $test_email = NULL;
        }
        // Never can be too careful...
        $login = sanitize_user($author, true);
        // Possible for, e.g., foreign script author names
        if (strlen($login) < 1) {
            // No usable characters in author name for a login.
            // (Sometimes results from, e.g., foreign scripts.)
            //
            // We just need *something* in Western alphanumerics,
            // so let's try the domain name.
            //
            // Uniqueness will be guaranteed below if necessary.
            $url = parse_url($hostUrl);
            $login = sanitize_user($url['host'], true);
            if (strlen($login) < 1) {
                // This isn't working. Frak it.
                $login = 'syndicated';
            }
        }
        $login = apply_filters('pre_user_login', $login);
        $nice_author = sanitize_title($author);
        $nice_author = apply_filters('pre_user_nicename', $nice_author);
        $reg_author = esc_sql(preg_quote($author));
        $author = esc_sql($author);
        $email = esc_sql($email);
        $test_email = esc_sql($test_email);
        $authorUrl = esc_sql($authorUrl);
        // Check for an existing author rule....
        if (isset($this->link->settings['map authors']['name']['*'])) {
            $author_rule = $this->link->settings['map authors']['name']['*'];
        } elseif (isset($this->link->settings['map authors']['name'][strtolower(trim($author))])) {
            $author_rule = $this->link->settings['map authors']['name'][strtolower(trim($author))];
        } else {
            $author_rule = NULL;
        }
        // User name is mapped to a particular author. If that author ID exists, use it.
        if (is_numeric($author_rule) and get_userdata((int) $author_rule)) {
            $id = (int) $author_rule;
            // User name is filtered out
        } elseif ('filter' == $author_rule) {
            $id = NULL;
        } else {
            // Check the database for an existing author record that might fit
            // First try the user core data table.
            $id = $wpdb->get_var("SELECT ID FROM {$wpdb->users}\n\t\t\tWHERE TRIM(LCASE(display_name)) = TRIM(LCASE('{$author}'))\n\t\t\tOR TRIM(LCASE(user_login)) = TRIM(LCASE('{$author}'))\n\t\t\tOR (\n\t\t\t\tLENGTH(TRIM(LCASE(user_email))) > 0\n\t\t\t\tAND TRIM(LCASE(user_email)) = TRIM(LCASE('{$test_email}'))\n\t\t\t)");
            // If that fails, look for aliases in the user meta data table
            if (is_null($id)) {
                $id = $wpdb->get_var("SELECT user_id FROM {$wpdb->usermeta}\n\t\t\t\tWHERE\n\t\t\t\t\t(meta_key = 'description' AND TRIM(LCASE(meta_value)) = TRIM(LCASE('{$author}')))\n\t\t\t\t\tOR (\n\t\t\t\t\t\tmeta_key = 'description'\n\t\t\t\t\t\tAND TRIM(LCASE(meta_value))\n\t\t\t\t\t\tRLIKE CONCAT(\n\t\t\t\t\t\t\t'(^|\\n)a\\.?k\\.?a\\.?( |\\t)*:?( |\\t)*',\n\t\t\t\t\t\t\tTRIM(LCASE('{$reg_author}')),\n\t\t\t\t\t\t\t'( |\\t|\\r)*(\\n|\$)'\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t");
            }
            // ... if you don't find one, then do what you need to do
            if (is_null($id)) {
                if ($unfamiliar_author === 'create') {
                    $userdata = array();
                    // WordPress 3 is going to pitch a fit if we attempt to register
                    // more than one user account with an empty e-mail address, so we
                    // need *something* here. Ugh.
                    if (strlen($email) == 0 or FeedWordPress::is_null_email($email)) {
                        $url = parse_url($hostUrl);
                        $email = $nice_author . '@' . $url['host'];
                    }
                    #-- user table data
                    $userdata['ID'] = NULL;
                    // new user
                    $userdata['user_login'] = $login;
                    $userdata['user_nicename'] = $nice_author;
                    $userdata['user_pass'] = substr(md5(uniqid(microtime())), 0, 6);
                    // just something random to lock it up
                    $userdata['user_email'] = $email;
                    $userdata['user_url'] = $authorUrl;
                    $userdata['nickname'] = $author;
                    $parts = preg_split('/\\s+/', trim($author), 2);
                    if (isset($parts[0])) {
                        $userdata['first_name'] = $parts[0];
                    }
                    if (isset($parts[1])) {
                        $userdata['last_name'] = $parts[1];
                    }
                    $userdata['display_name'] = $author;
                    $userdata['role'] = 'contributor';
                    do {
                        // Keep trying until you get it right. Or until PHP crashes, I guess.
                        $id = wp_insert_user($userdata);
                        if (is_wp_error($id)) {
                            $codes = $id->get_error_code();
                            switch ($codes) {
                                case 'empty_user_login':
                                case 'existing_user_login':
                                    // Add a random disambiguator
                                    $userdata['user_login'] .= substr(md5(uniqid(microtime())), 0, 6);
                                    break;
                                case 'user_nicename_too_long':
                                    // Add a limited 50 caracters user_nicename based on user_login
                                    $userdata['user_nicename'] = mb_substr($userdata['user_login'], 0, 50);
                                    break;
                                case 'existing_user_email':
                                    // No disassemble!
                                    $parts = explode('@', $userdata['user_email'], 2);
                                    // Add a random disambiguator as a gmail-style username extension
                                    $parts[0] .= '+' . substr(md5(uniqid(microtime())), 0, 6);
                                    // Reassemble
                                    $userdata['user_email'] = $parts[0] . '@' . $parts[1];
                                    break;
                            }
                        }
                    } while (is_wp_error($id));
                } elseif (is_numeric($unfamiliar_author) and get_userdata((int) $unfamiliar_author)) {
                    $id = (int) $unfamiliar_author;
                } elseif ($unfamiliar_author === 'default') {
                    $id = 1;
                }
            }
        }
        if ($id) {
            $this->link->settings['map authors']['name'][strtolower(trim($author))] = $id;
            // Multisite: Check whether the author has been recorded
            // on *this* blog before. If not, put her down as a
            // Contributor for *this* blog.
            $user = new WP_User((int) $id);
            if (empty($user->roles)) {
                $user->add_role('contributor');
            }
        }
        return $id;
    }