| 1 |
<?php |
| 2 |
/** |
| 3 |
* Social Follow |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Extensions |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Init module |
| 11 |
*/ |
| 12 |
class Powerkit_Social_Follow extends Powerkit_Module { |
| 13 |
|
| 14 |
/** |
| 15 |
* Register module |
| 16 |
*/ |
| 17 |
public function register() { |
| 18 |
$this->name = 'Social Follow'; |
| 19 |
$this->slug = 'social_follow'; |
| 20 |
$this->type = 'extension'; |
| 21 |
$this->category = 'basic'; |
| 22 |
$this->public = false; |
| 23 |
$this->enabled = true; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Transient Prefix |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @var string $cache_timeout Cache Timeout (minutes). |
| 31 |
*/ |
| 32 |
private $trans_prefix = 'powerkit_social_follow_'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialize module |
| 36 |
*/ |
| 37 |
public function initialize() { |
| 38 |
// Reset cache. |
| 39 |
add_filter( 'powerkit_reset_cache', array( $this, 'register_reset_cache' ) ); |
| 40 |
add_filter( 'powerkit_ajax_reset_cache', array( $this, 'register_reset_cache' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register Reset Cache |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @param array $list Change list reset cache. |
| 48 |
* @access private |
| 49 |
*/ |
| 50 |
public function register_reset_cache( $list ) { |
| 51 |
$slug = powerkit_get_page_slug( $this->slug ); |
| 52 |
|
| 53 |
$list[ $slug ] = array( |
| 54 |
'powerkit_social_follow', |
| 55 |
); |
| 56 |
|
| 57 |
return $list; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Return social follow |
| 62 |
* |
| 63 |
* @param string $name Name of social network. |
| 64 |
* @param string $username Username of social network. |
| 65 |
* @param string $type Type of social network. |
| 66 |
*/ |
| 67 |
public function get_data( $name, $username, $type = null ) { |
| 68 |
$social_function = sprintf( 'get_%s', $name ); |
| 69 |
|
| 70 |
if ( ! $username ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if ( method_exists( $this, $social_function ) ) { |
| 75 |
return $this->$social_function( $username, $type ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get youtube data. |
| 81 |
* |
| 82 |
* @param string $username Username of social network. |
| 83 |
* @param string $type Type of social network. |
| 84 |
*/ |
| 85 |
public function get_youtube( $username, $type = null ) { |
| 86 |
|
| 87 |
$result = array(); |
| 88 |
|
| 89 |
// Get data for social network. |
| 90 |
$counter = new Powerkit_Links_Social_Counter(); |
| 91 |
|
| 92 |
$counter->trans_prefix = $this->trans_prefix; |
| 93 |
|
| 94 |
$counter->config['youtube_slug'] = $username; |
| 95 |
$counter->config['youtube_channel_type'] = $type; |
| 96 |
|
| 97 |
$data = $counter->get_count( 'youtube', true ); |
| 98 |
|
| 99 |
if ( isset( $data['error'] ) ) { |
| 100 |
powerkit_alert_warning( $data['error'] ); |
| 101 |
} |
| 102 |
|
| 103 |
// Followers. |
| 104 |
|
| 105 |
// Manual Count Override. |
| 106 |
$count_override = get_option( 'powerkit_social_links_youtube_override' ); |
| 107 |
|
| 108 |
if ( $count_override ) { |
| 109 |
$result['followers'] = $count_override; |
| 110 |
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) { |
| 111 |
$result['followers'] = $data['count']; |
| 112 |
} |
| 113 |
|
| 114 |
// Avatar. |
| 115 |
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->thumbnails->default->url ) ) { |
| 116 |
$result['avatar_1x'] = $data['data']->items[0]->snippet->thumbnails->default->url; |
| 117 |
} |
| 118 |
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->thumbnails->medium->url ) ) { |
| 119 |
$result['avatar_2x'] = $data['data']->items[0]->snippet->thumbnails->medium->url; |
| 120 |
} |
| 121 |
|
| 122 |
// Username. |
| 123 |
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->items[0]->snippet->title ) ) { |
| 124 |
$result['text'] = $data['data']->items[0]->snippet->title; |
| 125 |
} |
| 126 |
|
| 127 |
// Link. |
| 128 |
if ( 'channel' === $type ) { |
| 129 |
$result['link'] = sprintf( 'https://www.youtube.com/channel/%s/', $username ); |
| 130 |
} else { |
| 131 |
$result['link'] = sprintf( 'https://www.youtube.com/user/%s/', $username ); |
| 132 |
} |
| 133 |
|
| 134 |
return $result; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get facebook data. |
| 139 |
* |
| 140 |
* @param string $username Username of social network. |
| 141 |
* @param string $type Type of social network. |
| 142 |
*/ |
| 143 |
public function get_facebook( $username, $type = null ) { |
| 144 |
|
| 145 |
$result = array(); |
| 146 |
|
| 147 |
// Get data for social network. |
| 148 |
$counter = new Powerkit_Links_Social_Counter(); |
| 149 |
|
| 150 |
$counter->trans_prefix = $this->trans_prefix; |
| 151 |
|
| 152 |
$counter->config['facebook_user'] = $username; |
| 153 |
|
| 154 |
$data = $counter->get_count( 'facebook', true ); |
| 155 |
|
| 156 |
if ( isset( $data['error'] ) ) { |
| 157 |
powerkit_alert_warning( $data['error'] ); |
| 158 |
} |
| 159 |
|
| 160 |
// Followers. |
| 161 |
|
| 162 |
// Manual Count Override. |
| 163 |
$count_override = get_option( 'powerkit_social_links_facebook_override' ); |
| 164 |
|
| 165 |
if ( $count_override ) { |
| 166 |
$result['followers'] = $count_override; |
| 167 |
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) { |
| 168 |
$result['followers'] = $data['count']; |
| 169 |
} |
| 170 |
|
| 171 |
// Link. |
| 172 |
$result['link'] = sprintf( 'https://facebook.com/%s/', $username ); |
| 173 |
|
| 174 |
// Avatar. |
| 175 |
$result['avatar_1x'] = "https://graph.facebook.com/{$username}/picture?width=80&height=80"; |
| 176 |
$result['avatar_2x'] = "https://graph.facebook.com/{$username}/picture?width=160&height=160"; |
| 177 |
|
| 178 |
// Username. |
| 179 |
$result['text'] = $username; |
| 180 |
|
| 181 |
return $result; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get instagram data. |
| 186 |
* |
| 187 |
* @param string $username Username of social network. |
| 188 |
* @param string $type Type of social network. |
| 189 |
*/ |
| 190 |
public function get_instagram( $username, $type = null ) { |
| 191 |
|
| 192 |
$result = array(); |
| 193 |
// Get data for social network. |
| 194 |
$counter = new Powerkit_Links_Social_Counter(); |
| 195 |
|
| 196 |
$counter->trans_prefix = $this->trans_prefix . md5( ( $username ) . powerkit_connect( 'instagram_app_access_token' ) ); |
| 197 |
|
| 198 |
$counter->config['instagram_user'] = $username; |
| 199 |
|
| 200 |
$data = $counter->get_count( 'instagram', true ); |
| 201 |
|
| 202 |
if ( isset( $data['error'] ) ) { |
| 203 |
powerkit_alert_warning( $data['error'] ); |
| 204 |
} |
| 205 |
|
| 206 |
// Followers. |
| 207 |
|
| 208 |
// Manual Count Override. |
| 209 |
$count_override = get_option( 'powerkit_social_links_instagram_override' ); |
| 210 |
|
| 211 |
if ( $count_override ) { |
| 212 |
$result['followers'] = $count_override; |
| 213 |
} elseif ( isset( $data['count'] ) && ! isset( $data['error'] ) ) { |
| 214 |
$result['followers'] = $data['count']; |
| 215 |
} |
| 216 |
|
| 217 |
// Avatar parse json. |
| 218 |
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->profile_picture_url ) ) { |
| 219 |
$result['avatar_1x'] = $data['data']->profile_picture_url; |
| 220 |
} |
| 221 |
if ( isset( $data['data'] ) && is_object( $data['data'] ) && isset( $data['data']->profile_picture_url ) ) { |
| 222 |
$result['avatar_2x'] = $data['data']->profile_picture_url; |
| 223 |
} |
| 224 |
|
| 225 |
// Avatar parse html. |
| 226 |
if ( isset( $data['data'] ) && is_array( $data['data'] ) && isset( $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url'] ) ) { |
| 227 |
$result['avatar_1x'] = $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url']; |
| 228 |
} |
| 229 |
if ( isset( $data['data'] ) && is_array( $data['data'] ) && isset( $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url_hd'] ) ) { |
| 230 |
$result['avatar_2x'] = $data['data']['entry_data']['ProfilePage'][0]['graphql']['user']['profile_pic_url_hd']; |
| 231 |
} |
| 232 |
|
| 233 |
/* Manual Override */ |
| 234 |
|
| 235 |
if ( get_option( 'powerkit_connect_instagram_custom_followers' ) ) { |
| 236 |
$result['followers'] = (int) get_option( 'powerkit_connect_instagram_custom_followers' ); |
| 237 |
} |
| 238 |
|
| 239 |
if ( get_option( 'powerkit_connect_instagram_custom_avatar' ) ) { |
| 240 |
$result['avatar_1x'] = get_option( 'powerkit_connect_instagram_custom_avatar' ); |
| 241 |
} |
| 242 |
|
| 243 |
if ( get_option( 'powerkit_connect_instagram_custom_avatar' ) ) { |
| 244 |
$result['avatar_2x'] = get_option( 'powerkit_connect_instagram_custom_avatar' ); |
| 245 |
} |
| 246 |
|
| 247 |
// Link. |
| 248 |
$result['link'] = sprintf( 'https://www.instagram.com/%s/', $username ); |
| 249 |
|
| 250 |
// Username. |
| 251 |
$result['text'] = $username; |
| 252 |
|
| 253 |
return $result; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Get social follow |
| 259 |
* |
| 260 |
* @param string $name Name of social network. |
| 261 |
* @param string $username Username of social network. |
| 262 |
* @param string $type Type of social network. |
| 263 |
*/ |
| 264 |
function powerkit_get_social_follow( $name, $username, $type = null ) { |
| 265 |
$object = new Powerkit_Social_Follow(); |
| 266 |
|
| 267 |
return $object->get_data( $name, $username, $type ); |
| 268 |
} |
| 269 |
|
| 270 |
new Powerkit_Social_Follow(); |
| 271 |
|