Last active
February 18, 2020 16:02
-
-
Save mustardBees/fc4ffb920064d34f4f17 to your computer and use it in GitHub Desktop.
Convert pw_gallery to file_list
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: Convert pw_gallery to file_list | |
Plugin URI: https://github.com/mustardBees/cmb-field-gallery | |
Description: Rough, inefficient snippet for converting existing data from pw_gallery to file_list format. Hit example.com/?convert_pw_gallery_to_file_list. Use at your own risk. Backup first! | |
Version: 1.0.0 | |
Author: Phil Wylie | |
Author URI: https://www.philwylie.co.uk/ | |
License: GPL2 | |
*/ | |
function pw_convert_pw_gallery_to_file_list() { | |
if ( ! isset( $_GET['convert_pw_gallery_to_file_list'] ) ) { | |
return; | |
} | |
$boxes = CMB2_Boxes::get_all(); | |
$gallery_fields = array(); | |
foreach ( $boxes as $box ) { | |
foreach ( $box->meta_box['fields'] as $key => $field ) { | |
if ( 'pw_gallery' === $field['type'] ) { | |
$gallery_fields[] = $key; | |
} | |
} | |
} | |
$gallery_fields = array_unique( $gallery_fields ); | |
foreach ( $gallery_fields as $gallery_field ) { | |
global $wpdb; | |
$meta_ids = $wpdb->get_col( $wpdb->prepare( | |
" | |
SELECT meta_id FROM $wpdb->postmeta | |
WHERE meta_key = '%s' | |
", | |
$gallery_field | |
) ); | |
foreach ( $meta_ids as $meta_id ) { | |
$old = get_metadata_by_mid( 'post', $meta_id ); | |
$values = $old->meta_value; | |
reset($values); | |
$first_key = key($values); | |
if ( 0 === $first_key ) { | |
$new = array(); | |
foreach( $values as $value ) { | |
$new[$value] = wp_get_attachment_url( $value ); | |
} | |
update_metadata_by_mid( 'post', $meta_id, $new ); | |
} | |
} | |
} | |
} | |
add_action( 'cmb2_after_init', 'pw_convert_pw_gallery_to_file_list' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment