key, 0); return version_compare($current_version, $this->version, '<'); } /** * Try db and return true on success * * @return bool */ public function try_update_db(){ // Check if requires Update? if( !$this->require_update() ){ return false; } // Is there creation script? if( !($query = $this->create_sql()) ){ return false; } // O.K. Let's create table $this->dbDelta($query); // Do something after update. $this->after_update(); // Update option update_option($this->key, $this->version); return true; } /** * Detect if innodb is available * * @return bool */ protected function have_innodb(){ $query = <<db->get_row($query); return 'YES' === $row->Value; } /** * Check if specific table is InnoDB * * @param string $table * * @return bool */ protected function is_innodb($table){ $query = <<db->get_row($this->db->prepare($query, $table)); } /** * Executed after table has been updated * * For example, index or strage engine. */ protected function after_update(){ // Do nothing. } /** * Do dbDelta * * @param string $query * * @return array */ protected function dbDelta($query){ // Here starts database update! // Load required files. require_once ABSPATH . "wp-admin/includes/upgrade.php"; // Do dbDelta! return dbDelta($query); } /** * Creation SQL * * If you need create SQL, override this. * * @return string */ protected function create_sql(){ return ''; } /** * Getter * * @param string $name * * @return null|string|\wpdb */ public function __get($name){ switch( $name ){ case 'db': global $wpdb; return $wpdb; break; case 'table': return $this->db->prefix.'afb_'.$this->name; break; case 'key': return $this->table.'_version'; break; case 'i18n': return i18n::get_instance(); break; default: return null; break; } } }