Нет описания

Subscribers.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. // Exit if accessed directly
  3. /*******************************************************************************
  4. * Copyright (c) 2019, Code Atlantic LLC
  5. ******************************************************************************/
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9. /**
  10. * PUM_Subscribers Class
  11. */
  12. class PUM_DB_Subscribers extends PUM_Abstract_Database {
  13. /**
  14. * The name of our database table
  15. */
  16. public $table_name = 'pum_subscribers';
  17. /**
  18. * The version of our database table
  19. */
  20. public $version = 20200917;
  21. /**
  22. * The name of the primary column
  23. */
  24. public $primary_key = 'ID';
  25. /**
  26. * Get columns and formats
  27. */
  28. public function get_columns() {
  29. return array(
  30. 'ID' => '%d',
  31. 'uuid' => '%s',
  32. 'popup_id' => '%d',
  33. 'email_hash' => '%s',
  34. 'email' => '%s',
  35. 'name' => '%s',
  36. 'fname' => '%s',
  37. 'lname' => '%s',
  38. 'user_id' => '%d',
  39. 'consent_args' => '%s',
  40. 'consent' => '%s',
  41. 'created' => '%s',
  42. );
  43. }
  44. /**
  45. * Get default column values
  46. */
  47. public function get_column_defaults() {
  48. return array(
  49. 'uuid' => '',
  50. 'popup_id' => 0,
  51. 'email_hash' => '',
  52. 'email' => '',
  53. 'name' => '',
  54. 'fname' => '',
  55. 'lname' => '',
  56. 'user_id' => 0,
  57. 'consent_args' => '',
  58. 'consent' => 'no',
  59. 'created' => current_time( 'mysql', 0 ),
  60. );
  61. }
  62. /**
  63. * Create the table
  64. */
  65. public function create_table() {
  66. global $wpdb;
  67. if ( ! function_exists( 'dbDelta' ) ) {
  68. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  69. }
  70. $charset_collate = $wpdb->get_charset_collate();
  71. /**
  72. * - [x] You must put each field on its own line in your SQL statement.
  73. * - [x] You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  74. * - [x] You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
  75. * - [x] KEY must be followed by a SINGLE SPACE then the key name then a space then open parenthesis with the field name then a closed parenthesis.
  76. * - [x] You must not use any apostrophes or backticks around field names.
  77. * - [x] Field types must be all lowercase.
  78. * - [x] SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
  79. * - [x] You must specify the length of all fields that accept a length parameter. int(11), for example.
  80. */
  81. $sql = "CREATE TABLE " . $this->table_name() . " (
  82. ID bigint(20) NOT NULL AUTO_INCREMENT,
  83. email_hash varchar(32) NOT NULL,
  84. popup_id bigint(20) NOT NULL,
  85. user_id bigint(20) NOT NULL,
  86. email varchar(191) NOT NULL,
  87. name varchar(255) NOT NULL,
  88. fname varchar(255) NOT NULL,
  89. lname varchar(255) NOT NULL,
  90. uuid varchar(255) NOT NULL,
  91. consent varchar(255) NOT NULL,
  92. consent_args longtext NOT NULL,
  93. created datetime NOT NULL,
  94. PRIMARY KEY (ID),
  95. KEY email (email),
  96. KEY user_id (user_id),
  97. KEY popup_id (popup_id),
  98. KEY email_hash (email_hash)
  99. ) $charset_collate;";
  100. $results = dbDelta( $sql );
  101. PUM_Utils_Logging::instance()->log( 'Subscriber table results: ' . implode( ',', $results ) );
  102. $previous_error = $wpdb->last_error; // The show tables query will erase the last error. So, record it now in case we need it.
  103. if ( $wpdb->get_var("SHOW TABLES LIKE '{$this->table_name()}'") !== $this->table_name() ) {
  104. PUM_Utils_Logging::instance()->log( "Subscriber table exists check failed! Last error from wpdb: $previous_error." );
  105. }
  106. update_option( $this->table_name . '_db_version', $this->version );
  107. }
  108. public function get_by_email( $email = '' ) {
  109. }
  110. public function query( $args = array(), $return_type = 'OBJECT' ) {
  111. global $wpdb;
  112. $args = wp_parse_args( $args, array(
  113. 'fields' => '*',
  114. 'page' => null,
  115. 'limit' => null,
  116. 'offset' => null,
  117. 's' => null,
  118. 'orderby' => null,
  119. 'order' => null,
  120. ) );
  121. $columns = $this->get_columns();
  122. $fields = $args['fields'];
  123. if ( $fields == '*' ) {
  124. $fields = array_keys( $columns );
  125. } else {
  126. $fields = explode( ',', $args['fields'] );
  127. $fields = array_map( 'trim', $fields );
  128. $fields = array_map( 'sanitize_text_field', $fields );
  129. }
  130. $select_fields = implode( '`, `', $fields );
  131. // Begin building query.
  132. $query = "SELECT `$select_fields` FROM {$this->table_name()}";
  133. // Set up $values array for wpdb::prepare
  134. $values = array();
  135. // Define an empty WHERE clause to start from.
  136. $where = "WHERE 1=1";
  137. // Build search query.
  138. if ( $args['s'] && ! empty( $args['s'] ) ) {
  139. $search = wp_unslash( trim( $args['s'] ) );
  140. $search_where = array();
  141. foreach ( $columns as $key => $type ) {
  142. if ( in_array( $key, $fields ) ) {
  143. if ( $type == '%s' || ( $type == '%d' && is_numeric( $search ) ) ) {
  144. $values[] = '%' . $wpdb->esc_like( $search ) . '%';
  145. $search_where[] = "`$key` LIKE '%s'";
  146. }
  147. }
  148. }
  149. if ( ! empty( $search_where ) ) {
  150. $where .= ' AND (' . join( ' OR ', $search_where ) . ')';
  151. }
  152. }
  153. $query .= " $where";
  154. if ( ! empty( $args['orderby'] ) ) {
  155. $query .= " ORDER BY %s";
  156. $values[] = wp_unslash( trim( $args['orderby'] ) );
  157. switch ( $args['order'] ) {
  158. case 'asc':
  159. case 'ASC':
  160. $query .= " ASC";
  161. break;
  162. case 'desc':
  163. case 'DESC':
  164. default:
  165. $query .= " DESC";
  166. break;
  167. }
  168. }
  169. if ( ! empty( $args['limit'] ) ) {
  170. $query .= " LIMIT %d";
  171. $values[] = absint( $args['limit'] );
  172. }
  173. // Pagination.
  174. if ( $args['page'] >= 1 ) {
  175. $args['offset'] = ( $args['page'] * $args['limit'] ) - $args['limit'];
  176. }
  177. if ( ! empty( $args['offset'] ) ) {
  178. $query .= " OFFSET %d";
  179. $values[] = absint( $args['offset'] );
  180. }
  181. if ( strpos( $query, '%s' ) || strpos( $query, '%d' ) ) {
  182. $query = $wpdb->prepare( $query, $values );
  183. }
  184. return $wpdb->get_results( $query, $return_type );
  185. }
  186. /**
  187. * @param $args
  188. *
  189. * @return int
  190. */
  191. public function total_rows( $args ) {
  192. $args['limit'] = null;
  193. $args['offset'] = null;
  194. $args['page'] = null;
  195. $results = $this->query( $args );
  196. return $results ? count( $results ) : 0;
  197. }
  198. }