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,73 @@
<?php
class WPML_Copy_Once_Custom_Field implements IWPML_Backend_Action, IWPML_Frontend_Action, IWPML_DIC_Action {
/** @var SitePress $sitepress */
private $sitepress;
/** @var WPML_Post_Translation $wpml_post_translation */
private $wpml_post_translation;
/**
* WPML_Copy_Once_Custom_Field constructor.
*
* @param SitePress $sitepress
* @param WPML_Post_Translation $wpml_post_translation
*/
public function __construct( SitePress $sitepress, WPML_Post_Translation $wpml_post_translation ) {
$this->sitepress = $sitepress;
$this->wpml_post_translation = $wpml_post_translation;
}
public function add_hooks() {
add_action( 'wpml_after_save_post', array( $this, 'copy' ), 10, 1 );
add_action( 'wpml_pro_translation_completed', array( $this, 'copy' ), 10, 1 );
}
/**
* @param int $post_id
*/
public function copy( $post_id ) {
$custom_fields_to_copy = $this->sitepress->get_custom_fields_translation_settings( WPML_COPY_ONCE_CUSTOM_FIELD );
if ( empty( $custom_fields_to_copy ) ) {
return;
}
$source_element_id = $this->wpml_post_translation->get_original_element( $post_id );
$custom_fields = get_post_meta( $post_id );
foreach ( $custom_fields_to_copy as $meta_key ) {
$values = isset( $custom_fields[ $meta_key ] ) ? $custom_fields[ $meta_key ] : [];
if ( ! empty( $values ) ) {
$values = array( $values );
}
/**
* Custom fields values for given post obtained directly from database
*
* @since 4.1
*
* @param array<mixed> $values Custom fields values as they are in the database
* @param array<int|string> $args {
* @type int $post_id ID of post associated with custom field
* @type string $meta_key custom fields meta key
* @type int $custom_fields_translation field translation option
*
* }
*/
$values = apply_filters(
'wpml_custom_field_values',
$values,
[
'post_id' => $post_id,
'meta_key' => $meta_key,
'custom_fields_translation' => WPML_COPY_ONCE_CUSTOM_FIELD,
]
);
if ( empty( $values ) && $source_element_id ) {
$this->sitepress->sync_custom_field( $source_element_id, $post_id, $meta_key );
}
}
}
}

View File

