PUM_DB_Subscribers::create_table()

Create the table


Description Description


Source Source

File: classes/DB/Subscribers.php

	public function create_table() {

		global $wpdb;

		if ( ! function_exists( 'dbDelta' ) ) {
			require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		}

		$charset_collate = $wpdb->get_charset_collate();

		/**
		 * - [x] You must put each field on its own line in your SQL statement.
		 * - [x] You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
		 * - [x] You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
		 * - [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.
		 * - [x] You must not use any apostrophes or backticks around field names.
		 * - [x] Field types must be all lowercase.
		 * - [x] SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
		 * - [x] You must specify the length of all fields that accept a length parameter. int(11), for example.
		 */
		$sql = "CREATE TABLE " . $this->table_name() . " (
			ID bigint(20) NOT NULL AUTO_INCREMENT,
			email_hash varchar(32) NOT NULL,
			popup_id bigint(20) NOT NULL,
			user_id bigint(20) NOT NULL,
			email varchar(191) NOT NULL,
			name varchar(255) NOT NULL,
			fname varchar(255) NOT NULL,
			lname varchar(255) NOT NULL,
			uuid varchar(255) NOT NULL,
			consent varchar(255) NOT NULL,
			consent_args longtext NOT NULL,
			created datetime NOT NULL,
		  PRIMARY KEY  (ID),
		  KEY email (email),
		  KEY user_id (user_id),
		  KEY popup_id (popup_id),
		  KEY email_hash (email_hash)
		) $charset_collate;";

		$results = dbDelta( $sql );
		PUM_Utils_Logging::instance()->log( 'Subscriber table results: ' . implode( ',', $results ) );

		$previous_error = $wpdb->last_error; // The show tables query will erase the last error. So, record it now in case we need it.
		if ( $wpdb->get_var("SHOW TABLES LIKE '{$this->table_name()}'") !== $this->table_name() ) {
			PUM_Utils_Logging::instance()->log( "Subscriber table exists check failed! Last error from wpdb: $previous_error." );
		}

		update_option( $this->table_name . '_db_version', $this->version );
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.