';
// echo 'My Custom Submenu Page
';
// echo '';
// }
add_action('acf/init', 'my_acf_op_init');
function my_acf_op_init() {
// Check function exists.
if( function_exists('acf_add_options_page') ) {
// Register options page.
$option_page = acf_add_options_page(array(
'page_title' => __('Import CSV2CPT'),
'menu_title' => __('Import CSV2CPT'),
'menu_slug' => 'import-csv2cpt',
'capability' => 'edit_posts',
'icon_url' => 'dashicons-media-code',
'redirect' => false
));
}
}
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array (
'key' => 'group_import_csv2cpt',
'title' => 'Import CSV to CPT',
'fields' => array (
array (
'key' => 'field_file_import_csv2cpt',
'label' => 'CSV File',
'name' => 'csv_file',
'type' => 'file',
'prefix' => '',
'instructions' => 'Use only comma separated files',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
'readonly' => 0,
'disabled' => 0,
),
array (
'key' => 'field_button_import_csv2cpt',
'label' => 'Importer csv data',
'name' => 'importer_csv_data',
'type' => 'acfe_button',
'prefix' => '',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'readonly' => 0,
'disabled' => 0,
'button_value' => 'Envoyer',
'button_type' => 'button',
'button_class' => 'button button-secondary',
'button_id' => '',
'button_before' => '',
'button_after' => '',
'button_ajax' => 1,
'_name' => 'importer_csv_data',
'_valid' => 1
)
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'import-csv2cpt',
),
),
),
'menu_order' => 10,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
));
endif;
add_action('acfe/fields/button/key=field_button_import_csv2cpt', 'import_csv2cpt_button_ajax', 10, 2);
function import_csv2cpt_button_ajax($field, $post_id){
/*
[0] => 22
[1] => 3
[2] => 570
[3] => Anne
[4] => Belgique
[5] => Bravo pour votre prestation dans l'église de Lessines !
Les frissons étaient au rendez-vous !
[6] => 10/09/2012
[7] => 1
*/
$file_ID = $_POST['acf']['field_file_import_csv2cpt'];
$file = get_attached_file($file_ID);
if($file == ""){
wp_send_json_success(__LINE__." Error File is Empty file_ID:".$file_ID);
return;
}
// $csvFile = file($file);
$csvFile = fopen($file, 'r');
$i = 0;
$count_adjusted = 0;
$i= 0;
while (($row = fgetcsv($csvFile)) !== false) {
if($i==0){
$i++;
// continue;
}
// $tmp = print_r($row,1);
// wp_send_json_success(__LINE__." End of Function:".$tmp."\n");
// return;
if(empty($row)){
wp_send_json_success(__LINE__." Error! File is empty");
return;
}
$csv_data['post_title'] = ucwords($row[3])." - ".ucwords($row[4]);
$csv_data['creation_id'] = $row[2];
$csv_data['nom'] = ucwords($row[3]);
$csv_data['pays'] = ucwords($row[4]);
$csv_data['citation'] = $row[5];
$csv_data['date'] = $row[6];
$csv_data['valide'] = $row[7];
$csv_data['post_title'] .= " - ".date( 'd.m.Y', strtotime($csv_data['date']));
// Insert the post into the database
$my_post = array(
'post_title' => wp_strip_all_tags( $csv_data['post_title'] ),
'post_content' => $csv_data['citation'],
'post_type' => 'item-livre-dor',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_date' => date( 'Y-m-d H:i:s', strtotime($csv_data['date']) ),
'post_date_gmt' => date( 'Y-m-d H:i:s', strtotime($csv_data['date']) ),
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
$tmp .= __LINE__." Post ID:".$post_id;
update_field( 'creation', $csv_data['creation_id'], $post_id );
update_field( 'citation', $csv_data['citation'], $post_id );
update_field( 'nom', $csv_data['nom'], $post_id );
update_field( 'pays', $csv_data['pays'], $post_id );
update_field( 'date', $csv_data['date'], $post_id );
update_field( 'valide', $csv_data['valide'], $post_id );
}
wp_send_json_success(__LINE__." End of Function:".$tmp);
return;
}
add_action('acf/input/admin_enqueue_scripts', 'import_cpt_admin_enqueue_scripts');
function import_cpt_admin_enqueue_scripts() {
$plugin_url = plugin_dir_url( __FILE__ );
wp_enqueue_script( 'import-csv2cpt-acf-input-js', $plugin_url . 'js/import-csv2cpt-acf-input.js', false, '1.0.0' );
}
?>