class-wpml-pb-handle-post-body.php
1.53 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
<?php
class WPML_PB_Handle_Post_Body implements IWPML_Backend_Action, IWPML_Frontend_Action, IWPML_DIC_Action {
private $page_builders_built;
public function __construct( WPML_Page_Builders_Page_Built $page_builders_built ) {
$this->page_builders_built = $page_builders_built;
}
public function add_hooks() {
add_filter( 'wpml_pb_should_body_be_translated', array( $this, 'should_translate' ), 10, 3 );
add_action( 'wpml_pb_finished_adding_string_translations', array( $this, 'copy' ), 10, 3 );
}
/**
* @param int $translate
* @param WP_Post $post
*
* @return int
*/
public function should_translate( $translate, WP_Post $post ) {
return $this->page_builders_built->is_page_builder_page( $post ) ? 0 : $translate;
}
/**
* @param int $new_post_id
* @param int $original_post_id
* @param array $fields
*/
public function copy( $new_post_id, $original_post_id, array $fields ) {
$original_post = get_post( $original_post_id );
if ( $original_post && $this->page_builders_built->is_page_builder_page( $original_post ) && ! $this->job_has_packages( $fields ) ) {
wpml_update_escaped_post( [ 'ID' => $new_post_id, 'post_content' => $original_post->post_content ] );
do_action( 'wpml_pb_after_page_without_elements_post_content_copy', $new_post_id, $original_post_id );
}
}
/**
* @param array $fields
*
* @return bool
*/
private function job_has_packages( array $fields ) {
foreach ( $fields as $key => $field ) {
if ( 0 === strpos( $key, 'package' ) ) {
return true;
}
}
return false;
}
}