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,14 @@
<?php
class WPML_Media_Add_To_Basket_Factory implements IWPML_Backend_Action_Loader {
public function create() {
global $sitepress;
if ( isset( $_POST['icl_tm_action'] ) && 'add_jobs' === $_POST['icl_tm_action'] ) {
return new WPML_Media_Add_To_Basket( $sitepress );
}
return null;
}
}

View File

@@ -0,0 +1,42 @@
<?php
class WPML_Media_Add_To_Basket implements IWPML_Action {
/**
* @var SitePress
*/
private $sitepress;
/**
* WPML_Media_Add_To_Basket constructor.
*/
public function __construct( SitePress $sitepress ) {
$this->sitepress = $sitepress;
}
public function add_hooks() {
add_filter(
'pre_update_option_' . $this->sitepress->get_wp_api()->constant( 'TranslationProxy_Basket::ICL_TRANSLATION_JOBS_BASKET' ),
array( $this, 'add_media' )
);
}
public function add_media( $data ) {
if ( ! empty( $data['post'] ) ) {
foreach ( $data['post'] as $post_id => $post ) {
if ( $media = $this->get_post_media( $post_id ) ) {
$data['post'][ $post_id ]['media-translation'] = $media;
}
}
}
return $data;
}
private function get_post_media( $post_id ) {
return isset( $_POST['post'][ $post_id ]['media-translation'] ) ?
array_map( 'intval', $_POST['post'][ $post_id ]['media-translation'] ) :
array();
}
}

View File

@@ -0,0 +1,20 @@
<?php
class WPML_Media_Selector_Factory implements IWPML_Backend_Action_Loader {
public function create() {
global $sitepress;
$wpml_wp_api = $sitepress->get_wp_api();
$wpml_media_path = $wpml_wp_api->constant( 'WPML_MEDIA_PATH' );
return new WPML_Media_Selector(
$sitepress,
new WPML_Twig_Template_Loader( array( $wpml_media_path . '/templates/media-selector/' ) ),
new WPML_Media_Post_With_Media_Files_Factory(),
new WPML_Translation_Element_Factory( $sitepress )
);
}
}

View File

