Skip to content

Commit

Permalink
Stop unbabble from running when other known i18n plugins are active. …
Browse files Browse the repository at this point in the history
…Set terms language on ajax term create. Fix other bugs
  • Loading branch information
goncaloasimoes committed Dec 19, 2022
1 parent 4e00877 commit 7c65e3b
Show file tree
Hide file tree
Showing 28 changed files with 128 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/API/Actions/HiddenContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct( Plugin $plugin, string $namespace ) {
}

public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\register_rest_route(
Expand Down
2 changes: 1 addition & 1 deletion lib/API/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Header {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_filter( 'rest_pre_dispatch', [ $this, 'accept_language_header' ], 2, 3 );
Expand Down
2 changes: 1 addition & 1 deletion lib/API/QueryVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class QueryVar {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_filter( 'rest_pre_dispatch', [ $this, 'set_lang_query_var' ], 1, 3 );
Expand Down
2 changes: 1 addition & 1 deletion lib/Admin/LanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LanguageSwitcher {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Admin/Redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Redirector {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Integrations/AdvancedCustomFieldsPro.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AdvancedCustomFieldsPro {
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Integrations/YoastDuplicatePost.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class YoastDuplicatePost {
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/LangInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class LangInterface {
* @return string The current language code.
*/
public static function get_current_language() : string {
global $wp_query;
$options = Options::get();
$lang = get_query_var( 'lang', null );
if ( $wp_query !== null ) {
$lang = get_query_var( 'lang', null );
}
// TODO: Auto-draft saving does not put the query var.
if ( ! isset( $lang ) && isset( $_GET['lang'] ) ) {
$lang = $_GET['lang'];
Expand Down
2 changes: 1 addition & 1 deletion lib/Language/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Frontend {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Language/LanguagePacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LanguagePacks {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions lib/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ public static function only_one_language_allowed() : bool {
return count( self::get()['allowed_languages'] ) < 2;
}

/**
* Returns whether unbabble functionality (filtering, etc) should be running.
*
* @since 0.0.1
* @return bool False if only a single language is allowed or other language plugins are
* active. True otherwise.
*/
public static function should_run_unbabble() : bool {
if ( self::only_one_language_allowed() ) {
return false;
}

// Check for conflicting language plugins.
$active_plugins = get_option( 'active_plugins', [] );
$known_language_plugins = [
'sitepress-multilingual-cms/sitepress.php',
// TODO: Add others.
];
foreach ( $known_language_plugins as $plugin_name ) {
if ( in_array( $plugin_name, $active_plugins, true ) ) {
return false;
}
}
return true;
}

/**
* Returns the allowed post types.
*
Expand Down
1 change: 1 addition & 0 deletions lib/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function define_plugin_hooks() {
'terms_change_language' => new Terms\ChangeLanguage( $this ),
'terms_language_filter' => new Terms\LangFilter( $this ),
'terms_admin_notices' => new Terms\AdminNotices( $this ),
'terms_new_term' => new Terms\NewTerm( $this ),

'router_query_var' => new Router\QueryVar( $this ),
'router_directory' => new Router\Directory( $this ),
Expand Down
2 changes: 1 addition & 1 deletion lib/Posts/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AdminNotices {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_action( 'admin_notices', [ $this, 'duplicate_language' ], PHP_INT_MAX );
Expand Down
2 changes: 1 addition & 1 deletion lib/Posts/ChangeLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ChangeLanguage {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Posts/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CreateTranslation {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
26 changes: 22 additions & 4 deletions lib/Posts/LangFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LangFilter {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_filter( 'posts_where', [ $this, 'filter_posts_by_language' ], 10, 2 );
Expand All @@ -41,9 +41,22 @@ public function filter_posts_by_language( string $where, WP_Query $query ) : str
return $where;
}

$current_lang = esc_sql( LangInterface::get_current_language() );
$post_lang_table = ( new PostTable() )->get_table_name();
$where .= " AND ({$wpdb->posts}.ID IN ( SELECT post_id FROM {$post_lang_table} WHERE locale = '$current_lang' ))";
// TODO: Deal with untranslatable post types.

$current_lang = esc_sql( LangInterface::get_current_language() );
$post_lang_table = ( new PostTable() )->get_table_name();
$allowed_post_types = implode( "','", Options::get_allowed_post_types() );
$where .= " AND (
{$wpdb->posts}.ID IN (
SELECT post_id
FROM {$post_lang_table} AS PT
WHERE locale = '$current_lang'
UNION
SELECT ID
FROM {$wpdb->posts}
WHERE post_type NOT IN ('{$allowed_post_types}')
)
)";
return $where;
}

Expand All @@ -69,6 +82,11 @@ public function allow_filter( WP_Query $query ) : bool {
return false;
}

// Don't apply filters on switch_to_blog to blogs without the plugin.
if ( ! LangInterface::is_unbabble_active() ) {
return false;
}

/**
* Filters whether posts should be filtered by their language.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Posts/LangMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LangMetaBox {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Posts/LinkTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LinkTranslation {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_action( 'save_post', [ $this, 'link_translations' ], PHP_INT_MAX - 10 );
Expand Down
2 changes: 1 addition & 1 deletion lib/Router/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Directory {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() || Options::get_router() !== 'directory' ) {
if ( ! Options::should_run_unbabble() || Options::get_router() !== 'directory' ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Router/QueryVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class QueryVar {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() || Options::get_router() !== 'query_var' ) {
if ( ! Options::should_run_unbabble() || Options::get_router() !== 'query_var' ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AdminNotices {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_action( 'admin_notices', [ $this, 'duplicate_language' ], PHP_INT_MAX );
Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/ChangeLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ChangeLanguage {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_action( 'saved_term', [ $this, 'change_language' ], PHP_INT_MAX );
Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/CreateTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CreateTranslation {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Terms/LangFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LangFilter {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_filter( 'terms_clauses', [ $this, 'filter_terms_by_language' ], 10, 3 );
Expand All @@ -37,6 +37,11 @@ public function register() {
*/
public function filter_terms_by_language( array $pieces, array $taxonomies, array $args ) : array {

// Don't apply filters on switch_to_blog to blogs without the plugin.
if ( ! LangInterface::is_unbabble_active() ) {
return $pieces;
}

/**
* Filters whether terms should be filtered by their language.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/LangMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LangMetaBox {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Terms/LinkTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LinkTranslation {
* @since 0.0.1
*/
public function register() {
if ( Options::only_one_language_allowed() ) {
if ( ! Options::should_run_unbabble() ) {
return;
}
\add_action( 'saved_term', [ $this, 'link_translations' ], PHP_INT_MAX, 4 );
Expand Down
46 changes: 46 additions & 0 deletions lib/Terms/NewTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace TwentySixB\WP\Plugin\Unbabble\Terms;

use TwentySixB\WP\Plugin\Unbabble\LangInterface;
use TwentySixB\WP\Plugin\Unbabble\Options;

/**
* Hooks related to new terms.
*
* @since 0.0.1
*/
class NewTerm {

/**
* Register hooks.
*
* @since 0.0.1
*/
public function register() {
if ( ! Options::should_run_unbabble() ) {
return;
}

$taxonomies = array_intersect( \get_taxonomies(), Options::get_allowed_taxonomies() );
foreach ( $taxonomies as $taxonomy ) {
\add_action( "create_{$taxonomy}", [ $this, 'new_term_ajax' ] );
}
}

/**
* Set a new term's language, when its created via via ajax.
*
* @since 0.0.1
*
* @param int $term_id
* @return void
*/
public function new_term_ajax( int $term_id ) : void {
if ( ( $_POST['action'] ?? '' ) !== 'add-category' ) {
return;
}

LangInterface::set_term_language( $term_id, LangInterface::get_current_language() );
}
}
2 changes: 2 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @package 26b
*/

// TODO: Clean tables and metas.

// If uninstall is not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
Expand Down

0 comments on commit 7c65e3b

Please sign in to comment.