Skip to content

Instantly share code, notes, and snippets.

@swooningfish
Created August 16, 2018 11:26
Show Gist options
  • Save swooningfish/1f008758b553a3eb8633e59778351a8c to your computer and use it in GitHub Desktop.
Save swooningfish/1f008758b553a3eb8633e59778351a8c to your computer and use it in GitHub Desktop.

Revisions

  1. @caspersch caspersch created this gist Oct 19, 2016.
    136 changes: 136 additions & 0 deletions cs-repeater-download.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    <?php
    /**
    * Plugin Name: CS Repeater Download
    * Plugin URI: https://casperschultz.dk
    * Description: Plugin for downloading repeater field data.
    * Author: Casper Schultz
    * Author URI: https://casperschultz.dk
    * Text Domain: cs-repeater-download
    * Domain Path: /lang/
    * Version: 1.0
    */

    namespace CasperSchultz\ACF\RepeaterDownload;

    /**
    * Class CS_Repeater_Download
    * @package CasperSchultz\ACF\RepeaterDownload
    */
    class CS_Repeater_Download {

    protected $repeater;

    public function __construct() {
    // @TODO add the repeater name that are to be downloadable.
    $this->repeater = 'team_attendants'; // Name of the repeater field that are to be downloadable.
    // @TODO add the post type that the download button should be displayed at.
    $this->post_types = 'hold'; // The post type that the download metabox is to be displayed at.
    $this->init();
    }

    public function init() {

    // Register meta box.
    add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );

    // Add a new custom endpoint for the download rule.
    add_action( 'init', array( $this, 'add_rewrite_rule' ) );
    add_filter( 'query_vars', array( $this, 'add_query_vars' ) );

    // Generate the file.
    add_action( 'parse_request', array( $this, 'parse_request' ) );
    }

    /**
    * Handle the custom endpoint we added.
    */
    public function parse_request( $wp ) {

    // If the key does not exist, stop execution.
    if ( ! array_key_exists( 'cs-repeater-page-id', $wp->query_vars ) ) {
    return;
    }

    $page = absint( $wp->query_vars['cs-repeater-page-id'] );
    $output = '';

    if( have_rows( $this->repeater, $page ) ) {

    $rows = get_field( $this->repeater, $page );
    $field_count = count( $rows[0] );

    foreach ( $rows as $fields ) {

    $count = 1;

    foreach ( $fields as $value ) {
    $output .= $value;
    $count++;
    if ( $count < $field_count ) {
    $output .= ',';
    }
    }

    $output .= "\n";
    }
    }

    // Send the downloadable file.
    header("Content-type: text/plain");
    header("Content-Disposition: attachment; filename=attendants.txt");

    // Print the content.
    print $output;
    exit();
    }

    /**
    * Add the needed query args.
    */
    public function add_query_vars( $query_vars ) {
    $query_vars[] = 'cs-repeater-page-id';
    return $query_vars;
    }

    /**
    * Adds a rewrite rule for our downloadable file.
    */
    public function add_rewrite_rule() {
    add_rewrite_rule( '^cs-repeater-download/([0-9]+)/?', 'index.php?cs-repeater-page-id=$matches[1]', 'top' );
    }

    /**
    * Register meta box(es).
    */
    public function register_meta_boxes() {
    add_meta_box( 'meta-box-id', __( 'Download teams', 'cs-repeater-download' ), array( $this, 'meta_box_callback' ), $this->post_types, 'side' );
    }

    /**
    * Meta box display callback.
    *
    * @param \WP_Post $post Current post object.
    */
    public function meta_box_callback( $post ) {
    ?>
    <p><?php _e( 'Download the teams as a comma seperated file (csv)', 'cs-repeater-download' ) ?></p>
    <a href="<?php get_site_url() ?>/cs-repeater-download/<?php echo $post->ID ?>" class="button button-primary button-large"><?php _e( 'Download', 'cs-repeater-download' ) ?></a>
    <?php
    }
    }

    /**
    * Run the plugin.
    */
    function init() {
    new CS_Repeater_Download();
    }
    init();

    /**
    * Need to flush rewrite rules on plugin activation.
    */
    register_activation_hook( __FILE__, function() {
    add_rewrite_rule( '^cs-repeater-download/([0-9]+)/?', 'index.php?cs-repeater-page-id=$matches[1]', 'top' );
    flush_rewrite_rules();
    } );