Пример #1
0
 /**
  * Import or update speakers from CSV.
  */
 public static function import_speakers($file, $output_callback)
 {
     $import_options = get_option('importer_settings');
     $speaker_tab = $import_options['speaker_doc_tab'];
     // If this is a table, we need to turn it into a CSV
     if (!($data = self::check_if_table($file, $output_callback, $speaker_tab))) {
         // Otherwise, it's already a CSV
         $data = self::get_file_data($file, $output_callback);
     }
     $csv_rows = self::parse_csv_from_string($data, $output_callback);
     foreach ($csv_rows as $i => $csv_speaker) {
         // Uh oh, someone messed up the rows
         if (empty($csv_speaker['Speaker First Name'])) {
             continue;
         }
         // If the speaker doesn't exist, let's create it first.
         $speaker_name = sanitize_text_field($csv_speaker['Speaker First Name']) . ' ' . sanitize_text_field($csv_speaker['Speaker Last Name']);
         if (false === ($speaker = ONA_Speaker::get_by_name($speaker_name))) {
             $post_id = wp_insert_post(array('post_type' => ONA_Speaker::$post_type, 'post_title' => $speaker_name, 'post_status' => 'publish'));
             $speaker = new ONA_Speaker($post_id);
             $output_callback(sprintf("Inserting speaker '%s' as post #%d", $speaker_name, $speaker->get_id()));
         } else {
             $output_callback(sprintf("Updating speaker '%s' (post #%d)", $speaker_name, $speaker->get_id()));
         }
         // No full name spreadsheet column
         $csv_speaker['Speaker Name'] = $speaker_name;
         foreach ($csv_speaker as $key => $value) {
             foreach (self::$speaker_fields as $speaker_field) {
                 if ($speaker_field['csv_field'] != $key) {
                     continue;
                 }
                 if (isset($speaker_field['pre_sanitize_callback']) && is_callable($speaker_field['pre_sanitize_callback'])) {
                     $value = call_user_func_array($speaker_field['pre_sanitize_callback'], array($value, $csv_speaker));
                 }
                 if (is_callable($speaker_field['sanitize_callback'])) {
                     $new_value = call_user_func_array($speaker_field['sanitize_callback'], array($value));
                 } else {
                     $new_value = $value;
                 }
                 $get_method = 'get_' . $speaker_field['object_field'];
                 $set_method = 'set_' . $speaker_field['object_field'];
                 // See whether the values should be updated
                 if (isset($speaker_field['comparison_callback']) && is_callable($speaker_field['comparison_callback'])) {
                     $update = call_user_func_array($speaker_field['comparison_callback'], array($new_value, $speaker->{$get_method}()));
                 } else {
                     $update = $new_value != $speaker->{$get_method}();
                 }
                 if ($update) {
                     $speaker->{$set_method}($new_value);
                     self::output_diff($key, $new_value, $output_callback);
                 } else {
                     self::output_diff($key, false, $output_callback);
                 }
             }
         }
         $output_callback();
         // Line break
         $output_callback();
         // Line break
     }
     if (!empty($tmp_file)) {
         @unlink($tmp_file);
     }
     $output_callback("Import complete", 'Success');
 }