WordPress.org

Ready to get started?Download WordPress

Forums

CSV to SortTable
[resolved] url encoding (9 posts)

  1. 2020media
    Member
    Posted 1 year ago #

    I have a rather complete url that returns a csv like structure.

    in it's basic form its fine
    http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=52762

    but in a more complex example, it does not work

    http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=52762&ReturnList=LineName,DestinationText,EstimatedTime

    Is it feasible to add urlencoding (if that's the right term) to get your script to blindly accept the url as a csv?
    Or is this straying too far from the purpose of this plugin do you think?

    http://wordpress.org/extend/plugins/csv-to-sorttable/

  2. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    I'll have to look into this a bit closer, but at first glance I think part of the problem is that your URL results in three columns in the first row, but four columns in every other row.

  3. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    Using the URL you provided above, I'm getting this in the table data array:

    array (size=3)
      0 =>
        array (size=3)
          0 => string '[4' (length=2)
          1 => string '1.0' (length=3)
          2 => string '1371517737617]' (length=14)
      1 =>
        array (size=4)
          0 => string '[1' (length=2)
          1 => string 'Bullen Street' (length=13)
          2 => string 'N44' (length=3)
          3 => string '1371518096000]' (length=14)
      2 =>
        array (size=4)
          0 => string '[1' (length=2)
          1 => string 'Bullen Street' (length=13)
          2 => string '344' (length=3)
          3 => string '1371519166000]' (length=14)

    Not sure if that is the expected result, but if it is you can always clean it up using the csv_to_sorttable_data_array filter.

  4. 2020media
    Member
    Posted 1 year ago #

    Hi

    The first url I gave does indeed produc those results but I am using your filter to discard the first row to cope with the poor data.

    However it is the second url I am posting about. If you just click this url you'll see it produces a csv-like structure pretty much idenditcal to the first url.
    However the url itself is more complex, and I think this is the problem. Possibly the commas are confusing it.

    My first thought was about using rawurlencode or urlencode to make sure WordPress does not get confused.

  5. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    Yeah, sorry but this is a bit beyond the scope of the support I can provide.

    I can tell you that the plugin uses wp_remote_fopen() to open the source file...hopefully that will get you pointed in the right direction.

    If you find a solution, please post it here and I will be sure to document it.

  6. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    Marking this thread as resolved. If you are able to modify the plugin to support this feature and would like to contribute to this open source project, please submit a pull request at: https://github.com/sscovil/csv-to-sorttable

  7. 2020media
    Member
    Posted 1 year ago #

    Hi
    Yes I agree it's pushing the support a bit.

    I have got it working by changing a line in the plugin (complete bodge) to hardcode the url I want.

    // Get contents of .CSV file as string.
    // $file = wp_remote_fopen( $src );
    $file = wp_remote_fopen("my_long_complex_url");

    for my purposes this does the job!

  8. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    Ah, interesting. So it's probably the shortcode attribute parser that is mucking things up.

    In that case, you could use something like this in your theme functions.php instead of modifying the plugin:

    if ( class_exists( 'CSV_to_SortTable' ) ) {
        /**
         * Custom CSV Shortcode
         *
         * Use shortcode [customcsv] with all the same attributes as [csv] except 'src', which is hard-coded.
         *
         * @uses function shortcode_atts
         * @uses function wp_remote_fopen
         *
         * @param  array  $atts User-defined shortcode attributes.
         * @return string       HTML for rendered table, or blank string if no data.
         */
        function custom_csv_shortcode( $atts ) {
            $plugin = CSV_to_SortTable::instance();
    
            // Default values for shortcode attributes.
            $defaults = array(
                'unsortable' => '',     // Comma-separated list of column numbers that should be unsortable.
                'number'     => '',     // Comma-separated list of column numbers that should be sorted as numeric.
                'date'       => '',     // Comma-separated list of column numbers that should be sorted as date.
                'group'      => 0,      // Column to check for identical values and apply matching class to rows.
                'disable'    => '',     // Available options: css, icons, images, all
            );
            $atts = shortcode_atts( $defaults, $atts );
    
            // Create an array of enabled features based on 'disable' attribute.
            $enabled = $plugin->enabled_features( $atts['disable'] );
    
            // Enqueue plugin JavaScript & CSS only on pages where shortcode is used.
            $plugin->load_js_css( $enabled );
    
            // Use hard-coded CSV file source.
            $src = 'http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=52762&ReturnList=LineName,DestinationText,EstimatedTime';
    
            // Get contents of .CSV file as string.
            $file = wp_remote_fopen( $src );
    
            // Parse CSV string into a multidimensional array.
            $rows = $plugin->parse_csv( $file );
    
            // Render table and return HTML string.
            return $plugin->render_table( $rows, $atts );
        }
        add_shortcode( 'customcsv', 'custom_csv_shortcode' );
    }
  9. 2020media
    Member
    Posted 1 year ago #

    That's a nice solution, I'll give it a go.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.