@@ -0,0 +1,187 @@
<?php
class WPML_Media_Selector implements IWPML_Action {
/**
* @var SitePress
*/
private $sitepress;
/**
* @var WPML_Twig_Template_Loader
*/
private $template_loader;
/**
* @var WPML_Media_Post_With_Media_Files_Factory
*/
private $post_with_media_files_factory;
/**
* @var WPML_Media_Post_With_Media_Files_Factory
*/
private $translation_element_factory;
const USER_META_HIDE_POST_MEDIA_SELECTOR = '_wpml_media_hide_post_media_selector';
public function __construct(
SitePress $sitepress,
WPML_Twig_Template_Loader $template_loader,
WPML_Media_Post_With_Media_Files_Factory $post_with_media_files_factory,
WPML_Translation_Element_Factory $translation_element_factory
) {
$this->sitepress = $sitepress;
$this->template_loader = $template_loader;
$this->post_with_media_files_factory = $post_with_media_files_factory;
$this->translation_element_factory = $translation_element_factory;
}
public function add_hooks() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_res' ) );
add_action( 'wp_ajax_wpml_media_load_image_selector', array( $this, 'load_images_selector' ) );
add_action( 'wp_ajax_wpml_media_toogle_show_media_selector', array( $this, 'toggle_show_media_selector' ) );
add_filter( 'wpml_translation_dashboard_row_data', array( $this, 'add_media_data_to_dashboard_row' ), 10, 2 );
add_action( 'wpml_tm_after_translation_dashboard_documents', array( $this, 'add_media_selector_preloader' ) );
}
public function enqueue_res() {
$current_screen = get_current_screen();
if ( $current_screen->id === 'wpml_page_' . $this->sitepress->get_wp_api()->constant( 'WPML_TM_FOLDER' ) . '/menu/main' ) {
$wpml_media_url = $this->sitepress->get_wp_api()->constant( 'WPML_MEDIA_URL' );
wp_enqueue_script( 'wpml-media-selector', $wpml_media_url . '/res/js/media-selector.js', array( 'jquery' ), false, true );
wp_enqueue_style( 'wpml-media-selector', $wpml_media_url . '/res/css/media-selector.css', array() );
}
}
public function load_images_selector() {
$post_id = (int) $_POST['post_id'];
if ( isset( $_POST['languages'] ) && is_array( $_POST['languages'] ) ) {
$languages = array_map( 'sanitize_text_field', $_POST['languages'] );
} else {
$languages = array();
}
$media_files_list = $this->get_media_files_list( $post_id, $languages );
$media_files_count = count( $media_files_list );
$model = array(
'files' => $media_files_list,
'post_id' => $post_id,
);
$html = $this->template_loader->get_template()->show( $model, 'media-selector.twig' );
wp_send_json_success(
array(
'html' => $html,
'media_files_count' => $media_files_count,
)
);
}
/**
* @param int $post_id
* @param array $languages
*
* @return array
*/
private function get_media_files_list( $post_id, $languages ) {
$media_files_list = array();
$post_with_media = $this->post_with_media_files_factory->create( $post_id );
$media_ids = $post_with_media->get_media_ids();
foreach ( $media_ids as $attachment_id ) {
$media_files_list[ $attachment_id ] = array(
'thumbnail' => wp_get_attachment_thumb_url( $attachment_id ),
'name' => get_post_field( 'post_title', $attachment_id ),
'translated' => $this->media_file_is_translated( $attachment_id, $languages ),
);
}
return $media_files_list;
}
private function media_file_is_translated( $attachment_id, $languages ) {
$post_element = $this->translation_element_factory->create( $attachment_id, 'post' );
foreach ( $languages as $language ) {
$translation = $post_element->get_translation( $language );
if ( null === $translation || get_post_meta( $attachment_id, '_wp_attached_file', true )
=== get_post_meta( $translation->get_id(), '_wp_attached_file', true ) ) {
return false;
}
}
return true;
}
public function toggle_show_media_selector() {
$current_value = get_user_meta( get_current_user_id(), self::USER_META_HIDE_POST_MEDIA_SELECTOR, true );
update_user_meta( get_current_user_id(), self::USER_META_HIDE_POST_MEDIA_SELECTOR, ! $current_value );
wp_send_json_success();
}
/**
* @param array $row_data
* @param stdClass $doc_data
*
* @return array
*/
public function add_media_data_to_dashboard_row( $row_data, $doc_data ) {
if ( 0 !== strpos( $doc_data->translation_element_type, 'post_' ) ) {
return $row_data;
}
$row_data = $this->add_post_has_media_flag( $row_data, $doc_data->ID );
$row_data = $this->add_post_type_attribute_data( $row_data, $doc_data->ID );
return $row_data;
}
/**
* @param array $data
* @param int $post_id
*
* @return array
*/
private function add_post_has_media_flag( array $data, $post_id ) {
$data['has-media'] = get_post_meta( $post_id, WPML_Media_Set_Posts_Media_Flag::HAS_MEDIA_POST_FLAG, true );
return $data;
}
/**
* @param array $data
* @param int $post_id
*
* @return array
*/
private function add_post_type_attribute_data( $data, $post_id ) {
$post_type = get_post_type( $post_id );
$post_type_object = get_post_type_object( $post_type );
$data['post-type'] = strtolower( $post_type_object->labels->singular_name );
return $data;
}
public function add_media_selector_preloader() {
$model = array(
'strings' => array(
'has_posts' => sprintf(
__(
'Choose which media to translate with this %s',
'wpml-media'
),
'%POST_TYPE%'
),
'loading' => __( 'Loading...', 'wpml-media' ),
),
'hide_selector' => get_user_meta( get_current_user_id(), self::USER_META_HIDE_POST_MEDIA_SELECTOR, true ),
);
echo $this->template_loader->get_template()->show( $model, 'media-selector-preloader.twig' );
}
}

View File

