Skip to content

Commit

Permalink
First pass at refactoring, still includes old code
Browse files Browse the repository at this point in the history
  • Loading branch information
goncaloasimoes committed Sep 29, 2022
1 parent 9d6d205 commit 17c2147
Show file tree
Hide file tree
Showing 13 changed files with 764 additions and 31 deletions.
7 changes: 6 additions & 1 deletion lib/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TwentySixB\WP\Plugin\Unbabble;

use TwentySixB\WP\Plugin\Unbabble\DB;

/**
* Fired during plugin activation
*
Expand Down Expand Up @@ -47,5 +49,8 @@ public static function activate( $network_wide = false ) {
* false if WPMU is disabled or plugin is activated on an
* individual blog.
*/
public static function single_activate( $network_wide ) {}
public static function single_activate( $network_wide ) {
(new DB\PostTable() )->create_table();
(new DB\TermTable() )->create_table();
}
}
25 changes: 25 additions & 0 deletions lib/DB/PostTable.php
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;";
}
}
76 changes: 76 additions & 0 deletions lib/DB/Table.php
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;
}

}
25 changes: 25 additions & 0 deletions lib/DB/TermTable.php
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;";
}
}
91 changes: 70 additions & 21 deletions lib/LanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace TwentySixB\WP\Plugin\Unbabble;

use TwentySixB\WP\Plugin\Unbabble\Refactor\LangInterface;
use WP_Post;

/**
* Handle Language switching for backoffice and frontend.
*
Expand All @@ -27,29 +30,24 @@ public function add_switcher_backoffice_admin_bar( \WP_Admin_Bar $wp_admin_bar )
$options = Options::get();
$current = $_GET['lang'] ?? $_COOKIE['ubb_lang'] ?? $options['default_language'];

$make_lang_url = function ( $lang ) {
$params = $_GET;
unset( $params['lang'], $params['ubb_switch_lang'] );
$params['ubb_switch_lang'] = $lang;
$query = http_build_query( $params );
if ( empty( $query ) ) {
return $_SERVER['PHP_SELF'];
// TODO: This shouldn't happen. Should always be array.
$allowed_languages = is_array( $options['allowed_languages'] ) ? $options['allowed_languages'] : [];

$langs = [];
foreach ( $allowed_languages as $allowed_lang ) {
if ( ! isset( $_REQUEST['post'] ) || ! is_numeric( $_REQUEST['post'] ) ) {
$url = $this->make_switch_url( $allowed_lang );
} else {
$url = $this->make_switch_post_url( $_REQUEST['post'], $allowed_lang );
}
return $_SERVER['PHP_SELF'] . '?' . $query;
};

$langs = array_map(
function ( $lang ) use ( $make_lang_url, $current ) {
return sprintf(
'<option value="%1$s" %2$s>%3$s</option>',
$make_lang_url( $lang ),
\selected( $lang, $current, false ),
$lang
);
},
// TODO: This shouldn't happen. Should always be array.
is_array( $options['allowed_languages'] ) ? $options['allowed_languages'] : []
);
$langs[] = sprintf(
'<option value="%1$s" %2$s>%3$s</option>',
$url,
\selected( $allowed_lang, $current, false ),
$allowed_lang
);
}

$html = sprintf(
'<select onChange="window.location.href=this.value" style="width:4.5em;">
Expand All @@ -69,4 +67,55 @@ function ( $lang ) use ( $make_lang_url, $current ) {
]
);
}

/**
* Make the default language switch url.
*
* @param string $lang
* @return string
*/
private function make_switch_url( string $lang ) : string {
$params = $_GET;
unset( $params['lang'], $params['ubb_switch_lang'] );
$params['ubb_switch_lang'] = $lang;
$query = http_build_query( $params );
if ( empty( $query ) ) {
return $_SERVER['PHP_SELF'];
}
return $_SERVER['PHP_SELF'] . '?' . $query;
}

/**
* Make the post language switch url.
*
* @param int $post_id
* @param string $lang
* @return string
*/
private function make_switch_post_url( int $post_id, string $lang ) : string {
$translations = LangInterface::get_translation_list( $post_id );
$translation_id = array_search( $lang, $translations, true );
if ( ! $translation_id ) {
$post_type = get_post_type( $post_id );
// TODO: better way to get this
return get_site_url(
null,
sprintf(
"/wp-admin/edit.php?%slang=%s",
$post_type === 'post' ? '' : "post_type={$post_type}",
$lang
)
);
}

$params = $_GET;
unset( $params['lang'], $params['ubb_switch_lang'] );
$params['post'] = $translation_id;
$params['ubb_switch_lang'] = $lang;
$query = http_build_query( $params );
if ( empty( $query ) ) {
return $_SERVER['PHP_SELF'];
}
return $_SERVER['PHP_SELF'] . '?' . $query;
}
}
29 changes: 21 additions & 8 deletions lib/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TwentySixB\WP\Plugin\Unbabble;

use TwentySixB\WP\Plugin\Unbabble\Refactor;

/**
* The core plugin class.
*
Expand Down Expand Up @@ -56,6 +58,7 @@ public function __construct( $name, $version ) {
public function run() {
$this->set_locale();
$this->define_admin_hooks();
$this->include_function_files();
}

/**
Expand Down Expand Up @@ -104,18 +107,28 @@ private function set_locale() {
private function define_admin_hooks() {

$components = [
'admin' => new Admin( $this ),
'create_translation' => new CreateTranslation( $this ),
'language_metabox' => new LanguageMetaBox( $this ),
'language_switcher' => new LanguageSwitcher( $this ),
'language_filter' => new LanguageFilter( $this ),
'options_page' => new OptionsPage( $this ),
'post_translation' => new PostTranslation( $this ),
'post_meta_translation' => new PostMetaTranslation( $this ),
'admin' => new Admin( $this ),
'options_page' => new OptionsPage( $this ),
'language_switcher' => new LanguageSwitcher( $this ),
'language_metabox' => new Refactor\LangMetaBox( $this ),
'create_translation' => new Refactor\CreateTranslation( $this ),
'redirector' => new Refactor\Redirector( $this ),
'language_filter' => new Refactor\LangFilter( $this ),

// 'language_metabox' => new LanguageMetaBox( $this ),
// 'language_filter' => new LanguageFilter( $this ),
// 'post_translation' => new PostTranslation( $this ),
// 'post_meta_translation' => new PostMetaTranslation( $this ),

// 'create_translation' => new CreateTranslation( $this ),
];

foreach ( $components as $component ) {
$component->register();
}
}

private function include_function_files() {
include __DIR__ . '/utilities.php';
}
}
Loading

0 comments on commit 17c2147

Please sign in to comment.