123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_WPML_Compatibility' ) ) :
- class ACF_WPML_Compatibility {
-
- function __construct() {
-
- global $sitepress;
-
- acf_update_setting( 'default_language', $sitepress->get_default_language() );
- acf_update_setting( 'current_language', $sitepress->get_current_language() );
-
- acf_localize_data(
- array(
- 'language' => $sitepress->get_current_language(),
- )
- );
-
- add_action( 'acf/verify_ajax', array( $this, 'verify_ajax' ) );
-
- add_filter( 'get_translatable_documents', array( $this, 'get_translatable_documents' ) );
-
- if ( $this->is_translatable() ) {
-
- add_action( 'acf/upgrade_500_field_group', array( $this, 'upgrade_500_field_group' ), 10, 2 );
- add_action( 'icl_make_duplicate', array( $this, 'icl_make_duplicate' ), 10, 4 );
-
- add_filter( 'acf/settings/save_json', array( $this, 'settings_save_json' ) );
- add_filter( 'acf/settings/load_json', array( $this, 'settings_load_json' ) );
- }
- }
-
- function is_translatable() {
-
- global $sitepress;
-
- $post_types = $sitepress->get_setting( 'custom_posts_sync_option' );
-
- if ( ! acf_is_array( $post_types ) ) {
- return false;
- }
-
- if ( ! empty( $post_types['acf-field'] ) ) {
- $post_types['acf-field'] = 0;
- $sitepress->set_setting( 'custom_posts_sync_option', $post_types );
- }
-
-
- if ( ! empty( $post_types['acf'] ) && ! isset( $post_types['acf-field-group'] ) ) {
- $post_types['acf-field-group'] = 1;
- $sitepress->set_setting( 'custom_posts_sync_option', $post_types );
- }
-
- if ( ! empty( $post_types['acf-field-group'] ) ) {
- return true;
- }
-
- return false;
- }
-
- function upgrade_500_field_group( $field_group, $ofg ) {
-
- global $wpdb;
-
- $old_row = $wpdb->get_row(
- $wpdb->prepare(
- "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
- 'post_acf',
- $ofg->ID
- ),
- ARRAY_A
- );
- $new_row = $wpdb->get_row(
- $wpdb->prepare(
- "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
- 'post_acf-field-group',
- $field_group['ID']
- ),
- ARRAY_A
- );
-
- if ( ! $old_row || ! $new_row ) {
- return;
- }
-
-
- if ( empty( $this->trid_ref ) ) {
- $this->trid_ref = array();
- }
-
- if ( isset( $this->trid_ref[ $old_row['trid'] ] ) ) {
-
- $new_row['trid'] = $this->trid_ref[ $old_row['trid'] ];
- } else {
-
- $this->trid_ref[ $old_row['trid'] ] = $new_row['trid'];
- }
-
-
- $table = "{$wpdb->prefix}icl_translations";
- $data = array(
- 'trid' => $new_row['trid'],
- 'language_code' => $old_row['language_code'],
- );
- $where = array( 'translation_id' => $new_row['translation_id'] );
- $data_format = array( '%d', '%s' );
- $where_format = array( '%d' );
-
- if ( $old_row['source_language_code'] ) {
- $data['source_language_code'] = $old_row['source_language_code'];
- $data_format[] = '%s';
- }
-
- $result = $wpdb->update( $table, $data, $where, $data_format, $where_format );
- }
-
- function settings_save_json( $path ) {
-
- if ( ! is_writable( $path ) ) {
- return $path;
- }
-
- $path = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' );
-
- if ( ! file_exists( $path ) ) {
- mkdir( $path, 0777, true );
- }
-
- return $path;
- }
-
- function settings_load_json( $paths ) {
-
- if ( $paths ) {
- foreach ( $paths as $i => $path ) {
- $paths[ $i ] = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' );
- }
- }
-
- return $paths;
- }
-
- function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) {
-
- if ( $postarr['post_type'] != 'acf-field-group' ) {
- return;
- }
-
- acf_update_setting( 'current_language', $lang );
-
- acf_duplicate_field_group( $master_post_id, $id );
-
-
-
- global $iclTranslationManagement;
- $iclTranslationManagement->reset_duplicate_flag( $id );
- }
-
- function verify_ajax() {
-
-
-
- if ( isset( $_REQUEST['lang'] ) ) {
- global $sitepress;
- $sitepress->switch_lang( sanitize_text_field( $_REQUEST['lang'] ) );
- }
-
- }
-
- function get_translatable_documents( $icl_post_types ) {
-
- unset( $icl_post_types['acf-field'] );
-
- return $icl_post_types;
- }
- }
- acf_new_instance( 'ACF_WPML_Compatibility' );
- endif;
|