Ejemplo n.º 1
0
 public static function on_activate()
 {
     global $wpdb;
     if (WebPush_DB::getInstance()->version === get_option('webpush_db_version')) {
         return;
     }
     $table_name = $wpdb->prefix . 'webpush_subscription';
     $sql = 'CREATE TABLE ' . $table_name . ' (
   `id` INT NOT NULL AUTO_INCREMENT,
   `endpoint` VARCHAR(300) NOT NULL,
   `userKey` VARCHAR(300) NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE (`endpoint`)
 );';
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     dbDelta($sql);
     update_option('webpush_db_version', WebPush_DB::getInstance()->version);
     // Set default options.
     update_option('webpush_title', 'blog_title');
     update_option('webpush_icon', function_exists('get_site_icon_url') ? 'blog_icon' : '');
     update_option('webpush_min_visits', 3);
     update_option('webpush_gcm_key', '');
     update_option('webpush_gcm_sender_id', '');
     update_option('webpush_notification_count', 0);
     update_option('webpush_opened_notification_count', 0);
     $default_triggers = WebPush_Main::get_triggers_by_key_value('enable_by_default', true);
     $default_triggers_keys = array();
     foreach ($default_triggers as $trigger) {
         $default_triggers_keys[] = $trigger['key'];
     }
     update_option('webpush_triggers', $default_triggers_keys);
 }
 function test_update_notifications_clicked_on_correct_post()
 {
     $post1ID = $this->factory->post->create();
     update_post_meta($post1ID, '_notifications_clicked', 0);
     update_post_meta($post1ID, '_notifications_sent', 0);
     $post2ID = $this->factory->post->create();
     update_post_meta($post2ID, '_notifications_clicked', 0);
     update_post_meta($post2ID, '_notifications_sent', 0);
     $post3ID = $this->factory->post->create();
     update_post_meta($post3ID, '_notifications_clicked', 0);
     update_post_meta($post3ID, '_notifications_sent', 0);
     $query = (object) array('query_vars' => array('webpush_post_id' => $post1ID));
     WebPush_Main::on_parse_request($query);
     $this->assertEquals(1, get_post_meta($post1ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post1ID, '_notifications_sent', true));
     $this->assertEquals(0, get_post_meta($post2ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post2ID, '_notifications_sent', true));
     $this->assertEquals(0, get_post_meta($post3ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post3ID, '_notifications_sent', true));
     WebPush_Main::on_parse_request($query);
     $this->assertEquals(2, get_post_meta($post1ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post1ID, '_notifications_sent', true));
     $this->assertEquals(0, get_post_meta($post2ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post2ID, '_notifications_sent', true));
     $this->assertEquals(0, get_post_meta($post3ID, '_notifications_clicked', true));
     $this->assertEquals(0, get_post_meta($post3ID, '_notifications_sent', true));
 }
 function test_webpush_main_sendnotification()
 {
     $oldNum = getSentNotificationNum();
     WebPush_Main::sendNotification('A Title', 'A Body', 'http://marco/marco.png', 'http://marco/', null);
     $payload = get_option('webpush_payload');
     $this->assertEquals('A Title', $payload['title']);
     $this->assertEquals('A Body', $payload['body']);
     $this->assertEquals('http://marco/marco.png', $payload['icon']);
     $this->assertEquals('http://marco/', $payload['url']);
     $this->assertEquals('', $payload['postID']);
     $this->assertEquals($oldNum + 1, getSentNotificationNum());
 }
Ejemplo n.º 4
0
 public static function init()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
 }
