PluginProbe
Gianism / 2.0.0
Gianism v2.0.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.0.0, at functions.php

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