From 0e42a12a33b9bb193fa7851628edf018bd922362 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 18 Jun 2022 11:38:26 +0200 Subject: Add phpcs config and do automatic fixups. --- includes/admin/views/giglog_import_gigs.php | 96 ++++++++++++++--------------- 1 file changed, 45 insertions(+), 51 deletions(-) (limited to 'includes/admin/views/giglog_import_gigs.php') diff --git a/includes/admin/views/giglog_import_gigs.php b/includes/admin/views/giglog_import_gigs.php index c6b6a56..505e4bd 100644 --- a/includes/admin/views/giglog_import_gigs.php +++ b/includes/admin/views/giglog_import_gigs.php @@ -5,7 +5,7 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { +if ( ! class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { require_once __DIR__ . '/../../concert.php'; require_once __DIR__ . '/../../venue.php'; @@ -15,7 +15,7 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) {

Import gigs

Import gig data from a tab separated data file.

-
+ @@ -26,13 +26,12 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { } static function submit_form(): void { - if ('POST' === $_SERVER['REQUEST_METHOD'] && current_user_can('upload_files') && !empty($_FILES['giglog_import_file']['tmp_name'])) { - if (isset($_POST['giglog_import_nonce']) && wp_verify_nonce($_POST['giglog_import_nonce'], plugin_basename( __FILE__ )) ) { - GiglogAdmin_ImportGigsPage::process_upload($_FILES['giglog_import_file']); - } - else { - header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden"); - wp_die('CSRF validation failed.', 403); + if ( 'POST' === $_SERVER['REQUEST_METHOD'] && current_user_can( 'upload_files' ) && ! empty( $_FILES['giglog_import_file']['tmp_name'] ) ) { + if ( isset( $_POST['giglog_import_nonce'] ) && wp_verify_nonce( $_POST['giglog_import_nonce'], plugin_basename( __FILE__ ) ) ) { + self::process_upload( $_FILES['giglog_import_file'] ); + } else { + header( "{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden" ); + wp_die( 'CSRF validation failed.', 403 ); } } } @@ -55,85 +54,80 @@ if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) { * * @param array $file */ - static function process_upload(array $file): void - { - $fo = new SplFileObject($file['tmp_name']); - $importerrors = []; - $rid=0; + static function process_upload( array $file ): void { + $fo = new SplFileObject( $file['tmp_name'] ); + $importerrors = array(); + $rid = 0; - foreach ($fo as $line) { + foreach ( $fo as $line ) { $rid++; $line = trim( $line ); - if ( !empty($line) ) { + if ( ! empty( $line ) ) { try { self::process_line( $line ); - } - catch (Exception $e) { + } catch ( Exception $e ) { $importerrors[] = "Error importing line {$rid}: {$e->getMessage()}"; } } } - if (!empty($importerrors)) { - echo implode("
", $importerrors); - } - else { - echo ('All rows imported ok'); + if ( ! empty( $importerrors ) ) { + echo implode( '
', $importerrors ); + } else { + echo ( 'All rows imported ok' ); } } - static function process_line( string $line ) : void - { - $resultArray = explode("\t", $line); + static function process_line( string $line ) : void { + $resultArray = explode( "\t", $line ); // unsure if this is needed, considering we are also checking if // each individual important field is missing. But if they are not // replaced by tabs, then everything gets shifted so probably best // to check if a value is empty and NOT replaced by tab - if (count($resultArray) < 6) { - throw new Exception("missing a field"); + if ( count( $resultArray ) < 6 ) { + throw new Exception( 'missing a field' ); } - if (! preg_match("/\d{4}\-\d{2}-\d{2}/",$resultArray[3])) { - throw new Exception("invalid date: " . esc_html($resultArray[3])); + if ( ! preg_match( '/\d{4}\-\d{2}-\d{2}/', $resultArray[3] ) ) { + throw new Exception( 'invalid date: ' . esc_html( $resultArray[3] ) ); } - if (empty(trim($resultArray[0]))) { - throw new Exception("missing concert name"); + if ( empty( trim( $resultArray[0] ) ) ) { + throw new Exception( 'missing concert name' ); } - if (empty(trim($resultArray[1]))) { - throw new Exception("missing venue"); + if ( empty( trim( $resultArray[1] ) ) ) { + throw new Exception( 'missing venue' ); } - if (empty(trim($resultArray[2]))) { - throw new Exception("missing city"); + if ( empty( trim( $resultArray[2] ) ) ) { + throw new Exception( 'missing city' ); } - $condate = date('Y-m-d', strtotime($resultArray[3])); + $condate = date( 'Y-m-d', strtotime( $resultArray[3] ) ); - if ($condateid(), $condate, $ticketlink, $eventlink); + GiglogAdmin_Concert::create( $cname, $venue->id(), $condate, $ticketlink, $eventlink ); } } } -- cgit v1.2.3