Skip to content

Instantly share code, notes, and snippets.

@Biont
Forked from thefuxia/mlp-addon-post-meta.php
Last active April 26, 2017 09:07

Revisions

  1. Biont revised this gist Jun 15, 2015. 1 changed file with 24 additions and 15 deletions.
    39 changes: 24 additions & 15 deletions mlp-addon-post-meta.php
    Original file line number Diff line number Diff line change
    @@ -10,58 +10,65 @@
    * 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';
    private $meta_key;

    /**
    * @type array
    */
    private $_post = array();
    private $send_post_meta = array();

    /**
    * @type array
    * Set up class variables
    *
    * @param $meta_key
    */
    private $send_post_meta = array();
    public function __construct( $meta_key ) {

    $this->meta_key = $meta_key;

    }

    public function setup() {

    add_action(
    'mlp_translation_meta_box_bottom',
    array ( $this, 'show_field' ),
    array( $this, 'show_field' ),
    2,
    3
    );

    add_filter(
    'mlp_pre_save_post_meta',
    array ( $this, 'register_meta_fields' ),
    array( $this, 'register_meta_fields' ),
    10,
    2
    );

    add_filter(
    'mlp_pre_insert_post_meta',
    array ( $this, 'save_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,
    $remote_site_id,
    WP_Post $remote_post = NULL
    ) {

    @@ -78,9 +85,9 @@ public function show_field(
    printf(
    '
    <label for="%1$s_%2$s_id">
    Insert a value
    Value for <strong>%1$s</strong>
    <input id="%1$s_%2$s_id" type="text" name="%1$s_%2$s" value="%3$s">
    </label>',
    </label><br>',
    $this->meta_key,
    $remote_site_id,
    $value
    @@ -90,7 +97,6 @@ public function show_field(
    public function register_meta_fields( array $post_meta ) {

    if ( ! empty ( $_POST ) ) {
    $this->_post = $_POST;

    foreach ( $_POST as $key => $value ) {

    @@ -104,7 +110,8 @@ public function register_meta_fields( array $post_meta ) {
    if ( ! empty ( $m[ 1 ] ) ) {
    $site_id = $m[ 1 ];
    $this->send_post_meta[ $site_id ] = $value;
    $post_meta[ $key ] = $value;
    } else {
    $post_meta[ $key ] = $value;
    }
    }
    }
    @@ -116,13 +123,15 @@ 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 ] ) )
    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' ) );
    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' ) );
  2. @toscho toscho revised this gist Mar 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mlp-addon-post-meta.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * 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: 03.03.15
    * Version: 2015.03.03
    * Required: 4.0
    * Author: Thomas Scholz <[email protected]>
    * Author URI: http://toscho.de
  3. @toscho toscho revised this gist Mar 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mlp-addon-post-meta.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php # -*- coding: utf-8 -*-
    /**
    * Plugin Name: MLP Addon: Sync post meta fields
    * Plugin URI: https://github.com/toscho/${SLUG}
    * 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: 03.03.15
    * Required: 4.0
  4. @toscho toscho created this gist Mar 3, 2015.
    128 changes: 128 additions & 0 deletions mlp-addon-post-meta.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,128 @@
    <?php # -*- coding: utf-8 -*-
    /**
    * Plugin Name: MLP Addon: Sync post meta fields
    * Plugin URI: https://github.com/toscho/${SLUG}
    * Description: Create a custom field, put it into the MLP translation box, and sync it across the sites.
    * Version: 03.03.15
    * 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' ) );