From 6b4a421ec2a4c882409bf3fb49338d6143ec89f1 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Tue, 26 Aug 2014 02:17:00 +1000 Subject: [PATCH] Improve CSV importer close #50 --- assets/css/admin.css | 47 + assets/css/menu.css | 12 +- assets/fonts/sportspress.eot | Bin 4716 -> 5380 bytes assets/fonts/sportspress.svg | 3 + assets/fonts/sportspress.ttf | Bin 4536 -> 5200 bytes assets/fonts/sportspress.woff | Bin 3416 -> 3800 bytes assets/js/admin/sportspress-admin.js | 49 + includes/admin/class-sp-admin-assets.php | 3 +- .../importers/class-sp-event-importer.php | 853 +++++++++--------- .../admin/importers/class-sp-importer.php | 143 ++- .../importers/class-sp-player-importer.php | 228 ++--- .../importers/class-sp-staff-importer.php | 219 ++--- .../importers/class-sp-team-importer.php | 152 +--- includes/admin/sp-admin-functions.php | 1 + includes/class-sp-wpml.php | 4 +- 15 files changed, 903 insertions(+), 811 deletions(-) diff --git a/assets/css/admin.css b/assets/css/admin.css index 7abed824..8d98f2e8 100644 --- a/assets/css/admin.css +++ b/assets/css/admin.css @@ -106,6 +106,15 @@ margin: 0; } +.sportspress table.form-table .forminp-radio ul li { + display: inline; + margin-right: 20px; +} + +.sportspress table.form-table .forminp-radio ul li label.post-format-icon { + margin-top: 0 !important; +} + .sportspress table.form-table fieldset { margin-top: 4px; } @@ -483,6 +492,44 @@ table.sp_status_table td mark.yes { color: #7ad03a; } +/* Importers */ +#import-upload-form table.form-table th, +#import-upload-form table.form-table td { + padding-bottom: 0; +} +#import-upload-form table.form-table tbody tr:last-child td { + padding-bottom: 20px; +} +.sp-import-table th, +.sp-import-table td { + padding-right: 0; +} +.sp-import-table th:last-child, +.sp-import-table td:last-child { + padding-right: 10px; +} +.sp-import-table tbody tr:nth-child(2n-1) { + background: #f9f9f9; +} +.sp-import-table select { + max-width: 100%; +} +.sp-import-table .sp-actions-column { + width: 40px; + text-align: right; +} +.sp-import-table .sp-add-row { + line-height: 1.5; +} +.sp-import-table .sp-delete-row { + font-size: 16px; + line-height: 1.5; + color: #a00; +} +.sp-import-table .sp-delete-row:hover { + color: #f00; +} + /* Media Queries */ @media only screen and (max-width: 782px) { diff --git a/assets/css/menu.css b/assets/css/menu.css index de27f7c8..a578ee81 100644 --- a/assets/css/menu.css +++ b/assets/css/menu.css @@ -60,6 +60,9 @@ .sp-icon-smile:before { content: "\f328"; } +.sp-icon-trash:before { + content: "\f153"; +} #adminmenu #toplevel_page_sportspress .toplevel_page_sportspress div.wp-menu-image:before, #adminmenu #menu-posts-sp_event .menu-icon-sp_event div.wp-menu-image:before, @@ -84,12 +87,15 @@ box-shadow: none; } +.sp-prefs .button input[type=checkbox]:before { + font: 400 14px/22px sportspress, dashicons; + width: 22px; +} + .sp-prefs .button input[type=checkbox]:not(:checked):before { float: left; display: inline-block; vertical-align: middle; - width: 16px; - font: 400 21px/1 dashicons; speak: none; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; @@ -99,11 +105,9 @@ } /* TinyMCE */ - .mce-toolbar i.mce-i-sp_shortcodes_button, .mce-toolbar span.mce_sp_shortcodes_button { margin-right: -5px; } - i.mce-i-sp_shortcodes_button:before, span.mce_sp_shortcodes_button:before { font-family: 'sportspress'; speak: none; diff --git a/assets/fonts/sportspress.eot b/assets/fonts/sportspress.eot index 8a43961d6cc01b4454d1c6773a9bab9ea3565f1c..3320c79295cd3741192f7820591fe6f6df6585c8 100755 GIT binary patch delta 1063 zcmYjQO-vI}5T4m?yDht|ZMSP%TH0;52qNLH6oNw27>PzSQF=9zhW`YF1P=HUi_wF6 zP;gE3qJczXBHl3JVj>3Tb!?cYD0B(tRY-D_TCbYuo#G8m;8k^|P1iz_!#P1NV9nZ{6LI~@Lzag&P z$c+037oK^h$nk}Wj80BWO@oOf@HFXTc>Bytf>BU`2M!R_oGaIzJD*GE2KH+9g?)Ly zc0X}2dT{UHVd1DyV0on&y^;(2KRZ4;-r9HUy!1!4y zz=ofVE1Ik&T3KtSimg{sE3Ba^jJrIn&3ME(hJ-6#jMJ(qDIrH(5f?+Krc$f3X5K6c zvw}#rxT}T-%Siz!GQWx`1`rk~v)RZnLQ}@B=2uHDOsNJ@RW(10YJO{L0hY7;Ih&n+ zTKs`)G_-n1=^OSyRxnP!{ohQA!!VamOEJbIAT*}K;aDsjPB)g&l9}fv;kW3O?3E5J z>GKA#t(+M$uEoNkbYoL*F~7H|F&zrWcI*Mk?hRG^vPwM92fW^7tiOac2U9DSSM3+L zm@lz7bp2T8A&49s32dl~)g@aTk6!N#dPqhiqO{5SHD{ojE(5R?FIz1P+Xn5AyPk^AY|96mrGk delta 500 zcmZqCdZWUYBgDY4LvSLS8OyCro4F=BOsPLzyYvwQ1EUNB1H+v3#Nq-VEdb>20MZ=k zIhAQ!rW>UL`3D#nczZHZ6H^$iUu|Jv;P(N_n`Hn6n9nhO1oB&ee3gvck_w6KOyxlS z3LxJhCqFq+?1-=)kpBY6SISMSC}5Cg)B#$-1LP~@CFV+|E|?@z3={wgGG-R!7nd+F z1Em-QXH3pvtWNI$GUb2{VrJlDU}RAEDEQI!W6CG>&+MOtKWlt8`I7mi@k_`5zyJS( z^g!j4vC1?4f6Vup?*QLo-iN$QJl}a9^W5NG!rjZ=4m6u_aw3z=<|-y}rp=CQPMn*) z1zefxo1^!b#q-;IW#DE48-76|{^&b}j}HV4zH4WMfQW+`**56vE8J Za*TBu>p!+2wqrmQAd>|)#|bZE1OS6^drSZT diff --git a/assets/fonts/sportspress.svg b/assets/fonts/sportspress.svg index 72d5d674..41bd499c 100755 --- a/assets/fonts/sportspress.svg +++ b/assets/fonts/sportspress.svg @@ -9,6 +9,8 @@ + + @@ -16,6 +18,7 @@ + diff --git a/assets/fonts/sportspress.ttf b/assets/fonts/sportspress.ttf index 096385a86b6f05df04216c634e5484adb6725dcb..2381393233bd95bc0261024c809461d46112b3bd 100755 GIT binary patch delta 1042 zcmYjQ&rcIk5T0qbY|Cz2+U?SomUi1Mf=b##DGdruV`|zyW{6V!Wsa zMSIYT1`>^lcrd{P4kq?sf{7On#se{?0lle7X*qbXb>7xkHhFKpZ@!tGoj3co)SZqM zCHwa9(HQ{r0>G=u%)&g`CgLB+8YgG2O#B#ovPXP}+}zZ7W(+Ug;sI=*Nbj8@hkg&s z0CtghXlizG$+xM?5swldo|zlXct0x##IFUS06eS$nl9NloA=F}_*?ug?g}r272z%wz}y}v5C#PEUW0H3hUs4F1SEn z5^Uft$otUg!&rB3B8iDGwue!*1&@(wC!QpsnPex$Sx`+lSca;}@g9~@lVt>TPzemQ zvth$6T(kEQeiLSIqsN5biS>d07-?-e{^LTz9+OLtWiNWEkr1VyN;`vPF2>dcHkge^C=E0++3KUt% zV~PQURm$wrGPKZ?wkwr<-GwPdBZ{I_mQkq`3aha3QGeECrhgW{;WiDe6H+;^*^}k9 zlTZAcNo^R0qtlYLG4TkIwBH{J`Tglg9jzNUPUL?HPSMaZqK%rLDOdlg%`yw#81+!w3o_*ML>>({O5LEow0H9T#& d(Xhv#;_n&RO#+bc1T<=I)6rd!HX==T@Go(oz~BG? delta 354 zcmcbhu|s)+LjCF5rH>dG7-bk380MrW78d|%0U&<|kmg9wsZ854-6$Q%Kfu7i+mn%+ zn8IlNY6}AczYkE}ECVROe2(!WklzC2t7PPsR7h-RDhKjc0Qn9%`N@f5M}+l&{1-sJ zQf^{J0fRK74$ul7AYUObF;_Bm!6cDlpa4*iF|#1QxP*ZjD8(Q+W8#i#(GDO>4(J|c z20jKx28EA;A6-ADOfFy)71&N?Z!2}~hAtFLy7>G&zF~hL_-87DMtMwl3!*lQR-19u|$2pvHXE)6zIRYgf zPl?xCLWp&im3f)Fr)7E-BlSNr=>i(K8%0Man%}@4JBBBI<`6 z&%X8?Ip=Fe&m2Nc{~)fW%2$cr9n||cgfo|nHE5zYjXIS>KQAoT)VH*uw}QHuW2<{q zr_}q}u%RU%jhI!~OUZd(eHD6`B=|Y*-EuTvXlQQ1oj;Snvxkb!EDFeVKk#|Hbe<=DM-fI78iZY{QlCJVt||l|0!6 zuEGK>;kA~yShxuIL|(JR0+!&4xPXH8SuO<6xcB4pg{kofL}2Q~^9=7^&xt~yFIb8U zwT(@6PzQ|y{)*ZmESeUfy|<$e=n6*qptl$LD6nY^4gKp78shzHi@3+UKOC|)4S>rcYa^l- zVlDsHw=x%dFOLD7JP9x+LhkB3T%nH>%TLQ%JUE@8*+7d|oh%;kRaMT+=2S6AwLC2# z!=s+zy91P6)H=n6fInCb>5%&dK2tW4CTka2r#NtTxaTo^g4sL_RR{fn1{$uVPX#5_ z6gYGnZ+bgP#_Qpil&sp+0`;g`rdEF0v1DAbFQqMIFZ-7}m%E}rqfyKy|8taxX`4K5 z9kKRXK3ELqZ{`K_TWLVLA$2RiOespK#Sj#TfK{>B8UHpylvi7RR>bCfC6aEn)l~9* zC!ef+qjR{0Ig}LlvNo_rn2VyfW+3xU>xX^4;6cnOM!0!}fs=Aa+GgK^GY6s?;U=V% L5PNt+x+eSv%y&Q3 delta 534 zcmca1dqYa3+~3X3KP1GTfq^%Ifr|kMcm*aG@JLM5;i~5}U|`@hSP{M4-#=L2h=GAi z1t^^W!W_!ncFDPk1wgSDAb$=BpRQf{C_S+lD8{V->F=WTp!9ImV9}xg`}qF_3uxe^5ugAY7z+e2#cS zFfe9<@PbJq#Rd7rB|xWJOnl+1afDwMsPv>Dh~xwkoa#ahf{ctj;*!#G${M;x<~ELQ z-T|RelM@)VCQo5hvNSQXEOj%nFa!}63=B++oeX>o0!$2y3@i-nz(C<;*vy#8_=72v z=`1q`iva6Vwr;i;?8O{bn|}etH~X@vux`G@xs7phCwG}~2hdhI1_1_Upc#w|3LgbO zx_(Ugr2d)xv+!q)&n90ozcha7`2Y9+f1o-Bi2P(L9(lpVybpPqc)s&I=DESWgu9o! zeR3X;^5m&J{3=|VH{FWoxB1Gz%>>qSK_cWUhykJh|7Lu{cxmzp9vL0RH$WRvfdDcW hBndRa2Z)U)OYq9`ivU?^ARYrl00V=-W-s18MgX4Kct`*M diff --git a/assets/js/admin/sportspress-admin.js b/assets/js/admin/sportspress-admin.js index 5bb58bcc..43a53c51 100644 --- a/assets/js/admin/sportspress-admin.js +++ b/assets/js/admin/sportspress-admin.js @@ -466,4 +466,53 @@ jQuery(document).ready(function($){ nonce: $("#sp-config-nonce").val() }); }); + + // Update importer post count + $(".sp-import-table").on("updatePostCount", function() { + $(".sp-post-count").text(localized_strings.displaying_posts.replace("%s", 1).replace(/%s/g, count = $(this).find("tbody tr").length)); + }); + + // Delete importer row + $(".sp-import-table").on("click", ".sp-delete-row", function() { + $self = $(this); + $self.closest("tr").css("background-color", "#fcc").fadeOut(400, function() { + $table = $self.closest(".sp-import-table"); + $(this).remove(); + $table.trigger("updatePostCount"); + }); + return false; + }); + + // Add importer row + $(".sp-import-table").on("click", ".sp-add-row", function() { + $self = $(this); + $table = $self.closest(".sp-import-table"); + if ( $self.hasClass("sp-add-first") ) { + $tr = $table.find("tbody tr:first-child"); + $row = $tr.clone(); + $row.insertBefore($tr).find("input").val(""); + } else { + $tr = $self.closest("tr"); + $row = $tr.clone(); + $tr.find("input").val(""); + $row.insertBefore($tr); + } + $table.trigger("updatePostCount"); + return false; + }); + + // Enable or disable importer inputs based on column label + $(".sp-import-table").on("change", "select", function() { + $self = $(this); + $table = $self.closest(".sp-import-table"); + index = parseInt($self.data("index")); + if ( $self.val() == 0 ) { + $table.find("tbody tr td:nth-child("+parseInt(index+1)+") input").prop("disabled", true); + } else { + $table.find("tbody tr td:nth-child("+parseInt(index+1)+") input").prop("disabled", false); + $self.closest("th").siblings().find("select").each(function() { + if ( $(this).val() == $self.val() ) $(this).val("0").trigger("change"); + }); + } + }); }); \ No newline at end of file diff --git a/includes/admin/class-sp-admin-assets.php b/includes/admin/class-sp-admin-assets.php index d6f8b77c..8943b931 100755 --- a/includes/admin/class-sp-admin-assets.php +++ b/includes/admin/class-sp-admin-assets.php @@ -108,7 +108,8 @@ class SP_Admin_Assets { 'days' => __( 'days', 'sportspress' ), 'hrs' => __( 'hrs', 'sportspress' ), 'mins' => __( 'mins', 'sportspress' ), - 'secs' => __( 'secs', 'sportspress' ) + 'secs' => __( 'secs', 'sportspress' ), + 'displaying_posts' => html_entity_decode( __( 'Displaying %s–%s of %s', 'sportspress' ) ), ); // Localize scripts diff --git a/includes/admin/importers/class-sp-event-importer.php b/includes/admin/importers/class-sp-event-importer.php index caeb6193..01d46575 100644 --- a/includes/admin/importers/class-sp-event-importer.php +++ b/includes/admin/importers/class-sp-event-importer.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.9 + * @version 1.3 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -21,351 +21,385 @@ if ( class_exists( 'WP_Importer' ) ) { */ public function __construct() { $this->import_page = 'sportspress_event_csv'; + $this->import_label = __( 'Import Events', 'sportspress' ); + $this->columns = array( + 'post_date' => __( 'Date', 'sportspress' ), + 'post_time' => __( 'Time', 'sportspress' ), + 'sp_venue' => __( 'Venue', 'sportspress' ), + 'sp_team' => __( 'Teams', 'sportspress' ), + 'sp_results' => __( 'Results', 'sportspress' ), + 'sp_outcome' => __( 'Outcome', 'sportspress' ), + 'sp_player' => __( 'Players', 'sportspress' ), + ); + $performance_labels = sp_get_var_labels( 'sp_performance' ); + if ( $performance_labels && is_array( $performance_labels ) && sizeof( $performance_labels ) ) + $this->columns = array_merge( $this->columns, $performance_labels ); } /** * import function. * * @access public - * @param mixed $file + * @param array $array + * @param array $columns * @return void */ - function import( $file ) { - global $wpdb; - + function import( $array = array(), $columns = array( 'post_title' ) ) { $this->imported = $this->skipped = 0; - if ( ! is_file($file) ): + if ( ! is_array( $array ) || ! sizeof( $array ) ): $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 = sp_get_var_labels( 'sp_result' ); - $performance_labels = sp_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 .= ' ' . get_option( 'sportspress_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; + $rows = array_chunk( $array, sizeof( $columns ) ); + + // Get event format, league, and season from post vars + $event_format = ( empty( $_POST['sp_format'] ) ? false : $_POST['sp_format'] ); + $league = ( sp_array_value( $_POST, 'sp_league', '-1' ) == '-1' ? false : $_POST['sp_league'] ); + $season = ( sp_array_value( $_POST, 'sp_season', '-1' ) == '-1' ? false : $_POST['sp_season'] ); + + // Get labels from result and performance post types + $result_labels = sp_get_var_labels( 'sp_result' ); + $performance_labels = sp_get_var_labels( 'sp_performance' ); + + foreach ( $rows as $row ): + + if ( empty( array_filter( $row ) ) ) continue; + + $meta = array(); + + foreach ( $columns as $index => $key ): + $meta[ $key ] = sp_array_value( $row, $index ); + endforeach; + + // Slice array into event, team, and player + $event = array_slice( $row, 0, 3 ); + $team = array_slice( $row, 3, 3 ); + $player = array_slice( $row, 6 ); + + // Get event details + $event = array( + sp_array_value( $meta, 'post_date' ), + sp_array_value( $meta, 'post_time' ), + sp_array_value( $meta, 'sp_venue' ), + ); + unset( $meta['post_date'] ); + unset( $meta['post_time'] ); + unset( $meta['sp_venue'] ); + + // Get team results + $team = array( + sp_array_value( $meta, 'sp_team' ), + sp_array_value( $meta, 'sp_results' ), + sp_array_value( $meta, 'sp_outcome' ), + ); + unset( $meta['sp_team'] ); + unset( $meta['sp_results'] ); + unset( $meta['sp_outcome'] ); + + // Get player performance + $player = array( + sp_array_value( $meta, 'sp_player' ), + ); + unset( $meta['sp_player' ] ); + foreach ( $performance_labels as $key => $label ): + $player[] = sp_array_value( $meta, $key, '' ); + endforeach; + + // 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; - else: + // List event columns + list( $date, $time, $venue ) = $event; - echo '

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

