예제 #1
0
 public function __construct()
 {
     self::$ALLOWED_TRIGGERS = array(array('text' => __('When a post is published.', 'web-push'), 'key' => 'new-post', 'enable_by_default' => true, 'hook' => 'transition_post_status', 'action' => 'on_transition_post_status'), array('text' => __('When a post is updated.', 'web-push'), 'key' => 'update-post', 'parentKey' => 'new-post', 'enable_by_default' => true, 'hook' => 'transition_post_status', 'action' => 'on_transition_post_status'), array('text' => __('Right after subscription (useful to show to users what notifications look like).', 'web-push'), 'key' => 'on-subscription', 'enable_by_default' => true));
     self::add_trigger_handlers();
     if (get_option('webpush_subscription_button')) {
         add_action('wp_footer', array($this, 'add_subscription_button'), 9999);
     }
     Mozilla\WP_SW_Manager::get_manager()->sw()->add_content(array($this, 'service_worker'));
     add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_scripts'));
     add_filter('query_vars', array($this, 'on_query_vars'), 10, 1);
     add_action('parse_request', array($this, 'on_parse_request'));
     add_action('wp_ajax_webpush_register', array($this, 'handle_register'));
     add_action('wp_ajax_nopriv_webpush_register', array($this, 'handle_register'));
     add_action('wp_ajax_webpush_unregister', array($this, 'handle_unregister'));
     add_action('wp_ajax_nopriv_webpush_unregister', array($this, 'handle_unregister'));
     add_action('wp_ajax_webpush_get_payload', array($this, 'handle_get_payload'));
     add_action('wp_ajax_nopriv_webpush_get_payload', array($this, 'handle_get_payload'));
     add_action('wp_ajax_webpush_prompt', array($this, 'handle_prompt'));
     add_action('wp_ajax_nopriv_webpush_prompt', array($this, 'handle_prompt'));
     $senderID = get_option('webpush_gcm_sender_id');
     if ($senderID && get_option('webpush_generate_manifest')) {
         $manifestGenerator = Mozilla\WebAppManifestGenerator::getInstance();
     }
     $wpServeFile = Mozilla\WP_Serve_File::getInstance();
     $wpServeFile->add_file('subscription_button.css', array($this, 'subscriptionButtonCSSGenerator'));
     $wpServeFile->add_file('bell.svg', array($this, 'bellSVGGenerator'));
 }
예제 #2
0
 function test_manifest_generation()
 {
     Mozilla\WebAppManifestGenerator::getInstance()->set_field('test', 'Marco');
     $result = Mozilla\WebAppManifestGenerator::getInstance()->manifestJSONGenerator();
     $this->assertEquals('{"start_url":"\\/","test":"Marco"}', $result['content']);
     $this->assertEquals('application/manifest+json', $result['contentType']);
 }
