-
-
Save Biont/8e6552f060ee519eb991 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: MLP Addon: Sync post meta fields | |
* Plugin URI: https://gist.github.com/toscho/6393c79aacba0983a96a | |
* Description: Create a custom field, put it into the MLP translation box, and sync it across the sites. | |
* Version: 2015.03.03 | |
* Required: 4.0 | |
* Author: Thomas Scholz <[email protected]> | |
* Author URI: http://toscho.de | |
* License: MIT | |
* License URI: http://www.opensource.org/licenses/mit-license.php | |
*/ | |
class Mlp_Support_Post_Meta_Sync { | |
/** | |
* @type string | |
*/ | |
private $meta_key; | |
/** | |
* @type array | |
*/ | |
private $send_post_meta = array(); | |
/** | |
* Set up class variables | |
* | |
* @param $meta_key | |
*/ | |
public function __construct( $meta_key ) { | |
$this->meta_key = $meta_key; | |
} | |
public function setup() { | |
add_action( | |
'mlp_translation_meta_box_bottom', | |
array( $this, 'show_field' ), | |
2, | |
3 | |
); | |
add_filter( | |
'mlp_pre_save_post_meta', | |
array( $this, 'register_meta_fields' ), | |
10, | |
2 | |
); | |
add_filter( | |
'mlp_pre_insert_post_meta', | |
array( $this, 'save_meta' ), | |
10, | |
2 | |
); | |
} | |
/** | |
* @wp-hook mlp_translation_meta_box_bottom | |
* | |
* @param WP_Post $post | |
* @param int $remote_site_id | |
* @param WP_Post $remote_post | |
* | |
* @return void | |
*/ | |
public function show_field( | |
WP_Post $post, | |
$remote_site_id, | |
WP_Post $remote_post = NULL | |
) { | |
$value = ''; | |
if ( NULL !== $remote_post ) { | |
switch_to_blog( $remote_site_id ); | |
$value = get_post_meta( $remote_post->ID, $this->meta_key, TRUE ); | |
restore_current_blog(); | |
} | |
$value = esc_attr( $value ); | |
printf( | |
' | |
<label for="%1$s_%2$s_id"> | |
Value for <strong>%1$s</strong> | |
<input id="%1$s_%2$s_id" type="text" name="%1$s_%2$s" value="%3$s"> | |
</label><br>', | |
$this->meta_key, | |
$remote_site_id, | |
$value | |
); | |
} | |
public function register_meta_fields( array $post_meta ) { | |
if ( ! empty ( $_POST ) ) { | |
foreach ( $_POST as $key => $value ) { | |
if ( 0 !== strpos( $key, $this->meta_key ) ) { | |
continue; | |
} | |
$m = array(); | |
preg_match( '~_(\d+)$~', $key, $m ); | |
if ( ! empty ( $m[ 1 ] ) ) { | |
$site_id = $m[ 1 ]; | |
$this->send_post_meta[ $site_id ] = $value; | |
} else { | |
$post_meta[ $key ] = $value; | |
} | |
} | |
} | |
return $post_meta; | |
} | |
public function save_meta( array $post_meta, array $save_context ) { | |
$site_id = $save_context[ 'target_blog_id' ]; | |
if ( empty ( $this->send_post_meta[ $site_id ] ) ) { | |
return $post_meta; | |
} | |
$post_meta[ $this->meta_key ] = $this->send_post_meta[ $site_id ]; | |
return $post_meta; | |
} | |
} | |
add_action( 'mlp_and_wp_loaded', array( new Mlp_Support_Post_Meta_Sync( 'column_left' ), 'setup' ) ); | |
add_action( 'mlp_and_wp_loaded', array( new Mlp_Support_Post_Meta_Sync( 'column_right' ), 'setup' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Biont,
I want to translate post connections with mlp. Do you have an idea, how it works?
When I implement your Post-Meta Add-on, I only have to add the code to the post-translator folder?
Thank you a lot!