'; - $this->footer(); - die(); + // 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 + $this->imported ++; endif; - fclose( $handle ); + // 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 .= ' ' . get_option( 'sportspress_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; + + // Get player number + $player_number = get_post_meta( $player_id, 'sp_number', true ); + + 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 ); + + // Get player number + $player_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( 'number' => $player_number ); + + // 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; + + endforeach; + + // Add player performance to last event if available + if ( isset( $id ) && isset( $players ) && sizeof( $players ) > 0 ): + update_post_meta( $id, 'sp_players', $players ); endif; // Show Result @@ -385,16 +419,6 @@ if ( class_exists( 'WP_Importer' ) ) { do_action( 'import_end' ); } - /** - * header function. - * - * @access public - * @return void - */ - function header() { - echo '

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

'; - } - /** * greet function. * @@ -402,98 +426,87 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function greet() { - echo '
'; echo '

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

'; - echo '

' . sprintf( __( 'Events 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/events-sample.csv' ) . '

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

-

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - -
- - - -


-
- -
-
-
-

'sp_league', - 'name' => 'sp_league', - 'values' => 'slug', - 'show_option_none' => __( '-- Not set --', 'sportspress' ), - ); - if ( ! sp_dropdown_taxonomies( $args ) ): - echo '

' . __( 'None', 'sportspress' ) . '

