wpml-st-slug-translation-strings-sync.php
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class WPML_ST_Slug_Translation_Strings_Sync implements IWPML_Action {
/** @var WPML_Slug_Translation_Records_Factory $slug_records_factory */
private $slug_records_factory;
/** @var WPML_ST_Slug_Translation_Settings_Factory $slug_settings_factory */
private $slug_settings_factory;
public function __construct(
WPML_Slug_Translation_Records_Factory $slug_records_factory,
WPML_ST_Slug_Translation_Settings_Factory $slug_settings_factory
) {
$this->slug_records_factory = $slug_records_factory;
$this->slug_settings_factory = $slug_settings_factory;
}
public function add_hooks() {
add_action( 'registered_taxonomy', array( $this, 'run_taxonomy_sync' ), 10, 3 );
add_action( 'registered_post_type', array( $this, 'run_post_type_sync' ), 10, 2 );
}
/**
* @param string $taxonomy
* @param string|array $object_type
* @param array $taxonomy_array
*/
public function run_taxonomy_sync( $taxonomy, $object_type, $taxonomy_array ) {
if ( isset( $taxonomy_array['rewrite']['slug'] ) && $taxonomy_array['rewrite']['slug'] ) {
$this->sync_element_slug( $taxonomy_array['rewrite']['slug'], $taxonomy, WPML_Slug_Translation_Factory::TAX );
}
}
/**
* @param string $post_type
* @param WP_Post_Type $post_type_object
*/
public function run_post_type_sync( $post_type, $post_type_object ) {
if ( isset( $post_type_object->rewrite['slug'] ) && $post_type_object->rewrite['slug'] ) {
$this->sync_element_slug( trim( $post_type_object->rewrite['slug'], '/' ), $post_type, WPML_Slug_Translation_Factory::POST );
}
}
/**
* @param string $rewrite_slug
* @param string $type
* @param string $element_type
*/
public function sync_element_slug( $rewrite_slug, $type, $element_type ) {
$settings = $this->slug_settings_factory->create( $element_type );
if ( ! $settings->is_translated( $type ) ) {
return;
}
$records = $this->slug_records_factory->create( $element_type );
$slug = $records->get_slug( $type );
if ( $slug->get_original_value() !== $rewrite_slug ) {
$records->update_original_slug( $type, $rewrite_slug );
}
}
}