Skip to content

Instantly share code, notes, and snippets.

@clovepod
Last active March 8, 2016 02:55
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/5ac87da53dbc7544f7c3 to your computer and use it in GitHub Desktop.
Save clovepod/5ac87da53dbc7544f7c3 to your computer and use it in GitHub Desktop.
This is a section of the convert URLs plugin from Crowd Favorite with some modifications to handle our CMB2 fields that contain TinyMCE editors.
<?php
function sas_CMB2_convert() {
$sas_CMB2_TinyMCE_fields = array(
0 => array(
"CMB2_group" => "repeat_group_name", //name of the repeat group
"CMB2_field" => "field_name" //name of the field w/in the repeat group to translate
)
);
return $sas_CMB2_TinyMCE_fields;
}
class cfcu_deploy_callbacks {
// buncha functions I didn't need to modify...
// Transfer Callback Methods
public function cfcu_receive_callback($cfcu_settings) {
global $wpdb;
$success = true;
$message = __('No posts to update', 'cfcu');
$message = print_r($cfcu_settings, true);
$messages = array();
$prod_url = home_url('/', 'http');
$prod_url = preg_replace('/^http:/', '', $prod_url);
if (isset($cfcu_settings['guids']) && !empty($cfcu_settings['guids'])
&& isset($cfcu_settings['staging_url']) && !empty($cfcu_settings['staging_url'])) {
foreach ($cfcu_settings['guids'] as $guid => $true) {
$post_id = cfd_get_post_id_by_guid($guid);
$post = get_post($post_id);
// Begin SAS customizations
// We need to get the postmeta as well
$meta = get_post_meta( $post_id );
if (!$post) {
$messages[] = "couldn't get post $post_id from guid $guid";
continue;
}
$updated_post = array();
$updated_post['post_content'] = str_replace($cfcu_settings['staging_url'], $prod_url, $post->post_content);
$updated_post['post_excerpt'] = str_replace($cfcu_settings['staging_url'], $prod_url, $post->post_excerpt);
$result = $wpdb->update($wpdb->posts, $updated_post, array('ID' => $post->ID));
// We also have to convert content within CMB2 custom fields
// use WP functions to unserialize the meta, then update it after the url has been corrected.
//we want to do this conditionally, only if we have postmeta to be translated
//and we want to do this for any number of different CMB2 fields that contain a TinyMCE editor.
$sas_fields = sas_CMB2_convert(); // get our array of fields that need URL conversion
$updated_meta = array();
foreach ( $sas_fields as $fields ) {
$my_CMB2_group_name = $fields["CMB2_group"]; //meta_key in postmeta
$my_CMB2_field_name = $fields["CMB2_field"]; //after unserialization, name of field where our data to translate is kept
$my_CMB2_group_value = maybe_unserialize( $meta[$my_CMB2_group_name][0] );
if (isset ( $my_CMB2_group_value ) ) {
$a = 0; //counter
foreach ( $my_CMB2_group_value as $value ) {
$updated_meta[ $my_CMB2_group_name ] = str_replace( $cfcu_settings['staging_url'], $prod_url, $value[ $my_CMB2_field_name ] );
$value[ $my_CMB2_field_name ] = $updated_meta[ $my_CMB2_group_name ];
//push the updated value back onto the original unserialized postmeta array
$my_CMB2_group_value[ $a ][ $my_CMB2_field_name ] = $updated_meta[ $my_CMB2_group_name ];
$a ++;
}
// update_post_meta takes care of serialization for us
$meta_result = update_post_meta( $post->ID, $my_CMB2_group_name, $my_CMB2_group_value );
}
}
if ($result === false) {
$success = false;
$messages[] = sprintf(__('error updating %s %d (%s)', 'cfcu'), $post->post_type, $post->ID, $post->post_title);
}
else {
$messages[] = sprintf(__('updated %s %d (%s)', 'cfcu'), $post->post_type, $post->ID, $post->post_title);
}
}
}
if (!empty($messages)) {
$message = sprintf(__('Results: %s', 'cfcu'), implode('; ', $messages));
}
return array(
'success' => $success,
'message' => $message
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment