| 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.0.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 |
|
| 19 |
This program is free software; you can redistribute it and/or modify |
| 20 |
it under the terms of the GNU General Public License, version 2, as |
| 21 |
published by the Free Software Foundation. |
| 22 |
|
| 23 |
This program is distributed in the hope that it will be useful, |
| 24 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
GNU General Public License for more details. |
| 27 |
|
| 28 |
You should have received a copy of the GNU General Public License |
| 29 |
along with this program; if not, write to the Free Software |
| 30 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 31 |
*/ |
| 32 |
|
| 33 |
class BNFW { |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor. |
| 37 |
* |
| 38 |
* @since 1.0 |
| 39 |
*/ |
| 40 |
function __construct() { |
| 41 |
$this->load_textdomain(); |
| 42 |
$this->includes(); |
| 43 |
$this->hooks(); |
| 44 |
|
| 45 |
$this->notifier = new BNFW_Notification; |
| 46 |
$this->engine = new BNFW_Engine; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Loads the plugin language files |
| 51 |
* |
| 52 |
* @since 1.0 |
| 53 |
*/ |
| 54 |
public function load_textdomain() { |
| 55 |
// Load localization domain |
| 56 |
$this->translations = dirname( plugin_basename( __FILE__ ) ) . '/languages/'; |
| 57 |
load_plugin_textdomain( 'bnfw', false, $this->translations ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Include required files. |
| 62 |
* |
| 63 |
* @since 1.0 |
| 64 |
*/ |
| 65 |
public function includes() { |
| 66 |
// Load Engine and post type |
| 67 |
require_once( 'includes/engine/class-bnfw-engine.php' ); |
| 68 |
require_once( 'includes/admin/class-bnfw-notification.php' ); |
| 69 |
|
| 70 |
// Load Admin Pages |
| 71 |
if ( is_admin() ) { |
| 72 |
require_once( 'includes/admin/bnfw-settings.php' ); |
| 73 |
} |
| 74 |
|
| 75 |
// uncomment for debugging |
| 76 |
//require_once ('includes/debug.php'); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Register Hooks. |
| 81 |
* |
| 82 |
* @since 1.0 |
| 83 |
*/ |
| 84 |
public function hooks() { |
| 85 |
register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
| 86 |
|
| 87 |
add_action( 'draft_to_publish' , array( $this, 'publish_post' ) ); |
| 88 |
add_action( 'publish_to_publish' , array( $this, 'update_post' ) ); |
| 89 |
|
| 90 |
add_action( 'comment_post' , array( $this, 'comment_post' ) ); |
| 91 |
add_action( 'trackback_post' , array( $this, 'trackback_post' ) ); |
| 92 |
add_action( 'pingback_post' , array( $this, 'pingback_post' ) ); |
| 93 |
|
| 94 |
add_action( 'user_register' , array( $this, 'user_register' ) ); |
| 95 |
add_action( 'lostpassword_post' , array( $this, 'lost_password' ) ); |
| 96 |
|
| 97 |
add_action( 'create_term' , array( $this, 'create_term' ), 10, 3 ); |
| 98 |
|
| 99 |
add_filter( 'plugin_action_links' , array( $this, 'plugin_action_links' ), 10, 4 ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Run this on first-time plugin activation |
| 104 |
* |
| 105 |
* @since 1.0 |
| 106 |
*/ |
| 107 |
// importer |
| 108 |
public function activate() { |
| 109 |
require_once( dirname( __FILE__ ) . '/includes/import.php' ); |
| 110 |
$importer = new BNFW_Import; |
| 111 |
$importer->import(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Add 'Settings' link below BNFW in Plugins list. |
| 116 |
* |
| 117 |
* @since 1.0 |
| 118 |
*/ |
| 119 |
public function plugin_action_links( $links, $file ) { |
| 120 |
$plugin_file = 'bnfw/bnfw.php'; |
| 121 |
if ( $file == $plugin_file ) { |
| 122 |
$settings_link = '<a href="' . admin_url( 'admin.php?page=bnfw-settings' ) . '">' . 'Settings' . '</a>'; |
| 123 |
array_unshift( $links, $settings_link ); |
| 124 |
} |
| 125 |
return $links; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* When a new term is created. |
| 130 |
* |
| 131 |
* @since 1.0 |
| 132 |
*/ |
| 133 |
public function create_term( $term_id, $tt_id, $taxonomy ) { |
| 134 |
if ( 'category' == $taxonomy ) { |
| 135 |
$this->send_notification( 'new-category', $term_id ); |
| 136 |
} else { |
| 137 |
$this->send_notification( 'new-term', $term_id ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Fires when a post is created for the first time. |
| 143 |
* |
| 144 |
* @since 1.0 |
| 145 |
*/ |
| 146 |
function publish_post( $post ) { |
| 147 |
$post_id = $post->ID; |
| 148 |
$post_type = $post->post_type; |
| 149 |
|
| 150 |
if ( BNFW_Notification::POST_TYPE != $post_type ) { |
| 151 |
$this->send_notification( 'new-' . $post_type, $post_id ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Fires when a post is updated. |
| 157 |
* |
| 158 |
* @since 1.0 |
| 159 |
*/ |
| 160 |
function update_post( $post ) { |
| 161 |
$post_id = $post->ID; |
| 162 |
$post_type = $post->post_type; |
| 163 |
|
| 164 |
if ( BNFW_Notification::POST_TYPE != $post_type ) { |
| 165 |
$this->send_notification( 'update-' . $post_type, $post_id ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Send notification for new comments |
| 171 |
* |
| 172 |
* @since 1.0 |
| 173 |
*/ |
| 174 |
function comment_post( $comment_id ) { |
| 175 |
$the_comment = get_comment( $comment_id ); |
| 176 |
if ( $this->can_send_comment_notification( $the_comment ) ) { |
| 177 |
$this->send_notification( 'new-comment', $comment_id ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Send notification for new trackback |
| 183 |
* |
| 184 |
* @since 1.0 |
| 185 |
*/ |
| 186 |
function trackback_post( $comment_id ){ |
| 187 |
$the_comment = get_comment( $comment_id ); |
| 188 |
if ( $this->can_send_comment_notification( $the_comment ) ) { |
| 189 |
$this->send_notification( 'new-trackback', $comment_id ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Send notification for new pingbacks |
| 195 |
* |
| 196 |
* @since 1.0 |
| 197 |
*/ |
| 198 |
function pingback_post( $comment_id ) { |
| 199 |
$the_comment = get_comment( $comment_id ); |
| 200 |
if ( $this->can_send_comment_notification( $the_comment ) ) { |
| 201 |
$this->send_notification( 'new-pingback', $comment_id ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Send notification for lost password. |
| 207 |
* |
| 208 |
* @since 1.0 |
| 209 |
*/ |
| 210 |
function lost_password() { |
| 211 |
$user = get_user_by( 'login', trim( $_POST['user_login'] ) ); |
| 212 |
if ( $user ) { |
| 213 |
$this->send_notification( 'user-password', $user->ID ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Send notification for new uses. |
| 219 |
* |
| 220 |
* @since 1.0 |
| 221 |
*/ |
| 222 |
function user_register( $user_id ) { |
| 223 |
$this->send_notification( 'new-user', $user_id ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Send notification based on type and ref id |
| 228 |
* |
| 229 |
* @access private |
| 230 |
* @since 1.0 |
| 231 |
*/ |
| 232 |
private function send_notification( $type, $ref_id ) { |
| 233 |
$notifications = $this->notifier->get_notifications( $type ); |
| 234 |
foreach ( $notifications as $notification ) { |
| 235 |
$this->engine->send_notification( $this->notifier->read_settings( $notification->ID ), $ref_id ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Can send comment notification or not |
| 241 |
* |
| 242 |
* @since 1.0 |
| 243 |
*/ |
| 244 |
private function can_send_comment_notification( $comment ) { |
| 245 |
// Returns false if the comment is marked as spam AND admin has enabled suppression of spam |
| 246 |
$suppress_spam = get_option( 'bnfw_suppress_spam' ); |
| 247 |
if ( '1' === $suppress_spam && ( 0 === strcmp( $comment->comment_approved, 'spam' ) ) ) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
return true; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/* ------------------------------------------------------------------------ * |
| 255 |
* Fire up the plugin |
| 256 |
* ------------------------------------------------------------------------ */ |
| 257 |
new BNFW; |
| 258 |
|