| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
class DeepL_Metabox { |
| 4 |
protected $metabox_config = array(); |
| 5 |
|
| 6 |
function __construct() { |
| 7 |
// adding the box |
| 8 |
add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) ); |
| 9 |
|
| 10 |
// adding the javascript footer |
| 11 |
//add_action( 'admin_footer', array( &$this, 'deepl_admin_footer_javascript' ) ); |
| 12 |
|
| 13 |
} |
| 14 |
|
| 15 |
public function add_meta_box() { |
| 16 |
$post_types = DeepLConfiguration::getMetaBoxPostTypes(); |
| 17 |
//plouf( $post_types); wpdeepl_debug_display($post_types, " context = " . DeepLConfiguration::getMetaBoxContext() . " prio =" . DeepLConfiguration::getMetaBoxPriority() ); |
| 18 |
|
| 19 |
|
| 20 |
add_meta_box( |
| 21 |
'deepl_metabox', |
| 22 |
__( 'Translation with DeepL', 'wpdeepl' ), |
| 23 |
array( &$this, 'output' ), |
| 24 |
$post_types, |
| 25 |
DeepLConfiguration::getMetaBoxContext(), |
| 26 |
DeepLConfiguration::getMetaBoxPriority() |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function output() { |
| 31 |
|
| 32 |
|
| 33 |
global $post; |
| 34 |
|
| 35 |
$nonce_action = DeepLConfiguration::getNonceAction(); |
| 36 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 37 |
$action = admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ); |
| 38 |
|
| 39 |
$default_behaviour = DeepLConfiguration::getMetaBoxDefaultBehaviour(); |
| 40 |
$default_metabox_behaviours = DeepLConfiguration::DefaultsMetaboxBehaviours(); |
| 41 |
|
| 42 |
//var_dump(DeeplConfiguration::usingMultilingualPlugins()); |
| 43 |
if ( !DeeplConfiguration::usingMultilingualPlugins() ) { |
| 44 |
$default_behaviour = 'replace'; |
| 45 |
} |
| 46 |
if ( !$default_behaviour ) { |
| 47 |
$default_behaviour = 'replace'; |
| 48 |
} |
| 49 |
|
| 50 |
global $pagenow; |
| 51 |
|
| 52 |
if( $pagenow == 'post-new.php' ) { |
| 53 |
esc_html_e('Please save or publish the post first', 'wpdeepl' ); |
| 54 |
return; |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
add_action('admin_action_' . $action, 'deepl_translate_post_link' ); |
| 59 |
|
| 60 |
$target_lang = get_option( 'deepl_default_locale'); |
| 61 |
if( function_exists( 'pll_current_language' ) ) { |
| 62 |
$current_language = pll_current_language(); |
| 63 |
if( $current_language ) { |
| 64 |
$target_lang = DeepLConfiguration::getLanguageFromIsoCode2( $current_language ); |
| 65 |
} |
| 66 |
else { |
| 67 |
$terms = wp_get_post_terms( $post->ID,'language' ); |
| 68 |
if( $terms ) { |
| 69 |
$language = $terms[0]; |
| 70 |
$target_lang = DeepLConfiguration::getLanguageFromIsoCode2( $language->slug ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$html = ' |
| 77 |
<input type="hidden" id="wpdeepl_action" name="wpdeepl_action" value="' . esc_attr( $action ) .'" /> |
| 78 |
<input type="hidden" id="wpdeepl_force_polylang" name="wpdeepl_force_polylang" value="1" /> |
| 79 |
' . wp_nonce_field( $nonce_action, '_wpdeeplnonce', false, false ) .' |
| 80 |
' . wpdeepl_language_selector( 'source', 'wpdeepl_source_lang', false ) . ' |
| 81 |
<br />' . esc_html__( 'Translating to', 'wpdeepl' ) . '<br /> |
| 82 |
' . wpdeepl_language_selector( 'target', 'wpdeepl_target_lang', $target_lang ) . ' |
| 83 |
<span id="wpdeepl_error" class="error" style="display: none;"></span> |
| 84 |
<input id="deepl_translate_do" type="submit" class="button button-primary button-large" value="' . esc_attr__( 'Translate' , 'wpdeepl' ) . '"> |
| 85 |
<hr />'; |
| 86 |
|
| 87 |
|
| 88 |
foreach ( $default_metabox_behaviours as $value => $label ) { |
| 89 |
$html.= ' |
| 90 |
<span style="display: block;"> |
| 91 |
<input type="radio" name="wpdeepl_replace" value="'. $value .'"'; |
| 92 |
|
| 93 |
if ( $value == $default_behaviour ) { |
| 94 |
$html .= ' checked="checked"'; |
| 95 |
} |
| 96 |
if ( $value == 'append' && !DeeplConfiguration::usingMultilingualPlugins() ) { |
| 97 |
$html .= ' disabled="disabled"'; |
| 98 |
} |
| 99 |
$html .= '> |
| 100 |
<label for="wpdeepl_replace">' . $label . '</label> |
| 101 |
</span>'; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
//</form> |
| 106 |
$html .= ' |
| 107 |
'; |
| 108 |
|
| 109 |
$html = apply_filters( 'deepl_metabox_html', $html); |
| 110 |
// Allow form elements for metabox functionality |
| 111 |
$allowed_tags = array_merge( |
| 112 |
wp_kses_allowed_html( 'post' ), |
| 113 |
array( |
| 114 |
'input' => array( |
| 115 |
'type' => true, |
| 116 |
'id' => true, |
| 117 |
'name' => true, |
| 118 |
'value' => true, |
| 119 |
'checked' => true, |
| 120 |
'disabled' => true, |
| 121 |
'class' => true, |
| 122 |
), |
| 123 |
'select' => array( |
| 124 |
'id' => true, |
| 125 |
'name' => true, |
| 126 |
'class' => true, |
| 127 |
), |
| 128 |
'option' => array( |
| 129 |
'value' => true, |
| 130 |
'selected' => true, |
| 131 |
), |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
echo wp_kses( $html, $allowed_tags ); |
| 136 |
} |
| 137 |
} |