WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-user.php
Go to the documentation of this file.
1 <?php
2 
13 class WPDKUserMeta {
14 
20  const STATUS = '_wpdk_user_status';
21 
27  const STATUS_DESCRIPTION = '_wpdk_user_status_description';
28 
34  const COUNT_SUCCESS_LOGIN = '_wpdk_user_count_success_login';
35 
41  const COUNT_WRONG_LOGIN = '_wpdk_user_count_wrong_login';
42 
48  const LAST_TIME_SUCCESS_LOGIN = '_wpdk_user_last_time_success_login';
49 
55  const LAST_TIME_WRONG_LOGIN = '_wpdk_user_last_time_wrong_login';
56 
62  const LAST_TIME_LOGOUT = '_wpdk_user_last_time_logout';
63 
70  const REMOTE_ADDR = '_wpdk_user_remote_address';
71 
88  public static function update( $user_id, $post_data )
89  {
90 
91  /* LAST_TIME_SUCCESS_LOGIN */
92  $value = isset( $post_data[self::LAST_TIME_SUCCESS_LOGIN] ) ? $post_data[self::LAST_TIME_SUCCESS_LOGIN] : '';
93  if ( !empty( $value ) ) {
94  $value = strtotime( $value );
95  update_user_meta( $user_id, self::LAST_TIME_SUCCESS_LOGIN, $value );
96  }
97 
98  /* COUNT_SUCCESS_LOGIN */
99  $value = isset( $post_data[self::COUNT_SUCCESS_LOGIN] ) ? $post_data[self::COUNT_SUCCESS_LOGIN] : '';
100  update_user_meta( $user_id, self::COUNT_SUCCESS_LOGIN, $value );
101 
102  /* LAST_TIME_WRONG_LOGIN */
103  $value = isset( $post_data[self::LAST_TIME_WRONG_LOGIN] ) ? $post_data[self::LAST_TIME_WRONG_LOGIN] : '';
104  if ( !empty( $value ) ) {
105  $value = strtotime( $value );
106  update_user_meta( $user_id, self::LAST_TIME_WRONG_LOGIN, $value );
107  }
108 
109  /* COUNT_WRONG_LOGIN */
110  $value = isset( $post_data[self::COUNT_WRONG_LOGIN] ) ? $post_data[self::COUNT_WRONG_LOGIN] : '';
111  update_user_meta( $user_id, self::COUNT_WRONG_LOGIN, $value );
112 
113  /* LAST_TIME_LOGOUT */
114  $value = isset( $post_data[self::LAST_TIME_LOGOUT] ) ? $post_data[self::LAST_TIME_LOGOUT] : '';
115  if ( !empty( $value ) ) {
116  $value = strtotime( $value );
117  update_user_meta( $user_id, self::LAST_TIME_LOGOUT, $value );
118  }
119 
120  /* STATUS */
121  $value = isset( $post_data[self::STATUS] ) ? $post_data[self::STATUS] : '';
122  update_user_meta( $user_id, self::STATUS, $value );
123 
124  /* STATUS_DESCRIPTION */
125  $value = isset( $post_data[self::STATUS_DESCRIPTION] ) ? $post_data[self::STATUS_DESCRIPTION] : '';
126  update_user_meta( $user_id, self::STATUS_DESCRIPTION, $value );
127 
128  }
129 
130 }
131 
132 
144 
150  const DISABLED = 'disabled';
151 
159  public static function statuses()
160  {
161  $statuses = array(
162  '' => __( 'Not set' ),
163  self::DISABLED => __( 'Disabled' ),
164  );
165  return apply_filters( 'wpdk_user_status_statuses', $statuses );
166  }
167 }
168 
169 
183 class WPDKUser extends WP_User {
184 
192  public $first_name;
193 
201  public $last_name;
202 
210  public $nice_name;
211 
219  public $full_name;
220 
229 
237  public $email;
238 
246  public $status;
247 
256 
266  public static function current_user()
267  {
268  // Check for user logged in
269  if( !is_user_logged_in() ) {
270  return null;
271  }
272  return new self;
273  }
274 
287  public function __construct( $user = 0, $name = '', $blog_id = '' )
288  {
289 
290  $id_user = 0;
291 
292  // Sanitize $id
293  if ( is_numeric( $user ) ) {
294  $id_user = $user;
295 
296  // If zero get the current id user
297  if ( empty( $id_user ) ) {
298  $id_user = get_current_user_id();
299  }
300  }
301  elseif ( is_object( $user ) && isset( $user->ID ) ) {
302  $id_user = absint( $user->ID );
303  }
304  elseif ( is_array( $user ) && isset( $user['ID'] ) ) {
305  $id_user = absint( $user['ID'] );
306  }
307 
308  // Get by email
309  elseif ( is_string( $user ) && is_email( $user ) ) {
310  $user = get_user_by( 'email', $user );
311  $id_user = $user->ID;
312  }
313 
314  parent::__construct( $id_user, $name, $blog_id );
315 
316  // Set the extended property when an user is set
317  if ( !empty( $id_user ) ) {
318  $this->first_name = $this->get( 'first_name' );
319  $this->last_name = $this->get( 'last_name' );
320  $this->nice_name = $this->data->user_nicename;
321  $this->full_name = $this->full_name( $this->first_name, $this->last_name );
322  $this->display_name = $this->data->display_name;
323  $this->email = sanitize_email( $this->data->user_email );
324  $this->status = $this->get( WPDKUserMeta::STATUS );
325  $this->statusDescription = $this->get( WPDKUserMeta::STATUS_DESCRIPTION );
326 
327  // Sanitize string->int
328  $this->data->ID = absint( $id_user );
329  }
330  }
331 
342  public static function nice_name( $firstName, $lastName )
343  {
344  $result = sprintf( '%s.%s', strtoupper( substr( $firstName, 0, 1 ) ), ucfirst( $lastName ) );
345  return $result;
346  }
347 
359  public static function full_name( $firstName, $lastName, $nameFirst = true )
360  {
361  if ( $nameFirst ) {
362  $result = sprintf( '%s %s', $firstName, $lastName );
363  }
364  else {
365  $result = sprintf( '%s %s', $lastName, $firstName );
366  }
367  return $result;
368  }
369 
384  public function create( $first_name, $last_name, $email, $password = false, $enabled = false, $role = 'subscriber' )
385  {
386  return WPDKUsers::init()->create( $first_name, $last_name, $email, $password, $enabled, $role );
387  }
388 
389  // -------------------------------------------------------------------------------------------------------------------
390  // User transient
391  // -------------------------------------------------------------------------------------------------------------------
392 
409  public static function getTransientWithUser( $transient, $user_id = null )
410  {
411  $user_id = is_null( $user_id ) ? get_current_user_id() : $user_id;
412 
413  $pre = apply_filters( 'pre_user_transient_' . $transient, false, $user_id );
414  if ( false !== $pre ) {
415  return $pre;
416  }
417 
418  $transient_timeout = '_transient_timeout_' . $transient;
419  $transient = '_transient_' . $transient;
420  if ( get_user_meta( $user_id, $transient_timeout, true ) < time() ) {
421  delete_user_meta( $user_id, $transient );
422  delete_user_meta( $user_id, $transient_timeout );
423  return false;
424  }
425 
426  $value = get_user_meta( $user_id, $transient, true );
427 
428  return apply_filters( 'user_transient_' . $transient, $value, $user_id );
429  }
430 
446  public function getTransient( $transient )
447  {
448  if( !empty( $this->ID ) ) {
449  return self::getTransientWithUser( $transient, $this->ID );
450  }
451  }
452 
464  public static function getTransientTimeWithUser( $transient, $user_id = null )
465  {
466  $user_id = is_null( $user_id ) ? get_current_user_id() : $user_id;
467  $transient_timeout = '_transient_timeout_' . $transient;
468  return get_user_meta( $user_id, $transient_timeout, true );
469  }
470 
481  public function getTransientTime( $transient )
482  {
483  if ( !empty( $this->ID ) ) {
484  return self::getTransientTimeWithUser( $transient, $this->ID );
485  }
486  }
487 
488 
508  public static function setTransientWithUser( $transient, $value, $expiration = 0, $user_id = null )
509  {
510  $user_id = is_null( $user_id ) ? get_current_user_id() : $user_id;
511 
512  $value = apply_filters( 'pre_set_user_transient_' . $transient, $value, $user_id );
513 
514  $transient_timeout = '_transient_timeout_' . $transient;
515  $transient = '_transient_' . $transient;
516  if ( false === get_user_meta( $user_id, $transient, true ) ) {
517  if ( $expiration ) {
518  update_user_meta( $user_id, $transient_timeout, time() + $expiration );
519  }
520  $result = update_user_meta( $user_id, $transient, $value );
521  }
522  else {
523  if ( $expiration ) {
524  update_user_meta( $user_id, $transient_timeout, time() + $expiration );
525  }
526  $result = update_user_meta( $user_id, $transient, $value );
527  }
528 
529  if ( $result ) {
530  do_action( 'set_user_transient_' . $transient );
531  do_action( 'setted_user_transient', $transient, $user_id );
532  }
533  return $result;
534  }
535 
552  public function setTransient( $transient, $value, $expiration = 0 )
553  {
554  if ( !empty( $this->ID ) ) {
555  self::setTransientWithUser( $transient, $value, $expiration, $this->ID );
556  }
557  }
558 
559 
560  // -------------------------------------------------------------------------------------------------------------------
561  // User info
562  // -------------------------------------------------------------------------------------------------------------------
563 
575  public function gravatar( $size = 40, $alt = '', $default = "wavatar" )
576  {
577  return WPDKUsers::init()->gravatar( $this->ID, $size, $alt, $default );
578  }
579 
590  public function avatar( $size = 40 )
591  {
592  return WPDKUsers::init()->avatar( $this->ID, $size );
593  }
594 
595 
596 
608  public function age( $birthday )
609  {
610  $year_diff = 0;
611 
612  if ( !empty( $birthday ) ) {
613  if ( false !== strpos( $birthday, '-' ) ) {
614  list( $year, $month, $day ) = explode( '-', $birthday );
615  }
616  else {
617  list( $day, $month, $year ) = explode( '/', $birthday );
618  }
619  $year_diff = date( 'Y' ) - $year;
620  $month_diff = date( 'm' ) - $month;
621  $day_diff = date( 'd' ) - $day;
622  if ( $month_diff < 0 || ( $month_diff == 0 && $day_diff < 0 ) ) {
623  $year_diff--;
624  }
625  }
626  return intval( $year_diff );
627  }
628 
629  // -------------------------------------------------------------------------------------------------------------------
630  // Roles
631  // -------------------------------------------------------------------------------------------------------------------
632 
642  public function hasRoles( $roles )
643  {
644  global $wp_roles;
645 
646  if ( !isset( $wp_roles ) ) {
647  $wp_roles = new WP_Roles();
648  }
649 
650  $current_roles = $this->roles;
651  if ( is_array( $roles ) ) {
652  foreach ( $roles as $role ) {
653  if ( in_array( $role, $current_roles ) ) {
654  return true;
655  }
656  }
657  return false;
658  }
659  else {
660  return in_array( $roles, $current_roles );
661  }
662  }
663 
671  public static function roleNameForUserID( $id_user )
672  {
673  global $wp_roles;
674 
675  $id_user = absint( $id_user );
676  $user = new WP_User( $id_user );
677  if ( !empty( $user ) ) {
678  $role_key = $user->roles[key( $user->roles )];
679  if ( !empty( $role_key ) ) {
680  return $wp_roles->roles[$role_key]['name'];
681  }
682  }
683  return false;
684  }
685 
697  public static function hasCap( $cap, $id_user = null )
698  {
699  if ( is_null( $id_user ) ) {
700  $id_user = get_current_user_id();
701  }
702  $user = new WP_User( $id_user );
703  if ( $user ) {
704  return $user->has_cap( $cap );
705  }
706  return false;
707  }
708 
718  public function hasCaps( $caps )
719  {
720 
721  $all_caps = $this->allcaps;
722  if ( is_array( $caps ) ) {
723  foreach ( $caps as $cap ) {
724  if ( isset( $all_caps[$cap] ) ) {
725  return true;
726  }
727  }
728  }
729  else {
730  return in_array( $all_caps, $caps );
731  }
732  return false;
733  }
734 
743  public static function allCapabilities()
744  {
745  global $wp_roles;
746  $merge = array();
747 
748  $roles = $wp_roles->get_names();
749  foreach ( $roles as $key => $rolename ) {
750  $role = get_role( $key );
751  $merge = array_merge( $merge, $role->capabilities );
752  }
753  $result = array_keys( $merge );
754  sort( $result );
755  $result = array_combine( $result, $result );
756  return $result;
757  }
758 
771  public static function updateUserCapabilities( $id_user, $selected_caps, $capabilities )
772  {
773  if ( $id_user && is_array( $selected_caps ) ) {
774  $user = new WP_User( $id_user );
775  foreach ( $capabilities as $key => $cap ) {
776  if ( in_array( $key, $selected_caps ) ) {
777  /* Add */
778  $user->add_cap( $key );
779  }
780  else {
781  /* Del */
782  $user->remove_cap( $key );
783  }
784  }
785  }
786  }
787 
788 }
789 
790 
809 class WPDKUsers {
810 
818  public static function init()
819  {
820  static $instance = null;
821  if ( is_null( $instance ) ) {
822  $instance = new WPDKUsers();
823  }
824  return $instance;
825  }
826 
835  private function __construct()
836  {
837 
838  /* Do a several action/filter to monitoring user action. */
839 
840  //$this->logout();
841  add_action( 'init', array( $this, 'logout' ) );
842 
843  /* Main hook for common check in front end. */
844  //add_action( 'wp_head', array( $this, 'wp_head_signin' ) );
845  //add_action( 'wp_head', array( $this, 'wp_head_signout' ) );
846 
847  /* Hook on Login. */
848  add_action( 'wp_login', array( $this, 'wp_login' ) );
849  add_action( 'wp_logout', array( $this, 'wp_logout' ) );
850  add_action( 'wp_login_failed', array( $this, 'wp_login_failed' ) );
851 
852  /* includes/wp_insert_user() Nuovo Utente registrato */
853  //add_action( 'user_register', array( $this, 'user_register' ) );
854 
855  /* includes/wp_insert_user() Utente già registrato quindi aggiornamento dati */
856  add_action( 'delete_user', array( $this, 'delete_user' ) );
857  add_action( 'deleted_user', array( $this, 'deleted_user' ) );
858 
859  /* Backend user profile (own) */
860  add_action( 'show_user_profile', array( $this, 'show_user_profile' ) );
861  add_action( 'personal_options_update', array( $this, 'personal_options_update' ) );
862  add_action( 'personal_options', array( $this, 'personal_options' ) );
863  add_action( 'profile_personal_options', array( $this, 'profile_personal_options' ) );
864 
865  /* Backend user profile (other) */
866  add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) );
867  add_action( 'edit_user_profile_update', array( $this, 'edit_user_profile_update' ) );
868 
869  /* Extends User edit profile */
870 
871  /* Disable and locking featured */
872  add_filter( 'wp_authenticate_user', array( $this, 'wp_authenticate_user' ), 1 );
873 
874  add_filter( 'user_contactmethods', array( $this, 'user_contactmethods' ) );
875  }
876 
882  public function logout()
883  {
884  /* If a user is logged in. */
885  if ( is_user_logged_in() ) {
886  $user_id = get_current_user_id();
887  $status = get_user_meta( $user_id, WPDKUserMeta::STATUS, true );
888  $logout = false;
889 
890  /* Logout for disabled User. */
891  if ( !empty( $status ) && WPDKUserStatus::DISABLED == $status ) {
892  $logout = true;
893  }
894 
895  /* Manual logout. */
896  if ( isset( $_REQUEST['wpdk_logout'] ) ) {
897  $logout = true;
898  }
899 
900  if ( true === $logout ) {
901  /* Log off the user. */
902  wp_logout();
903  if ( is_admin() ) {
904  wp_safe_redirect( wp_login_url( wp_get_referer() ) );
905  }
906  else {
907  wp_safe_redirect( stripslashes( $_SERVER['REQUEST_URI'] ) );
908  }
909  exit;
910  }
911  }
912  }
913 
914 
915  // -----------------------------------------------------------------------------------------------------------------
916  // Signin actions and filters
917  // -----------------------------------------------------------------------------------------------------------------
918 
928  public function wp_authenticate_user( $user )
929  {
930  if ( is_wp_error( $user ) ) {
931  return $user;
932  }
933 
934  /* Get the user status. */
935  $status = $user->get( WPDKUserMeta::STATUS );
936 
937  if ( WPDKUserStatus::DISABLED == $status ) {
938  /* Ask for continue. */
939  $continue = apply_filters( 'wpdk_users_should_denied_signin', true, $user->ID, $status );
940  if ( $continue ) {
941  $message = $user->get( WPDKUserMeta::STATUS_DESCRIPTION );
942  $message = apply_filters( 'wpdk_users_access_denied_status_description', $message, $user->ID, $status );
943  return new WP_Error( 'wpdk_users_access_denied', $message, array(
944  $user,
945  $status
946  ) );
947  }
948  }
949 
950  return $user;
951  }
952 
961  public function wp_login( $user_login, $user = null )
962  {
963 
964  /* Get user by login. */
965  if ( is_null( $user ) ) {
966  $user = get_user_by( 'login', $user_login );
967  }
968 
969  /* Get success login count. */
970  $count = absint( $user->get( WPDKUserMeta::COUNT_SUCCESS_LOGIN ) );
971  if ( empty( $count ) ) {
972  $count = 0;
973  }
974  update_user_meta( $user->ID, WPDKUserMeta::COUNT_SUCCESS_LOGIN, $count + 1 );
975  update_user_meta( $user->ID, WPDKUserMeta::COUNT_WRONG_LOGIN, 0 );
976  update_user_meta( $user->ID, WPDKUserMeta::LAST_TIME_SUCCESS_LOGIN, time() );
977  }
978 
988  public function wp_login_failed( $user_login )
989  {
990 
991  if ( empty( $user_login ) ) {
992  return false;
993  }
994 
995  $user = get_user_by( 'login', $user_login );
996 
997  /* Check if the user exists. */
998  if ( false === $user ) {
999  return false;
1000  }
1001 
1002  $count = absint( $user->get( WPDKUserMeta::COUNT_WRONG_LOGIN ) );
1003  if ( empty( $count ) ) {
1004  $count = 0;
1005  }
1006  $count++;
1007 
1008  /* Update the wrong login count. */
1009  update_user_meta( $user->ID, WPDKUserMeta::COUNT_WRONG_LOGIN, $count );
1010 
1011  /* Notified the wrong user login count. */
1012  do_action( 'wpdk_user_wrong_login_count', $user->ID, $count );
1013 
1014  return true;
1015 
1016  }
1017 
1018  // -------------------------------------------------------------------------------------------------------------------
1019  // Signin utility
1020  // -------------------------------------------------------------------------------------------------------------------
1021 
1033  public function signIn( $user, $password, $remember = false )
1034  {
1035 
1036  $user_id = false;
1037  $email = '';
1038 
1039  /* Check user id */
1040  if ( is_numeric( $user ) ) {
1041  $user_id = $user;
1042  }
1043  /* Check for email */
1044  elseif ( is_email( $user ) ) {
1045  /* Sanitize email */
1046  $email = sanitize_email( $user );
1047  /* User exists with email */
1048  $user_id = email_exists( $email );
1049  }
1050  /* Check for user login */
1051  else {
1052  $user = get_user_by( 'login', $user );
1053  if ( $user ) {
1054  $user_id = $user->ID;
1055  }
1056  }
1057 
1058  if ( false !== $user_id ) {
1059  $user = new WPDKUser( $user_id );
1060  if ( $user->exists() && WPDKUserStatus::DISABLED !== $user->status ) {
1061  $result = wp_authenticate( $user->user_login, $password );
1062  if ( !is_wp_error( $result ) ) {
1063  /* Set remember cookie */
1064  wp_set_auth_cookie( $user->ID, $remember );
1065  do_action( 'wp_login', $user->user_login, $user );
1066 
1067  /* Internal counter */
1068  $this->wp_login( $user->user_login, $user );
1069 
1070  /* Authenticate! You are */
1071  wp_set_current_user( $user->ID );
1072  return true;
1073  }
1074  }
1075  }
1076 
1077  do_action( 'wpdk_singin_wrong', empty( $email ) ? $user_id : $email, $password );
1078 
1079  return false;
1080  }
1081 
1093  public function authenticate( $email, $password )
1094  {
1095  if ( empty( $email ) || empty( $password ) ) {
1096  return false;
1097  }
1098 
1099  $id_user = email_exists( $email );
1100  if ( !empty( $id_user ) ) {
1101  $user = new WPDKUser( $id_user );
1102  $access = wp_authenticate( $user->data->user_login, $password );
1103  if ( !is_wp_error( $access ) ) {
1104  return $id_user;
1105  }
1106  }
1107  return false;
1108  }
1109 
1110  // -----------------------------------------------------------------------------------------------------------------
1111  // Signout actions and filters
1112  // -----------------------------------------------------------------------------------------------------------------
1113 
1119  public function wp_logout()
1120  {
1121  $user_id = get_current_user_id();
1122  if ( !empty( $user_id ) ) {
1123  update_user_meta( $user_id, WPDKUserMeta::LAST_TIME_LOGOUT, time() );
1124  }
1125  }
1126 
1127  // -----------------------------------------------------------------------------------------------------------------
1128  // Signout utility
1129  // -----------------------------------------------------------------------------------------------------------------
1130 
1136  public function signout()
1137  {
1138 
1139  /* Esegue il logout da WordPress. */
1140  wp_logout();
1141 
1142  /* Per default ridirezione sulla home page del sito. */
1143  $blog_url = get_bloginfo( 'url' );
1144  wp_safe_redirect( $blog_url );
1145  exit();
1146  }
1147 
1148  // ----------------------------------------------------------------------------------------------------------------
1149  // Create utility
1150  // -----------------------------------------------------------------------------------------------------------------
1151 
1164  public function create( $first_name, $last_name, $email, $password = false, $enabled = false, $role = 'subscriber' )
1165  {
1166 
1167  /* For security reason an user must have a password. */
1168  if ( false === $password ) {
1169  $password = WPDKCrypt::randomAlphaNumber();
1170  /* @todo Notify to user the password. */
1171  do_action( 'wpdk_users_random_password', $password );
1172  }
1173 
1174  $nice_name = WPDKUser::nice_name( $first_name, $last_name );
1175  $display_name = WPDKUser::full_name( $first_name, $last_name );
1176 
1177  $user_data = array(
1178  'user_login' => $email,
1179  'user_pass' => $password,
1180  'user_email' => $email,
1181  'user_nicename' => $nice_name,
1182  'nickname' => $nice_name,
1183  'display_name' => $display_name,
1184  'first_name' => $first_name,
1185  'last_name' => $last_name,
1186  'role' => $role
1187  );
1188 
1189  $user_id = wp_insert_user( $user_data );
1190 
1191  if ( is_wp_error( $user_id ) ) {
1192  return $user_id;
1193  }
1194 
1195  /* Store IP Address */
1196  if ( isset( $_SERVER['REMOTE_ADDR'] ) && !empty( $_SERVER['REMOTE_ADDR'] ) ) {
1197  update_user_meta( $user_id, WPDKUserMeta::REMOTE_ADDR, $_SERVER['REMOTE_ADDR'] );
1198  }
1199 
1200  /* Disable user if required */
1201  if ( false === $enabled ) {
1202  $status = apply_filters( 'wpdk_users_status', WPDKUserStatus::DISABLED, $user_id );
1203  update_user_meta( $user_id, WPDKUserMeta::STATUS, $status );
1204 
1205  $status_description = apply_filters( 'wpdk_users_status_description', '', $user_id );
1206  update_user_meta( $user_id, WPDKUserMeta::STATUS_DESCRIPTION, $status_description );
1207  }
1208 
1209  return $user_id;
1210  }
1211 
1212 
1213  // -----------------------------------------------------------------------------------------------------------------
1214  // User profile (own)
1215  // -----------------------------------------------------------------------------------------------------------------
1216 
1225  public function show_user_profile( $user )
1226  {
1227 
1228  echo '<br clear="all" /><a name="wpdk"></a>';
1229 
1230  $message = __( 'This view <strong>is enhanced</strong> by wpXtreme and WPDK framework. Please, have a look below for additional information.', WPDK_TEXTDOMAIN );
1231  $alert = new WPDKTwitterBootstrapAlert( 'wpdk-alert-show_user_profile', $message, WPDKTwitterBootstrapAlertType::INFORMATION );
1232  $alert->permanent_dismiss = true;
1233  $alert->display();
1234 
1235  /* Only the administrator can edit this extra information */
1236  $disabled = !current_user_can( 'manage_options' );
1237 
1238  /* Sanitize values */
1239  $last_time_success_login = $user->get( WPDKUserMeta::LAST_TIME_SUCCESS_LOGIN );
1240  $last_time_wrong_login = $user->get( WPDKUserMeta::LAST_TIME_WRONG_LOGIN );
1241  $last_time_logout = $user->get( WPDKUserMeta::LAST_TIME_LOGOUT );
1242  $status_description = $user->get( WPDKUserMeta::STATUS_DESCRIPTION );
1243 
1244  $fields = array(
1245  __( 'Login information', WPDK_TEXTDOMAIN ) => array(
1246  array(
1247  array(
1248  'type' => WPDKUIControlType::DATETIME,
1250  'label' => __( 'Last success login', WPDK_TEXTDOMAIN ),
1251  'value' => empty( $last_time_success_login ) ? '' : date( __( 'm/d/Y H:i', WPDK_TEXTDOMAIN ), $last_time_success_login ),
1252  'disabled' => $disabled
1253  ),
1254  array(
1255  'type' => WPDKUIControlType::NUMBER,
1257  'label' => __( '# success login', WPDK_TEXTDOMAIN ),
1258  'value' => $user->get( WPDKUserMeta::COUNT_SUCCESS_LOGIN ),
1259  'disabled' => true
1260  ),
1261  ),
1262  array(
1263  array(
1264  'type' => WPDKUIControlType::DATETIME,
1266  'label' => __( 'Last wrong login', WPDK_TEXTDOMAIN ),
1267  'value' => empty( $last_time_wrong_login ) ? '' : date( __( 'm/d/Y H:i', WPDK_TEXTDOMAIN ), $last_time_wrong_login ),
1268  'disabled' => $disabled
1269  ),
1270  array(
1271  'type' => WPDKUIControlType::NUMBER,
1273  'label' => __( '# wrong login', WPDK_TEXTDOMAIN ),
1274  'value' => $user->get( WPDKUserMeta::COUNT_WRONG_LOGIN ),
1275  'disabled' => $disabled
1276  ),
1277  ),
1278  ),
1279 
1280  __( 'Logout information', WPDK_TEXTDOMAIN ) => array(
1281  array(
1282  array(
1283  'type' => WPDKUIControlType::DATETIME,
1285  'label' => __( 'Last logout', WPDK_TEXTDOMAIN ),
1286  'value' => empty( $last_time_logout ) ? '' : date( __( 'm/d/Y H:i', WPDK_TEXTDOMAIN ), $last_time_logout ),
1287  'disabled' => $disabled
1288  ),
1289  ),
1290  ),
1291 
1292  __( 'Status', WPDK_TEXTDOMAIN ) => array(
1293  array(
1294  array(
1295  'type' => WPDKUIControlType::SELECT,
1296  'name' => WPDKUserMeta::STATUS,
1297  'label' => __( 'Status', WPDK_TEXTDOMAIN ),
1298  'value' => $user->get( WPDKUserMeta::STATUS ),
1299  'options' => WPDKUserStatus::statuses(),
1300  'disabled' => $disabled
1301  ),
1302  ),
1303  array(
1304  array(
1305  'type' => WPDKUIControlType::TEXTAREA,
1307  'rows' => 3,
1308  'cols' => 40,
1309  'label' => __( 'Description', WPDK_TEXTDOMAIN ),
1310  'placeholder' => __( 'eg: this user is disabled because...', WPDK_TEXTDOMAIN ),
1311  'value' => $status_description,
1312  'disabled' => $disabled,
1313  )
1314  ),
1315  ),
1316  );
1317 
1318  $fields = apply_filters( 'wpdk_users_fields_profile', $fields, $user );
1319 
1320  $layout = new WPDKUIControlsLayout( $fields );
1321  $layout->display();
1322 
1323  do_action( 'wpdk_users_show_user_profile', $user );
1324  }
1325 
1335  public function personal_options_update( $id_user )
1336  {
1337  /* Same for other users, see below */
1338  $this->edit_user_profile_update( $id_user );
1339  }
1340 
1349  public function personal_options( $user )
1350  {
1351  $message = __( 'This view <strong>is enhanced</strong> by wpXtreme and WPDK framework. Please, <strong><a href="#wpdk">have a look below</a></strong> for additional information.', WPDK_TEXTDOMAIN );
1352  $alert = new WPDKTwitterBootstrapAlert( 'wpdk-alert-personal_options', $message, WPDKTwitterBootstrapAlertType::INFORMATION );
1353  $alert->permanent_dismiss = true;
1354  $alert->display();
1355  }
1356 
1367  public function profile_personal_options( $user )
1368  {
1369  /* Nothing to do... for now */
1370  }
1371 
1372  // -----------------------------------------------------------------------------------------------------------------
1373  // User profile (other)
1374  // -----------------------------------------------------------------------------------------------------------------
1375 
1385  public function edit_user_profile( $user )
1386  {
1387  /* At this moment display the same informations */
1388  $this->show_user_profile( $user );
1389  }
1390 
1402  public function edit_user_profile_update( $id_user )
1403  {
1404  if ( !current_user_can( 'edit_user' ) ) {
1405  return false;
1406  }
1407 
1408  /* Update the WPDK extra information */
1409  if ( current_user_can( 'manage_options' ) ) {
1410  WPDKUserMeta::update( $id_user, $_POST );
1411  }
1412  return true;
1413  }
1414 
1415 
1433  public function delete_user( $id_user )
1434  {
1435  /* @todo Do implement */
1436  }
1437 
1457  public function deleted_user( $id_user )
1458  {
1459  /* @todo Do implement */
1460  }
1461 
1474  public function user_contactmethods( $contacts )
1475  {
1476  return $contacts;
1477  }
1478 
1479 
1490  public static function userWithMetaAndValue( $meta_key, $meta_value )
1491  {
1492  global $wpdb;
1493 
1494  $sql = <<< SQL
1495 SELECT user_id
1496 FROM {$wpdb->usermeta}
1497 WHERE meta_key = '{$meta_key}'
1498 AND meta_value = '{$meta_value}'
1499 SQL;
1500  $result = $wpdb->get_var( $sql );
1501 
1502  return $result;
1503  }
1504 
1512  public static function usersWithCaps( $find_caps )
1513  {
1514  $users_caps = WPDKCapabilities::getInstance()->usersCapability();
1515 
1516  $users = array();
1517  foreach ( $users_caps as $user_id => $caps ) {
1518  $keys = array_keys( $caps );
1519  if ( in_array( $find_caps, $keys ) ) {
1520  $users[] = $user_id;
1521  }
1522  }
1523  return $users;
1524  }
1525 
1526  // -------------------------------------------------------------------------------------------------------------------
1527  // User info
1528  // -------------------------------------------------------------------------------------------------------------------
1529 
1542  public function gravatar( $id_user = null, $size = 40, $alt = '', $default = 'wavatar' )
1543  {
1544  if ( is_null( $id_user ) ) {
1545  $id_user = 0;
1546  }
1547  $user = new WPDKUser( $id_user );
1548  if ( $user ) {
1549  $alt = empty( $alt ) ? $user->display_name : $alt;
1550 
1551  // Use SSL
1552  $gravatar = is_ssl() ? 'https://secure.gravatar.com/avatar/' : 'http://0.gravatar.com/avatar/';
1553 
1554  $src = sprintf( '%s%s?s=%s&d=%s', $gravatar, md5( $user->email ), $size, $default );
1555  $html = sprintf( '<img src="%s" alt="%s" title="%s" />', $src, $alt, $alt );
1556 
1557  return $html;
1558  }
1559  return false;
1560  }
1561 
1573  public function avatar( $id_user = null, $size = 40 )
1574  {
1575  if ( is_null( $id_user ) ) {
1576  $id_user = 0;
1577  }
1578  $user = new WPDKUser( $id_user );
1579  if ( $user ) {
1580  $alt = empty( $alt ) ? $user->display_name : $alt;
1581 
1582  $default = apply_filters( 'wpdk_default_avatar', 'wavatar' );
1583 
1584  // Use SSL
1585  $gravatar = is_ssl() ? 'https://secure.gravatar.com/avatar/' : 'http://0.gravatar.com/avatar/';
1586 
1587  $src = sprintf( '%s%s?s=%s&d=%s', $gravatar, md5( $user->email ), $size, $default );
1588  $img = new WPDKHTMLTagImg( $src, $alt, $size, $size );
1589  return $img;
1590  }
1591  return false;
1592  }
1593 
1594 
1595 
1596  // -----------------------------------------------------------------------------------------------------------------
1597  // Users list
1598  // -----------------------------------------------------------------------------------------------------------------
1599 
1609  public function arrayUserForSDF()
1610  {
1611  $users = array();
1612  $users_list = get_users();
1613  if ( $users_list ) {
1614  foreach ( $users_list as $user ) {
1615  $user_id = '' . $user->ID;
1616  $users[$user_id] = sprintf( '%s (%s)', $user->display_name, $user->user_email );
1617  }
1618  }
1619  return $users;
1620  }
1621 
1622 
1623  // -----------------------------------------------------------------------------------------------------------------
1624  // DEPRECATED
1625  // -----------------------------------------------------------------------------------------------------------------
1626 
1636  public static function arrayRoles()
1637  {
1638  global $wp_roles;
1639 
1640  if ( !isset( $wp_roles ) ) {
1641  $wp_roles = new WP_Roles();
1642  }
1643 
1644  $result = array();
1645 
1646  $roles = $wp_roles->get_names();
1647  foreach ( $roles as $key => $role ) {
1648  $result[$key] = $role;
1649  }
1650  return $result;
1651  }
1652 }
1653 
1654 
1669 class WPDKRole extends WP_Role {
1670 
1678  public $displayName;
1679 
1688  public $description;
1689 
1697  public $owner;
1698 
1699 
1715  public function __construct( $role, $display_name = '', $capabilities = array(), $description = '', $owner = '' )
1716  {
1717 
1718  /* Sanitize the role name. */
1719  $role_id = sanitize_title( strtolower( $role ) );
1720 
1721  /* Get Roles */
1722  $wpdk_roles = WPDKRoles::getInstance();
1723  $role_object = $wpdk_roles->get_role( $role_id );
1724 
1725  /* If role not exists then create it. */
1726  if ( is_null( $role_object ) ) {
1727  /* If display name is empty and doesn't exists, then the display name is the role name-id */
1728  if ( empty( $display_name ) ) {
1729  $display_name = ucfirst( $role );
1730  }
1731  $role_object = $wpdk_roles->add_role( $role_id, $display_name, $capabilities, $description, $owner );
1732  $this->displayName = $display_name;
1733  $this->capabilities = $role_object->capabilities;
1734  $this->name = $role_id;
1735 
1736  /* Extends */
1737  $this->description = $description;
1738  $this->owner = $owner;
1739  }
1740  else {
1741  $this->name = $role;
1742  $this->displayName = $wpdk_roles->role_names[$role_id];
1743  $this->capabilities = $role_object->capabilities;
1744 
1745  /* Extends */
1746  $extra = get_option( WPDKRoles::OPTION_KEY );
1747 
1748  if ( !empty( $extra ) && isset( $extra[$role_id] ) ) {
1749  $this->description = $extra[$role][1];
1750  $this->owner = $extra[$role][2];
1751  }
1752  }
1753  }
1754 
1762  public function update()
1763  {
1764  $extra = get_option( WPDKRoles::OPTION_KEY );
1765  if ( !empty( $extra ) ) {
1766  $extra[$this->name] = array(
1767  $this->displayName,
1768  $this->description,
1769  $this->owner
1770  );
1771  }
1772  else {
1773  $extra = WPDKRoles::init()->activeRoles;
1774  }
1775  return update_option( WPDKRoles::OPTION_KEY, $extra );
1776  }
1777 
1778 }
1779 
1780 
1791 class WPDKRoles extends WP_Roles {
1792 
1799  const OPTION_KEY = '_wpdk_roles_extends';
1801  // since 1.4.16 - WordPress has six pre-defined roles:
1802  const SUPER_ADMIN = 'super-admin';
1803  const ADMINISTRATOR = 'administrator';
1804  const EDITOR = 'editor';
1805  const AUTHOR = 'author';
1806  const CONTRIBUTOR = 'contributor';
1807  const SUBSCRIBER = 'subscriber';
1808 
1816  public $activeRoles;
1817 
1825  public $inactiveRoles;
1826 
1834  public $wordPressRoles;
1835 
1843  public $count;
1844 
1852  public $arrayCountUsersByRole;
1853 
1861  public $arrayCapabilitiesByRole;
1862 
1870  private $_extendedData;
1871 
1879  private static $instance = null;
1880 
1889  public static function init()
1890  {
1891  return self::getInstance();
1892  }
1893 
1901  public static function getInstance()
1902  {
1903  if ( is_null( self::$instance ) ) {
1904  self::$instance = new WPDKRoles();
1905  }
1906  return self::$instance;
1907  }
1908 
1916  public static function invalidate()
1917  {
1918  self::$instance = null;
1919  return self::getInstance();
1920  }
1921 
1922 
1933  public function __construct()
1934  {
1935  parent::__construct();
1936 
1937  /* Get the extended data. */
1938  $this->_extendedData = get_option( self::OPTION_KEY );
1939 
1940  if ( !empty( $this->role_names ) ) {
1941  $this->count = count( $this->role_names );
1942  }
1943 
1944  /* Init properties. */
1945  $this->wordPressRoles();
1946  $this->activeRoles();
1947  $this->inactiveRoles();
1948  $this->countUsersByRole();
1949 
1950  /* Create An key value pairs array with key = role and value = list of capabilities. */
1951  $this->arrayCapabilitiesByRole();
1952 
1953  if ( empty( $this->_extendedData ) ) {
1954  $this->_extendedData = array_merge( $this->activeRoles, $this->inactiveRoles, $this->wordPressRoles );
1955  update_option( self::OPTION_KEY, $this->_extendedData );
1956  }
1957 
1958  }
1959 
1960  // -----------------------------------------------------------------------------------------------------------------
1961  // Information
1962  // -----------------------------------------------------------------------------------------------------------------
1963 
1971  public function activeRoles()
1972  {
1973 
1974  /* Calculate only if the property if note set. */
1975  if ( !isset( $this->activeRoles ) ) {
1976 
1977  $this->activeRoles = array();
1978  foreach ( $this->role_names as $role => $name ) {
1979  $count = $this->countUsersByRole( $role );
1980  if ( !empty( $count ) ) {
1981  $this->activeRoles[$role] = isset( $this->_extendedData[$role] ) ? $this->_extendedData[$role] : array( $name, '', '' );
1982  }
1983  }
1984  }
1985  $this->activeRoles = apply_filters( 'wpdk_roles_active', $this->activeRoles );
1986  return $this->activeRoles;
1987  }
1988 
1996  public function inactiveRoles()
1997  {
1998 
1999  /* Calculate only if the property if note set. */
2000  if ( !isset( $this->inactiveRoles ) ) {
2001 
2002  $this->inactiveRoles = array();
2003  foreach ( $this->role_names as $role => $name ) {
2004  $count = $this->countUsersByRole( $role );
2005  if ( empty( $count ) ) {
2006  $this->inactiveRoles[$role] = isset( $this->_extendedData[$role] ) ? $this->_extendedData[$role] : array( $name, '', '' );
2007  }
2008  }
2009  }
2010 
2011  $this->inactiveRoles = apply_filters( 'wpdk_roles_inactive', $this->inactiveRoles );
2012  return $this->inactiveRoles;
2013  }
2014 
2026  public function countUsersByRole( $user_role = '' )
2027  {
2028 
2029  /* If the count is not already set for all roles, let's get it. */
2030  if ( !isset( $this->arrayCountUsersByRole ) ) {
2031 
2032  $this->arrayCountUsersByRole = array();
2033 
2034  /*
2035  * Count users
2036  *
2037  * array(2) {
2038  * ["total_users"]=> int(9)
2039  * ["avail_roles"]=> array(4) {
2040  * ["administrator"]=> int(6)
2041  * ["author"]=> int(1)
2042  * ["contributor"]=> int(1)
2043  * ["subscriber"]=> int(1)
2044  * }
2045  * }
2046  */
2047  $user_count = count_users();
2048 
2049  /* Loop through the user count by role to get a count of the users with each role. */
2050  foreach ( $user_count['avail_roles'] as $role => $count ) {
2051  $this->arrayCountUsersByRole[$role] = $count;
2052  }
2053  }
2054 
2055  /* If the $user_role parameter wasn't passed into this function, return the array of user counts. */
2056  if ( empty( $user_role ) ) {
2058  }
2059 
2060  /* If the role has no users, we need to set it to '0'. */
2061  if ( !isset( $this->arrayCountUsersByRole[$user_role] ) ) {
2062  $this->arrayCountUsersByRole[$user_role] = 0;
2063  }
2064 
2065  /* Return the user count for the given role. */
2066  return $this->arrayCountUsersByRole[$user_role];
2067  }
2068 
2076  public function arrayCapabilitiesByRole()
2077  {
2078 
2079  /* If the count is not already set for all roles, let's get it. */
2080  if ( !isset( $this->arrayCapabilitiesByRole ) ) {
2081 
2082  foreach ( $this->get_names() as $role => $name ) {
2083 
2084  /* Count capabilities for role too. */
2085  $wp_role = $this->get_role( $role );
2086  ksort( $wp_role->capabilities );
2087  $this->arrayCapabilitiesByRole[$role] = $wp_role->capabilities;
2088  }
2089  }
2091  }
2092 
2102  public function roleExists( $role )
2103  {
2104  return ( array_key_exists( $role, $this->role_names ) );
2105  }
2106 
2114  public function wordPressRoles()
2115  {
2116  $this->wordPressRoles = array(
2117  'administrator' => array( 'Administrator', __( 'Somebody who has access to all the administration features', WPDK_TEXTDOMAIN ), 'WordPress' ),
2118  'editor' => array( 'Editor', __( 'Somebody who can publish and manage posts and pages as well as manage other users posts, etc.', WPDK_TEXTDOMAIN ), 'WordPress' ),
2119  'author' => array( 'Author', __( 'Somebody who can publish and manage their own posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2120  'contributor' => array( 'Contributor', __( 'Somebody who can write and manage their posts but not publish them', WPDK_TEXTDOMAIN ), 'WordPress' ),
2121  'subscriber' => array( 'Subscriber', __( 'Somebody who can only manage their profile', WPDK_TEXTDOMAIN ), 'WordPress' ),
2122  );
2123 
2124  $this->wordPressRoles = apply_filters( 'wpdk_roles_defaults', $this->wordPressRoles );
2125  return $this->wordPressRoles;
2126  }
2127 
2128  // -----------------------------------------------------------------------------------------------------------------
2129  // Override
2130  // -----------------------------------------------------------------------------------------------------------------
2131 
2153  public function add_role( $role, $display_name, $capabilities = array(), $description = '', $owner = '' )
2154  {
2155  /* Normalize caps */
2156  $caps = array();
2157  foreach ( $capabilities as $cap ) {
2158  $caps[$cap] = true;
2159  }
2160 
2161  $role_object = parent::add_role( $role, $display_name, $caps );
2162  if ( !is_null( $role_object ) ) {
2163  if ( !isset( $this->_extendedData[$role] ) ) {
2164  $this->_extendedData[$role] = array(
2165  $display_name,
2166  $description,
2167  $owner
2168  );
2169  }
2170  update_option( self::OPTION_KEY, $this->_extendedData );
2171  }
2172  return $role_object;
2173  }
2174 
2182  public function remove_role( $role )
2183  {
2184  parent::remove_role( $role );
2185  unset( $this->_extendedData[$role] );
2186  update_option( self::OPTION_KEY, $this->_extendedData );
2187  }
2188 
2189 
2190  // -----------------------------------------------------------------------------------------------------------------
2191  // UI
2192  // -----------------------------------------------------------------------------------------------------------------
2193 
2201  public function selectCapabilitiesWithRole( $role )
2202  {
2203 
2204  if ( is_object( $role ) && is_a( $role, 'WPDKRole' ) ) {
2205  $role = $role->name;
2206  }
2207 
2208  ob_start(); ?>
2209 
2210  <select>
2211  <?php foreach ( $this->arrayCapabilitiesByRole[$role] as $cap => $enabled ): ?>
2212  <option><?php echo $cap ?></option>
2213  <?php endforeach ?>
2214  </select>
2215 
2216  <?php
2217  $content = ob_get_contents();
2218  ob_end_clean();
2219  return $content;
2220  }
2221 
2222 }
2223 
2224 
2242 class WPDKCapabilities {
2243 
2250  const OPTION_KEY = '_wpdk_capabilities_extends';
2251 
2259  public $roleCapabilities;
2260 
2268  public $defaultCapabilities;
2269 
2277  public $capabilities;
2278 
2286  public $userCapabilities;
2287 
2295  public $allCapabilities;
2296 
2297 
2305  private $_extendedData;
2306 
2314  public static function getInstance()
2315  {
2316  static $instance = null;
2317  if ( is_null( $instance ) ) {
2318  $instance = new WPDKCapabilities();
2319  }
2320  return $instance;
2321  }
2322 
2330  private function __construct()
2331  {
2332 
2333  /* Get the extended data. */
2334  $this->_extendedData = get_option( self::OPTION_KEY );
2335 
2336  /* Preset WordPress capabilities list. */
2337  $this->defaultCapabilities = array_keys( $this->defaultCapabilities() );
2338 
2339  /* Get the role capabilities. */
2340  $this->roleCapabilities = array_keys( $this->roleCapabilities() );
2341 
2342  /* Get the role users. */
2343  $this->userCapabilities = array_keys( $this->userCapabilities() );
2344 
2345  /* Setup only plugin capabilities. */
2346  $this->capabilities = array_unique( array_merge( array_diff( $this->roleCapabilities, $this->defaultCapabilities ), $this->userCapabilities ) );
2347  sort( $this->capabilities );
2348 
2349  /* All caps */
2350  $this->allCapabilities = array_unique( array_merge( $this->userCapabilities, $this->roleCapabilities, $this->defaultCapabilities ) );
2351  sort( $this->allCapabilities );
2352  }
2353 
2363  public function get_cap( $cap_id )
2364  {
2365  $cap = false;
2366  if ( isset( $this->allCapabilities[$cap_id] ) ) {
2367  $description = '';
2368  $owner = '';
2369  if ( isset( $this->_extendedData[$cap_id] ) ) {
2370  list( $cap_id, $description, $owner ) = $this->_extendedData[$cap_id];
2371  }
2372  $cap = new WPDKCapability( $cap_id, $description, $owner );
2373  }
2374  return $cap;
2375  }
2376 
2377  // -----------------------------------------------------------------------------------------------------------------
2378  // Capabilities list
2379  // -----------------------------------------------------------------------------------------------------------------
2380 
2397  public static function defaultCapabilities()
2398  {
2399 
2400  /* Create an array of all the default WordPress capabilities so the user doesn't accidentally get rid of them. */
2401  $defaults = array(
2402  'activate_plugins' => array( 'activate_plugins', __( 'Allows access to Administration Panel options: Plugins', WPDK_TEXTDOMAIN ), 'WordPress' ),
2403  'add_users' => array( 'add_users', __( 'add_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2404  'create_users' => array( 'create_users', __( 'create_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2405  'delete_others_pages' => array( 'delete_others_pages', __( 'delete_others_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2406  'delete_others_posts' => array( 'delete_others_posts', __( 'delete_others_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2407  'delete_pages' => array( 'delete_pages', __( 'delete_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2408  'delete_plugins' => array( 'delete_plugins', __( 'delete_plugins', WPDK_TEXTDOMAIN ), 'WordPress' ),
2409  'delete_posts' => array( 'delete_posts', __( 'delete_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2410  'delete_private_pages' => array( 'delete_private_pages', __( 'delete_private_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2411  'delete_private_posts' => array( 'delete_private_posts', __( 'delete_private_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2412  'delete_published_pages' => array( 'delete_published_pages', __( 'delete_published_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2413  'delete_published_posts' => array( 'delete_published_posts', __( 'delete_published_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2414  'delete_users' => array( 'delete_users', __( 'delete_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2415  'edit_dashboard' => array( 'edit_dashboard', __( 'edit_dashboard', WPDK_TEXTDOMAIN ), 'WordPress' ),
2416  'edit_files' => array( 'edit_files', __( 'No longer used.', WPDK_TEXTDOMAIN ), 'WordPress' ),
2417  'edit_others_pages' => array( 'edit_others_pages', __( 'edit_others_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2418  'edit_others_posts' => array( 'edit_others_posts', __( 'edit_others_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2419  'edit_pages' => array( 'edit_pages', __( 'edit_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2420  'edit_plugins' => array( 'edit_plugins', __( 'edit_plugins', WPDK_TEXTDOMAIN ), 'WordPress' ),
2421  'edit_posts' => array( 'edit_posts', __( 'edit_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2422  'edit_private_pages' => array( 'edit_private_pages', __( 'edit_private_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2423  'edit_private_posts' => array( 'edit_private_posts', __( 'edit_private_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2424  'edit_published_pages' => array( 'edit_published_pages', __( 'edit_published_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2425  'edit_published_posts' => array( 'edit_published_posts', __( 'edit_published_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2426  'edit_theme_options' => array( 'edit_theme_options', __( 'Allows access to Administration Panel options: Appearance > Widgets, Appearance > Menus, Appearance > Theme Options if they are supported by the current theme, Appearance > Background, Appearance > Header ', WPDK_TEXTDOMAIN ), 'WordPress' ),
2427  'edit_themes' => array( 'edit_themes', __( 'edit_themes', WPDK_TEXTDOMAIN ), 'WordPress' ),
2428  'edit_users' => array( 'edit_users', __( 'Allows access to Administration Panel options: Users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2429  'import' => array( 'import', __( 'import', WPDK_TEXTDOMAIN ), 'WordPress' ),
2430  'install_plugins' => array( 'install_plugins', __( 'Allows access to Administration Panel options: Plugins > Add New ', WPDK_TEXTDOMAIN ), 'WordPress' ),
2431  'install_themes' => array( 'install_themes', __( 'Allows access to Administration Panel options: Appearance > Add New Themes', WPDK_TEXTDOMAIN ), 'WordPress' ),
2432  'list_users' => array( 'list_users', __( 'list_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2433  'manage_categories' => array( 'manage_categories', __( 'Allows access to Administration Panel options: Posts > Categories, Links > Categories', WPDK_TEXTDOMAIN ), 'WordPress' ),
2434  'manage_links' => array( 'manage_links', __( 'Allows access to Administration Panel options: Links Links > Add New', WPDK_TEXTDOMAIN ), 'WordPress' ),
2435  'manage_options' => array( 'manage_options', __( 'Allows access to Administration Panel options: Settings > General, Settings > Writing, Settings > Reading, Settings > Discussion, Settings > Permalinks, Settings > Miscellaneous', WPDK_TEXTDOMAIN ), 'WordPress' ),
2436  'moderate_comments' => array( 'moderate_comments', __( 'Allows users to moderate comments from the Comments SubPanel (although a user needs the edit_posts Capability in order to access this)', WPDK_TEXTDOMAIN ), 'WordPress' ),
2437  'promote_users' => array( 'promote_users', __( 'promote_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2438  'publish_pages' => array( 'publish_pages', __( 'publish_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2439  'publish_posts' => array( 'publish_posts', __( 'See and use the "publish" button when editing their post (otherwise they can only save drafts). Can use XML-RPC to publish (otherwise they get a "Sorry, you can not post on this weblog or category.")', WPDK_TEXTDOMAIN ), 'WordPress' ),
2440  'read' => array( 'read', __( 'Allows access to Administration Panel options: Dashboard, Users > Your Profile. Used nowhere in the core code except the menu.php', WPDK_TEXTDOMAIN ), 'WordPress' ),
2441  'read_private_pages' => array( 'read_private_pages', __( 'read_private_pages', WPDK_TEXTDOMAIN ), 'WordPress' ),
2442  'read_private_posts' => array( 'read_private_posts', __( 'read_private_posts', WPDK_TEXTDOMAIN ), 'WordPress' ),
2443  'remove_users' => array( 'remove_users', __( 'remove_users', WPDK_TEXTDOMAIN ), 'WordPress' ),
2444  'switch_themes' => array( 'switch_themes', __( 'Allows access to Administration Panel options: Appearance, Appearance > Themes ', WPDK_TEXTDOMAIN ), 'WordPress' ),
2445  'unfiltered_html' => array( 'unfiltered_html', __( 'Allows user to post HTML markup or even JavaScript code in pages, posts, and comments. Note: Enabling this option for untrusted users may result in their posting malicious or poorly formatted code. ', WPDK_TEXTDOMAIN ), 'WordPress' ),
2446  'unfiltered_upload' => array( 'unfiltered_upload', __( 'unfiltered_upload', WPDK_TEXTDOMAIN ), 'WordPress' ),
2447  'update_core' => array( 'update_core', __( 'update_core', WPDK_TEXTDOMAIN ), 'WordPress' ),
2448  'update_plugins' => array( 'update_plugins', __( 'update_plugins', WPDK_TEXTDOMAIN ), 'WordPress' ),
2449  'update_themes' => array( 'update_themes', __( 'update_themes', WPDK_TEXTDOMAIN ), 'WordPress' ),
2450  'upload_files' => array( 'upload_files', __( 'Allows access to Administration Panel options: Media, Media > Add New ', WPDK_TEXTDOMAIN ), 'WordPress' ),
2451  );
2452 
2453  /* Return the array of default capabilities. */
2454  return apply_filters( 'wpdk_capabilities_defaults', $defaults );
2455  }
2456 
2467  private static function oldLevels()
2468  {
2469  $old_levels = array(
2470  'level_0' => array( 'level_0', '', 'WordPress' ),
2471  'level_1' => array( 'level_1', '', 'WordPress' ),
2472  'level_2' => array( 'level_2', '', 'WordPress' ),
2473  'level_3' => array( 'level_3', '', 'WordPress' ),
2474  'level_4' => array( 'level_4', '', 'WordPress' ),
2475  'level_5' => array( 'level_5', '', 'WordPress' ),
2476  'level_6' => array( 'level_6', '', 'WordPress' ),
2477  'level_7' => array( 'level_7', '', 'WordPress' ),
2478  'level_8' => array( 'level_8', '', 'WordPress' ),
2479  'level_9' => array( 'level_9', '', 'WordPress' ),
2480  'level_10' => array( 'level_10', '', 'WordPress' )
2481  );
2482  return apply_filters( 'wpdk_capabilities_old_levels', $old_levels );
2483  }
2484 
2485 
2500  public function roleCapabilities()
2501  {
2502 
2503  /* Get WPDKRoles */
2504  $wpdk_roles = WPDKRoles::getInstance();
2505 
2506  /* Set up an empty capabilities array. */
2507  $capabilities = array();
2508 
2509  /* Loop through each role object because we need to get the caps. */
2510  foreach ( $wpdk_roles->role_objects as $key => $role ) {
2511 
2512  /* Roles without capabilities will cause an error, so we need to check if $role->capabilities is an array. */
2513  if ( is_array( $role->capabilities ) ) {
2514 
2515  /* Loop through the role's capabilities and add them to the $capabilities array. */
2516  $exclude = self::oldLevels();
2517  foreach ( $role->capabilities as $cap => $grant ) {
2518  if ( !isset( $exclude[$cap] ) ) {
2519  $capabilities[$cap] = isset( $this->_extendedData[$cap] ) ? $this->_extendedData[$cap] : array(
2520  $cap,
2521  '',
2522  ''
2523  );
2524  }
2525  }
2526  }
2527  }
2528 
2529  /* Sort the capabilities by name so they're easier to read when shown on the screen. */
2530  ksort( $capabilities );
2531 
2532  /* Return the capabilities array */
2533  return $capabilities;
2534  }
2535 
2546  public function userCapabilities()
2547  {
2548  global $wpdb;
2549 
2550  //$capabilities = get_transient( '_wpdk_users_caps' );
2551  $capabilities = ''; // cache off for debug
2552  if ( empty( $capabilities ) ) {
2553  $sql = "SELECT user_id, meta_value FROM `{$wpdb->usermeta}` WHERE meta_key = 'wp_capabilities'";
2554  $result = $wpdb->get_results( $sql, ARRAY_A );
2555 
2556  foreach ( $result as $user_cap ) {
2557  /* A cap is store with a bolean flah that here is ignored. */
2558  $temp = array_keys( unserialize( $user_cap['meta_value'] ) );
2559  foreach ( $temp as $key ) {
2560  $capabilities[$key] = isset( $this->_extendedData[$key] ) ? $this->_extendedData[$key] : array(
2561  $key,
2562  '',
2563  ''
2564  );
2565  }
2566  }
2567  //set_transient( '_wpdk_users_caps', $capabilities, 120 );
2568  }
2569 
2570  /* Sort the capabilities by name so they're easier to read when shown on the screen. */
2571  ksort( $capabilities );
2572 
2573  return $capabilities;
2574 
2575  }
2576 
2586  public function allCapabilities()
2587  {
2588  $capabilities = $this->userCapabilities();
2589  $capabilities = array_merge( $capabilities, $this->roleCapabilities() );
2590  $capabilities = array_merge( $capabilities, $this->defaultCapabilities() );
2591 
2592  /* Sort the capabilities by name so they're easier to read when shown on the screen. */
2593  ksort( $capabilities );
2594 
2595  return $capabilities;
2596  }
2597 
2598 
2599  // -------------------------------------------------------------------------------------------------------------------
2600  // Extra
2601  // -------------------------------------------------------------------------------------------------------------------
2602 
2614  public function usersCapability()
2615  {
2616  global $wpdb;
2617 
2618  //$user_caps = get_transient( '_wpdk_users_caps' );
2619  $user_caps = false; // cache off for debug
2620  if ( empty( $user_caps ) ) {
2621  $sql = "SELECT user_id, meta_value FROM `{$wpdb->usermeta}` WHERE meta_key = 'wp_capabilities'";
2622  $result = $wpdb->get_results( $sql, ARRAY_A );
2623 
2624  foreach ( $result as $user_cap ) {
2625  $user_caps[$user_cap['user_id']] = get_userdata( $user_cap['user_id'] )->allcaps;
2626  }
2627 
2628  //set_transient( '_wpdk_users_caps', $user_caps, 120 );
2629  }
2630  return $user_caps;
2631 
2632  }
2633 
2634 }
2635 
2636 
2647 class WPDKCapability {
2648 
2656  public $id;
2657 
2665  public $description;
2666 
2674  public $owner;
2675 
2687  public function __construct( $id, $description = '', $owner = '' )
2688  {
2689  $this->id = $id;
2690  $this->description = $description;
2691  $this->owner = $owner;
2692 
2693  }
2694 
2700  public function update()
2701  {
2702  $extra = get_option( WPDKCapabilities::OPTION_KEY );
2703  if ( !empty( $extra ) ) {
2704  $extra[$this->id] = array(
2705  $this->id,
2706  $this->description,
2707  $this->owner
2708  );
2709  }
2710  else {
2711  $extra = WPDKCapabilities::getInstance()->capabilities;
2712  }
2713  return update_option( WPDKCapabilities::OPTION_KEY, $extra );
2714  }
2715 
2716 }