'aboutme_widget', 'description' => __( 'Display your about.me profile with thumbnail', 'aboutme-widget' ) ); parent::__construct( 'aboutme_widget', __( 'About.me Widget', 'aboutme-widget' ), $widget_ops ); } /** * Front-end display of widget. * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { //If there is no client_id alotted yet or some error, return if ( empty($instance['client_id']) || 0 != $instance['error'] ) return; extract( $args, EXTR_SKIP ); $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] ); $fontsize = empty( $instance['fontsize'] ) ? 'large' : $instance['fontsize']; $photo = empty( $instance['photo'] ) ? 'background' : $instance['photo']; $username = empty( $instance['username'] ) ? '' : $instance['username']; //We need to check the key existence as this option was absent in initial release, otherwise widget might break $headline = array_key_exists( 'headline', $instance )? $instance['headline'] : "1"; $biography = array_key_exists( 'biography', $instance )? $instance['biography'] : "1"; $apps = array_key_exists( 'apps', $instance )? $instance['apps'] : "1"; /*$links = array_key_exists( 'links', $instance )? $instance['links'] : "1";*/ //If no username is there, return if ( empty( $username ) ) return; $data = get_transient( 'am_' . $username . '_data' ); //if transient data got expired create new data if ( false === $data ) { $url = 'https://api.about.me/api/v2/json/user/view/' . $username . '?client_id=' . $instance['client_id'] . '&extended=true&on_match=true&strip_html=false'; $data = $this->get_api_content( $url ); if ( false !== $data ) { $data = $this->extract_api_data( $data ); if ( ! empty( $data ) ) { //Store this profile data in database set_transient( 'am_' . $username . '_data', $data, self::CACHE_TIME ); } else { //If empty profile data, return return; } } else { //Some wrong happen in getting profle response from aboutme server, so return return; } } //Display the profile: // if any key value is not present in stored data, delete the data as it is not in proper format $keys = array( 'app_icons', 'link_icons', 'profile_url', 'thumbnail', 'avatar', 'first_name', 'last_name', 'header', 'bio' ); foreach ( $keys as $k => $val ) { if ( ! array_key_exists( $val, $data ) ) { delete_transient( 'am_' . $username . '_data' ); return; } } //Check the non emptyness of $data if ( is_array( $data ) && ! empty( $data ) ) { ?> ' . esc_attr( $data['first_name'] ) . ' ' . esc_attr( $data['last_name'] ) . ''; } if( $fontsize != 'no-name' ) { echo '

' . esc_attr( $data['first_name'] ) . ' ' . esc_attr( $data['last_name'] ) . '

'; } //If user opts to show headline show that if ( $headline && ! empty( $data['header'] ) ) echo '

' . esc_attr( $data['header'] ) . '

'; //If user opts to show bio show that if ( $biography && ! empty( $data['bio'] ) ) { $biostr = '

' . str_replace( "\n", '

', wp_kses_data( $data['bio'] ) ) . '