@@ -0,0 +1,12 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Custom_Fields_Post_Meta_Info_Factory implements IWPML_AJAX_Action_Loader, IWPML_Backend_Action_Loader {
public function create() {
global $sitepress;
$translatable_element_factory = new WPML_Translation_Element_Factory( $sitepress );
return new WPML_Custom_Fields_Post_Meta_Info( $translatable_element_factory );
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* @author OnTheGo Systems
*/
class WPML_Custom_Fields_Post_Meta_Info implements IWPML_Action {
const RESOURCES_HANDLE = 'wpml-cf-info';
const AJAX_ACTION = 'wpml-cf-info-get';
private $translatable_element_factory;
/**
* WPML_Custom_Fields_Post_Meta_Info constructor.
*
* @param WPML_Translation_Element_Factory $translatable_element_factory
*/
public function __construct( WPML_Translation_Element_Factory $translatable_element_factory ) {
$this->translatable_element_factory = $translatable_element_factory;
}
public function add_hooks() {
add_action( 'wp_ajax_wpml_cf_get_info', array( $this, 'get_info_ajax' ) );
add_filter( 'wpml_custom_field_original_data', array( $this, 'get_info_filter' ), 10, 3 );
}
public function get_info_ajax() {
$result = null;
if ( check_ajax_referer( self::AJAX_ACTION, 'nonceGet', false ) ) {
$meta_id = filter_var( $_GET['meta_id'], FILTER_SANITIZE_NUMBER_INT );
if ( $meta_id ) {
/** @var \stdClass $custom_field */
$custom_field = get_post_meta_by_id( $meta_id );
if ( $custom_field ) {
$post_id = $custom_field->post_id;
$meta_key = $custom_field->meta_key;
$result = $this->get_info( $post_id, $meta_key );
if ( $result ) {
wp_send_json_success( $result );
return;
}
} else {
$result = 'Custom field not found';
}
} else {
$result = 'Missing meta_id';
}
}
wp_send_json_error( $result );
}
public function get_info_filter( $ignore, $post_id, $meta_key ) {
return $this->get_info( $post_id, $meta_key );
}
private function get_info( $post_id, $meta_key ) {
$post_element = $this->translatable_element_factory->create( $post_id, 'post' );
$original_element = $post_element->get_source_element();
if ( $original_element && $original_element->get_id() !== $post_element->get_id() ) {
return array(
'meta_key' => $meta_key,
'value' => get_post_meta( $original_element->get_id(), $meta_key, true )
);
}
return null;
}
}

View File

@@ -0,0 +1,98 @@
<?php
class WPML_Sync_Custom_Fields {
/** @var WPML_Translation_Element_Factory $element_factory */
private $element_factory;
/** @var array $fields_to_sync */
private $fields_to_sync;
/**
* WPML_Sync_Custom_Fields constructor.
*
* @param WPML_Translation_Element_Factory $element_factory
* @param array $fields_to_sync
*/
public function __construct( WPML_Translation_Element_Factory $element_factory, array $fields_to_sync ) {
$this->element_factory = $element_factory;
$this->fields_to_sync = $fields_to_sync;
}
/**
* @param int $post_id_from
* @param string $meta_key
*/
public function sync_to_translations( $post_id_from, $meta_key ) {
if ( in_array( $meta_key, $this->fields_to_sync, true ) ) {
$post_element = $this->element_factory->create( $post_id_from, 'post' );
$translations = $post_element->get_translations();
foreach ( $translations as $translation ) {
$translation_id = $translation->get_element_id();
if ( $translation_id !== $post_id_from ) {
$this->sync_custom_field( $post_id_from, $translation_id, $meta_key );
}
}
}
}
/**
* @param int $post_id_from
*/
public function sync_all_custom_fields( $post_id_from ) {
foreach ( $this->fields_to_sync as $meta_key ) {
$this->sync_to_translations( $post_id_from, $meta_key );
}
}
/**
* @param int $post_id_from
* @param int $post_id_to
* @param string $meta_key
*/
public function sync_custom_field( $post_id_from, $post_id_to, $meta_key ) {
$custom_fields_from = get_post_meta( $post_id_from );
$custom_fields_to = get_post_meta( $post_id_to );
$values_from = isset( $custom_fields_from[ $meta_key ] ) ? $custom_fields_from[ $meta_key ] : [];
$values_to = isset( $custom_fields_to[ $meta_key ] ) ? $custom_fields_to[ $meta_key ] : [];
$removed = array_diff( $values_to, $values_from );
foreach ( $removed as $v ) {
delete_post_meta( $post_id_to, $meta_key, maybe_unserialize( $v ) );
}
$added = array_diff( $values_from, $values_to );
$args = [
'values_from' => $values_from,
'values_to' => $values_to,
'removed' => $removed,
'added' => $added,
];
foreach ( $added as $v ) {
$copied_value = maybe_unserialize( $v );
/**
* Filters the $copied_value of $meta_key which will be copied from $post_id_from to $post_id_to
*
* @param mixed $copied_value The unserialized and slashed value.
* @param int $post_id_from The ID of the source post.
* @param int $post_id_to The ID of the destination post.
* @param string $meta_key The key of the post meta being copied.
* @param array $args The internal parameters.
*
* @since 4.3.0
*/
$copied_value = apply_filters( 'wpml_sync_custom_field_copied_value', $copied_value, $post_id_from, $post_id_to, $meta_key, $args );
$copied_value = wp_slash( $copied_value );
add_post_meta( $post_id_to, $meta_key, $copied_value );
}
do_action( 'wpml_after_copy_custom_field', $post_id_from, $post_id_to, $meta_key );
}
}

View File

@@ -0,0 +1,19 @@
<?php
class WPML_Translate_Link_Targets_In_Custom_Fields_Hooks {
/**
* WPML_Translate_Link_Targets_In_Custom_Fields_Hook constructor.
*
* @param WPML_Translate_Link_Targets_In_Custom_Fields $translate_links
* @param WPML_WP_API $wp_api
*/
public function __construct( $translate_links, &$wp_api ) {
if ( $translate_links->has_meta_keys() ) {
$wp_api->add_filter( 'get_post_metadata', array( $translate_links, 'maybe_translate_link_targets' ), 10, 4 );
}
}
}

View File

@@ -0,0 +1,97 @@
<?php
class WPML_Translate_Link_Targets_In_Custom_Fields extends WPML_Translate_Link_Targets {
/* @var TranslationManagement $tm_instance */
private $tm_instance;
/* @var WPML_WP_API $wp_api */
private $wp_api;
/* @var array $meta_keys */
private $meta_keys;
/**
* WPML_Translate_Link_Targets_In_Custom_Fields constructor.
*
* @param TranslationManagement $tm_instance
* @param WPML_WP_API $wp_api
* @param AbsoluteLinks $absolute_links
* @param WPML_Absolute_To_Permalinks $permalinks_converter
*/
public function __construct( &$tm_instance, &$wp_api, $absolute_links, $permalinks_converter ) {
parent::__construct( $absolute_links, $permalinks_converter );
$this->tm_instance = &$tm_instance;
$this->wp_api = &$wp_api;
$this->tm_instance->load_settings_if_required();
if ( isset( $this->tm_instance->settings['custom_fields_translate_link_target'] ) &&
! empty( $this->tm_instance->settings['custom_fields_translate_link_target'] ) ) {
$this->meta_keys = $this->tm_instance->settings['custom_fields_translate_link_target'];
}
}
public function has_meta_keys() {
return (bool) $this->meta_keys;
}
/**
* maybe_translate_link_targets
*
* @param string|array $metadata - Always null for post metadata.
* @param int $object_id - Post ID for post metadata
* @param string $meta_key - metadata key.
* @param bool $single - Indicates if processing only a single $metadata value or array of values.
*
* @return string|array Original or Modified $metadata.
*/
public function maybe_translate_link_targets( $metadata, $object_id, $meta_key, $single ) {
if ( array_key_exists( $meta_key, $this->meta_keys ) ) {
$custom_field_setting = new WPML_Post_Custom_Field_Setting( $this->tm_instance, $meta_key );
if ( $custom_field_setting->is_translate_link_target() ) {
$sub_fields = $custom_field_setting->get_translate_link_target_sub_fields();
$this->wp_api->remove_filter( 'get_post_metadata', array( $this, 'maybe_translate_link_targets' ), 10 );
$metadata_raw = maybe_unserialize( $this->wp_api->get_post_meta( $object_id, $meta_key, $single ) );
$this->wp_api->add_filter( 'get_post_metadata', array( $this, 'maybe_translate_link_targets' ), 10, 4 );
if ( $metadata_raw ) {
if ( $single ) {
$metadata_raw = array( $metadata_raw );
}
foreach ( $metadata_raw as $index => $metadata ) {
if ( ! empty( $sub_fields ) ) {
$metadata = $this->convert_sub_fields( $sub_fields, $metadata );
} else {
$metadata = $this->convert_text( $metadata );
}
$metadata_raw[ $index ] = $metadata;
}
if ( $single && ! is_array( $metadata_raw[0] ) ) {
$metadata_raw = $metadata_raw[0];
}
}
$metadata = $metadata_raw;
}
}
return $metadata;
}
private function convert_sub_fields( $sub_fields, $metadata ) {
foreach ( $sub_fields as $sub_field ) {
if ( isset( $sub_field['value'], $sub_field['attr']['translate_link_target'] ) && $sub_field['attr']['translate_link_target'] ) {
$key = trim( $sub_field['value'] );
if ( isset( $metadata[ $key ] ) ) {
$metadata[ $key ] = $this->convert_text( $metadata[ $key ] );
}
}
}
return $metadata;
}
}