PluginProbe
Gianism / 1.1
Gianism v1.1
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 / WP_Gianism.php

WP_Gianism.php in Gianism 1.1, at WP_Gianism.php

552 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Utility Class for WP Gianism.
4 * @package wp_gianism
5 */
6 class WP_Gianism{
7
8 /**
9 * @var Facebook_Controller
10 */
11 public $fb = null;
12
13 /**
14 * @var Twitter_Controller
15 */
16 public $twitter = null;
17
18 /**
19 * @var Google_Controller
20 */
21 public $google = null;
22
23 /**
24 * @var string
25 */
26 private $version;
27
28 /**
29 * @var string
30 */
31 protected $name = "wp_gianism";
32
33 /**
34 * @var string
35 */
36 private $domain = 'wp-gianism';
37
38 /**
39 * @var string
40 */
41 public $dir;
42
43 /**
44 * @var string
45 */
46 public $url;
47
48 /**
49 * @var array
50 */
51 protected $admin_error = array();
52
53 /**
54 * @var array
55 */
56 protected $admin_message = array();
57
58 /**
59 * @var array
60 */
61 protected $container = array();
62
63 /**
64 * @var array
65 */
66 public $option = array();
67
68 /**
69 * @var string
70 */
71 public $message_post_type = 'gianism_message';
72
73 /**
74 * オプション初期値
75 * @var array
76 */
77 protected $default_option = array(
78 'fb_enabled' => 0,
79 'fb_app_id' => '',
80 'fb_app_secret' => '',
81 'fb_fan_gate' => 0,
82 'tw_enabled' => 0,
83 "tw_screen_name" => "",
84 "tw_consumer_key" => "",
85 "tw_consumer_secret" => "",
86 "tw_access_token" => "",
87 "tw_access_token_secret" => "",
88 "ggl_enabled" => 0,
89 "ggl_consumer_key" => "",
90 "ggl_consumer_secret" => "",
91 "ggl_redirect_uri" => ""
92 );
93
94 /**
95 *
96 * @param string $base_file
97 * @param string $version
98 */
99 public function __construct($base_file, $version) {
100 //Setup Configs
101 $this->dir = plugin_dir_path($base_file);
102 $this->url = plugin_dir_url($base_file);
103 $this->version = $version;
104 $saved_option = get_option($this->name."_option");
105 foreach($this->default_option as $key => $value){
106 if(!isset($saved_option[$key])){
107 $this->option[$key] = $value;
108 }else{
109 $this->option[$key] = $saved_option[$key];
110 }
111 }
112 //Register Hooks
113 add_action("init", array($this, "init"));
114 add_action("admin_init", array($this, "admin_init"));
115 add_action("admin_menu", array($this, "admin_menu"));
116 add_action("admin_notice", array($this, "admin_notice"));
117 //Add i18n
118 load_plugin_textdomain($this->domain, false, basename($this->dir).DIRECTORY_SEPARATOR."language");
119 }
120
121 /**
122 * Common Hook
123 */
124 public function init(){
125 //Facebook
126 if($this->is_enabled('facebook')){
127 require_once $this->dir."/sdks/facebook/facebook_controller.php";
128 $this->fb = new Facebook_Controller($this->option);
129 }
130 //Twitter
131 if($this->is_enabled('twitter')){
132 require_once $this->dir."/sdks/twitter/twitter_controller.php";
133 $this->twitter = new Twitter_Controller($this->option);
134 }
135 //Google
136 if($this->is_enabled("google")){
137 require_once $this->dir."/sdks/google/google_controller.php";
138 $this->google = new Google_Controller($this->option);
139 }
140 if($this->is_enabled()){
141 //Create Post type
142 $this->create_message_post_type();
143 //Show Login button on profile page
144 add_action('show_user_profile', array($this, 'show_user_profile'));
145 add_action('show_user_profile', array($this, 'show_direct_message'));
146 //Show Login button on login page
147 add_action('login_form', array($this, 'show_login_form'));
148 //Show Register button on Register page
149 add_action('register_form', array($this, 'show_regsiter_form'));
150 //Load CSS
151 add_action('admin_print_styles', array($this, 'enqueue_style'));
152 add_action('wp_print_styles', array($this, 'enqueue_style'));
153 //Add Ajax Action
154 add_action('wp_ajax_wpg_ajax', array($this, 'ajax'));
155 }
156 //Add Assets
157 add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
158 add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
159 }
160
161 public function create_message_post_type(){
162 register_post_type($this->message_post_type,array(
163 'public' => false
164 ));
165 }
166
167 public function show_direct_message(){
168 global $user_ID, $user_email, $user_identity;
169 if(!defined("IS_PROFILE_PAGE")){
170 return;
171 }
172 ?>
173 <h3><?php printf($this->_('Message to %s'), $user_identity); ?></h3>
174 <?php
175 $query = new WP_Query("post_type={$this->message_post_type}&author={$user_ID}&posts_per_page=-1&post_status=publish,private");
176 if($query->have_posts()): ?>
177 <div class="wpg-message">
178 <table class="form-table">
179 <tbody>
180 <?php while($query->have_posts()): $query->the_post(); ?>
181 <tr>
182 <th>
183 <?php the_title(); ?><br />
184 <small><?php the_time('Y-m-d H:i:s'); ?></small>
185 </th>
186 <td><?php the_content(); ?></td>
187 <td>
188 <a href="#<?php the_ID(); ?>" class="button delete"><?php $this->e('Delete'); ?></a>
189 </td>
190 </tr>
191 <?php endwhile; wp_reset_query(); ?>
192 </tbody>
193 </table>
194 </div>
195 <?php else:
196 printf("<p>%s</p>", $this->_('No message.'));
197 endif;
198 }
199
200 /**
201 * Public Hook
202 */
203 public function template_redirect(){
204
205 }
206
207
208
209 /**
210 * Action hook for admin panel.
211 */
212 public function admin_init(){
213 //Execute when option updated.
214 if($this->verify_nonce('option')){
215 $this->option = array(
216 'fb_enabled' => ($this->post('fb_enabled') == 1) ? 1 : 0,
217 'fb_app_id' => (string)$this->post('fb_app_id'),
218 'fb_app_secret' => (string)$this->post('fb_app_secret'),
219 'fb_fan_gate' => (int)$this->post('fb_fan_gate'),
220 'tw_screen_name' => (string)$this->post('tw_screen_name'),
221 'tw_enabled' => (string)($this->post('tw_enabled') == 1) ? 1 : 0,
222 "tw_consumer_key" => (string)$this->post('tw_consumer_key'),
223 "tw_consumer_secret" => (string)$this->post('tw_consumer_secret'),
224 "tw_access_token" => (string)$this->post('tw_access_token'),
225 "tw_access_token_secret" => (string)$this->post('tw_access_token_secret'),
226 'ggl_enabled' => ($this->post('ggl_enabled') == 1) ? 1 : 0,
227 "ggl_consumer_key" => (string)$this->post('ggl_consumer_key'),
228 "ggl_consumer_secret" => (string)$this->post('ggl_consumer_secret'),
229 "ggl_redirect_uri" => (string)$this->post('ggl_redirect_uri')
230 );
231 if(update_option("{$this->name}_option", $this->option)){
232 $this->add_message($this->_('Option updated.'));
233 }else{
234 $this->add_message($this->_('Option failed to update.'), true);
235 }
236 }
237 }
238
239 /**
240 * Hook for admin_menu
241 * @return void
242 */
243 public function admin_menu(){
244 add_users_page($this->_('External Service'), $this->_("External Service"), 'edit_users', 'gianism', array($this, 'render'));
245 //Create plugin link
246 add_filter('plugin_action_links', array($this, 'plugin_page_link'), 10, 2);
247 add_filter('plugin_row_meta', array($this, 'plugin_row_meta'), 10, 2);
248
249 }
250
251 /**
252 * Render options page
253 * @return void
254 */
255 public function render(){
256 require_once $this->dir.DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR."setting.php";
257 }
258
259 /**
260 * Render Profile Options
261 * @param WP_User $profileuser
262 * @return void
263 */
264 public function show_user_profile($profileuser){
265 ?>
266 <h3><?php $this->e('External Service'); ?></h3>
267 <table class="form-table">
268 <tbody>
269 <?php do_action('gianism_user_profile', $profileuser);?>
270 </tbody>
271 </table>
272 <?php
273 }
274
275 /**
276 * Show Login Form
277 * @return void
278 */
279 public function show_login_form(){
280 ?>
281 <p id="wpg-login">
282 <?php do_action('gianism_login_form');?>
283 </p>
284 <?php
285 }
286
287 /**
288 * Show registeration button on register form.
289 * @return void
290 */
291 public function show_regsiter_form(){
292 ?>
293 <p id="wpg-regsiter">
294 <?php do_action('gianism_regsiter_form');?>
295 </p>
296 <?php
297 }
298
299 /**
300 * Enqueue Javascripts on admin panel
301 * @param string $hook
302 */
303 public function enqueue_scripts($hook){
304 wp_enqueue_script('jquery');
305 if(defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE){
306 wp_enqueue_script('wpg-ajax', $this->url."/assets/message-manager.js", array('jquery'), $this->version);
307 wp_localize_script('wpg-ajax', 'WPG', array(
308 'endpoint' => admin_url('admin-ajax.php'),
309 'nonce' => wp_create_nonce('wpg_ajax'),
310 'action' => 'wpg_ajax',
311 'deleteConfirm' => $this->_('You really delete this message?'),
312 'deleteFailed' => $this->_('You cannot delete this message.'),
313 'deleteComplete' => $this->_('No message.')
314 ));
315 }
316 }
317
318 /**
319 * Manage Ajax Request
320 * @global int $user_ID
321 */
322 public function ajax(){
323 global $user_ID;
324 if(wp_verify_nonce($this->request('_wpnonce'), 'wpg_ajax')){
325 switch($this->request('type')){
326 default:
327 $post = wp_get_single_post($this->request('post_id'));
328 $json = array('status' => false);
329 if($post && $post->post_author == $user_ID){
330 wp_delete_post($post->ID);
331 $json['status'] = true;
332 }
333 header('Content-Type: application/json; charset=utf-8');
334 echo json_encode($json);
335 die();
336 break;
337 }
338 }
339 }
340
341 /**
342 * Enqueue CSS on both Public and Admin
343 */
344 public function enqueue_style(){
345 if($this->is_enabled()){
346 wp_enqueue_style($this->name, $this->url."/assets/gianism-style.css", array(), $this->version);
347 }
348 }
349
350 /**
351 * Returns whether service is enbaled.
352 * @param string $service
353 * @return boolean
354 */
355 public function is_enabled($service = 'any'){
356 $flg = false;
357 switch($service){
358 case "facebook":
359 $flg = (boolean)$this->option['fb_enabled'];
360 break;
361 case "twitter":
362 $flg = (boolean)$this->option['tw_enabled'];
363 break;
364 case "google":
365 $flg = (boolean)$this->option['ggl_enabled'];
366 break;
367 default:
368 $flg = (boolean)($this->option['fb_enabled'] || $this->option['tw_enabled']);
369 break;
370 }
371 return $flg;
372 }
373
374 /**
375 * Returns appropriate redirect url
376 * @param string $default
377 * @param string $redirect_to
378 * @return string
379 */
380 public function sanitize_redirect_to($default, $redirect_to){
381 if(!empty($redirect_to)){
382 $url = (string) $redirect_to;
383 //Check if it has schema
384 $url_splited = explode('://', $redirect_to);
385 if(count($url_splited) > 1){
386 $server_name = str_replace(".", '\.', $_SERVER['SERVER_NAME']);
387 //has schema and in same domain.
388 if(preg_match("/^https?(:\/\/{$server_name}[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/", $url)){
389 $default = $url;
390 }
391 }else{
392 //no schema and not started with absolute external path.
393 if(!preg_match("/^\/\//", $url) && preg_match("/^([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/", $url)){
394 $default = $url;
395 }
396 }
397 }
398 return (string) $default;
399 }
400
401
402 /**
403 * $_GETに値が設定されていたら返す
404 * @param string $key
405 * @return mixed
406 */
407 public function get($key){
408 if(isset($_GET[$key])){
409 return $_GET[$key];
410 }else{
411 return null;
412 }
413 }
414
415 /**
416 * $_POSTに値が設定されていたら返す
417 * @param string $key
418 * @return mixed
419 */
420 public function post($key){
421 if(isset($_POST[$key])){
422 return $_POST[$key];
423 }else{
424 return null;
425 }
426 }
427
428 /**
429 * $_REQUESTに値が設定されていたら返す
430 * @param string $key
431 * @return mixed
432 */
433 public function request($key){
434 if(isset($_REQUEST[$key])){
435 return $_REQUEST[$key];
436 }else{
437 return null;
438 }
439 }
440
441
442 /**
443 * nonce用に接頭辞をつけて返す
444 * @param string $action
445 * @return string
446 */
447 public function nonce_action($action){
448 return $this->name."_".$action;
449 }
450
451 /**
452 * wp_nonce_fieldのエイリアス
453 * @param type $action
454 */
455 public function nonce_field($action){
456 wp_nonce_field($this->nonce_action($action), "_{$this->name}_nonce");
457 }
458
459 /**
460 * nonceが合っているか確かめる
461 * @param string $action
462 * @param string $referrer
463 * @return boolean
464 */
465 public function verify_nonce($action, $referrer = false){
466 if($referrer){
467 return ( (wp_verify_nonce($this->request("_{$this->name}_nonce"), $this->nonce_action($action)) && $referrer == $this->request("_wp_http_referer")) );
468 }else{
469 return wp_verify_nonce($this->request("_{$this->name}_nonce"), $this->nonce_action($action));
470 }
471 }
472
473
474 /**
475 * 管理画面にメッセージを表示する
476 * @return void
477 */
478 public function admin_notice(){
479 if(!empty($this->admin_error)){
480 ?><div class="error"><?php
481 foreach($this->admin_error as $err){
482 ?><p><?php echo $err; ?></p><?php
483 }
484 ?></div><?php
485 }
486 if(!empty($this->admin_message)){
487 ?><div class="updated"><?php
488 foreach($this->admin_message as $message){
489 ?><p><?php echo $message; ?></p><?php
490 } ?></div><?php
491 }
492 }
493
494 /**
495 * 管理画面に表示するメッセージを追加する
496 * @param string $string
497 * @param boolean $error (optional) trueにするとエラーメッセージ
498 * @return void
499 */
500 public function add_message($string, $error = false){
501 if($error){
502 $this->admin_error[] = (string) $string;
503 }else{
504 $this->admin_message[] = (string) $string;
505 }
506 }
507
508 /**
509 * WordPressの_eのエイリアス
510 * @param string $text
511 * @return void
512 */
513 public function e($text){
514 _e($text, $this->domain);
515 }
516
517 /**
518 * WordPressの__のエイリアス
519 * @param string $text
520 * @return string
521 */
522 public function _($text){
523 return __($text, $this->domain);
524 }
525
526 /**
527 * For not called gettext
528 */
529 private function ___(){
530 $this->_('Connect user accounts with major web services like Facebook, twitter, etc. Stand on the shoulders of giants!');
531 }
532
533 /**
534 * Setup plugin links.
535 * @param array $links
536 * @param string $file
537 * @return array
538 */
539 public function plugin_page_link($links, $file){
540 if(false !== strpos($file, 'wp-gianism')){
541 array_unshift($links, '<a href="'.admin_url('users.php?page=gianism').'">'.__('Settings').'</a>');
542 }
543 return $links;
544 }
545
546 public function plugin_row_meta($links, $file){
547 if(false !== strpos($file, 'wp-gianism')){
548
549 }
550 return $links;
551 }
552 }