file_path = $this->get_file_path( $filename ); $this->file_url = $this->get_file_url( $filename ); // Create the .csv. $this->create( $appointments ); if ( ! file_exists( $this->file_path ) ) { return new WP_Error( 'ssa_export_csv_file_not_found', __( 'Error creating .csv file.', 'simply-schedule-appointments' ) ); } return array( 'file_path' => $this->file_path, 'file_url' => $this->file_url, ); } /** * Get file path. * * @param string $filename Filename. * * @return string */ protected function get_file_path( $filename ) { $path = SSA_Filesystem::get_uploads_dir_path(); if ( empty( $path ) ) { return false; } $path .= '/csv'; if ( ! wp_mkdir_p( $path ) ) { return false; } if ( ! file_exists( $path . '/index.html' ) ) { // @codingStandardsIgnoreStart $handle = @fopen( $path . '/index.html', 'w' ); @fwrite( $handle, '' ); @fclose( $handle ); // @codingStandardsIgnoreEnd } return $path . '/' . sanitize_title( $filename ) . '.csv'; } /** * Get file url * * @param string $filename Filename. * * @return string */ protected function get_file_url( $filename ) { $url = SSA_Filesystem::get_uploads_dir_url(); if ( empty( $url ) ) { return false; } $url .= '/csv'; return $url . '/' . sanitize_title( $filename ) . '.csv'; } /** * Create the .ics file. * * @param array $appointments The appointments array. * * @return void */ protected function create( $appointments ) { // @codingStandardsIgnoreStart $handle = @fopen( $this->file_path, 'w' ); fputcsv( $handle, array_keys( $appointments[0] ) ); foreach ( $appointments as $appointment ) { fputcsv( $handle, $appointment ); } @fclose( $handle ); // @codingStandardsIgnoreEnd } }