first commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Help_Tab_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
return new WPML_Media_Help_Tab();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Help_Tab implements IWPML_Action {
|
||||
|
||||
public function add_hooks() {
|
||||
|
||||
add_action( 'admin_head', array( $this, 'add' ) );
|
||||
}
|
||||
|
||||
public function add() {
|
||||
$current_screen = get_current_screen();
|
||||
|
||||
if ( $this->is_media_related_screen( $current_screen ) ) {
|
||||
$media_translation_dashboard = esc_html__( 'WPML » Media Translation', 'wpml-media' );
|
||||
$wpml_translation_dashboard = esc_html__( 'WPML » Translation Management', 'wpml-media' );
|
||||
$current_screen->add_help_tab(
|
||||
array(
|
||||
'id' => 'wpml-media-translation',
|
||||
'title' => esc_html__( 'Translating Media', 'wpml-media' ),
|
||||
'content' =>
|
||||
'<p>' . esc_html__( 'There are two ways for you to translate Media:', 'wpml-media' ) . '</p>' .
|
||||
'<ul>' .
|
||||
'<li>' . sprintf(
|
||||
esc_html__( 'Use the dashboard on the %s page to translate your images and other media files.', 'wpml-media' ),
|
||||
$media_translation_dashboard
|
||||
) . '</li>' .
|
||||
'<li>' . sprintf(
|
||||
esc_html__( 'Use the dashboard on the %s page to send pages that contain media, for translation.', 'wpml-media' ),
|
||||
$wpml_translation_dashboard
|
||||
) . '</li>' .
|
||||
'</ul>' .
|
||||
'</ul>' .
|
||||
'<a href="https://wpml.org/?page_id=113610">' . esc_html__( 'Learn more about WPML Media Translation', 'wpml-media' ) . '</a>',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function is_media_related_screen( $current_screen ) {
|
||||
$accepted_bases = array(
|
||||
'wpml_page_wpml-media',
|
||||
'upload',
|
||||
'media',
|
||||
'wpml_page_wpml-translation-management/menu/main',
|
||||
);
|
||||
|
||||
return $current_screen && in_array( $current_screen->base, $accepted_bases );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Menus_Factory
|
||||
*/
|
||||
class WPML_Media_Menus_Factory {
|
||||
|
||||
public function create() {
|
||||
global $sitepress, $wpdb;
|
||||
|
||||
$wpml_wp_api = $sitepress->get_wp_api();
|
||||
$wpml_media_path = $wpml_wp_api->constant( 'WPML_MEDIA_PATH' );
|
||||
|
||||
$template_service_loader = new WPML_Twig_Template_Loader( array( $wpml_media_path . '/templates/menus/' ) );
|
||||
$pagination = new WPML_Admin_Pagination();
|
||||
|
||||
return new WPML_Media_Menus( $template_service_loader, $sitepress, $wpdb, $pagination );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Menus {
|
||||
|
||||
/**
|
||||
* @var IWPML_Template_Service
|
||||
*/
|
||||
private $template_service;
|
||||
/**
|
||||
* @var SitePress
|
||||
*/
|
||||
private $sitepress;
|
||||
/**
|
||||
* @var wpdb
|
||||
*/
|
||||
private $wpdb;
|
||||
/**
|
||||
* @var WPML_Admin_Pagination
|
||||
*/
|
||||
private $pagination;
|
||||
|
||||
/**
|
||||
* WPML_Media_Menus constructor.
|
||||
*
|
||||
* @param WPML_Twig_Template_Loader $template_service
|
||||
* @param SitePress $sitepress
|
||||
* @param wpdb $wpdb
|
||||
*/
|
||||
public function __construct( WPML_Twig_Template_Loader $template_service, SitePress $sitepress, wpdb $wpdb, WPML_Admin_Pagination $pagination = null ) {
|
||||
$this->template_service = $template_service;
|
||||
$this->sitepress = $sitepress;
|
||||
$this->wpdb = $wpdb;
|
||||
$this->pagination = $pagination;
|
||||
|
||||
}
|
||||
|
||||
public function display() {
|
||||
global $wp_locale, $wpml_query_filter;
|
||||
|
||||
do_action( 'wpml_media_messages' );
|
||||
do_action( 'wpml_media_menu' );
|
||||
|
||||
$menu_overrides = apply_filters( 'wpml_media_menu_overrides', array() );
|
||||
if ( $menu_overrides ) {
|
||||
foreach ( $menu_overrides as $menu_override ) {
|
||||
call_user_func( $menu_override );
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$wpml_media_url = $this->sitepress->get_wp_api()->constant( 'WPML_MEDIA_URL' );
|
||||
$wpml_media_version = $this->sitepress->get_wp_api()->constant( 'WPML_MEDIA_VERSION' );
|
||||
|
||||
wp_enqueue_style( OTGS_Assets_Handles::POPOVER_TOOLTIP );
|
||||
wp_enqueue_script( OTGS_Assets_Handles::POPOVER_TOOLTIP );
|
||||
wp_enqueue_style( 'wpml-media', $wpml_media_url . '/res/css/media-translation.css', array(), $wpml_media_version );
|
||||
wp_enqueue_script(
|
||||
'wpml-media',
|
||||
$wpml_media_url . '/res/js/media-translation-popup.js',
|
||||
array(
|
||||
'jquery',
|
||||
'jquery-ui-dialog',
|
||||
),
|
||||
$wpml_media_version,
|
||||
true
|
||||
);
|
||||
$wpml_media_popup_strings = array(
|
||||
'title' => esc_js( __( 'Media Translation', 'wpml-media' ) ),
|
||||
'cancel' => esc_js( __( 'Cancel', 'wpml-media' ) ),
|
||||
'save' => esc_js( __( 'Save media translation', 'wpml-media' ) ),
|
||||
'status_labels' => WPML_Media_Translations_UI::get_translation_status_labels(),
|
||||
);
|
||||
wp_localize_script( 'wpml-media', 'wpml_media_popup', $wpml_media_popup_strings );
|
||||
wp_enqueue_script( 'wpml-media-batch-url-translation', $wpml_media_url . '/res/js/batch-url-translation.js', array( 'jquery' ), $wpml_media_version, true );
|
||||
$batch_translation_vars = array(
|
||||
'complete' => esc_js( __( 'Scan complete!', 'wpml-media' ) ),
|
||||
'is_st_enabled' => (bool) $this->sitepress->get_wp_api()->constant( 'WPML_ST_VERSION' ),
|
||||
);
|
||||
wp_localize_script( 'wpml-media-batch-url-translation', 'wpml_media_batch_translation', $batch_translation_vars );
|
||||
|
||||
wp_enqueue_script( OTGS_Assets_Handles::TABLE_STICKY_HEADER );
|
||||
|
||||
$media_translations_ui = new WPML_Media_Translations_UI(
|
||||
$this->sitepress,
|
||||
$this->wpdb,
|
||||
$wp_locale,
|
||||
$wpml_query_filter,
|
||||
$this->pagination
|
||||
);
|
||||
|
||||
$media_translations_ui->show();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Posts_Media_Flag_Notice_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $sitepress;
|
||||
|
||||
if ( current_user_can( 'manage_options' ) && ! WPML_Media::has_setup_run() && $sitepress->is_setup_complete() ) {
|
||||
return new WPML_Media_Posts_Media_Flag_Notice( $sitepress );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Posts_Media_Flag_Notice implements IWPML_Action {
|
||||
|
||||
const PREPARE_ACTION = 'wpml-media-has-media-flag-prepare';
|
||||
const PROCESS_ACTION = 'wpml-media-has-media-flag';
|
||||
|
||||
const NOTICE_ID = 'wpml-media-posts-media-flag';
|
||||
const NOTICE_GROUP = 'wpml-media';
|
||||
|
||||
/**
|
||||
* @var SitePress
|
||||
*/
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* WPML_Media_Has_Media_Notice constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
|
||||
if ( $this->is_wpml_media_screen() ) {
|
||||
add_filter( 'wpml_media_menu_overrides', array( $this, 'override_default_menu' ) );
|
||||
} else {
|
||||
add_action( 'admin_head', array( $this, 'add_top_notice' ) );
|
||||
}
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_js' ) );
|
||||
}
|
||||
|
||||
public function override_default_menu( $menu_elements ) {
|
||||
$menu_elements[] = array( $this, 'render_menu' );
|
||||
|
||||
return $menu_elements;
|
||||
}
|
||||
|
||||
public function enqueue_js() {
|
||||
$wpml_media_url = $this->sitepress->get_wp_api()->constant( 'WPML_MEDIA_URL' );
|
||||
wp_enqueue_script( 'wpml-media-setup', $wpml_media_url . '/res/js/wpml-media-posts-media-flag.js', array( 'jquery' ), false, true );
|
||||
}
|
||||
|
||||
private function is_wpml_media_screen() {
|
||||
return isset( $_GET['page'] ) && 'wpml-media' === $_GET['page'];
|
||||
}
|
||||
|
||||
public function add_top_notice() {
|
||||
|
||||
/* translators: name ot WPML-Media plugin */
|
||||
$wpml_media = '<strong>' . __( 'WPML Media Translation', 'wpml-media' ) . '</strong>';
|
||||
|
||||
/* translators: used to build a link in the "Click here to finish the setup" */
|
||||
$here_text = _x( 'here', 'Used to build a link in the "Click here to finish the setup"', 'wpml-media' );
|
||||
$here_link = '<a href="' . admin_url( 'admin.php?page=wpml-media' ) . '">' . $here_text . '</a>';
|
||||
|
||||
/* translators: %1$s will be replaced with a translation of "WPML Media Translation", while %2$s is a link with the translation of the word "here" */
|
||||
$text = vsprintf(
|
||||
esc_html__( 'The %1$s setup is almost complete. Click %2$s to finish the setup.', 'wpml-media' ),
|
||||
array(
|
||||
$wpml_media,
|
||||
$here_link
|
||||
)
|
||||
);
|
||||
|
||||
$notice = new WPML_Notice( self::NOTICE_ID, $text, self::NOTICE_GROUP );
|
||||
$notice->set_css_class_types( 'notice-warning' );
|
||||
$notice->set_hideable( false );
|
||||
$notice->set_dismissible( false );
|
||||
$notice->set_collapsable( false );
|
||||
$notice->add_exclude_from_page( 'wpml-media' );
|
||||
$notice->add_capability_check( array( 'manage_options' ) );
|
||||
$wpml_admin_notices = wpml_get_admin_notices();
|
||||
$wpml_admin_notices->add_notice( $notice );
|
||||
|
||||
}
|
||||
|
||||
public function render_menu() {
|
||||
?>
|
||||
<div class="wrap wpml-media-setup">
|
||||
<h2><?php esc_html_e( 'Setup required', 'wpml-media' ) ?></h2>
|
||||
<div
|
||||
id="wpml-media-posts-media-flag"
|
||||
class="notice notice-warning"
|
||||
style="padding-bottom:8px"
|
||||
|
||||
data-prepare-action="<?php echo esc_attr( self::PREPARE_ACTION ); ?>"
|
||||
data-prepare-nonce="<?php echo wp_create_nonce( self::PREPARE_ACTION ); ?>"
|
||||
|
||||
data-process-action="<?php echo esc_attr( self::PROCESS_ACTION ); ?>"
|
||||
data-process-nonce="<?php echo wp_create_nonce( self::PROCESS_ACTION ); ?>"
|
||||
|
||||
>
|
||||
<p>
|
||||
<?php esc_html_e( 'In order to get WPML Media Translation fully working, you need to run this set up which takes only a few moments depending on the total number of posts in your WordPress install.', 'wpml-media' ); ?>
|
||||
</p>
|
||||
<input type="button" class="button-primary alignright"
|
||||
value="<?php esc_attr_e( 'Finish setup', 'wpml-media' ) ?>"/>
|
||||
|
||||
<span class="spinner"> </span>
|
||||
<p class="alignleft status description"></p>
|
||||
<br clear="all"/>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Screen_Options_Factory
|
||||
*/
|
||||
class WPML_Media_Screen_Options_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
/**
|
||||
* @return IWPML_Action|WPML_Media_Screen_Options
|
||||
*/
|
||||
public function create() {
|
||||
|
||||
$options = array();
|
||||
|
||||
if ( $this->is_translation_dashboard() ) {
|
||||
|
||||
$option_name = 'wpml_media_translation_dashboard_items_per_page';
|
||||
$options[] = array(
|
||||
'key' => 'per_page',
|
||||
'args' => array(
|
||||
'label' => __( 'Number of items per page:', 'wpml-media' ),
|
||||
'default' => get_option( $option_name, 20 ),
|
||||
'option' => $option_name,
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if ( $options ) {
|
||||
return new WPML_Media_Screen_Options( $options );
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function is_translation_dashboard() {
|
||||
return ! isset( $_GET['sm'] ) || 'media-translation' === $_GET['sm'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
class WPML_Media_Screen_Options implements IWPML_Action {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options = array();
|
||||
|
||||
/**
|
||||
* WPML_Media_Screen_Options constructor.
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct( $options ) {
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'load-wpml_page_wpml-media', array( $this, 'add_options' ) );
|
||||
add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
|
||||
}
|
||||
|
||||
public function add_options() {
|
||||
foreach ( $this->options as $option ) {
|
||||
add_screen_option( $option['key'], $option['args'] );
|
||||
}
|
||||
}
|
||||
|
||||
public function set_screen_option( $status, $option_name, $value ) {
|
||||
if ( $this->is_valid_option( $option_name ) ) {
|
||||
update_option( $option_name, $value );
|
||||
}
|
||||
}
|
||||
|
||||
private function is_valid_option( $option_name ) {
|
||||
$valid = false;
|
||||
foreach ( $this->options as $option ) {
|
||||
if ( $option_name === $option['args']['option'] ) {
|
||||
$valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Editor_Notices_Factory
|
||||
*/
|
||||
class WPML_Media_Editor_Notices_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
return new WPML_Media_Editor_Notices();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Editor_Notices
|
||||
*/
|
||||
class WPML_Media_Editor_Notices implements IWPML_Action {
|
||||
const TEXT_EDIT_NOTICE_DISMISSED = '_wpml_media_editor_text_edit_notice_dismissed';
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_ajax_wpml_media_editor_text_edit_notice_dismissed', array( $this, 'dismiss_texts_change_notice' ) );
|
||||
}
|
||||
|
||||
public function dismiss_texts_change_notice() {
|
||||
update_user_meta( get_current_user_id(), self::TEXT_EDIT_NOTICE_DISMISSED, 1 );
|
||||
wp_send_json_success();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,550 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_Media_Translations_UI
|
||||
*/
|
||||
class WPML_Media_Translations_UI extends WPML_Templates_Factory {
|
||||
const PREVIEW_MAX_WIDTH = 800;
|
||||
const PREVIEW_MAX_HEIGHT = 600;
|
||||
/**
|
||||
* @var SitePress
|
||||
*/
|
||||
private $sitepress;
|
||||
/**
|
||||
* @var wpdb
|
||||
*/
|
||||
private $wpdb;
|
||||
/**
|
||||
* @var WP_Locale
|
||||
*/
|
||||
private $wp_locale;
|
||||
/**
|
||||
* @var WPML_Query_Filter
|
||||
*/
|
||||
private $wpml_query_filter;
|
||||
/**
|
||||
* @var WPML_Admin_Pagination
|
||||
*/
|
||||
private $pagination;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $query_args = array();
|
||||
|
||||
/**
|
||||
* WPML_Media_Translations_UI constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
* @param wpdb $wpdb
|
||||
* @param WP_Locale $wp_locale
|
||||
* @param WPML_Query_Filter $wpml_query_filter
|
||||
* @param WPML_Admin_Pagination $pagination
|
||||
*/
|
||||
public function __construct(
|
||||
SitePress $sitepress,
|
||||
wpdb $wpdb,
|
||||
WP_Locale $wp_locale,
|
||||
WPML_Query_Filter $wpml_query_filter,
|
||||
WPML_Admin_Pagination $pagination
|
||||
) {
|
||||
parent::__construct();
|
||||
$this->sitepress = $sitepress;
|
||||
$this->wpdb = $wpdb;
|
||||
$this->wp_locale = $wp_locale;
|
||||
$this->wpml_query_filter = $wpml_query_filter;
|
||||
$this->pagination = $pagination;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_model() {
|
||||
|
||||
$this->set_query_args();
|
||||
|
||||
$languages = $this->get_languages();
|
||||
|
||||
$model = array(
|
||||
'strings' => array(
|
||||
'heading' => __( 'Media Translation', 'wpml-media' ),
|
||||
'filter_by_date' => __( 'Filter by date', 'wpml-media' ),
|
||||
'all_dates' => __( 'All dates', 'wpml-media' ),
|
||||
'in' => __( 'in', 'wpml_media' ),
|
||||
'to' => __( 'to', 'wpml_media' ),
|
||||
'filter_by_status' => __( 'Filter by translation status', 'wpml-media' ),
|
||||
'status_all' => __( 'All translation statuses', 'wpml-media' ),
|
||||
'status_not' => __( 'Media file not translated', 'wpml-media' ),
|
||||
'status_translated' => __( 'Translated media uploaded', 'wpml-media' ),
|
||||
'status_in_progress' => __( 'Translation in progress', 'wpml-media' ),
|
||||
'status_needs_translation' => __( 'Needs media file translation', 'wpml-media' ),
|
||||
'filter_by_language' => __( 'Filter by language', 'wpml-media' ),
|
||||
'any_language' => __( 'Any language', 'wpml-media' ),
|
||||
'search_media' => __( 'Search Media:', 'wpml-media' ),
|
||||
'search_placeholder' => __( 'Title, caption or description', 'wpml-media' ),
|
||||
'search_button_label' => __( 'Filter', 'wpml-media' ),
|
||||
'original_language' => __( 'Original language', 'wpml-media' ),
|
||||
'no_attachments' => __( 'No attachments found', 'wpml-media' ),
|
||||
'add_translation' => __( 'Add media file translation', 'wpml-media' ),
|
||||
'edit_translation' => __( 'Edit %s translation', 'wpml-media' ),
|
||||
'original' => __( 'Original:', 'wpml-media' ),
|
||||
'translation' => __( 'Translation:', 'wpml-media' ),
|
||||
'file' => __( 'File', 'wpml-media' ),
|
||||
'name' => __( 'Name', 'wpml-media' ),
|
||||
'caption' => __( 'Caption', 'wpml-media' ),
|
||||
'alt_text' => __( 'Alt text', 'wpml-media' ),
|
||||
'description' => __( 'Description', 'wpml-media' ),
|
||||
'copy_from_original' => __( 'Copy from original', 'wpml-media' ),
|
||||
'upload_translated_media' => __( 'Upload translated media file', 'wpml-media' ),
|
||||
'use_different_file' => __( 'Use a different file', 'wpml-media' ),
|
||||
'revert_to_original' => __( 'Revert to original', 'wpml-media' ),
|
||||
'restore_original_media' => __( 'Restore original media file', 'wpml-media' ),
|
||||
'statuses' => self::get_translation_status_labels(),
|
||||
'texts_change_notice' => __( 'Any changes you make to the text here will not affect any previous publications of this media on your website. This edited version will only appear if you select it from the library to be embedded.', 'wpml-media' ),
|
||||
),
|
||||
'months' => $this->get_months(),
|
||||
'selected_month' => isset( $this->query_args['m'] ) ? (int) $this->query_args['m'] : 0,
|
||||
'selected_status' => isset( $this->query_args['status'] ) ? $this->query_args['status'] : '',
|
||||
'from_language' => isset( $this->query_args['slang'] ) ? $this->query_args['slang'] : '',
|
||||
'to_language' => isset( $this->query_args['tlang'] ) ? $this->query_args['tlang'] : '',
|
||||
'statuses' => array(
|
||||
'not_translated' => WPML_Media_Translation_Status::NOT_TRANSLATED,
|
||||
'in_progress' => WPML_Media_Translation_Status::IN_PROGRESS,
|
||||
'translated' => WPML_Media_Translation_Status::TRANSLATED,
|
||||
'needs_translation' => WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION,
|
||||
),
|
||||
'search' => isset( $this->query_args['s'] ) ? $this->query_args['s'] : '',
|
||||
'languages' => $languages,
|
||||
'attachments' => $this->get_attachments( $languages ),
|
||||
'nonce' => wp_nonce_field( 'media-translation', 'wpnonce', false, false ),
|
||||
'pagination' => $this->get_pagination(),
|
||||
'target_language' => $this->should_filter_by_target_language() ? $this->query_args['tlang'] : '',
|
||||
|
||||
'batch_translation' => $this->get_batch_translation(),
|
||||
|
||||
'show_text_change_notice' => ! get_user_meta( get_current_user_id(), WPML_Media_Editor_Notices::TEXT_EDIT_NOTICE_DISMISSED, true ),
|
||||
);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function get_translation_status_labels() {
|
||||
return array(
|
||||
WPML_Media_Translation_Status::NOT_TRANSLATED => __( 'Not translated', 'wpml-media' ),
|
||||
WPML_Media_Translation_Status::IN_PROGRESS => __( 'In progress', 'wpml-media' ),
|
||||
WPML_Media_Translation_Status::TRANSLATED => __( 'Translated', 'wpml-media' ),
|
||||
WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION => __( 'Needs translation', 'wpml-media' ),
|
||||
);
|
||||
}
|
||||
|
||||
private function set_query_args() {
|
||||
$arg_keys = array( 'm', 'status', 'slang', 'tlang', 's', 'paged' );
|
||||
foreach ( $arg_keys as $key ) {
|
||||
if ( isset( $_GET[ $key ] ) ) {
|
||||
$this->query_args[ $key ] = sanitize_text_field( $_GET[ $key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function get_months() {
|
||||
$months = array();
|
||||
|
||||
$month_results = $this->wpdb->get_results(
|
||||
"
|
||||
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
|
||||
FROM {$this->wpdb->posts}
|
||||
WHERE post_type = 'attachment'
|
||||
ORDER BY post_date DESC
|
||||
"
|
||||
);
|
||||
|
||||
foreach ( $month_results as $month ) {
|
||||
$months[] = array(
|
||||
'id' => $month->year . zeroise( $month->month, 2 ),
|
||||
'label' => sprintf( __( '%1$s %2$d' ), $this->wp_locale->get_month( $month->month ), $month->year ),
|
||||
);
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function get_languages() {
|
||||
$languages = array();
|
||||
|
||||
$active_languages = $this->sitepress->get_active_languages();
|
||||
foreach ( $active_languages as $language ) {
|
||||
$languages[ $language['code'] ] = array(
|
||||
'name' => $language['display_name'],
|
||||
'flag' => $this->sitepress->get_flag_url( $language['code'] ),
|
||||
);
|
||||
}
|
||||
|
||||
return $languages;
|
||||
}
|
||||
|
||||
private function get_items_per_page() {
|
||||
return get_option( 'wpml_media_translation_dashboard_items_per_page', 20 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $languages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_attachments( $languages ) {
|
||||
$attachments = array();
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'attachment',
|
||||
'orderby' => 'ID',
|
||||
'order' => 'DESC',
|
||||
'posts_per_page' => $this->get_items_per_page(),
|
||||
);
|
||||
$args = array_merge( $args, $this->query_args );
|
||||
|
||||
if ( $this->should_filter_by_status() ) {
|
||||
$args['meta_query'] = array(
|
||||
'key' => '_media_translation_status',
|
||||
'value' => $this->query_args['status'],
|
||||
);
|
||||
}
|
||||
|
||||
$this->add_query_filters();
|
||||
|
||||
$attachment_posts = get_posts( $args );
|
||||
|
||||
$this->remove_query_filters();
|
||||
|
||||
foreach ( $attachment_posts as $attachment ) {
|
||||
$attachment_id = $attachment->ID;
|
||||
$post_element = new WPML_Post_Element( $attachment_id, $this->sitepress );
|
||||
$media_file_original = get_post_meta( $attachment_id, '_wp_attached_file', true );
|
||||
$translations = array();
|
||||
|
||||
$is_image = (int) ( 0 === strpos( $attachment->post_mime_type, 'image/' ) );
|
||||
|
||||
foreach ( $languages as $code => $language ) {
|
||||
$translation = $post_element->get_translation( $code );
|
||||
|
||||
if ( $translation ) {
|
||||
$translation_post = $translation->get_wp_object();
|
||||
$media_file_translated = get_post_meta( $translation->get_id(), '_wp_attached_file', true );
|
||||
|
||||
$translations[ $code ] = array(
|
||||
'id' => $translation->get_id(),
|
||||
'file_name' => basename( $media_file_translated ),
|
||||
'title' => $translation_post->post_title,
|
||||
'caption' => $translation_post->post_excerpt,
|
||||
'description' => $translation_post->post_content,
|
||||
'alt' => get_post_meta( $translation->get_id(), '_wp_attachment_image_alt', true ),
|
||||
'thumb' => $this->get_attachment_thumb( $translation->get_id(), $is_image ),
|
||||
'media_is_translated' => $media_file_translated && $media_file_translated !== $media_file_original,
|
||||
'status' => $this->get_translation_status( $attachment_id, $translation ),
|
||||
);
|
||||
|
||||
} else {
|
||||
$translations[ $code ] = array(
|
||||
'status' => WPML_Media_Translation_Status::NOT_TRANSLATED,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$attachments[] = array(
|
||||
'post' => $attachment,
|
||||
'mime_type' => $is_image ? 'image/*' : $attachment->post_mime_type,
|
||||
'is_image' => $is_image,
|
||||
'file_name' => basename( $media_file_original ),
|
||||
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
|
||||
'meta' => get_post_meta( $attachment_id, '_wp_attachment_metadata', true ),
|
||||
'language' => $post_element->get_language_code(),
|
||||
'thumb' => $this->get_attachment_thumb( $attachment_id, $is_image ),
|
||||
'url' => $is_image ? $this->get_attachment_url( $attachment_id ) : '',
|
||||
'translations' => $translations,
|
||||
'preview' => [
|
||||
'width' => self::PREVIEW_MAX_WIDTH,
|
||||
'height' => self::PREVIEW_MAX_HEIGHT,
|
||||
],
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $attachment_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_attachment_url( $attachment_id ) {
|
||||
$image = wp_get_attachment_image_src( $attachment_id, [ self::PREVIEW_MAX_WIDTH, self::PREVIEW_MAX_HEIGHT ] );
|
||||
|
||||
return $image[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $attachment_id
|
||||
* @param bool $is_image
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_attachment_thumb( $attachment_id, $is_image = true ) {
|
||||
$image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );
|
||||
|
||||
$thumb = array(
|
||||
'src' => $image ? $image[0] : '',
|
||||
'width' => $is_image ? 60 : $image[1],
|
||||
'height' => $is_image ? 60 : $image[2],
|
||||
);
|
||||
|
||||
return $thumb;
|
||||
}
|
||||
|
||||
public function set_total_items_in_pagination( $query ) {
|
||||
|
||||
$query_count = preg_replace(
|
||||
'/^SELECT /',
|
||||
'SELECT SQL_CALC_FOUND_ROWS ',
|
||||
$query
|
||||
);
|
||||
$this->wpdb->get_results( $query_count );
|
||||
|
||||
$this->pagination->set_total_items( $this->wpdb->get_var( 'SELECT FOUND_ROWS()' ) );
|
||||
|
||||
return $query;
|
||||
}
|
||||
|
||||
private function get_pagination() {
|
||||
|
||||
$this->pagination->set_items_per_page( $this->get_items_per_page() );
|
||||
$current_page = isset( $this->query_args['paged'] ) ? $this->query_args['paged'] : 1;
|
||||
$this->pagination->set_current_page( $current_page );
|
||||
|
||||
$model = array(
|
||||
'strings' => array(
|
||||
'list_navigation' => __( 'Navigation', 'wpml-media' ),
|
||||
'of' => __( 'of', 'wpml-media' ),
|
||||
),
|
||||
'pagination' => array(
|
||||
'get_first_page_url' => $this->pagination->get_first_page_url(),
|
||||
'first_page' => __( 'First page', 'wpml-media' ),
|
||||
|
||||
'get_previous_page_url' => $this->pagination->get_previous_page_url(),
|
||||
'previous_page' => __( 'Previous page', 'wpml-media' ),
|
||||
|
||||
'get_current_page' => $this->pagination->get_current_page(),
|
||||
'get_total_pages' => $this->pagination->get_total_pages(),
|
||||
|
||||
'get_next_page_url' => $this->pagination->get_next_page_url(),
|
||||
'next_page' => __( 'Next page', 'wpml-media' ),
|
||||
|
||||
'get_last_page_url' => $this->pagination->get_last_page_url(),
|
||||
'last_page' => __( 'Last page', 'wpml-media' ),
|
||||
),
|
||||
'total_items' => sprintf(
|
||||
_n( '%s item', '%s items', $this->pagination->get_total_items() ),
|
||||
$this->pagination->get_total_items()
|
||||
),
|
||||
);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
private function get_batch_translation() {
|
||||
$model = array(
|
||||
'strings' => array(
|
||||
'close' => __( 'Close', 'wpml-media' ),
|
||||
'was_replaced' => __( 'The media that you uploaded was replaced in these translated posts:', 'wpml-media' ),
|
||||
'other_posts' => __( 'The same media might be used in other posts too. Do you want to scan and replace it?', 'wpml-media' ),
|
||||
'without_usage' => __( 'The media that you uploaded will be used in future post translations. The same media might be used in already existing posts. Do you want to scan and replace it now?', 'wpml-media' ),
|
||||
'scan_for_this_media' => __( 'Scan for content that has specifically this media (faster)', 'wpml-media' ),
|
||||
'scan_for_all_media' => __( 'Scan for content that has any of the media files that I translated (takes more time)', 'wpml-media' ),
|
||||
'button_label' => _x( 'Scan & Replace', 'Button label (verb)', 'wpml-media' ),
|
||||
),
|
||||
);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
private function add_query_filters() {
|
||||
remove_filter( 'posts_join', array( $this->wpml_query_filter, 'posts_join_filter' ), 10 );
|
||||
remove_filter( 'posts_where', array( $this->wpml_query_filter, 'posts_where_filter' ), 10 );
|
||||
|
||||
add_filter( 'posts_join', array( $this, 'filter_request_clause_join' ), PHP_INT_MAX );
|
||||
add_filter( 'posts_where', array( $this, 'filter_request_clause_where' ), PHP_INT_MAX );
|
||||
add_filter( 'posts_request', array( $this, 'set_total_items_in_pagination' ), - PHP_INT_MAX );
|
||||
}
|
||||
|
||||
private function remove_query_filters() {
|
||||
add_filter( 'posts_join', array( $this->wpml_query_filter, 'posts_join_filter' ), 10, 2 );
|
||||
add_filter( 'posts_where', array( $this->wpml_query_filter, 'posts_where_filter' ), 10, 2 );
|
||||
|
||||
remove_filter( 'posts_join', array( $this, 'filter_request_clause_join' ), PHP_INT_MAX );
|
||||
remove_filter( 'posts_where', array( $this, 'filter_request_clause_where' ), PHP_INT_MAX );
|
||||
remove_filter( 'posts_request', array( $this, 'set_total_items_in_pagination' ), - PHP_INT_MAX );
|
||||
}
|
||||
|
||||
public function filter_request_clause_join( $join ) {
|
||||
|
||||
$join .= " LEFT JOIN {$this->wpdb->prefix}icl_translations icl_translations_source
|
||||
ON icl_translations_source.element_id = {$this->wpdb->posts}.ID ";
|
||||
|
||||
if ( $this->should_filter_by_status() ) {
|
||||
if ( $this->should_filter_by_target_language() ) {
|
||||
$join .= $this->wpdb->prepare(
|
||||
" LEFT JOIN {$this->wpdb->postmeta} post_meta
|
||||
ON icl_translations_source.element_id = post_meta.post_id
|
||||
AND post_meta.meta_key = %s
|
||||
",
|
||||
WPML_Media_Translation_Status::STATUS_PREFIX . $this->query_args['tlang']
|
||||
);
|
||||
} else {
|
||||
$active_language = $this->sitepress->get_active_languages();
|
||||
$default_lanuage = $this->sitepress->get_default_language();
|
||||
|
||||
foreach ( $active_language as $language ) {
|
||||
if ( $language['code'] !== $default_lanuage ) {
|
||||
$sanitized_code = str_replace( '-', '_', $language['code'] );
|
||||
|
||||
$join .= $this->wpdb->prepare(
|
||||
" LEFT JOIN {$this->wpdb->postmeta} post_meta_{$sanitized_code}
|
||||
ON {$this->wpdb->posts}.ID = post_meta_{$sanitized_code}.post_id
|
||||
AND post_meta_{$sanitized_code}.meta_key=%s",
|
||||
WPML_Media_Translation_Status::STATUS_PREFIX . $language['code']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( $this->should_filter_by_target_language() ) {
|
||||
$join .= " JOIN {$this->wpdb->prefix}icl_translations icl_translations_target
|
||||
ON icl_translations_target.trid = icl_translations_source.trid
|
||||
AND icl_translations_target.language_code='{$this->query_args['tlang']}'";
|
||||
}
|
||||
}
|
||||
|
||||
return $join;
|
||||
}
|
||||
|
||||
public function filter_request_clause_where( $where ) {
|
||||
|
||||
$where .= " AND icl_translations_source.element_type='post_attachment'
|
||||
AND icl_translations_source.source_language_code IS NULL ";
|
||||
|
||||
if ( ! empty( $this->query_args['slang'] ) && $this->query_args['slang'] !== 'all' ) {
|
||||
$where .= $this->wpdb->prepare( ' AND icl_translations_source.language_code = %s ', $this->query_args['slang'] );
|
||||
}
|
||||
|
||||
if ( $this->should_filter_by_status() ) {
|
||||
if ( $this->should_filter_by_target_language() ) {
|
||||
if ( $this->query_args['status'] === WPML_Media_Translation_Status::NOT_TRANSLATED ) {
|
||||
$where .= $this->wpdb->prepare(
|
||||
' AND (
|
||||
post_meta.meta_value IS NULL OR
|
||||
post_meta.meta_value = %s OR
|
||||
post_meta.meta_value = %s OR
|
||||
post_meta.meta_value = %s
|
||||
)',
|
||||
WPML_Media_Translation_Status::NOT_TRANSLATED,
|
||||
WPML_Media_Translation_Status::IN_PROGRESS,
|
||||
WPML_Media_Translation_Status::NEEDS_MEDIA_TRANSLATION
|
||||
);
|
||||
} else {
|
||||
$where .= $this->wpdb->prepare( ' AND post_meta.meta_value = %s', $this->query_args['status'] );
|
||||
}
|
||||
} else {
|
||||
$active_language = $this->sitepress->get_active_languages();
|
||||
$default_language = $this->sitepress->get_default_language();
|
||||
|
||||
if ( $this->query_args['status'] === WPML_Media_Translation_Status::TRANSLATED ) {
|
||||
foreach ( $active_language as $language ) {
|
||||
$sanitized_code = str_replace( '-', '_', $language['code'] );
|
||||
if ( $language['code'] !== $default_language ) {
|
||||
$where .= $this->wpdb->prepare(
|
||||
"AND post_meta_{$sanitized_code}.meta_value = %s",
|
||||
WPML_Media_Translation_Status::TRANSLATED
|
||||
);
|
||||
}
|
||||
}
|
||||
} elseif ( $this->query_args['status'] === WPML_Media_Translation_Status::NOT_TRANSLATED ) {
|
||||
$where .= 'AND ( 0 ';
|
||||
foreach ( $active_language as $language ) {
|
||||
$sanitized_code = str_replace( '-', '_', $language['code'] );
|
||||
if ( $language['code'] !== $default_language ) {
|
||||
$where .= $this->wpdb->prepare(
|
||||
"
|
||||
OR post_meta_{$sanitized_code}.meta_value = %s
|
||||
OR post_meta_{$sanitized_code}.meta_value IS NULL",
|
||||
$this->query_args['status']
|
||||
);
|
||||
}
|
||||
}
|
||||
$where .= ') ';
|
||||
} else {
|
||||
$where .= 'AND ( 0 ';
|
||||
foreach ( $active_language as $language ) {
|
||||
$sanitized_code = str_replace( '-', '_', $language['code'] );
|
||||
if ( $language['code'] !== $default_language ) {
|
||||
$where .= $this->wpdb->prepare(
|
||||
"
|
||||
OR post_meta_{$sanitized_code}.meta_value = %s",
|
||||
$this->query_args['status']
|
||||
);
|
||||
}
|
||||
}
|
||||
$where .= ') ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
|
||||
private function should_filter_by_status() {
|
||||
return isset( $this->query_args['status'] ) && $this->query_args['status'] !== '';
|
||||
}
|
||||
|
||||
private function should_filter_by_target_language() {
|
||||
return isset( $this->query_args['tlang'] ) && $this->query_args['tlang'] !== '';
|
||||
}
|
||||
|
||||
protected function init_template_base_dir() {
|
||||
$this->template_paths = array(
|
||||
WPML_MEDIA_PATH . '/templates/menus/',
|
||||
WPML_PLUGIN_PATH . '/templates/pagination/',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_template() {
|
||||
return 'media-translation.twig';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $attachment_id
|
||||
* @param $translation
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
private function get_translation_status( $attachment_id, $translation ) {
|
||||
$translation_status = get_post_meta(
|
||||
$attachment_id,
|
||||
WPML_Media_Translation_Status::STATUS_PREFIX . $translation->get_language_code(),
|
||||
true
|
||||
);
|
||||
if ( ! $translation_status ) {
|
||||
$translation_status = WPML_Media_Translation_Status::NOT_TRANSLATED;
|
||||
}
|
||||
|
||||
return $translation_status;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user