예제 #3
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'));
        $subscription_button_option = get_option('webpush_subscription_button');
        $subscription_button_color_option = get_option('webpush_subscription_button_color');
        $prompt_interval_option = get_option('webpush_prompt_interval');
        $triggers_option = get_option('webpush_triggers');
        $gcm_key_option = get_option('webpush_gcm_key');
        $gcm_sender_id_option = get_option('webpush_gcm_sender_id');
        $generate_manifest_option = get_option('webpush_generate_manifest');
        if (USE_VAPID) {
            // Regenerate VAPID info if needed (for example, when the user installs the needed
            // dependencies).
            WebPush_DB::generate_vapid_options();
            $vapid_key_option = get_option('webpush_vapid_key');
            $vapid_subject_option = get_option('webpush_vapid_subject');
            $vapid_audience_option = get_option('webpush_vapid_audience');
        }
        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', 'web-push'));
                }
            }
            if ($_POST['webpush_icon'] === '') {
                $icon_option = '';
            } else {
                if ($_POST['webpush_icon'] === 'blog_icon') {
                    $icon_option = 'blog_icon';
                } else {
                    if ($_POST['webpush_icon'] === 'post_icon') {
                        $icon_option = 'post_icon';
                    } else {
                        if ($_POST['webpush_icon'] === 'custom') {
                            $icon_option = $_POST['webpush_icon_custom'];
                        } else {
                            wp_die(__('Invalid value for the Notification Icon', 'web-push'));
                        }
                    }
                }
            }
            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 {
                    if ($_POST['webpush_min_visits'] === '-1') {
                        $min_visits_option = -1;
                    } else {
                        wp_die(__('Invalid value for `Registration Behavior`', 'web-push'));
                    }
                }
            }
            $subscription_button_option = isset($_POST['webpush_subscription_button']) ? true : false;
            $subscription_button_color_option = $this->sanitize_hex_color($_POST['webpush_subscription_button_color']);
            if (!$subscription_button_color_option) {
                wp_die(__('Invalid color for the subscription button', 'web-push'));
            } else {
                if ($subscription_button_color_option !== get_option('webpush_subscription_button_color')) {
                    update_option('webpush_subscription_button_color', $subscription_button_color_option);
                    Mozilla\WP_Serve_File::getInstance()->invalidate_files(array('subscription_button.css', 'bell.svg'));
                }
            }
            $prompt_interval_option = intval($_POST['webpush_prompt_interval']);
            $triggers_option = isset($_POST['webpush_triggers']) ? $_POST['webpush_triggers'] : array();
            foreach ($triggers_option as $trigger_option) {
                if (!WebPush_Main::get_trigger_by_key_value('key', $trigger_option)) {
                    wp_die(sprintf(__('Invalid value in Push Triggers: %s'), $trigger_option), 'web-push');
                }
            }
            $gcm_key_option = $_POST['webpush_gcm_key'];
            $gcm_sender_id_option = $_POST['webpush_gcm_sender_id'];
            if ($gcm_sender_id_option != get_option('webpush_gcm_sender_id')) {
                $manifestGenerator = Mozilla\WebAppManifestGenerator::getInstance();
                $manifestGenerator->set_field('gcm_sender_id', $gcm_sender_id_option);
                $manifestGenerator->set_field('gcm_user_visible_only', true);
            }
            $generate_manifest_option = isset($_POST['webpush_generate_manifest']) ? true : false;
            if (USE_VAPID) {
                $vapid_key_option = $_POST['webpush_vapid_key'];
                $vapid_subject_option = $_POST['webpush_vapid_subject'];
                $vapid_audience_option = $_POST['webpush_vapid_audience'];
            }
            update_option('webpush_title', $title_option);
            update_option('webpush_icon', $icon_option);
            update_option('webpush_min_visits', $min_visits_option);
            update_option('webpush_subscription_button', $subscription_button_option);
            update_option('webpush_prompt_interval', $prompt_interval_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);
            update_option('webpush_generate_manifest', $generate_manifest_option);
            if (USE_VAPID) {
                update_option('webpush_vapid_key', $vapid_key_option);
                update_option('webpush_vapid_audience', $vapid_audience_option);
                update_option('webpush_vapid_subject', $vapid_subject_option);
            }
            ?>
<div class="updated"><p><strong><?php 
            _e('Settings saved.');
            ?>
</strong></p></div>
<?php 
        }
        $icon_url = '';
        if ($icon_option !== 'blog_icon' && $icon_option !== '' && $icon_option !== 'post_icon') {
            $icon_url = $icon_option;
        }
        ?>

<div class="wrap">
<h2><?php 
        _e('Web Push Settings', 'web-push');
        ?>
</h2>

<form method="post" action="" enctype="multipart/form-data">
<h2 class="title"><?php 
        _e('Notification UI Options', 'web-push');
        ?>
</h2>
<p><?php 
        _e('In this section, you can customize the information that appears in the notifications that will be shown to users.<br> Here\'s a preview of the notification:', 'web-push');
        ?>
</p>

<div style="border: 1px solid darkgrey; overflow: auto; display: inline-block; background-color: lightgrey;">
<h3 id="notification-title" style="margin-left: 10px;"></h3>
<div style="float: left; margin-left: 10px; margin-bottom:10px;"><img id="notification-icon" style="display: block; max-width: 64px; max-height: 64px;"></div>
<div id="notification-text" style="margin-left: 90px; padding-right: 50px;"><p><?php 
        _e('The title of your post.', 'web-push');
        ?>
</p></div>
</div>

