settings = require($config_path);
}
$this->settings['view_path'] = realpath(AOPH_AOPUSH_DIR) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
}
/**
* wpPush()
*/
public function aopush_wpPush()
{
add_action('wp_print_footer_scripts', [$this, 'aoph_push']);
add_action('wp_head', [$this, 'aoph_manifest_link']);
if (preg_match('/(sw.js)/i', $_SERVER['REQUEST_URI'])) {
self::aoph_getSWJS();
}
if (preg_match('/(manifest.json)/i', $_SERVER['REQUEST_URI'])) {
self::aoph_getManifest();
}
add_action('save_post', [$this, 'aoph_event_save_post'], 10, 3);
}
/**
* aopp_player()
*/
public function aoph_push()
{
if (
!empty(get_option('aoph_pushsender_post_used')) &&
!empty(get_option('aoph_pushsender_id')) &&
!empty(get_option('aoph_pushsender_hash'))
) {
$id_user = 0;
if (get_current_user_id()) {
$id_user = get_current_user_id();
}
echo '
';
}
}
/**
* aoph_manifest_link()
*/
public function aoph_manifest_link()
{
if (
!empty(get_option('aoph_pushsender_post_used')) &&
!empty(get_option('aoph_pushsender_id')) &&
!empty(get_option('aoph_pushsender_hash'))
) {
echo '';
}
}
/**
* aoph_event_save_post($post_ID, $post, $update)
*/
public function aoph_event_save_post($post_ID, $post, $update)
{
if (
empty(get_option('aoph_pushsender_post_used')) ||
empty(get_option('aoph_pushsender_id')) ||
empty(get_option('aoph_pushsender_hash')) ||
$post->post_status!=='publish'
) {
return false;
}
preg_match_all('/(\
post_content, $arr);
$data = [
'text' => $post->post_title,
'url' => $post->guid,
'id' => $post_ID,
'icon' => '',
'subject' => '',
];
$index_obsolescence = 300;
$post_date = strtotime($post->post_date);
$curr_date = strtotime(current_time('mysql'));
$obsolescence = $curr_date - $post_date;
if ($obsolescence>$index_obsolescence) {
if (
empty(get_option('aoph_pushsender_template_update_subject')) ||
empty(get_option('aoph_pushsender_template_update_icon')) ||
empty(get_option('aoph_pushsender_post_update'))
) {
return false;
}
$data['subject'] = get_option('aoph_pushsender_template_update_subject');
$data['icon'] = get_option('aoph_pushsender_template_update_icon');
$data['send_type'] = 2;
$send = AopushPushApi::aoph()->aopush_sendpush($data);
} else {
if (
empty(get_option('aoph_pushsender_template_insert_subject')) ||
empty(get_option('aoph_pushsender_template_insert_icon')) ||
empty(get_option('aoph_pushsender_post_insert'))
) {
return false;
}
$data['subject'] = get_option('aoph_pushsender_template_insert_subject');
$data['icon'] = get_option('aoph_pushsender_template_insert_icon');
$data['send_type'] = 1;
$send = AopushPushApi::aoph()->aopush_sendpush($data);
}
if (!empty($send)) {
return true;
}
return false;
}
/**
* getSWJS()
*/
public function aoph_getSWJS()
{
if (
!empty(get_option('aoph_pushsender_post_used')) &&
!empty(get_option('aoph_pushsender_id')) &&
!empty(get_option('aoph_pushsender_hash'))
) {
$script = "
'use strict';
importScripts('".plugins_url('aopush/assets/js/Dexie.min.js')."');
var dDB;
function getDb(){
if(!dDB){
dDB=new Dexie('chrome-push-notifications');
dDB.version(1).stores({pushData:'tag,url',});
dDB.open();
}
return dDB;
}
self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function (event) {
console.log('The ServiceWorker was activated.');
});
self.addEventListener('push',function(event){
event.waitUntil(
self.registration.pushManager.getSubscription().then(function(subscription){
var ids = subscription.endpoint.split('/').slice(-1);
return fetch('https://aoserver.ru/resurces/push/getdata?".get_option('aoph_pushsender_hash')."', {
method: 'post',
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: 'ids=' + ids[0]
}).then(function(response){
return response.json().then(function(data){
var promises=[];
if(!data.notification.title) data.notification.title='New Notification!';
if(!data.notification.body) data.notification.body='';
getDb().pushData.put({tag:data.notification.tag,url:data.notification.url});
promises.push(showNotification(data.notification.title,data.notification.body,data.notification.tag,data.notification.icon,data.notification.image,data.notification.interaction));
return Promise.all(promises);
});
});
})
);
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(getDb().pushData.get(event.notification.tag).then(function(push_data){
getDb().pushData.delete();
return clients.openWindow(push_data.url).focus();
}));
});
function showNotification(title,body,tag,icon,image,interaction){
var options={body:body,tag:tag,icon:icon,image:image,requireInteraction:interaction};
return self.registration.showNotification(title,options);
};
";
header('Content-Type: application/json; charset=utf-8');
header('Content-Type: application/javascript');
header('Content-Type: application/x-javascript');
header('Content-Type: text/javascript');
exit($script);
}
}
/**
* getManifest()
*/
public function aoph_getManifest()
{
if (
!empty(get_option('aoph_pushsender_post_used')) &&
!empty(get_option('aoph_pushsender_id')) &&
!empty(get_option('aoph_pushsender_hash'))
) {
$manifest = '
{
"name": "AutoOffice.Push",
"short_name": "AO Push",
"display": "standalone",
"gcm_sender_id": "'.get_option('aoph_pushsender_id').'",
"permissions": ["gcm", "storage"],
"gcm_user_visible_only": true,
"theme_color": "#ffffff",
"background_color": "#ffffff"
}';
header('Content-Type: application/manifest+json');
exit($manifest);
}
}
/**
* aomailer($className=__CLASS__)
*/
public static function aoph($className=__CLASS__)
{
return new $className();
}
}