-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.php
287 lines (243 loc) · 12 KB
/
settings.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* Generated by the WordPress Option Page generator
* at http://jeremyhixon.com/wp-tools/option-page/
*/
$pllx_options_original = [
'feature_translate_cat_in_menus' => 'true',
'feature_replace_front_page' => 'true',
'feature_category_featured_post' => 'true',
'feature_remove_duplicate_posts' => 'false',
'language_behaviour' => 'browser',
'forced_languages' => '',
'use_current_language' => 'false',
'custom_post_types' => 'post,page',
];
$pllx_options_recommended = [
'feature_translate_cat_in_menus' => 'false',
'feature_replace_front_page' => 'true',
'feature_category_featured_post' => 'true',
'feature_remove_duplicate_posts' => 'true',
'language_behaviour' => 'all',
'forced_languages' => 'en',
'use_current_language' => 'true',
'custom_post_types' => 'post,page',
];
$pllx_options_defaults = $pllx_options_recommended;
//$pllx_options_defaults = $pllx_options_original;
/*
* Retrieve this value with:
* $language_mix_options = get_option( 'language_mix_option_name' ); // Array of All Options
* $language_behaviour = $language_mix_options['language_behaviour']; // Language behavior
* $forced_languages = $language_mix_options['forced_languages']; // Forced languages
* $use_current_language = $language_mix_options['use_current_language']; // Use current language
* $custom_post_types = $language_mix_options['custom_post_types']; // Custom post types
*/
class LanguageMix {
private $language_mix_options;
public function __construct() {
add_action( 'admin_menu', array( $this, 'language_mix_add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'language_mix_page_init' ) );
}
public function language_mix_add_plugin_page() {
add_options_page(
__('Language Mix', 'language-mix'), // page_title
__('Language Mix', 'language-mix'), // menu_title
'manage_options', // capability
'language-mix', // menu_slug
array( $this, 'language_mix_create_admin_page' ) // function
);
}
public function language_mix_create_admin_page() {
$this->language_mix_options = get_option( 'language_mix_option_name' ); ?>
<div class="wrap">
<h2>Language Mix</h2>
<p></p>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'language_mix_option_group' );
do_settings_sections( 'language-mix-admin' );
submit_button();
?>
</form>
</div>
<?php }
public function language_mix_page_init() {
global $pllx_options_defaults;
register_setting(
'language_mix_option_group', // option_group
'language_mix_option_name', // option_name
[
'sanitize_callback' => array( $this, 'language_mix_sanitize' ),
'default' => $pllx_options_defaults,
]
);
add_settings_section(
'language_mix_setting_section', // id
__('Settings', 'language-mix'), // title
array( $this, 'language_mix_section_info' ), // callback
'language-mix-admin' // page
);
add_settings_field(
'feature_translate_cat_in_menus', // id
__('Translate categories in menus', 'language-mix'), // title
array( $this, 'feature_translate_cat_in_menus_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'feature_replace_front_page', // id
__('Replacing front page', 'language-mix'), // title
array( $this, 'feature_replace_front_page_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'feature_category_featured_post', // id
__('Category featured post', 'language-mix'), // title
array( $this, 'feature_category_featured_post_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'feature_remove_duplicate_posts', // id
__('Hide redundant translations', 'language-mix'), // title
array( $this, 'feature_remove_duplicate_posts_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'language_behaviour', // id
__('Language behavior', 'language-mix'), // title
array( $this, 'language_behaviour_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'forced_languages', // id
__('Forced languages', 'language-mix'), // title
array( $this, 'forced_languages_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'use_current_language', // id
__('Use current language', 'language-mix'), // title
array( $this, 'use_current_language_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
add_settings_field(
'custom_post_types', // id
__('Custom post types', 'language-mix'), // title
array( $this, 'custom_post_types_callback' ), // callback
'language-mix-admin', // page
'language_mix_setting_section' // section
);
}
public function language_mix_sanitize($input) {
$sanitary_values = array();
if ( isset( $input['language_behaviour'] ) ) {
$sanitary_values['language_behaviour'] = $input['language_behaviour'];
}
if ( isset( $input['forced_languages'] ) ) {
$sanitary_values['forced_languages'] = sanitize_text_field( $input['forced_languages'] );
}
if ( isset( $input['use_current_language'] ) ) {
$sanitary_values['use_current_language'] = $input['use_current_language'];
}
if ( isset( $input['custom_post_types'] ) ) {
$sanitary_values['custom_post_types'] = sanitize_text_field( $input['custom_post_types'] );
}
if ( isset( $input['feature_translate_cat_in_menus'] ) ) {
$sanitary_values['feature_translate_cat_in_menus'] = sanitize_text_field( $input['feature_translate_cat_in_menus'] );
}
if ( isset( $input['feature_replace_front_page'] ) ) {
$sanitary_values['feature_replace_front_page'] = sanitize_text_field( $input['feature_replace_front_page'] );
}
if ( isset( $input['feature_category_featured_post'] ) ) {
$sanitary_values['feature_category_featured_post'] = sanitize_text_field( $input['feature_category_featured_post'] );
}
if ( isset( $input['feature_remove_duplicate_posts'] ) ) {
$sanitary_values['feature_remove_duplicate_posts'] = sanitize_text_field( $input['feature_remove_duplicate_posts'] );
}
return $sanitary_values;
}
public function language_mix_section_info() {
global $pllx_options_defaults;
global $pllx_options_original;
global $pllx_options_recommended;
?>
<script type="text/javascript" >
function pllx_set_defaults(defaults) {
Object.keys(defaults).forEach(function(key) {
if (jQuery('#'+key).is(':checkbox')) {
jQuery('#'+key).prop('checked', (defaults[key] ==='true') );
} else {
jQuery('#'+key).val(defaults[key]);
}
});
}
</script>
<a href="#" class="button" onclick='javascript: pllx_set_defaults(<?php echo json_encode($pllx_options_defaults) ?>);'>
<?php echo __('Reset to default', 'language-mix'); ?></a>
<a href="#" class="button" onclick='javascript: pllx_set_defaults(<?php echo json_encode($pllx_options_original) ?>);'>
<?php echo __('Set to original settings', 'language-mix'); ?></a>
<a href="#" class="button" onclick='javascript: pllx_set_defaults(<?php echo json_encode($pllx_options_recommended) ?>);'>
<?php echo __('Set to recommended settings', 'language-mix'); ?></a>
<?php
}
public function language_behaviour_callback() {
?> <fieldset><?php $checked = ( isset( $this->language_mix_options['language_behaviour'] ) && $this->language_mix_options['language_behaviour'] === 'all' ) ? 'checked' : '' ; ?>
<label for="language_behaviour-0"><input type="radio" name="language_mix_option_name[language_behaviour]" id="language_behaviour-0" value="all" <?php echo $checked; ?>> <?php echo __('All languages', 'language-mix'); ?></label><br>
<?php $checked = ( isset( $this->language_mix_options['language_behaviour'] ) && $this->language_mix_options['language_behaviour'] === 'browser' ) ? 'checked' : '' ; ?>
<label for="language_behaviour-1"><input type="radio" name="language_mix_option_name[language_behaviour]" id="language_behaviour-1" value="browser" <?php echo $checked; ?>> <?php echo __('Only the languages allowed by the browser', 'language-mix'); ?></label><br>
<?php $checked = ( isset( $this->language_mix_options['language_behaviour'] ) && $this->language_mix_options['language_behaviour'] === 'override' ) ? 'checked' : '' ; ?>
<label for="language_behaviour-2"><input type="radio" name="language_mix_option_name[language_behaviour]" id="language_behaviour-2" value="override" <?php echo $checked; ?>> <?php echo __('The languages allowed by the browser plus the ones that are forced', 'language-mix'); ?></label></fieldset> <?php
}
public function forced_languages_callback() {
printf(
'<input class="regular-text" type="text" name="language_mix_option_name[forced_languages]" id="forced_languages" value="%s">',
isset( $this->language_mix_options['forced_languages'] ) ? esc_attr( $this->language_mix_options['forced_languages']) : ''
);
}
public function use_current_language_callback() {
printf(
'<input type="checkbox" name="language_mix_option_name[use_current_language]" id="use_current_language" value="true" %s> <label for="use_current_language">' . __('Allow always the current language with higher priority', 'language-mix') . '</label>',
( isset( $this->language_mix_options['use_current_language'] ) && $this->language_mix_options['use_current_language'] === 'true' ) ? 'checked' : ''
);
}
public function custom_post_types_callback() {
printf(
'<input class="regular-text" type="text" name="language_mix_option_name[custom_post_types]" id="custom_post_types" value="%s">',
isset( $this->language_mix_options['custom_post_types'] ) ? esc_attr( $this->language_mix_options['custom_post_types']) : ''
);
}
public function feature_translate_cat_in_menus_callback() {
printf(
'<input type="checkbox" name="language_mix_option_name[feature_translate_cat_in_menus]" id="feature_translate_cat_in_menus" value="true" %s> <label for="feature_translate_cat_in_menus">' . __('Enable translating categories in menu according to the most wanted language', 'language-mix') . '</label>',
( isset( $this->language_mix_options['feature_translate_cat_in_menus'] ) && $this->language_mix_options['feature_translate_cat_in_menus'] === 'true' ) ? 'checked' : ''
);
}
public function feature_category_featured_post_callback() {
printf(
'<input type="checkbox" name="language_mix_option_name[feature_category_featured_post]" id="feature_category_featured_post" value="true" %s> <label for="feature_category_featured_post">' . __('Enable replacing translation for featured post of a category', 'language-mix') . '</label>',
( isset( $this->language_mix_options['feature_category_featured_post'] ) && $this->language_mix_options['feature_category_featured_post'] === 'true' ) ? 'checked' : ''
);
}
public function feature_replace_front_page_callback() {
printf(
'<input type="checkbox" name="language_mix_option_name[feature_replace_front_page]" id="feature_replace_front_page" value="true" %s> <label for="feature_replace_front_page">' . __('Enable replacing the front page by the most wanted language translation.', 'language-mix') . '</label>',
( isset( $this->language_mix_options['feature_replace_front_page'] ) && $this->language_mix_options['feature_replace_front_page'] === 'true' ) ? 'checked' : ''
);
}
public function feature_remove_duplicate_posts_callback() {
printf(
'<input type="checkbox" name="language_mix_option_name[feature_remove_duplicate_posts]" id="feature_remove_duplicate_posts" value="true" %s> <label for="feature_remove_duplicate_posts">' . __('Enable hiding other translations than the most wanted one for a post, thus avoiding duplicate contents.', 'language-mix') . '</label>',
( isset( $this->language_mix_options['feature_remove_duplicate_posts'] ) && $this->language_mix_options['feature_remove_duplicate_posts'] === 'true' ) ? 'checked' : ''
);
}
}
if ( is_admin() )
$language_mix = new LanguageMix();