/**
  * An unauthenticated request to the JSON API to check the validity of a
  * username and an API key
  *
  * @return array
  */
 public static function authenticate()
 {
     // Make an unauthenticated request to the API
     $response = PushUp_Notifications_JSON_API::perform_unauthenticated_query(array('push.authenticate' => array('username' => PushUp_Notifications_Core::get_username(), 'api_key' => PushUp_Notifications_Core::get_api_key())));
     // If we got a response, let's use it
     if (true === $response['push.authenticate']) {
         // Cache the authentication
         self::_set_cached_authentication(true);
         // Authenticated
         return true;
     }
     // Cache the authentication as false
     self::_set_cached_authentication(false);
     // Boourns... not authenticated
     return false;
 }
 /**
  * Attempt to connect to the json API endpoint
  *
  * @param array $data
  * @return array|bool|mixed
  */
 protected static function _remote_post($data = array())
 {
     self::$_last_request = wp_remote_post(self::$_api_url, array('timeout' => 10, 'blocking' => true, 'body' => json_encode($data), 'sslverify' => false));
     if (is_wp_error(self::$_last_request) || empty(self::$_last_request)) {
         return false;
     }
     return self::$_last_request;
 }
    /**
     * Renders the checkbox and custom JavaScript needed to ensure that the checkbox performs the way we need it to.
     *
     * Note: there was an interesting problem for when the checkbox transitioned through the save processes and the
     * value of the 'push setting' would be wiped because the checkbox wasn't checked or simply not even posted but WAS
     * checked... This is the reason for the JavaScript below.
     */
    public static function post_submitbox_misc_actions()
    {
        // Get some post data
        $post_id = get_the_ID();
        $post_status = get_post_status($post_id);
        $post_type = get_post_type($post_id);
        // Bail if current user cannot edit others posts
        if (!current_user_can('pushup_push_posts', $post_id)) {
            return;
        }
        // Bail if not an allowed post type
        if (!self::_is_post_type_allowed($post_type)) {
            return;
        }
        // Get the push setting
        $value = self::get_push_setting($post_id);
        ?>

		<style type="text/css">
			.pushup-notification-pushed-icon {
				margin: 1px 8px 2px 1px;
				float: left;
			}
			.pushup-notification-pushed-time:before {
				speak: none;
				display: inline-block;
				padding: 0 2px 0 0;
				position: relative;
				vertical-align: top;
				-webkit-font-smoothing: antialiased;
				-moz-osx-font-smoothing: grayscale;
				text-decoration: none !important
				background: url('../images/pushup-grey.svg') no-repeat;
			}
		</style>

		<div class="misc-pub-section" id="pushup-notifications-container">

			<?php 
        if (!empty($value['time']) && $value['status'] === 'pushed') {
            ?>

				<img class="pushup-notification-pushed-icon" src="<?php 
            echo plugin_dir_url(__FILE__);
            ?>
../images/pushup-grey.svg" width="16" height="16" />
				<span class="pushup-notification-pushed-time"><?php 
            printf(__('Pushed on: %s', 'pushup'), '<strong>' . date_i18n('M j, Y @ G:i', $value['time'], false) . '</strong>');
            ?>
</span>

			<?php 
        } elseif (PushUp_Notifications_Authentication::is_authenticated() && PushUp_Notifications_JSON_API::is_domain_enabled()) {
            ?>

				<?php 
            wp_nonce_field('push_notification_nonce', 'post_nonce_' . $post_id);
            ?>
				<input <?php 
            checked(!empty($value['time']));
            ?>
 title="<?php 
            esc_html_e('Push to registered OS X subscribers upon publication', 'pushup');
            ?>
" type="checkbox" name="pushup-notification-creation" id="pushup-notification-creation" />

				<?php 
            if ('error' === $value['status']) {
                ?>
					<label title="<?php 
                esc_html_e('Push to registered OS X subscribers upon publication', 'pushup');
                ?>
" for="pushup-notification-creation"><?php 
                esc_html_e('Push Error. Try again!', 'pushup');
                ?>
</label>
				<?php 
            } else {
                ?>
					<label title="<?php 
                esc_html_e('Push to registered OS X subscribers upon publication', 'pushup');
                ?>
" for="pushup-notification-creation"><?php 
                esc_html_e('Push desktop notification', 'pushup');
                ?>
</label>
				<?php 
            }
            ?>

				<?php 
            if ($post_status === 'published' || $value['time'] === 0) {
                ?>
					<input type="hidden" name="pushup-notification-creation" value="<?php 
                echo !empty($value['time']) ? 'on' : 'off';
                ?>
" id="pushup-notification-hidden-input" />
				<?php 
            }
            ?>

			<?php 
        }
        ?>

		</div>

		<script type="text/javascript">
			( function( window, $, undefined ) {
				var $container = $( document.getElementById( 'pushup-notifications-container' ) );
				var $checkbox = $( document.getElementById( 'pushup-notification-creation' ) );

				function checkBoxChanged( event ) {
					var $checkboxes = $container.find( 'input[type=checkbox]:checked' );

					// check to see if any of these checkboxes match the one we're looking for already
					var isChecked = false;
					$checkboxes.each( function( index, element ) {
						if ( element.getAttribute( 'id' ) === 'pushup-notification-creation' ) {
							isChecked = true;
						}
					} );

					if ( isChecked === true ) {
						removeHiddenInput();
					} else {
						addHiddenInput();
					}
				}

				function removeHiddenInput() {
					$( document.getElementById( 'pushup-notification-hidden-input' ) ).remove();
				}

				function addHiddenInput() {
					var input = document.createElement( 'input' );
					input.type = 'hidden';
					input.name = 'pushup-notification-creation';
					input.value = 'off';
					input.id = 'pushup-notification-hidden-input';

					$container.append( input );
				}

				$checkbox.on( 'change', checkBoxChanged );

			} )( window, jQuery );
		</script>

		<?php 
    }
 /**
  * Helper function to return the settings fields, their descriptions, and
  * help setup their callbacks for rendering.
  *
  * @return array
  */
 public static function get_settings_fields()
 {
     // Authentication settings
     $settings_fields = array('connectivity' => array('title' => esc_html__('Service Connectivity', 'pushup'), 'callback' => array(__CLASS__, '_render_account_connectivity_field'), 'section' => 'default'), 'username' => array('title' => esc_html__('PushUp Username', 'pushup'), 'callback' => array(__CLASS__, '_render_account_username_field'), 'section' => 'default'), 'api-key' => array('title' => esc_html__('PushUp API Key', 'pushup'), 'callback' => array(__CLASS__, '_render_account_api_key_field'), 'section' => 'default'));
     // The following fields are only for authenticated users
     if (PushUp_Notifications_Authentication::is_authenticated() && PushUp_Notifications_JSON_API::is_domain_enabled()) {
         /** Analytics *****************************************************/
         // Skip if we can't get any analytics
         if (PushUp_Notifications_JSON_API::get_analytics()) {
             // Subscribers chart
             $settings_fields['subscribers'] = array('title' => esc_html__('Subscribers', 'pushup'), 'callback' => array(__CLASS__, '_render_analytics_pie_field'), 'section' => 'analytics', 'args' => array('canvas_id' => 'conversion', 'desc' => esc_html__('Number of Subscribers', 'pushup'), 'data_points' => array(esc_html('Declined', 'pushup') => array('color' => '#63bde4', 'path' => array('total_declined')), esc_html('Accepted', 'pushup') => array('color' => 'rgba(74,198,143,1)', 'path' => array('total_granted')))));
             // Pushes chart
             $settings_fields['pushes'] = array('title' => esc_html__('Push Requests', 'pushup'), 'callback' => array(__CLASS__, '_render_analytics_pie_field'), 'section' => 'analytics', 'desc' => '', 'args' => array('canvas_id' => 'requests', 'desc' => esc_html__('Total Number of Posted Items Pushed to Subscribers', 'pushup'), 'data_points' => array(esc_html('This Month', 'pushup') => array('color' => '#63bde4', 'path' => array('total_monthly_pushes', 'total_push_requests')), esc_html('All Time', 'pushup') => array('color' => 'rgba(74,198,143,1)', 'path' => array('total_all_time_pushes', 'total_push_requests')))));
             // Notifications chart
             $settings_fields['notifications'] = array('title' => esc_html__('Notifications', 'pushup'), 'callback' => array(__CLASS__, '_render_analytics_pie_field'), 'section' => 'analytics', 'args' => array('canvas_id' => 'recipients', 'desc' => esc_html__('Total Number of Notifications Sent to Subscribers', 'pushup'), 'data_points' => array(esc_html__('This Month', 'pushup') => array('color' => '#63bde4', 'path' => array('total_monthly_pushes', 'total_push_recipients')), esc_html__('All Time', 'pushup') => array('color' => 'rgba(74,198,143,1)', 'path' => array('total_all_time_pushes', 'total_push_recipients')))));
         }
         /** Display *******************************************************/
         // Website Name
         $settings_fields['website_name'] = array('title' => esc_html__('Website Name', 'pushup'), 'callback' => array(__CLASS__, '_render_display_website_name_field'), 'section' => 'display');
         // Notification Title
         $settings_fields['post_title'] = array('title' => esc_html__('Notification Title', 'pushup'), 'callback' => array(__CLASS__, '_render_display_post_title_field'), 'section' => 'display');
         // Icons
         if (PushUp_Notifications_JSON_API::get_icon_data()) {
             $settings_fields['icon'] = array('title' => esc_html__('Icon(s)', 'pushup'), 'callback' => array(__CLASS__, '_render_display_icon_field'), 'section' => 'display');
         }
         // Prompt Visitor
         $settings_fields['prompt'] = array('title' => esc_html__('Offer Notifications', 'pushup'), 'callback' => array(__CLASS__, '_render_display_prompt_field'), 'section' => 'display');
     }
     return $settings_fields;
 }
<?php

/**
 * Plugin Name: PushUp Notifications
 * Plugin URI:  http://pushupnotifications.com
 * Description: Desktop push notifications for your WordPress powered website, starting with OS X Mavericks.
 * Version:     1.2.2
 * Author:      10up
 * Author URI:  http://10up.com
 * License:     GPLv2 or later
 */
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
// Include classes
require_once plugin_dir_path(__FILE__) . 'includes/class-core.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-json-api.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-authentication.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-push-notifications.php';
// Instantiate objects
PushUp_Notifications_Core::instance();
PushUp_Notifications_JSON_API::instance();
PushUp_Notifications_Authentication::instance();
PushUp_Notifications::instance();