diff --git a/includes/admin/importers/class-sp-team-importer.php b/includes/admin/importers/class-sp-team-importer.php new file mode 100644 index 00000000..d447c0cc --- /dev/null +++ b/includes/admin/importers/class-sp-team-importer.php @@ -0,0 +1,183 @@ +import_page = 'sportspress_team_csv'; + } + + /** + * 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 '

' . __( 'Sorry, there has been an error.', 'sportspress' ) . '
'; + echo __( 'The CSV is invalid.', 'sportspress' ) . '

'; + $this->footer(); + die(); + + endif; + + fclose( $handle ); + endif; + + // Show Result + echo '

+ '.sprintf( __( 'Import complete - imported %s teams and skipped %s.', 'sportspress' ), $this->imported, $this->skipped ).' +

'; + + $this->import_end(); + } + + /** + * Performs post-import cleanup of files and the cache + */ + function import_end() { + echo '

' . __( 'All done!', 'sportspress' ) . ' ' . __( 'View Teams', 'sportspress' ) . '' . '

'; + + do_action( 'import_end' ); + } + + /** + * header function. + * + * @access public + * @return void + */ + function header() { + echo '

' . __( 'Import Teams', 'sportspress' ) . '

'; + } + + /** + * greet function. + * + * @access public + * @return void + */ + function greet() { + + echo '
'; + echo '

' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'

'; + + echo '

' . sprintf( __( 'Teams need to be defined with columns in a specific order (3 columns). Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/teams-sample.csv' ) . '

'; + + $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'] ) ) : + ?>

+

+
+ + + + + + + + + + + + + + + +
+ + + + + + +
+ + + +

+

+ +

+
+ '; + } + } +}