/** * Maybe fixes the broken migration. * * @since 2.3.9.6 * * @return null Return early if not fixing the broken migration */ public function maybe_fix_migration() { // Check if user pressed 'Fix' button and nonce is valid if (!isset($_POST['soliloquy-serialization-submit'])) { return; } if (!wp_verify_nonce($_POST['soliloquy-serialization-nonce'], 'soliloquy-serialization-nonce')) { return; } // 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("\tSELECT meta_value\n\t\t\t\t\t \t\t\t\tFROM " . $wpdb->prefix . "postmeta\n\t\t\t\t\t \t\t\t\tWHERE post_id = %d\n\t\t\t\t\t \t\t\t\tAND meta_key = %s\n\t\t\t\t\t \t\t\t\tLIMIT 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')); }
* serialized string * * @since 2.3.9.6 * * @param array $matches preg_replace matches * @return string Replacement string */ private function fix_str_length($matches) { $string = $matches[2]; $right_length = strlen($string); return 's:' . $right_length . ':"' . $string . '"'; } /** * Returns the singleton instance of the class. * * @since 2.3.9.6 * * @return object The Soliloquy_Serialization_Admin object. */ public static function get_instance() { if (!isset(self::$instance) && !self::$instance instanceof Soliloquy_Serialization_Admin) { self::$instance = new Soliloquy_Serialization_Admin(); } return self::$instance; } } // Load the serialization admin class. $soliloquy_serialization_admin = Soliloquy_Serialization_Admin::get_instance();