PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.2
Customize WordPress Emails and Alerts – Better Notifications for WP v1.2
1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.8 1.8.1 1.8.10 1.8.11 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 81 releases
bnfw / bnfw.php

bnfw.php in Customize WordPress Emails and Alerts – Better Notifications for WP 1.2, at bnfw.php

388 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Better Notifications for WordPress
4 * Plugin URI: http://wordpress.org/plugins/bnfw/
5 * Description: Send customisable HTML emails to your users for different WordPress notifications.
6 * Version: 1.2
7 * Author: Voltronik
8 * Author URI: http://www.voltronik.co.uk/
9 * Author Email: plugins@voltronik.co.uk
10 * License: GPLv2 or later
11 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
12 * Text Domain: bnfw
13 * Domain Path: languages/
14 */
15
16 /**
17 * Copyright © 2014 Voltronik (plugins@voltronik.co.uk)
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License, version 2, as
20 * published by the Free Software Foundation.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29 class BNFW {
30
31 /**
32 * Constructor.
33 *
34 * @since 1.0
35 */
36 function __construct() {
37 $this->load_textdomain();
38 $this->includes();
39 $this->hooks();
40
41 $this->notifier = new BNFW_Notification;
42 $this->engine = new BNFW_Engine;
43 }
44
45 /**
46 * Factory method to return the instance of the class.
47 *
48 * Makes sure that only one instance is created.
49 *
50 * @return object Instance of the class
51 */
52 public static function factory() {
53 static $instance = false;
54 if ( ! $instance ) {
55 $instance = new self();
56 }
57 return $instance;
58 }
59
60 /**
61 * Loads the plugin language files
62 *
63 * @since 1.0
64 */
65 public function load_textdomain() {
66 // Load localization domain
67 $this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
68 load_plugin_textdomain( 'bnfw', false, $this->translations );
69 }
70
71 /**
72 * Include required files.
73 *
74 * @since 1.0
75 */
76 public function includes() {
77 // Load Engine and post type
78 require_once 'includes/engine/class-bnfw-engine.php';
79 require_once 'includes/admin/class-bnfw-notification.php';
80 require_once 'includes/overrides.php';
81
82 // Load Admin Pages
83 if ( is_admin() ) {
84 require_once 'includes/admin/bnfw-settings.php';
85 }
86 }
87
88 /**
89 * Register Hooks.
90 *
91 * @since 1.0
92 */
93 public function hooks() {
94 register_activation_hook( __FILE__ , array( $this, 'activate' ) );
95
96 add_action( 'draft_to_publish' , array( $this, 'publish_post' ) );
97 add_action( 'publish_to_publish' , array( $this, 'update_post' ) );
98 add_action( 'init' , array( $this, 'custom_post_type_hooks' ), 100 );
99 add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
100
101 add_action( 'comment_post' , array( $this, 'comment_post' ) );
102 add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
103 add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
104
105 add_action( 'user_register' , array( $this, 'user_register' ) );
106 add_action( 'user_register' , array( $this, 'welcome_email' ) );
107
108 add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
109 add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
110 add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
111 add_filter( 'wp_mail_content_type' , array( $this, 'change_email_content_type' ) );
112
113 add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
114 }
115
116 /**
117 * Setup hooks for custom post types.
118 *
119 * @since 1.2
120 */
121 function custom_post_type_hooks() {
122 $post_types = get_post_types( array( 'public' => true ), 'names' );
123 $post_types = array_diff( $post_types, array( BNFW_Notification::POST_TYPE ) );
124
125 foreach ( $post_types as $post_type ) {
126 add_action( 'pending_' . $post_type, array( $this, 'on_post_pending' ), 10, 2 );
127 add_action( 'future_' . $post_type, array( $this, 'on_post_scheduled' ), 10, 2 );
128 }
129 }
130
131 /**
132 * importer
133 */
134 public function activate() {
135 require_once dirname( __FILE__ ) . '/includes/import.php';
136 $importer = new BNFW_Import;
137 $importer->import();
138 }
139
140 /**
141 * Add 'Settings' link below BNFW in Plugins list.
142 *
143 * @since 1.0
144 * @param unknown $links
145 * @param unknown $file
146 * @return unknown
147 */
148 public function plugin_action_links( $links, $file ) {
149 $plugin_file = 'bnfw/bnfw.php';
150 if ( $file == $plugin_file ) {
151 $settings_link = '<a href="' . admin_url( 'admin.php?page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
152 array_unshift( $links, $settings_link );
153 }
154 return $links;
155 }
156
157 /**
158 * When a new term is created.
159 *
160 * @since 1.0
161 * @param int $term_id
162 * @param int $tt_id
163 * @param string $taxonomy
164 */
165 public function create_term( $term_id, $tt_id, $taxonomy ) {
166 $this->send_notification( 'newterm-' . $taxonomy, $term_id );
167 }
168
169 /**
170 * Fires when a post is created for the first time.
171 *
172 * @since 1.0
173 * @param unknown $post
174 */
175 function publish_post( $post ) {
176 $post_id = $post->ID;
177 $post_type = $post->post_type;
178
179 if ( BNFW_Notification::POST_TYPE != $post_type ) {
180 $this->send_notification( 'new-' . $post_type, $post_id );
181 }
182 }
183
184 /**
185 * Fires when a post is updated.
186 *
187 * @since 1.0
188 * @param unknown $post
189 */
190 function update_post( $post ) {
191 $post_id = $post->ID;
192 $post_type = $post->post_type;
193
194 if ( BNFW_Notification::POST_TYPE != $post_type ) {
195 $this->send_notification( 'update-' . $post_type, $post_id );
196 }
197 }
198
199 /**
200 * Fires when a post is pending for review.
201 *
202 * @since 1.1
203 * @param int $post_id Post ID
204 * @param object $post Post object
205 */
206 function on_post_pending( $post_id, $post ) {
207 $post_type = $post->post_type;
208
209 if ( BNFW_Notification::POST_TYPE != $post_type ) {
210 $this->send_notification( 'pending-' . $post_type, $post_id );
211 }
212 }
213
214 /**
215 * Fires when a post is scheduled.
216 *
217 * @since 1.1.5
218 * @param int $post_id Post ID
219 * @param object $post Post object
220 */
221 function on_post_scheduled( $post_id, $post ) {
222 $post_type = $post->post_type;
223
224 if ( BNFW_Notification::POST_TYPE != $post_type ) {
225 $this->send_notification( 'future-' . $post_type, $post_id );
226 }
227 }
228
229 /**
230 * Send notification for new comments
231 *
232 * @since 1.0
233 * @param int $comment_id
234 */
235 function comment_post( $comment_id ) {
236 $the_comment = get_comment( $comment_id );
237 if ( $this->can_send_comment_notification( $the_comment ) ) {
238 $post = get_post( $the_comment->comment_post_ID );
239 $notification_type = 'new-comment'; // old notification name
240 if ( 'post' != $post->post_type ) {
241 $notification_type = 'comment-' . $post->post_type;
242 }
243 $this->send_notification( $notification_type, $comment_id );
244 }
245 }
246
247 /**
248 * Send notification for new trackback
249 *
250 * @since 1.0
251 * @param unknown $comment_id
252 */
253 function trackback_post( $comment_id ) {
254 $the_comment = get_comment( $comment_id );
255 if ( $this->can_send_comment_notification( $the_comment ) ) {
256 $this->send_notification( 'new-trackback', $comment_id );
257 }
258 }
259
260 /**
261 * Send notification for new pingbacks
262 *
263 * @since 1.0
264 * @param unknown $comment_id
265 */
266 function pingback_post( $comment_id ) {
267 $the_comment = get_comment( $comment_id );
268 if ( $this->can_send_comment_notification( $the_comment ) ) {
269 $this->send_notification( 'new-pingback', $comment_id );
270 }
271 }
272
273 /**
274 * Send notification for lost password.
275 *
276 * @since 1.0
277 */
278 function on_lost_password() {
279 $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
280 if ( $user ) {
281 $this->send_notification( 'admin-password', $user->ID );
282 }
283 }
284
285 /**
286 * Change the title of the password reset email that is sent to the user.
287 *
288 * @since 1.1
289 */
290 public function change_password_email_title( $title ) {
291 $notifications = $this->notifier->get_notifications( 'user-password' );
292 if ( count( $notifications ) > 0 ) {
293 // Ideally there should be only one notification for this type.
294 // If there are multiple notification then we will read data about only the last one
295 $setting = $this->notifier->read_settings( end( $notifications )->ID );
296 return $setting['subject'];
297 }
298
299 return $title;
300 }
301
302 /**
303 * Change the message of the password reset email.
304 *
305 * @since 1.1
306 */
307 public function change_password_email_message( $message, $key, $user_login, $user_data ) {
308 $notifications = $this->notifier->get_notifications( 'user-password' );
309 if ( count( $notifications ) > 0 ) {
310 // Ideally there should be only one notification for this type.
311 // If there are multiple notification then we will read data about only the last one
312 $setting = $this->notifier->read_settings( end( $notifications )->ID );
313
314 return $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
315 }
316
317 return $message;
318 }
319
320 /**
321 * Change the email content type to HTML.
322 *
323 * @since 1.2
324 */
325 public function change_email_content_type() {
326 return 'text/html';
327 }
328
329 /**
330 * Send notification for new uses.
331 *
332 * @since 1.0
333 * @param unknown $user_id
334 */
335 function user_register( $user_id ) {
336 $this->send_notification( 'admin-user', $user_id );
337 }
338
339 /**
340 * New User - Welcome email
341 *
342 * @since 1.1
343 * @param int $user_id New user id
344 */
345 function welcome_email( $user_id ) {
346 $notifications = $this->notifier->get_notifications( 'welcome-email' );
347 foreach ( $notifications as $notification ) {
348 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
349 }
350 }
351
352 /**
353 * Send notification based on type and ref id
354 *
355 * @access private
356 * @since 1.0
357 * @param unknown $type
358 * @param unknown $ref_id
359 */
360 private function send_notification( $type, $ref_id ) {
361 $notifications = $this->notifier->get_notifications( $type );
362 foreach ( $notifications as $notification ) {
363 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
364 }
365 }
366
367 /**
368 * Can send comment notification or not
369 *
370 * @since 1.0
371 * @param unknown $comment
372 * @return unknown
373 */
374 private function can_send_comment_notification( $comment ) {
375 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
376 $suppress_spam = get_option( 'bnfw_suppress_spam' );
377 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
378 return false;
379 }
380 return true;
381 }
382 }
383
384 /* ------------------------------------------------------------------------ *
385 * Fire up the plugin
386 * ------------------------------------------------------------------------ */
387 BNFW::factory();
388