__( '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 "
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; }