PluginProbe
Gianism / 2.2.5
Gianism v2.2.5
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / functions.php

functions.php in Gianism 2.2.5, at functions.php

452 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Global functions for Gianism.
4 *
5 * @package Gianism
6 * @since 1.0
7 * @author Takahashi Fumiki
8 */
9
10 /**
11 * Class auto loader
12 *
13 * Load base class and vendor libraries.
14 *
15 * @ignore
16 * @param string $class_name
17 */
18 function _gianism_autoloader($class_name){
19 $class_name = ltrim($class_name, '\\');
20 $vendor_dir = __DIR__.DIRECTORY_SEPARATOR.'vendor';
21 $path = false;
22 switch( $class_name ){
23 case 'Facebook':
24 $path = implode(DIRECTORY_SEPARATOR, array($vendor_dir, 'facebook-php-sdk', 'src', 'facebook.php' ));
25 break;
26 case 'TwitterOAuth':
27 $path = implode(DIRECTORY_SEPARATOR, array($vendor_dir, 'twitteroauth', 'twitteroauth', 'twitteroauth.php'));
28 break;
29 case 'OAuthException':
30 case 'OAuthConsumer':
31 case 'OAuthToken':
32 case 'OAuthSignatureMethod':
33 case 'OAuthSignatureMethod_HMAC_SHA1':
34 case 'OAuthSignatureMethod_PLAINTEXT':
35 case 'OAuthSignatureMethod_RSA_SHA1':
36 case 'OAuthRequest':
37 case 'OAuthServer':
38 case 'OAuthDataStore':
39 case 'OAuthUtil':
40 $path = implode(DIRECTORY_SEPARATOR, array($vendor_dir, 'twitteroauth', 'twitteroauth', 'OAuth.php'));
41 break;
42 case 'JWT':
43 $path = implode(DIRECTORY_SEPARATOR, array($vendor_dir, 'jwt', 'JWT.php'));
44 break;
45 default:
46 if( 0 === strpos( $class_name, 'Gianism\\') ){
47 // Original Class
48 $base_dir = __DIR__.DIRECTORY_SEPARATOR.'app';
49 $path_segments = explode('\\', $class_name);
50 array_shift($path_segments);
51 $path = $base_dir.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, array_map(function($path){
52 return strtolower(preg_replace_callback('/(?<!^)([A-Z]+)/u', function($matches){
53 return strtolower('-'.$matches[1]);
54 }, $path));
55 }, $path_segments)).'.php';
56 }elseif( 0 === strpos( $class_name, 'Google_') ){
57 // Google
58 $path_segments = explode('_', $class_name);
59 $path = implode(DIRECTORY_SEPARATOR, array_merge(
60 array($vendor_dir, 'google-api-php-client', 'src'),
61 $path_segments
62 )).'.php';
63 }elseif( 0 === strpos($class_name, 'YConnect\\')){
64 // YConnect
65 $path = str_replace('YConnect\\', implode(DIRECTORY_SEPARATOR, array($vendor_dir, 'yconnect-php-sdk', 'lib', '')), $class_name).'.php';
66 }
67 break;
68 }
69 if( $path && file_exists($path)){
70 require_once $path;
71 }
72 }
73
74 /**
75 * Save message to specified user
76 *
77 * If user's email is pseudo one(e.g. example@pseudo.twitter.com),
78 * WordPress's mail function <code>wp_mail</code> fails.
79 * This function is originally used for <code>wp_mail</code> fallback.
80 * But you can use this function to send message which will be
81 * displayed on admin screen.
82 *
83 * @param int $user_id
84 * @param string $body
85 * @param int $from
86 * @param string $subject
87 * @return bool
88 */
89 function gianism_message($user_id, $body, $from = 0, $subject = '' ){
90 /** @var \Gianism\Message $gianism */
91 $gianism = \Gianism\Message::get_instance();
92 return $gianism->send_message($user_id, $body, $from, $subject);
93 }
94
95 /**
96 * Returns Facebook ID
97 *
98 * @since 1.0
99 * @param int $user_id
100 * @return string
101 */
102 function get_facebook_id($user_id){
103 /** @var \Gianism\Service\Facebook $facebook */
104 $facebook = \Gianism\Service\Facebook::get_instance();
105 return get_user_meta($user_id, $facebook->umeta_id, true);
106 }
107
108
109
110 /**
111 * Returns url to get Publish stream permission
112 *
113 * This function itself has no effect without action hook.
114 * See example below:
115 *
116 * <pre>
117 * // In some template, display button.
118 * get_facebook_publish_permission_link(get_permalink(), 'my_favorite_action', array('post_id' => get_the_ID()));
119 *
120 * // Then, hook action in functions.php in your theme.
121 * add_action('my_favorite_action', 'my_publish_action');
122 *
123 * function my_publish_action($facebook, $args){
124 * $post = get_post($args['post_id']);
125 * try{
126 * $facebook->api("/me/feed", "POST", array(
127 * "message" => "I read this article!",
128 * "link" => get_permalink($post->ID),
129 * "name" => get_the_title($post->ID),
130 * "description" => strip_tags($post->post_content),
131 * "action" => json_encode(array(
132 * "name" => get_bloginfo('name'),
133 * "link" => home_url('/')))
134 * ));
135 * }catch(FacebookApiException $e){
136 * // Error
137 * wp_die($e->getMessage());
138 * }
139 * }
140 * </pre>
141 *
142 * @since 1.3
143 * @param string $redirect_url URL where user will be redirect afeter authentication
144 * @param string $action Action name which will be fired after authenticaction
145 * @param array $args Array which will be passed to action hook
146 * @return string
147 */
148 function get_facebook_publish_permission_link($redirect_url = null, $action = '', $args = array()){
149 /** @var \Gianism\Service\Facebook $facebook */
150 $facebook = \Gianism\Service\Facebook::get_instance();
151 return $facebook->get_publish_permission_link($redirect_url, $action, $args);
152 }
153
154 /**
155 * Get facebook API client for admin
156 *
157 * <pre>
158 * $fb = gianism_fb_admin();
159 * if( !is_wp_error($fb) ){
160 * // Get feed.
161 * $feeds = $fb->api('/me/feed');
162 * // Post feed.
163 * $fb->api('/me/feed', 'POST', array(
164 * 'message' => 'Hola!',
165 * ));
166 * }
167 * </pre>
168 *
169 * @return Facebook|WP_Error
170 */
171 function gianism_fb_admin(){
172 /** @var \Gianism\Service\Facebook $facebook */
173 $facebook = \Gianism\Service\Facebook::get_instance();
174 return $facebook->admin;
175 }
176
177 /**
178 * Get admin facebook id
179 *
180 * This will be user id or facebook page id.
181 *
182 * <pre>
183 * // Example
184 * $fb = gianism_fb_admin();
185 * if( !is_wp_error($fb) ){
186 * $feed = $fb->api(gianism_fb_admin_id().'/feed');
187 * foreach( $feed['data'] as $status ){
188 * // Do stuff.
189 * }
190 * }
191 * </pre>
192 *
193 * @return int|string
194 */
195 function gianism_fb_admin_id() {
196 /** @var \Gianism\Service\Facebook $facebook */
197 $facebook = \Gianism\Service\Facebook::get_instance();
198 return $facebook->admin_id;
199 }
200
201 /**
202 * Returns if user is connected with particular web service.
203 *
204 * @since 1.0
205 * @param string $service One of facebook, mixi, yahoo, twitter or google.
206 * @param int $user_id If not specified, current user id will be used.
207 * @return boolean
208 */
209 function is_user_connected_with($service, $user_id = 0){
210 /** @var \Gianism\Bootstrap $gianism */
211 $gianism = \Gianism\Bootstrap::get_instance();
212 if(!$user_id){
213 $user_id = get_current_user_id();
214 }
215 return ($instance = $gianism->get_service_instance($service) ) && $instance->is_connected($user_id);
216 }
217
218
219
220 /**
221 * Get user object by credencial
222 *
223 * Only facebook is supported.
224 *
225 * @todo Make other services to work.
226 * @global \wpdb $wpdb
227 * @since 1.0
228 * @param string $service
229 * @param mixed $credential
230 * @return \WP_User
231 */
232 function get_user_by_service($service, $credential){
233 global $wpdb;
234 $gianism = \Gianism\Bootstrap::get_instance();
235 $instance = $gianism->get_instance($service);
236 if(!$instance){
237 return false;
238 }
239 switch($service){
240 case 'facebook':
241 $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s", $credential));
242 if($user_id){
243 return new WP_User($user_id);
244 }else{
245 return null;
246 }
247 break;
248 default:
249 return null;
250 break;
251 }
252 }
253
254
255
256 /**
257 * Returns if current user liked or not.
258 *
259 * You can use this function only on Facebook fan page tab.
260 *
261 * @since 1.0
262 * @return boolean
263 */
264 function is_user_like_fangate(){
265 /** @var \Gianism\Service\Facebook $fb */
266 $fb = \Gianism\Service\Facebook::get_instance();
267 return $fb->is_user_like_me_on_fangate();
268 }
269
270
271
272 /**
273 * Returns if current user is guest.
274 *
275 * Valid only on facebook fan gate.
276 *
277 * @since 1.0
278 * @return boolean
279 */
280 function is_guest_on_fangate(){
281 /** @var \Gianism\Service\Facebook $fb */
282 $fb = \Gianism\Service\Facebook::get_instance();
283 return $fb->is_guest_on_fangate();
284 }
285
286
287
288 /**
289 * Returns if current user has wordpress account.
290 *
291 * You can this function only on Facebook fan page tab.
292 *
293 * @global \wpdb $wpdb
294 * @param string $service
295 * @return boolean
296 */
297 function is_user_registered_with($service){
298 global $wpdb;
299 $gianism = \Gianism\Bootstrap::get_instance();
300 $instance = $gianism->get_instance($service);
301 switch($service){
302 case 'facebook':
303 $user_id = get_user_id_on_fangate();
304 $sql = <<<EOS
305 SELECT user_id FROM {$wpdb->usermeta}
306 WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s
307 EOS;
308 return $wpdb->get_var($wpdb->prepare($sql, $signed['user_id']));
309 break;
310 default:
311 return false;
312 break;
313 }
314 }
315
316
317
318 /**
319 * Returns facebook id on fan gate.
320 *
321 * @since 1.0
322 * @return string|boolean
323 */
324 function get_user_id_on_fangate(){
325 /** @var \Gianism\Service\Facebook $fb */
326 $fb = \Gianism\Service\Facebook::get_instance();
327 return $fb->is_registered_user_on_fangate();
328 }
329
330
331
332 /**
333 * Get Twitter Screen Name
334 *
335 * @since 1.2
336 * @param int $user_id
337 * @return string|false
338 */
339 function get_twitter_screen_name($user_id){
340 /** @var \Gianism\Service\Twitter $twitter */
341 $twitter = \Gianism\Service\Twitter::get_instance();
342 return get_user_meta($user_id, $twitter->umeta_screen_name, true);
343 }
344
345
346
347 /**
348 * Update Twitter timeline
349 *
350 * @since 1.3
351 * @param string $string
352 */
353 function update_twitter_status($string){
354 /** @var \Gianism\Service\Twitter $twitter */
355 $twitter = \Gianism\Service\Twitter::get_instance();
356 $twitter->tweet($string);
357 }
358
359 /**
360 * Reply to specified user by Owner Account
361 *
362 * @since 1.0
363 * @param int $user_id
364 * @param string $string
365 * @return boolean
366 */
367 function twitter_reply_to($user_id, $string){
368 $screen_name = get_twitter_screen_name($user_id);
369 if($screen_name){
370 update_twitter_status("@{$screen_name} ".$string);
371 return true;
372 }else{
373 return false;
374 }
375 }
376
377 /**
378 * Get twitter time line in JSON format object
379 *
380 * Caching is recommended.
381 *
382 * <pre>
383 * $timeline = get_transient('twitter_timeline');
384 * if( false === $timeline){
385 * $timeline = twitter_get_timeline('my_screen_name');
386 * set_transient('twitter_timeline', $timeline, 3600);
387 * }
388 * foreach($timeline as $status){
389 * // Echo status
390 * }
391 * </pre>
392 *
393 * @since 1.3
394 * @param string $screen_name If not specified, admin user's screen name will be used.
395 * @param array $additional_data
396 * @return object JSON format object.
397 */
398 function twitter_get_timeline($screen_name = null, array $additional_data = array()){
399 /** @var \Gianism\Service\Twitter $twitter */
400 $twitter = \Gianism\Service\Twitter::get_instance();
401 if( is_null($screen_name) ){
402 $screen_name = $twitter->tw_screen_name;
403 }
404 return $twitter->call_api('statuses/user_timeline', array_merge(
405 array('screen_name' => $screen_name),
406 $additional_data
407 ));
408 }
409
410
411 /**
412 * Show Login buttons
413 *
414 * Show login buttons where you want.
415 *
416 * @since 1.0
417 * @param string $before
418 * @param string $after
419 * @param string $redirect_to
420 */
421 function gianism_login($before = '', $after = '', $redirect_to = ''){
422 /** @var \Gianism\Login $login */
423 $login = \Gianism\Login::get_instance();
424 $login->login_form($before, $after, false, $redirect_to);
425 }
426
427 /**
428 * Detect if user is geek
429 *
430 * Geek means user is connected with Github.
431 *
432 * @since 2.0.0
433 * @param int $user_id
434 * @return bool
435 */
436 function is_geek($user_id){
437 /** @var \Gianism\Service\Github $github */
438 $github = \Gianism\Service\Github::get_instance();
439 return (bool)get_user_meta($user_id, $github->umeta_id);
440 }
441
442 /**
443 * Detect if current user is geek
444 *
445 * @since 2.0.0
446 * @see is_geek
447 * @return bool
448 */
449 function is_current_user_geek(){
450 return is_geek(get_current_user_id());
451 }
452