'; - sp_taxonomy_adder( 'sp_league', 'sp_team', __( 'Add New', 'sportspress' ) ); - endif; - ?>

'sp_season', - 'name' => 'sp_season', - 'values' => 'slug', - 'show_option_none' => __( '-- Not set --', 'sportspress' ), - ); - if ( ! sp_dropdown_taxonomies( $args ) ): - echo '

' . __( 'None', 'sportspress' ) . '

'; - sp_taxonomy_adder( 'sp_season', 'sp_team', __( 'Add New', 'sportspress' ) ); - endif; - ?>
-

- -

-
- '; } + + /** + * options function. + * + * @access public + * @return void + */ + function options() { + ?> + + + + + + + + + + + + + + + + + + + +
+ + +
+
    +
  • + +
  • +
  • + +
  • +
  • + +
  • +
+
+

+
+
    +
  • +
  • +
    +
+

'sp_league', + 'name' => 'sp_league', + 'values' => 'slug', + 'show_option_none' => __( '-- Not set --', 'sportspress' ), + ); + if ( ! sp_dropdown_taxonomies( $args ) ): + echo '

' . __( 'None', 'sportspress' ) . '

'; + sp_taxonomy_adder( 'sp_league', 'sp_team', __( 'Add New', 'sportspress' ) ); + endif; + ?>

