Skip to content

Instantly share code, notes, and snippets.

@clovepod
Last active March 8, 2016 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clovepod/d4c18ce23eab6d977ceb to your computer and use it in GitHub Desktop.
Save clovepod/d4c18ce23eab6d977ceb to your computer and use it in GitHub Desktop.
Plugin to get RAMP to translate post ID's in CMB2 repeating fields. This relies not only on RAMP, but also on the postmeta translation plugin that is distributed to RAMP customers. Parts of this were written by a Crowd Favorite staffer, but not released under any license.
<?php
/**
* Plugin Name: SAS Translate CMB2 repeating
* Description: Get post ID's that need translation with RAMP when they are in a repeating custom field
* Version: 0.1
* Author: Lisa Linn Allen, SAS
*/
/**
*
* Since each data structure is different, you will need to take care of unserializing, pulling out the
* relevant data, etc. for each structure. This can get a bit complicated in dynamic structures and is
* generally recommended being done in a separate set of functions for each data structure, if they’re
* very different.
*
* You will need a list of IDs to translate. The foreach simply adds them to the
* list of post IDs that get translated.
*
*/
function sas_ramp_repeating_myfields() {
$groups = array(
0 => array(
"repeat_group" => "repeat_group_name", //name of the CMB2 repeat group
"image_id" => "field_name" //name of the field w/in the group to translate
),
2 => array(
"repeat_group" => "repeat_group_name", //name of the CMB2 repeat group
"image_id" => 99 //set to 99 if the id is held in an array key
//this was a special case we had to handle
)
);
return $groups;
}
/**
* @param $post_id the ID of the post whose attachments we need to find and RAMPify
* @param $mm is the object that holds the Meta Translation plugin instance
* @param $groups is the array of repeat groups that we need to iterate through to get all affected attachment IDs
*/
function sas_ramp_repeating_after_process_post( $post_id, $mm ) {
//array of repeating fields to be processed
$groups = sas_ramp_repeating_myfields();
$new_ids = array();
$ids = array();
$my_post_meta = get_post_meta( $post_id );
$stopHere = 0;
foreach ($groups as $group ) {
if ( 1 == $stopHere ) { continue; }
$repeat_group = $group["repeat_group"];
if (isset ( $my_post_meta[ $repeat_group ] ) ) {
$my_repeat_group = maybe_unserialize( $my_post_meta[ $repeat_group ][0] );
//shove the image id in each part of this array on to our array of ids to translate
$my_image_id = $group["image_id"];
if ( 99 == $my_image_id ) {
$ids = array_merge( $ids, array_keys( $my_repeat_group ) );
$stopHere = 1; // In this case, we only need to do this once, set a flag to break the loop.
} else {
foreach ( $my_repeat_group as $rgroup ) {
$actual_image_id = $rgroup[$my_image_id];
array_push( $ids, $actual_image_id );
}
}
}
}
// Process each ID
foreach ( $ids as $id ) {
// We only need to add an ID once.
if ( ! in_array( $id, $new_ids ) ) {
$new_ids[] = $id;
$post = get_post ( $id );
if ( ! in_array( $post->ID, $mm->existing_ids ) ) {
if ( ! is_array( $mm->data['post_types'][ $post->post_type ] ) ) {
$mm->data['post_types'][ $post->post_type ] = array();
}
$mm->data['post_types'][ $post->post_type ][] = $post->ID;
$mm->existing_ids[] = $post->ID;
// Use for processes and notices to alert user that posts were added
$mm->added_posts[ $post->ID ] = array(
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'guid' => $post->guid,
);
}
}
}
}
add_action( 'ramp_after_process_post', 'sas_ramp_repeating_after_process_post', 10, 2 );
/**
* Production processing to receive and iterate over batched posts by GUID,
* make the necessary ID replacements in the [dcp_embed] short codes, and then
* replace the shortcodes, updating the server post.
*
* This makes use of the same $groups array as sas_ramp_repeating_after_process_post
*/
function sas_ramp_repeating_after_do_batch_extra_receive ( $extra_data, $extra_id, $batch_args, $batch_guids, $mapped_keys ) {
$groups = sas_ramp_repeating_myfields();
// Loop through list of guids sent in the batch
foreach ( $batch_guids as $guid ) {
$server_post = cfd_get_post_by_guid( $guid );
if ( $server_post ) {
// get the meta data and iterate, using the "old_id" in the data
$my_post_meta = get_post_meta( $server_post->ID );
foreach ($groups as $group) {
$repeat_group = $group["repeat_group"];
// the repeat group will be in the post meta
if (isset ( $my_post_meta[$repeat_group] ) ) {
$my_repeat_group = maybe_unserialize( $my_post_meta[ $repeat_group ][0] );
$my_image_id = $group["image_id"];
if ( 99 == $my_image_id ) {
$ids = array_keys( $my_repeat_group );
$img_guids = array_values( $my_repeat_group );
$new_ids = array();
for ( $a=0; $a < sizeof( $ids ); $a++ ) {
// translate as necessary using this code:
$new_id = cfd_get_post_by_guid( $batch_args['mapped_posts'][ $ids[$a] ]['guid'] );
// build an array of the new ids - these will be keys
array_push( $new_ids, $new_id->ID );
}
$shiny_and_new = array_combine( $new_ids, $img_guids );
// store the data for the $server_post
// update_post_meta will do the serialization for us, do not need to call
// an additional function for that.
update_post_meta( $server_post->ID, $repeat_group, $shiny_and_new );
} else {
for ($a=0; sizeof($my_repeat_group)>$a; $a++ ) {
$old_id = $my_repeat_group[$a][ $my_image_id ];
$new_id = cfd_get_post_by_guid( $batch_args['mapped_posts'][ $old_id ]['guid'] );
// replace $old_id with $new_id
$my_repeat_group[$a][ $my_image_id ] = $new_id->ID;
}
update_post_meta( $server_post->ID, $repeat_group, $my_repeat_group );
}
}
}
}
}
}
add_action( 'ramp_after_do_batch_extra_receive', 'sas_ramp_repeating_after_do_batch_extra_receive', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment