1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
<?php
/**
* @since 2.0.0
*/
namespace REL\Models;
use \hji\membership\Membership;
use \hji\common\utils\WPHelper;
use \REL\controllers\PocketListings;
class Upgrades
{
function __construct()
{
add_action('admin_init', array($this, 'runUpgrades'), 1);
}
/**
* Runs version dependent upgrades
*
* @since 2.0.0
*/
function runUpgrades()
{
// This needs to be used in all future upgrades
WPHelper::register_update_hook(REL_FILE, array($this, 'upgrade_2_0_0'), '2.1.2');
}
/**
* Breaking meta data from a single array into separate items.
*
* Originally all meta data was saved as an array under a single meta key.
* This prevented from using any of that data for filtering.
*
* @since 2.0.0
*/
function upgrade_2_0_0()
{
// If the code is still executing - looks like the upgrade is in order.
// Convert the meta data structure:
// Get all existing pocket listing posts
$posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'listing'));
// Loop through them and convert meta data to new structure
if (!empty($posts))
{
require_once(Membership::$dir . '/common/utils/String.php');
foreach ($posts as $post)
{
$meta = get_post_meta($post->ID, 'mcmb_listing');
if (!empty($meta[0]))
{
foreach ($meta[0] as $k => $v)
{
// Convert price to numeric value (without $ or ,) so we can sort
if ($k == 'price')
{
$v = \hji\common\utils\String::moneyToInteger($v);
}
// Convert checkbox value to ON if they were initially checked
if (($k == 'gmap' || $k == 'birdseye') && $v == true)
{
$v = 'on';
}
// Update meta
update_post_meta($post->ID, PocketListings::PREFIX . $k, $v);
// delete_post_meta($post->ID, PocketListings::PREFIX . $k);
}
}
// Convert images into a cmb gallery
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') );
if (!empty($attachments))
{
update_post_meta($post->ID, PocketListings::PREFIX . 'photos', array_keys($attachments));
}
}
}
}
}