Esempio n. 1
0
 private function import_row($row)
 {
     $post = array('post_type' => $this->post_type, 'post_status' => 'publish');
     $post_meta = array();
     $tax_input = array();
     $tax_meta = array();
     foreach ($this->fields as $col => $field) {
         if (isset($row[$col])) {
             $post[$field] = $row[$col];
         }
     }
     foreach ($this->custom_fields as $col => $data) {
         if (isset($row[$col])) {
             $val = $row[$col];
         } elseif ('' !== $data['default']) {
             $val = $data['default'];
         } else {
             continue;
         }
         $post_meta[$data['internal_key']] = $val;
     }
     foreach ($this->taxonomies as $col) {
         if (isset($row[$col])) {
             $tax_input[$col] = array_filter(array_map('trim', explode(',', $row[$col])));
         }
     }
     foreach ($this->tax_meta as $tax => $fields) {
         foreach ($fields as $col => $key) {
             if (isset($row[$col])) {
                 $term = $tax_input[$tax][0];
                 $tax_meta[$tax][$term][$key] = $row[$col];
             }
         }
     }
     foreach ($tax_meta as $tax => $terms) {
         foreach ($terms as $term => $meta_data) {
             if (empty($term)) {
                 continue;
             }
             $t = $this->maybe_create_term($term, $tax);
             if (is_wp_error($t)) {
                 continue;
             }
             foreach ($meta_data as $meta_key => $meta_value) {
                 if ('desc' == substr($meta_key, -4)) {
                     wp_update_term($t['term_id'], $tax, array('description' => sanitize_text_field($meta_value)));
                 } else {
                     if (function_exists('update_metadata')) {
                         update_metadata($tax, $t['term_id'], $meta_key, $meta_value);
                     }
                 }
             }
         }
     }
     foreach ($tax_input as $tax => $terms) {
         $_terms = array();
         foreach ($terms as $term) {
             if (empty($term)) {
                 continue;
             }
             $t = $this->maybe_create_term($term, $tax);
             if (!is_wp_error($t)) {
                 $_terms[] = (int) $t['term_id'];
             }
         }
         $post['tax_input'][$tax] = $_terms;
     }
     if (!empty($post['post_author'])) {
         $user = get_user_by('login', $post['post_author']);
         if ($user) {
             $post['post_author'] = $user->ID;
         }
     }
     if (!empty($post['post_date'])) {
         $post['post_date'] = date('Y-m-d H:i:s', strtotime($post['post_date']));
     }
     $post_id = wp_insert_post($post, true);
     if (is_wp_error($post_id)) {
         return false;
     }
     foreach ($post_meta as $meta_key => $meta_value) {
         add_post_meta($post_id, $meta_key, $meta_value, true);
     }
     if ($this->geodata) {
         appthemes_set_coordinates($post_id, $row['lat'], $row['lng']);
     }
     return true;
 }
Esempio n. 2
0
 private function import_row($row)
 {
     $post = array('post_type' => $this->post_type, 'post_status' => 'publish');
     $post_meta = array();
     $tax_input = array();
     $tax_meta = array();
     foreach ($this->fields as $col => $field) {
         if (isset($row[$col])) {
             $post[$field] = $row[$col];
         }
     }
     foreach ($this->custom_fields as $col => $data) {
         if (isset($row[$col])) {
             $val = $row[$col];
         } elseif ('' !== $data['default']) {
             $val = $data['default'];
         } else {
             continue;
         }
         $post_meta[$data['internal_key']] = $val;
     }
     foreach ($this->taxonomies as $col) {
         if (isset($row[$col])) {
             $tax_input[$col] = array_filter(array_map('trim', explode(',', $row[$col])));
         }
     }
     foreach ($this->tax_meta as $tax => $fields) {
         foreach ($fields as $col => $key) {
             if (isset($row[$col])) {
                 $term = $tax_input[$tax][0];
                 $tax_meta[$tax][$term][$key] = $row[$col];
             }
         }
     }
     $data = compact(array('post', 'post_meta', 'tax_input', 'tax_meta'));
     $data = apply_filters('appthemes_importer_import_row_data', $data);
     if (!$data) {
         return false;
     }
     // export array as variables
     extract($data, EXTR_OVERWRITE);
     foreach ($tax_meta as $tax => $terms) {
         foreach ($terms as $term => $meta_data) {
             if (empty($term)) {
                 continue;
             }
             $t = appthemes_maybe_insert_term($term, $tax);
             if (is_wp_error($t)) {
                 continue;
             }
             foreach ($meta_data as $meta_key => $meta_value) {
                 if ('desc' == substr($meta_key, -4)) {
                     wp_update_term($t['term_id'], $tax, array('description' => sanitize_text_field($meta_value)));
                 } else {
                     update_metadata($tax, $t['term_id'], $meta_key, $meta_value);
                 }
             }
         }
     }
     foreach ($tax_input as $tax => $terms) {
         $_terms = array();
         foreach ($terms as $term) {
             if (empty($term)) {
                 continue;
             }
             $t = appthemes_maybe_insert_term($term, $tax);
             if (!is_wp_error($t)) {
                 $_terms[] = (int) $t['term_id'];
             }
         }
         $post['tax_input'][$tax] = $_terms;
     }
     if (!empty($post['post_author'])) {
         $user = get_user_by('login', $post['post_author']);
         if ($user) {
             $post['post_author'] = $user->ID;
         }
     }
     if (!empty($post['post_date'])) {
         $post['post_date'] = date('Y-m-d H:i:s', strtotime($post['post_date']));
     }
     if (!in_array($post['post_status'], get_post_stati())) {
         $post['post_status'] = 'publish';
     }
     $post = apply_filters('appthemes_importer_import_row_post', $post);
     $post_id = wp_insert_post($post, true);
     if (is_wp_error($post_id)) {
         return false;
     }
     $post_meta = apply_filters('appthemes_importer_import_row_post_meta', $post_meta);
     foreach ($post_meta as $meta_key => $meta_value) {
         add_post_meta($post_id, $meta_key, $meta_value, true);
     }
     if ($this->geodata) {
         appthemes_set_coordinates($post_id, $row['lat'], $row['lng']);
     }
     if ($this->attachments && isset($row['attachments'])) {
         $attachments = explode(',', $row['attachments']);
         if (!empty($attachments)) {
             $imported_attachments = array();
             foreach ($attachments as $attachment_url) {
                 $attachment_url = trim($attachment_url);
                 $attachment_id = $this->process_attachment($attachment_url, $post_id);
                 if (!is_wp_error($attachment_id)) {
                     $imported_attachments[$attachment_url] = $attachment_id;
                 }
             }
             if (isset($row['featured_attachment'])) {
                 if (!empty($imported_attachments[$row['featured_attachment']])) {
                     update_post_meta($post_id, '_thumbnail_id', $imported_attachments[$row['featured_attachment']]);
                 }
             }
         }
     }
     do_action('appthemes_importer_import_row_after', $post_id, $row);
     return true;
 }