<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 />
<label><input type="radio" name="webpush_icon" value="post_icon" <?php 
        echo $icon_option === 'post_icon' ? 'checked' : '';
        ?>
 /> <?php 
        _e('Use the Post Thumbnail', '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>
</table>


<h2 class="title"><?php 
        _e('Subscription Behavior', 'web-push');
        ?>
</h2>
<p><?php 
        _e('In this section, you can customize the subscription behavior and tailor it to your site. We suggest limiting automatic prompting to avoid nagging users and always giving the option to subscribe/unsubscribe through the subscription button.', 'web-push');
        ?>
</p>

<table class="form-table">
<tr>
<th scope="row"></th>
<td>
<object id="webpush_subscription_button_svg" data="<?php 
        echo Mozilla\WP_Serve_File::get_relative_to_host_root_url('bell.svg');
        ?>
" type="image/svg+xml" style="max-width:64px;max-height:64px;"></object><br>
<input type="text" value="<?php 
        echo $subscription_button_color_option;
        ?>
" name="webpush_subscription_button_color" class="webpush_subscription_button_color" data-default-color="#005189" /><br>
<label><input type="checkbox" name="webpush_subscription_button" <?php 
        echo $subscription_button_option ? 'checked' : '';
        ?>
 /> <?php 
        _e('Show subscription icon', 'web-push');
        ?>
</label>
<p class="description"><?php 
        _e('A button in the bottom-right corner of the page that the user can use to subscribe/unsubscribe. We suggest enabling it to offer an easy way for users to manage their subscription.', 'web-push');
        ?>
</p>
</td>
</tr>

<tr>
<th scope="row"><?php 
        _e('Automatic prompting', 'web-push');
        ?>
</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', 'web-push');
        ?>
</label><br />
<label><input type="radio" name="webpush_min_visits" value="custom" <?php 
        echo $min_visits_option !== 0 && $min_visits_option !== -1 ? 'checked' : '';
        ?>
 /> <?php 
        _e('Ask the user to register after N visits:', 'web-push');
        ?>
</label>
<input type="number" name="webpush_min_visits_custom" value="<?php 
        echo $min_visits_option !== 0 && $min_visits_option !== -1 ? $min_visits_option : 3;
        ?>
" class="small-text" /><br />
<label><input type="radio" name="webpush_min_visits" value="-1" <?php 
        echo $min_visits_option === -1 ? 'checked' : '';
        ?>
 /> <?php 
        _e('Never automatically ask the user to register', 'web-push');
        ?>
</label>
</fieldset>
<p class="description"><?php 
        _e('Limiting automatic prompting is suggested to avoid nagging users (unless you know that your visitors are really interested).', 'web-push');
        ?>
</p>
</td>
</tr>

<tr>
<th scope="row"><label for="webpush_prompt_interval"><?php 
        _e('Interval between prompts', 'web-push');
        ?>
</label></th>
<td><input name="webpush_prompt_interval" type="number" value="<?php 
        echo $prompt_interval_option;
        ?>
" class="small-text" />
<p class="description"><?php 
        _e('If the user declines or dismisses the prompt, this is the time interval (in days) to wait before prompting again.', 'web-push');
        ?>
</p>
</td>
</tr>
</table>


<h2 class="title"><?php 
        _e('Push Triggers', 'web-push');
        ?>
</h2>
<p><?php 
        _e('Select which events should trigger sending a push notification to users.', 'web-push');
        ?>
</p>

<table class="form-table">
<tr>
<th scope="row"></th>
<td>
<fieldset>
  <?php 
        foreach ($allowed_triggers as $trigger) {
            ?>
  <label><input type="checkbox" name="webpush_triggers[]" id="webpush_trigger_<?php 
            echo $trigger['key'];
            ?>
" value="<?php 
            echo esc_attr($trigger['key']);
            ?>
" <?php 
            echo in_array($trigger['key'], $triggers_option) ? 'checked' : '';
            ?>
 <?php 
            if (array_key_exists('parentKey', $trigger)) {
                echo 'parent="' . esc_attr($trigger['parentKey']) . '"';
            }
            ?>
 /> <?php 
            echo $trigger['text'];
            ?>
</label><br />
  <?php 
        }
        ?>
</fieldset>
<p class="description"><?php 
        _e('N.B.: You can override these options for individual posts when you create/edit them.', 'web-push');
        ?>
</p>
</td>
</tr>
</table>


<h2 class="title"><?php 
        _e('Voluntary Application Server Identification (VAPID)', 'web-push');
        ?>
</h2>
<p><?php 
        _e('VAPID is useful to monitor your push messages. It allows your server to submit information about itself to the push service, which improves application stability, exception handling, and security. <b>It is automatically configured for you.</b>', 'web-push');
        ?>
</p>

<?php 
        if (USE_VAPID) {
            ?>
<input type="button" class="button" id="webpush_vapid_show_config" value="<?php 
            esc_attr_e('Show advanced options', 'web-push');
            ?>
"></input>
<br>
<table class="form-table" id="vapid_config" style="display:none;">
<tr>
<th scope="row"><label for="webpush_vapid_key"><?php 
            _e('Private Key', 'web-push');
            ?>
</label></th>
<td><textarea name="webpush_vapid_key" id="webpush_vapid_key" type="text" rows="5" cols="65" class="regular-text code"><?php 
            echo $vapid_key_option;
            ?>
</textarea>
<p class="description"><?php 
            _e('The private key used to sign your push notifications.', 'web-push');
            ?>
</p></td>
</tr>

<tr>
<th scope="row"><?php 
            _e('Public Key', 'web-push');
            ?>
</th>
<td><code><b><span id="webpush_vapid_public_key"><?php 
            echo $this->get_public_key($vapid_key_option);
            ?>
</span></b></code></td>
</tr>

<tr>
<th scope="row"><label for="webpush_vapid_audience"><?php 
            _e('Audience', 'web-push');
            ?>
</label></th>
<td><input name="webpush_vapid_audience" type="url" value="<?php 
            echo $vapid_audience_option;
            ?>
" class="regular-text code" />
<p class="description"><?php 
            _e('The origin URL of the sender.', 'web-push');
            ?>
</p></td>
</tr>

<tr>
<th scope="row"><label for="webpush_vapid_subject"><?php 
            _e('Subject', 'web-push');
            ?>
</label></th>
<td><input name="webpush_vapid_subject" type="url" value="<?php 
            echo $vapid_subject_option;
            ?>
" class="regular-text code" />
<p class="description"><?php 
            _e('The primary contact in case something goes wrong.', 'web-push');
            ?>
</p></td>
</tr>
<?php 
        } else {
            ?>
<p><?php 
            _e('Unfortunately, VAPID can\'t be enabled on your website, because one or more prerequisites are missing.', 'web-push');
            ?>
</p>
<table class="form-table">
<tr>
<th scope="row"><?php 
            _e('VAPID Prerequisites:', 'web-push');
            ?>
</th>
<td>
<ul style="list-style-type: circle;">
<li style="color:<?php 
            echo version_compare(phpversion(), '5.4') >= 0 ? 'green' : 'red';
            ?>
;">PHP 5.4+</li>
<li style="color:<?php 
            echo function_exists('mcrypt_encrypt') ? 'green' : 'red';
            ?>
;"><?php 
            _e('mcrypt extension', 'web-push');
            ?>
</li>
<li style="color:<?php 
            echo function_exists('gmp_mod') ? 'green' : 'red';
            ?>
;"><?php 
            _e('gmp extension', 'web-push');
            ?>
</li>
</ul>
</td>
</tr>
<?php 
        }
        ?>
</table>


<a href="#gcm" name="gcm" style="text-decoration:none;"><h2 class="title"><?php 
        _e('Google Chrome Support', 'web-push');
        ?>
</h2></a>
<p><?php 
        _e('To configure Google Chrome support, follow the steps in <a href="https://developers.google.com/web/fundamentals/getting-started/push-notifications/step-04" target="_blank">Make a project on the Google Developer Console</a> to configure Google Cloud Messaging (GCM), then copy the <i>GCM API Key</i> and <i>Project Number</i> into the fields below.', 'web-push');
        ?>
</p>

<table class="form-table">
<tr>
<th scope="row"><label for="webpush_gcm_key"><?php 
        _e('GCM API Key', 'web-push');
        ?>
</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 Project Number', 'web-push');
        ?>
</label></th>
<td><input name="webpush_gcm_sender_id" id="webpush_gcm_sender_id" type="text" value="<?php 
        echo $gcm_sender_id_option;
        ?>
" class="code" /></td>
</tr>
</table>

<p><?php 
        _e('The GCM project number should be added to your Web App Manifest. You can either let the plugin generate a manifest for you or add the info to your own manifest (if you already have one). The plugin is compatible with the <a href="https://wordpress.org/plugins/add-to-home-screen">Add to Home Screen plugin</a>. <b>You only need to disable the following option if you have created your own manifest manually</b>.', 'web-push');
        ?>
</p>
<label><input type="checkbox" name="webpush_generate_manifest" id="webpush_generate_manifest" <?php 
        echo $generate_manifest_option ? 'checked' : '';
        ?>
 /> <?php 
        _e('The plugin will automatically create a Web App Manifest for you and add it to your page.', 'web-push');
        ?>
</label>
<br><br>
<p id="webpush_generate_manifest_text" style="display:none;"><?php 
        printf(__('You need to add %s and %s to your manifest', 'web-push'), '<b>"gcm_sender_id": "<span id="webpush_generate_manifest_sender_id_field">%s"</b>', '<b>"gcm_user_visible_only": true</b>');
        ?>
</p>

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

</form>

</div>

<?php 
    }