bhubbard
3/16/2016 - 4:53 PM

soliloquy-fix-slider.php

<?php
// If here, fix potentially broken migration
		// Get WPDB and serialization class
		global $wpdb, $fixedSliders;
		require plugin_dir_path( __FILE__ ) . 'serialization.php';
		$instance = Soliloquy_Serialization_Admin::get_instance();
		 
		// Keep count of the number of sliders that get fixed
		$fixedSliders = 0;
		
		// Query to get all Soliloquy CPTs
		$sliders = new WP_Query( array (
			'post_type' => 'soliloquy',
			'post_status' => 'any',
			'posts_per_page' => -1,	
		) );
		
		// Iterate through sliders
		if ( $sliders->posts ) {
			foreach ( $sliders->posts as $slider ) {
				
				// Attempt to get slider data
				$slider_data = get_post_meta( $slider->ID, '_sol_slider_data', true );
				if ( is_array( $slider_data ) ) {
					// Nothing to fix here, continue
					continue;
				}
				
				// Need to fix the broken serialized string for this slider
				// Get raw string from DB
				$query = $wpdb->prepare( "	SELECT meta_value
					        				FROM ".$wpdb->prefix."postmeta
					        				WHERE post_id = %d
					        				AND meta_key = %s
					        				LIMIT 1",
					        				$slider->ID,
					        				'_sol_slider_data' );
				$raw_slider_data = $wpdb->get_row( $query );
				
				// Do the fix, which returns an unserialized array
				$slider_data = $instance->fix_serialized_string( $raw_slider_data->meta_value );
				
				// Check we now have an array of unserialized data
				if ( is_array ( $slider_data ) ) {
					update_post_meta( $slider->ID, '_sol_slider_data', $slider_data );
					$fixedSliders++;
				}
			}
		}
	    
	    // Output an admin notice so the user knows what happened
	    add_action( 'admin_notices', array( $this, 'fixed_migration' ) );