PluginProbe
Gianism / 2.1.0
Gianism v2.1.0
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.1.0, at functions.php

405 lines 10.7 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
156 /**
157 * Returns if user is connected with particular web service.
158 *
159 * @since 1.0
160 * @param string $service One of facebook, mixi, yahoo, twitter or google.
161 * @param int $user_id If not specified, current user id will be used.
162 * @return boolean
163 */
164 function is_user_connected_with($service, $user_id = 0){
165 /** @var \Gianism\Bootstrap $gianism */
166 $gianism = \Gianism\Bootstrap::get_instance();
167 if(!$user_id){
168 $user_id = get_current_user_id();
169 }
170 return ($instance = $gianism->get_service_instance($service) ) && $instance->is_connected($user_id);
171 }
172
173
174
175 /**
176 * Get user object by credencial
177 *
178 * Only facebook is supported.
179 *
180 * @todo Make other services to work.
181 * @global \wpdb $wpdb
182 * @since 1.0
183 * @param string $service
184 * @param mixed $credential
185 * @return \WP_User
186 */
187 function get_user_by_service($service, $credential){
188 global $wpdb;
189 $gianism = \Gianism\Bootstrap::get_instance();
190 $instance = $gianism->get_instance($service);
191 if(!$instance){
192 return false;
193 }
194 switch($service){
195 case 'facebook':
196 $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s", $credential));
197 if($user_id){
198 return new WP_User($user_id);
199 }else{
200 return null;
201 }
202 break;
203 default:
204 return null;
205 break;
206 }
207 }
208
209
210
211 /**
212 * Returns if current user liked or not.
213 *
214 * You can use this function only on Facebook fan page tab.
215 *
216 * @since 1.0
217 * @return boolean
218 */
219 function is_user_like_fangate(){
220 /** @var \Gianism\Service\Facebook $fb */
221 $fb = \Gianism\Service\Facebook::get_instance();
222 return $fb->is_user_like_me_on_fangate();
223 }
224
225
226
227 /**
228 * Returns if current user is guest.
229 *
230 * Valid only on facebook fan gate.
231 *
232 * @since 1.0
233 * @return boolean
234 */
235 function is_guest_on_fangate(){
236 /** @var \Gianism\Service\Facebook $fb */
237 $fb = \Gianism\Service\Facebook::get_instance();
238 return $fb->is_guest_on_fangate();
239 }
240
241
242
243 /**
244 * Returns if current user has wordpress account.
245 *
246 * You can this function only on Facebook fan page tab.
247 *
248 * @global \wpdb $wpdb
249 * @param string $service
250 * @return boolean
251 */
252 function is_user_registered_with($service){
253 global $wpdb;
254 $gianism = \Gianism\Bootstrap::get_instance();
255 $instance = $gianism->get_instance($service);
256 switch($service){
257 case 'facebook':
258 $user_id = get_user_id_on_fangate();
259 $sql = <<<EOS
260 SELECT user_id FROM {$wpdb->usermeta}
261 WHERE meta_key = '{$instance->umeta_id}' AND meta_value = %s
262 EOS;
263 return $wpdb->get_var($wpdb->prepare($sql, $signed['user_id']));
264 break;
265 default:
266 return false;
267 break;
268 }
269 }
270
271
272
273 /**
274 * Returns facebook id on fan gate.
275 *
276 * @since 1.0
277 * @return string|boolean
278 */
279 function get_user_id_on_fangate(){
280 /** @var \Gianism\Service\Facebook $fb */
281 $fb = \Gianism\Service\Facebook::get_instance();
282 return $fb->is_registered_user_on_fangate();
283 }
284
285
286
287 /**
288 * Get Twitter Screen Name
289 *
290 * @since 1.2
291 * @param int $user_id
292 * @return string|false
293 */
294 function get_twitter_screen_name($user_id){
295 /** @var \Gianism\Service\Twitter $twitter */
296 $twitter = \Gianism\Service\Twitter::get_instance();
297 return get_user_meta($user_id, $twitter->umeta_screen_name, true);
298 }
299
300
301
302 /**
303 * Update Twitter timeline
304 *
305 * @since 1.3
306 * @param string $string
307 */
308 function update_twitter_status($string){
309 /** @var \Gianism\Service\Twitter $twitter */
310 $twitter = \Gianism\Service\Twitter::get_instance();
311 $twitter->tweet($string);
312 }
313
314 /**
315 * Reply to specified user by Owner Account
316 *
317 * @since 1.0
318 * @param int $user_id
319 * @param string $string
320 * @return boolean
321 */
322 function twitter_reply_to($user_id, $string){
323 $screen_name = get_twitter_screen_name($user_id);
324 if($screen_name){
325 update_twitter_status("@{$screen_name} ".$string);
326 return true;
327 }else{
328 return false;
329 }
330 }
331
332 /**
333 * Get twitter time line in JSON format object
334 *
335 * Caching is recommended.
336 *
337 * <pre>
338 * $timeline = get_transient('twitter_timeline');
339 * if( false === $timeline){
340 * $timeline = twitter_get_timeline('my_screen_name');
341 * set_transient('twitter_timeline', $timeline, 3600);
342 * }
343 * foreach($timeline as $status){
344 * // Echo status
345 * }
346 * </pre>
347 *
348 * @since 1.3
349 * @param string $screen_name If not specified, admin user's screen name will be used.
350 * @param array $additional_data
351 * @return object JSON format object.
352 */
353 function twitter_get_timeline($screen_name = null, array $additional_data = array()){
354 /** @var \Gianism\Service\Twitter $twitter */
355 $twitter = \Gianism\Service\Twitter::get_instance();
356 if(is_null($screen_name)){
357 $screen_name = $twitter->tw_screen_name;
358 }
359 return $twitter->call_api('statuses/user_timeline', array_merge(
360 array('screen_name' => $screen_name),
361 $additional_data
362 ));
363 }
364
365 /**
366 * Show Login buttons
367 *
368 * Show login buttons where you want.
369 *
370 * @since 1.0
371 * @param string $before
372 * @param string $after
373 */
374 function gianism_login($before = '', $after = ''){
375 /** @var \Gianism\Login $login */
376 $login = \Gianism\Login::get_instance();
377 $login->login_form($before, $after);
378 }
379
380 /**
381 * Detect if user is geek
382 *
383 * Geek means user is connected with Github.
384 *
385 * @since 2.0.0
386 * @param int $user_id
387 * @return bool
388 */
389 function is_geek($user_id){
390 /** @var \Gianism\Service\Github $github */
391 $github = \Gianism\Service\Github::get_instance();
392 return (bool)get_user_meta($user_id, $github->umeta_id);
393 }
394
395 /**
396 * Detect if current user is geek
397 *
398 * @since 2.0.0
399 * @see is_geek
400 * @return bool
401 */
402 function is_current_user_geek(){
403 return is_geek(get_current_user_id());
404 }
405