Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WPML compatibility #1615

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions admin/blocks/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public function populateGlobalData( $globalData ) {
}

$blocks = get_posts( array(
'post_type' => Brizy_Admin_Blocks_Main::CP_GLOBAL,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
'post_type' => Brizy_Admin_Blocks_Main::CP_GLOBAL,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
'suppress_filters' => false,
) );

foreach ( $blocks as $block ) {
Expand All @@ -71,11 +72,12 @@ public function populateGlobalData( $globalData ) {
}

$blocks = get_posts( array(
'post_type' => Brizy_Admin_Blocks_Main::CP_SAVED,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
'post_type' => Brizy_Admin_Blocks_Main::CP_SAVED,
'posts_per_page' => - 1,
'post_status' => 'publish',
'orderby' => 'ID',
'order' => 'ASC',
'suppress_filters' => false,
) );

foreach ( $blocks as $block ) {
Expand Down
2 changes: 1 addition & 1 deletion compatibilities/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private function load_compatibilites() {
new Brizy_Compatibilities_Autoptimize();
}

if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
if ( apply_filters( 'wpml_setting', false, 'setup_complete' ) ) {
new Brizy_Compatibilities_WPML();
}

Expand Down
78 changes: 77 additions & 1 deletion compatibilities/wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class Brizy_Compatibilities_WPML {
public function __construct() {
add_action( 'wp_insert_post', array( $this, 'insertNewPost' ), -10000, 3 );
add_action( 'wp_insert_post', array( $this, 'duplicatePosts' ), -10000, 3 );

add_filter( 'wpml_decode_custom_field', array( $this, 'decode_custom_field' ), 10, 2 );
add_filter( 'wpml_encode_custom_field', array( $this, 'encode_custom_field' ), 10, 2 );
add_filter( 'wpml_pb_should_body_be_translated', array( $this, 'remove_body' ), 10, 2 );
add_action( 'wpml_pro_translation_completed', array( $this, 'save_post' ), 10, 3 );

add_filter( 'wpml_basket_base64_item', '__return_false' );
add_filter( 'wpml_document_view_item_link', '__return_empty_string' );
add_filter( 'wpml_document_edit_item_link', '__return_empty_string' );
}

/**
Expand Down Expand Up @@ -65,4 +74,71 @@ public function insertNewPost($postId,$post) {
}
}
}
}

/**
* Decodes editor_data to send for translation.
*
* @param array $value The value of the custom field.
* @param string $key The key of the custom field.
* @return array
*/
public function decode_custom_field( $value, $key ) {
// We only need to handle this for 'brizy' custom field.
if ( 'brizy' === $key ) {
// WPML calls this filter twice, so we need to check if it was already decoded.
if ( isset( $value['brizy-post']['editor_data'] ) && is_scalar( $value['brizy-post']['editor_data'] ) ) {
$value['brizy-post']['editor_data'] = json_decode( base64_decode( $value['brizy-post']['editor_data'], true ), true );
}
}
return $value;
}

/**
* Encode editor_data after its translated.
*
* @param array $value The value of the custom field.
* @param string $key The key of the custom field.
* @return array
*/
public function encode_custom_field( $value, $key ) {
// We only need to handle this for 'brizy' custom field.
if ( 'brizy' === $key ) {
// WPML calls this filter twice, so we need to check if it was already encoded.
if ( isset( $value['brizy-post']['editor_data'] ) && ! is_scalar( $value['brizy-post']['editor_data'] ) ) {
$value['brizy-post']['editor_data'] = base64_encode( json_encode( $value['brizy-post']['editor_data'] ) );
}
}
return $value;
}

/**
* Remove body from the translation editor because it is generated automatically.
*
* @param bool $translate Does body need to be translated.
* @param WP_Post $post The post we are translating.
* @return bool
*/
public function remove_body( $translate, $post ) {
$brizyPost = Brizy_Editor_Post::get( $post );
if ( $brizyPost->uses_editor() ) {
$translate = false;
}
return $translate;
}

/**
* Compile translated brizy pages.
*
* @param int $post_id The ID of the translated post.
*/
public function save_post( $post_id, $fields, $job ) {
$originalPost = Brizy_Editor_Post::get( $job->original_doc_id );
if ( $originalPost->uses_editor() ) {
$translatedPost = Brizy_Editor_Post::get( $post_id );
if ( ! $translatedPost->uses_editor() ) {
$translatedPost->set_uses_editor( true );
}
$translatedPost->set_needs_compile( true );
}
}
}
11 changes: 6 additions & 5 deletions editor/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ protected function populateAutoSavedData( $autosave ) {
public static function getBlocksByType( $type, $arags = array() ) {

$filterArgs = array(
'post_type' => $type,
'posts_per_page' => - 1,
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'ASC',
'post_type' => $type,
'posts_per_page' => - 1,
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'ASC',
'suppress_filters' => false,
);
$filterArgs = array_merge( $filterArgs, $arags );

Expand Down
143 changes: 143 additions & 0 deletions wpml-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<wpml-config>
<custom-types>
<custom-type translate="1" display-as-translated="1">brizy-global-block</custom-type>
<custom-type translate="1" display-as-translated="1">brizy-saved-block</custom-type>
<custom-type translate="1" display-as-translated="1">brizy_template</custom-type>
</custom-types>
<custom-fields>
<custom-field action="copy">brizy_post_uid</custom-field>
<custom-field action="ignore">brizy-need-compile</custom-field>
<custom-field action="copy">brizy-post-plugin-version</custom-field>
<custom-field action="copy">brizy-post-editor-version</custom-field>
<custom-field action="copy">brizy-post-compiler-version</custom-field>
<custom-field action="translate">brizy</custom-field>
</custom-fields>
<custom-fields-texts>
<key name="brizy">
<key name="brizy-post">
<key name="editor_data">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" /></key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" />
<key name="items">
<key name="*">
<key name="value">
<key name="text" /></key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</key>
</custom-fields-texts>
</wpml-config>