'sp_season', + 'name' => 'sp_season', + 'values' => 'slug', + 'show_option_none' => __( '-- Not set --', 'sportspress' ), + ); + if ( ! sp_dropdown_taxonomies( $args ) ): + echo '

' . __( 'None', 'sportspress' ) . '

'; + sp_taxonomy_adder( 'sp_season', 'sp_team', __( 'Add New', 'sportspress' ) ); + endif; + ?>
+ plugin_url() . '/assets/js/admin/sportspress-admin.js', array( 'jquery', 'chosen', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-tiptip', 'jquery-caret', 'jquery-countdown' ), SP_VERSION, true ); + } /** * Registered callback function for the WordPress Importer @@ -58,13 +74,125 @@ if ( class_exists( 'WP_Importer' ) ) { @ob_flush(); @flush(); - $this->import( $file ); + $this->table( $file ); + endif; + break; + case 2: + check_admin_referer( 'import-upload' ); + if ( isset( $_POST['sp_import'] ) ): + $this->import( $_POST['sp_import'], array_values( array_filter( sp_array_value( $_POST, 'sp_columns', array( 'post_title' ) ) ) ) ); endif; break; endswitch; $this->footer(); } + /** + * dropdown function. + * + * @access public + * @param mixed $file + * @return void + */ + function dropdown( $selected ) { + ?> + + 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 ) >= 1 ): + $action = 'admin.php?import=' . $this->import_page . '&step=2'; + ?> +
+ options(); ?> + + + + columns as $key => $label ): ?> + + + + + + + delimiter ) ) !== FALSE ): ?> + + columns as $key => $label ): $value = sp_array_value( $row, $index ); ?> + + + + + imported++; endwhile; ?> + + columns as $key => $label ): ?> + + + + + +
+ dropdown( $key ); ?> + + +
+ + + + +
+ + + +
+

