WJ_FieldUser

An user field is a custom field identified by ID, linked to a particular user, identified by ID.

Defined (1)

The class is defined in the following location(s).

/classes/WJ_FieldUser.php  
  1. class WJ_FieldUser { 
  2.  
  3. // User Table. 
  4. private $user_table; 
  5. // Custom fields table. 
  6. private $field_table; 
  7. // User ID. 
  8. public $user_id; 
  9. // The field object. 
  10. public $field; 
  11. // The field value. 
  12. public $value; 
  13.  
  14. /** 
  15. $field_user = new WJ_FieldUser(); 
  16. Just setup correct tables names. 
  17. */ 
  18. function __construct() { 
  19. $this->user_table = WJ_Settings::db_prefix('user'); 
  20. $this->field_table = WJ_Settings::db_prefix('custom_field'); 
  21.  
  22. /** 
  23. After object creation, you can set the user id,  
  24. and the field id. The object will now contain the right 
  25. custom field, and the user id. 
  26. $field_user->set(array( 
  27. 'user_id' => 1,  
  28. 'field_id' => 2 
  29. )); 
  30. */ 
  31. public function set($args) { 
  32. $this->user_id = $args['user_id']; 
  33. $this->field = WJ_Field::get($args['field_id']); 
  34.  
  35. /** 
  36. Updates the field user value. 
  37. $field_user->update('Main Street'); 
  38. */ 
  39. public function update($value) { 
  40. global $wpdb; 
  41. $column_name = $this->field->user_column_name(); 
  42. // Cast value to the correct column type. 
  43. switch ($this->field->type) { 
  44. case 'checkbox': 
  45. $validation = '%d'; 
  46. $value = (int)$value; 
  47. break; 
  48. default: 
  49. // We default to a string. 
  50. $validation = '%s'; 
  51. $value = (string)$value; 
  52. break; 
  53. $result = $wpdb->update( 
  54. $this->user_table,  
  55. array( 
  56. $column_name => $value 
  57. ), array("user_id" => $this->user_id),  
  58. array($validation),  
  59. array("%d") 
  60. ); 
  61. if ($result != false) { 
  62. $this->value = $value; 
  63. return $result; 
  64.  
  65. /** 
  66. Get the user field value. 
  67. $field_user->value(); 
  68. # => 'Main Street' 
  69. */ 
  70. public function value() { 
  71. $value = ''; 
  72. if (isset($this->value)) { 
  73. $value = $this->value; 
  74. } else { 
  75. $column_name = $this->field->user_column_name(); 
  76. global $wpdb; 
  77. $result = $wpdb->get_row($wpdb->prepare( 
  78. "SELECT $column_name FROM $this->user_table 
  79. WHERE user_id = %d",  
  80. array($this->user_id) 
  81. ), ARRAY_A); 
  82. $this->value = $result[$column_name]; 
  83. $value = $result[$column_name]; 
  84. return $value; 
  85.  
  86. /** 
  87. Get the user unique column name. 
  88. $field_user->column_name(); 
  89. # => 'cf_1' 
  90. */ 
  91. public function column_name() { 
  92. return $this->field->user_column_name(); 
  93.  
  94. /** 
  95. Get all UserFields by User ID. 
  96. $WJ_FieldUser::get_all(); 
  97. # => Array of WJ_FieldUser 
  98. */ 
  99. public static function get_all($user_id) { 
  100. $fields = WJ_Field::get_all(); 
  101. if(isset($fields) && !empty($fields)) { 
  102. $collection = array(); 
  103. foreach ($fields as $field) { 
  104. $user_field = new self(); 
  105. $user_field->user_id = $user_id; 
  106. $user_field->field = $field; 
  107. $collection[] = $user_field; 
  108. return $collection; 
  109. } else { 
  110. return null; 
  111.