/** * 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; }
/** * 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); }
/** * 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; }