'; } else { $biostr = ''; } echo '
' . $biostr . '
'; //If user opts to show apps show that if ( $apps && count( $data['app_icons'] ) > 0 ) { echo '
'; foreach ( $data['app_icons'] as $v ) { echo ''; } echo '
'; } //If user opts to show links show that /* if ( $links && count( $data['link_icons'] ) > 0 ) { echo ''; } */ echo $after_widget; } } /** * Sanitize widget form values as they are saved. * * @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 ) { $discard = array( 'https://about.me/', 'http://about.me/', 'about.me/' ); $username = empty( $new_instance['username'] ) ? '' : trim($new_instance['username']); $new_instance['username'] = trim( strip_tags( stripslashes( str_replace( $discard, '', $username ) ) ) ); $pos = strpos( $new_instance['username'], '/' ); if( false !== $pos) { $new_instance['username'] = substr($new_instance['username'], 0 , $pos); } $new_instance['username'] = trim($new_instance['username']); $username = $new_instance['username']; $src_url = empty( $new_instance['src_url'] ) ? get_site_url() : $new_instance['src_url']; $new_instance['src_url'] = str_ireplace( array('https://','http://'), '' , $src_url ); $new_instance['headline'] = array_key_exists('headline', $new_instance) ? '1' : '0'; $new_instance['biography'] = array_key_exists('biography', $new_instance) ? '1' : '0'; $new_instance['apps'] = array_key_exists('apps', $new_instance) ? '1' : '0'; /*$new_instance['links'] = array_key_exists('links', $new_instance) ? '1' : '0';*/ $registration_flag = true; //This determines if we need to call registration api or not. $dataurl = ''; $url = ''; $new_instance['debug_url'] = ''; //Process only if username has been entered if ( empty( $username ) ) { $new_instance['error'] = self::ERROR_EMPTY_USER; } elseif( false !== strpos( $username, ' ') ) { $new_instance['error'] = self::ERROR_NO_USER; } else { //If no client_id has been alloted, call for aboutme registration //If username has been changed, call for aboutme registration //If src_url or wordpress site url got changed, call for aboutme registration if ( empty( $new_instance['client_id'] ) ) { $registration_flag = false; } elseif ( $username != $old_instance['username'] ) { delete_transient( 'am_' . $old_instance['username'] . '_data' ); $registration_flag = false; } elseif ( ! array_key_exists( 'src_url', $old_instance ) || $src_url != $old_instance['src_url']) { $registration_flag = false; } if ( ! $registration_flag ) { $new_instance['client_id'] = ''; $url = 'https://api.about.me/api/v2/json/user/register/' . $username . '?apikey=' . self::API_KEY . '&src_url=' . $src_url . '&src=wordpress&verify=true'; $data = $this->get_api_content( $url ); if ( false === $data ) { $new_instance['error'] = self::API_SERVER_ERROR; $new_instance['debug_url'] = $url; } else { if ( ! empty( $data ) ) { if ( 200 == $data->status ) { //store this apikey as persistence object $new_instance['client_id'] = $data->apikey; $new_instance['error'] = 0; } elseif ( 401 == $data->status ) { $new_instance['error'] = self::API_REGISTRATION_ERROR; $new_instance['debug_url'] = $url.'&status=401'; } elseif ( 404 == $data->status ) { $new_instance['error'] = self::ERROR_NO_USER; } else { $new_instance['error'] = self::ERROR_UNKNOWN; $new_instance['debug_url'] = $url.'&status='.$data->status; } } else { $new_instance['error'] = self::API_EMPTY_RESPONSE; $new_instance['debug_url'] = $url.'&status=empty'; } } } // If client_id is available call profile api to get profile data if ( ! empty( $new_instance['client_id'] ) ){ $dataurl = "https://api.about.me/api/v2/json/user/view/$username?client_id={$new_instance['client_id']}&extended=true&on_match=true&strip_html=false"; $userdata = $this->get_api_content( $dataurl ); if ( false === $userdata ) { $new_instance['error'] = self::API_SERVER_ERROR; $new_instance['debug_url'] = $dataurl; } else { if ( ! empty( $userdata ) ){ if ( 200 == $userdata->status ) { // Reset any previous error that might have been set $new_instance['error'] = 0; $data = $this->extract_api_data( $userdata ); set_transient( 'am_' . $username . '_data', $data, self::CACHE_TIME ); } elseif ( 401 == $userdata->status ) { $new_instance['error'] = self::API_PROFILE_ERROR; $new_instance['client_id'] = ''; $new_instance['debug_url'] = $dataurl.'&status=401'; } elseif ( 404 == $userdata->status ) { $new_instance['error'] = self::ERROR_NO_USER; $new_instance['client_id'] = ''; } else { $new_instance['error'] = self::ERROR_UNKNOWN; $new_instance['client_id'] = ''; $new_instance['debug_url'] = $dataurl.'&status='.$userdata->status; } } else { $new_instance['error'] = self::API_EMPTY_RESPONSE; $new_instance['client_id'] = ''; $new_instance['debug_url'] = $dataurl.'&status=empty'; } } } } return $new_instance; } /** * To read the response from aboutme api call * * @params string $url api url * * @retun mixed json class or false */ private function get_api_content( $url ) { $response = wp_remote_get( $url, array( 'sslverify'=>0, 'User-Agent' => 'WordPress.com About.me Widget' ) ); if ( is_wp_error( $response ) ) { return false; } else { return json_decode( $response['body'] ); } } /** * Only extract required keys from json data of api profile call * @param class $data json content of profile * * @return array */ private function extract_api_data( $data ) { $retarr = array(); if ( ! empty( $data ) && 200 == $data->status ) { $app_icons = array(); $link_icons = array(); $i = 0; $j = 0; foreach ( $data->websites as $c ) { if ( 'default' == $c->platform || 'syndication_feed' == $c->platform) { continue; //we want to show only service icons } elseif ( 'link' == $c->platform ){ $icon_url = $c->icon_url; if ( $c->site_url ) { $url = $c->site_url; } else if( $c->modal_url ) { $url = $c->modal_url; } else if( $c->service_url ) { $url = $c->service_url; } $link_icons[$i++] = array( 'icon'=>$icon_url, 'url'=>$url, 'text'=>$c->display_name ); } elseif ( ! empty( $c->icon42_url ) ) { $icon_url = $c->icon42_url; $icon_url = str_replace( '42x42', '32x32', $icon_url ); if ( $c->site_url ) { $url = $c->site_url; } else if( $c->modal_url ) { $url = $c->modal_url; } else { $url = 'http://about.me/' . $data->user_name . '/#!/service/' . $c->platform; } $app_icons[$j++] = array( 'icon'=>$icon_url, 'url'=>$url ); } } $retarr['app_icons'] = $app_icons; $retarr['link_icons'] = $link_icons; $retarr['profile_url'] = $data->profile; $retarr['thumbnail'] = $data->background; $retarr['avatar'] = $data->avatar; $retarr['first_name'] = $data->first_name; $retarr['last_name'] = $data->last_name; $retarr['header'] = $data->header; $retarr['bio'] = $data->bio; } return $retarr; } /** * Back-end widget form. * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $instance = wp_parse_args( ( array ) $instance, array( 'title' => 'about.me', 'fontsize' =>'large', 'photo' => 'background', 'client_id' => '', 'error' => 0, 'debug_url' => '', 'src_url' => str_ireplace( array('https://','http://'), '' , get_site_url() ), 'username' => '', 'headline' => '1', 'biography' => 1, 'apps' => 1, 'links' => 1) ); $title = $instance['title']; $fontsize = $instance['fontsize']; $photo = $instance['photo']; $username = array_key_exists( 'username', $instance )? $instance['username'] : ''; $headline = array_key_exists( 'headline', $instance )? $instance['headline'] : '1'; $biography = array_key_exists( 'biography', $instance )? $instance['biography'] : '1'; $apps = array_key_exists( 'apps', $instance )? $instance['apps'] : '1'; $links = array_key_exists( 'links', $instance )? $instance['links'] : '1'; if ( empty($username) ) { ?>

