PageRenderTime 76ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/profile.php

https://gitlab.com/0072016/wordpress
PHP | 225 lines | 129 code | 35 blank | 61 comment | 45 complexity | e7ef69879bfd50f32173539844fc0b60 MD5 | raw file
  1. <?php
  2. /**
  3. * Add content to a WordPress user profile.
  4. *
  5. * @since 1.2
  6. */
  7. class Facebook_User_Profile {
  8. /**
  9. * Conditionally load features on the edit profile page.
  10. *
  11. * @since 1.2
  12. *
  13. * @return void
  14. */
  15. public static function init() {
  16. if ( ! current_user_can( 'edit_posts' ) )
  17. return;
  18. add_action( 'show_user_profile', array( 'Facebook_User_Profile', 'facebook_section' ) );
  19. add_action( 'admin_enqueue_scripts', array( 'Facebook_User_Profile', 'enqueue_scripts' ) );
  20. // disable posting to Facebook when publish_actions present
  21. add_action( 'personal_options', array( 'Facebook_User_Profile', 'personal_options' ) );
  22. // listen for Facebook changes
  23. add_action( 'personal_options_update', array( 'Facebook_User_Profile', 'save_data' ) );
  24. }
  25. /**
  26. * Add the login JavaScript to the WordPress script queue.
  27. *
  28. * @since 1.5
  29. *
  30. * @uses wp_enqueue_script()
  31. * @global \WP_Scripts $wp_scripts Add a script block to the enqueued script handle
  32. * @return void
  33. */
  34. public static function enqueue_scripts() {
  35. global $wp_scripts;
  36. if ( ! class_exists( 'Facebook_Settings' ) )
  37. require_once( dirname(__FILE__) . '/settings.php' );
  38. $handle = Facebook_Settings::register_login_script();
  39. wp_enqueue_script( $handle );
  40. // attach initialization JavaScript to WordPress enqueue. enqueue function for execution with Facebook SDK for JavaScript async loader
  41. $script = 'jQuery(document).one("facebook-login-load",function(){if(FB_WP.queue && FB_WP.queue.add){FB_WP.queue.add(function(){FB_WP.admin.login.person.init()})}});';
  42. $data = $wp_scripts->get_data( $handle, 'data' );
  43. if ( $data )
  44. $script = $data . "\n" . $script;
  45. $wp_scripts->add_data( $handle, 'data', $script );
  46. }
  47. /**
  48. * Allow an author to disable posting to Timeline by default.
  49. *
  50. * @since 1.2
  51. *
  52. * @param WP_User $wordpress_user WordPress user object for the current profile.
  53. * @return void
  54. */
  55. public static function personal_options( $wordpress_user ) {
  56. if ( ! ( $wordpress_user && isset( $wordpress_user->ID ) ) )
  57. return;
  58. if ( ! class_exists( 'Facebook_User' ) )
  59. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  60. if ( ! Facebook_User::can_publish_to_facebook( $wordpress_user->ID, false /* do not check for the presence of publish override in this option field */ ) )
  61. return;
  62. echo '<tr class="facebook-post-to-timeline"><th scope="row">Facebook</th><td><input class="checkbox" type="checkbox" name="facebook_timeline" id="facebook-timeline" value="1"';
  63. checked( ! Facebook_User::get_user_meta( $wordpress_user->ID, 'facebook_timeline_disabled', true ) );
  64. echo ' /> <label for="facebook-timeline">' . esc_html( __( 'Post an article to my Facebook Timeline after it is public.', 'facebook' ) ) . '</label><br /></td></tr>';
  65. }
  66. /**
  67. * Add a Facebook section to the WordPress user profile page.
  68. *
  69. * @since 1.5
  70. *
  71. * @global \Facebook_Loader $facebook_loader Access Facebook application credentials.
  72. * @param WP_User $wp_user WordPress user for the current profile page.
  73. * @return void
  74. */
  75. public static function facebook_section( $wp_user ) {
  76. global $facebook_loader;
  77. if ( ! ( $wp_user && isset( $wp_user->ID ) && method_exists( $wp_user, 'exists' ) && $wp_user->exists() && user_can( $wp_user, 'edit_posts' ) ) )
  78. return;
  79. $section = '<h3>' . esc_html( __( 'Facebook Account', 'facebook' ) ) . '</h3>';
  80. if ( ! class_exists( 'Facebook_User' ) )
  81. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  82. $facebook_user_data = Facebook_User::get_user_meta( $wp_user->ID, 'fb_data', true );
  83. $section .= '<table id="facebook-info" class="form-table"';
  84. // test if Facebook account associated with current WordPress user context
  85. if ( is_array( $facebook_user_data ) && isset( $facebook_user_data['fb_uid'] ) ) {
  86. $section .= ' data-fbid="' . esc_attr( $facebook_user_data['fb_uid'] ) . '"';
  87. if ( isset( $facebook_loader->credentials['app_id'] ) )
  88. $section .= ' data-appid="' . esc_attr( $facebook_loader->credentials['app_id'] ) . '">';
  89. $section .= '<tr><th scope="row">' . esc_html( _x( 'Connected Profile', 'Connected Facebook Profile', 'facebook' ) ) . '</th>';
  90. $section .= '<td><p><a href="' . esc_url( Facebook_User::facebook_profile_link( $facebook_user_data ), array( 'http', 'https' ) ) . '">' . esc_html( $facebook_user_data['fb_uid'] ) . '</a></p>';
  91. if ( isset( $facebook_user_data['activation_time'] ) )
  92. $section .= '<div class="description"><p>' . sprintf( esc_html( __( 'Associated on %s', 'facebook' ) ), '<time datetime="' . gmstrftime( '%FT%T', $facebook_user_data['activation_time'] ) . '+00:00">' . date_i18n( get_option('date_format'), $facebook_user_data['activation_time'] ) . '</time>' ) . '</p></div>';
  93. $section .= '<p class="submit"><input id="facebook-remove" name="facebook_remove" class="button button-primary" type="submit" value="' . esc_attr( _x( 'Remove Facebook account', 'Remove an association between a Facebook account and a WordPress user profile', 'facebook' ) ) . '" />' . '</p>';
  94. $section .= '</td></tr>';
  95. if ( ! class_exists( 'Facebook_WP_Extend' ) )
  96. require_once( $facebook_loader->plugin_directory . 'includes/facebook-php-sdk/class-facebook-wp.php' );
  97. $permissions = Facebook_WP_Extend::get_permissions_by_facebook_user_id( $facebook_user_data['fb_uid'] );
  98. if ( ! empty( $permissions ) ) {
  99. $permission_labels = array();
  100. if ( isset( $permissions['installed'] ) )
  101. $permission_labels[] = '<a href="https://www.facebook.com/about/privacy/your-info#public-info">' . esc_html( __( 'Public profile information', 'facebook' ) ) . '</a>';
  102. if ( isset( $permissions['publish_actions'] ) )
  103. $permission_labels[] = esc_html( __( 'Publish to Timeline', 'facebook' ) );
  104. if ( isset( $permissions['manage_pages'] ) && isset( $permissions['publish_stream'] ) )
  105. $permission_labels[] = '<a href="https://developers.facebook.com/docs/reference/login/page-permissions/">' . esc_html( __( 'Manage your pages on your behalf (including creating content)', 'facebook' ) ) . '</a>';
  106. $section .= '<tr><th scope="row">' . esc_html( __( 'Permissions', 'facebook' ) ) . '</th><td>';
  107. if ( empty( $permissions ) ) {
  108. $section .= __( 'None', 'facebook' );
  109. } else {
  110. $section .= '<ul><li>' . implode( '</li><li>', $permission_labels ) . '</li></ul>';
  111. }
  112. $section .= '<div id="facebook-login"></div></td></tr>';
  113. }
  114. } else {
  115. $section .= '><tr><th scope="row">' . esc_html( _x( 'Get started', 'Begin the process', 'facebook' ) ) . '</th>';
  116. $section .= '<td id="facebook-login"></td></tr>';
  117. }
  118. $section .= '</table>';
  119. echo $section;
  120. }
  121. /**
  122. * Save custom user information.
  123. *
  124. * @since 1.2
  125. *
  126. * @uses current_user_can() current user must be able to edit the passed WordPress user ID
  127. * @param int $wordpress_user_id WordPress user identifier
  128. * @return void
  129. */
  130. public static function save_data( $wordpress_user_id ) {
  131. if ( ! ( $wordpress_user_id && current_user_can( 'edit_user', $wordpress_user_id ) ) )
  132. return;
  133. // allow decoupling of a WordPress account and a Facebook account
  134. if ( isset( $_POST['facebook_remove'] ) ) {
  135. // WordPress Facebook User helper functions
  136. if ( ! class_exists( 'Facebook_User' ) )
  137. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  138. $facebook_user_id = Facebook_User::get_facebook_profile_id( $wordpress_user_id );
  139. if ( $facebook_user_id ) {
  140. // delete mapped FBID and other data
  141. Facebook_User::delete_user_meta( $wordpress_user_id, 'fb_data' );
  142. // delete post to Timeline opt-in if stored
  143. Facebook_User::delete_user_meta( $wordpress_user_id, 'facebook_timeline_disabled' );
  144. // Load WP HTTP helpers
  145. if ( ! class_exists( 'Facebook_WP_Extend' ) )
  146. require_once( dirname( dirname(__FILE__) ) . '/includes/facebook-php-sdk/class-facebook-wp.php' );
  147. // Revoke connection to app and all permissions
  148. Facebook_WP_Extend::graph_api_with_app_access_token( $facebook_user_id . '/permissions', 'DELETE' );
  149. }
  150. unset( $facebook_user_id );
  151. // no need to store any other Facebook data
  152. return;
  153. }
  154. if ( isset( $_POST['facebook_fbid'] ) && ctype_digit( $_POST['facebook_fbid'] ) ) {
  155. // WordPress Facebook User helper functions
  156. if ( ! class_exists( 'Facebook_User' ) )
  157. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  158. try {
  159. $facebook_user = Facebook_User::get_facebook_user( $_POST['facebook_fbid'], array( 'fields' => array( 'id', 'username', 'link', 'third_party_id' ) ) );
  160. if ( isset( $facebook_user['id'] ) ) {
  161. $facebook_user_data = array(
  162. 'fb_uid' => $facebook_user['id'],
  163. 'activation_time' => time()
  164. );
  165. if ( ! empty( $facebook_user['username'] ) )
  166. $facebook_user_data['username'] = $facebook_user['username'];
  167. if ( ! empty( $facebook_user['link'] ) )
  168. $facebook_user_data['link'] = $facebook_user['link'];
  169. if ( ! empty( $facebook_user['third_party_id'] ) )
  170. $facebook_user_data['third_party_id'] = $facebook_user['third_party_id'];
  171. Facebook_User::update_user_meta( $wordpress_user_id, 'fb_data', $facebook_user_data );
  172. unset( $facebook_user_data );
  173. }
  174. unset( $facebook_user );
  175. } catch(Exception $e) {}
  176. }
  177. if ( isset( $_POST[ 'facebook_timeline' ] ) && $_POST[ 'facebook_timeline' ] == '1' ) {
  178. // WordPress Facebook User helper functions
  179. if ( ! class_exists( 'Facebook_User' ) )
  180. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  181. Facebook_User::delete_user_meta( $wordpress_user_id, 'facebook_timeline_disabled' ); // delete if stored
  182. } else {
  183. // WordPress Facebook User helper functions
  184. if ( ! class_exists( 'Facebook_User' ) )
  185. require_once( dirname( dirname(__FILE__) ) . '/facebook-user.php' );
  186. Facebook_User::update_user_meta( $wordpress_user_id, 'facebook_timeline_disabled', '1' );
  187. }
  188. }
  189. }
  190. ?>