Move admin functions
This commit is contained in:
102
includes/admin/class-sp-admin-importers.php
Normal file
102
includes/admin/class-sp-admin-importers.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Setup importers for SP data.
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin
|
||||
* @version 0.7
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if ( ! class_exists( 'SP_Admin_Importers' ) ) :
|
||||
|
||||
/**
|
||||
* SP_Admin_Importers Class
|
||||
*/
|
||||
class SP_Admin_Importers {
|
||||
|
||||
/**
|
||||
* Hook in tabs.
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_init', array( $this, 'register_importers' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add menu items
|
||||
*/
|
||||
public function register_importers() {
|
||||
register_importer( 'sportspress_event_csv', __( 'SportsPress Events (CSV)', 'sportspress' ), __( 'Import <strong>events</strong> from a csv file.', 'sportspress'), array( $this, 'events_importer' ) );
|
||||
register_importer( 'sportspress_team_csv', __( 'SportsPress Teams (CSV)', 'sportspress' ), __( 'Import <strong>teams</strong> from a csv file.', 'sportspress'), array( $this, 'teams_importer' ) );
|
||||
register_importer( 'sportspress_player_csv', __( 'SportsPress Players (CSV)', 'sportspress' ), __( 'Import <strong>players</strong> from a csv file.', 'sportspress'), array( $this, 'players_importer' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add menu item
|
||||
*/
|
||||
public function events_importer() {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Includes
|
||||
require 'importers/class-sp-event-importer.php';
|
||||
|
||||
// Dispatch
|
||||
$importer = new SP_Event_Importer();
|
||||
$importer->dispatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add menu item
|
||||
*/
|
||||
public function teams_importer() {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Includes
|
||||
require 'importers/class-sp-team-importer.php';
|
||||
|
||||
// Dispatch
|
||||
$importer = new SP_Team_Importer();
|
||||
$importer->dispatch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add menu item
|
||||
*/
|
||||
public function players_importer() {
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Includes
|
||||
require 'importers/class-sp-player-importer.php';
|
||||
|
||||
// Dispatch
|
||||
$importer = new SP_Player_Importer();
|
||||
$importer->dispatch();
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
||||
|
||||
return new SP_Admin_Importers();
|
||||
97
includes/admin/class-sp-admin.php
Normal file
97
includes/admin/class-sp-admin.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
/**
|
||||
* SportsPress Admin.
|
||||
*
|
||||
* @class SP_Admin
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin
|
||||
* @version 0.7
|
||||
*/
|
||||
class SP_Admin {
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'init', array( $this, 'includes' ) );
|
||||
add_action( 'current_screen', array( $this, 'conditonal_includes' ) );
|
||||
add_action( 'admin_init', array( $this, 'prevent_admin_access' ) );
|
||||
// add_action( 'admin_init', array( $this, 'preview_emails' ) );
|
||||
// add_action( 'admin_footer', 'wc_print_js', 25 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include any classes we need within admin.
|
||||
*/
|
||||
public function includes() {
|
||||
// Functions
|
||||
// include_once( 'wc-admin-functions.php' );
|
||||
// include_once( 'wc-meta-box-functions.php' );
|
||||
|
||||
// Classes
|
||||
// include_once( 'class-wc-admin-post-types.php' );
|
||||
// include_once( 'class-wc-admin-taxonomies.php' );
|
||||
|
||||
// Classes we only need if the ajax is not-ajax
|
||||
if ( ! is_ajax() ) {
|
||||
// include( 'class-wc-admin-menus.php' );
|
||||
// include( 'class-wc-admin-welcome.php' );
|
||||
// include( 'class-wc-admin-notices.php' );
|
||||
// include( 'class-wc-admin-assets.php' );
|
||||
// include( 'class-wc-admin-permalink-settings.php' );
|
||||
// include( 'class-wc-admin-editor.php' );
|
||||
|
||||
// Help
|
||||
// if ( apply_filters( 'sportspress_enable_admin_help_tab', true ) )
|
||||
// include( 'class-wc-admin-help.php' );
|
||||
}
|
||||
|
||||
// Importers
|
||||
if ( defined( 'WP_LOAD_IMPORTERS' ) )
|
||||
include( 'class-sp-admin-importers.php' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Include admin files conditionally
|
||||
*/
|
||||
public function conditonal_includes() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
switch ( $screen->id ) {
|
||||
case 'dashboard' :
|
||||
include( 'class-sp-admin-dashboard.php' );
|
||||
break;
|
||||
case 'users' :
|
||||
case 'user' :
|
||||
case 'profile' :
|
||||
case 'user-edit' :
|
||||
include( 'class-sp-admin-profile.php' );
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent any user who cannot 'edit_posts' (subscribers, customers etc) from accessing admin
|
||||
*/
|
||||
public function prevent_admin_access() {
|
||||
$prevent_access = false;
|
||||
|
||||
if ( 'yes' == get_option( 'sportspress_lock_down_admin' ) && ! is_ajax() && ! ( current_user_can( 'edit_posts' ) || current_user_can( 'manage_sportspress' ) ) && basename( $_SERVER["SCRIPT_FILENAME"] ) !== 'admin-post.php' ) {
|
||||
$prevent_access = true;
|
||||
}
|
||||
|
||||
$prevent_access = apply_filters( 'sportspress_prevent_admin_access', $prevent_access );
|
||||
|
||||
if ( $prevent_access ) {
|
||||
wp_safe_redirect( get_permalink( sp_get_page_id( 'myaccount' ) ) );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new SP_Admin();
|
||||
620
includes/admin/importers/class-sp-event-importer.php
Normal file
620
includes/admin/importers/class-sp-event-importer.php
Normal file
@@ -0,0 +1,620 @@
|
||||
<?php
|
||||
/**
|
||||
* Event importer - import events into SportsPress.
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin/Importers
|
||||
* @version 0.5
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if ( class_exists( 'WP_Importer' ) ) {
|
||||
class SP_Event_Importer extends WP_Importer {
|
||||
|
||||
var $id;
|
||||
var $file_url;
|
||||
var $import_page;
|
||||
var $delimiter;
|
||||
var $posts = array();
|
||||
var $imported;
|
||||
var $skipped;
|
||||
|
||||
/**
|
||||
* __construct function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->import_page = 'sportspress_event_csv';
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered callback function for the WordPress Importer
|
||||
*
|
||||
* Manages the three separate stages of the CSV import process
|
||||
*/
|
||||
function dispatch() {
|
||||
$this->header();
|
||||
|
||||
if ( ! empty( $_POST['delimiter'] ) )
|
||||
$this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
|
||||
|
||||
if ( ! $this->delimiter )
|
||||
$this->delimiter = ',';
|
||||
|
||||
$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() ):
|
||||
|
||||
if ( $this->id )
|
||||
$file = get_attached_file( $this->id );
|
||||
else
|
||||
$file = ABSPATH . $this->file_url;
|
||||
|
||||
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
|
||||
|
||||
if ( function_exists( 'gc_enable' ) )
|
||||
gc_enable();
|
||||
|
||||
@set_time_limit(0);
|
||||
@ob_flush();
|
||||
@flush();
|
||||
|
||||
$this->import( $file );
|
||||
endif;
|
||||
break;
|
||||
endswitch;
|
||||
$this->footer();
|
||||
}
|
||||
|
||||
/**
|
||||
* format_data_from_csv function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $data
|
||||
* @param string $enc
|
||||
* @return string
|
||||
*/
|
||||
function format_data_from_csv( $data, $enc ) {
|
||||
return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* import function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $file
|
||||
* @return void
|
||||
*/
|
||||
function import( $file ) {
|
||||
global $wpdb, $sportspress_options;
|
||||
|
||||
$this->imported = $this->skipped = 0;
|
||||
|
||||
if ( ! is_file($file) ):
|
||||
$this->footer();
|
||||
die();
|
||||
endif;
|
||||
|
||||
ini_set( 'auto_detect_line_endings', '1' );
|
||||
|
||||
if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ):
|
||||
|
||||
$header = fgetcsv( $handle, 0, $this->delimiter );
|
||||
|
||||
if ( sizeof( $header ) >= 3 ):
|
||||
|
||||
$loop = 0;
|
||||
|
||||
// Get event format, league, and season from post vars
|
||||
$event_format = ( empty( $_POST['sp_format'] ) ? false : $_POST['sp_format'] );
|
||||
$league = ( empty( $_POST['sp_league'] ) ? false : $_POST['sp_league'] );
|
||||
$season = ( empty( $_POST['sp_season'] ) ? false : $_POST['sp_season'] );
|
||||
|
||||
// Get labels from result and performance post types
|
||||
$result_labels = sportspress_get_var_labels( 'sp_result' );
|
||||
$performance_labels = sportspress_get_var_labels( 'sp_performance' );
|
||||
|
||||
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
|
||||
|
||||
// Slice array into event, team, and player
|
||||
$event = array_slice( $row, 0, 3 );
|
||||
$team = array_slice( $row, 3, 3 );
|
||||
$player = array_slice( $row, 6 );
|
||||
|
||||
// Add new event if date is given
|
||||
if ( sizeof( $event ) > 0 && ! empty( $event[0] ) ):
|
||||
|
||||
// Add player performance to last event if available
|
||||
if ( isset( $id ) && isset( $players ) && sizeof( $players ) > 0 ):
|
||||
update_post_meta( $id, 'sp_players', $players );
|
||||
endif;
|
||||
|
||||
// List event columns
|
||||
list( $date, $time, $venue ) = $event;
|
||||
|
||||
// Format date by replacing slashes with dashes
|
||||
$date = str_replace( '/', '-', trim( $date ) );
|
||||
|
||||
// Add time to date if given
|
||||
if ( ! empty( $time ) ):
|
||||
$date .= ' ' . trim( $time );
|
||||
endif;
|
||||
|
||||
// Define post type args
|
||||
$args = array( 'post_type' => 'sp_event', 'post_status' => 'publish', 'post_date' => $date );
|
||||
|
||||
// Insert event
|
||||
$id = wp_insert_post( $args );
|
||||
|
||||
// Initialize performance array
|
||||
$players = array();
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $id, '_sp_import', 1 );
|
||||
|
||||
// Update event format
|
||||
if ( $event_format ):
|
||||
update_post_meta( $id, 'sp_format', $event_format );
|
||||
endif;
|
||||
|
||||
// Update league
|
||||
if ( $league ):
|
||||
wp_set_object_terms( $id, $league, 'sp_league', false );
|
||||
endif;
|
||||
|
||||
// Update season
|
||||
if ( $season ):
|
||||
wp_set_object_terms( $id, $season, 'sp_season', false );
|
||||
endif;
|
||||
|
||||
// Update venue
|
||||
wp_set_object_terms( $id, $venue, 'sp_venue', false );
|
||||
|
||||
// Increment
|
||||
$loop ++;
|
||||
$this->imported ++;
|
||||
|
||||
endif;
|
||||
|
||||
// Add new team if team name is given
|
||||
if ( sizeof( $team ) > 0 && ! empty( $team[0] ) ):
|
||||
|
||||
// List team columns
|
||||
list( $team_name, $result, $outcome ) = $team;
|
||||
|
||||
// Find out if team exists
|
||||
$team_object = get_page_by_title( $team_name, OBJECT, 'sp_team' );
|
||||
|
||||
// Get or insert team
|
||||
if ( $team_object ):
|
||||
|
||||
// Make sure team is published
|
||||
if ( $team_object->post_status != 'publish' ):
|
||||
wp_update_post( array( 'ID' => $team_object->ID, 'post_status' => 'publish' ) );
|
||||
endif;
|
||||
|
||||
// Get team ID
|
||||
$team_id = $team_object->ID;
|
||||
|
||||
else:
|
||||
|
||||
// Insert team
|
||||
$team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team_name ) );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $team_id, '_sp_import', 1 );
|
||||
|
||||
endif;
|
||||
|
||||
// Update league
|
||||
if ( $league ):
|
||||
wp_set_object_terms( $team_id, $league, 'sp_league', true );
|
||||
endif;
|
||||
|
||||
// Update season
|
||||
if ( $season ):
|
||||
wp_set_object_terms( $team_id, $season, 'sp_season', true );
|
||||
endif;
|
||||
|
||||
// Add to event if exists
|
||||
if ( isset( $id ) ):
|
||||
|
||||
// Add team to event
|
||||
add_post_meta( $id, 'sp_team', $team_id );
|
||||
|
||||
// Add empty player to event
|
||||
add_post_meta( $id, 'sp_player', 0 );
|
||||
|
||||
// Explode results into array
|
||||
$results = explode( '|', $result );
|
||||
|
||||
// Create team results array from result keys
|
||||
$team_results = array();
|
||||
if ( sizeof( $result_labels ) > 0 ):
|
||||
foreach( $result_labels as $key => $label ):
|
||||
$team_results[ $key ] = trim( array_shift( $results ) );
|
||||
endforeach;
|
||||
$team_results[ 'outcome' ] = array();
|
||||
endif;
|
||||
|
||||
// Explode outcomes into array
|
||||
$outcomes = explode( '|', $outcome );
|
||||
|
||||
// Add outcome slugs to team outcomes array
|
||||
foreach ( $outcomes as $outcome ):
|
||||
|
||||
// Continue if outcome doesn't exist
|
||||
if ( $outcome == null ):
|
||||
continue;
|
||||
endif;
|
||||
|
||||
// Remove whitespace
|
||||
$outcome = trim( $outcome );
|
||||
|
||||
// Get or insert outcome
|
||||
$outcome_object = get_page_by_title( $outcome, OBJECT, 'sp_outcome' );
|
||||
|
||||
if ( $outcome_object ):
|
||||
|
||||
// Make sure outcome is published
|
||||
if ( $outcome_object->post_status != 'publish' ):
|
||||
wp_update_post( array( 'ID' => $outcome_object->ID, 'post_status' => 'publish' ) );
|
||||
endif;
|
||||
|
||||
// Get outcome slug
|
||||
$outcome_slug = $outcome_object->post_name;
|
||||
|
||||
else:
|
||||
|
||||
// Insert outcome
|
||||
$outcome_id = wp_insert_post( array( 'post_type' => 'sp_outcome', 'post_status' => 'publish', 'post_title' => $outcome ) );
|
||||
|
||||
// Get outcome slug
|
||||
$post_data = get_post( $outcome_id, ARRAY_A );
|
||||
$outcome_slug = $post_data['post_name'];
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $outcome_id, '_sp_import', 1 );
|
||||
|
||||
endif;
|
||||
|
||||
// Add to team results array
|
||||
$team_results[ 'outcome' ][] = $outcome_slug;
|
||||
|
||||
endforeach;
|
||||
|
||||
// Get existing results
|
||||
$event_results = get_post_meta( $id, 'sp_results', true );
|
||||
|
||||
// Create new array if results not exists
|
||||
if ( ! $event_results ):
|
||||
$event_results = array();
|
||||
endif;
|
||||
|
||||
// Add team results to existing results
|
||||
$event_results[ $team_id ] = $team_results;
|
||||
|
||||
// Update event results
|
||||
update_post_meta( $id, 'sp_results', $event_results );
|
||||
|
||||
// Get event name
|
||||
$title = get_the_title( $id );
|
||||
|
||||
// Add delimiter if event name is set
|
||||
if ( $title ):
|
||||
$title .= ' ' . sportspress_array_value( $sportspress_options, 'event_teams_delimiter', 'vs' ) . ' ';
|
||||
endif;
|
||||
|
||||
// Append team name to event name
|
||||
$title .= $team_name;
|
||||
|
||||
// Update event with new name
|
||||
$post = array(
|
||||
'ID' => $id,
|
||||
'post_title' => $title,
|
||||
);
|
||||
wp_update_post( $post );
|
||||
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
// Add new player if player name is given
|
||||
if ( sizeof( $player ) > 0 && ! empty( $player[0] ) ):
|
||||
|
||||
// Get and unset player name leaving us with the performance
|
||||
$player_name = $player[0];
|
||||
unset( $player[0] );
|
||||
|
||||
// Find out if player exists
|
||||
$player_object = get_page_by_title( $player_name, OBJECT, 'sp_player' );
|
||||
|
||||
// Get or insert player
|
||||
if ( $player_object ):
|
||||
|
||||
// Make sure player is published
|
||||
if ( $player_object->post_status != 'publish' ):
|
||||
wp_update_post( array( 'ID' => $player_object->ID, 'post_status' => 'publish' ) );
|
||||
endif;
|
||||
|
||||
// Get player ID
|
||||
$player_id = $player_object->ID;
|
||||
|
||||
else:
|
||||
|
||||
// Insert player
|
||||
$player_id = wp_insert_post( array( 'post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $player_name ) );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $player_id, '_sp_import', 1 );
|
||||
|
||||
// Update number
|
||||
update_post_meta( $player_id, 'sp_number', null );
|
||||
|
||||
endif;
|
||||
|
||||
// Update league
|
||||
if ( $league ):
|
||||
wp_set_object_terms( $player_id, $league, 'sp_league', true );
|
||||
endif;
|
||||
|
||||
// Update season
|
||||
if ( $season ):
|
||||
wp_set_object_terms( $player_id, $season, 'sp_season', true );
|
||||
endif;
|
||||
|
||||
// Add to event if exists
|
||||
if ( isset( $id ) ):
|
||||
|
||||
// Add player to event
|
||||
add_post_meta( $id, 'sp_player', $player_id );
|
||||
|
||||
// Add player performance to array if team is available
|
||||
if ( isset( $team_id ) ):
|
||||
|
||||
// Initialize performance array
|
||||
$performance = array();
|
||||
|
||||
// Map keys to player performance
|
||||
foreach ( $performance_labels as $key => $label ):
|
||||
$performance[ $key ] = array_shift( $player );
|
||||
endforeach;
|
||||
$players[ $team_id ][ $player_id ] = $performance;
|
||||
|
||||
// Get player teams
|
||||
$player_teams = get_post_meta( $player_id, 'sp_team', false );
|
||||
$current_team = get_post_meta( $player_id, 'sp_current_team', true );
|
||||
$past_teams = get_post_meta( $player_id, 'sp_past_team', false );
|
||||
|
||||
// Add team if not exists in player
|
||||
if ( ! in_array( $team_id, $player_teams ) ):
|
||||
add_post_meta( $player_id, 'sp_team', $team_id );
|
||||
endif;
|
||||
|
||||
// Add as past team or set current team if not set
|
||||
if ( ! $current_team ):
|
||||
update_post_meta( $player_id, 'sp_current_team', $team_id );
|
||||
elseif ( $current_team != $team_id && ! in_array( $team_id, $past_teams ) ):
|
||||
add_post_meta( $player_id, 'sp_past_team', $team_id );
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
endif;
|
||||
|
||||
endwhile;
|
||||
|
||||
// Add player performance to last event if available
|
||||
if ( isset( $id ) && isset( $players ) && sizeof( $players ) > 0 ):
|
||||
update_post_meta( $id, 'sp_players', $players );
|
||||
endif;
|
||||
|
||||
else:
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo __( 'The CSV is invalid.', 'sportspress' ) . '</p>';
|
||||
$this->footer();
|
||||
die();
|
||||
|
||||
endif;
|
||||
|
||||
fclose( $handle );
|
||||
endif;
|
||||
|
||||
// Show Result
|
||||
echo '<div class="updated settings-error below-h2"><p>
|
||||
'.sprintf( __( 'Import complete - imported <strong>%s</strong> events and skipped <strong>%s</strong>.', 'sportspress' ), $this->imported, $this->skipped ).'
|
||||
</p></div>';
|
||||
|
||||
$this->import_end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs post-import cleanup of files and the cache
|
||||
*/
|
||||
function import_end() {
|
||||
echo '<p>' . __( 'All done!', 'sportspress' ) . ' <a href="' . admin_url('edit.php?post_type=sp_event') . '">' . __( 'View Events', 'sportspress' ) . '</a>' . '</p>';
|
||||
|
||||
do_action( 'import_end' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the CSV upload and initial parsing of the file to prepare for
|
||||
* displaying author import options
|
||||
*
|
||||
* @return bool False if error uploading or invalid file, true otherwise
|
||||
*/
|
||||
function handle_upload() {
|
||||
|
||||
if ( empty( $_POST['file_url'] ) ) {
|
||||
|
||||
$file = wp_import_handle_upload();
|
||||
|
||||
if ( isset( $file['error'] ) ) {
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo esc_html( $file['error'] ) . '</p>';
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->id = (int) $file['id'];
|
||||
|
||||
} else {
|
||||
|
||||
if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
|
||||
|
||||
$this->file_url = esc_attr( $_POST['file_url'] );
|
||||
|
||||
} else {
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong></p>';
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* header function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function header() {
|
||||
echo '<div class="wrap"><h2>' . __( 'Import Events', 'sportspress' ) . '</h2>';
|
||||
}
|
||||
|
||||
/**
|
||||
* footer function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function footer() {
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* greet function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function greet() {
|
||||
|
||||
echo '<div class="narrow">';
|
||||
echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>';
|
||||
|
||||
echo '<p>' . sprintf( __( 'Events need to be defined with columns in a specific order (3+ columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/events-sample.csv' ) . '</p>';
|
||||
|
||||
$action = 'admin.php?import=sportspress_event_csv&step=1';
|
||||
|
||||
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
|
||||
$size = size_format( $bytes );
|
||||
$upload_dir = wp_upload_dir();
|
||||
if ( ! empty( $upload_dir['error'] ) ) :
|
||||
?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:', 'sportspress'); ?></p>
|
||||
<p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
|
||||
else :
|
||||
?>
|
||||
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="file" id="upload" name="import" size="25" />
|
||||
<input type="hidden" name="action" value="save" />
|
||||
<input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
|
||||
<small><?php printf( __( 'Maximum size: %s', 'sportspress' ), $size ); ?></small>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="file_url"><?php _e( 'OR enter path to file:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Delimiter', 'sportspress' ); ?></label><br/></th>
|
||||
<td><input type="text" name="delimiter" placeholder="," size="2" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Format', 'sportspress' ); ?></label><br/></th>
|
||||
<td id="sp_formatdiv">
|
||||
<div id="post-formats-select">
|
||||
<input type="radio" name="sp_format" class="post-format" id="post-format-league" value="league" checked="checked"> <label for="post-format-league" class="post-format-icon post-format-league">League</label>
|
||||
<br><input type="radio" name="sp_format" class="post-format" id="post-format-friendly" value="friendly"> <label for="post-format-friendly" class="post-format-icon post-format-friendly">Friendly</label>
|
||||
<br>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'League', 'sportspress' ); ?></label><br/></th>
|
||||
<td><?php
|
||||
$args = array(
|
||||
'taxonomy' => 'sp_league',
|
||||
'name' => 'sp_league',
|
||||
'values' => 'slug',
|
||||
'show_option_none' => __( '-- Not set --', 'sportspress' ),
|
||||
);
|
||||
if ( ! sportspress_dropdown_taxonomies( $args ) ):
|
||||
echo '<p>' . __( 'None', 'sportspress' ) . '</p>';
|
||||
sportspress_taxonomy_adder( 'sp_league', 'sp_team', __( 'Add New', 'sportspress' ) );
|
||||
endif;
|
||||
?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Season', 'sportspress' ); ?></label><br/></th>
|
||||
<td><?php
|
||||
$args = array(
|
||||
'taxonomy' => 'sp_season',
|
||||
'name' => 'sp_season',
|
||||
'values' => 'slug',
|
||||
'show_option_none' => __( '-- Not set --', 'sportspress' ),
|
||||
);
|
||||
if ( ! sportspress_dropdown_taxonomies( $args ) ):
|
||||
echo '<p>' . __( 'None', 'sportspress' ) . '</p>';
|
||||
sportspress_taxonomy_adder( 'sp_season', 'sp_team', __( 'Add New', 'sportspress' ) );
|
||||
endif;
|
||||
?></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="submit">
|
||||
<input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'sportspress' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Added to http_request_timeout filter to force timeout at 60 seconds during import
|
||||
* @param int $val
|
||||
* @return int 60
|
||||
*/
|
||||
function bump_request_timeout( $val ) {
|
||||
return 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
347
includes/admin/importers/class-sp-player-importer.php
Normal file
347
includes/admin/importers/class-sp-player-importer.php
Normal file
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
/**
|
||||
* Player importer - import players into SportsPress.
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin/Importers
|
||||
* @version 0.2.11
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if ( class_exists( 'WP_Importer' ) ) {
|
||||
class SP_Player_Importer extends WP_Importer {
|
||||
|
||||
var $id;
|
||||
var $file_url;
|
||||
var $import_page;
|
||||
var $delimiter;
|
||||
var $posts = array();
|
||||
var $imported;
|
||||
var $skipped;
|
||||
|
||||
/**
|
||||
* __construct function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->import_page = 'sportspress_player_csv';
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered callback function for the WordPress Importer
|
||||
*
|
||||
* Manages the three separate stages of the CSV import process
|
||||
*/
|
||||
function dispatch() {
|
||||
$this->header();
|
||||
|
||||
if ( ! empty( $_POST['delimiter'] ) )
|
||||
$this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
|
||||
|
||||
if ( ! $this->delimiter )
|
||||
$this->delimiter = ',';
|
||||
|
||||
$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() ):
|
||||
|
||||
if ( $this->id )
|
||||
$file = get_attached_file( $this->id );
|
||||
else
|
||||
$file = ABSPATH . $this->file_url;
|
||||
|
||||
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
|
||||
|
||||
if ( function_exists( 'gc_enable' ) )
|
||||
gc_enable();
|
||||
|
||||
@set_time_limit(0);
|
||||
@ob_flush();
|
||||
@flush();
|
||||
|
||||
$this->import( $file );
|
||||
endif;
|
||||
break;
|
||||
endswitch;
|
||||
$this->footer();
|
||||
}
|
||||
|
||||
/**
|
||||
* format_data_from_csv function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $data
|
||||
* @param string $enc
|
||||
* @return string
|
||||
*/
|
||||
function format_data_from_csv( $data, $enc ) {
|
||||
return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* import function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $file
|
||||
* @return void
|
||||
*/
|
||||
function import( $file ) {
|
||||
global $wpdb;
|
||||
|
||||
$this->imported = $this->skipped = 0;
|
||||
|
||||
if ( ! is_file($file) ):
|
||||
$this->footer();
|
||||
die();
|
||||
endif;
|
||||
|
||||
ini_set( 'auto_detect_line_endings', '1' );
|
||||
|
||||
if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ):
|
||||
|
||||
$header = fgetcsv( $handle, 0, $this->delimiter );
|
||||
|
||||
if ( sizeof( $header ) == 7 ):
|
||||
|
||||
$loop = 0;
|
||||
|
||||
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
|
||||
|
||||
list( $number, $name, $positions, $teams, $leagues, $seasons, $nationality ) = $row;
|
||||
|
||||
$nationality = trim( strtoupper( $nationality ) );
|
||||
|
||||
if ( $nationality == '*' )
|
||||
$nationality = '';
|
||||
|
||||
if ( ! $name ):
|
||||
$this->skipped++;
|
||||
continue;
|
||||
endif;
|
||||
|
||||
$args = array( 'post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $name );
|
||||
|
||||
$id = wp_insert_post( $args );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $id, '_sp_import', 1 );
|
||||
|
||||
// Update number
|
||||
update_post_meta( $id, 'sp_number', $number );
|
||||
|
||||
// Update positions
|
||||
$positions = explode( '|', $positions );
|
||||
wp_set_object_terms( $id, $positions, 'sp_position', false );
|
||||
|
||||
// Update leagues
|
||||
$leagues = explode( '|', $leagues );
|
||||
wp_set_object_terms( $id, $leagues, 'sp_league', false );
|
||||
|
||||
// Update seasons
|
||||
$seasons = explode( '|', $seasons );
|
||||
wp_set_object_terms( $id, $seasons, 'sp_season', false );
|
||||
|
||||
// Update teams
|
||||
$teams = (array)explode( '|', $teams );
|
||||
$i = 0;
|
||||
foreach ( $teams as $team ):
|
||||
// Get or insert team
|
||||
$team_object = get_page_by_title( $team, OBJECT, 'sp_team' );
|
||||
if ( $team_object ):
|
||||
if ( $team_object->post_status != 'publish' ):
|
||||
wp_update_post( array( 'ID' => $team_object->ID, 'post_status' => 'publish' ) );
|
||||
endif;
|
||||
$team_id = $team_object->ID;
|
||||
else:
|
||||
$team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team ) );
|
||||
// Flag as import
|
||||
update_post_meta( $team_id, '_sp_import', 1 );
|
||||
wp_set_object_terms( $team_id, $leagues, 'sp_league', false );
|
||||
wp_set_object_terms( $team_id, $seasons, 'sp_season', false );
|
||||
endif;
|
||||
|
||||
// Add team to player
|
||||
add_post_meta( $id, 'sp_team', $team_id );
|
||||
|
||||
// Update current team if first in array
|
||||
if ( $i == 0 ):
|
||||
update_post_meta( $id, 'sp_current_team', $team_id );
|
||||
endif;
|
||||
|
||||
$i++;
|
||||
endforeach;
|
||||
|
||||
// Update nationality
|
||||
update_post_meta( $id, 'sp_nationality', $nationality );
|
||||
|
||||
$loop ++;
|
||||
$this->imported++;
|
||||
endwhile;
|
||||
|
||||
else:
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo __( 'The CSV is invalid.', 'sportspress' ) . '</p>';
|
||||
$this->footer();
|
||||
die();
|
||||
|
||||
endif;
|
||||
|
||||
fclose( $handle );
|
||||
endif;
|
||||
|
||||
// Show Result
|
||||
echo '<div class="updated settings-error below-h2"><p>
|
||||
'.sprintf( __( 'Import complete - imported <strong>%s</strong> players and skipped <strong>%s</strong>.', 'sportspress' ), $this->imported, $this->skipped ).'
|
||||
</p></div>';
|
||||
|
||||
$this->import_end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs post-import cleanup of files and the cache
|
||||
*/
|
||||
function import_end() {
|
||||
echo '<p>' . __( 'All done!', 'sportspress' ) . ' <a href="' . admin_url('edit.php?post_type=sp_player') . '">' . __( 'View Players', 'sportspress' ) . '</a>' . '</p>';
|
||||
|
||||
do_action( 'import_end' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the CSV upload and initial parsing of the file to prepare for
|
||||
* displaying author import options
|
||||
*
|
||||
* @return bool False if error uploading or invalid file, true otherwise
|
||||
*/
|
||||
function handle_upload() {
|
||||
|
||||
if ( empty( $_POST['file_url'] ) ) {
|
||||
|
||||
$file = wp_import_handle_upload();
|
||||
|
||||
if ( isset( $file['error'] ) ) {
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo esc_html( $file['error'] ) . '</p>';
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->id = (int) $file['id'];
|
||||
|
||||
} else {
|
||||
|
||||
if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
|
||||
|
||||
$this->file_url = esc_attr( $_POST['file_url'] );
|
||||
|
||||
} else {
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong></p>';
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* header function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function header() {
|
||||
echo '<div class="wrap"><h2>' . __( 'Import Players', 'sportspress' ) . '</h2>';
|
||||
}
|
||||
|
||||
/**
|
||||
* footer function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function footer() {
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* greet function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function greet() {
|
||||
|
||||
echo '<div class="narrow">';
|
||||
echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>';
|
||||
|
||||
echo '<p>' . sprintf( __( 'Players need to be defined with columns in a specific order (7 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/players-sample.csv' ) . '</p>';
|
||||
|
||||
$action = 'admin.php?import=sportspress_player_csv&step=1';
|
||||
|
||||
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
|
||||
$size = size_format( $bytes );
|
||||
$upload_dir = wp_upload_dir();
|
||||
if ( ! empty( $upload_dir['error'] ) ) :
|
||||
?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:', 'sportspress'); ?></p>
|
||||
<p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
|
||||
else :
|
||||
?>
|
||||
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="upload"><?php _e( 'Choose a file from your computer:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="file" id="upload" name="import" size="25" />
|
||||
<input type="hidden" name="action" value="save" />
|
||||
<input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
|
||||
<small><?php printf( __( 'Maximum size: %s', 'sportspress' ), $size ); ?></small>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="file_url"><?php _e( 'OR enter path to file:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Delimiter', 'sportspress' ); ?></label><br/></th>
|
||||
<td><input type="text" name="delimiter" placeholder="," size="2" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="submit">
|
||||
<input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'sportspress' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Added to http_request_timeout filter to force timeout at 60 seconds during import
|
||||
* @param int $val
|
||||
* @return int 60
|
||||
*/
|
||||
function bump_request_timeout( $val ) {
|
||||
return 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
304
includes/admin/importers/class-sp-team-importer.php
Normal file
304
includes/admin/importers/class-sp-team-importer.php
Normal file
@@ -0,0 +1,304 @@
|
||||
<?php
|
||||
/**
|
||||
* Team importer - import teams into SportsPress.
|
||||
*
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin/Importers
|
||||
* @version 0.2.11
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if ( class_exists( 'WP_Importer' ) ) {
|
||||
class SP_Team_Importer extends WP_Importer {
|
||||
|
||||
var $id;
|
||||
var $file_url;
|
||||
var $import_page;
|
||||
var $delimiter;
|
||||
var $posts = array();
|
||||
var $imported;
|
||||
var $skipped;
|
||||
|
||||
/**
|
||||
* __construct function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->import_page = 'sportspress_team_csv';
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered callback function for the WordPress Importer
|
||||
*
|
||||
* Manages the three separate stages of the CSV import process
|
||||
*/
|
||||
function dispatch() {
|
||||
$this->header();
|
||||
|
||||
if ( ! empty( $_POST['delimiter'] ) )
|
||||
$this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
|
||||
|
||||
if ( ! $this->delimiter )
|
||||
$this->delimiter = ',';
|
||||
|
||||
$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() ):
|
||||
|
||||
if ( $this->id )
|
||||
$file = get_attached_file( $this->id );
|
||||
else
|
||||
$file = ABSPATH . $this->file_url;
|
||||
|
||||
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
|
||||
|
||||
if ( function_exists( 'gc_enable' ) )
|
||||
gc_enable();
|
||||
|
||||
@set_time_limit(0);
|
||||
@ob_flush();
|
||||
@flush();
|
||||
|
||||
$this->import( $file );
|
||||
endif;
|
||||
break;
|
||||
endswitch;
|
||||
$this->footer();
|
||||
}
|
||||
|
||||
/**
|
||||
* format_data_from_csv function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $data
|
||||
* @param string $enc
|
||||
* @return string
|
||||
*/
|
||||
function format_data_from_csv( $data, $enc ) {
|
||||
return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* import function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $file
|
||||
* @return void
|
||||
*/
|
||||
function import( $file ) {
|
||||
global $wpdb;
|
||||
|
||||
$this->imported = $this->skipped = 0;
|
||||
|
||||
if ( ! is_file($file) ):
|
||||
$this->footer();
|
||||
die();
|
||||
endif;
|
||||
|
||||
ini_set( 'auto_detect_line_endings', '1' );
|
||||
|
||||
if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ):
|
||||
|
||||
$header = fgetcsv( $handle, 0, $this->delimiter );
|
||||
|
||||
if ( sizeof( $header ) == 3 ):
|
||||
|
||||
$loop = 0;
|
||||
|
||||
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
|
||||
|
||||
list( $name, $leagues, $seasons ) = $row;
|
||||
|
||||
$team_object = get_page_by_title( $name, OBJECT, 'sp_team' );
|
||||
|
||||
if ( ! $name || $team_object ):
|
||||
$this->skipped++;
|
||||
continue;
|
||||
endif;
|
||||
|
||||
$args = array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $name );
|
||||
|
||||
$id = wp_insert_post( $args );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $id, '_sp_import', 1 );
|
||||
|
||||
// Update leagues
|
||||
$leagues = explode( '|', $leagues );
|
||||
wp_set_object_terms( $id, $leagues, 'sp_league', false );
|
||||
|
||||
// Update seasons
|
||||
$seasons = explode( '|', $seasons );
|
||||
wp_set_object_terms( $id, $seasons, 'sp_season', false );
|
||||
|
||||
$loop ++;
|
||||
$this->imported++;
|
||||
endwhile;
|
||||
|
||||
else:
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo __( 'The CSV is invalid.', 'sportspress' ) . '</p>';
|
||||
$this->footer();
|
||||
die();
|
||||
|
||||
endif;
|
||||
|
||||
fclose( $handle );
|
||||
endif;
|
||||
|
||||
// Show Result
|
||||
echo '<div class="updated settings-error below-h2"><p>
|
||||
'.sprintf( __( 'Import complete - imported <strong>%s</strong> teams and skipped <strong>%s</strong>.', 'sportspress' ), $this->imported, $this->skipped ).'
|
||||
</p></div>';
|
||||
|
||||
$this->import_end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs post-import cleanup of files and the cache
|
||||
*/
|
||||
function import_end() {
|
||||
echo '<p>' . __( 'All done!', 'sportspress' ) . ' <a href="' . admin_url('edit.php?post_type=sp_team') . '">' . __( 'View Teams', 'sportspress' ) . '</a>' . '</p>';
|
||||
|
||||
do_action( 'import_end' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the CSV upload and initial parsing of the file to prepare for
|
||||
* displaying author import options
|
||||
*
|
||||
* @return bool False if error uploading or invalid file, true otherwise
|
||||
*/
|
||||
function handle_upload() {
|
||||
|
||||
if ( empty( $_POST['file_url'] ) ) {
|
||||
|
||||
$file = wp_import_handle_upload();
|
||||
|
||||
if ( isset( $file['error'] ) ) {
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
echo esc_html( $file['error'] ) . '</p>';
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->id = (int) $file['id'];
|
||||
|
||||
} else {
|
||||
|
||||
if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
|
||||
|
||||
$this->file_url = esc_attr( $_POST['file_url'] );
|
||||
|
||||
} else {
|
||||
|
||||
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong></p>';
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* header function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function header() {
|
||||
echo '<div class="wrap"><h2>' . __( 'Import Teams', 'sportspress' ) . '</h2>';
|
||||
}
|
||||
|
||||
/**
|
||||
* footer function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function footer() {
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* greet function.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function greet() {
|
||||
|
||||
echo '<div class="narrow">';
|
||||
echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>';
|
||||
|
||||
echo '<p>' . sprintf( __( 'Teams need to be defined with columns in a specific order (3 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/teams-sample.csv' ) . '</p>';
|
||||
|
||||
$action = 'admin.php?import=sportspress_team_csv&step=1';
|
||||
|
||||
$bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
|
||||
$size = size_format( $bytes );
|
||||
$upload_dir = wp_upload_dir();
|
||||
if ( ! empty( $upload_dir['error'] ) ) :
|
||||
?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:', 'sportspress'); ?></p>
|
||||
<p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
|
||||
else :
|
||||
?>
|
||||
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="upload"><?php _e( 'Choose a file from your computer:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="file" id="upload" name="import" size="25" />
|
||||
<input type="hidden" name="action" value="save" />
|
||||
<input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
|
||||
<small><?php printf( __( 'Maximum size: %s', 'sportspress' ), $size ); ?></small>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="file_url"><?php _e( 'OR enter path to file:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Delimiter', 'sportspress' ); ?></label><br/></th>
|
||||
<td><input type="text" name="delimiter" placeholder="," size="2" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="submit">
|
||||
<input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'sportspress' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Added to http_request_timeout filter to force timeout at 60 seconds during import
|
||||
* @param int $val
|
||||
* @return int 60
|
||||
*/
|
||||
function bump_request_timeout( $val ) {
|
||||
return 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
10
includes/admin/views/html-notice-install.php
Normal file
10
includes/admin/views/html-notice-install.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
?>
|
||||
<div id="message" class="updated sportspress-message">
|
||||
<p><?php _e( '<strong>Welcome to SportsPress</strong> – Get Started', 'sportspress' ); ?></p>
|
||||
<p class="submit">
|
||||
<a class="button-primary" href="<?php echo admin_url('options-general.php?page=sportspress'); ?>"><?php _e( 'Go to SportsPress Settings', 'sportspress' ); ?></a>
|
||||
<a class="button-secondary" href="<?php echo add_query_arg('sportspress_installed', '1' ); ?>"><?php _e( 'Skip setup', 'sportspress' ); ?></a>
|
||||
</p>
|
||||
</div>
|
||||
10
includes/admin/views/html-notice-theme-support.php
Normal file
10
includes/admin/views/html-notice-theme-support.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
?>
|
||||
<div id="message" class="error sportspress-message">
|
||||
<p><?php _e( '<strong>Your theme does not declare SportsPress support</strong> – if you encounter layout issues please read our integration guide or choose a SportsPress theme :)', 'sportspress' ); ?></p>
|
||||
<p class="submit">
|
||||
<a class="button-primary" href="http://docs.themeboy.com/sportspress/compatibility/"><?php _e( 'Theme Integration Guide', 'sportspress' ); ?></a>
|
||||
<a class="button-secondary" href="<?php echo add_query_arg( 'hide_sportspress_theme_support_check', 'true' ); ?>"><?php _e( 'Hide this notice', 'sportspress' ); ?></a>
|
||||
</p>
|
||||
</div>
|
||||
Reference in New Issue
Block a user