wpml-sync-custom-field-note.class.php
2.45 KB
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
<?php
class WPML_Sync_Custom_Field_Note extends WPML_SP_User {
/**
* Prints and admins notice if custom fields where copied to a new post.
*
* @param string $source_lang
* @param int[] $translations
*/
public function print_sync_copy_custom_field_note( $source_lang, $translations ) {
$copied_cf = $this->get_copied_custom_fields( $source_lang, $translations );
if ( ! empty( $copied_cf ) ) {
$lang_details = $this->sitepress->get_language_details( $source_lang );
$user_preferences = $this->sitepress->get_user_preferences();
if ( (bool) $copied_cf === true && ( ! isset( $user_preferences['notices']['hide_custom_fields_copy'] ) || ! $user_preferences['notices']['hide_custom_fields_copy'] ) ) {
$ccf_note = '<i class="otgs-ico-warning"></i> ';
$ccf_note .= wp_nonce_field( 'save_user_preferences_nonce', '_icl_nonce_sup', false, false );
$ccf_note .= sprintf(
esc_html__( 'WPML will copy %s from %s when you save this post.', 'sitepress' ),
'<i><strong>' . join( '</strong>, <strong>', $copied_cf ) . '</strong></i>',
$lang_details['display_name']
);
$ccf_note .= '<a class="icl_user_notice_hide notice-dismiss" href="#hide_custom_fields_copy"><span class="screen-reader-text">'. esc_html__( 'Never show this', 'sitepress' ) . '</span></a>';
$this->sitepress->admin_notices( $ccf_note, 'notice notice-info otgs-notice otgs-is-dismissible' );
}
}
}
/**
* @param string $source_lang
* @param array $translations
*
* @return array
*/
private function get_copied_custom_fields( $source_lang, $translations ) {
$tm_settings = $this->sitepress->get_setting( 'translation-management', array() );
$custom_fields_translation = ! empty( $tm_settings['custom_fields_translation'] )
? (array) $tm_settings['custom_fields_translation'] : array();
$copied_cf = array_keys( (array) $custom_fields_translation, 1 );
$source_lang = $source_lang ? $source_lang : $this->sitepress->get_default_language();
if ( isset( $translations[ $source_lang ] ) ) {
$original_custom = $this->sitepress->get_wp_api()->get_post_custom( $translations[ $source_lang ] );
$copied_cf = (bool) $original_custom
? array_intersect( $copied_cf, array_keys( $original_custom ) ) : array();
$copied_cf = apply_filters(
'icl_custom_fields_to_be_copied',
$copied_cf,
$translations[ $source_lang ]
);
} else {
$copied_cf = array();
}
return $copied_cf;
}
}