/wp-content/plugins/wp-to-twitter/wp-to-twitter-users.php

https://github.com/livinglab/openlab · PHP · 143 lines · 104 code · 9 blank · 30 comment · 14 complexity · b5cb48cd28f4e62066d8e51a691be514 MD5 · raw file

  1. <?php
  2. /**
  3. * User settings WP to Twitter
  4. *
  5. * @category Users
  6. * @package WP to Twitter
  7. * @author Joe Dolson
  8. * @license GPLv2 or later
  9. * @link https://www.joedolson.com/wp-to-twitter/
  10. */
  11. if ( '1' === get_option( 'jd_individual_twitter_users' ) ) {
  12. add_action( 'show_user_profile', 'wpt_twitter_profile' );
  13. add_action( 'edit_user_profile', 'wpt_twitter_profile' );
  14. add_action( 'profile_update', 'wpt_twitter_save_profile' );
  15. }
  16. if ( ! defined( 'ABSPATH' ) ) {
  17. exit;
  18. }
  19. /**
  20. * Show user profile data on Edit User pages.
  21. */
  22. function wpt_twitter_profile() {
  23. global $user_ID;
  24. $current_user = wp_get_current_user();
  25. $user_edit = ( isset( $_GET['user_id'] ) ) ? (int) $_GET['user_id'] : $user_ID;
  26. $is_enabled = get_user_meta( $user_edit, 'wp-to-twitter-enable-user', true );
  27. $twitter_username = get_user_meta( $user_edit, 'wp-to-twitter-user-username', true );
  28. $wpt_remove = get_user_meta( $user_edit, 'wpt-remove', true );
  29. if ( $current_user->ID === $user_ID || current_user_can( 'manage_options' ) ) {
  30. ?>
  31. <h3><?php _e( 'WP Tweets User Settings', 'wp-to-twitter' ); ?></h3>
  32. <?php
  33. if ( function_exists( 'wpt_connect_oauth_message' ) ) {
  34. wpt_connect_oauth_message( $user_edit );
  35. }
  36. ?>
  37. <table class="form-table">
  38. <tr>
  39. <th scope="row"><?php _e( 'Use My Twitter Username', 'wp-to-twitter' ); ?></th>
  40. <td>
  41. <input type="radio" name="wpt-enable-user" id="wpt-enable-user-3" value="mainAtTwitter"<?php checked( $is_enabled, 'mainAtTwitter' ); ?> /> <label for="wpt-enable-user-3"><?php _e( 'Tweet my posts with an @ reference to my username.', 'wp-to-twitter' ); ?></label><br/>
  42. <input type="radio" name="wpt-enable-user" id="wpt-enable-user-4" value="mainAtTwitterPlus"<?php checked( $is_enabled, 'mainAtTwitterPlus' ); ?> /> <label for="wpt-enable-user-4"><?php _e( 'Tweet my posts with an @ reference to both my username and to the main site username.', 'wp-to-twitter' ); ?></label>
  43. </td>
  44. </tr>
  45. <tr>
  46. <th scope="row">
  47. <label for="wpt-username"><?php _e( 'Your Twitter Username', 'wp-to-twitter' ); ?></label>
  48. </th>
  49. <td>
  50. <input type="text" name="wpt-username" id="wpt-username" value="<?php echo esc_attr( $twitter_username ); ?>"/>
  51. </td>
  52. </tr>
  53. <tr>
  54. <th scope="row">
  55. <label for="wpt-remove"><?php _e( 'Hide account name in Tweets', 'wp-to-twitter' ); ?></label></th>
  56. <td>
  57. <input type="checkbox" name="wpt-remove" id="wpt-remove" aria-describedby="wpt-remove-desc" value="on"<?php checked( 'on', $wpt_remove ); ?> /> <span id="wpt-remove-desc"><?php _e( 'Do not display my account in the #account# template tag.', 'wp-to-twitter' ); ?></span>
  58. </td>
  59. </tr>
  60. <?php echo apply_filters( 'wpt_twitter_user_fields', $user_edit ); ?>
  61. </table>
  62. <?php
  63. if ( current_user_can( 'wpt_twitter_oauth' ) || current_user_can( 'manage_options' ) ) {
  64. if ( function_exists( 'wtt_connect_oauth' ) ) {
  65. wtt_connect_oauth( $user_edit );
  66. }
  67. }
  68. } else {
  69. // hidden fields. If function is enabled, but this user does not have privileges to edit.
  70. ?>
  71. <input type="hidden" name="wp-to-twitter-enable-user" value="<?php echo esc_attr( $is_enabled ); ?>" />
  72. <input type="hidden" name="wp-to-twitter-user-username" value="<?php echo esc_attr( $twitter_username ); ?>" />
  73. <input type="hidden" name="wpt-remove" value="<?php echo esc_attr( $wpt_remove ); ?>" />
  74. <?php
  75. }
  76. }
  77. /**
  78. * This compensates for an old error where the user ID is echoed directly into the page.
  79. */
  80. add_filter( 'wpt_twitter_user_fields', 'wpt_basic_user_fields', 100, 1 );
  81. /**
  82. * Return empty string if value is an integer.
  83. *
  84. * @param int $user_edit User ID.
  85. *
  86. * @return empty string.
  87. */
  88. function wpt_basic_user_fields( $user_edit ) {
  89. if ( is_int( $user_edit ) ) {
  90. return '';
  91. }
  92. return $user_edit;
  93. }
  94. /**
  95. * Save user profile data
  96. */
  97. function wpt_twitter_save_profile() {
  98. global $user_ID;
  99. $current_user = wp_get_current_user();
  100. if ( isset( $_POST['user_id'] ) ) {
  101. $edit_id = (int) $_POST['user_id'];
  102. } else {
  103. $edit_id = $user_ID;
  104. }
  105. if ( current_user_can( 'wpt_twitter_oauth' ) || current_user_can( 'manage_options' ) ) {
  106. $enable = ( isset( $_POST['wpt-enable-user'] ) ) ? sanitize_text_field( $_POST['wpt-enable-user'] ) : '';
  107. $username = ( isset( $_POST['wpt-username'] ) ) ? sanitize_text_field( $_POST['wpt-username'] ) : '';
  108. $wpt_remove = ( isset( $_POST['wpt-remove'] ) ) ? 'on' : '';
  109. update_user_meta( $edit_id, 'wp-to-twitter-enable-user', $enable );
  110. update_user_meta( $edit_id, 'wp-to-twitter-user-username', $username );
  111. update_user_meta( $edit_id, 'wpt-remove', $wpt_remove );
  112. }
  113. // WPT PRO.
  114. apply_filters( 'wpt_save_user', $edit_id, $_POST );
  115. }
  116. add_action( 'admin_head', 'wpt_css' );
  117. /**
  118. * Output CSS governing styles for authorized users column.
  119. */
  120. function wpt_css() {
  121. ?>
  122. <style type="text/css">
  123. th#wpt {
  124. width: 60px;
  125. }
  126. .wpt_twitter .authorized {
  127. padding: 1px 3px;
  128. border-radius: 3px;
  129. background: #070;
  130. color: #fff;
  131. }
  132. </style>
  133. <?php
  134. }