WJ_Field

A custom field.

Defined (1)

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

/classes/WJ_Field.php  
  1. class WJ_Field { 
  2.  
  3. // The custom fields DB table. 
  4. private $table; 
  5. // The Users table. 
  6. private $user_table; 
  7. // The ID of the custom field. 
  8. public $id; 
  9. // The name of the custom field. 
  10. public $name; 
  11. // input, textarea, checkbox, radio, select 
  12. public $type; 
  13. // true | false 
  14. public $required; 
  15. // Settings. 
  16. public $settings = array(); 
  17.  
  18. static $_table = 'custom_field'; 
  19. static $_user_table = 'user'; 
  20.  
  21. static $defaults = array( 
  22. 'date' => array( 
  23. 'date_type' => 'year_month_day',  
  24. 'is_default_today' => 0,  
  25. 'date_order' => 'mm/dd/yyyy',  
  26. ),  
  27. 'radio' => array( 
  28. 'values' => array(),  
  29. ),  
  30. 'checkbox' => array( 
  31. 'values' => array(),  
  32. ),  
  33. 'select' => array( 
  34. 'values' => array(),  
  35. ); 
  36.  
  37. /** 
  38. Just set the correct tables on creation. 
  39. $custom_field = new WJ_Field(); 
  40. */ 
  41. function __construct( $args = null ) { 
  42. $this->table = WJ_Settings::db_prefix( self::$_table ); 
  43. $this->user_table = WJ_Settings::db_prefix( self::$_user_table ); 
  44.  
  45. if ( ! is_null( $args ) ) { 
  46. $this->set( $args ); 
  47.  
  48. /** 
  49. Get the table name. 
  50. Useful for statistics and counts on Custom Fields. 
  51. Generally when we want to run DB queries. 
  52. */ 
  53. public function get_table_name() { 
  54. return $this->table; 
  55.  
  56. /** 
  57. Static method to get a Custom field by id. 
  58. WJ_Field::get(id); 
  59. # => WJCustomField Object 
  60. */ 
  61. public static function get( $id ) { 
  62. global $wpdb; 
  63. $result = $wpdb->get_row( $wpdb->prepare( 'SELECT * from ' . WJ_Settings::db_prefix( self::$_table ) . ' WHERE id = %d', $id ), ARRAY_A ); 
  64. if ( $result != null ) { 
  65. return new self( $result ); 
  66. } else { 
  67. return null; 
  68.  
  69. /** 
  70. Get all custom fields. 
  71. WJ_Field::get_all(); 
  72. # => Array of WJ_Field 
  73. */ 
  74. public static function get_all( $options = array() ) { 
  75. global $wpdb; 
  76.  
  77. // default order by 
  78. $order_by = 'id ASC'; 
  79. if ( isset( $options['order_by'] ) ) { 
  80. $order_by = $options['order_by']; 
  81.  
  82. // fetch rows from db 
  83. $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * from ' . WJ_Settings::db_prefix( self::$_table ) . ' ORDER BY %s', $order_by ), ARRAY_A ); 
  84.  
  85. if ( ! is_null( $results ) ) { 
  86. $collection = array(); 
  87. foreach ( $results as $result ) { 
  88. $collection[] = new self( $result ); 
  89. return $collection; 
  90. } else { 
  91. return null; 
  92.  
  93.  
  94. /** 
  95. Get a custom fields names list. 
  96. WJ_Field::get_names_list(); 
  97. # => array(1 => 'Address', 2 => 'Gender') 
  98. */ 
  99. public static function get_all_names() { 
  100. $fields = self::get_all(); 
  101. $fields_list = array(); 
  102. if ( isset( $fields ) ) { 
  103. foreach ( $fields as $field ) { 
  104. $fields_list[ $field->id ] = $field->name; 
  105. return $fields_list; 
  106.  
  107. /** 
  108. Set all object properties. 
  109. $custom_field->set(array( 
  110. 'name' => 'First Name',  
  111. 'type' => 'text',  
  112. 'required' => true 
  113. )); 
  114. */ 
  115. public function set( $args ) { 
  116. if ( isset( $args['id'] ) ) { 
  117. $this->id = $args['id']; 
  118. $this->name = $args['name']; 
  119. $this->type = $args['type']; 
  120. $this->required = WJ_Utils::to_bool( $args['required'] ); 
  121. $this->settings = maybe_unserialize( $args['settings'] ); 
  122.  
  123. if ( ! is_array( $this->settings ) ) { 
  124. $this->settings = array(); 
  125.  
  126. if ( isset( self::$defaults[ $this->type ] ) && is_array( self::$defaults[ $this->type ] ) ) { 
  127. $this->settings = wp_parse_args( (array) $this->settings, (array) self::$defaults[ $this->type ] ); 
  128.  
  129. /** 
  130. Store Custom Field in DB. 
  131. If already stored, updates it. 
  132. $custom_field->save(); 
  133. */ 
  134. public function save() { 
  135. // Check if it's a new object or an update. 
  136. if ( isset( $this->id ) ) { 
  137. $this->update(); 
  138. } else { 
  139. $this->create(); 
  140.  
  141. // Delete custom field from DB. 
  142. public function delete() { 
  143. global $wpdb; 
  144. $result = $wpdb->delete( $this->table, array( 'id' => $this->id ), array( '%d' ) ); 
  145. $this->delete_user_col( $this->id ); 
  146. return $result; 
  147.  
  148. /** 
  149. Generates user column name; 
  150. $custom_field->user_column_name(); 
  151. # => 'cf_1' 
  152. */ 
  153. public function user_column_name() { 
  154. $column_name = 'cf_' . $this->id; 
  155. return $column_name; 
  156.  
  157. /** 
  158. Creates the Custom Field. 
  159. It also creates the user column, depending on type. 
  160. */ 
  161. private function create() { 
  162. global $wpdb; 
  163. $required = WJ_Utils::to_int( $this->required ); 
  164. $wpdb->insert( 
  165. $this->table,  
  166. array( 
  167. 'name' => $this->name,  
  168. 'type' => $this->type,  
  169. 'required' => $required,  
  170. 'settings' => serialize( $this->settings ),  
  171. ),  
  172. array( '%s', '%s', '%d', '%s', '%s' ) 
  173. ); 
  174. // ! No id in user col? 
  175. if ( $wpdb->insert_id ) { 
  176. $this->id = $wpdb->insert_id; 
  177. $this->create_user_col(); 
  178. } else { 
  179. return false; 
  180.  
  181. /** 
  182. Updates the value of the custom field. 
  183. $custom_field->update('New address'); 
  184. */ 
  185. private function update() { 
  186. global $wpdb; 
  187. $required = WJ_Utils::to_int( $this->required ); 
  188. $result = $wpdb->update( 
  189. $this->table,  
  190. array( 
  191. 'name' => $this->name,  
  192. 'type' => $this->type,  
  193. 'required' => $required,  
  194. 'settings' => maybe_serialize( $this->settings ),  
  195. ),  
  196. array( 'id' => $this->id ),  
  197. array( '%s', '%s', '%d' ),  
  198. array( '%d' ) 
  199. ); 
  200. return $result; 
  201.  
  202. /** 
  203. Creates the correct user columnn, named cf_x, in the 
  204. user table. x is the ID if the custom field. 
  205. */ 
  206. private function create_user_col() { 
  207. global $wpdb; 
  208. $column_name = $this->user_column_name(); 
  209. $column_type = $this->generate_column_type(); 
  210. $result = $wpdb->query( 
  211. "ALTER TABLE $this->user_table ADD COLUMN $column_name $column_type" 
  212. ); 
  213. return $result; 
  214.  
  215. /** 
  216. Calculates the correct column type, based on custom field type. 
  217. $custom_field->generate_column_type(); 
  218. # => 'VARCHAR(100)' 
  219. */ 
  220. private function generate_column_type() { 
  221. switch ( $this->type ) { 
  222. case 'input': 
  223. $column_type = 'VARCHAR(100)'; 
  224. break; 
  225. case 'textarea': 
  226. $column_type = 'VARCHAR(255)'; 
  227. break; 
  228. case 'checkbox': 
  229. $column_type = 'TINYINT(1)'; 
  230. break; 
  231. case 'radio': 
  232. $column_type = 'VARCHAR(255)'; 
  233. break; 
  234. case 'select': 
  235. $column_type = 'VARCHAR(255)'; 
  236. break; 
  237. case 'date': 
  238. $column_type = 'INT(20)'; 
  239. break; 
  240. default: 
  241. $column_type = 'VARCHAR(255)'; 
  242. break; 
  243. return $column_type; 
  244.  
  245. /** 
  246. Deletes the user column in the user table. 
  247. Needed when we remove a custom field. 
  248. */ 
  249. private function delete_user_col( $custom_field_id ) { 
  250. global $wpdb; 
  251. $cf_column = 'cf_' . $custom_field_id; 
  252. $result = $wpdb->query( "ALTER TABLE $this->user_table DROP COLUMN $cf_column" ); 
  253. return $result; 
  254.