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