. ' . $instance['debug_url'] .''; } } else{ $message .= __( ' Please contact help@about.me for support', 'aboutme-widget' ); }?> ' . $instance['debug_url'] .''; } } else{ $message .= __( ' Please email help@about.me for support', 'aboutme-widget' ); }?>

0, 'User-Agent' => 'WordPress.com About.me Widget' ) ); $data = array(); if ( ! is_wp_error( $response ) ) { $data = json_decode( $response['body'] ); } $data = get_am_api_data( $data ); return get_am_generate_content($data); } add_shortcode( 'aboutme', 'am_shortcode' ); /** * Only extract required keys from json data of api profile call * @param class $data json content of profile * * @return array */ function get_am_api_data( $data ) { $retarr = array(); if ( ! empty( $data ) && 200 == $data->status ) { $icons = array(); $app_icons = array(); $link_icons = array(); $i = 0; $j = 0; foreach ( $data->websites as $c ) { if ( 'default' == $c->platform || 'syndication_feed' == $c->platform ) { continue; //we want to show only service icons } elseif ( 'link' == $c->platform ){ $icon_url = $c->icon_url; if ( $c->site_url ) { $url = $c->site_url; } else if( $c->modal_url ) { $url = $c->modal_url; } else if( $c->service_url ) { $url = $c->service_url; } $link_icons[$i++] = array( 'icon'=>$icon_url, 'url'=>$url, 'text'=>$c->display_name ); } elseif ( ! empty( $c->icon42_url ) ) { $icon_url = $c->icon42_url; $icon_url = str_replace( '42x42', '32x32', $icon_url ); if ( $c->site_url ) { $url = $c->site_url; } else if( $c->modal_url ) { $url = $c->modal_url; } else { $url = 'http://about.me/' . $data->user_name . '/#!/service/' . $c->platform; } $app_icons[$j++] = array( 'icon'=>$icon_url, 'url'=>$url ); } } $retarr['app_icons'] = $app_icons; $retarr['link_icons'] = $link_icons; $retarr['profile_url'] = $data->profile; $retarr['thumbnail'] = $data->background; $retarr['first_name'] = $data->first_name; $retarr['last_name'] = $data->last_name; $retarr['header'] = $data->header; $retarr['bio'] = $data->bio; } return $retarr; } /** * Generate shortcode parsed content. * * @param array $data aboutme profile data. * @return string. */ function get_am_generate_content( $data ){ $biostr =''; if ( ! empty( $data['bio'] ) ) { $biostr = '

' . str_replace( '\n', '

', wp_kses_data( $data['bio'] ) ) . '

'; } $appstr = ''; if ( count( $data['app_icons'] ) > 0 ) { $appstr = '
'; foreach ( $data['app_icons'] as $v ) { $appstr .= ''; } $appstr .= '
'; } /* $linkstr = ''; if ( count( $data['link_icons'] ) > 0 ) { $linkstr = ''; } */ $content = '
'. esc_attr( $data['first_name'] ) . ' ' . esc_attr( $data['last_name'] ) . '

' . esc_attr( $data['first_name'] ) . ' ' . esc_attr( $data['last_name'] ) . '

' . esc_attr( $data['header'] ) . '

' . $biostr .'
' . $appstr; return $content; } ?>