+ imported+1, $this->imported+1 ); ?> +

+

+ +

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

'; + $this->footer(); + die(); + + endif; + + fclose( $handle ); + endif; + } + /** * format_data_from_csv function. * @@ -122,7 +250,7 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function header() { - echo '

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

'; + echo '

' . $this->import_label . '

'; } /** @@ -135,6 +263,15 @@ if ( class_exists( 'WP_Importer' ) ) { echo '
'; } + /** + * options function. + * + * @access public + * @return void + */ + function options() { + } + /** * Added to http_request_timeout filter to force timeout at 60 seconds during import * @param int $val diff --git a/includes/admin/importers/class-sp-player-importer.php b/includes/admin/importers/class-sp-player-importer.php index eefeb1cf..fdbc5868 100644 --- a/includes/admin/importers/class-sp-player-importer.php +++ b/includes/admin/importers/class-sp-player-importer.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.9 + * @version 1.3 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -21,119 +21,114 @@ if ( class_exists( 'WP_Importer' ) ) { */ public function __construct() { $this->import_page = 'sportspress_player_csv'; + $this->import_label = __( 'Import Players', 'sportspress' ); + $this->columns = array( + 'sp_number' => __( 'Number', 'sportspress' ), + 'post_title' => __( 'Name', 'sportspress' ), + 'sp_position' => __( 'Positions', 'sportspress' ), + 'sp_team' => __( 'Teams', 'sportspress' ), + 'sp_league' => __( 'Leagues', 'sportspress' ), + 'sp_season' => __( 'Seasons', 'sportspress' ), + 'sp_nationality' => __( 'Nationality', 'sportspress' ), + ); + parent::__construct(); } /** * import function. * * @access public - * @param mixed $file + * @param array $array + * @param array $columns * @return void */ - function import( $file ) { - global $wpdb; - + function import( $array = array(), $columns = array( 'post_title' ) ) { $this->imported = $this->skipped = 0; - if ( ! is_file($file) ): + if ( ! is_array( $array ) || ! sizeof( $array ) ): $this->footer(); die(); endif; - ini_set( 'auto_detect_line_endings', '1' ); + $rows = array_chunk( $array, sizeof( $columns ) ); - if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ): + foreach ( $rows as $row ): - $header = fgetcsv( $handle, 0, $this->delimiter ); + if ( empty( array_filter( $row ) ) ) continue; - if ( sizeof( $header ) == 7 ): + $meta = array(); - $loop = 0; + foreach ( $columns as $index => $key ): + $meta[ $key ] = sp_array_value( $row, $index ); + endforeach; - 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 '

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

