PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/facebook/admin/login.php

https://bitbucket.org/metrobee/tr.deconord.eu
PHP | 118 lines | 66 code | 17 blank | 35 comment | 15 complexity | 019da02bc8b0f4d7a23508657315bddb MD5 | raw file
  1. <?php
  2. /**
  3. * Encourage WordPress users to associate their account with a Facebook profile
  4. *
  5. * @since 1.1
  6. */
  7. class Facebook_Admin_Login {
  8. /**
  9. * Check if the current user has associated his or her Facebook profile with his or her WordPress account
  10. * If the current user can edit posts and has not authorized Facebook then show a prompt encouraging action.
  11. *
  12. * @since 1.1
  13. */
  14. public static function connect_facebook_account( $verify_permissions = null ) {
  15. global $facebook;
  16. $profile_prompt = false;
  17. // check for permission to publish Open Graph action (publish article)
  18. // check for the superset permission: publish_stream
  19. if ( ! is_array( $verify_permissions ) ) {
  20. $profile_prompt = true;
  21. $verify_permissions = array( 'publish_actions', 'publish_stream' );
  22. }
  23. $current_user = wp_get_current_user();
  24. // no need to alert if he cannot create a post
  25. if ( ! user_can( $current_user, 'edit_posts' ) )
  26. return;
  27. if ( ! class_exists( 'Facebook_User' ) )
  28. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  29. $facebook_user_data_exists = false;
  30. $facebook_user_data = Facebook_User::get_user_meta( $current_user->ID, 'fb_data', true );
  31. if ( is_array( $facebook_user_data ) && isset( $facebook_user_data['fb_uid'] ) ) {
  32. if ( empty( $verify_permissions ) )
  33. return;
  34. $facebook_user_data_exists = true;
  35. }
  36. // Facebook information not found
  37. $facebook_user = Facebook_User::get_current_user( array( 'id','username' ) );
  38. if ( $facebook_user ) {
  39. $permissions = $facebook->get_current_user_permissions( $facebook_user );
  40. $all_permissions_exist = true;
  41. foreach( $verify_permissions as $permission_to_verify ) {
  42. if ( ! isset( $permissions[$permission_to_verify] ) ) {
  43. $all_permissions_exist = false;
  44. break;
  45. }
  46. }
  47. if ( $all_permissions_exist ) {
  48. if ( ! $facebook_user_data_exists || $facebook_user_data['fb_uid'] != $facebook_user['id'] ) {
  49. $facebook_user_data = array(
  50. 'fb_uid' => $facebook_user['id'],
  51. 'activation_time' => time()
  52. );
  53. if ( ! empty( $facebook_user['username'] ) )
  54. $facebook_user_data['username'] = $facebook_user['username'];
  55. Facebook_User::update_user_meta( $current_user->ID, 'fb_data', $facebook_user_data );
  56. }
  57. return;
  58. }
  59. }
  60. // priority before js sdk registration needed to add JS inside FbAsyncInit
  61. add_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'add_async_load_javascript_filter' ), -1, 0 );
  62. // add all others at P11 after scripts registered
  63. add_action( 'admin_enqueue_scripts', array( 'Facebook_Admin_Login', 'enqueue_scripts' ), 11 );
  64. if ( $profile_prompt )
  65. add_action( 'admin_notices', array( 'Facebook_Admin_Login', 'admin_notice' ), 1, 0 ); // up top
  66. }
  67. /**
  68. * Prompt current user to associate his or her WordPress account with a Facebook profile
  69. *
  70. * @since 1.1
  71. */
  72. public static function admin_notice() {
  73. // prompt user to associate his or her WordPress account with a Facebook account
  74. echo '<div class="updated"><p>';
  75. echo sprintf( esc_html( __( '%s to publish new posts to your Facebook timeline.', 'facebook' ) ), '<span class="facebook-login" data-scope="person" style="font-weight:bold">' . esc_html( __( 'Associate your WordPress account with a Facebook profile', 'facebook' ) ) . '</span>' );
  76. echo '</p></div>';
  77. }
  78. public static function add_async_load_javascript_filter() {
  79. // async load our script after we async load Facebook JavaScript SDK
  80. add_filter( 'facebook_jssdk_init_extras', array( 'Facebook_Admin_Login', 'async_load_javascript' ), 10, 2 );
  81. }
  82. /**
  83. * Load support for Facebook JavaScript SDK and FB.login
  84. *
  85. * @since 1.1
  86. */
  87. public static function enqueue_scripts() {
  88. wp_enqueue_script( 'jquery' ); // should already be enqueued in wp-admin
  89. wp_enqueue_script( 'facebook-jssdk' );
  90. }
  91. /**
  92. * add JavaScript code to the fbAsyncInit function run after Facebook JavaScript SDK has loaded.
  93. *
  94. * @since 1.1
  95. * @return string JavaScript code to be appended to the fbAsyncInit function
  96. */
  97. public static function async_load_javascript( $js_block = '', $app_id = '' ) {
  98. return $js_block . 'jQuery.ajax({url:' . json_encode( plugins_url( 'static/js/admin/login' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min' ) . '.js', dirname(__FILE__) ) ) . ',cache:true,dataType:"script"}).success(function(){FB_WP.admin.login.attach_events();});';
  99. }
  100. }
  101. ?>