Last active
September 21, 2020 17:25
-
-
Save wpmudev-sls/7c083c80069090c0ab22633ed4101dab to your computer and use it in GitHub Desktop.
[Hustle] - Import entries from CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hustle] - Import entries from CSV | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Imports entries from a csv file | |
* Task : SLS-771 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
// Hustle import entries | |
/** | |
* INSTRUCTIONS | |
* 1. Replace `$module_id = 2;` with the id of the module you want to import entries to | |
* 2. Replace `$module_type = 'popup';`with the module type. Accepted values can be popup, slidein, embedded, social_sharing | |
* 3. Upload your CSV file and insert the URL in this line: | |
* $csv_path = '/PATH/TO/FILE/hustle-import.csv'; | |
* 4. Make sure you map each column correctly. This can be done in the following lines: | |
* `$csv_map = array( | |
* 'email', | |
* 'first_name', | |
* 'last_name', | |
* );` | |
* With above mapping, the first column contains the email, the second the first name and the third contains the last name | |
*/ | |
add_action( | |
'admin_footer', | |
function() { | |
if ( ! isset( $_GET[ 'import_entries' ] ) ) { | |
return; | |
} | |
$module_id = 2; | |
$module_type = 'popup'; // popup, slidein, embedded, social_sharing | |
$csv_path = '/PATH/TO/FILE/hustle-import.csv'; | |
$csv_map = array( | |
'email', | |
'first_name', | |
'last_name', | |
); | |
$client = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : null; | |
$forward = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : null; | |
$remote = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null; | |
$client_real = isset( $_SERVER['HTTP_X_REAL_IP'] ) ? $_SERVER['HTTP_X_REAL_IP'] : null; | |
$hustle_ip = $remote; | |
if ( filter_var( $client, FILTER_VALIDATE_IP ) ) { | |
$hustle_ip = $client; | |
} elseif ( filter_var( $client_real, FILTER_VALIDATE_IP ) ) { | |
$hustle_ip = $client_real; | |
} elseif ( ! empty( $forward ) ) { | |
$forward = explode( ',', $forward ); | |
$ip = array_shift( $forward ); | |
$ip = trim( $ip ); | |
if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) { | |
$hustle_ip = $ip; | |
} | |
} | |
$file = fopen( $csv_path, 'r' ); | |
while ( ( $line = fgetcsv( $file ) ) !== FALSE ) { | |
$entry = new Hustle_Entry_Model(); | |
$entry->entry_type = $module_type; | |
$entry->module_id = $module_id; | |
$entry_items = array(); | |
$entry->save(); | |
// Map CSV keys | |
foreach ( $csv_map as $map_key => $map_value ) { | |
$entry_items[] = array( | |
'name' => $map_value, | |
'value' => $line[ $map_key ], | |
); | |
} | |
$entry_items[] = array( | |
'name' => 'hustle_ip', | |
'value' => $hustle_ip, | |
); | |
$entry->set_fields( $entry_items ); | |
$entry_items = array(); | |
} | |
fclose( $file ); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment