/** * Sends a list of actions to the API to be performed as needed. * * @param array $actions * @return array|bool|mixed */ public static function perform_authenticated_query($actions = array(), $username = '', $api_key = '') { // Use username if none passed if (empty($username)) { $username = PushUp_Notifications_Core::get_username(); } // Use api key is none passed if (empty($api_key)) { $api_key = PushUp_Notifications_Core::get_api_key(); } $request = self::_remote_post(array('username' => $username, 'api_key' => $api_key, 'actions' => $actions)); return json_decode(wp_remote_retrieve_body($request), true); }
<?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();
/** * Handles connecting to our API via `wp_remote_post()`. This function is responsible for pushing the notification * details to our push notification server which will then process everything and validate the API request before * sending it. * * @param string $title * @param string $body * @param string $action * @param array $url_arguments * @return bool */ public static function send_message($title = '', $body = '', $action = '', $url_arguments = array()) { $username = PushUp_Notifications_Core::get_username(); $api_key = PushUp_Notifications_Core::get_api_key(); if (empty($username) || empty($api_key)) { return false; } $params = array('method' => 'POST', 'timeout' => 12, 'sslverify' => false, 'body' => array('title' => $title, 'body' => $body, 'action' => $action, 'username' => $username, 'api_key' => $api_key, 'url_arguments' => $url_arguments, 'mode' => self::$_recipient_mode, 'domain' => PushUp_Notifications_Core::get_site_url())); if (self::$_recipient_mode === 'single') { if (is_null(self::$_device_token)) { return false; } $params['body']['device_token'] = self::$_device_token; } /** * Filter the push notifications API URL. * * @since 1.0.0 * * @param string $_api_url The push notifications API URL. */ $api_url = apply_filters('pushup_notifications_api_url', self::$_api_url) . '/send'; // Default return value $retval = array('time' => current_time('timestamp'), 'status' => 'error'); // Attempt to make the remote request $result = wp_remote_post($api_url, $params); if (is_wp_error($result)) { return $retval; } // Parse the body of the json request $body = wp_remote_retrieve_body($result); $data = @json_decode($body, true); if (!is_array($data)) { return $retval; } // Set the status to 'pushed' if (isset($data['status']) && $data['status'] === 'ok') { $retval['status'] = 'pushed'; } // Return the return value return $retval; }
/** * Get any pre-existing authentication data, if it exists. * * @return mixed False if no data exists. Array of data if data exists. */ public static function get_authentication_data() { // Get the required data for a valid authentication $domain = PushUp_Notifications_Core::get_site_url(); $websitePushID = PushUp_Notifications_Core::get_website_push_id(); $userID = PushUp_Notifications_Core::get_user_id(); // Return an array of authenticated data if valid if (!empty($domain) && !empty($websitePushID) && !empty($userID)) { return array('domain' => $domain, 'website_push_id' => $websitePushID, 'user_id' => $userID); } // Return false if not a valid authentication return false; }