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
<?php
namespace hji\geofarms\controllers;
use hji\geofarms\GeoFarms;
use hji\geofarms\models\CMB as CMBModel;
use hji\geofarms\models\Settings as SettingsModel;
class UpdateNeighborhoods
{
private $cmb_model;
private $settings_model;
function __construct()
{
$this->cmb_model = new CMBModel();
$this->settings_model = new SettingsModel();
$neighborhoods = $this->getNeighborhoodsPosts();
$old_meta = $this->getNeighborhoodsPostMeta($neighborhoods);
$this->convertGeoFarmsPosts($neighborhoods, $old_meta);
$this->updateGeoFarmsSlugOption();
}
function getNeighborhoodsPosts()
{
return get_posts(array(
'post_type' => 'hji_neighborhood',
'posts_per_page' => -1
));
}
function getNeighborhoodsPostMeta($neighborhoods)
{
$meta = array();
foreach($neighborhoods as $hood)
{
$id = $hood->ID;
$meta[$id] = get_post_meta($id);
}
return $meta;
}
function convertGeoFarmsPosts($neighborhoods, $old_meta)
{
$all_menu_items = array();
$menus = wp_get_nav_menus();
foreach($menus as $menu) {
$all_menu_items[] = wp_get_nav_menu_items($menu->term_id);
}
foreach($neighborhoods as $hood)
{
set_post_type($hood->ID, GeoFarms::$post_type);
for($i = 1; $i <= 4; $i++)
{
update_post_meta($hood->ID, $this->cmb_model->cmb_prefix . $this->cmb_model->cmb_tab_name($i), $old_meta[$hood->ID]["tab_{$i}"][0]);
if($i != 1) {
update_post_meta($hood->ID, $this->cmb_model->cmb_prefix . $this->cmb_model->cmb_tab_shortcode($i), $old_meta[$hood->ID]["tab_{$i}_content"][0]);
}
}
update_post_meta($hood->ID, $this->cmb_model->cmb_prefix . $this->cmb_model->cmb_featured_listings_name(), $old_meta[$hood->ID]['featured_listings_sc'][0]);
foreach($all_menu_items as $menu)
{
foreach($menu as $menu_items)
{
if ( $menu_items->object_id == $hood->ID ) {
update_post_meta( $menu_items->ID, '_menu_item_object', GeoFarms::$post_type );
}
}
}
}
deactivate_plugins('hji-neighborhoods/hji-neighborhoods.php');
}
function updateGeoFarmsSlugOption()
{
$options = $this->settings_model->getOptions();
$options['general']['hji_geo_farms_page_slug'] = 'neighborhood';
update_option(GeoFarms::$settings_key, $options);
wp_redirect(admin_url('edit.php?post_type=' . GeoFarms::$post_type . '&page=' . GeoFarms::$slug));
exit();
}
}