-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Teemu Suoranta
committed
Nov 5, 2016
0 parents
commit 833d337
Showing
3 changed files
with
188 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "aucor/polylang-smart-language-select-disabler", | ||
"description": "", | ||
"license": "GPLv2 or later", | ||
"type": "wordpress-plugin", | ||
"homepage": "https://github.com/aucor/polylang-smart-language-select-disabler", | ||
"authors": [ | ||
{ | ||
"name": "Teemu Suoranta", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} |
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,100 @@ | ||
<?php | ||
/* | ||
Plugin Name: Polylang Add-on: Smart Language Select Disabler | ||
Plugin URI: | ||
Version: 0.1.0 | ||
Author: Aucor Oy | ||
Author URI: https://github.com/aucor | ||
Description: Disable content language select when there's translations in Polylang | ||
License: GPLv2 or later | ||
License URI: http://www.gnu.org/licenses/gpl-2.0.html | ||
Text Domain: polylang-smart-language-select-disabler | ||
*/ | ||
|
||
class PolylangSmartLanguageSelectDisabler { | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public function __construct() { | ||
|
||
// Check that Polylang is active | ||
global $polylang; | ||
|
||
if (isset($polylang)) { | ||
add_action('current_screen', array(&$this, 'maybe_disable_language_select')); | ||
} | ||
} | ||
|
||
/** | ||
* Maybe disable language select on edit posts and terms | ||
* | ||
* Check switcher when there's translations. Switching language then will mess things up. | ||
* | ||
* @param $current_screen WP_Screen Object | ||
*/ | ||
function maybe_disable_language_select($current_screen) { | ||
|
||
$translations = false; | ||
|
||
if( $current_screen->base == 'post' ) { | ||
|
||
// $_GET['post'] has ID for all post types | ||
if( isset( $_GET['post'] ) ) { | ||
$post_id = intval( $_GET['post'] ); | ||
$translations = PLL()->model->post->get_translations( $post_id ); | ||
|
||
// $_GET['new_lang'] is used when creating a translation so there's always translations | ||
} elseif( isset( $_GET['new_lang'] ) ) { | ||
$translations = true; | ||
} | ||
|
||
// Give filter to add custom logic for disabler | ||
$translations = apply_filters( 'polylang-disable-language-select', $translations, $current_screen ); | ||
|
||
if ($translations) { | ||
?> | ||
<script> | ||
if(typeof addEventListener === 'function') { | ||
document.addEventListener('DOMContentLoaded', function() { | ||
document.getElementById('post_lang_choice').setAttribute('disabled', 'disabled'); | ||
}); | ||
} | ||
</script> | ||
<?php | ||
} | ||
|
||
} elseif ($current_screen->base == 'term' || $current_screen->base == 'edit-tags' ) { | ||
|
||
// $_GET['tag_ID'] has ID for all taxonomies | ||
if( isset( $_GET['tag_ID'] ) ) { | ||
$term_id = intval( $_GET['tag_ID'] ); | ||
$translations = PLL()->model->term->get_translations( $term_id ); | ||
|
||
// $_GET['new_lang'] is used when creating a translation so there's always translations | ||
} elseif( isset( $_GET['new_lang'] ) ) { | ||
$translations = true; | ||
} | ||
|
||
// Give filter to add custom logic for disabler | ||
$translations = apply_filters( 'polylang-disable-language-select', $translations, $current_screen ); | ||
|
||
if ($translations) { | ||
?> | ||
<script> | ||
if(typeof addEventListener === 'function') { | ||
document.addEventListener('DOMContentLoaded', function() { | ||
document.getElementById('term_lang_choice').setAttribute('disabled', 'disabled'); | ||
}); | ||
} | ||
</script> | ||
<?php | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
add_action( 'plugins_loaded', create_function('', 'global $polylang_smart_language_select_disabler; $polylang_smart_language_select_disabler = new PolylangSmartLanguageSelectDisabler();') ); | ||
|
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,75 @@ | ||
# Polylang Add-on: Smart Language Select Disabler | ||
|
||
**Contributors:** [Teemu Suoranta](https://github.com/TeemuSuoranta) | ||
|
||
**Tags:** Polylang, Admin, Language Select, WordPress | ||
|
||
**License:** GPLv2 or later | ||
|
||
 | ||
|
||
|
||
## Why this plugin exists? | ||
|
||
### Changing post's or term's language when it has translations messes things up | ||
|
||
Basically the translations are unlinked and there is no warning for this. You may need to change post's language for example when you start to write a new post and notice that it's in wrong language. When translations are involved, there's really no use for changing the language. | ||
|
||
### Users confuse adding translations and changing post's language | ||
|
||
I've seen that users have multiple times changed post's language when they meant to navigate to translation. Smartly disabling the select enhances the UI. | ||
|
||
### Changing the posts language is risky business anyway | ||
|
||
Changing the language of post when it has content is prone to errors. Even though the language can be changed, the images added to content may still be in wrong language. Custom fields, relations etc are not automatically changed. Language should be changed right away before adding content. | ||
|
||
## What it does? | ||
|
||
* Checks if currently edited post or term has translations | ||
* If it has, prints one line of JS that disables the select | ||
|
||
|
||
## Installation | ||
|
||
Download and activate. That's it. You will need Polylang, too (d'oh). | ||
|
||
**Composer:** | ||
``` | ||
$ composer aucor/polylang-smart-language-select-disabler | ||
``` | ||
**With composer.json:** | ||
``` | ||
{ | ||
"require": { | ||
"aucor/polylang-smart-language-select-disabler": "*" | ||
}, | ||
"extra": { | ||
"installer-paths": { | ||
"htdocs/wp-content/plugins/{$name}/": ["type:wordpress-plugin"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Filters | ||
|
||
You can disable select always or build some fancy custom logic: | ||
|
||
``` | ||
function my_polylang_disable_language_select($disable_select, $current_screen) { | ||
return true; | ||
} | ||
add_filter('polylang-disable-language-select', 'my_polylang_disable_language_select', 10, 2); | ||
``` | ||
|
||
|
||
## Issues and feature whishlist | ||
|
||
**Issues:** | ||
|
||
* There is a tiny delay for disable to happen. Maybe another JS hook would help? | ||
|
||
|
||
**Feature whishlist:** | ||
|
||
* You tell me. |