first commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_API_Hook_Copy_Post_To_Language
|
||||
*/
|
||||
class WPML_API_Hook_Copy_Post_To_Language implements IWPML_Action {
|
||||
|
||||
/** @var WPML_Post_Duplication $post_duplication */
|
||||
private $post_duplication;
|
||||
|
||||
public function __construct( WPML_Post_Duplication $post_duplication ) {
|
||||
$this->post_duplication = $post_duplication;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter( 'wpml_copy_post_to_language', array( $this, 'copy_post_to_language' ), 10, 3 );
|
||||
}
|
||||
|
||||
public function copy_post_to_language( $post_id, $target_language, $mark_as_duplicate ) {
|
||||
$duplicate_post_id = $this->post_duplication->make_duplicate( $post_id, $target_language );
|
||||
|
||||
if( ! $mark_as_duplicate ) {
|
||||
delete_post_meta( $duplicate_post_id, '_icl_lang_duplicate_of' );
|
||||
}
|
||||
|
||||
return $duplicate_post_id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_API_Hook_Links
|
||||
*
|
||||
* This class provides various links by hooks
|
||||
*/
|
||||
class WPML_API_Hook_Links implements IWPML_Action {
|
||||
|
||||
const POST_TRANSLATION_SETTINGS_PRIORITY = 10;
|
||||
const LINK_TO_TRANSLATION_PRIORITY = 9;
|
||||
|
||||
/** @var WPML_Post_Status_Display_Factory */
|
||||
private $post_status_display_factory;
|
||||
|
||||
public function __construct(
|
||||
WPML_Post_Status_Display_Factory $post_status_display_factory
|
||||
) {
|
||||
$this->post_status_display_factory = $post_status_display_factory;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter(
|
||||
'wpml_get_post_translation_settings_link',
|
||||
array(
|
||||
$this,
|
||||
'get_post_translation_settings_link',
|
||||
),
|
||||
self::POST_TRANSLATION_SETTINGS_PRIORITY,
|
||||
1
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wpml_get_link_to_edit_translation',
|
||||
array(
|
||||
$this,
|
||||
'get_link_to_edit_translation',
|
||||
),
|
||||
self::LINK_TO_TRANSLATION_PRIORITY,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
public function get_post_translation_settings_link( $link ) {
|
||||
return admin_url( 'admin.php?page=' . WPML_PLUGIN_FOLDER . '/menu/translation-options.php#icl_custom_posts_sync_options' );
|
||||
}
|
||||
|
||||
public function get_link_to_edit_translation( $link, $post_id, $lang ) {
|
||||
$status_display = $this->post_status_display_factory->create();
|
||||
$status_data = $status_display->get_status_data( $post_id, $lang );
|
||||
|
||||
$status_link = $status_data[1];
|
||||
$trid = $status_data[2];
|
||||
$css_class = $status_data[3];
|
||||
|
||||
return apply_filters( 'wpml_link_to_translation', $status_link, $post_id, $lang, $trid, $css_class );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
class WPML_API_Hook_Permalink implements IWPML_Action {
|
||||
|
||||
/** @var WPML_URL_Converter $url_converter */
|
||||
private $url_converter;
|
||||
|
||||
/** @var IWPML_Resolve_Object_Url $absolute_resolver */
|
||||
private $absolute_resolver;
|
||||
|
||||
public function __construct( WPML_URL_Converter $url_converter, IWPML_Resolve_Object_Url $absolute_resolver ) {
|
||||
$this->url_converter = $url_converter;
|
||||
$this->absolute_resolver = $absolute_resolver;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter( 'wpml_permalink', array( $this, 'wpml_permalink_filter' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param null|string $lang
|
||||
* @param bool $absolute_url If `true`, WPML will try to resolve the object behind the URL
|
||||
* and try to find the matching translation's URL.
|
||||
* WARNING: This is a heavy process which could lead to performance hit.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function wpml_permalink_filter( $url, $lang = null, $absolute_url = false ) {
|
||||
if ( $absolute_url ) {
|
||||
$new_url = $this->absolute_resolver->resolve_object_url( $url, $lang );
|
||||
|
||||
if ( $new_url ) {
|
||||
$url = $new_url;
|
||||
}
|
||||
} else {
|
||||
$url = $this->url_converter->convert_url( $url, $lang );
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
class WPML_API_Hook_Sync_Custom_Fields implements IWPML_Action {
|
||||
|
||||
/** @var WPML_Sync_Custom_Fields $sync_custom_fields */
|
||||
private $sync_custom_fields;
|
||||
|
||||
public function __construct( WPML_Sync_Custom_Fields $sync_custom_fields ) {
|
||||
$this->sync_custom_fields = $sync_custom_fields;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_action( 'wpml_sync_custom_field', array( $this, 'sync_custom_field' ), 10, 2 );
|
||||
add_action( 'wpml_sync_all_custom_fields', array( $this, 'sync_all_custom_fields' ), 10, 1 );
|
||||
}
|
||||
|
||||
public function sync_custom_field( $post_id, $custom_field_name ) {
|
||||
$this->sync_custom_fields->sync_to_translations( $post_id, $custom_field_name );
|
||||
}
|
||||
|
||||
public function sync_all_custom_fields( $post_id ) {
|
||||
$this->sync_custom_fields->sync_all_custom_fields( $post_id );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author OnTheGo Systems
|
||||
*/
|
||||
class WPML_API_Hook_Translation_Element implements IWPML_Action {
|
||||
private $flags_factory;
|
||||
private $sitepress;
|
||||
private $translation_element_factory;
|
||||
|
||||
/**
|
||||
* WPML_API_Hook_Post constructor.
|
||||
*
|
||||
* @param SitePress $sitepress
|
||||
* @param WPML_Translation_Element_Factory $translation_element_factory
|
||||
* @param WPML_Flags_Factory $flags_factory
|
||||
*/
|
||||
public function __construct(
|
||||
SitePress $sitepress,
|
||||
WPML_Translation_Element_Factory $translation_element_factory,
|
||||
WPML_Flags_Factory $flags_factory
|
||||
) {
|
||||
$this->sitepress = $sitepress;
|
||||
$this->translation_element_factory = $translation_element_factory;
|
||||
$this->flags_factory = $flags_factory;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
/**
|
||||
* Use this filter to obtain the language flag URL of a given post
|
||||
*
|
||||
* @param string $default
|
||||
* @param int $element_id
|
||||
* @param string $element_type any of `WPML_Translation_Element_Factory::ELEMENT_TYPE_POST`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_TERM`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_MENU`
|
||||
*/
|
||||
add_filter( 'wpml_post_language_flag_url', array( $this, 'get_post_language_flag_url' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $default
|
||||
* @param int $element_id
|
||||
* @param string $element_type any of `WPML_Translation_Element_Factory::ELEMENT_TYPE_POST`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_TERM`, `WPML_Translation_Element_Factory::ELEMENT_TYPE_MENU`
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_post_language_flag_url(
|
||||
$default,
|
||||
$element_id,
|
||||
$element_type = WPML_Translation_Element_Factory::ELEMENT_TYPE_POST
|
||||
) {
|
||||
if ( ! $element_id ) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$wpml_post = $this->translation_element_factory->create( $element_id, $element_type );
|
||||
$flag = $this->flags_factory->create();
|
||||
|
||||
return $flag->get_flag_url( $wpml_post->get_language_code() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
class WPML_API_Hook_Translation_Mode implements IWPML_Action {
|
||||
|
||||
const OPTION_KEY = 'custom_posts_sync_option';
|
||||
|
||||
/** Allowed modes */
|
||||
const DO_NOT_TRANSLATE = 'do_not_translate';
|
||||
const TRANSLATE = 'translate';
|
||||
const DISPLAY_AS_TRANSLATED = 'display_as_translated';
|
||||
|
||||
/** @var WPML_Settings_Helper $settings */
|
||||
private $settings;
|
||||
|
||||
public function __construct( WPML_Settings_Helper $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
if ( is_admin() ) {
|
||||
add_action( 'wpml_set_translation_mode_for_post_type', array( $this, 'set_mode_for_post_type' ), 10, 2 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $post_type
|
||||
* @param string $translation_mode any of
|
||||
* `WPML_API_Hook_Translation_Mode::DO_NOT_TRANSLATE`,
|
||||
* `WPML_API_Hook_Translation_Mode::TRANSLATE`,
|
||||
* `WPML_API_Hook_Translation_Mode::DISPLAY_AS_TRANSLATED`
|
||||
*/
|
||||
public function set_mode_for_post_type( $post_type, $translation_mode ) {
|
||||
switch ( $translation_mode ) {
|
||||
case self::DO_NOT_TRANSLATE:
|
||||
$this->settings->set_post_type_not_translatable( $post_type );
|
||||
break;
|
||||
|
||||
case self::TRANSLATE:
|
||||
$this->settings->set_post_type_translatable( $post_type );
|
||||
break;
|
||||
|
||||
case self::DISPLAY_AS_TRANSLATED:
|
||||
$this->settings->set_post_type_display_as_translated( $post_type );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
class WPML_API_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
global $wpdb, $sitepress, $wpml_post_translations, $wpml_url_converter;
|
||||
|
||||
$hooks = array();
|
||||
|
||||
$hooks[] = new WPML_API_Hook_Sync_Custom_Fields(
|
||||
new WPML_Sync_Custom_Fields(
|
||||
new WPML_Translation_Element_Factory( $sitepress ),
|
||||
$sitepress->get_custom_fields_translation_settings( WPML_COPY_CUSTOM_FIELD )
|
||||
)
|
||||
);
|
||||
|
||||
$hooks[] = new WPML_API_Hook_Links( new WPML_Post_Status_Display_Factory( $sitepress ) );
|
||||
|
||||
$hooks[] = new WPML_API_Hook_Translation_Element(
|
||||
$sitepress,
|
||||
new WPML_Translation_Element_Factory( $sitepress ),
|
||||
new WPML_Flags_Factory( $wpdb )
|
||||
);
|
||||
|
||||
$hooks[] = new WPML_API_Hook_Translation_Mode( new WPML_Settings_Helper( $wpml_post_translations, $sitepress ) );
|
||||
|
||||
$hooks[] = new WPML_API_Hook_Copy_Post_To_Language( new WPML_Post_Duplication( $wpdb, $sitepress ) );
|
||||
|
||||
$url_resolver_factory = new WPML_Resolve_Object_Url_Helper_Factory();
|
||||
$absolute_resolver = $url_resolver_factory->create( WPML_Resolve_Object_Url_Helper_Factory::ABSOLUTE_URL_RESOLVER );
|
||||
$hooks[] = new WPML_API_Hook_Permalink( $wpml_url_converter, $absolute_resolver );
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class WPML_TM_API_Hook_Links
|
||||
*
|
||||
* This class provides various links by hooks
|
||||
*/
|
||||
class WPML_TM_API_Hook_Links implements IWPML_Action {
|
||||
|
||||
public function add_hooks() {
|
||||
// TODO: Use WPML_API_Hook_Links::POST_TRANSLATION_SETTINGS_PRIORITY + 1 instead of the hardcoded 11.
|
||||
// It's done this way right now so there's no potential for an error if TM is updated before Core for
|
||||
// the minor 3.9.1 release
|
||||
add_filter(
|
||||
'wpml_get_post_translation_settings_link',
|
||||
array(
|
||||
$this,
|
||||
'get_post_translation_settings_link',
|
||||
),
|
||||
11,
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
public function get_post_translation_settings_link( $link ) {
|
||||
return admin_url( 'admin.php?page=' . WPML_TM_FOLDER . WPML_Translation_Management::PAGE_SLUG_SETTINGS . '&sm=mcsetup#icl_custom_posts_sync_options' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_API_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
|
||||
|
||||
public function create() {
|
||||
$hooks = array();
|
||||
|
||||
$hooks[] = new WPML_TM_API_Hook_Links();
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user