@@ -0,0 +1,34 @@
<?php
class WPML_Media_Submitted_Basket_Notice_Factory implements IWPML_Backend_Action_Loader {
public function create() {
global $sitepress;
if ( $sitepress->get_wp_api()->is_tm_page( 'basket' ) && $this->basket_has_media() ) {
$template_loader = new WPML_Twig_Template_Loader( array( WPML_MEDIA_PATH . '/templates/media-selector/' ) );
return new WPML_Media_Submitted_Basket_Notice( $template_loader );
}
return null;
}
private function basket_has_media() {
$basket = TranslationProxy_Basket::get_basket( true );
$item_types = TranslationProxy_Basket::get_basket_items_types();
foreach ( $item_types as $item_type => $type_type ) {
if ( isset( $basket[ $item_type ] ) ) {
foreach ( $basket[ $item_type ] as $item ) {
if ( ! empty( $item['media-translation'] ) ) {
return true;
}
}
}
}
return false;
}
}

View File

@@ -0,0 +1,80 @@
<?php
class WPML_Media_Submitted_Basket_Notice implements IWPML_Action {
/**
* @var WPML_Twig_Template_Loader
*/
private $template_loader;
public function __construct( WPML_Twig_Template_Loader $template_loader ) {
$this->template_loader = $template_loader;
}
public function add_hooks() {
add_action( 'wpml_tm_scripts_enqueued', array( $this, 'load_js' ) );
add_action( 'wpml_translation_basket_page_after', array( $this, 'load_dialog_template' ) );
}
public function load_js() {
$script_handle = 'submitted-basket-notice';
wp_enqueue_script(
$script_handle,
WPML_MEDIA_URL . '/res/js/submitted-basket-notice.js',
array( 'jquery-ui-dialog' ),
WPML_MEDIA_VERSION,
false
);
$wpml_media_basket_notice_data = array(
'button_label' => __( 'Continue', 'wpml_media' ),
);
wp_localize_script( $script_handle, 'wpml_media_basket_notice_data', $wpml_media_basket_notice_data );
}
public function load_dialog_template() {
/* translators: WPML plugin name */
$wpml_plugin_name = __( 'WPML', 'wpml-media' );
/* translators: WPML Media Translation saddon/section name */
$media_translation_name = __( 'Media Translation', 'wpml-media' );
$media_translation_url = admin_url( 'admin.php?page=wpml-media' );
$media_translation_link = sprintf(
'<a href="%s" target="_blank" rel="noopener" class="wpml-external-link">%s &raquo; %s</a>',
$media_translation_url,
$wpml_plugin_name,
$media_translation_name
);
/* translators: media file string used in "if you want to use a different media file for each language..." */
$media_file_string = __( 'media file', 'wpml-media' );
$redirect_url = '#';
if ( defined( 'WPML_TM_FOLDER' ) ) {
$redirect_url = add_query_arg( 'page', WPML_TM_FOLDER . '/menu/main.php', admin_url( 'admin.php' ) );
}
$model = array(
'strings' => array(
'dialog_title' => __( 'Media sent to translation', 'wpml-media' ),
'content_with_media_sent' => __( 'You have sent content which contains media attachments for translation.', 'wpml-media' ),
'media_texts_translated' => sprintf( __( 'Translators will translate all your %1$smedia texts%2$s.', 'wpml-media' ), '<strong>', '</strong>' ),
'use_different_media' => sprintf(
__( 'If you want to use a different %1$s for each language, you can set them in: %2$s.', 'wpml-media' ),
'<strong>' . $media_file_string . '</strong>',
$media_translation_link
),
'learn_more' => __( 'Learn more about Media Translation', 'wpml-media' ),
'wpml' => _x( 'WPML', 'plugin name', 'wpml-media' ),
'media_translation' => _x( 'Media Translation', 'wpml addon name', 'wpml-media' ),
),
'learn_more_url' => 'https://wpml.org/?page_id=113610',
'redirect_url' => $redirect_url,
);
echo $this->template_loader->get_template()->show( $model, 'submitted-basket-notice.twig' );
}
}