-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at refactoring, still includes old code
- Loading branch information
1 parent
9d6d205
commit 17c2147
Showing
13 changed files
with
764 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace TwentySixB\WP\Plugin\Unbabble\DB; | ||
|
||
/** | ||
* Custom Post Table handler. | ||
* | ||
* @since 0.0.0 | ||
*/ | ||
class PostTable extends Table { | ||
|
||
protected function get_table_suffix() : string { | ||
return 'ubb_post_translations'; | ||
} | ||
|
||
protected function get_table_mysql( string $table_name, string $charset_collate ) : string { | ||
global $wpdb; | ||
return "CREATE TABLE {$table_name} ( | ||
post_id bigint(20) unsigned NOT NULL, | ||
locale varchar(5) NOT NULL, | ||
PRIMARY KEY (post_id), | ||
FOREIGN KEY (post_id) REFERENCES {$wpdb->posts} (ID) ON DELETE CASCADE | ||
) $charset_collate;"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace TwentySixB\WP\Plugin\Unbabble\DB; | ||
|
||
/** | ||
* Base Custom Table handler. | ||
* | ||
* @since 0.0.0 | ||
*/ | ||
abstract class Table { | ||
|
||
abstract protected function get_table_suffix() : string; | ||
abstract protected function get_table_mysql( string $table_name, string $charset_collate ) : string; | ||
|
||
public function get_table_name() : string { | ||
global $wpdb; | ||
return $wpdb->prefix . $this->get_table_suffix(); | ||
} | ||
|
||
public function create_table() : bool { | ||
global $wpdb; | ||
$table_name = $this->get_table_name(); | ||
|
||
// Validate db exists. | ||
if ( ! $this->table_exists() ) { | ||
|
||
// Create Table. | ||
$charset_collate = $wpdb->get_charset_collate(); | ||
|
||
// Be careful when changing this query. | ||
// See https://codex.wordpress.org/Creating_Tables_with_Plugins#Creating_or_Updating_the_Table | ||
$sql = $this->get_table_mysql( $table_name, $charset_collate ); | ||
|
||
"CREATE TABLE {$table_name} ( | ||
post_id bigint(20) unsigned NOT NULL, | ||
locale varchar(5) NOT NULL, | ||
FOREIGN KEY (post_id) REFERENCES {$wpdb->posts} (ID) ON DELETE CASCADE | ||
) $charset_collate;"; | ||
|
||
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | ||
dbDelta( $sql ); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Delete Countries table if it exists. | ||
* | ||
* @since 0.0.0 | ||
* @return void | ||
*/ | ||
public function delete_table() : void { | ||
global $wpdb; | ||
$table_name = $this->get_table_name(); | ||
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | ||
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" ); | ||
} | ||
|
||
/** | ||
* Check if Countries table exists in the BD. | ||
* | ||
* @since 0.0.0 | ||
* | ||
* @return bool | ||
*/ | ||
private function table_exists() : bool { | ||
global $wpdb; | ||
return $wpdb->get_var( | ||
$wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $this->get_table_name() ) ) | ||
) !== null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace TwentySixB\WP\Plugin\Unbabble\DB; | ||
|
||
/** | ||
* Custom Term Table handler. | ||
* | ||
* @since 0.0.0 | ||
*/ | ||
class TermTable extends Table { | ||
|
||
protected function get_table_suffix() : string { | ||
return 'ubb_term_translations'; | ||
} | ||
|
||
protected function get_table_mysql( string $table_name, string $charset_collate ) : string { | ||
global $wpdb; | ||
return "CREATE TABLE {$table_name} ( | ||
term_id bigint(20) unsigned NOT NULL, | ||
locale varchar(5) NOT NULL, | ||
PRIMARY KEY (term_id), | ||
FOREIGN KEY (term_id) REFERENCES {$wpdb->terms} (term_id) ON DELETE CASCADE | ||
) $charset_collate;"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.