Skip to content

Instantly share code, notes, and snippets.

@boswall
Last active February 27, 2018 11:43
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 boswall/c9ccd723f9664d3dfac7cbb198b9b990 to your computer and use it in GitHub Desktop.
Save boswall/c9ccd723f9664d3dfac7cbb198b9b990 to your computer and use it in GitHub Desktop.
Export from the old CCTM plugin to use WP native custom post types and CMB2
<?php
/**
* Plugin Name: Boswall's CCTM exporter
* Plugin URI: https://glaikit.co.uk/
* Description: Export from the old CCTM plugin to use WP native custom post types and CMB2
* Version: 1.0
* Author: Matt Rose
* Author URI: https://glaikit.co.uk/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if (!defined('WPINC')) {
die;
}
// Add to the WP menu
add_action('admin_menu', 'boswall_cctm_export_plugin_menu');
function boswall_cctm_export_plugin_menu() {
add_management_page('Boswall\'s CCTM exporter', 'CCTM exporter', 'read', 'boswall-cctm-export', 'boswall_cctm_export_export_page');
}
function boswall_cctm_export_concat_array( $value, $next_value ) {
if ( !$next_value ) return '';
if ( !$value ) return "'" . $next_value . "'";
return $value . ", '" . $next_value . "'";
}
// catch the register post type args and display them as valid PHP
function boswall_cctm_export_catch_post_type_args ( $args, $post_type ) {
?>
// Register the <?php echo $post_type; ?> post type
function <?php echo $post_type; ?>_post_type() {
$args = <?php var_export( $args ); ?>;
register_post_type( '<?php echo $post_type; ?>', $args );
}
add_action( 'init', '<?php echo $post_type; ?>_post_type', 0 );
<?php
return $args;
}
function boswall_cctm_export_export_cmb2( $metaboxes ) {
$field_defs = CCTM::get_custom_field_defs();
foreach ( $metaboxes as $metabox_id => $metabox ) {
// print_r( $metabox );
?>
// Define the <?php echo $metabox_id; ?> metabox and field configurations.
function cmb2_<?php echo $metabox_id; ?>_metabox() {
$cmb = new_cmb2_box( array(
'id' => '<?php echo $metabox_id; ?>',
'title' => '<?php echo $metabox['title']; ?>',
'object_types' => array( <?php echo array_reduce( $metabox['post_types'], 'boswall_cctm_export_concat_array' ); ?> ), // Post type
'context' => '<?php echo $metabox['context']; ?>',
'priority' => '<?php echo $metabox['priority']; ?>',
'show_names' => true, // Show field names on the left
) );
<?php
foreach ( $metabox['custom_fields'] as $custom_field ) :
echo '/* CCTM field definition = ' . print_r( $field_defs[$custom_field], true ) . ' */';
?>
$cmb->add_field( array(
'name' => '<?php echo $field_defs[$custom_field]['label']; ?>',
'desc' => '<?php echo $field_defs[$custom_field]['description']; ?>',
'id' => '<?php echo $field_defs[$custom_field]['name']; ?>',
'type' => '<?php echo $field_defs[$custom_field]['type']; ?>',
) );
<?php endforeach; ?>
}
add_action( 'cmb2_admin_init', 'cmb2_<?php echo $metabox_id; ?>_metabox' );
<?php
}
}
// Show the page
function boswall_cctm_export_export_page() {
add_filter( 'register_post_type_args', 'boswall_cctm_export_catch_post_type_args', 1, 2 );
$post_type_defs = CCTM::get_post_type_defs();
$post_types = array();
$metaboxes = array();
foreach ( $post_type_defs as $post_type_name => $post_type ) {
if ( isset($post_type['is_active']) && !empty($post_type['is_active']) && !in_array($post_type_name, CCTM::$built_in_post_types) && isset($post_type['post_type']) ) {
$post_types[] = $post_type['post_type'];
}
foreach ( $post_type['custom_fields'] as $custom_field ) {
// get metabox name
$metabox_name = 'cctm_default';
if ( isset( $post_type['map_field_metabox'][$custom_field] ) ) {
$metabox_name = $post_type['map_field_metabox'][$custom_field];
}
$metabox = $post_type_name . '_' . $metabox_name;
// get metabox definition as needed
if ( !isset( $metaboxes[$metabox] ) ) {
// Default metabox def
$metabox_def = CCTM::$metabox_def;
if ( isset( CCTM::$data['metabox_defs'][$metabox_name] ) ) {
$metabox_def = CCTM::$data['metabox_defs'][$metabox_name];
}
$metaboxes[$metabox] = $metabox_def;
}
// add post type to metabox as needed
if ( !in_array( $post_type_name, $metaboxes[$metabox]['post_types'] ) ) {
$metaboxes[$metabox]['post_types'][] = $post_type_name;
}
if ( !isset( $metaboxes[$metabox]['custom_fields'] ) ) {
$metaboxes[$metabox]['custom_fields'] = array();
}
if ( !in_array( $custom_field, $metaboxes[$metabox]['custom_fields'] ) ) {
$metaboxes[$metabox]['custom_fields'][] = $custom_field;
}
}
}
?>
<div class="wrap">
<h1>Boswall's CCTM exporter</h1>
<p>Found <?php echo count( $post_types ); ?> post types:
<?php foreach( $post_types as $post_type ) { echo '<code>' . $post_type . '</code>, '; } ?>
</p>
<textarea class="widefat" style="height: 75vh;">
<?php CCTM::register_custom_post_types(); ?>
</textarea>
<p>Found <?php echo count( $metaboxes ); ?> metaboxes:</p>
<textarea class="widefat" style="height: 75vh;">
<?php //print_r( $metaboxes ); ?>
<?php boswall_cctm_export_export_cmb2( $metaboxes ); ?>
</textarea>
<p>Experimental area:</p>
<textarea class="widefat" style="height: 75vh;">
<?php
/*
* Use this area to find and update post meta data
*/
$posts = get_posts( array(
'post_type' => 'any',
'post_status' => 'any',
'posts_per_page' => -1,
'meta_key' => 'attached_media_id',
// 'meta_value' => '_wp_zero_value',
// 'meta_compare' => '=',
) );
foreach ($posts as $post) {
// print_r( $post->ID );
// print_r( get_post_meta( $post->ID, 'attached_media_id' ) );
// print_r( get_post_meta( $post->ID, 'attached_media' ) );
echo 'Post : ' . $post->ID . PHP_EOL;
echo 'attached_media : ' . get_post_meta( $post->ID, 'attached_media' )[0] . PHP_EOL;
if ( !get_post_meta( $post->ID, 'attached_media' ) && $attached_media_id = get_post_meta( $post->ID, 'attached_media_id' )[0] ) {
echo 'attached_media_id : ' . $attached_media_id . PHP_EOL;
$attached_media_src = wp_get_attachment_url( $attached_media_id );
// print_r( $attached_media_src );
if ( $attached_media_src[0] ) {
echo 'Creating : ' . $attached_media_src . ' for post ' . $post->ID . PHP_EOL;
update_post_meta( $post->ID, 'attached_media', $attached_media_src );
}
}
}
?>
</textarea>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment