Last active
April 28, 2023 08:32
-
-
Save mattiasghodsian/8f2477a7cc33ab562362bdd0050864b2 to your computer and use it in GitHub Desktop.
[Wordpress] Custom importer class
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 | |
/** | |
* FM_Importer | |
* Author: Mattias Ghodsian | |
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian | |
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { exit; } | |
if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) | |
return; | |
// Load Importer API | |
require_once ABSPATH . 'wp-admin/includes/import.php'; | |
if ( ! class_exists( 'WP_Importer' ) ) { | |
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; | |
if ( file_exists( $class_wp_importer ) ) | |
require $class_wp_importer; | |
} | |
if ( !class_exists( 'FM_Importer' ) ) { | |
class FM_Importer extends WP_Importer { | |
var $id; | |
var $import_data; | |
var $users; | |
/** | |
* construct do nothing | |
* | |
* @since 1.0.0 | |
* @return html | |
*/ | |
function __construct() { | |
} | |
/** | |
* Core of the importer | |
* | |
* @since 1.0.0 | |
* @return html | |
*/ | |
function dispatch() { | |
$this->header(); | |
$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; | |
switch ( $step ) { | |
case 0: | |
$this->greet(); | |
break; | |
case 1: | |
check_admin_referer( 'import-upload' ); | |
if ( $this->handle_upload() ){ | |
$this->get_users(); | |
$this->import_options(); | |
} | |
break; | |
case 2: | |
check_admin_referer( 'import-fm-ga' ); | |
$this->id = (int) $_POST['import_id']; | |
$file = get_attached_file( $this->id ); | |
set_time_limit(0); | |
$this->import( $file ); | |
break; | |
} | |
$this->footer(); | |
} | |
/** | |
* Display import page title | |
* | |
* @since 1.0.0 | |
* @return html | |
*/ | |
function header() { | |
echo '<div class="wrap">'; | |
screen_icon(); | |
echo '<h2>' . __( 'Import Google Analytics', 'forfragan' ) . '</h2>'; | |
} | |
/** | |
* Close div.wrap | |
* | |
* @since 1.0.0 | |
* @return html | |
*/ | |
function footer() { | |
echo '</div>'; | |
} | |
/** | |
* Display introductory text and file upload form | |
* | |
* @since 1.0.0 | |
* @return html | |
*/ | |
function greet() { | |
echo '<div class="narrow">'; | |
echo '<p>'.__( 'Howdy! Upload your Google Analytics (.csv) file and we’ll import the data into this site.', 'forfragan' ).'</p>'; | |
wp_import_upload_form( 'admin.php?import=fm_ga&step=1' ); | |
echo '</div>'; | |
} | |
/** | |
* Handles the CSV upload and initial parsing of the file to prepare for | |
* displaying author import options | |
* | |
* @return bool | |
*/ | |
function handle_upload() { | |
$file = wp_import_handle_upload(); | |
if ( isset( $file['error'] ) ) { | |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'forfragan' ) . '</strong><br />'; | |
echo esc_html( $file['error'] ) . '</p>'; | |
return false; | |
} else if ( ! file_exists( $file['file'] ) ) { | |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'forfragan' ) . '</strong><br />'; | |
printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'forfragan' ), esc_html( $file['file'] ) ); | |
echo '</p>'; | |
return false; | |
} | |
$this->id = (int) $file['id']; | |
$import_data = $this->parse( $file['file'] ); | |
$fileParts = pathinfo($file['file']); | |
if ( is_wp_error( $import_data ) ) { | |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'forfragan' ) . '</strong><br />'; | |
echo esc_html( $import_data->get_error_message() ) . '</p>'; | |
return false; | |
} | |
if ( $fileParts['extension'] !== "txt" ) { | |
echo '<p><strong>' . __( 'Sorry, this is not a CSV file.', 'forfragan' ) . '</strong><br />'; | |
return false; | |
} | |
return true; | |
} | |
/** | |
* Get users with venue slugs | |
* | |
* @since 1.0.0 | |
* @return array | |
*/ | |
function get_users(){ | |
$query = new \WP_User_Query(); | |
$meta_query = array( | |
'key' => 'venue_slugs', // field_5f8d9ff578d40 | |
'value' => false, | |
'compare' => '!=' | |
); | |
$query->prepare_query([ | |
'meta_key' => 'venue_slugs', // field_5f8d9ff578d40 | |
'meta_query' => $meta_query | |
]); | |
$query->query(); | |
$users = $query->get_results(); | |
$u = array_map(function($user) { | |
$data = get_field('field_5f8d9ff578d40','user_'.$user->ID); | |
$slugs = []; | |
foreach ($data[0] as $key => $row) { | |
$slugs[] = $row; | |
} | |
return ['ID' => $user->ID, 'email' => $user->user_email, 'slugs' => $slugs ]; | |
}, $users); | |
$this->users = array_filter($u); | |
} | |
/** | |
* Parses the CSV file and prepares us for the task of processing parsed data | |
* | |
* @since 1.0.0 | |
* @param string $file Path to the CSV file for importing | |
* @return null | |
*/ | |
function import_start( $file ) { | |
if ( ! is_file($file) ) { | |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'forfragan' ) . '</strong><br />'; | |
echo __( 'The file does not exist, please try again.', 'forfragan' ) . '</p>'; | |
$this->footer(); | |
die(); | |
} | |
$import_data = $this->parse( $file ); | |
if ( is_wp_error( $import_data ) ) { | |
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'forfragan' ) . '</strong><br />'; | |
echo esc_html( $import_data->get_error_message() ) . '</p>'; | |
$this->footer(); | |
die(); | |
} | |
unset($import_data[0],$import_data[1],$import_data[2],$import_data[3],$import_data[4],$import_data[5],$import_data[6]); | |
$this->import_data = $import_data; | |
do_action( 'import_start' ); | |
} | |
/** | |
* Parse a CSV file | |
* | |
* @since 1.0.0 | |
* @param string $file Path to CSV file for parsing | |
* @return array Information gathered from the CSV file | |
*/ | |
function parse( $file ) { | |
$parser = array_map('str_getcsv', file($file)); | |
return $parser; | |
} | |
/** | |
* The main controller for the actual import stage. | |
* | |
* @since 1.0.0 | |
* @param string $file Path to the CSV file for importing | |
* @return null | |
*/ | |
function import( $file ) { | |
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); | |
$this->import_start( $file ); | |
$this->process_posts(); | |
$this->import_end(); | |
} | |
/** | |
* Performs post-import cleanup of files and the cache | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
function import_end() { | |
wp_import_cleanup( $this->id ); | |
wp_cache_flush(); | |
echo '<p>' . __( 'Import done.', 'forfragan' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>'; | |
do_action( 'import_end' ); | |
} | |
/** | |
* Create new posts based on import information | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
function process_posts() { | |
$success = 0; | |
$errors = 0; | |
$data = $this->import_data; | |
foreach ($data as $key => $row) { | |
$post = wp_insert_post([ | |
'post_type' => 'analytics', | |
'post_status' => 'publish', | |
'post_author' => 1 , | |
'post_title' => $row[0] | |
]); | |
if ($post) { | |
update_field('page_views',$row[1], $post); | |
update_field('unique_page_views',$row[2], $post); | |
update_field('time_on_page',$row[3], $post); | |
update_field('entrances',$row[4], $post); | |
update_field('bounce_rate',$row[5], $post); | |
update_field('exit',$row[6], $post); | |
update_field('page_value',str_replace('SEK ', '', $row[7]), $post); | |
$success++; | |
}else{ | |
$errors++; | |
} | |
} | |
dump( $data ); | |
?> | |
<p><?= __('Imported', 'forfragan');?> <?= $success; ?> <?= __('and', 'forfragan'); ?> <?= $errors; ?> <?= __('rejected', 'forfragan'); ?></p> | |
<?php | |
} | |
/** | |
* Display pre-import information | |
* | |
* @since 1.0.0 | |
* @return null | |
*/ | |
function import_options() { | |
?> | |
<form action="<?php echo admin_url( 'admin.php?import=fm_ga&step=2' ); ?>" method="post"> | |
<?php wp_nonce_field( 'import-fm-ga' ); ?> | |
<input type="hidden" name="import_id" value="<?php echo $this->id; ?>" /> | |
<p><?= __('Found:', 'forfragan'); ?> <?= count($this->users); ?> <?= __('user(s)', 'forfragan'); ?></p> | |
<p><?= __( 'The imported content will be assign to the user(s) by there venue slug', 'forfragan' ); ?></p> | |
<p><?= __( 'Are you sure you want to import the data ?', 'forfragan' ); ?></p> | |
<p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Yes, Import!', 'forfragan' ); ?>" /></p> | |
</form> | |
<?php | |
} | |
} | |
} | |
function fm_ga_importer_init() { | |
$GLOBALS['wp_import'] = new FM_Importer(); | |
register_importer('fm_ga', | |
__('Google Analytics', 'forfragan'), | |
__('Import google analytics data (.csv)', 'forfragan'), | |
array( $GLOBALS['wp_import'], 'dispatch' ) | |
); | |
} | |
add_action( 'admin_init', 'fm_ga_importer_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment