/** * 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; }
/** * Sets this domain's website name via the JSON API. * * @param string $name * @return bool */ public static function set_website_name($name = '', $username = '', $api_key = '') { return self::perform_authenticated_query(array('push.domain.config.setWebsiteName' => array('domain' => PushUp_Notifications_Core::get_site_url(), 'website_name' => sanitize_text_field($name))), $username, $api_key); }
/** * 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; }