Last active
November 26, 2015 09:00
-
-
Save thefuxia/6393c79aacba0983a96a to your computer and use it in GitHub Desktop.
MLP Addon: Sync post meta fields
This file contains 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 = '_mlp_pm_sync'; | |
/** | |
* @type array | |
*/ | |
private $_post = array(); | |
/** | |
* @type array | |
*/ | |
private $send_post_meta = array(); | |
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"> | |
Insert a value | |
<input id="%1$s_%2$s_id" type="text" name="%1$s_%2$s" value="%3$s"> | |
</label>', | |
$this->meta_key, | |
$remote_site_id, | |
$value | |
); | |
} | |
public function register_meta_fields( array $post_meta ) { | |
if ( ! empty ( $_POST ) ) { | |
$this->_post = $_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; | |
$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(), 'setup' ) ); |
@lkraav right, not necessary in this example. But is good style to set the $_POST
to a internal var, like $this->_post
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the purpose of L93?
Doesn't seem to be used anywhere afterwards.