first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
<?php
class WPML_TM_Post_Edit_TM_Editor_Mode {
const POST_META_KEY_USE_NATIVE = '_wpml_post_translation_editor_native';
const TM_KEY_FOR_POST_TYPE_USE_NATIVE = 'post_translation_editor_native_for_post_type';
const TM_KEY_GLOBAL_USE_NATIVE = 'post_translation_editor_native';
/**
* Check post meta first
* Then check setting for post type
* Then finally check global setting
*
* @param SitePress $sitepress
* @param $post
*
* @return bool
*/
public static function is_using_tm_editor( SitePress $sitepress, $post_id ) {
$post_id = self::get_source_id( $sitepress, $post_id );
$post_meta = get_post_meta( $post_id, self::POST_META_KEY_USE_NATIVE, true );
if ( 'no' === $post_meta ) {
return true;
} elseif ( 'yes' === $post_meta ) {
return false;
}
$tm_settings = self::init_settings( $sitepress );
$post_type = get_post_type( $post_id );
if ( isset( $tm_settings[ self::TM_KEY_FOR_POST_TYPE_USE_NATIVE ][ $post_type ] ) ) {
return ! $tm_settings[ self::TM_KEY_FOR_POST_TYPE_USE_NATIVE ][ $post_type ];
}
return ! $tm_settings[ self::TM_KEY_GLOBAL_USE_NATIVE ];
}
/**
* @param SitePress $sitepress
* @param int $post_id
*
* @return int
*/
private static function get_source_id( SitePress $sitepress, $post_id ) {
$source_id = $post_id;
$wpml_post_type = 'post_' . get_post_type( $post_id );
$trid = $sitepress->get_element_trid( $post_id, $wpml_post_type );
$translations = $sitepress->get_element_translations( $trid, $wpml_post_type );
if ( ! $translations ) {
return (int) $post_id;
}
foreach ( $translations as $translation ) {
if ( $translation->original ) {
$source_id = $translation->element_id;
break;
}
}
return (int) $source_id;
}
/**
* @param SitePress $sitepress
*
* @return array
*/
private static function init_settings( SitePress $sitepress ) {
$tm_settings = $sitepress->get_setting( 'translation-management' );
/**
* Until a user explicitly change the settings through
* the switcher ( @see WPML_TM_Post_Edit_TM_Editor_Select::save_mode ),
* we'll set it by default at run time:
* - Native editor set to true if using the manual method
* - Native editor set to false otherwise
*/
if ( ! isset( $tm_settings['post_translation_editor_native'] ) ) {
if ( ( (string) ICL_TM_TMETHOD_MANUAL === (string) $tm_settings['doc_translation_method'] ) ) {
$tm_settings['post_translation_editor_native'] = true;
} else {
$tm_settings['post_translation_editor_native'] = false;
}
if ( ! isset( $tm_settings['post_translation_editor_native_for_post_type'] ) ) {
$tm_settings['post_translation_editor_native_for_post_type'] = array();
}
}
return $tm_settings;
}
/**
* @param null|string $post_type
*/
public static function delete_all_posts_option( $post_type = null ) {
global $wpdb;
if ( $post_type ) {
$wpdb->query(
$wpdb->prepare(
"DELETE postmeta FROM {$wpdb->postmeta} AS postmeta
INNER JOIN {$wpdb->posts} AS posts ON posts.ID = postmeta.post_id
WHERE posts.post_type = %s AND postmeta.meta_key = %s",
$post_type,
self::POST_META_KEY_USE_NATIVE
)
);
} else {
delete_post_meta_by_key( self::POST_META_KEY_USE_NATIVE );
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
class WPML_TM_Post_Edit_TM_Editor_Select_Factory implements IWPML_Backend_Action_Loader, IWPML_AJAX_Action_Loader {
public function create() {
global $sitepress;
if (
$sitepress->is_post_edit_screen() ||
wpml_is_ajax() ||
apply_filters( 'wpml_enable_language_meta_box', false )
) {
return new WPML_TM_Post_Edit_TM_Editor_Select( $sitepress );
} else {
return null;
}
}
}

View File

@@ -0,0 +1,162 @@
<?php
class WPML_TM_Post_Edit_TM_Editor_Select implements IWPML_Action {
const SCRIPT_HANDLE = 'wpml-post-edit-tm-mode';
const NONCE_ACTION = 'wpml-tm-editor-mode';
/** @var SitePress $sitepress */
private $sitepress;
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
public function add_hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'wpml_before_post_edit_translations_table', array( $this, 'add_ui' ) );
add_action( 'wpml_before_post_edit_translations_summary', array( $this, 'add_ui' ) );
add_action( 'wp_ajax_wpml-tm-editor-mode', array( $this, 'save_mode' ) );
}
public function enqueue_scripts() {
wp_register_script(
self::SCRIPT_HANDLE,
WPML_TM_URL . '/dist/js/postEditTranslationEditor/app.js',
array( 'jquery-ui-dialog' ),
WPML_TM_VERSION,
true
);
wp_enqueue_script( self::SCRIPT_HANDLE );
wp_enqueue_style( 'otgs-switcher' );
}
public function add_ui( WP_Post $post ) {
/**
* Filters the TM editor select control.
*
* @since 4.5.3
* @internal
*
* @param bool $use_tm_editor Use TM editor.
*/
if ( ! apply_filters( 'wpml_tm_post_edit_tm_editor_selector_display', true ) ) {
return;
}
$this->prevent_displaying_ui_twice();
$is_source = $this->sitepress->is_original_content_filter( false, $post->ID, 'post_' . $post->post_type );
if ( $is_source ) {
$this->add_checkbox( $post );
$this->add_dialog( $post );
}
}
private function prevent_displaying_ui_twice() {
remove_action( 'wpml_before_post_edit_translations_table', array( $this, 'add_ui' ) );
remove_action( 'wpml_before_post_edit_translations_summary', array( $this, 'add_ui' ) );
}
private function add_checkbox( WP_Post $post ) {
$wpml_tm_editor = esc_html__( "Use WPML's Translation Editor", 'wpml-translation-management' );
$checked = WPML_TM_Post_Edit_TM_Editor_Mode::is_using_tm_editor( $this->sitepress, $post->ID ) ? 'checked' : '';
?>
<p class="otgs-toggle-group">
<input type="checkbox" class="js-toggle-tm-editor otgs-switcher-input" id="wpml_tm_toggle_tm_editor" <?php echo $checked; ?>>
<label for="wpml_tm_toggle_tm_editor" class="otgs-switcher wpml-theme" data-on="ON" data-off="OFF"><?php echo $wpml_tm_editor; ?></label>
</p>
<?php
}
private function add_dialog( WP_Post $post ) {
global $wp_post_types;
$wpml_tm_editor = esc_html__( "Use WPML's Translation Editor to translate:", 'wpml-translation-management' );
$wp_editor = esc_html__( "Use the WordPress Editor to translate:", 'wpml-translation-management' );
$cancel_text = esc_html__( 'Cancel', 'wpml-translation-management' );
$apply_text = esc_html__( 'Apply', 'wpml-translation-management' );
$post_label_singular = sprintf(
/* translators: %s: Post name singular */
esc_html__( 'This %s', 'wpml-translation-management' ),
$wp_post_types[ $post->post_type ]->labels->singular_name
);
$post_label_plural = sprintf(
/* translators: %s: Post name plural */
esc_html__( 'All %s', 'wpml-translation-management' ),
$wp_post_types[ $post->post_type ]->labels->name
);
$all_site_content = esc_html__( "All the site's content", 'wpml-translation-management' );
$nonce = wp_create_nonce( self::NONCE_ACTION );
?>
<div
id="js-tm-editor-dialog"
data-tm-editor="<?php echo $wpml_tm_editor; ?>"
data-wp-editor="<?php echo $wp_editor; ?>"
data-cancel-text="<?php echo $cancel_text; ?>"
data-apply-text="<?php echo $apply_text; ?>"
data-nonce="<?php echo $nonce; ?>"
style="display: none"
>
<label for="tm-editor-this">
<input type="radio" id="tm-editor-this" name="tm-editor-mode" value="this_post"
checked/><?php echo $post_label_singular; ?>
</label>
<br/>
<label for="tm-editor-all">
<input type="radio" id="tm-editor-all" name="tm-editor-mode"
value="all_posts_of_type"/><?php echo $post_label_plural; ?>
</label>
<br/>
<label for="tm-editor-global">
<input type="radio" id="tm-editor-global" name="tm-editor-mode"
value="global"/><?php echo $all_site_content; ?>
</label>
</div>
<?php
}
public function save_mode() {
if ( wp_verify_nonce( $_POST['nonce'], self::NONCE_ACTION ) ) {
$tm_settings = $this->sitepress->get_setting( 'translation-management' );
$use_native_editor = 'false' === $_POST['useTMEditor'];
$post_id = filter_var( $_POST['postId'], FILTER_SANITIZE_NUMBER_INT );
switch ( $_POST['mode'] ) {
case 'global':
$tm_settings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_GLOBAL_USE_NATIVE ] = $use_native_editor;
$tm_settings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_FOR_POST_TYPE_USE_NATIVE ] = array();
$this->sitepress->set_setting( 'translation-management', $tm_settings, true );
WPML_TM_Post_Edit_TM_Editor_Mode::delete_all_posts_option();
break;
case 'all_posts_of_type':
$post_type = get_post_type( $post_id );
$tm_settings[ WPML_TM_Post_Edit_TM_Editor_Mode::TM_KEY_FOR_POST_TYPE_USE_NATIVE ][ $post_type ] = $use_native_editor;
$this->sitepress->set_setting( 'translation-management', $tm_settings, true );
WPML_TM_Post_Edit_TM_Editor_Mode::delete_all_posts_option( $post_type );
break;
case 'this_post':
update_post_meta(
$post_id,
WPML_TM_Post_Edit_TM_Editor_Mode::POST_META_KEY_USE_NATIVE,
$use_native_editor ? 'yes' : 'no'
);
break;
}
}
wp_send_json_success();
}
}