Ejemplo n.º 5
0
    public function options()
    {
        $allowed_triggers = WebPush_Main::$ALLOWED_TRIGGERS;
        $title_option = get_option('webpush_title');
        $icon_option = get_option('webpush_icon');
        $min_visits_option = intval(get_option('webpush_min_visits'));
        $triggers_option = get_option('webpush_triggers');
        $gcm_key_option = get_option('webpush_gcm_key');
        $gcm_sender_id_option = get_option('webpush_gcm_sender_id');
        if (isset($_POST['webpush_form']) && $_POST['webpush_form'] === 'submitted') {
            if ($_POST['webpush_title'] === 'blog_title') {
                $title_option = 'blog_title';
            } else {
                if ($_POST['webpush_title'] === 'custom') {
                    $title_option = $_POST['webpush_title_custom'];
                } else {
                    wp_die(__('Invalid value for the Notification Title', 'wpwebpush'));
                }
            }
            if ($_POST['webpush_icon'] === '') {
                $icon_option = '';
            } else {
                if ($_POST['webpush_icon'] === 'blog_icon') {
                    $icon_option = 'blog_icon';
                } else {
                    if ($_POST['webpush_icon'] === 'custom') {
                        if (isset($_FILES['webpush_icon_custom']) && $_FILES['webpush_icon_custom']['size'] > 0) {
                            $file_type = wp_check_filetype(basename($_FILES['webpush_icon_custom']['name']));
                            if (!in_array($file_type['type'], array('image/jpg', 'image/jpeg', 'image/gif', 'image/png'))) {
                                wp_die(__('The notification icon should be an image', 'wpwebpush'));
                            }
                            $file = wp_handle_upload($_FILES['webpush_icon_custom'], array('test_form' => false));
                            if (isset($file['error'])) {
                                wp_die(sprintf(__('Error in handling upload for the notification icon: %s', 'wpwebpush'), $file['error']));
                            }
                            $attachment = array('post_mime_type' => $file_type['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['file'])), 'post_content' => '', 'post_status' => 'inherit');
                            $attach_id = wp_insert_attachment($attachment, $file['file']);
                            require_once ABSPATH . 'wp-admin/includes/image.php';
                            $attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
                            wp_update_attachment_metadata($attach_id, $attach_data);
                            $icon_option = wp_get_attachment_url($attach_id);
                        } else {
                            if ($icon_option === 'blog_icon' || $icon_option === '') {
                                // If it wasn't set to custom and there's no file selected, die.
                                wp_die(__('Invalid value for the Notification Icon', 'wpwebpush'));
                            }
                        }
                    } else {
                        wp_die(__('Invalid value for the Notification Icon', 'wpwebpush'));
                    }
                }
            }
            if ($_POST['webpush_min_visits'] === '0') {
                $min_visits_option = 0;
            } else {
                if ($_POST['webpush_min_visits'] === 'custom') {
                    $min_visits_option = intval($_POST['webpush_min_visits_custom']);
                } else {
                    wp_die(__('Invalid value for `Registration Behavior`', 'wpwebpush'));
                }
            }
            if (isset($_POST['webpush_triggers'])) {
                $triggers_option = $_POST['webpush_triggers'];
                foreach ($triggers_option as $trigger_option) {
                    if (!WebPush_Main::get_trigger_by_key_value('key', $trigger_option)) {
                        wp_die(__('Invalid value in Push Triggers: ' . $trigger_option, 'wpwebpush'));
                    }
                }
            } else {
                // If it's not set it means they've removed all options
                $triggers_option = array();
            }
            $gcm_key_option = $_POST['webpush_gcm_key'];
            $gcm_sender_id_option = $_POST['webpush_gcm_sender_id'];
            update_option('webpush_title', $title_option);
            update_option('webpush_icon', $icon_option);
            update_option('webpush_min_visits', $min_visits_option);
            update_option('webpush_triggers', $triggers_option);
            update_option('webpush_gcm_key', $gcm_key_option);
            update_option('webpush_gcm_sender_id', $gcm_sender_id_option);
            ?>
<div class="updated"><p><strong><?php 
            _e('Settings saved.');
            ?>
</strong></p></div>
<?php 
        }
        ?>

<div class="wrap">
<h2><?php 
        _e('Web Push', 'wpwebpush');
        ?>
</h2>

<form method="post" action="" enctype="multipart/form-data">
<table class="form-table">

<input type="hidden" name="webpush_form" value="submitted" />

<tr>
<th scope="row"><?php 
        _e('Notification Title', 'wpwebpush');
        ?>
</th>
<td>
<fieldset>
<label><input type="radio" name="webpush_title" value="blog_title" <?php 
        echo $title_option === 'blog_title' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Use the Site Title', 'wpwebpush');
        ?>
: <b><?php 
        echo get_bloginfo('name');
        ?>
</b></label><br />
<label><input type="radio" name="webpush_title" value="custom" <?php 
        echo $title_option !== 'blog_title' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Custom:');
        ?>
</label>
<input type="text" name="webpush_title_custom" value="<?php 
        echo $title_option !== 'blog_title' ? $title_option : esc_attr__('Your custom title', 'wpwebpush');
        ?>
" class="long-text" />
</fieldset>
</td>
</tr>

<tr>
<th scope="row"><?php 
        _e('Notification Icon', 'wpwebpush');
        ?>
</th>
<td>
<fieldset>
<label><input type="radio" name="webpush_icon" value="" <?php 
        echo $icon_option === '' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Don\'t use any icon', 'wpwebpush');
        ?>
</label>
<br />
<?php 
        if (function_exists('get_site_icon_url')) {
            ?>
<label><input type="radio" name="webpush_icon" value="blog_icon" <?php 
            echo $icon_option === 'blog_icon' ? 'checked' : '';
            ?>
 /> <?php 
            _e('Use the Site Icon', 'wpwebpush');
            ?>
</label>
<?php 
            $site_icon_url = get_site_icon_url();
            if ($site_icon_url) {
                echo '<img src="' . $site_icon_url . '">';
            }
            ?>
<br />
<?php 
        }
        ?>
<label><input type="radio" name="webpush_icon" value="custom" <?php 
        echo $icon_option !== 'blog_icon' && $icon_option !== '' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Custom');
        ?>
</label>
<?php 
        if ($icon_option !== 'blog_icon' && $icon_option !== '') {
            echo '<img src="' . $icon_option . '">';
        }
        ?>
<input type="file" name="webpush_icon_custom" id="webpush_icon_custom" />
</fieldset>
</td>
</tr>

<tr>
<th scope="row"><?php 
        _e('Registration Behavior', 'wpwebpush');
        ?>
</th>
<td>
<fieldset>
<label><input type="radio" name="webpush_min_visits" value="0" <?php 
        echo $min_visits_option === 0 ? 'checked' : '';
        ?>
 /> <?php 
        _e('Ask the user to register as soon as he visits the site.', 'wpwebpush');
        ?>
</label><br />
<label><input type="radio" name="webpush_min_visits" value="custom" <?php 
        echo $min_visits_option !== 0 ? 'checked' : '';
        ?>
 /> <?php 
        _e('Ask the user to register after N visits:');
        ?>
</label>
<input type="text" name="webpush_min_visits_custom" value="<?php 
        echo $min_visits_option !== 0 ? $min_visits_option : 3;
        ?>
" class="small-text" />
</fieldset>
</td>
</tr>

<tr>
<th scope="row"><?php 
        _e('Push Triggers', 'wpwebpush');
        ?>
</th>
<td>
<fieldset>
  <?php 
        foreach ($allowed_triggers as $trigger) {
            ?>
  <label><input type="checkbox" name="webpush_triggers[]" value="<?php 
            echo esc_attr($trigger['key']);
            ?>
" <?php 
            echo in_array($trigger['key'], $triggers_option) ? 'checked' : '';
            ?>
 /> <?php 
            _e($trigger['text'], 'wpwebpush');
            ?>
</label><br />
  <?php 
        }
        ?>
</fieldset>
</td>
</tr>

</table>

<table class="form-table">

<h2 class="title"><?php 
        _e('GCM Configuration', 'wpwebpush');
        ?>
</h2>

<tr>
<th scope="row"><label for="webpush_gcm_key"><?php 
        _e('GCM Key', 'wpwebpush');
        ?>
</label></th>
<td><input name="webpush_gcm_key" type="text" value="<?php 
        echo $gcm_key_option;
        ?>
" class="regular-text code" /></td>
</tr>

<tr>
<th scope="row"><label for="webpush_gcm_sender_id"><?php 
        _e('GCM Sender ID', 'wpwebpush');
        ?>
</label></th>
<td><input name="webpush_gcm_sender_id" type="text" value="<?php 
        echo $gcm_sender_id_option;
        ?>
" class="code" /></td>
</tr>

</table>

<?php 
        submit_button(__('Save Changes'), 'primary');
        ?>

</form>

</div>

<?php 
    }
Ejemplo n.º 6
0
    public function tools()
    {
        if (isset($_POST['webpush_form']) && $_POST['webpush_form'] === 'submitted') {
            $title = '';
            if ($_POST['webpush_title'] === 'blog_title') {
                $title = get_bloginfo('name');
            } else {
                if ($_POST['webpush_title'] === 'custom') {
                    $title = $_POST['webpush_title_custom'];
                } else {
                    wp_die(__('Invalid value for the Notification Title', 'web-push'));
                }
            }
            $icon = '';
            if ($_POST['webpush_icon'] === 'blog_icon') {
                $icon = get_site_icon_url();
            } else {
                if ($_POST['webpush_icon'] === 'custom') {
                    $icon = $_POST['webpush_icon_custom'];
                } else {
                    wp_die(__('Invalid value for the Notification Icon', 'web-push'));
                }
            }
            WebPush_Main::sendNotification($title, $_POST['webpush_body'], $icon, $_POST['webpush_url'], null);
            ?>
<div class="updated"><p><strong><?php 
            _e('Notification sent.');
            ?>
</strong></p></div>
<?php 
        }
        $title_option = get_option('webpush_title');
        $icon_option = get_option('webpush_icon');
        ?>
<div class="wrap">
<h2><?php 
        _e('Send a custom push notification', 'web-push');
        ?>
</h2>

<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="webpush_form" value="submitted" />
<table class="form-table">
<tr>
<th scope="row"><?php 
        _e('Title', 'web-push');
        ?>
</th>
<td>
<fieldset>
<label><input type="radio" name="webpush_title" value="blog_title" <?php 
        echo $title_option === 'blog_title' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Use the Site Title', 'web-push');
        ?>
</label><br />
<label><input type="radio" name="webpush_title" value="custom" <?php 
        echo $title_option !== 'blog_title' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Custom:');
        ?>
</label>
<input type="text" id="webpush_title_custom" name="webpush_title_custom" value="<?php 
        echo $title_option !== 'blog_title' ? $title_option : esc_attr__('Your custom title', 'web-push');
        ?>
" class="regular-text" />
</fieldset>
</td>
</tr>

<tr>
<th scope="row"><?php 
        _e('Icon', 'web-push');
        ?>
</th>
<td>
<fieldset>
<label><input type="radio" name="webpush_icon" value="" <?php 
        echo $icon_option === '' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Don\'t use any icon', 'web-push');
        ?>
</label>
<br />
<?php 
        if (function_exists('get_site_icon_url')) {
            ?>
<label><input type="radio" name="webpush_icon" value="blog_icon" <?php 
            echo $icon_option === 'blog_icon' ? 'checked' : '';
            ?>
 /> <?php 
            _e('Use the Site Icon', 'web-push');
            ?>
</label>
<br />
<?php 
        }
        ?>
<label><input type="radio" name="webpush_icon" value="custom" <?php 
        echo $icon_option !== 'blog_icon' && $icon_option !== '' && $icon_option !== 'post_icon' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Custom:');
        ?>
</label>
<input type="hidden" id="webpush_icon_custom" name="webpush_icon_custom" value="<?php 
        echo $icon_url;
        ?>
" />
<input type="button" class="button" id="webpush_icon_custom_button" value="<?php 
        esc_attr_e('Select');
        ?>
"></input>
</fieldset>
</td>
</tr>

<tr>
<th scope="row"><label for="webpush_body"><?php 
        _e('Body', 'web-push');
        ?>
</label></th>
<td><textarea name="webpush_body" type="text" rows="5" cols="65" class="regular-text"><?php 
        esc_attr_e('Notification Body', 'web-push');
        ?>
</textarea></td>
</tr>

<tr>
<th scope="row"><label for="webpush_url"><?php 
        _e('URL', 'web-push');
        ?>
</label></th>
<td><input name="webpush_url" type="text" value="<?php 
        echo esc_attr(home_url());
        ?>
" class="regular-text code" />
<p class="description"><?php 
        _e('The URL to open when users click on the notification.', 'web-push');
        ?>
</p></td>
</tr>
</table>

<?php 
        submit_button(__('Send notification'), 'primary');
        ?>

</form>
<?php 
    }
