first commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class WPML_Cache_Terms_Per_Lang_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $sitepress;
|
||||
return new WPML_Cache_Terms_Per_Lang( $sitepress );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class WPML_Cache_Terms_Per_Lang implements IWPML_Action {
|
||||
|
||||
const CACHE_GROUP = 'WPML_Cache_Terms_Per_Lang';
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* WPML_Cache_Terms_Per_Lang constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
*/
|
||||
public function __construct( SitePress $sitepress ) {
|
||||
$this->sitepress = $sitepress;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter( 'get_the_terms', array( $this, 'terms_per_lang' ), 10, 3 );
|
||||
add_action( 'clean_object_term_cache', array( $this, 'clear_cache' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $terms
|
||||
* @param int $post_id
|
||||
* @param string $taxonomy
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function terms_per_lang( $terms, $post_id, $taxonomy ) {
|
||||
$all_terms = wp_cache_get( $post_id, self::CACHE_GROUP, false );
|
||||
if ( ! is_array( $all_terms ) ) {
|
||||
$current_post = get_post( $post_id );
|
||||
|
||||
$all_terms = [];
|
||||
if ( $current_post && $this->sitepress->is_display_as_translated_post_type( $current_post->post_type ) ) {
|
||||
$taxonomies = get_post_taxonomies( $current_post );
|
||||
$all_terms = wp_get_object_terms( $post_id, $taxonomies );
|
||||
$all_terms = is_wp_error( $all_terms ) ? [] : $all_terms;
|
||||
}
|
||||
|
||||
wp_cache_set( $post_id, $all_terms, self::CACHE_GROUP );
|
||||
}
|
||||
|
||||
$terms_per_lang = $this->get_terms_by_tax( $all_terms ? $all_terms : [], $taxonomy );
|
||||
$terms_per_lang = $terms_per_lang->isEmpty() ? false : $terms_per_lang->toArray();
|
||||
|
||||
return $terms_per_lang ?: $terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $all_terms
|
||||
* @param string $taxonomy
|
||||
*
|
||||
* @return WPML\Collect\Support\Collection
|
||||
*/
|
||||
private function get_terms_by_tax( $all_terms, $taxonomy ) {
|
||||
return wpml_collect( $all_terms )
|
||||
->filter(
|
||||
function ( $term ) use ( $taxonomy ) {
|
||||
return $taxonomy === $term->taxonomy;
|
||||
}
|
||||
)
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $object_ids An array of object IDs.
|
||||
*/
|
||||
public function clear_cache( $object_ids ) {
|
||||
foreach ( $object_ids as $id ) {
|
||||
wp_cache_delete( $id, self::CACHE_GROUP );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Default_Lang_Messages_Factory extends WPML_Current_Screen_Loader_Factory {
|
||||
|
||||
/**
|
||||
* @return WPML_Display_As_Translated_Default_Lang_Messages
|
||||
*/
|
||||
public function create_hooks() {
|
||||
global $sitepress;
|
||||
|
||||
$template_service_loader = new WPML_Twig_Template_Loader(
|
||||
array( WPML_PLUGIN_PATH . '/templates/display-as-translated' )
|
||||
);
|
||||
|
||||
return new WPML_Display_As_Translated_Default_Lang_Messages(
|
||||
$sitepress,
|
||||
new WPML_Display_As_Translated_Default_Lang_Messages_View( $template_service_loader->get_template() )
|
||||
);
|
||||
}
|
||||
|
||||
/** @return string */
|
||||
public function get_screen_regex() {
|
||||
return '/^sitepress-multilingual-cms\/menu\/languages$/';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Default_Lang_Messages_View {
|
||||
|
||||
const TEMPLATE = 'default-language-change.twig';
|
||||
|
||||
/**
|
||||
* @var WPML_Twig_Template
|
||||
*/
|
||||
private $template_service;
|
||||
|
||||
public function __construct( WPML_Twig_Template $template_service ) {
|
||||
$this->template_service = $template_service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prev_default_lang
|
||||
* @param string $default_lang
|
||||
*/
|
||||
public function display( $prev_default_lang, $default_lang ) {
|
||||
echo $this->template_service->show( $this->get_model( $prev_default_lang, $default_lang ), self::TEMPLATE );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prev_default_lang
|
||||
* @param string $default_lang
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_model( $prev_default_lang, $default_lang ) {
|
||||
return array(
|
||||
'before_message' => __( "Changing the site's default language can cause some content to disappear.", 'sitepress' ),
|
||||
'after_message' => sprintf(
|
||||
__( "If some content appears gone, it might be because you switched the site's default language from %1\$s to %2\$s.", 'sitepress' ),
|
||||
$prev_default_lang,
|
||||
$default_lang
|
||||
),
|
||||
'help_text' => __( 'Tell me more', 'sitepress' ),
|
||||
'help_link' => 'https://wpml.org/?page_id=1451509',
|
||||
'got_it' => __( 'Got it', 'sitepress' ),
|
||||
'lang_has_changed' => $prev_default_lang !== $default_lang,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Default_Lang_Messages {
|
||||
|
||||
const PREVIOUS_LANG_KEY = 'wpml-previous-default-language';
|
||||
|
||||
/**
|
||||
* @var SitePress
|
||||
*/
|
||||
private $sitepress;
|
||||
|
||||
/**
|
||||
* @var WPML_Display_As_Translated_Default_Lang_Messages_View
|
||||
*/
|
||||
private $view;
|
||||
|
||||
public function __construct( SitePress $sitepress, WPML_Display_As_Translated_Default_Lang_Messages_View $view ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->view = $view;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
if ( $this->should_display_message() ) {
|
||||
add_action( 'wpml_after_active_languages_display', array( $this, 'display_messages' ) );
|
||||
add_action( 'icl_after_set_default_language', array( $this, 'save_previous_lang' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function enqueue_scripts() {
|
||||
wp_enqueue_script(
|
||||
'wpml-default-lang-change-message',
|
||||
ICL_PLUGIN_URL . '/res/js/display-as-translated/toggle-default-lang-change-message.js',
|
||||
array( 'jquery' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prev_lang
|
||||
*/
|
||||
public function save_previous_lang( $prev_lang ) {
|
||||
update_option( self::PREVIOUS_LANG_KEY, $prev_lang );
|
||||
}
|
||||
|
||||
public function display_messages() {
|
||||
$previous_lang = get_option( self::PREVIOUS_LANG_KEY );
|
||||
$this->view->display(
|
||||
$this->sitepress->get_display_language_name( $previous_lang ? $previous_lang : $this->sitepress->get_default_language() ),
|
||||
$this->sitepress->get_display_language_name( $this->sitepress->get_default_language() )
|
||||
);
|
||||
update_option( self::PREVIOUS_LANG_KEY, $this->sitepress->get_default_language() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function should_display_message() {
|
||||
$post_types = get_post_types();
|
||||
$taxonomies = get_taxonomies();
|
||||
|
||||
foreach ( $post_types as $post_type ) {
|
||||
if ( $this->sitepress->is_display_as_translated_post_type( $post_type ) && get_posts(
|
||||
array(
|
||||
'post_type' => $post_type,
|
||||
'posts_per_page' => 1,
|
||||
)
|
||||
) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
if ( $this->sitepress->is_display_as_translated_taxonomy( $taxonomy ) && get_terms( array( 'taxonomy' => $taxonomy ) ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Message_For_New_Post_Factory implements IWPML_Backend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $pagenow, $sitepress;
|
||||
|
||||
$notices = wpml_get_admin_notices();
|
||||
|
||||
if ( 'post-new.php' === $pagenow ) {
|
||||
return new WPML_Display_As_Translated_Message_For_New_Post( $sitepress, $notices );
|
||||
} else {
|
||||
$notices->remove_notice( WPML_Notices::DEFAULT_GROUP, 'WPML_Display_As_Translated_Message_For_New_Post' );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Message_For_New_Post implements IWPML_Action {
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var WPML_Notices $notices */
|
||||
private $notices;
|
||||
|
||||
public function __construct( SitePress $sitepress, WPML_Notices $notices ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->notices = $notices;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wp_loaded', array( $this, 'init' ), 10 );
|
||||
}
|
||||
|
||||
public function init() {
|
||||
|
||||
if (
|
||||
$this->is_display_as_translated_mode() &&
|
||||
$this->current_language_is_not_default_language()
|
||||
) {
|
||||
$notice = $this->notices->create_notice( __CLASS__, $this->get_notice_content() );
|
||||
$notice->set_css_class_types( 'warning' );
|
||||
$notice->set_dismissible( true );
|
||||
|
||||
$this->notices->add_notice( $notice );
|
||||
} else {
|
||||
$this->notices->remove_notice( WPML_Notices::DEFAULT_GROUP, __CLASS__ );
|
||||
}
|
||||
}
|
||||
|
||||
public function get_notice_content() {
|
||||
$current_lang = $this->sitepress->get_language_details( $this->sitepress->get_current_language() );
|
||||
$default_lang = $this->sitepress->get_language_details( $this->sitepress->get_default_language() );
|
||||
|
||||
$post_type_name = $this->get_post_type();
|
||||
$post_type = get_post_type_object( $post_type_name );
|
||||
$singular_name = strtolower( $post_type->labels->singular_name );
|
||||
$plural_name = strtolower( $post_type->labels->name );
|
||||
|
||||
$output = esc_html(
|
||||
sprintf( __( "You are creating a %1\$s in %3\$s and you've set %2\$s to display even when not translated. Please note that this %1\$s will only appear in %3\$s. Only %2\$s that you create in the site's default language (%4\$s) will appear in all the site's languages.", 'sitepress' ),
|
||||
$singular_name,
|
||||
$plural_name,
|
||||
$current_lang['display_name'],
|
||||
$default_lang['display_name']
|
||||
)
|
||||
);
|
||||
$output .= '<br /><br />';
|
||||
$output .= '<a href="https://wpml.org/?page_id=1451509" target="_blank">' . esc_html__( 'Read how this works', 'sitepress' ) . '</a>';
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
private function is_display_as_translated_mode() {
|
||||
return $this->sitepress->is_display_as_translated_post_type( $this->get_post_type() );
|
||||
}
|
||||
|
||||
private function current_language_is_not_default_language() {
|
||||
return $this->sitepress->get_current_language() != $this->sitepress->get_default_language();
|
||||
}
|
||||
|
||||
private function get_post_type() {
|
||||
return isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Snippet_Filters_Factory implements IWPML_Frontend_Action_Loader, IWPML_Backend_Action_Loader {
|
||||
public function create() {
|
||||
return new WPML_Display_As_Translated_Snippet_Filters();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
class WPML_Display_As_Translated_Snippet_Filters implements IWPML_Action {
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter( 'wpml_should_use_display_as_translated_snippet', array( $this, 'filter_post_types' ), 10, 2 );
|
||||
}
|
||||
|
||||
public function filter_post_types( $should_use_snippet, array $post_type ) {
|
||||
return $should_use_snippet || $this->is_media_ajax_query( $post_type ) || $this->is_admin_media_list_page() || WPML_Ajax::is_frontend_ajax_request();
|
||||
}
|
||||
|
||||
private function is_admin_media_list_page() {
|
||||
if ( ! function_exists( 'get_current_screen' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$screen = get_current_screen();
|
||||
if (
|
||||
null !== $screen &&
|
||||
'upload' === $screen->base &&
|
||||
'list' === get_user_meta( get_current_user_id(), 'wp_media_library_mode', true )
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function is_media_ajax_query( array $post_type ) {
|
||||
return false !== strpos( $_SERVER['REQUEST_URI'], 'admin-ajax' )
|
||||
&& isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action']
|
||||
&& array_key_exists( 'attachment', $post_type );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: bruce
|
||||
* Date: 28/10/17
|
||||
* Time: 5:07 PM
|
||||
*/
|
||||
|
||||
class WPML_Fix_Links_In_Display_As_Translated_Content implements IWPML_Action, IWPML_Frontend_Action, IWPML_DIC_Action {
|
||||
|
||||
/** @var SitePress $sitepress */
|
||||
private $sitepress;
|
||||
|
||||
/** @var WPML_Translate_Link_Targets $translate_link_targets */
|
||||
private $translate_link_targets;
|
||||
|
||||
public function __construct( SitePress $sitepress, WPML_Translate_Link_Targets $translate_link_targets ) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->translate_link_targets = $translate_link_targets;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter(
|
||||
'the_content',
|
||||
array(
|
||||
$this,
|
||||
'fix_fallback_links',
|
||||
),
|
||||
WPML_LS_Render::THE_CONTENT_FILTER_PRIORITY - 1
|
||||
);
|
||||
}
|
||||
|
||||
public function fix_fallback_links( $content ) {
|
||||
if ( stripos( $content, '<a' ) !== false ) {
|
||||
if ( $this->is_display_as_translated_content_type() ) {
|
||||
list( $content, $encoded_ls_links ) = $this->encode_language_switcher_links( $content );
|
||||
$content = $this->translate_link_targets->convert_text( $content );
|
||||
$content = $this->decode_language_switcher_links( $content, $encoded_ls_links );
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function is_display_as_translated_content_type() {
|
||||
$queried_object = get_queried_object();
|
||||
if ( isset( $queried_object->post_type ) ) {
|
||||
return $this->sitepress->is_display_as_translated_post_type( $queried_object->post_type );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function encode_language_switcher_links( $content ) {
|
||||
$encoded_ls_links = array();
|
||||
|
||||
if ( preg_match_all( '/<a\s[^>]*class\s*=\s*"([^"]*)"[^>]*>/', $content, $matches ) ) {
|
||||
foreach ( $matches[1] as $index => $match ) {
|
||||
if ( strpos( $match, WPML_LS_Model_Build::LINK_CSS_CLASS ) !== false ) {
|
||||
$link = $matches[0][ $index ];
|
||||
$encoded_link = md5( $link );
|
||||
$encoded_ls_links[ $encoded_link ] = $link;
|
||||
$content = str_replace( $link, $encoded_link, $content );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array( $content, $encoded_ls_links );
|
||||
}
|
||||
|
||||
private function decode_language_switcher_links( $content, $encoded_ls_links ) {
|
||||
foreach ( $encoded_ls_links as $encoded => $link ) {
|
||||
$content = str_replace( $encoded, $link, $content );
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user