WP-tools/import-csv2cpt/import-csv2cpt.php

229 lines
7.3 KiB
PHP
Raw Normal View History

<?php
/*
Plugin Name: Import CSV2CPT
Description: This a quick and dirty way to import data into CPTs.
It requires setting up the an options page with ACF and recuperating the values from the inputs
see : https://gist.github.com/stephenharris/00a6a601b274b38a59cc82c146a95688
Add options page with ACF with file field and button.
Version: 0.1
License: GPL
Author: Richard Turner
Author URI: yoururl
Requires : ACF Pro, ACF Extended
*/
define('FILE_FIELD','field_' . uniqid());
function import_csv2cpt_enqueue_styles(){
$template_url = get_template_directory_uri();
$plugin_url = plugin_dir_url( __FILE__ );
wp_register_style( 'import_csv2cpt-style-css', $plugin_url.'css/style.css', false, NULL, 'all');
wp_enqueue_style( 'import_csv2cpt-style-css' );
wp_register_script('import_csv2cpt-custom-script', $plugin_url . 'js/custom-script.js', array('jquery'), null, true);
wp_enqueue_script('import_csv2cpt-custom-script');
}
add_action( 'wp_enqueue_scripts', 'import_csv2cpt_enqueue_styles' );
// add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
// function wpdocs_register_my_custom_submenu_page() {
// add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug', 'wpdocs_my_custom_submenu_page_callback');
// add_submenu_page(
// 'my-top-level-slug',
// 'My Custom Submenu Page',
// 'My Custom Submenu Page',
// 'manage_options',
// 'my-custom-submenu-page',
// 'wpdocs_my_custom_submenu_page_callback' );
// }
// function wpdocs_my_custom_submenu_page_callback() {
// echo '<div class="wrap"><div id="icon-tools" class="icon32"></div>';
// echo '<h2>My Custom Submenu Page</h2>';
// echo '</div>';
// }
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'&eacute;glise de Lessines !
Les frissons &eacute;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' );
}
?>