'; - $this->footer(); - die(); + $name = sp_array_value( $meta, 'post_title' ); + if ( ! $name ): + $this->skipped++; + continue; endif; - fclose( $handle ); - 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', sp_array_value( $meta, 'sp_number' ) ); + + // Update positions + $positions = explode( '|', sp_array_value( $meta, 'sp_position' ) ); + wp_set_object_terms( $id, $positions, 'sp_position', false ); + + // Update leagues + $leagues = explode( '|', sp_array_value( $meta, 'sp_league' ) ); + wp_set_object_terms( $id, $leagues, 'sp_league', false ); + + // Update seasons + $seasons = explode( '|', sp_array_value( $meta, 'sp_season' ) ); + wp_set_object_terms( $id, $seasons, 'sp_season', false ); + + // Update teams + $teams = (array)explode( '|', sp_array_value( $meta, 'sp_team' ) ); + $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 + $nationality = trim( strtoupper( sp_array_value( $meta, 'sp_nationality' ) ) ); + if ( $nationality == '*' ) $nationality = ''; + update_post_meta( $id, 'sp_nationality', $nationality ); + + $this->imported++; + + endforeach; // Show Result echo '

@@ -169,57 +164,10 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function greet() { - echo '

'; echo '

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

'; - echo '

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

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

-

-
- - - - - - - - - - - - - - - -
- - - - - - -
- - - -

-

- -

-
- '; } } diff --git a/includes/admin/importers/class-sp-staff-importer.php b/includes/admin/importers/class-sp-staff-importer.php index e90f8d18..433511b3 100644 --- a/includes/admin/importers/class-sp-staff-importer.php +++ b/includes/admin/importers/class-sp-staff-importer.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.9 + * @version 1.3 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -21,112 +21,110 @@ if ( class_exists( 'WP_Importer' ) ) { */ public function __construct() { $this->import_page = 'sportspress_staff_csv'; + $this->import_label = __( 'Import Staff', 'sportspress' ); + $this->columns = array( + 'post_title' => __( 'Name', 'sportspress' ), + 'sp_role' => __( 'Roles', 'sportspress' ), + 'sp_team' => __( 'Teams', 'sportspress' ), + 'sp_league' => __( 'Leagues', 'sportspress' ), + 'sp_season' => __( 'Seasons', 'sportspress' ), + 'sp_nationality' => __( 'Nationality', 'sportspress' ), + ); + parent::__construct(); } /** * import function. * * @access public - * @param mixed $file + * @param array $array + * @param array $columns * @return void */ - function import( $file ) { - global $wpdb; - + function import( $array = array(), $columns = array( 'post_title' ) ) { $this->imported = $this->skipped = 0; - if ( ! is_file($file) ): + if ( ! is_array( $array ) || ! sizeof( $array ) ): $this->footer(); die(); endif; - ini_set( 'auto_detect_line_endings', '1' ); + $rows = array_chunk( $array, sizeof( $columns ) ); - if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ): + foreach ( $rows as $row ): - $header = fgetcsv( $handle, 0, $this->delimiter ); + if ( empty( array_filter( $row ) ) ) continue; - if ( sizeof( $header ) == 5 ): + $meta = array(); - $loop = 0; + foreach ( $columns as $index => $key ): + $meta[ $key ] = sp_array_value( $row, $index ); + endforeach; - while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): - - list( $name, $teams, $leagues, $seasons, $nationality ) = $row; - - $nationality = trim( strtoupper( $nationality ) ); - - if ( $nationality == '*' ) - $nationality = ''; - - if ( ! $name ): - $this->skipped++; - continue; - endif; - - $args = array( 'post_type' => 'sp_staff', '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 ); - - // 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 staff - 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 '

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

'; - $this->footer(); - die(); + $name = sp_array_value( $meta, 'post_title' ); + if ( ! $name ): + $this->skipped++; + continue; endif; - fclose( $handle ); - endif; + $args = array( 'post_type' => 'sp_staff', 'post_status' => 'publish', 'post_title' => $name ); + + $id = wp_insert_post( $args ); + + // Flag as import + update_post_meta( $id, '_sp_import', 1 ); + + // Update roles + $roles = explode( '|', sp_array_value( $meta, 'sp_role' ) ); + wp_set_object_terms( $id, $roles, 'sp_role', false ); + + // Update leagues + $leagues = explode( '|', sp_array_value( $meta, 'sp_league' ) ); + wp_set_object_terms( $id, $leagues, 'sp_league', false ); + + // Update seasons + $seasons = explode( '|', sp_array_value( $meta, 'sp_season' ) ); + wp_set_object_terms( $id, $seasons, 'sp_season', false ); + + // Update teams + $teams = (array)explode( '|', sp_array_value( $meta, 'sp_team' ) ); + $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 staff + 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 + $nationality = trim( strtoupper( sp_array_value( $meta, 'sp_nationality' ) ) ); + if ( $nationality == '*' ) $nationality = ''; + update_post_meta( $id, 'sp_nationality', $nationality ); + + $this->imported++; + + endforeach; // Show Result echo '

