WordPress.org

Forums

WP Post Notifier For All
I fixed it (4 posts)

  1. ExpIorer
    Member
    Posted 4 years ago #

    In my case it was only the mailing part.
    Everything else worked fine.
    * I added the $mailTo variable (it was empty)
    * changed function mail to wp_mail
    * modified text part and headers to adapt new settings (optional)

    now my php looks like this:

    <?php
    /*
    Plugin Name: WP Post Notifier For All
    Description: Notify all WordPress users (and not only the admin) on post publishing. The notification is sent only one time after the first post publishing(not on every update).
    Version: 1.0
    Author: Fayēal Tirich
    */
    
    $pnfa_from_tpl = "Name <ilove@gmail.com>";
    
    $pnfa_subject_tpl = "[BLOG_NAME] - [AUTHOR] just published a new article: [TITLE]";
    
    $pnfa_body_tpl = <<<EOD
    <center>[LOGO]http://www.example.org/logo.png[/LOGO]</center><br />
    [AUTHOR] just published a new article !<br /><br />
    <h3>[TITLE]</h3>
    [LINK]<br /><br />
    Good reading !<br /><br />
    EOD;
    
    $pnfa_otions_msg = '';
    
    function pnfa_get_users () {
        $blog_users = array();
        global $wpdb;
        $users = $wpdb->get_results("SELECT ID, user_login, display_name, user_email  FROM $wpdb->users");
        foreach($users as $user) {
            $object = new stdClass();
            $object->ID = $user->ID;
            $object->user_login = $user->user_login;
            $object->display_name = $user->display_name;
            $object->user_email = $user->user_email;
            $blog_users[$user->ID]=$object;
        }
        return $blog_users;
    }
    
    if ( isset($_POST['pnfa_submit']) ) {
        update_option('fay-post-notifier-for-all_from-tpl', stripslashes_deep(trim($_POST['pnfa_from'])));
        update_option('fay-post-notifier-for-all_subject-tpl', stripslashes_deep(trim($_POST['pnfa_subject'])));
        update_option('fay-post-notifier-for-all_body-tpl', stripslashes_deep(trim($_POST['pnfa_body'])));
        $pnfa_otions_msg = '<span style="color:green">'.__('Options updated').'</span><br />';
    }
    
    $pnfa_from = get_option('fay-post-notifier-for-all_from-tpl');
    if (!isset($pnfa_from)  || empty($pnfa_from)) {
            $pnfa_from = $pnfa_from_tpl ;
    }
    
    $pnfa_subject = get_option('fay-post-notifier-for-all_subject-tpl');
    if (!isset($pnfa_subject)  || empty($pnfa_subject)) {
            $pnfa_subject = $pnfa_subject_tpl ;
    }
    
    $pnfa_body = get_option('fay-post-notifier-for-all_body-tpl');
    if (!isset($pnfa_body)  || empty($pnfa_body)) {
        $pnfa_body = $pnfa_body_tpl ;
    }
    
    function pnfa_notify_users($post_ID) {
    
        global $pnfa_from, $pnfa_subject, $pnfa_body;
        $process = 0;
        $post = get_post($post_ID);
    
        //check if the post was already notified
        $options =  get_option('fay-notifay-all_notified-posts');
        if (!is_array($options)) {
            $options = array ();
            update_option('fay-notifay-all_notified-posts', $options);
            $process = 1;
        } else {
            if (in_array($post_ID, $options)) {
                $process = 0;
            } else {
                $process = 1;
            }
        }
        if ($process==1)
        {
            //only notify for new posts
            $activation_date = get_option('fay-post-notifier_first-activation-date');
            if(strtotime($post->post_date)<strtotime($activation_date)) {
                $process = 0;
            }
        }
    
        $author = get_the_author_meta('display_name',$post->post_author);
    
        if ($process == 1) {
            global $wpdb;
            $users = $wpdb->get_results("SELECT ID, user_email FROM $wpdb->users");
            $emails = '';
            foreach($users as $user) {
                if (get_usermeta($user->ID,'pnfa_exclude')!='true') {
                    $emails = $user->user_email.', '.$emails ;
                }
            }
    
            $pnfa_subject = str_replace('[AUTHOR]', htmlspecialchars_decode($author), $pnfa_subject);
            $pnfa_subject = str_replace('[BLOG_NAME]',html_entity_decode(get_bloginfo('name'), ENT_QUOTES), $pnfa_subject);
            $pnfa_subject = str_replace('[TITLE]',  htmlspecialchars_decode($post->post_title), $pnfa_subject);
    
            $pattern = '/(\[LOGO\])(.*)(\[\/LOGO\])/';
            $replacement = '<img src="${2}" alt="'. htmlspecialchars_decode(get_bloginfo('name')).'"/>';
            $pnfa_body = preg_replace($pattern, $replacement, $pnfa_body);
    
            $pnfa_body = str_replace('[AUTHOR]', htmlspecialchars_decode($author), $pnfa_body);
    
            $link = '<a style="color: #2D83D5" href="'.get_permalink($post_ID).'">'.get_permalink($post_ID).'</a>';
            $pnfa_body = str_replace('[LINK]', $link, $pnfa_body);
    
    	    $pnfa_body = str_replace('[TITLE]', htmlspecialchars_decode($post->post_title), $pnfa_body);
    
            $headers = "From: $pnfa_from\n";
        	$headers .= "Bcc: $emails\n";
        	$headers .= 'Content-Type: ' . get_bloginfo('html_type') . '; charset=' . get_bloginfo('charset') . "\r\n";
    
            $message .= "<html>\n";
            $message .= "<body style=\"font-family:Verdana, Verdana, Geneva, sans-serif; font-size:12px; color:#666666;\">\n";
            $message .= $pnfa_body;
            $message .= "\n\n";
            $message .= "</body>\n";
            $message .= "</html>\n";
            $mailTo = 'all@wordpress';
            if (wp_mail($mailTo, $pnfa_subject, $message, $headers)) {
                $options[] = $post_ID;
                sort($options);
                update_option('fay-notifay-all_notified-posts', $options);
            }
        }
        return $post_ID;
    }
    
    // Options page
    function pnfa_options() {
        global $pnfa_from_tpl, $pnfa_from, $pnfa_body_tpl, $pnfa_subject_tpl, $pnfa_body, $pnfa_subject, $pnfa_otions_msg ;
        if ( isset($_POST['pnfa_exclude_submit']) ) {
            $users_to_exclude_array = array();
            if(isset($_POST['pnfa_excluded_users'])) {
                $users_to_exclude_array = $_POST['pnfa_excluded_users'];
            }
            $users = pnfa_get_users();
            $log = '';
            foreach($users as $user) {
                if (in_array($user->ID, $users_to_exclude_array)) {
                    if (get_usermeta($user->ID,'pnfa_exclude')!='true'){
                        update_usermeta($user->ID,'pnfa_exclude','true');
                        $log = $log .'<span style="color:green">'.$user->display_name.' excluded</span><br />';
                    }
                } else {
                    if (get_usermeta($user->ID,'pnfa_exclude')!='false'){
                        update_usermeta($user->ID,'pnfa_exclude','false');
                        $log = $log .'<span style="color:green">'.$user->display_name.' will be notified</span><br />';
                    }
                }
            }
            if ($log!=''){
                $pnfa_otions_msg = $log;
            }
        }
        if(!empty($pnfa_otions_msg)) {
    ?>
        <!-- Last Action --><div id="message" class="updated fade"><p><?php echo $pnfa_otions_msg; ?></p></div>
    <?php
    }
    ?>
    <div class="wrap">
        <?php screen_icon(); ?>
        <h2>Post Notifier For All</h2>
        <br />
        <form method="post" action="">
            <div>
                <div>
                    <label for="pnfa_from"><strong>Email "From" Template</strong></label>
                    <br />
                    <input type="text" size="150" id="pnfa_from" name="pnfa_from" value="<?php echo $pnfa_from; ?>" />
                    <p>
                        Default "From" Template:<br />
                        <?php
                            $temp = $pnfa_from_tpl ;
                            $temp = str_replace("<","<",$temp);
                            $temp = str_replace(">",">",$temp);
                            echo nl2br($temp);
                        ?>
                    </p>
                </div>
                <div>
                    <label for="pnfa_subject"><strong>Email "Subject" Template</strong></label>
                    <br />
                    <input type="text" size="150" id="pnfa_subject" name="pnfa_subject" value="<?php echo $pnfa_subject; ?>" />
                    <p>
                        Default "Subject" Template:<br />
                        <?php echo $pnfa_subject_tpl; ?>
                    </p>
                </div>
                <div>
                    <label for="pnfa_body"><strong>Email "Body" Template</strong></label>
                    <br />
                    <textarea style="width: 90%; font-size: 12px;" rows="8" cols="60" id="pnfa_body" name="pnfa_body"><?php echo $pnfa_body; ?></textarea>
                    <p>
                        Default "Body" Template:<br />
                        <?php
                            $temp = $pnfa_body_tpl ;
                            $temp = str_replace("<","<",$temp);
                            $temp = str_replace(">",">",$temp);
                            echo nl2br($temp);
                        ?>
                    </p>
                </div>
                <p class="submit">
                    <input class="button-primary" type="submit" name="pnfa_submit" class="button" value="<?php _e('Save Changes'); ?>" />
                </p>
            </div>
        </form>
        <br />
        <h2>Exclude users</h2>
        <form method="post" action="">
                <table class="widefat fixed" cellspacing="0">
                <thead>
                <tr class="thead">
                    <th id="cb" class="manage-column column-cb column-exclude" style="" scope="col">
                        <?php echo __('Exclude'); ?>?
                    </th>
                    <th id="username" class="manage-column column-username" style="" scope="col">
                       <?php echo __('Username'); ?>
                    </th>
                    <th id="email" class="manage-column column-email" style="" scope="col">
                        <?php echo __('Email'); ?>
                    </th>
                </tr>
                </thead>
    
                <tfoot>
                <tr class="thead">
                    <th id="cb" class="manage-column column-cb column-exclude" style="" scope="col">
                        <?php echo __('Exclude'); ?>?
                    </th>
                    <th id="username" class="manage-column column-username" style="" scope="col">
                       <?php echo __('Username'); ?>
                    </th>
                    <th id="email" class="manage-column column-email" style="" scope="col">
                        <?php echo __('Email'); ?>
                    </th>
                </tr>
                </tfoot>
    
                <tbody id="users" class="list:user user-list">
                <?php
                $style = '';
                $users = pnfa_get_users();
                foreach($users as $user) {
                    $is_checked = false ;
                    if (get_usermeta($user->ID,'pnfa_exclude')=='true') {
                        $is_checked = true;
                    }
                    $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
                    ?>
                    <tr id='user-<?php echo $user->ID; ?>' <?php echo $style; ?> <?php echo ($is_checked)?'style="background-color: rgb(255, 153, 153);"':''; ?>>
                    <th scope='row' class='check-column'><input type='checkbox'  name='pnfa_excluded_users[]' id='user_<?php echo $user->ID; ?>' <?php echo ($is_checked)?"checked":""; ?>  value='<?php echo $user->ID; ?>' /></th>
                    <td><?php echo $user->user_login; ?></td>
                    <td><?php echo $user->user_email; ?></td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
                </table>
                <p class="submit">
                    <input class="button-primary" type="submit" name="pnfa_exclude_submit" class="button" value="<?php _e('Save Changes'); ?>" />
                </p>
            </form>
    </div>
    <?php
    }
    
    function pnfa_user_options() {
            $text = '';
            global $user_ID;
            get_currentuserinfo();
            if ( isset($_POST['pnfa_user_submit']) ) {
                    if(isset($_POST['pnfa_user_active']) && $_POST['pnfa_user_active']=='true') {
                        if (get_usermeta($user_ID,'pnfa_exclude')!='true'){
                            update_usermeta($user_ID,'pnfa_exclude','true');
                        }
                    } else {
                        if (get_usermeta($user_ID,'pnfa_exclude')!='false'){
                            update_usermeta($user_ID,'pnfa_exclude','false');
                        }
                    }
                    $text = '<span style="color:green">'.__('Option updated').'</span><br />';
            }
            if(!empty($text)) { echo '<!-- Last Action --><div id="message" class="updated fade"><p>'.$text.'</p></div>'; }
            ?>
            <div class="wrap">
            <?php screen_icon(); ?>
            <h2>Rakstu ziņojumi</h2>
            <br /><br />
            <form method="post" action="">
                <table class="widefat">
                    <thead>
                        <tr>
                            <th>Atslēgt e-pasta ziņojumus par jauniem rakstiem</th>
                        </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>
                            <input type="checkbox" name="pnfa_user_active" value="true" <?php if(get_usermeta($user_ID,'pnfa_exclude')=='true') echo ' checked="checked"'; ?> />&nbsp;<?php _e('Atķeksējiet, lai nesaņemtu e-pasta ziņojumus'); ?>
                            <p class="submit">
                                    <input class="button-primary" type="submit" name="pnfa_user_submit" class="button" value="<?php _e('Save Changes'); ?>" />
                            </p>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </form>
            </div>
        <?php
        }
    
    function pnfa_activation() {
    	$first_date = get_option('fay-post-notifier_first-activation-date');
    	if (!isset($first_date) || empty($first_date))
    	{
    		update_option('fay-post-notifier_first-activation-date', date("Y-m-d H:m:s"));
    	}
    }
    register_activation_hook( __FILE__, 'pnfa_activation' );
    
    function pnfa_menu() {
        if (function_exists('add_options_page')) {
            if( current_user_can('manage_options') ) {
                add_options_page(__('Post Notifier'), __('Post Notifier'), 'manage_options', __FILE__, 'pnfa_options') ;
            }
        }
         if (function_exists('add_submenu_page')) {
            add_submenu_page('users.php', __('Post Notifier'), __('Post Notifier'), 'read', __FILE__, pnfa_user_options);
         }
    }
    
    add_action('admin_menu', 'pnfa_menu');
    
    add_action('publish_post', 'pnfa_notify_users' );
    
    ?>

    http://wordpress.org/extend/plugins/wp-post-notifier-for-all/

  2. Mugsy
    Member
    Posted 4 years ago #

    Thanks, but you didn't explain WHAT exactly your "fix" corrects.

    Because it still doesn't work for me.

  3. jauphrwa
    Member
    Posted 4 years ago #

    Hi guys,

    I quickly translated the "gui" in French.
    J'ai traduit vite fait l'interface utilisateur et admin en français.

    edit : I made a new topic for this

  4. Fayal Tirich
    Member
    Plugin Author

    Posted 3 years ago #

    Sorry for being late, a new version is now available
    http://wordpress.org/extend/plugins/wp-post-notifier-for-all/

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic