| 1 |
<?php |
| 2 |
/** |
| 3 |
* Global functions for Gianism. |
| 4 |
* |
| 5 |
* @package Gianism |
| 6 |
* @since 1.0 |
| 7 |
* @author Takahashi Fumiki |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Returns Facebook ID |
| 13 |
* |
| 14 |
* @since 3.0.0 |
| 15 |
* |
| 16 |
* @param int $user_id |
| 17 |
* |
| 18 |
* @return mixed |
| 19 |
*/ |
| 20 |
function gianism_get_facebook_id( $user_id ) { |
| 21 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 22 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 23 |
|
| 24 |
return get_user_meta( $user_id, $facebook->umeta_id, true ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Returns url to get Publish stream permission |
| 30 |
* |
| 31 |
* This function itself has no effect without action hook. |
| 32 |
* See example below: |
| 33 |
* |
| 34 |
* <pre> |
| 35 |
* // In some template, display button. |
| 36 |
* get_facebook_publish_permission_link(get_permalink(), 'my_favorite_action', array('post_id' => get_the_ID())); |
| 37 |
* |
| 38 |
* // Then, hook action in functions.php in your theme. |
| 39 |
* add_action('my_favorite_action', 'my_publish_action'); |
| 40 |
* |
| 41 |
* function my_publish_action($facebook, $args){ |
| 42 |
* $post = get_post($args['post_id']); |
| 43 |
* try{ |
| 44 |
* $facebook->api("/me/feed", "POST", array( |
| 45 |
* "message" => "I read this article!", |
| 46 |
* "link" => get_permalink($post->ID), |
| 47 |
* "name" => get_the_title($post->ID), |
| 48 |
* "description" => strip_tags($post->post_content), |
| 49 |
* "action" => json_encode(array( |
| 50 |
* "name" => get_bloginfo('name'), |
| 51 |
* "link" => home_url('/'))) |
| 52 |
* )); |
| 53 |
* }catch(FacebookApiException $e){ |
| 54 |
* // Error |
| 55 |
* wp_die($e->getMessage()); |
| 56 |
* } |
| 57 |
* } |
| 58 |
* </pre> |
| 59 |
* |
| 60 |
* @since 3.0.0 |
| 61 |
* |
| 62 |
* @param string $redirect_url URL where user will be redirect after authentication |
| 63 |
* @param string $action Action name which will be fired after authentication |
| 64 |
* @param array $args Array which will be passed to action hook |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
function gianism_get_facebook_publish_permission_link( $redirect_url = null, $action = '', $args = [] ) { |
| 69 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 70 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 71 |
|
| 72 |
return $facebook->get_publish_permission_link( $redirect_url, $action, $args ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get facebook API client for admin |
| 77 |
* |
| 78 |
* <pre> |
| 79 |
* $fb = gianism_fb_admin(); |
| 80 |
* if( !is_wp_error($fb) ){ |
| 81 |
* // Get feed. |
| 82 |
* $feeds = $fb->api('/me/feed'); |
| 83 |
* // Post feed. |
| 84 |
* $fb->api('/me/feed', 'POST', array( |
| 85 |
* 'message' => 'Hola!', |
| 86 |
* )); |
| 87 |
* } |
| 88 |
* </pre> |
| 89 |
* |
| 90 |
* @return \Facebook\Facebook|WP_Error |
| 91 |
*/ |
| 92 |
function gianism_fb_admin() { |
| 93 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 94 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 95 |
|
| 96 |
return $facebook->admin; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get admin facebook id |
| 101 |
* |
| 102 |
* This will be user id or facebook page id. |
| 103 |
* |
| 104 |
* <pre> |
| 105 |
* // Example |
| 106 |
* $fb = gianism_fb_admin(); |
| 107 |
* if( !is_wp_error($fb) ){ |
| 108 |
* $feed = $fb->api(gianism_fb_admin_id().'/feed'); |
| 109 |
* foreach( $feed['data'] as $status ){ |
| 110 |
* // Do stuff. |
| 111 |
* } |
| 112 |
* } |
| 113 |
* </pre> |
| 114 |
* |
| 115 |
* @return int|string |
| 116 |
*/ |
| 117 |
function gianism_fb_admin_id() { |
| 118 |
/** @var \Gianism\Service\Facebook $facebook */ |
| 119 |
$facebook = \Gianism\Service\Facebook::get_instance(); |
| 120 |
|
| 121 |
return $facebook->admin_id; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns if user is connected with particular web service. |
| 126 |
* |
| 127 |
* @since 3.0.0 |
| 128 |
* |
| 129 |
* @param string $service One of facebook, mixi, yahoo, twitter or google. |
| 130 |
* @param int $user_id If not specified, current user id will be used. |
| 131 |
* |
| 132 |
* @return boolean |
| 133 |
*/ |
| 134 |
function gianism_is_user_connected_with( $service, $user_id = 0 ) { |
| 135 |
/** @var \Gianism\Bootstrap $gianism */ |
| 136 |
$gianism = \Gianism\Bootstrap::get_instance(); |
| 137 |
if ( ! $user_id ) { |
| 138 |
$user_id = get_current_user_id(); |
| 139 |
} |
| 140 |
|
| 141 |
return ( $instance = $gianism->get_service_instance( $service ) ) && $instance->is_connected( $user_id ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Get user object by credential |
| 147 |
* |
| 148 |
* Only facebook is supported. |
| 149 |
* |
| 150 |
* @todo Make other services to work. |
| 151 |
* @since 3.0.0 |
| 152 |
* @global \wpdb $wpdb |
| 153 |
* @param string $service |
| 154 |
* @param mixed $credential |
| 155 |
* |
| 156 |
* @return \WP_User |
| 157 |
*/ |
| 158 |
function gianism_get_user_by_service( $service, $credential ) { |
| 159 |
global $wpdb; |
| 160 |
$gianism = \Gianism\Bootstrap::get_instance(); |
| 161 |
$instance = $gianism->get_instance( $service ); |
| 162 |
if ( ! $instance ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
switch ( $service ) { |
| 166 |
case 'facebook': |
| 167 |
$user_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s", $credential ) ); |
| 168 |
if ( $user_id ) { |
| 169 |
return new WP_User( $user_id ); |
| 170 |
} else { |
| 171 |
return null; |
| 172 |
} |
| 173 |
break; |
| 174 |
default: |
| 175 |
return null; |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Get Twitter Screen Name |
| 182 |
* |
| 183 |
* @since 3.0.0 |
| 184 |
* |
| 185 |
* @param int $user_id |
| 186 |
* |
| 187 |
* @return string|false |
| 188 |
*/ |
| 189 |
function gianism_get_twitter_screen_name( $user_id ) { |
| 190 |
/** @var \Gianism\Service\Twitter $twitter */ |
| 191 |
$twitter = \Gianism\Service\Twitter::get_instance(); |
| 192 |
|
| 193 |
return get_user_meta( $user_id, $twitter->umeta_screen_name, true ); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Update Twitter time line |
| 199 |
* |
| 200 |
* @since 3.0.0 |
| 201 |
* |
| 202 |
* @param string $string |
| 203 |
*/ |
| 204 |
function gianism_update_twitter_status( $string ) { |
| 205 |
/** @var \Gianism\Service\Twitter $twitter */ |
| 206 |
$twitter = \Gianism\Service\Twitter::get_instance(); |
| 207 |
$twitter->tweet( $string ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Reply to specified user by Owner Account |
| 212 |
* |
| 213 |
* @since 3.0.0 |
| 214 |
* |
| 215 |
* @param int $user_id |
| 216 |
* @param string $string |
| 217 |
* |
| 218 |
* @return boolean |
| 219 |
*/ |
| 220 |
function gianism_twitter_reply_to( $user_id, $string ) { |
| 221 |
$screen_name = gianism_get_twitter_screen_name( $user_id ); |
| 222 |
if ( $screen_name ) { |
| 223 |
gianism_update_twitter_status( "@{$screen_name} " . $string ); |
| 224 |
return true; |
| 225 |
} else { |
| 226 |
return false; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get twitter time line in JSON format object |
| 232 |
* |
| 233 |
* Caching is recommended. |
| 234 |
* |
| 235 |
* <pre> |
| 236 |
* $timeline = get_transient('twitter_timeline'); |
| 237 |
* if( false === $timeline){ |
| 238 |
* $timeline = twitter_get_timeline('my_screen_name'); |
| 239 |
* set_transient('twitter_timeline', $timeline, 3600); |
| 240 |
* } |
| 241 |
* foreach($timeline as $status){ |
| 242 |
* // Echo status |
| 243 |
* } |
| 244 |
* </pre> |
| 245 |
* |
| 246 |
* @since 3.0.0 |
| 247 |
* |
| 248 |
* @param string $screen_name If not specified, admin user's screen name will be used. |
| 249 |
* @param array $additional_data |
| 250 |
* |
| 251 |
* @return object JSON format object. |
| 252 |
*/ |
| 253 |
function gianism_twitter_get_timeline( $screen_name = null, array $additional_data = [] ) { |
| 254 |
/** @var \Gianism\Service\Twitter $twitter */ |
| 255 |
$twitter = \Gianism\Service\Twitter::get_instance(); |
| 256 |
if ( is_null( $screen_name ) ) { |
| 257 |
$screen_name = $twitter->tw_screen_name; |
| 258 |
} |
| 259 |
|
| 260 |
return $twitter->call_api( 'statuses/user_timeline', array_merge( |
| 261 |
array( 'screen_name' => $screen_name ), |
| 262 |
$additional_data |
| 263 |
) ); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
/** |
| 268 |
* Show Login buttons |
| 269 |
* |
| 270 |
* Show login buttons where you want. |
| 271 |
* |
| 272 |
* @since 1.0 |
| 273 |
* |
| 274 |
* @param string $before |
| 275 |
* @param string $after |
| 276 |
* @param string $redirect_to |
| 277 |
*/ |
| 278 |
function gianism_login( $before = '', $after = '', $redirect_to = '' ) { |
| 279 |
/** @var \Gianism\Controller\Login $login */ |
| 280 |
$login = \Gianism\Controller\Login::get_instance(); |
| 281 |
$login->login_form( $before, $after, false, $redirect_to ); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Add UTM campaign link to WordPress admin |
| 286 |
* |
| 287 |
* @param string $url |
| 288 |
* @param array $args |
| 289 |
* |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
function gianism_utm_link( $url, $args = [] ) { |
| 293 |
$args = wp_parse_args( $args, [ |
| 294 |
'utm_source' => 'wp-admin', |
| 295 |
'utm_medium' => 'link', |
| 296 |
'utm_campaign' => 'Plugin User', |
| 297 |
] ); |
| 298 |
return add_query_arg( $args, $url ); |
| 299 |
} |
| 300 |
|