// Setup the prefixed name $this->prefixed_name = $this->apply_prefix( $this->name, $glue ); // Maybe create database key if ( empty( $this->db_version_key ) ) { $this->db_version_key = implode( $glue, array( sanitize_key( $this->db_global ), $this->prefixed_name, 'version' ) ); } } /** * Set this table up in the database interface. * * This must be done directly because the database interface does not * have a common mechanism for manipulating them safely. * * @since 1.0.0 */ private function set_db_interface() { // Get the database once, to avoid duplicate function calls $db = $this->get_db(); // Bail if no database if ( empty( $db ) ) { return; } // Set variables for global tables if ( $this->is_global() ) { $site_id = 0; $tables = 'ms_global_tables'; // Set variables for per-site tables } else { $site_id = null; $tables = 'tables'; } // Set the table prefix and prefix the table name $this->table_prefix = $db->get_blog_prefix( $site_id ); // Get the prefixed table name $prefixed_table_name = "{$this->table_prefix}{$this->prefixed_name}"; // Set the database interface $db->{$this->prefixed_name} = $this->table_name = $prefixed_table_name; // Create the array if it does not exist if ( ! isset( $db->{$tables} ) ) { $db->{$tables} = array(); } // Add the table to the global table array $db->{$tables}[] = $this->prefixed_name; // Charset if ( ! empty( $db->charset ) ) { $this->charset_collation = "DEFAULT CHARACTER SET {$db->charset}"; } // Collation if ( ! empty( $db->collate ) ) { $this->charset_collation .= " COLLATE {$db->collate}"; } } /** * Set the database version for the table. * * @since 1.0.0 * * @param mixed $version Database version to set when upgrading/creating */ private function set_db_version( $version = '' ) { // If no version is passed during an upgrade, use the current version if ( empty( $version ) ) { $version = $this->version; } // Update the DB version $this->is_global() ? update_network_option( get_main_network_id(), $this->db_version_key, $version ) : update_option( $this->db_version_key, $version ); // Set the DB version $this->db_version = $version; } /** * Get the table version from the database. * * @since 1.0.0 */ private function get_db_version() { $this->db_version = $this->is_global() ? get_network_option( get_main_network_id(), $this->db_version_key, false ) : get_option( $this->db_version_key, false ); } /** * Delete the table version from the database. * * @since 1.0.0 */ private function delete_db_version() { $this->db_version = $this->is_global() ? delete_network_option( get_main_network_id(), $this->db_version_key ) : delete_option( $this->db_version_key ); } /** * Add class hooks to the parent application actions. * * @since 1.0.0 */ private function add_hooks() { // Add table to the global database object add_action( 'switch_blog', array( $this, 'switch_blog' ) ); add_action( 'admin_init', array( $this, 'maybe_upgrade' ) ); } /** * Check if the current request is from some kind of test. * * This is primarily used to skip 'admin_init' and force-install tables. * * @since 1.0.0 * * @return bool */ private function is_testing() { return (bool) // Tests constant is being used ( defined( 'WP_TESTS_DIR' ) && WP_TESTS_DIR ) || // Scaffolded (https://make.wordpress.org/cli/handbook/plugin-unit-tests/) function_exists( '_manually_load_plugin' ); } /** * Check if table is global. * * @since 1.0.0 * * @return bool */ private function is_global() { return ( true === $this->global ); } /** * Try to get a callable upgrade, with some magic to avoid needing to * do this dance repeatedly inside subclasses. * * @since 1.0.0 * * @param string $callback * * @return mixed Callable string, or false if not callable */ private function get_callable( $callback = '' ) { // Default return value $callable = $callback; // Look for global function if ( ! is_callable( $callable ) ) { // Fallback to local class method $callable = array( $this, $callback ); if ( ! is_callable( $callable ) ) { // Fallback to class method prefixed with "__" $callable = array( $this, "__{$callback}" ); if ( ! is_callable( $callable ) ) { $callable = false; } } } // Return callable string, or false if not callable return $callable; } }