Last active
August 29, 2015 14:27
-
-
Save simonwheatley/aa842df4204535f649f2 to your computer and use it in GitHub Desktop.
A proof of concept for creating a custom translation UI for a Fieldmanager field group utilising repeatable fields
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 | |
/* | |
Plugin Name: A8c VIP Translatable Fields Test | |
Version: 1.0b1 | |
*/ | |
// Code pasted from Fieldmanager site | |
// using Fieldmanager for a slideshow - any number of slides, | |
// with any number of related links | |
add_action( 'fm_post_post', function() { | |
$fm = new Fieldmanager_Group( array( | |
'name' => 'slideshow', | |
'limit' => 0, | |
'label' => __( 'New Slide', 'your-domain' ), | |
'label_macro' => array( __( 'Slide: %s', 'your-domain' ), 'title' ), | |
'add_more_label' => __( 'Add another slide', 'your-domain' ), | |
'collapsed' => true, | |
'sortable' => true, | |
'children' => array( | |
'title' => new Fieldmanager_Textfield( __( 'Slide Title', 'your-domain' ) ), | |
'slide' => new Fieldmanager_Media( __( 'Slide', 'your-domain' ) ), | |
'description' => new Fieldmanager_RichTextarea( __( 'Description', 'your-domain' ) ), | |
'posts' => new Fieldmanager_Autocomplete( array( | |
'label' => __( 'Related Posts', 'your-domain' ), | |
'limit' => 0, | |
'sortable' => true, | |
'one_label_per_item' => false, | |
'add_more_label' => __( 'Add another related link', 'your-domain' ), | |
'datasource' => new Fieldmanager_Datasource_Post( array( | |
'query_args' => array( | |
'post_status' => 'any', | |
), | |
) ), | |
) ), | |
), | |
) ); | |
$fm->add_meta_box( __( 'Slides', 'your-domain' ), 'post' ); | |
} ); | |
class A8c_FM_Bbl_Meta_Field extends Babble_Meta_Field { | |
public function get_output() { | |
$outputs = ''; | |
foreach( $this->meta_value as $i => $slide ) { | |
$outputs .= '<p>Title ' . absint( $i ) . ': <var>' . esc_html( $slide['title'] ) . '</var></p>'; | |
} | |
return $outputs; | |
} | |
public function get_input( $name, $value ) { | |
$inputs = ''; | |
foreach( $this->meta_value as $i => $slide ) { | |
$translated_value = ''; | |
if ( isset($value[$i]['title']) ) { | |
$translated_value = $value[$i]['title']; | |
} | |
$inputs .= '<p>Title ' . absint( $i ) . ':' . sprintf( '<input type="text" name="%s" value="%s">', | |
esc_attr( $name .'[' . absint($i) . '][title]' ), | |
esc_attr( $translated_value ) | |
) . '</p>'; | |
} | |
return $inputs; | |
} | |
public function update( $submitted_value, WP_Post $job ) { | |
$new_value = array_replace_recursive( $this->meta_value, $submitted_value ); | |
// At this point we could also find the translation for the related posts referenced | |
// in the `posts` array for this slide. | |
return $new_value; | |
} | |
} | |
function fm_bbl_poc_meta_fields( array $fields, WP_Post $post ) { | |
$fields[ 'slideshow' ] = new A8c_FM_Bbl_Meta_Field( $post, 'slideshow', __( 'Fieldmanager Slideshow', 'fm_bbl_poc' ) ); | |
return $fields; | |
} | |
add_filter( 'bbl_translated_meta_fields', 'fm_bbl_poc_meta_fields', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment