Caldera_Forms_DB_Base

Base class for database interactions via custom table.

Defined (1)

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

/classes/db/base.php  
  1. abstract class Caldera_Forms_DB_Base { 
  2.  
  3. /** 
  4. * Primary fields 
  5. * @since 1.3.5 
  6. * @var array 
  7. */ 
  8. protected $primary_fields = array(); 
  9.  
  10. /** 
  11. * Meta fields 
  12. * @since 1.3.5 
  13. * @var array 
  14. */ 
  15. protected $meta_fields = array(); 
  16.  
  17. /** 
  18. * Meta keys 
  19. * @since 1.3.5 
  20. * @var array 
  21. */ 
  22. protected $meta_keys = array(); 
  23.  
  24. /** 
  25. * Name of primary index 
  26. * @since 1.3.5 
  27. * @var string 
  28. */ 
  29. protected $index; 
  30.  
  31. /** 
  32. * Name of table 
  33. * NOTE: Don't call this, ever. Use $this->get_table_name() so prefix and possible suffix are extended. 
  34. * @since 1.3.5 
  35. * @var string 
  36. */ 
  37. protected $table_name; 
  38.  
  39. /** 
  40. * Constructor -- protected to force singleton upon subclasses. 
  41. */ 
  42. protected function __construct() {} 
  43.  
  44. /** 
  45. * Get name of table with prefix 
  46. * @since 1.3.5 
  47. * @param bool|false $meta Whether primary or meta table name is desired. Default is false, which returns primary table 
  48. * @return string 
  49. */ 
  50. public function get_table_name( $meta = false ) { 
  51. global $wpdb; 
  52.  
  53. $table_name = $wpdb->prefix . $this->table_name; 
  54. if( $meta ) { 
  55. $table_name .= '_meta'; 
  56.  
  57. return $table_name; 
  58.  
  59. /** 
  60. * Get names of fields for this collection 
  61. * @since 1.3.5 
  62. * @return array 
  63. */ 
  64. public function get_fields() { 
  65.  
  66. $fields[ 'primary' ] = array_keys( $this->primary_fields ); 
  67.  
  68. $fields[ 'meta_keys' ] = array_keys( $this->meta_keys ); 
  69.  
  70. /** 
  71. * Filter the allowed meta keys that can be saved 
  72. * @since 1.4.0 
  73. * @param array $fields Allowed fields 
  74. * @param string $table_name Name of table 
  75. */ 
  76. $fields[ 'meta_fields' ] = apply_filters( 'caldera_forms_db_meta_fields', array_keys( $this->meta_fields ), $this->get_table_name( true ) ); 
  77. return $fields; 
  78.  
  79. /** 
  80. * Create a new entry with meta 
  81. * @since 1.3.5 
  82. * @param array $data Data to save 
  83. * @return bool|int|null 
  84. */ 
  85. public function create( array $data ) { 
  86.  
  87. $_data = $_meta = array( 
  88. 'fields' => array(),  
  89. 'formats' => array() 
  90. ); 
  91.  
  92. foreach( $data as $field => $datum ) { 
  93. if( is_null( $datum ) || is_array( $datum ) || is_object( $datum ) ) { 
  94. $datum = ''; 
  95.  
  96. if( $this->valid_field( $field, 'primary' ) ) { 
  97. $_data[ 'fields' ][ $field ] = call_user_func( $this->primary_fields[ $field ][1], $datum ); 
  98. $_data[ 'formats' ][] = $this->primary_fields[ $field ][0]; 
  99.  
  100. if( $this->valid_field( $field, 'meta_key' ) ) { 
  101. $_meta[ 'fields' ][ $field ][ 'value' ] = call_user_func( $this->meta_keys[ $field ][1], $datum ); 
  102. $_meta[ 'fields' ][ $field ][ 'format' ] = $this->meta_keys[ $field ][0]; 
  103.  
  104.  
  105. $id = $this->save( $_data ); 
  106. if( is_numeric( $id ) && ! empty( $_meta[ 'fields' ] ) ) { 
  107.  
  108. foreach( $_meta as $meta ) { 
  109.  
  110. foreach( $this->meta_keys as $meta_key => $field ) { 
  111. $_meta_row = array(); 
  112. if( isset( $meta[ $meta_key ], $meta[ $meta_key ][ 'value' ] ) ) { 
  113.  
  114. $_meta_row[ 'fields' ][ 'meta_key' ] = $meta_key; 
  115. $_meta_row[ 'formats' ][] = '%s'; 
  116.  
  117. $_meta_row[ 'fields' ][ 'meta_value' ] = $_meta[ 'fields' ][ $meta_key ][ 'value' ]; 
  118. $_meta_row[ 'formats' ][] = $this->meta_keys[ $meta_key ][0]; 
  119.  
  120.  
  121. $_meta_row[ 'fields' ][ $this->index ] = $id; 
  122. $_meta_row[ 'formats' ][] = '%d'; 
  123.  
  124. $this->save( $_meta_row, true ); 
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. return $id; 
  132.  
  133.  
  134. /** 
  135. * Save a row 
  136. * @since 1.3.5 
  137. * @param array $data Row data to save 
  138. * @param bool|false $meta 
  139. * @return bool|int|null 
  140. */ 
  141. protected function save( array $data, $meta = false ) { 
  142.  
  143. if( ! empty( $data ) ) { 
  144. global $wpdb; 
  145. $inserted = $wpdb->insert( 
  146. $this->get_table_name( $meta ),  
  147. $data[ 'fields' ],  
  148. $data[ 'formats' ] 
  149. ); 
  150. if( $inserted ) { 
  151. return $wpdb->insert_id; 
  152. }else{ 
  153. return false; 
  154.  
  155. }else{ 
  156. return null; 
  157.  
  158.  
  159. /** 
  160. * Delete an entry from DB 
  161. * @since 1.3.5 
  162. * @param int $id ID of entry 
  163. * @return bool 
  164. */ 
  165. public function delete( $id ) { 
  166. global $wpdb; 
  167. $deleted = $wpdb->delete( $this->get_table_name(), array( 'ID' => $id ) ); 
  168. if( false != $deleted ) { 
  169. return true; 
  170.  
  171.  
  172.  
  173. /** 
  174. * Get a complete record or recrods -- primary or meta fields 
  175. * @since 1.3.5 
  176. * @param int|array $id ID of entry, or an array of IDs. 
  177. * @return array|null 
  178. */ 
  179. public function get_record( $id ) { 
  180. $primary = $this->get_primary( $id ); 
  181. if( is_array( $primary ) ) { 
  182. if( is_array( $id ) ) { 
  183. $data = array(); 
  184. foreach( $primary as $record ) { 
  185. $meta = $this->get_meta( $record[ 'ID' ] ); 
  186. $record = $this->add_meta_to_record( $meta, $record ); 
  187. $data[] = $record; 
  188.  
  189. return $data; 
  190.  
  191. }else{ 
  192. $meta = $this->get_meta( $id ); 
  193. $data = $primary; 
  194. if( is_array( $meta ) ) { 
  195. return $this->add_meta_to_record( $meta, $data ); 
  196.  
  197. }else{ 
  198. return $primary; 
  199.  
  200.  
  201.  
  202.  
  203. /** 
  204. * Get primary entry row from DB 
  205. * @since 1.3.5 
  206. * @param int|array $id ID of entry, or an array of IDs. 
  207. * @return array|null 
  208. */ 
  209. public function get_primary( $id ) { 
  210. global $wpdb; 
  211. $table_name = $this->get_table_name(); 
  212. if( is_array( $id ) ) { 
  213. $single = false; 
  214. $sql = "SELECT * FROM $table_name WHERE `ID` IN(" . $this->escape_array( $id ) . ")"; 
  215. }else{ 
  216. $single = true; 
  217. $sql = $wpdb->prepare( "SELECT * FROM $table_name WHERE `ID` = %d", absint( $id ) ); 
  218.  
  219.  
  220. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  221. if( ! empty( $results ) && $single ) { 
  222. $results = $results[0]; 
  223. return $results; 
  224.  
  225. /** 
  226. * Get meta rows from DB 
  227. * @since 1.3.5 
  228. * @param int|array $id ID of entry, or an array of IDs. 
  229. * @param string|bool $key Optional. If false, the default all the metas are returned. Use name of key to get one specific key. 
  230. * @return array|null|object 
  231. */ 
  232. public function get_meta( $id, $key = false ) { 
  233. global $wpdb; 
  234. $table_name = $this->get_table_name( true ); 
  235. if( is_array( $id ) ) { 
  236. $sql = "SELECT * FROM $table_name WHERE`$this->index` IN(" . $this->escape_array( $id ) . ")"; 
  237. }else{ 
  238. $sql = $wpdb->prepare( "SELECT * FROM $table_name WHERE `$this->index` = %d", absint( $id ) ); 
  239.  
  240. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  241.  
  242. if( ! empty( $results ) && is_string( $key ) ) { 
  243. return $this->reduce_meta( $results, $key ); 
  244.  
  245.  
  246. return $results; 
  247.  
  248. /** 
  249. * Reduce a meta row to the meta value of a key 
  250. * @since 1.3.5 
  251. * @param array $results Meta row 
  252. * @param string $key Name of meta key 
  253. * @return array|void 
  254. */ 
  255. protected function reduce_meta( array $results, $key ) { 
  256. if( $this->valid_field( $key, 'meta_key' ) ) { 
  257. $values = array_combine( wp_list_pluck( $results, 'meta_key' ), wp_list_pluck( $results, 'meta_value' ) ); 
  258. if( isset( $values[ $key ] ) ) { 
  259. return $values[ $key ]; 
  260.  
  261.  
  262.  
  263.  
  264. /** 
  265. * Check if a field is valid 
  266. * @since 1.3.5 
  267. * @param string $field Name of field to check 
  268. * @param string $type Type of field. Options: primary|meta|meta_key 
  269. * @return bool 
  270. */ 
  271. protected function valid_field( $field, $type = 'primary' ) { 
  272. switch( $type ) { 
  273. case 'primary' : 
  274. return array_key_exists( $field, $this->primary_fields ); 
  275. break; 
  276. case 'meta' : 
  277. return array_key_exists( $field, $this->meta_fields ); 
  278. break; 
  279. case 'meta_key' : 
  280. return array_key_exists( $field, $this->meta_keys ); 
  281. break; 
  282. default: 
  283. return false; 
  284. break; 
  285.  
  286.  
  287. /** 
  288. * Prepare an array for use with IN() or NOT IN() 
  289. * Creates comma separated string with numeric values of the all keys. 
  290. * @since 1.3.5 
  291. * @param array $array 
  292. * @return string 
  293. */ 
  294. protected function escape_array( array $array ) { 
  295. global $wpdb; 
  296. $escaped = array(); 
  297. foreach ( $array as $k => $v ) { 
  298. if ( is_numeric( $v ) ) { 
  299. $escaped[] = $wpdb->prepare( '%d', $v ); 
  300.  
  301. return implode( ', ', $escaped ); 
  302.  
  303. /** 
  304. * Add meta value to record by key 
  305. * @since 1.3.5 
  306. * @param array $meta Meta data 
  307. * @param array $data Record 
  308. * @return mixed 
  309. */ 
  310. protected function add_meta_to_record( array $meta, array $data ) { 
  311. if ( ! empty( $meta ) ) { 
  312. foreach ( $this->meta_keys as $key => $field ) { 
  313. $data[ $key ] = $this->reduce_meta( $meta, $key ); 
  314.  
  315. return $data; 
  316.  
  317. /** 
  318. * @return int|null 
  319. */ 
  320. public function highest_id() { 
  321. global $wpdb; 
  322. $table_name = $this->get_table_name(); 
  323. $results = $wpdb->get_results( "SELECT max(ID) FROM $table_name", ARRAY_N ); 
  324. if( is_array( $results ) && isset( $results[0], $results[0][0] ) ) { 
  325. return $results[0][0]; 
  326.  
  327.  
  328. /** 
  329. * Query by meta key 
  330. * @since 1.4.5 
  331. * @param string $key Meta key to query by 
  332. * @param string $value Meta value to query for 
  333. * @return array|null 
  334. */ 
  335. protected function query_meta( $key, $value ) { 
  336. global $wpdb; 
  337. $table = $this->get_table_name( true ); 
  338. $sql = $wpdb->prepare( "SELECT * FROM $table WHERE `meta_key` = '%s' AND `meta_value` = '%s' ", $key, $value ); 
  339. $r = $wpdb->get_results( $sql, ARRAY_A ); 
  340. return $r; 
  341.