Ejemplo n.º 7
0
<?php

/*
Plugin Name: WordPress Web Push
Text Domain: wpwebpush
*/
require_once plugin_dir_path(__FILE__) . 'wp-web-push-main.php';
require_once plugin_dir_path(__FILE__) . 'wp-web-push-db.php';
WebPush_Main::init();
if (is_admin()) {
    require_once plugin_dir_path(__FILE__) . 'wp-web-push-admin.php';
    WebPush_Admin::init();
}
register_activation_hook(__FILE__, array('WebPush_DB', 'on_activate'));
register_deactivation_hook(__FILE__, array('WebPush_DB', 'on_deactivate'));
register_uninstall_hook(__FILE__, array('WebPush_DB', 'on_uninstall'));
Ejemplo n.º 8
0
 public function update_check()
 {
     global $wpdb;
     if (self::VERSION === get_option('webpush_db_version')) {
         return;
     }
     $table_name = $wpdb->prefix . 'webpush_subscription';
     $sql = 'CREATE TABLE ' . $table_name . ' (
   `id` INT NOT NULL AUTO_INCREMENT,
   `endpoint` VARCHAR(300) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
   `userKey` VARCHAR(300) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
   `userAuth` BINARY(16) NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE (`endpoint`)
 );';
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     dbDelta($sql);
     update_option('webpush_db_version', self::VERSION);
     // Set default options.
     add_option('webpush_title', 'blog_title');
     add_option('webpush_icon', function_exists('get_site_icon_url') ? 'blog_icon' : '');
     add_option('webpush_min_visits', -1);
     add_option('webpush_subscription_button', true);
     add_option('webpush_prompt_interval', 3);
     add_option('webpush_gcm_key', '');
     add_option('webpush_gcm_sender_id', '');
     add_option('webpush_generate_manifest', true);
     self::generate_vapid_options();
     add_option('webpush_prompt_count', 0);
     add_option('webpush_accepted_prompt_count', 0);
     add_option('webpush_subscription_button_color', '#005189');
     Mozilla\WP_Serve_File::getInstance()->invalidate_files(array('subscription_button.css', 'bell.svg'));
     $default_triggers = WebPush_Main::get_triggers_by_key_value('enable_by_default', true);
     $default_triggers_keys = array();
     foreach ($default_triggers as $trigger) {
         $default_triggers_keys[] = $trigger['key'];
     }
     add_option('webpush_triggers', $default_triggers_keys);
 }