web-dev-qa-db-fra.com

Ecrire un plugin qui informe mes amis du nouveau post qui les mentionne (@)

Par exemple, j'écris un nouveau message avec le contenu "Aujourd'hui, je suis heureux, car j'ai enfin rencontré la fille que @David m'a racontée à propos de bla blah ..." Une fois le message publié, une notification par courrier électronique devrait être envoyée à David. à propos de ce post qui le mentionne. Est-ce qu'il y a déjà un tel plugin? Sinon, aidez-moi avec ma mise en œuvre. Merci mille fois! :)

Le script doit d'abord analyser le contenu de ce nouveau message. Recherchez les noms après le '@', puis allez dans la base de données pour faire correspondre le nom du commentateur afin d'obtenir son adresse e-mail (en supposant que la personne que je @ doive commenter sur mon blog auparavant)

La partie email de base d'envoi est ci-dessous.

function email_friend()  {

    $postTitle = get_the_title($id);
    $post_permalink = get_permalink( $id );
    $to = '[email protected]';
    $subject = 'Arch!tect mentioned you in his new post《'.$postTitle .'》';
    $from = "[email protected]";
    $headers = "From:" . $from;

    $message = "Arch!tect mentioned you in his new post《".$postTitle . 
    "》 check it out?\n\n"  ."Post link:".$post_permalink
    ."\n\n\nPlease don't reply this email.\r\n";

    mail($to, $subject, $message, $headers);
}

add_action ( 'publish post', 'email_friend' );

Fait avec une partie simple!

Maintenant, aidez-moi avec la partie difficile ... en analysant le nom dans post et en récupérant l'adresse e-mail dans la base de données.


P.S.

Merci à tous pour votre aide! J'ai réussi à finir le code et ça fonctionne bien maintenant (testé). Pour mentionner les personnes dont le nom contient de l’espace, j’utilise d’abord le trait de soulignement, puis le change en espace. Il envoie maintenant un courrier électronique à toutes les personnes que je mentionne dans mon message et après cela, il m'enverra un résumé avec le résultat. Je vais coller le code complet ci-dessous s'il vous plaît jeter un oeil et dites-moi ce que je dois améliorer.

function email_friend()  {

    // get post object
    $post = get_post($id);
    // get post content
    $content = $post->post_content;
    // get how many people is mentioned in this post
    //$mentionCount = preg_match_all('/(@(\w)+)/', $content, $matches);
    $mentionCount = preg_match_all('/(@[^\s]+)/', $content, $matches);//support other lang


    // if there is at least one @ with a name after it
    if (0 !== $mentionCount) {

        $friendList = array();//for storing correct names

        for ($mentionIndex=0; $mentionIndex < $mentionCount; $mentionIndex++) {

            $mentionName = $matches[0][$mentionIndex];  

            $mentionName = str_replace('_',' ',$mentionName); //change _ back to space

            $mentionName = substr($mentionName, 1); //get rid of @
            //for security and add wildcard
            $friend_display_name_like = '%' . like_escape($mentionName) . '%'; 

            global $wpdb;

            // get correct name first
            $friendCorrectName = $wpdb->get_var( $wpdb->prepare( "

                                                           SELECT comment_author
                                                           FROM $wpdb->comments
                                                           WHERE comment_author
                                                           LIKE %s ",
                                                           $friend_display_name_like
                                                           )) ;

            // get friend email by comment author name
            $friend_email = $wpdb->get_var( $wpdb->prepare( "

                                                           SELECT comment_author_email
                                                           FROM $wpdb->comments
                                                           WHERE comment_author
                                                           LIKE %s ",
                                                           $friendCorrectName
                                                           )) ;


            if($friend_email) {// if found email address then email

                $postTitle = get_the_title($id);
                $post_permalink = get_permalink( $id );
                $to =   $friend_email;
                $subject =   'Arch!tect mentioned you in his new post 《'.$postTitle . 

                '》';

                $from = "[email protected]";

                $headers = "From:" . $from;

                $message = "Arch!tect mentioned you in his new post《".$postTitle . 
                "》 check it out?\n\n"  ."Post link:".$post_permalink
                ."\n\n\nPlease don't reply this email.\r\n";

                if(mail($to, $subject, $message, $headers)) {
                    //if send successfully put his/her name in my list
                    array_Push($friendList, $friendCorrectName);
                   //array_Push($friendList, $friend_email);
                }


            } 



        } 
 $comma_separated = implode(",", $friendList); //friend list array to string 

        // now send an email to myself about the result
        $postTitle = get_the_title($id);
        $post_permalink = get_permalink( $id );
        $to =    '[email protected]';
        $subject =   "Your new post《".$postTitle . 
        "》has notified ".count($friendList)."friends successfully";
        $from = "[email protected]";
        $headers = "From:" . $from;
        //list all friends that received my email
        $message = "Your new post《".$postTitle . 
        "》has notified ".count($friendList)."friends successfully:\n\n".$comma_separated ;

        mail($to, $subject, $message, $headers);


    }





}//end of email_friend function


add_action ( 'publish_post', 'email_friend' );

Edit: changé (\ w) + en [^\s] + afin qu'il prenne en charge plus de langues. (Testé en chinois)

5
Arch1tect

Édité deux fois selon les suggestions de Kaiser, version non sécurisée supprimée.

<?php
function my_email_friend($post_id = '') {
    // get post object
    $post = get_post($post_id);
    // get post content
    $content = $post->post_content;
    // if @David exists
    if( 0 !== preg_match('/(@\w)/', $content, $matches) )
        $friend_display_name_like = '%' . like_escape($matches[0]) . '%';
    else
        return; // do nothing if no matches
    global $wpdb;
    // get friend email by 'display_name'
    $friend_email = $wpdb->get_var( $wpdb->prepare( "
        SELECT user_email
        FROM $wpdb->users
        WHERE display_name
        LIKE %s ",
        $friend_display_name_like
    )) ;
    if($friend_email) {
        /* Your  code here, 'mail()' can use '$friend_email' */
    }
}
add_action('publish_post', 'my_email_friend');

Utilisez-le uniquement comme point de départ car:

  • le code est partiellement testé (lire: non testé);
  • vous pouvez mentionner de nombreux amis dans le post, le premier mentionné sera notifié (pourrait être amélioré);
  • vous pouvez avoir quelque chose comme [email protected] dans le contenu du message. Le script va essayer de trouver l'utilisateur 'exemple';
  • un seul John de John Doe et John Roe sera envoyé par courrier électronique.

Tout pour être amélioré, mais semble prêt à tester.

3
Max Yudin