├── inc └── widget-snpe.php ├── js └── snpe-widget.js ├── readme.txt └── simple-new-post-emails.php /inc/widget-snpe.php: -------------------------------------------------------------------------------- 1 | 'snpe-widget', 'description' => 'Allow a user to sign up for simple new post emails.' ); 8 | parent::__construct( 'snpe-widget', 'Simple New Post Emails', $widget_ops ); 9 | } 10 | 11 | public function widget( $args, $instance ) { 12 | // Currently only on for logged in users 13 | if ( ! is_user_logged_in() ) { 14 | return; 15 | } 16 | 17 | wp_enqueue_script( 'snpe-widget', plugins_url( 'js/snpe-widget.js', dirname( __FILE__ ) ), array( 'jquery' ), '0.1', true ); 18 | 19 | wp_localize_script( 'snpe-widget', 'snpe_vars', array( 'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ) ) ); 20 | 21 | $user = wp_get_current_user(); 22 | 23 | $title = empty( $instance['title'] ) ? 'New Post Emails' : $instance['title']; 24 | 25 | echo $args['before_widget']; 26 | echo $args['before_title'] . $title . $args['after_title']; 27 | ?> 28 |
40 | '' ) ); 52 | $title = strip_tags( $instance['title'] ); 53 | ?> 54 |55 | 56 | 57 |
58 | 65 |Posted by ' . get_the_author_meta( 'display_name', $post->post_author ) . '.
'; 131 | $message .= 'View the post and comments at ' . get_permalink( $post_id ) . '.
'; 132 | $message .= ''; 133 | 134 | // Get the users who want emails 135 | $users = get_users( array( 'meta_key' => 'snpe_send' ) ); 136 | 137 | $subject = apply_filters( 'snpe_subject', $subject, $post ); 138 | $message = apply_filters( 'snpe_message', $message, $post ); 139 | $users = apply_filters( 'snpe_users', $users, $post ); 140 | 141 | if ( empty( $users ) ) { 142 | return; 143 | } 144 | 145 | $sent = $this->send_mail( $users, $subject, $message, $post ); 146 | 147 | if ( $sent ) { 148 | update_post_meta( $post_id, 'snpe_sent', 'Y' ); 149 | } 150 | } 151 | 152 | private function send_mail( $users, $subject, $message, $post ) { 153 | // BCC all users specified 154 | $headers = array(); 155 | 156 | foreach ( (array) $users as $user ) { 157 | $headers[] = 'Bcc: ' . $user->user_email; 158 | } 159 | 160 | $headers[] = "Content-Type: text/html; charset=utf-8\r\n"; 161 | $headers = apply_filters( 'snpe_headers', $headers, $post, $users ); 162 | 163 | // Get the site domain and get rid of www. 164 | // Borrowed from wp-includes/pluggable.php 165 | $sitename = strtolower( $_SERVER['SERVER_NAME'] ); 166 | if ( substr( $sitename, 0, 4 ) == 'www.' ) { 167 | $sitename = substr( $sitename, 4 ); 168 | } 169 | 170 | $to = apply_filters( 'snpe_to_email', 'wordpress@' . $sitename, $post ); 171 | 172 | return wp_mail( $to, $subject, $message, $headers ); 173 | } 174 | 175 | /** 176 | * Add a column to the Users admin table indicating subscription status 177 | * @param array $columns User table columns 178 | * 179 | * @return array 180 | */ 181 | public function add_snpe_user_column( $columns ) { 182 | $columns['snpe_send'] = 'Email New Posts?'; 183 | return $columns; 184 | } 185 | 186 | /** 187 | * Return a user-friendly string based on the value of the user meta field 188 | * @param string $value Passed column value 189 | * @param string $column_name Passed column name 190 | * @param int $user_id User ID 191 | * 192 | * @return string 193 | */ 194 | public function show_snpe_user_column_content( $value, $column_name, $user_id ) { 195 | $snpe_send = get_user_meta( $user_id, 'snpe_send', true ); 196 | if ( 'snpe_send' == $column_name ) { 197 | return ( true == $snpe_send ) ? 'Yes' : 'No'; 198 | } 199 | return $value; 200 | } 201 | } 202 | 203 | $simple_new_post_emails = new Simple_New_Post_Emails(); 204 | --------------------------------------------------------------------------------