예제 #1
0
 function test_add_get_remove()
 {
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     WebPush_DB::add_subscription('http://localhost/1', 'aKey1', '89IdFKBhvi9H5LlvawK9Iw==');
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     WebPush_DB::add_subscription('http://localhost/2', 'aKey2', '89IdFKBhvi9H5LlvawK9Iw==');
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(2, count($subscriptions));
     $this->assertEquals('http://localhost/1', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey1', $subscriptions[0]->userKey);
     $this->assertEquals('http://localhost/2', $subscriptions[1]->endpoint);
     $this->assertEquals('aKey2', $subscriptions[1]->userKey);
     WebPush_DB::remove_subscription('http://localhost/1');
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost/2', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey2', $subscriptions[0]->userKey);
     WebPush_DB::remove_subscription('http://localhost/2');
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(0, count($subscriptions));
 }
 function test_unregister_priv()
 {
     WebPush_DB::add_subscription('http://localhost', 'aKey', '89IdFKBhvi9H5LlvawK9Iw==');
     $_POST['endpoint'] = 'http://localhost';
     try {
         $this->_handleAjax('webpush_unregister');
         $this->assertTrue(false);
     } catch (WPAjaxDieStopException $e) {
         $this->assertTrue(true);
     }
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(0, count($subscriptions));
 }
예제 #3
0
 public static function on_transition_post_status($new_status, $old_status, $post)
 {
     if (empty($post) || $new_status !== "publish") {
         return;
     }
     $title_option = get_option('webpush_title');
     update_option('webpush_payload', array('title' => $title_option === 'blog_title' ? get_bloginfo('name') : $title_option, 'body' => get_the_title($post->ID), 'url' => get_permalink($post->ID)));
     $subscriptions = WebPush_DB::get_subscriptions();
     foreach ($subscriptions as $subscription) {
         if (!sendNotification($subscription->endpoint)) {
             // If there's an error while sending the push notification,
             // the subscription is no longer valid, hence we remove it.
             WebPush_DB::remove_subscription($subscription->endpoint);
         }
     }
 }
 function test_success_remove_invalid_subscription()
 {
     $oldNum = getSentNotificationNum();
     WebPush_DB::add_subscription('http://localhost:55555/400', 'aKey2', '89IdFKBhvi9H5LlvawK9Iw==');
     $post = get_post($this->factory->post->create(array('post_title' => 'Test Post Title')));
     $main = new WebPush_Main();
     $main->on_transition_post_status('publish', 'draft', $post);
     $payload = get_option('webpush_payload');
     $this->assertEquals('Test Blog', $payload['title']);
     $this->assertEquals('Test Post Title', $payload['body']);
     $this->assertEquals('', $payload['icon']);
     $this->assertEquals('http://example.org/?p=' . $post->ID, $payload['url']);
     $this->assertEquals($post->ID, $payload['postID']);
     $this->assertEquals(0, get_post_meta($post->ID, '_notifications_clicked', true));
     $this->assertEquals(1, get_post_meta($post->ID, '_notifications_sent', true));
     $this->assertEquals($oldNum + 2, getSentNotificationNum());
     // Test that the invalid subscription gets removed.
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost:55555/201', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey', $subscriptions[0]->userKey);
 }
예제 #5
0
 public static function sendNotification($title, $body, $icon, $url, $post)
 {
     require_once plugin_dir_path(__FILE__) . 'web-push.php';
     $webPush = new WebPush();
     update_option('webpush_payload', array('title' => html_entity_decode($title, ENT_COMPAT, get_option('blog_charset')), 'body' => html_entity_decode($body, ENT_COMPAT, get_option('blog_charset')), 'icon' => $icon, 'url' => $url, 'postID' => $post ? $post->ID : ''));
     $gcmKey = get_option('webpush_gcm_key');
     $webPush->setGCMKey($gcmKey);
     $webPush->setVAPIDInfo(get_option('webpush_vapid_key'), get_option('webpush_vapid_audience'), get_option('webpush_vapid_subject'));
     $notification_count = 0;
     // Sending notifications could take some time, so we extend the maximum
     // execution time for the script.
     set_time_limit(120);
     $subscriptions = WebPush_DB::get_subscriptions();
     $subscription_num = count($subscriptions);
     foreach ($subscriptions as $subscription) {
         // Ignore GCM endpoints if we don't have a GCM key.
         $isGCM = strpos($subscription->endpoint, GCM_REQUEST_URL) === 0;
         if (!$gcmKey && $isGCM) {
             continue;
         }
         $webPush->addRecipient($subscription->endpoint, function ($success) use($subscription, &$notification_count) {
             if (!$success) {
                 // If there's an error while sending the push notification,
                 // the subscription is no longer valid, hence we remove it.
                 WebPush_DB::remove_subscription($subscription->endpoint);
             } else {
                 $notification_count++;
             }
         });
     }
     $webPush->sendNotifications();
     return $notification_count;
 }
예제 #6
0
 function test_new_registration_without_key_and_auth()
 {
     $_POST['endpoint'] = 'http://localhost';
     try {
         $this->_handleAjax('nopriv_webpush_register');
         $this->assertTrue(false);
     } catch (WPAjaxDieStopException $e) {
         $this->assertTrue(true);
     }
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost', $subscriptions[0]->endpoint);
     $this->assertEquals('', $subscriptions[0]->userKey);
     $this->assertEquals('AAAAAAAAAAAAAAAAAAAAAA', Base64Url::encode($subscriptions[0]->userAuth));
     $this->assertEquals(0, get_option('webpush_accepted_prompt_count'));
 }