| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Push Notifications for WP - Self Hosted Web Push Notifications |
| 4 |
Plugin URI: https://wordpress.org/plugins/push-notification/ |
| 5 |
Description: Push Notifications allow admins to automatically notify their audience whenever new content or custom notices are published on the site. |
| 6 |
Author: Magazine3 |
| 7 |
Version: 1.52 |
| 8 |
Author URI: http://pushnotifications.io/ |
| 9 |
Text Domain: push-notification |
| 10 |
Domain Path: /languages |
| 11 |
License: GPL2+ |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 16 |
|
| 17 |
define('PUSH_NOTIFICATION_PLUGIN_FILE', __FILE__ ); |
| 18 |
define('PUSH_NOTIFICATION_PLUGIN_DIR', plugin_dir_path( __FILE__ )); |
| 19 |
define('PUSH_NOTIFICATION_PLUGIN_URL', plugin_dir_url( __FILE__ )); |
| 20 |
define('PUSH_NOTIFICATION_PLUGIN_VERSION', '1.52'); |
| 21 |
define('PUSH_NOTIFICATION_PLUGIN_BASENAME', plugin_basename(__FILE__)); |
| 22 |
|
| 23 |
/** |
| 24 |
* Initialize pwa functions |
| 25 |
*/ |
| 26 |
add_action('plugins_loaded', 'push_notification_initialize'); |
| 27 |
function push_notification_initialize(){ |
| 28 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/admin/admin.php"; |
| 29 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/admin/PnMetaBox.php"; |
| 30 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/admin/newsletter.php"; |
| 31 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/compatibility/ultimate-member.php"; |
| 32 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/compatibility/wise-chat.php"; |
| 33 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/admin/feedback-helper-functions.php"; |
| 34 |
if(is_admin()){ |
| 35 |
if ( is_multisite()) { |
| 36 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/admin/pn_multisite.php"; |
| 37 |
} |
| 38 |
add_filter( 'plugin_action_links_' . PUSH_NOTIFICATION_PLUGIN_BASENAME,'push_notification_add_action_links', 10, 4); |
| 39 |
} |
| 40 |
if( !is_admin() || wp_doing_ajax() ){ |
| 41 |
require_once PUSH_NOTIFICATION_PLUGIN_DIR."inc/frontend/pn-frontend.php"; |
| 42 |
} |
| 43 |
if ( class_exists( 'OneSignal' ) ) { |
| 44 |
add_action('admin_notices', 'push_notification_feature_notice'); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
function push_notification_feature_notice(){ |
| 49 |
$class = 'notice notice-warning'; |
| 50 |
$message = esc_html__( 'There is may some conflict issue with the other push notification plugin. To take benefit all features of the Push Notification plugin, Please deactivate the OneSignal plugin.', 'push-notification' ); |
| 51 |
|
| 52 |
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
function push_notification_add_action_links($actions, $plugin_file, $plugin_data, $context){ |
| 57 |
$mylinks = array('<a href="' . esc_url_raw(admin_url( 'admin.php?page=push-notification' )) . '">'.esc_html__( 'Settings', 'push-notification' ).'</a>', |
| 58 |
'<a href="https://pushnotifications.io/documentation/" target="_blank">'.esc_html__( 'Documentation', 'push-notification' ).'</a>', |
| 59 |
); |
| 60 |
return array_merge( $actions, $mylinks ); // no validation check since $mylinks will always be non-empty array |
| 61 |
} |
| 62 |
|
| 63 |
register_activation_hook( PUSH_NOTIFICATION_PLUGIN_FILE, 'push_notification_on_activate' ); |
| 64 |
|
| 65 |
function push_notification_on_activate( $network_wide ) { |
| 66 |
/** Setup notification feature in PWA-for-wp*/ |
| 67 |
$pwaforwp_settings = get_option( 'pwaforwp_settings'); |
| 68 |
|
| 69 |
if ( isset( $pwaforwp_settings['notification_feature'] ) && $pwaforwp_settings['notification_feature'] == 0 ) { |
| 70 |
|
| 71 |
$pwaforwp_settings['notification_feature'] = 1; |
| 72 |
update_option( 'pwaforwp_settings', $pwaforwp_settings, false ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
if ( is_multisite() && $network_wide ) { |
| 77 |
|
| 78 |
$sites = get_sites(); |
| 79 |
|
| 80 |
if ( $sites ) { |
| 81 |
|
| 82 |
foreach ( $sites as $site ) { |
| 83 |
|
| 84 |
switch_to_blog( $site->blog_id ); |
| 85 |
push_notification_on_install(); |
| 86 |
restore_current_blog(); |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
} else { |
| 93 |
|
| 94 |
push_notification_on_install(); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* on activation of plugin compatibile with PWA for wp |
| 100 |
* if PWA activated and now activating PushNotification, Need to regenerate service worker files |
| 101 |
*/ |
| 102 |
$auth_settings = get_option( 'push_notification_auth_settings', array() ); |
| 103 |
//Push notification Check |
| 104 |
if ( isset( $auth_settings['user_token'] ) && isset( $auth_settings['token_details']['validated']) && $auth_settings['token_details']['validated'] == 1 ) { |
| 105 |
//pwaforamp check |
| 106 |
if ( function_exists('pwaforwp_required_file_creation') ) { |
| 107 |
|
| 108 |
$pwaSettings = pwaforwp_defaultSettings(); |
| 109 |
|
| 110 |
if ( $pwaSettings['notification_feature']==1 && isset($pwaSettings['notification_options']) && $pwaSettings['notification_options']=='pushnotifications_io' ) { |
| 111 |
|
| 112 |
pwaforwp_required_file_creation(); |
| 113 |
|
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* TO compatible with older < 1.3 of pushnotification |
| 122 |
*/ |
| 123 |
|
| 124 |
add_action( 'plugins_loaded', 'push_notification_older_version_compatibility' ); |
| 125 |
|
| 126 |
function push_notification_older_version_compatibility() { |
| 127 |
|
| 128 |
$configCompatible = get_transient( 'push_notification_older_version' ); |
| 129 |
|
| 130 |
if ( ! $configCompatible ) { |
| 131 |
|
| 132 |
$auth_settings = push_notification_auth_settings(); |
| 133 |
|
| 134 |
if ( isset( $auth_settings['user_token'] ) && isset( $auth_settings['token_details']['validated']) && $auth_settings['token_details']['validated'] == 1 && ! isset($auth_settings['messageManager'] ) ) { |
| 135 |
|
| 136 |
$response = PN_Server_Request::varifyUser($auth_settings['user_token']); |
| 137 |
set_transient( 'push_notification_older_version', 1 ); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
function push_notification_pro_checker() { |
| 146 |
|
| 147 |
$_pro_checker = get_transient( 'push_notification_pro_checker' ); |
| 148 |
|
| 149 |
if ( ! $_pro_checker ) { |
| 150 |
|
| 151 |
$auth_settings = push_notification_auth_settings(); |
| 152 |
|
| 153 |
if ( ! empty( $auth_settings['user_token'] ) ) { |
| 154 |
|
| 155 |
PN_Server_Request::getProStatus(true); |
| 156 |
set_transient( 'push_notification_pro_checker', true, 86400 ); |
| 157 |
|
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
add_action( 'admin_init', 'push_notification_pro_checker', 0); |
| 163 |
|
| 164 |
|
| 165 |
/* |
| 166 |
Globlal function to send push notification from anywhere push_notification_send_push_notificatioin_filter |
| 167 |
|
| 168 |
$user_id => your meta_user_id required |
| 169 |
$title => Message title required |
| 170 |
$link_url => your website url required |
| 171 |
$message => text message required |
| 172 |
$image_url => png image link url optional |
| 173 |
$icon_url => icon link url optional |
| 174 |
|
| 175 |
*/ |
| 176 |
|
| 177 |
|
| 178 |
function push_notification_send_push_notificatioin_filter( $user_id = null, $title = "", $message = "", $link_url = "", $icon_url = "", $image_url = "" ) { |
| 179 |
$response = array('status' => false,'message' => esc_html__('Invalid Request','push-notification')); |
| 180 |
if ( ! empty( $user_id) && !empty($title) && !empty($message) && !empty($link_url)) { |
| 181 |
|
| 182 |
$verifyUrl = 'campaign/pn_send_push_filter'; |
| 183 |
$audience_token_id = get_user_meta($user_id, 'pnwoo_notification_token',true); |
| 184 |
if ( is_multisite() ) { |
| 185 |
$weblink = get_site_url(); |
| 186 |
}else{ |
| 187 |
$weblink = home_url(); |
| 188 |
} |
| 189 |
|
| 190 |
if ( is_multisite() ) { |
| 191 |
$auth_settings = get_site_option( 'push_notification_auth_settings', array() ); |
| 192 |
}else{ |
| 193 |
$auth_settings = get_option( 'push_notification_auth_settings', array() ); |
| 194 |
} |
| 195 |
foreach($audience_token_id as $key=>$value) { |
| 196 |
if( isset($auth_settings['user_token']) && !empty( $value ) ){ |
| 197 |
$data = array( |
| 198 |
"user_token" =>$auth_settings['user_token'], |
| 199 |
"audience_token_id" =>$value, |
| 200 |
"website" =>$weblink, |
| 201 |
'title' =>$title, |
| 202 |
'message' =>$message, |
| 203 |
'link_url' =>$link_url, |
| 204 |
'icon_url' =>$icon_url, |
| 205 |
'image_url' =>$image_url |
| 206 |
); |
| 207 |
$url = 'https://pushnotifications.io/api/'.$verifyUrl; |
| 208 |
|
| 209 |
$postdata = array('body'=> $data); |
| 210 |
|
| 211 |
$remoteResponse = wp_remote_post($url, $postdata); |
| 212 |
if( is_wp_error( $remoteResponse ) ){ |
| 213 |
if(!empty($remoteResponse->get_error_message()) ) { |
| 214 |
$error_message = strtolower($remoteResponse->get_error_message()); |
| 215 |
$error_pos = strpos($error_message, 'operation timed out'); |
| 216 |
if($error_pos !== false){ |
| 217 |
$message = esc_html('Request timed out, please try again','push-notification'); |
| 218 |
}else{ |
| 219 |
$message = esc_html($remoteResponse->get_error_message()); |
| 220 |
} |
| 221 |
}else{ |
| 222 |
$message = esc_html("could not connect to server",'push-notification'); |
| 223 |
} |
| 224 |
$response = array('status'=>401, "message"=>$message); |
| 225 |
|
| 226 |
}else{ |
| 227 |
$response['status'] = true; |
| 228 |
$response['message'] = esc_html('Sent Successfully','push-notification'); |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
}else{ |
| 234 |
$response['status'] = false; |
| 235 |
$response['message'] = esc_html('User id, title, link_url and message field are required','push-notification'); |
| 236 |
} |
| 237 |
return $response; |
| 238 |
} |
| 239 |
|
| 240 |
function push_notification_on_install(){ |
| 241 |
|
| 242 |
global $wpdb; |
| 243 |
|
| 244 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 245 |
|
| 246 |
$charset_collate = $engine = ''; |
| 247 |
|
| 248 |
if(!empty($wpdb->charset)) { |
| 249 |
$charset_collate .= " DEFAULT CHARACTER SET {$wpdb->charset}"; |
| 250 |
} |
| 251 |
if($wpdb->has_cap('collation') AND !empty($wpdb->collate)) { |
| 252 |
$charset_collate .= " COLLATE {$wpdb->collate}"; |
| 253 |
} |
| 254 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 255 |
$found_engine = $wpdb->get_var("SELECT ENGINE FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA` = '".DB_NAME."' AND `TABLE_NAME` = '{$wpdb->prefix}posts';"); |
| 256 |
|
| 257 |
if(strtolower($found_engine) == 'innodb') { |
| 258 |
$engine = ' ENGINE=InnoDB'; |
| 259 |
} |
| 260 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 261 |
$found_tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}pn_token_urls%';"); |
| 262 |
|
| 263 |
if(!in_array("{$wpdb->prefix}pn_token_urls", $found_tables)) { |
| 264 |
|
| 265 |
dbDelta("CREATE TABLE `{$wpdb->prefix}pn_token_urls` ( |
| 266 |
`id` bigint( 20 ) unsigned NOT NULL AUTO_INCREMENT, |
| 267 |
`url` varchar(250) NOT NULL, |
| 268 |
`status` varchar(20) NOT NULL DEFAULT 'active', |
| 269 |
`token` varchar(250) NOT NULL, |
| 270 |
`created_at` datetime NOT NULL, |
| 271 |
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), |
| 272 |
KEY `url` ( `url` ), |
| 273 |
PRIMARY KEY (`id`) |
| 274 |
) ".$charset_collate.$engine.";"); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
add_action('upgrader_process_complete', 'push_notification_plugin_upgrade_function', 10, 2); |
| 280 |
|
| 281 |
function push_notification_plugin_upgrade_function($upgrader_object, $options) { |
| 282 |
// Check if it's a plugin update |
| 283 |
if ( $options['type'] === 'plugin' && isset( $options['action'] ) && $options['action'] === 'update' ) { |
| 284 |
// Check if the updated plugin is your plugin |
| 285 |
$plugin_slug = 'push-notification/push-notification.php'; |
| 286 |
if ( is_array( $options['plugins'] ) && in_array( $plugin_slug, $options['plugins'] ) ) { |
| 287 |
push_notification_on_install(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
add_action('admin_footer', 'push_notification_add_footer_text'); |
| 293 |
|
| 294 |
function push_notification_add_footer_text() |
| 295 |
{ |
| 296 |
$screen = get_current_screen(); |
| 297 |
if ($screen->id !== 'toplevel_page_push-notification') { |
| 298 |
return; |
| 299 |
} |
| 300 |
|
| 301 |
echo '<div style="padding: 0 0 20px 15%;"> |
| 302 |
<p>' . esc_html__( 'If you like Push Notification, please', 'push-notification' ) . ' ' . esc_html__( "leave a" , 'push-notification' ) . ' <a href="' . esc_url( 'https://wordpress.org/support/plugin/push-notification/reviews/?rate=5#new-post' ) . '" target="_blank">★★★★★</a> ' . esc_html__( "rating to support continued development. Thanks a bunch!" , 'push-notification' ) . '</p> |
| 303 |
</div>'; |
| 304 |
} |