__( 'Display your App.net status.', 'adnstatus' ), ) // Args ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); $username = $instance['username']; $accesstoken = $instance['accesstoken']; $code='available'; $status=''; //Standard codes as defined for the user annotation - https://github.com/appdotnet/api-spec/issues/227#issuecomment-10209157 $codetextArr = array('available'=>'Available', 'busy'=>'Busy', 'away'=>'Away', 'dnd'=>'Do Not Disturb','xa'=>'Extended Away'); $image = "adnavailable.png"; try { //Retrieve the user annotation for org.xmpp.presence //Set code, status, and image $url = 'https://alpha-api.app.net/stream/0/users/@'.$username.'?include_annotations=1'; $user = json_decode(wp_remote_retrieve_body(wp_remote_get( $url, array( 'sslverify' => false ) ) ), true); foreach ($user['data']['annotations'] as $data) { if($data['type'] == 'org.xmpp.presence') { $code = $data['value']['code']; $status = stripslashes($data['value']['status']); } } $codetext = $codetextArr[$code]; if($code != 'available') { $image = "adnbusy.png"; } } catch (Exception $e) { } echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; ?>

"; echo "
"; echo ""; echo "
"; echo "

"; echo "

"; echo "

Reload Page

"; echo "

"; } echo $after_widget; } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = strip_tags( $new_instance['title'] ); $instance['username'] = strip_tags( $new_instance['username'] ); $instance['accesstoken'] = strip_tags( $new_instance['accesstoken'] ); return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { //From the App.net application setup. $client_id = 'sDE9kUcx8LfRWUqXNmdrsHNqz2DvpNMM'; //This page displays the App.net access token. $callback = 'http://whitleymedia.com/adn/adnstatus'; if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'New title', 'adnstatus' ); } if ( isset( $instance[ 'username' ] ) ) { $username = $instance[ 'username' ]; } if ( isset( $instance[ 'accesstoken' ] ) ) { $accesstoken = $instance[ 'accesstoken' ]; } ?>

Get Access Token

admin_url('admin-ajax.php'),'adnStatusNonce'=>wp_create_nonce('adnstatus-nonce'))); } } function adnstatus_update_ajax() { $nonce = $_POST['adnStatusNonce']; // check to see if the submitted nonce matches with the // generated nonce we created earlier if ( ! wp_verify_nonce( $nonce, 'adnstatus-nonce' ) ) die ( 'Busted!'); // ignore the request if the current user doesn't have // sufficient permissions if ( current_user_can('manage_options') ) { // get the submitted parameters $accesstoken = $_POST['accesstoken']; $status_code = $_POST['status_code']; $status = $_POST['status']; try { //Get the required properties (name, locale, timezone, description) $headers = array( 'Authorization' => 'Bearer '.$accesstoken, 'Content-Type' => 'application/json' ); $url = 'https://alpha-api.app.net/stream/0/users/me'; $user = json_decode(wp_remote_retrieve_body(wp_remote_get( $url, array( 'sslverify' => false, 'headers' => $headers ) ) ), true); $data = json_encode( array ( 'name' => $user['data']['name'], 'locale' => $user['data']['locale'], 'timezone' => $user['data']['timezone'], 'description' => array ( 'text' => $user['data']['description']['text'] ), 'annotations' => array(array ( 'type' => 'org.xmpp.presence', 'value' => array ( 'code' => $status_code, 'status' => $status ) )) )); $user = json_decode(wp_remote_retrieve_body(wp_remote_request( $url.'?include_user_annotations=1', array('method' => 'PUT','sslverify' => false, 'headers' => $headers, 'body' => $data ) ) ), true); $response = $user['meta']['code']; } catch(Exception $e){} // response output echo $response; } // IMPORTANT: don't forget to "exit" exit; }