pn_get_layout_files('messaging-sw.js'); $settings = $this->json_settings(); $messageSw = str_replace('{{pnScriptSetting}}', json_encode($settings), $messageSw); $swJsContent .= $messageSw; return $swJsContent; } function add_pn_config($firebaseconfig){ $firebaseconfig = 'var config = pnScriptSetting.pn_config;' .'if (!firebase.apps.length) {firebase.initializeApp(config);} const messaging = firebase.messaging();'; return $firebaseconfig; } function add_pn_use_sw($useserviceworker){ $useserviceworker = 'messaging.useServiceWorker(reg);'; return $useserviceworker; } function add_sw_register_template($swHtmlContent){ $sw_registerContent = $this->pn_get_layout_files('public/app.js'); $replaceCnt = "var config=pnScriptSetting.pn_config; if (!firebase.apps.length) { firebase.initializeApp(config); } var swsource = pnScriptSetting.swsource; const messaging = firebase.messaging(); if(\"serviceWorker\" in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register(swsource, {scope: pnScriptSetting.scope}).then(function(reg){ messaging.useServiceWorker(reg); console.log('Congratulations!!Service Worker Registered ServiceWorker scope: ', reg.scope); }).catch(function(err) { console.log('ServiceWorker registration failed: ', err); }); }) }"; $sw_registerContent = str_replace($replaceCnt, '', $sw_registerContent); //Concatnate content in main service worker register $swHtmlContent .= $sw_registerContent; return $swHtmlContent; } public function json_settings(){ if ( is_multisite() ) { $link = get_site_url(); } else { $link = home_url(); } $auth_settings = push_notification_auth_settings(); $messageConfig = ''; if(isset($auth_settings['user_token']) && isset($auth_settings['token_details']['validated']) && $auth_settings['token_details']['validated'] == 1){ $messageConfig = json_decode($auth_settings['messageManager'], true); } $settings = array( 'nonce' => wp_create_nonce("pn_notification"), 'pn_config'=> $messageConfig, "swsource" => esc_url_raw(trailingslashit($link)."?push_notification_sw=1"), "scope" => esc_url_raw(trailingslashit($link)), "ajax_url"=> esc_url_raw(admin_url('admin-ajax.php')) ); return $settings; } function load_admin_scripts($hook_suffix){ if($hook_suffix=='toplevel_page_push-notification'){ wp_enqueue_script('push_notification_script', PUSH_NOTIFICATION_PLUGIN_URL.'/assets/main-admin-script.js', array('jquery'), PUSH_NOTIFICATION_PLUGIN_VERSION, true); wp_enqueue_style('push-notification-style', PUSH_NOTIFICATION_PLUGIN_URL.'/assets/main-admin-style.css', array('dashboard'), PUSH_NOTIFICATION_PLUGIN_VERSION, 'all'); if ( is_multisite() ) { $link = get_site_url(); } else { $link = home_url(); } wp_localize_script("push_notification_script", 'pn_setings', array( "home_url"=> esc_url_raw($link), "remote_nonce"=> wp_create_nonce("pn_notification") ) ); } } public function add_menu_links(){ // Main menu page add_menu_page( esc_html__( 'Push Notification-Admin', 'push-notification' ), esc_html__( 'Push Notifications Options', 'push-notification' ), 'manage_options', 'push-notification', array($this, 'admin_interface_render'), '', 100 ); // Settings page - Same as main menu page add_submenu_page( 'push-notification', esc_html__( 'Push Notifications Options', 'push-notification' ), esc_html__( 'Settings', 'push-notification' ), 'manage_options', 'push-notification', array($this, 'admin_interface_render') ); } function admin_interface_render(){ // Authentication if ( ! current_user_can( 'manage_options' ) ) { return; } ?>

"; // Status do_settings_sections( 'push_notification_dashboard_section' ); // Page slug $authData = push_notification_auth_settings(); if(isset($authData['token_details']['validated']) && $authData['token_details']['validated']==1){ $this->shownotificationData(); } echo "
"; ?>
'.esc_html__($updated_at, 'push-notification').'
'.esc_html__('Total Subscribers', 'push-notification').':
'.esc_html($subscriber_count).'
'; do_settings_sections( 'push_notification_user_settings_section' ); echo ' '; echo '

'.esc_html__('Send Custom Notification', 'push-notification').'

'; } public function pn_key_validate_status_callback(){ $authData = push_notification_auth_settings(); if( !isset($authData['token_details']['validated']) || (isset($authData['token_details']) && $authData['token_details']['validated']!=1) ){ echo "
"; PN_Field_Generator::get_input_password('user_token', 'user_auth_token_key'); PN_Field_Generator::get_button('Validate', 'user_auth_vadation'); echo '

'.esc_html__('This plugin requires a free API key for PushNotification.io', 'push-notification').' '.esc_html__('Get the Key', 'push-notification').'

'; }else{ echo " ".esc_html__('User Verified', 'push-notification')." "; } }//function closed public function user_settings_callback(){ $notification = push_notification_settings(); PN_Field_Generator::get_input_checkbox('on_edit', '1', 'pn_push_on_edit', 'pn-checkbox pn_push_on_edit'); } public function user_settings_onpublish_callback(){ $notification = push_notification_settings(); PN_Field_Generator::get_input_checkbox('on_publish', '1', 'pn_push_on_publish', 'pn-checkbox pn_push_on_publish'); } public function pn_verify_user(){ $nonce = sanitize_text_field($_POST['nonce']); if( !wp_verify_nonce($nonce, 'pn_notification') ){ echo json_encode(array("status"=> 503, 'message'=>'Request not authorized'));die; }else{ $user_token = sanitize_text_field($_POST['user_token']); $response = PN_Server_Request::varifyUser($user_token); echo json_encode($response);die; } echo json_encode(array("status"=> 503, 'message'=>'Request not identified'));die; } public function pn_revoke_keys(){ $nonce = sanitize_text_field($_POST['nonce']); if( !wp_verify_nonce($nonce, 'pn_notification') ){ echo json_encode(array("status"=> 503, 'message'=>'Request not authorized'));die; }else{ delete_option('push_notification_auth_settings'); echo json_encode(array("status"=> 200, 'message'=>'API key removed successfully'));die; } } public function pn_subscribers_data(){ $nonce = sanitize_text_field($_POST['nonce']); if( !wp_verify_nonce($nonce, 'pn_notification') ){ echo json_encode(array("status"=> 503, 'message'=>'Request not authorized'));die; }else{ $auth_settings = push_notification_auth_settings(); if( isset( $auth_settings['user_token'] ) ){ PN_Server_Request::getsubscribersData( $auth_settings['user_token'] ); echo json_encode(array("status"=> 200, 'message'=>'Data updated'));die; }else{ echo json_encode(array("status"=> 503, 'message'=>'User token not found'));die; } } } public function pn_send_notification(){ $nonce = sanitize_text_field($_POST['nonce']); if( !wp_verify_nonce($nonce, 'pn_notification') ){ echo json_encode(array("status"=> 503, 'message'=>'Request not authorized'));die; }else{ $auth_settings = push_notification_auth_settings(); $title = sanitize_text_field($_POST['title']); $message = sanitize_textarea_field($_POST['message']); $link_url = esc_url_raw($_POST['link_url']); $image_url = esc_url_raw($_POST['image_url']); if( isset( $auth_settings['user_token'] ) ){ $response = PN_Server_Request::sendPushNotificatioData( $auth_settings['user_token'], $title,$message, $link_url, $image_url ); echo json_encode($response);die; }else{ echo json_encode(array("status"=> 503, 'message'=>'User token not found'));die; } } } public function send_notification_on_update($new_status, $old_status, $post){ $pn_settings = push_notification_settings(); $auth_settings = push_notification_auth_settings(); if ( 'publish' !== $new_status ){ return; } $send_notification = false; //for Edit if(isset($pn_settings['on_edit']) && $pn_settings['on_edit']==1){ if ( $new_status === $old_status) { $this->send_notification($post); $send_notification = true; } } //for publish if(!$send_notification && isset($pn_settings['on_publish']) && $pn_settings['on_publish']==1){ if ( $new_status !== $old_status) { $this->send_notification($post); } } } protected function send_notification($post){ $post_id = $post->ID; $post_content = $post->post_content; $post_title = $post->post_title; $auth_settings = push_notification_auth_settings(); //API Data $title = sanitize_text_field(wp_strip_all_tags($post_title) ); $message = wp_trim_words(wp_strip_all_tags(sanitize_text_field($post_content)), 20); $link_url = esc_url_raw(get_permalink( $post_id )); $image_url = ''; if(has_post_thumbnail($post_id)){ $image_url = esc_url_raw(get_the_post_thumbnail_url($post_id)); } if( isset( $auth_settings['user_token'] ) && !empty($auth_settings['user_token']) ){ $response = PN_Server_Request::sendPushNotificatioData( $auth_settings['user_token'], $title, $message, $link_url, $image_url ); }//auth token check } public function pn_get_layout_files($filePath){ $fileContentResponse = @wp_remote_get(esc_url_raw(PUSH_NOTIFICATION_PLUGIN_URL.'/assets/'.$filePath)); if(wp_remote_retrieve_response_code($fileContentResponse)!=200){ if(!function_exists('get_filesystem_method')){ require_once( ABSPATH . 'wp-admin/includes/file.php' ); } $access_type = get_filesystem_method(); if($access_type === 'direct') { $file = PUSH_NOTIFICATION_PLUGIN_DIR.($filePath); $creds = request_filesystem_credentials($file, '', false, false, array()); if ( ! WP_Filesystem($creds) ) { return false; } global $wp_filesystem; $htmlContentbody = $wp_filesystem->get_contents($file); return $htmlContentbody; } return false; }else{ return wp_remote_retrieve_body( $fileContentResponse ); } } } if(is_admin() || wp_doing_ajax()){ $push_Notification_Admin_Obj = new Push_Notification_Admin(); $push_Notification_Admin_Obj->init(); } function push_notification_settings(){ $push_notification_settings = get_option( 'push_notification_settings', array() ); return $push_notification_settings; } function push_notification_auth_settings(){ $push_notification_auth_settings = get_option( 'push_notification_auth_settings', array() ); return $push_notification_auth_settings; } function push_notification_details_settings(){ $push_notification_details_settings = get_option( 'push_notification_details_settings', array() ); return $push_notification_details_settings; } /** * Server Side fields generation class */ class PN_Field_Generator{ static $settingName = 'push_notification_settings'; public static function get_input($name, $id="", $class=""){ $settings = push_notification_settings(); ?> value=""/>