true,
'twitter_timeline' => true,
'facebook_recent_post' => true
);
foreach ( $ajax_events as $ajax_event => $nopriv ) {
add_action( 'wp_ajax_mpl_' . $ajax_event, array( &$this, esc_attr( $ajax_event ) ) );
if ( $nopriv ) {
add_action( 'wp_ajax_nopriv_mpl_' . $ajax_event, array( &$this, esc_attr( $ajax_event ) ) );
}
}
}
function facebook_recent_post(){
global $mpl;
$output = $header_html = $fb_cover = '';
if( !isset( $_POST['cfg'] ) || empty( $_POST['cfg'] ) ){
wp_send_json( array(
'html' => '',
'header_data' => '',
'message' => __('Fail', 'mageewp-page-layout')
));
exit;
}
$atts = (array) json_decode( base64_decode( $_POST['cfg'] ) );
$fb_app_id = $fb_page_id = $fb_app_secret = $number_post_show = '';
extract( $atts );
$fb_input_data = array(
'fb_page_id' => $fb_page_id,
'fb_app_id' => $mpl->secrect_storage( $fb_app_id ),
'fb_app_secret' => $mpl->secrect_storage( $fb_app_secret ),
'number_post_show' => $number_post_show
);
$fb_page_info = $this->get_facebook_page_feed( $fb_input_data, true );
if(isset($fb_page_info->id)){
if(isset($fb_page_info->cover->source)){
$fb_cover = 'background-image: url('. $fb_page_info->cover->source. ');';
}
$header_html .= '
';
}else{
$header_html .= __('Can not get page id', 'mageewp-page-layout');
}
$fb_page_posts = $this->get_facebook_page_feed( $fb_input_data );
//print_r($fb_page_posts);
if ( isset( $open_link_new_window ) && $open_link_new_window == 'yes' ) {
$link_target = ' target="_blank"';
} else {
$link_target = '';
}
if($fb_page_posts){
foreach($fb_page_posts as $data => $value){
$fb_post_id = explode('_', $value->id);
$post_id = $fb_post_id[1];
$output .= '';
if( isset( $show_image ) && $show_image == 'yes' ){
$a_end = '';
if( isset( $value->link ) ){
$output .= '';
$a_end = '';
}
if ( isset( $value->name ) ){
$alt = $value->name;
}
if( isset( $value->full_picture ) ){
$output .= '';
}
$output .= $a_end;
}
if( isset( $value->message ) ){
// remove emoji's
$_message = preg_replace( '/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]/u', '', $value->message );
$_message = sanitize_text_field( $_message ); // sanitize content
if(!empty($number_of_des) && $number_of_des > 0){
$_message = wp_trim_words( $_message, $number_of_des, $more = null );
}
$output .= ''. $_message .'
';
}
if( !empty( $show_time ) || !empty( $show_like_count ) || !empty( $show_comment_count ) ){
$output .= '';
}
$output .= '';
}
}else{
$output .= __('Warning: Fill correct facebook infomation to show new feed.', 'mageewp-page-layout');
}
$data = array(
'html' => $output,
'header_html' => $header_html,
'message' => __('ok', 'mageewp-page-layout')
);
wp_send_json( $data );
}
function get_facebook_page_feed( $args = array(), $only_info = false ) {
global $mpl;
$defaults = array(
'fb_page_id' => 'wordpress',
'number_post_show' => 10
);
$args = wp_parse_args( $args, $defaults );
$fb_page_id = $args['fb_page_id'];
$fb_app_id = !empty( $args['fb_app_id'] ) ? $args['fb_app_id'] : '301439076655535';
$fb_app_secret = !empty( $args['fb_app_secret'] ) ? $args['fb_app_secret'] : 'afddd33dc09cb364c79e8f0a46e7dff6';
$number_post_show = $args['number_post_show'];
if ( isset( $fb_page_id ) && isset( $fb_app_id ) && isset( $fb_app_secret ) && isset( $number_post_show ) ) {
$facebook_page_info_url = 'https://graph.facebook.com/'. $fb_page_id .'/?access_token='. $fb_app_id .'|'. $fb_app_secret;
$facebook_api_url = 'https://graph.facebook.com/' . $fb_page_id . '/posts?&access_token=' . $fb_app_id . '|' . $fb_app_secret . '&fields=id,picture,full_picture,actions,type,from,message,status_type,object_id,name,caption,description,link,created_time,comments.limit(1).summary(true),likes.limit(1).summary(true)&limit=' . $number_post_show;
if($only_info == true){
$result_data = json_decode( $this->file_get_contents( $facebook_page_info_url ) );
}else{
$facebook_data = json_decode( $this->file_get_contents( $facebook_api_url ) );
if( isset( $facebook_data ) && isset( $facebook_data->data ) )
$result_data = $facebook_data->data;
}
if ( isset( $result_data ) ) {
return $result_data;
} else {
return '';
}
} else {
return '';
}
}
function instagrams_feed(){
global $mpl;
$html = '';
$count = 1;
if( !isset( $_POST['cfg'] ) || empty( $_POST['cfg'] ) ){
wp_send_json( array(
'html' => '',
'header_data' => '',
'message' => __('Fail', 'mageewp-page-layout')
));
exit;
}
$atts = (array) json_decode( base64_decode( $_POST['cfg'] ) );
$access_token = $username = '';
extract( $atts );
$access_token = $mpl->secrect_storage( $access_token );
$ins_feed_data = $this->get_instagrams_feed( $username, $access_token );
if($ins_feed_data)
{
foreach ( $ins_feed_data as $key => $value )
{
switch ($count%$columns_style) {
case '1':
$li_class = 'el-start';
break;
case '0':
$li_class = 'el-end';
break;
default:
$li_class = '';
break;
}
$html .= ''
.''
.''
.'';
if($count >= $number_show){
break;
}
$count++;
}
}else{
$html .= __('Warning: Fill correct instagram infomation to show new feed.', 'mageewp-page-layout');
}
$data = array(
'html' => $html,
'message' => __('ok', 'mageewp-page-layout')
);
wp_send_json( $data );
}
/*
* twitter_timeline
* Add this function to wp_ajax_mpl_twitter_timeline hook
* Get new tweets update from user
*/
function twitter_timeline() {
global $mpl;
if( !isset( $_POST['cfg'] ) || empty( $_POST['cfg'] ) ){
wp_send_json( array(
'html' => '',
'header_data' => '',
'message' => __('Fail', 'mageewp-page-layout')
));
exit;
}
$atts = (array) json_decode( base64_decode( $_POST['cfg'] ) );
extract( $atts );
$output = $css_class = $rand_class = '';
$display_style = (!empty($display_style)) ? $display_style : 1;
$twitter_feed_data = array();
$consumer_key = !empty($consumer_key) ? $mpl->secrect_storage( $consumer_key ) : 'tHWsp0yQQioooQZJfXJdGP3d4';
$consumer_secret = !empty($consumer_secret) ? $mpl->secrect_storage( $consumer_secret ) : 'bl1kN9xH6nf167d0SJXnv9V5ZXuGXSShr5CeimLXaIGcUEQnsp';
$access_token = !empty($access_token) ? $mpl->secrect_storage( $access_token ) : '120290116-vmLx4sPp5O3hjhRxjpl28i0APJkCpg04YVsoZyb7';
$access_token_secrect = !empty($access_token_secrect) ? $mpl->secrect_storage( $access_token_secrect ) : 'B9mAhgZhQG0cspt1doF2cxDky40OEatjftRI5NCmQh1pE';
$number_tweet = (!empty( $number_tweet )) ? $number_tweet : 5;
switch ($display_style) {
case '1':
$el_in_start = '';
$el_item_start = '';
$el_item_end = '';
break;
case '2':
$el_in_start = '';
$el_item_start = '';
$el_item_end = '
';
break;
default:
# code...
break;
}
if (
isset( $username )
&& isset( $consumer_key )
&& isset( $consumer_secret )
&& isset( $access_token )
&& isset( $access_token_secrect )
&& isset( $number_tweet )
){
$twitter_feed_data = $this->get_tweets_feed_data(
$consumer_key,
$consumer_secret,
$access_token,
$access_token_secrect,
$number_tweet,
$username
);
$header_data = '';
if( isset( $show_follow_button ) && $show_follow_button == 'yes' && $display_style == 1 ){
$header_data .= '';
}
if(isset($twitter_feed_data)){
$output .= $el_in_start;
foreach($twitter_feed_data as $tweet){
if( empty( $tweet['text'] ) )
continue;
$latestTweet = $tweet['text'];
//Convert plain text URLs into HTML hyperlinks
$latestTweet = preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '$1', $latestTweet);
//Convert @username twiter to link clickable
$latestTweet = preg_replace('/(^|\s)@([a-z0-9_]+)/i', ' @$2 ', $latestTweet);
//Automatically create email link from a static text
$latestTweet = preg_replace('/(\S+@\S+\.\S+)/', ' $1 ', $latestTweet);
$twitterTime = strtotime( $tweet['created_at'] );
$timeAgo = $this->time_ago( $twitterTime );
$output .= $el_item_start;
if(!empty( $latestTweet )){
$avatar = '';
$has_avatar = '';
if ( isset( $show_avatar ) && $show_avatar == 'yes' ){
//$avatar = '';
if(isset($tweet['user']['profile_image_url'])){
$avatar = '';
$has_avatar = ' show_avatar';
}
}
if($display_style == 1){
$output .= '';
}else {
$output .= '';
}
}
$output .= '';
$output .= $el_item_end;
}
$output .= $el_in_end;
if( isset( $show_follow_button ) && $show_follow_button == 'yes' && $display_style == 2 ){
$output .= '';
}
}
}
$data = array(
'html' => $output,
'header_data' => $header_data,
'message' => __('Ok', 'mageewp-page-layout')
);
wp_send_json( $data );
}
/*
* get_tweets_feed_data
* @ $consumer_key
* @ $consumer_secret
* @ $access_token
* @ $access_token_secret
* @ $number_tweet
* @ $username
* Get new tweets update from user
*/
function get_tweets_feed_data( $consumer_key, $consumer_secret, $access_token, $access_token_secret, $number_tweet, $username ) {
$rd_twitter = mpl_random_string( 20 );
$trans_name = 'list_tweets_'.$rd_twitter;
$twitter_cache_time = 10;
if(false === ($twitter_twitter_data = get_transient($trans_name))) {
$twitter_token = get_option('cfTwitterToken_'.$rd_twitter);
// get a new token anyways
delete_option('cfTwitterToken_'.$rd_twitter);
// getting new auth bearer only if we don't have one
if(!$twitter_token) {
// preparing credentials
$twitter_credentials = $consumer_key . ':' . $consumer_secret;
$twitter_to_send = base64_encode( $twitter_credentials );
// http post arguments
$twitter_args = array(
'method' => 'POST',
'httpversion' => '1.1',
'blocking' => true,
'headers' => array(
'Authorization' => 'Basic ' . $twitter_to_send,
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
),
'body' => array( 'grant_type' => 'client_credentials' )
);
add_filter('https_ssl_verify', '__return_false');
$twitter_response = wp_remote_post('https://api.twitter.com/oauth2/token', $twitter_args);
$twitter_keys = json_decode(wp_remote_retrieve_body($twitter_response));
if($twitter_keys) {
// saving token to wp_options table
update_option('cfTwitterToken_'.$rd_twitter, $twitter_keys->access_token);
$twitter_token = $twitter_keys->access_token;
}
}
// we have bearer token wether we obtained it from API or from options
$twitter_args = array(
'httpversion' => '1.1',
'blocking' => true,
'headers' => array(
'Authorization' => "Bearer ".$twitter_token
)
);
add_filter('https_ssl_verify', '__return_false');
$twitter_api_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='.$username.'&count='.$number_tweet;
$twitter_response = wp_remote_get($twitter_api_url, $twitter_args);
set_transient($trans_name, wp_remote_retrieve_body($twitter_response), 60 * $twitter_cache_time);
}
@$twitter_feed_data = json_decode(get_transient($trans_name), true);
return $twitter_feed_data;
}
function time_ago($time) {
$periods = array(
__( 'second', 'mageewp-page-layout' ),
__( 'minute', 'mageewp-page-layout' ),
__( 'hour', 'mageewp-page-layout' ),
__( 'day', 'mageewp-page-layout' ),
__( 'week', 'mageewp-page-layout' ),
__( 'month', 'mageewp-page-layout' ),
__( 'year', 'mageewp-page-layout' ),
__( 'decade', 'mageewp-page-layout' )
);
$periods_plural = array(
__( 'seconds', 'mageewp-page-layout' ),
__( 'minutes', 'mageewp-page-layout' ),
__( 'hours', 'mageewp-page-layout' ),
__( 'days', 'mageewp-page-layout' ),
__( 'weeks', 'mageewp-page-layout' ),
__( 'months', 'mageewp-page-layout' ),
__( 'years', 'mageewp-page-layout' ),
__( 'decades', 'mageewp-page-layout' )
);
$lengths = array( '60', '60', '24', '7', '4.35', '12', '10' );
$now = time();
$difference = $now - $time;
$tense = __( 'ago', 'mageewp-page-layout' );
for( $j = 0; $difference >= $lengths[$j] && $j < count( $lengths )-1; $j++ ) {
$difference /= $lengths[$j];
}
$difference = round( $difference );
if( $difference != 1 ) {
$periods[$j] = $periods_plural[$j];
}
return sprintf('%s %s %s', $difference, $periods[$j], $tense);
}
function get_instagrams_feed( $username, $access_token ) {
$ins_user_id = $this->get_instagram_user_id( $username, $access_token );
if ($ins_user_id) {
$ins_api_url = 'https://api.instagram.com/v1/users/' . $ins_user_id . '/media/recent?access_token=' . $access_token;
$ins_ch = curl_init();
curl_setopt($ins_ch, CURLOPT_URL, $ins_api_url);
curl_setopt($ins_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ins_ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ins_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ins_ch, CURLOPT_SSL_VERIFYHOST, false);
$ins_data = curl_exec($ins_ch);
curl_close($ins_ch);
$ins_data = json_decode($ins_data);
return $ins_data->data;
} else {
return 0;
}
}
function get_instagram_user_id( $username, $access_token ) {
$ins_url = 'https://api.instagram.com/v1/users/self/?access_token=' . $access_token;
$ins_ch = curl_init();
curl_setopt($ins_ch, CURLOPT_URL, $ins_url);
curl_setopt($ins_ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ins_ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ins_ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ins_ch, CURLOPT_SSL_VERIFYHOST, false);
$ins_json_data = curl_exec($ins_ch);
curl_close($ins_ch);
$ins_json_data = json_decode($ins_json_data);
if (isset($ins_json_data->data->username) && ($ins_json_data->data->username == $username)) {
return $ins_json_data->data->id;
} else {
return 0;
}
}
function file_get_contents( $url ){
$url_ch = curl_init();
curl_setopt( $url_ch, CURLOPT_URL, $url );
curl_setopt( $url_ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $url_ch, CURLOPT_TIMEOUT, 20 );
curl_setopt( $url_ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $url_ch, CURLOPT_SSL_VERIFYHOST, false );
$_return = curl_exec( $url_ch );
curl_close( $url_ch );
return $_return;
}
}
#Start mpl_ajax_front
new mpl_ajax_front();