-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpolylang-smart-language-select-disabler.php
executable file
·202 lines (161 loc) · 6.35 KB
/
polylang-smart-language-select-disabler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Plugin Name: Smart Language Select Disabler for Polylang
* Plugin URI: https://github.com/aucor/polylang-smart-language-select-disabler
* Version: 1.1.1
* 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
*
* @package polylang-smart-language-select-disabler
*/
/**
* Wrapper class for plugin functionality.
*/
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' ) );
add_action( 'pll_default_lang_row_action', array( &$this, 'maybe_disable_default_language_select' ), 10, 2 );
}
} // end function __construct
/**
* Maybe disable language select on edit posts and terms.
* Check switcher when there's translations. Switching language then will mess things up.
*
* @param object $current_screen WP_Screen Object.
*/
public function maybe_disable_language_select( $current_screen ) {
$disable_translations = false;
if ( 'post' === $current_screen->base ) {
// $_GET['post'] has ID for all post types
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
$disable_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'] ) ) {
$disable_translations = true;
}
} elseif ( 'term' === $current_screen->base ) {
// $_GET['tag_ID'] has ID for all taxonomies
if ( isset( $_GET['tag_ID'] ) ) {
$term_id = intval( $_GET['tag_ID'] );
$disable_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'] ) ) {
$disable_translations = true;
}
}
// Posts bulk edit
if ( 'edit' === $current_screen->base ) {
$disable_translations = true;
}
// Terms bulk edit
if ( 'edit-tags' === $current_screen->base ) {
$disable_translations = true;
}
// Give filter to add custom logic for disabler
$disable_translations = apply_filters( 'polylang-disable-language-select', $disable_translations, $current_screen );
if ( $disable_translations ) {
add_action( 'in_admin_header', function() {
?>
<style>
/* Bulk edit */
.wp-admin .inline-edit-row select[name="inline_lang_choice"] {
display: none !important;
}
/* Edit screens */
.wp-admin #post_lang_choice,
.wp-admin #term_lang_choice {
display: none !important;
}
.wp-admin #select-post-language .pll-select-flag,
.wp-admin #select-term-language, .pll-select-flag {
margin-right: 6px;
}
.wp-admin #select-edit-term-language .description {
display: none; /* Description doesn't apply */
}
</style>
<script>
function polylang_addon_disable_language_select( el, label ) {
if( el == null ) {
return false;
}
// get current language, if not already set
if( label == null ) {
var label = el.options[ el.selectedIndex ].innerHTML;
}
// create <p>
var p = document.createElement( 'p' );
p.style.display = 'inline-block';
p.style.margin = '0';
p.textContent = label;
// insert <p>
el.parentNode.insertBefore(p, el.nextSibling);
}
if( typeof addEventListener === 'function' ) {
// Bulk edit
document.addEventListener( 'DOMNodeInserted', function( e ) {
var t = e.target;
// WP inserts the quick edit from
if ( '[object HTMLTableRowElement]' == Object.prototype.toString.call(t) && 'inline-edit' == t.getAttribute( 'id' ) ) {
// timeout so that inline lang choice gets real value
setTimeout(function() {
var lang_choice = document.getElementsByName('inline_lang_choice');
var lang_choice = lang_choice[0];
var label = lang_choice.options[ lang_choice.selectedIndex ].innerHTML
polylang_addon_disable_language_select(lang_choice, label);
}, 5);
}
} );
// Edit screens
document.addEventListener('DOMContentLoaded', function() {
// posts
var post_lang_choice = document.getElementById('post_lang_choice');
polylang_addon_disable_language_select(post_lang_choice);
// terms
var term_lang_choice = document.getElementById('term_lang_choice');
polylang_addon_disable_language_select(term_lang_choice);
});
}
</script>
<?php
});
} // end if $disable_translations
}
/**
* Maybe disable Polylang default language change select
*
* @param string $s html markup of the action.
* @param object $item the item.
* @return string
*/
public function maybe_disable_default_language_select( $s, $item ) {
$disable = true;
// Give get parameter to allow chaning the default language
if ( isset( $_GET['iknowwhatimdoing'] ) ) {
$disable = false;
}
// Give filter to add custom logic for disabler
$disable = apply_filters( 'polylang-disable-default-language-select', $disable, $item );
// Return empty string if disabled
if ( $disable ) {
return '';
}
// Return original string containing link to change
return $s;
}
} // end class PolylangSmartLanguageSelectDisabler
// Run plugin.
add_action( 'plugins_loaded', function() {
global $polylang_smart_language_select_disabler;
$polylang_smart_language_select_disabler = new PolylangSmartLanguageSelectDisabler();
} );