{"id":235,"date":"2025-01-10T20:33:20","date_gmt":"2025-01-10T12:33:20","guid":{"rendered":"http:\/\/sqtcw.cn\/?p=235"},"modified":"2025-01-10T20:33:20","modified_gmt":"2025-01-10T12:33:20","slug":"wordpress%e4%b8%adgravatar%e7%94%a8%e6%88%b7%e5%a4%b4%e5%83%8f%e4%b8%8d%e6%98%be%e7%a4%ba%ef%bc%8c%e5%85%8d%e6%8f%92%e4%bb%b6%e5%ae%9e%e7%8e%b0%e4%b8%8a%e4%bc%a0%e5%a4%b4%e5%83%8f%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=235","title":{"rendered":"WordPress\u4e2dGravatar\u7528\u6237\u5934\u50cf\u4e0d\u663e\u793a\uff0c\u514d\u63d2\u4ef6\u5b9e\u73b0\u4e0a\u4f20\u5934\u50cf\u529f\u80fd"},"content":{"rendered":"\n<p>\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u52a0\u5165\u5230\u4f60\u4e3b\u9898\u7684Functions.php \u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5237\u65b0\u9875\u9762\u3002\u8fdb\u5165\u7528\u6237\u8d44\u6599\u9875\uff0c\u6ed1\u5230\u5e95\u90e8\uff0c\u70b9\u51fb\u4e0a\u4f20\u5934\u50cf\u5373\u53ef<\/p>\n\n\n\n<p>\/\/\u81ea\u5b9a\u4e49\u5934\u50cf<br><br>class Simple_Local_Avatars {<br>    private $user_id_being_edited;<br>        <br>    public function __construct() {<br>        add_filter( &#8216;get_avatar&#8217;, array( $this, &#8216;get_avatar&#8217; ), 10, 5 );<br>        <br>        add_action( &#8216;admin_init&#8217;, array( $this, &#8216;admin_init&#8217; ) );<br>        <br>        add_action( &#8216;show_user_profile&#8217;, array( $this, &#8216;edit_user_profile&#8217; ) );<br>        add_action( &#8216;edit_user_profile&#8217;, array( $this, &#8216;edit_user_profile&#8217; ) );<br>        <br>        add_action( &#8216;personal_options_update&#8217;, array( $this, &#8216;edit_user_profile_update&#8217; ) );<br>        add_action( &#8216;edit_user_profile_update&#8217;, array( $this, &#8216;edit_user_profile_update&#8217; ) );<br>        <br>        add_filter( &#8216;avatar_defaults&#8217;, array( $this, &#8216;avatar_defaults&#8217; ) );<br>    }<br>        <br>    public function get_avatar( $avatar = &#8221;, $id_or_email, $size = 96, $default = &#8221;, $alt = false ) {<br>        <br>        if ( is_numeric($id_or_email) )<br>            $user_id = (int) $id_or_email;<br>        elseif ( is_string( $id_or_email ) &amp;&amp; ( $user = get_user_by( &#8217;email&#8217;, $id_or_email ) ) )<br>            $user_id = $user->ID;<br>        elseif ( is_object( $id_or_email ) &amp;&amp; ! empty( $id_or_email->user_id ) )<br>            $user_id = (int) $id_or_email->user_id;<br>        <br>        if ( empty( $user_id ) )<br>            return $avatar;<br>        <br>        $local_avatars = get_user_meta( $user_id, &#8216;simple_local_avatar&#8217;, true );<br>        <br>        if ( empty( $local_avatars ) || empty( $local_avatars[&#8216;full&#8217;] ) )<br>            return $avatar;<br>        <br>        $size = (int) $size;<br>        <br>        if ( empty( $alt ) )<br>            $alt = get_the_author_meta( &#8216;display_name&#8217;, $user_id );<br>        <br>        \/\/ generate a new size<br>        if ( empty( $local_avatars[$size] ) ) {<br>            $upload_path = wp_upload_dir();<br>            $avatar_full_path = str_replace( $upload_path[&#8216;baseurl&#8217;], $upload_path[&#8216;basedir&#8217;], $local_avatars[&#8216;full&#8217;] );<br>            $image_sized = image_resize( $avatar_full_path, $size, $size, true );      <br>            \/\/ deal with original being >= to original image (or lack of sizing ability)<br>            $local_avatars[$size] = is_wp_error($image_sized) ? $local_avatars[$size] = $local_avatars[&#8216;full&#8217;] : str_replace( $upload_path[&#8216;basedir&#8217;], $upload_path[&#8216;baseurl&#8217;], $image_sized );<br>            \/\/ save updated avatar sizes<br>            update_user_meta( $user_id, &#8216;simple_local_avatar&#8217;, $local_avatars );<br>        } elseif ( substr( $local_avatars[$size], 0, 4 ) != &#8216;http&#8217; ) {<br>            $local_avatars[$size] = home_url( $local_avatars[$size] );<br>        }<br>        <br>        $author_class = is_author( $user_id ) ? &#8216; current-author&#8217; : &#8221; ;<br>        $avatar = &#8220;&lt;img alt='&#8221; . esc_attr( $alt ) . &#8220;&#8216; src='&#8221; . $local_avatars[$size] . &#8220;&#8216; class=&#8217;avatar avatar-{$size}{$author_class} photo&#8217; height='{$size}&#8217; width='{$size}&#8217; \/>&#8221;;<br>        <br>        return apply_filters( &#8216;simple_local_avatar&#8217;, $avatar );<br>    }<br>        <br>    public function admin_init() {<br>        \/\/load_plugin_textdomain( &#8216;simple-local-avatars&#8217;, false, dirname( plugin_basename( __FILE__ ) ) . &#8216;\/localization\/&#8217; );<br>        <br>        register_setting( &#8216;discussion&#8217;, &#8216;simple_local_avatars_caps&#8217;, array( $this, &#8216;sanitize_options&#8217; ) );<br>        add_settings_field( &#8216;simple-local-avatars-caps&#8217;, __(&#8216;Local Avatar Permissions&#8217;,&#8217;simple-local-avatars&#8217;), array( $this, &#8216;avatar_settings_field&#8217; ), &#8216;discussion&#8217;, &#8216;avatars&#8217; );<br>    }<br>        <br>    public function sanitize_options( $input ) {<br>        $new_input[&#8216;simple_local_avatars_caps&#8217;] = empty( $input[&#8216;simple_local_avatars_caps&#8217;] ) ? 0 : 1;<br>        return $new_input;<br>    }<br>        <br>    public function avatar_settings_field( $args ) {       <br>        $options = get_option(&#8216;simple_local_avatars_caps&#8217;);<br>        <br>        echo &#8216;<br>            &lt;label for=&#8221;simple_local_avatars_caps&#8221;><br>                &lt;input type=&#8221;checkbox&#8221; name=&#8221;simple_local_avatars_caps&#8221; id=&#8221;simple_local_avatars_caps&#8221; value=&#8221;1&#8243; &#8216; . @checked( $options[&#8216;simple_local_avatars_caps&#8217;], 1, false ) . &#8216; \/><br>                &#8216; . __(&#8216;\u4ec5\u5177\u6709\u5934\u50cf\u4e0a\u4f20\u6743\u9650\u7684\u7528\u6237\u5177\u6709\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\u6743\u9650\uff08\u4f5c\u8005\u53ca\u66f4\u9ad8\u7b49\u7ea7\u89d2\u8272\uff09\u3002&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;<br>            &lt;\/label><br>        &#8216;;<br>    }<br>        <br>    public function edit_user_profile( $profileuser ) {<br>    ?><br>    &lt;h3>&lt;?php _e( &#8216;\u5934\u50cf&#8217;,&#8217;simple-local-avatars&#8217; ); ?>&lt;\/h3><br>        <br>    &lt;table class=&#8221;form-table&#8221;><br>        &lt;tr><br>            &lt;th>&lt;label for=&#8221;simple-local-avatar&#8221;>&lt;?php _e(&#8216;\u4e0a\u4f20\u5934\u50cf&#8217;,&#8217;simple-local-avatars&#8217;); ?>&lt;\/label>&lt;\/th><br>            &lt;td style=&#8221;width: 50px;&#8221; valign=&#8221;top&#8221;><br>                &lt;?php echo get_avatar( $profileuser->ID ); ?><br>            &lt;\/td><br>            &lt;td><br>            &lt;?php<br>                $options = get_option(&#8216;simple_local_avatars_caps&#8217;);<br>        <br>                if ( empty($options[&#8216;simple_local_avatars_caps&#8217;]) || current_user_can(&#8216;upload_files&#8217;) ) {<br>                    do_action( &#8216;simple_local_avatar_notices&#8217; );<br>                    wp_nonce_field( &#8216;simple_local_avatar_nonce&#8217;, &#8216;_simple_local_avatar_nonce&#8217;, false );<br>            ?><br>                    &lt;input type=&#8221;file&#8221; name=&#8221;simple-local-avatar&#8221; id=&#8221;simple-local-avatar&#8221; \/>&lt;br \/><br>            &lt;?php<br>                    if ( empty( $profileuser->simple_local_avatar ) )<br>                        echo &#8216;&lt;span class=&#8221;description&#8221;>&#8217; . __(&#8216;\u5c1a\u672a\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u70b9\u51fb\u201c\u6d4f\u89c8\u201d\u6309\u94ae\u4e0a\u4f20\u672c\u5730\u5934\u50cf\u3002&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;&lt;\/span>&#8217;;<br>                    else<br>                        echo &#8216;<br>                            &lt;input type=&#8221;checkbox&#8221; name=&#8221;simple-local-avatar-erase&#8221; value=&#8221;1&#8243; \/> &#8216; . __(&#8216;\u79fb\u9664\u672c\u5730\u5934\u50cf&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;&lt;br \/><br>                            &lt;span class=&#8221;description&#8221;>&#8217; . __(&#8216;\u5982\u9700\u8981\u4fee\u6539\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u91cd\u65b0\u4e0a\u4f20\u65b0\u5934\u50cf\u3002\u5982\u9700\u8981\u79fb\u9664\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u9009\u4e2d\u4e0a\u65b9\u7684\u201c\u79fb\u9664\u672c\u5730\u5934\u50cf\u201d\u590d\u9009\u6846\u5e76\u66f4\u65b0\u4e2a\u4eba\u8d44\u6599\u5373\u53ef\u3002&lt;br\/>\u79fb\u9664\u672c\u5730\u5934\u50cf\u540e\uff0c\u5c06\u6062\u590d\u4f7f\u7528 Gravatar \u5934\u50cf\u3002&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;&lt;\/span><br>                        &#8216;;     <br>                } else {<br>                    if ( empty( $profileuser->simple_local_avatar ) )<br>                        echo &#8216;&lt;span class=&#8221;description&#8221;>&#8217; . __(&#8216;\u5c1a\u672a\u8bbe\u7f6e\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u5728 Gravatar.com \u7f51\u7ad9\u8bbe\u7f6e\u5934\u50cf\u3002&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;&lt;\/span>&#8217;;<br>                    else<br>                        echo &#8216;&lt;span class=&#8221;description&#8221;>&#8217; . __(&#8216;\u4f60\u6ca1\u6709\u5934\u50cf\u4e0a\u4f20\u6743\u9650\uff0c\u5982\u9700\u8981\u4fee\u6539\u672c\u5730\u5934\u50cf\uff0c\u8bf7\u8054\u7cfb\u7ad9\u70b9\u7ba1\u7406\u5458\u3002&#8217;,&#8217;simple-local-avatars&#8217;) . &#8216;&lt;\/span>&#8217;;<br>                }<br>            ?><br>            &lt;\/td><br>        &lt;\/tr><br>    &lt;\/table><br>    &lt;script type=&#8221;text\/javascript&#8221;>var form = document.getElementById(&#8216;your-profile&#8217;);form.encoding = &#8216;multipart\/form-data&#8217;;form.setAttribute(&#8216;enctype&#8217;, &#8216;multipart\/form-data&#8217;);&lt;\/script><br>    &lt;?php       <br>    }<br>        <br>    public function edit_user_profile_update( $user_id ) {<br>        if ( ! isset( $_POST[&#8216;_simple_local_avatar_nonce&#8217;] ) || ! wp_verify_nonce( $_POST[&#8216;_simple_local_avatar_nonce&#8217;], &#8216;simple_local_avatar_nonce&#8217; ) )            \/\/security<br>            return;<br>        <br>        if ( ! empty( $_FILES[&#8216;simple-local-avatar&#8217;][&#8216;name&#8217;] ) ) {<br>            $mimes = array(<br>                &#8216;jpg|jpeg|jpe&#8217; => &#8216;image\/jpeg&#8217;,<br>                &#8216;gif&#8217; => &#8216;image\/gif&#8217;,<br>                &#8216;png&#8217; => &#8216;image\/png&#8217;,<br>                &#8216;bmp&#8217; => &#8216;image\/bmp&#8217;,<br>                &#8216;tif|tiff&#8217; => &#8216;image\/tiff&#8217;<br>            );<br>        <br>            \/\/ front end (theme my profile etc) support<br>            if ( ! function_exists( &#8216;wp_handle_upload&#8217; ) )<br>                require_once( ABSPATH . &#8216;wp-admin\/includes\/file.php&#8217; );<br>        <br>            $this->avatar_delete( $user_id );    \/\/ delete old images if successful<br>        <br>            \/\/ need to be more secure since low privelege users can upload<br>            if ( strstr( $_FILES[&#8216;simple-local-avatar&#8217;][&#8216;name&#8217;], &#8216;.php&#8217; ) )<br>                wp_die(&#8216;For security reasons, the extension &#8220;.php&#8221; cannot be in your file name.&#8217;);<br>        <br>            $this->user_id_being_edited = $user_id; \/\/ make user_id known to unique_filename_callback function<br>            $avatar = wp_handle_upload( $_FILES[&#8216;simple-local-avatar&#8217;], array( &#8216;mimes&#8217; => $mimes, &#8216;test_form&#8217; => false, &#8216;unique_filename_callback&#8217; => array( $this, &#8216;unique_filename_callback&#8217; ) ) );<br>        <br>            if ( empty($avatar[&#8216;file&#8217;]) ) {     \/\/ handle failures<br>                switch ( $avatar[&#8216;error&#8217;] ) {<br>                    case &#8216;File type does not meet security guidelines. Try another.&#8217; :<br>                        add_action( &#8216;user_profile_update_errors&#8217;, create_function(&#8216;$a&#8217;,&#8217;$a->add(&#8220;avatar_error&#8221;,__(&#8220;\u8bf7\u4e0a\u4f20\u6709\u6548\u7684\u56fe\u7247\u6587\u4ef6\u3002&#8221;,&#8221;simple-local-avatars&#8221;));&#8217;) );              <br>                        break;<br>                    default :<br>                        add_action( &#8216;user_profile_update_errors&#8217;, create_function(&#8216;$a&#8217;,&#8217;$a->add(&#8220;avatar_error&#8221;,&#8221;&lt;strong>&#8221;.__(&#8220;\u4e0a\u4f20\u5934\u50cf\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a&#8221;,&#8221;simple-local-avatars&#8221;).&#8221;&lt;\/strong> &#8216; . esc_attr( $avatar[&#8216;error&#8217;] ) . &#8216;&#8221;);&#8217;) );<br>                }<br>        <br>                return;<br>            }<br>        <br>            update_user_meta( $user_id, &#8216;simple_local_avatar&#8217;, array( &#8216;full&#8217; => $avatar[&#8216;url&#8217;] ) );      \/\/ save user information (overwriting old)<br>        } elseif ( ! empty( $_POST[&#8216;simple-local-avatar-erase&#8217;] ) ) {<br>            $this->avatar_delete( $user_id );<br>        }<br>    }<br>        <br>    \/**<br>     * remove the custom get_avatar hook for the default avatar list output on options-discussion.php<br>     *\/<br>    public function avatar_defaults( $avatar_defaults ) {<br>        remove_action( &#8216;get_avatar&#8217;, array( $this, &#8216;get_avatar&#8217; ) );<br>        return $avatar_defaults;<br>    }<br>        <br>    \/**<br>     * delete avatars based on user_id<br>     *\/<br>    public function avatar_delete( $user_id ) {<br>        $old_avatars = get_user_meta( $user_id, &#8216;simple_local_avatar&#8217;, true );<br>        $upload_path = wp_upload_dir();<br>        <br>        if ( is_array($old_avatars) ) {<br>            foreach ($old_avatars as $old_avatar ) {<br>                $old_avatar_path = str_replace( $upload_path[&#8216;baseurl&#8217;], $upload_path[&#8216;basedir&#8217;], $old_avatar );<br>                @unlink( $old_avatar_path );   <br>            }<br>        }<br>        <br>        delete_user_meta( $user_id, &#8216;simple_local_avatar&#8217; );<br>    }<br>        <br>    public function unique_filename_callback( $dir, $name, $ext ) {<br>        $user = get_user_by( &#8216;id&#8217;, (int) $this->user_id_being_edited );<br>        $name = $base_name = sanitize_file_name( substr(md5($user->user_login),0,12) . &#8216;_avatar&#8217; );<br>        $number = 1;<br>        <br>        while ( file_exists( $dir . &#8220;\/$name$ext&#8221; ) ) {<br>            $name = $base_name . &#8216;_&#8217; . $number;<br>            $number++;<br>        }<br>        <br>        return $name . $ext;<br>    }<br>}<br>        <br>$simple_local_avatars = new Simple_Local_Avatars;<br>        <br>function get_simple_local_avatar( $id_or_email, $size = &#8217;96&#8217;, $default = &#8221;, $alt = false ) {<br>    global $simple_local_avatars;<br>    $avatar = $simple_local_avatars->get_avatar( &#8221;, $id_or_email, $size, $default, $alt );<br>        <br>    if ( empty ( $avatar ) )<br>        $avatar = get_avatar( $id_or_email, $size, $default, $alt );<br>        <br>    return $avatar;<br>}<br>\/\/\u81ea\u5b9a\u4e49\u5934\u50cf \u7ed3\u675f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u52a0\u5165\u5230\u4f60\u4e3b\u9898\u7684Functions.php \u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5237\u65b0\u9875\u9762\u3002\u8fdb\u5165\u7528\u6237\u8d44\u6599\u9875\uff0c\u6ed1\u5230\u5e95\u90e8\uff0c\u70b9\u51fb\u4e0a\u4f20\u5934\u50cf\u5373\u53ef \/\/\u81ea\u5b9a\u4e49\u5934\u50cf class Simple_Local_Avatars { private $user_id_being_edited; public function __construct() { add_filter( &#8216;get_avatar&#8217;, array( $this, &#8216;get_avatar&#8217; ), 10, 5 ); add_action( &#8216;admin_init&#8217;, array( $this, &#8216;admin_init&#8217; ) ); add_action( &#8216;show_user_profile&#8217;,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":0,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}