class-simple-author-box-user-profile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class Simple_Author_Box_User_Profile {
  3. function __construct() {
  4. // Social Links
  5. add_action( 'show_user_profile', array( $this, 'add_social_area' ) );
  6. add_action( 'edit_user_profile', array( $this, 'add_social_area' ) );
  7. // Custom Profile Image
  8. add_action( 'show_user_profile', array( $this, 'add_profile_image' ), 9, 1 );
  9. add_action( 'edit_user_profile', array( $this, 'add_profile_image' ), 9, 1 );
  10. add_action( 'personal_options_update', array( $this, 'save_user_profile' ) );
  11. add_action( 'edit_user_profile_update', array( $this, 'save_user_profile' ) );
  12. // Allow HTML in user description.
  13. remove_filter( 'pre_user_description', 'wp_filter_kses' );
  14. add_filter( 'pre_user_description', 'wp_kses_post' );
  15. }
  16. public function add_social_area( $profileuser ) {
  17. $user_id = $profileuser->data->ID;
  18. $social_links = Simple_Author_Box_Helper::get_user_social_links( $user_id );
  19. $social_icons = apply_filters( 'sabox_social_icons', Simple_Author_Box_Helper::$social_icons );
  20. unset( $social_icons['user_email'] );
  21. ?>
  22. <div class="sab-user-profile-wrapper">
  23. <h2><?php _e( 'Social Media Links (Simple Author Box)', 'saboxplugin' ); ?></h2>
  24. <table class="form-table" id="sabox-social-table">
  25. <?php
  26. if ( ! empty( $social_links ) ) {
  27. foreach ( $social_links as $social_platform => $social_link ) {
  28. ?>
  29. <tr>
  30. <th>
  31. <span class="sabox-drag"></span>
  32. <select name="sabox-social-icons[]">
  33. <?php foreach ( $social_icons as $sabox_social_id => $sabox_social_name ) { ?>
  34. <option value="<?php echo $sabox_social_id; ?>" <?php selected( $sabox_social_id, $social_platform ); ?>><?php echo $sabox_social_name; ?></option>
  35. <?php } ?>
  36. </select>
  37. </th>
  38. <td>
  39. <input name="sabox-social-links[]" type="text" class="regular-text" value="<?php echo esc_url( $social_link ); ?>">
  40. <span class="dashicons dashicons-trash"></span>
  41. <td>
  42. </tr>
  43. <?php
  44. }
  45. } else {
  46. ?>
  47. <tr>
  48. <th>
  49. <span class="sabox-drag"></span>
  50. <select name="sabox-social-icons[]">
  51. <?php foreach ( $social_icons as $sabox_social_id => $sabox_social_name ) { ?>
  52. <option value="<?php echo $sabox_social_id; ?>"><?php echo $sabox_social_name; ?></option>
  53. <?php } ?>
  54. </select>
  55. </th>
  56. <td>
  57. <input name="sabox-social-links[]" type="text" class="regular-text" value="">
  58. <span class="dashicons dashicons-trash"></span>
  59. <td>
  60. </tr>
  61. <?php
  62. }
  63. ?>
  64. </table>
  65. <div class="sabox-add-social-link">
  66. <a href="#" class="button button-primary button-hero"></span><?php esc_html_e( '+ Add new social platform', 'saboxplugin' ); ?></a>
  67. </div>
  68. </div>
  69. <?php
  70. }
  71. public function add_profile_image( $user ) {
  72. if ( ! current_user_can( 'upload_files' ) ) {
  73. return;
  74. }
  75. $default_url = SIMPLE_AUTHOR_BOX_ASSETS . 'img/default.png';
  76. $image = get_user_meta( $user->ID, 'sabox-profile-image', true );
  77. ?>
  78. <div id="sabox-custom-profile-image">
  79. <h3><?php _e( 'Custom User Profile Image (Simple Author Box)', 'saboxplugin' ); ?></h3>
  80. <table class="form-table">
  81. <tr>
  82. <th><label for="cupp_meta"><?php _e( 'Profile Image', 'saboxplugin' ); ?></label></th>
  83. <td>
  84. <div id="sab-current-image">
  85. <?php wp_nonce_field( 'sabox-profile-image', 'sabox-profile-nonce' ); ?>
  86. <img data-default="<?php echo esc_url_raw( $default_url ); ?>" src="<?php echo '' != $image ? esc_url_raw( $image ) : esc_url_raw( $default_url ); ?>"><br>
  87. <input type="text" name="sabox-custom-image" id="sabox-custom-image" class="regular-text" value="<?php echo esc_attr( $image ); ?>">
  88. </div>
  89. <div class="actions">
  90. <a href="#" class="button-secondary" id="sabox-remove-image"><?php _e( 'Remove Image', 'saboxplugin' ); ?></a>
  91. <a href="#" class="button-primary" id="sabox-add-image"><?php _e( 'Upload Image', 'saboxplugin' ); ?></a>
  92. </div>
  93. </td>
  94. </tr>
  95. </table>
  96. </div>
  97. <?php
  98. }
  99. public function save_user_profile( $user_id ) {
  100. if ( isset( $_POST['sabox-social-icons'] ) && isset( $_POST['sabox-social-links'] ) ) {
  101. $social_platforms = apply_filters( 'sabox_social_icons', Simple_Author_Box_Helper::$social_icons );
  102. $social_links = array();
  103. foreach ( $_POST['sabox-social-links'] as $index => $social_link ) {
  104. if ( $social_link ) {
  105. $social_platform = isset( $_POST['sabox-social-icons'][ $index ] ) ? $_POST['sabox-social-icons'][ $index ] : false;
  106. if ( $social_platform && isset( $social_platforms[ $social_platform ] ) ) {
  107. $social_links[ $social_platform ] = esc_url_raw( $social_link );
  108. }
  109. }
  110. }
  111. update_user_meta( $user_id, 'sabox_social_links', $social_links );
  112. } else {
  113. delete_user_meta( $user_id, 'sabox_social_links' );
  114. }
  115. if ( ! isset( $_POST['sabox-profile-nonce'] ) || ! wp_verify_nonce( $_POST['sabox-profile-nonce'], 'sabox-profile-image' ) ) {
  116. return;
  117. }
  118. if ( ! current_user_can( 'upload_files', $user_id ) ) {
  119. return;
  120. }
  121. if ( isset( $_POST['sabox-custom-image'] ) && '' != $_POST['sabox-custom-image'] ) {
  122. update_user_meta( $user_id, 'sabox-profile-image', esc_url_raw( $_POST['sabox-custom-image'] ) );
  123. } else {
  124. delete_user_meta( $user_id, 'sabox-profile-image' );
  125. }
  126. }
  127. }
  128. new Simple_Author_Box_User_Profile();