@@ -162,57 +160,10 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function greet() { - echo '

'; echo '

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

'; - - echo '

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

'; - - $action = 'admin.php?import=sportspress_staff_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'] ) ) : - ?>

-

-
- - - - - - - - - - - - - - - -
- - - - - - -
- - - -

-

- -

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

'; + wp_import_upload_form( 'admin.php?import=sportspress_staff_csv&step=1' ); echo '
'; } } diff --git a/includes/admin/importers/class-sp-team-importer.php b/includes/admin/importers/class-sp-team-importer.php index e5511f74..08d3a4fa 100644 --- a/includes/admin/importers/class-sp-team-importer.php +++ b/includes/admin/importers/class-sp-team-importer.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.9 + * @version 1.3 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -21,76 +21,71 @@ if ( class_exists( 'WP_Importer' ) ) { */ public function __construct() { $this->import_page = 'sportspress_team_csv'; + $this->import_label = __( 'Import Teams', 'sportspress' ); + $this->columns = array( + 'post_title' => __( 'Name', 'sportspress' ), + 'sp_league' => __( 'Leagues', 'sportspress' ), + 'sp_season' => __( 'Seasons', 'sportspress' ), + 'sp_url' => __( 'Site URL', 'sportspress' ), + 'sp_abbreviation' => __( 'Abbreviation', 'sportspress' ), + 'sp_venue' => __( 'Home', 'sportspress' ), + ); + parent::__construct(); } /** * import function. * * @access public - * @param mixed $file + * @param array $array + * @param array $columns * @return void */ - function import( $file ) { - global $wpdb; - + function import( $array = array(), $columns = array( 'post_title' ) ) { $this->imported = $this->skipped = 0; - if ( ! is_file($file) ): + if ( ! is_array( $array ) || ! sizeof( $array ) ): $this->footer(); die(); endif; - ini_set( 'auto_detect_line_endings', '1' ); + $rows = array_chunk( $array, sizeof( $columns ) ); - if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ): + foreach ( $rows as $row ): - $header = fgetcsv( $handle, 0, $this->delimiter ); + if ( empty( array_filter( $row ) ) ) continue; - if ( sizeof( $header ) == 3 ): + $meta = array(); - $loop = 0; + foreach ( $columns as $index => $key ): + $meta[ $key ] = sp_array_value( $row, $index ); + endforeach; - 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' ) . '
'; - _e( 'The CSV is invalid.', 'sportspress' ) . '

'; - $this->footer(); - die(); + $name = sp_array_value( $meta, 'post_title' ); + if ( ! $name ): + $this->skipped++; + continue; endif; - fclose( $handle ); - 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( '|', sp_array_value( $meta, 'sp_league' ) ); + wp_set_object_terms( $id, $leagues, 'sp_league', false ); + + // Update seasons + $seasons = explode( '|', sp_array_value( $meta, 'sp_season' ) ); + wp_set_object_terms( $id, $seasons, 'sp_season', false ); + + $this->imported++; + + endforeach; // Show Result echo '

@@ -109,16 +104,6 @@ if ( class_exists( 'WP_Importer' ) ) { do_action( 'import_end' ); } - /** - * header function. - * - * @access public - * @return void - */ - function header() { - echo '

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

'; - } - /** * greet function. * @@ -126,57 +111,10 @@ if ( class_exists( 'WP_Importer' ) ) { * @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'] ) ) : - ?>

-

-
- - - - - - - - - - - - - - - -
- - - - - - -
- - - -

-

- -

-
- '; } } diff --git a/includes/admin/sp-admin-functions.php b/includes/admin/sp-admin-functions.php index b816f713..d6cc3b20 100755 --- a/includes/admin/sp-admin-functions.php +++ b/includes/admin/sp-admin-functions.php @@ -17,6 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly */ function sp_get_screen_ids() { return apply_filters( 'sportspress_screen_ids', array( + 'admin', 'widgets', 'dashboard', 'dashboard_page_sp-about', diff --git a/includes/class-sp-wpml.php b/includes/class-sp-wpml.php index f4279f32..2915651a 100644 --- a/includes/class-sp-wpml.php +++ b/includes/class-sp-wpml.php @@ -50,7 +50,7 @@ class SP_WPML { // Get translated post ID $translated_id = icl_object_id( $id, 'any', false, ICL_LANGUAGE_CODE ); - if ( $translated_id && $translated_id != $id ): + if ( $translated_id && $translated_id != $id && get_the_ID() != $translated_id ): return get_permalink( $translated_id, $leavename ); endif; endif; @@ -59,7 +59,7 @@ class SP_WPML { } public static function can_localize( $post, $id = null ) { - return function_exists( 'icl_object_id' ) && is_sp_post_type( get_post_type( $post ) ) && $id != get_the_ID(); + return function_exists( 'icl_object_id' ) && is_sp_post_type( get_post_type( $post ) ); } }