-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfp-translation-enabler.php
110 lines (93 loc) · 3.45 KB
/
tfp-translation-enabler.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
<?php
/**
* This file is responsible for compact management of the Displaying translation options in the front-end.
* Works with conjunction of extensions-for-pressbooks plugin v1.2.4
* Creates setting section in EFP Customization menu and 'Display post translations' metabox in post-edit page.
* Also is responsible for tfp_generatePostTranslationEntries in the DB.
*
* @package translations for pressbooks
* @since 1.2.6
*
*/
defined ("ABSPATH") or die ("Action denied!");
if ((1 != get_current_blog_id() || !is_multisite()) && is_plugin_active('pressbooks/pressbooks.php')){
if (is_plugin_active('extensions-for-pressbooks/extensions-for-pressbooks.php')) {
add_action('admin_init','tfp_initBookTransSection');
}
if ( "1" == get_option( 'tfp_book_translation_enable' )){
// If 'Display translations' checkbox enabled render metabox in post-edit page (chapters, book-info...).
add_action('admin_menu', 'tfp_initPostTranslationsSection');
}
}
/* --- BOOK translations --- */
/**
* Render page sections.
*
* @since 1.2.6
*
*/
function tfp_initBookTransSection(){
add_settings_section( 'translations_section',
'Translations section',
'',
'theme-customizations');
add_settings_field( 'tfp_book_translation_enable',
'Display translations menu',
'tfp_bookTranslationCallback',
'theme-customizations',
'translations_section'); //add settings field to the translations_section
add_option( 'tfp_book_translation_enable',0); // add theme option to database
register_setting( 'theme-customizations-grp',
'tfp_book_translation_enable');
}
/**
* Render page 'Display translation' checkbox.
*
* @since 1.2.6
*
*/
function tfp_bookTranslationCallback(){
$option = get_option( 'tfp_book_translation_enable' );
$toprint = '<input name="tfp_book_translation_enable" id="tfp_book_translation_enable" type="checkbox" value="1" class="code" ' . checked( 1, $option, false ) . ' /> Enable translations in front-end <br> <i>If the book is not featured, translations will not point to this book.</i>' ;
echo $toprint;
}
/* --- POST-edit page translations --- */
/**
* Initializes metabox in post-edit page.
*
* @since 1.2.6
*
*/
function tfp_initPostTranslationsSection () {
$post_types = ['metadata','front-matter','chapter','part', 'back-matter'];
add_meta_box( 'tre_translation_checkbox', 'Display post translations', 'tfp_renderPostMetabox', $post_types, 'side', 'low');
}
/**
* Renders metabox in post-edit page.
*
* @since 1.2.6
*
*/
function tfp_renderPostMetabox(){
global $post;
$option = get_post_meta($post->ID, 'tfp_post_translation_disable', true);
echo '<input name="tfp_post_translation_disable" id="tfp_post_translation_disable" type="checkbox" value="1" class="code" ' . checked( 1, $option, false ) . ' /> Disable for this post.';
}
/* --- RELATED FUNCTIONS --- */
/**
* Save post option from 'Post translations display' checkbox on post-edit page.
*
* @since 1.2.6
*
*/
function tfp_savePostTranslationOption() {
global $post;
if(isset($post)){
$post_type = get_post_type($post->ID );
}
if (isset($post_type)){
$post_trans_state = !empty($_POST['tfp_post_translation_disable']) ? tfp_sanitize_checkbox($_POST['tfp_post_translation_disable'], '1', '0') : '';
update_post_meta($post->ID, "tfp_post_translation_disable", $post_trans_state);
}
}
add_action('save_post', 'tfp_savePostTranslationOption');