PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.1
Customize WordPress Emails and Alerts – Better Notifications for WP v1.1
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.1, at bnfw.php

353 lines 9.5 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.1
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
99 add_action( 'draft_to_pending' , array( $this, 'pending_post' ) );
100 add_action( 'new_to_pending' , array( $this, 'pending_post' ) );
101 add_action( 'auto-draft_to_pending' , array( $this, 'pending_post' ) );
102
103 add_action( 'comment_post' , array( $this, 'comment_post' ) );
104 add_action( 'trackback_post' , array( $this, 'trackback_post' ) );
105 add_action( 'pingback_post' , array( $this, 'pingback_post' ) );
106
107 add_action( 'user_register' , array( $this, 'user_register' ) );
108 add_action( 'user_register' , array( $this, 'welcome_email' ) );
109
110 add_action( 'lostpassword_post' , array( $this, 'on_lost_password' ) );
111 add_filter( 'retrieve_password_title' , array( $this, 'change_password_email_title' ) );
112 add_filter( 'retrieve_password_message' , array( $this, 'change_password_email_message' ), 10, 4 );
113
114 add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 );
115
116 add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 );
117 }
118
119 /**
120 * Run this on first-time plugin activation
121 *
122 * @since 1.0
123 */
124
125 /**
126 * importer
127 */
128 public function activate() {
129 require_once dirname( __FILE__ ) . '/includes/import.php';
130 $importer = new BNFW_Import;
131 $importer->import();
132 }
133
134 /**
135 * Add 'Settings' link below BNFW in Plugins list.
136 *
137 * @since 1.0
138 * @param unknown $links
139 * @param unknown $file
140 * @return unknown
141 */
142 public function plugin_action_links( $links, $file ) {
143 $plugin_file = 'bnfw/bnfw.php';
144 if ( $file == $plugin_file ) {
145 $settings_link = '<a href="' . admin_url( 'admin.php?page=bnfw-settings' ) . '">' . 'Settings' . '</a>';
146 array_unshift( $links, $settings_link );
147 }
148 return $links;
149 }
150
151 /**
152 * When a new term is created.
153 *
154 * @since 1.0
155 * @param int $term_id
156 * @param int $tt_id
157 * @param string $taxonomy
158 */
159 public function create_term( $term_id, $tt_id, $taxonomy ) {
160 $this->send_notification( 'new-' . $taxonomy, $term_id );
161 }
162
163 /**
164 * Fires when a post is created for the first time.
165 *
166 * @since 1.0
167 * @param unknown $post
168 */
169 function publish_post( $post ) {
170 $post_id = $post->ID;
171 $post_type = $post->post_type;
172
173 if ( BNFW_Notification::POST_TYPE != $post_type ) {
174 $this->send_notification( 'new-' . $post_type, $post_id );
175 }
176 }
177
178 /**
179 * Fires when a post is updated.
180 *
181 * @since 1.0
182 * @param unknown $post
183 */
184 function update_post( $post ) {
185 $post_id = $post->ID;
186 $post_type = $post->post_type;
187
188 if ( BNFW_Notification::POST_TYPE != $post_type ) {
189 $this->send_notification( 'update-' . $post_type, $post_id );
190 }
191 }
192
193 /**
194 * Fires when a post is pending for review.
195 *
196 * @since 1.1
197 * @param unknown $post
198 */
199 function pending_post( $post ) {
200 $post_id = $post->ID;
201 $post_type = $post->post_type;
202
203 if ( BNFW_Notification::POST_TYPE != $post_type ) {
204 $this->send_notification( 'pending-' . $post_type, $post_id );
205 }
206 }
207
208 /**
209 * Send notification for new comments
210 *
211 * @since 1.0
212 * @param unknown $comment_id
213 */
214 function comment_post( $comment_id ) {
215 $the_comment = get_comment( $comment_id );
216 if ( $this->can_send_comment_notification( $the_comment ) ) {
217 $this->send_notification( 'new-comment', $comment_id );
218 }
219 }
220
221 /**
222 * Send notification for new trackback
223 *
224 * @since 1.0
225 * @param unknown $comment_id
226 */
227 function trackback_post( $comment_id ) {
228 $the_comment = get_comment( $comment_id );
229 if ( $this->can_send_comment_notification( $the_comment ) ) {
230 $this->send_notification( 'new-trackback', $comment_id );
231 }
232 }
233
234 /**
235 * Send notification for new pingbacks
236 *
237 * @since 1.0
238 * @param unknown $comment_id
239 */
240 function pingback_post( $comment_id ) {
241 $the_comment = get_comment( $comment_id );
242 if ( $this->can_send_comment_notification( $the_comment ) ) {
243 $this->send_notification( 'new-pingback', $comment_id );
244 }
245 }
246
247 /**
248 * Send notification for lost password.
249 *
250 * @since 1.0
251 */
252 function on_lost_password() {
253 $user = get_user_by( 'login', trim( $_POST['user_login'] ) );
254 if ( $user ) {
255 $this->send_notification( 'admin-password', $user->ID );
256 }
257 }
258
259 /**
260 * Change the title of the password reset email that is sent to the user.
261 *
262 * @since 1.1
263 */
264 public function change_password_email_title( $title ) {
265 $notifications = $this->notifier->get_notifications( 'user-password' );
266 if ( count( $notifications ) > 0 ) {
267 // Ideally there should be only one notification for this type.
268 // If there are multiple notification then we will read data about only the last one
269 $setting = $this->notifier->read_settings( end( $notifications )->ID );
270 return $setting['subject'];
271 }
272
273 return $title;
274 }
275
276 /**
277 * Change the message of the password reset email.
278 *
279 * @since 1.1
280 */
281 public function change_password_email_message( $message, $key, $user_login, $user_data ) {
282 $notifications = $this->notifier->get_notifications( 'user-password' );
283 if ( count( $notifications ) > 0 ) {
284 // Ideally there should be only one notification for this type.
285 // If there are multiple notification then we will read data about only the last one
286 $setting = $this->notifier->read_settings( end( $notifications )->ID );
287
288 return $this->engine->handle_password_reset_shortcodes( $setting, $key, $user_login, $user_data );
289 }
290
291 return $message;
292 }
293
294 /**
295 * Send notification for new uses.
296 *
297 * @since 1.0
298 * @param unknown $user_id
299 */
300 function user_register( $user_id ) {
301 $this->send_notification( 'admin-user', $user_id );
302 }
303
304 /**
305 * New User - Welcome email
306 *
307 * @since 1.1
308 * @param int $user_id New user id
309 */
310 function welcome_email( $user_id ) {
311 $notifications = $this->notifier->get_notifications( 'welcome-email' );
312 foreach ( $notifications as $notification ) {
313 $this->engine->send_registration_email( $this->notifier->read_settings( $notification->ID ), get_userdata( $user_id ) );
314 }
315 }
316
317 /**
318 * Send notification based on type and ref id
319 *
320 * @access private
321 * @since 1.0
322 * @param unknown $type
323 * @param unknown $ref_id
324 */
325 private function send_notification( $type, $ref_id ) {
326 $notifications = $this->notifier->get_notifications( $type );
327 foreach ( $notifications as $notification ) {
328 $this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id );
329 }
330 }
331
332 /**
333 * Can send comment notification or not
334 *
335 * @since 1.0
336 * @param unknown $comment
337 * @return unknown
338 */
339 private function can_send_comment_notification( $comment ) {
340 // Returns false if the comment is marked as spam AND admin has enabled suppression of spam
341 $suppress_spam = get_option( 'bnfw_suppress_spam' );
342 if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) {
343 return false;
344 }
345 return true;
346 }
347 }
348
349 /* ------------------------------------------------------------------------ *
350 * Fire up the plugin
351 * ------------------------------------------------------------------------ */
352 BNFW::factory();
353