Example #1
0
 public function import_grids($import_grids, $import_ids = true, $check_append = true)
 {
     if ($import_grids !== false && !empty($import_grids)) {
         global $wpdb;
         $c_grid = new Essential_Grid();
         $base = new Essential_Grid_Base();
         $table_name = $wpdb->prefix . Essential_Grid::TABLE_GRID;
         $item_skin = new Essential_Grid_Item_Skin();
         $grids = $c_grid->get_essential_grids();
         foreach ($import_grids as $i_grid) {
             if (!empty($import_ids) && is_array($import_ids)) {
                 $found = false;
                 foreach ($import_ids as $id) {
                     if ($i_grid['id'] == $id) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     continue;
                 }
             } else {
                 if ($import_ids != true) {
                     //only break if we do not want to import all
                     break;
                 }
             }
             //create/get category and tags if there are some selected
             $check = json_decode($i_grid['postparams'], true);
             if (isset($check['post_category']) && !empty($check['post_category'])) {
                 $slug_cats = array();
                 $the_cats = explode(',', $check['post_category']);
                 foreach ($the_cats as $cat) {
                     $raw = explode('_', $cat);
                     $catSlug = $raw[count($raw) - 1];
                     unset($raw[count($raw) - 1]);
                     $cat = implode('_', $raw);
                     $category = $base->get_create_category_by_slug($catSlug, $cat);
                     if ($category !== false) {
                         //only add if we have a ID
                         $slug_cats[] = $cat . '_' . $category;
                     }
                 }
                 $check['post_category'] = implode(',', $slug_cats);
                 $i_grid['postparams'] = json_encode($check);
             }
             $check = json_decode($i_grid['params'], true);
             if (isset($check['entry-skin']) && !empty($check['entry-skin'])) {
                 $skin = $item_skin->get_id_by_handle($check['entry-skin']);
                 if (!empty($skin)) {
                     $check['entry-skin'] = $skin['id'];
                 }
                 $i_grid['params'] = json_encode($check);
             }
             $exist = false;
             if (!empty($grids)) {
                 foreach ($grids as $grid) {
                     if ($grid->handle == $i_grid['handle']) {
                         $i_grid['id'] = $grid->id;
                         //this will force an update
                         $exist = true;
                         break;
                     }
                 }
             }
             if ($import_ids === true) {
                 //do not insert if handle exists. This is for the import demo data process
                 if ($exist) {
                     continue;
                 }
             }
             $append = true;
             if ($exist) {
                 //skin exists - append or overwrite
                 if ($check_append) {
                     //check in data if append or overwrite
                     $do = $base->getVar($this->overwrite_data, 'element-overwrite-' . $i_grid['id'], 'append');
                     $append = $do == 'append' ? true : false;
                 }
             }
             if ($import_ids !== true) {
                 if ($append) {
                     //append
                     if ($exist) {
                         $i_grid['handle'] = $i_grid['handle'] . '-' . date('His');
                         $i_grid['name'] = $i_grid['name'] . '-' . date('His');
                     }
                     $response = $wpdb->insert($table_name, array('name' => $i_grid['name'], 'handle' => $i_grid['handle'], 'postparams' => $i_grid['postparams'], 'params' => $i_grid['params'], 'layers' => $i_grid['layers']));
                 } else {
                     //overwrite
                     $response = $wpdb->update($table_name, array('name' => $i_grid['name'], 'handle' => $i_grid['handle'], 'postparams' => $i_grid['postparams'], 'params' => $i_grid['params'], 'layers' => $i_grid['layers']), array('id' => $i_grid['id']));
                 }
             } else {
                 //create or overwrite
                 if ($exist) {
                     $response = $wpdb->update($table_name, array('name' => $i_grid['name'], 'handle' => $i_grid['handle'], 'postparams' => $i_grid['postparams'], 'params' => $i_grid['params'], 'layers' => $i_grid['layers']), array('id' => $i_grid['id']));
                 } else {
                     $response = $wpdb->insert($table_name, array('name' => $i_grid['name'], 'handle' => $i_grid['handle'], 'postparams' => $i_grid['postparams'], 'params' => $i_grid['params'], 'layers' => $i_grid['layers']));
                 }
             }
         }
     }
 }