PluginProbe
Push Notifications for WP – Self Hosted Web Push Notifications / 1.50
Push Notifications for WP – Self Hosted Web Push Notifications v1.50
1.52 1.51 trunk 1.0 1.1 1.10 1.11 1.12 1.12.1 1.13 1.14 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.21 1.22 1.23 1.24 1.25 1.25.1 1.26 All 58 releases
push-notification / push-notification.php

push-notification.php in Push Notifications for WP – Self Hosted Web Push Notifications 1.50, at push-notification.php

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