Skip to content

Commit

Permalink
Add CSV download link widget
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg committed Feb 26, 2024
1 parent 60ee45d commit 8b5d14c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
124 changes: 124 additions & 0 deletions includes/widgets/class-gravityview-widget-csv-link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

use GV\Template_Context;
use GV\View;
use GV\Widget;

/**
* Widget to add a CSV download link.
*
* @since $ver$
*/
final class GravityView_Widget_Csv_Link extends Widget {
/**
* @inheritDoc
* @since $ver$
*/
public $icon = 'dashicons-database-export';

/**
* @inheritDoc
* @since $ver$
*/
protected $show_on_single = false;

/**
* Returns the settings for this widget.
*
* @since $ver$
* @return array[] The settings.
*/
private static function settings(): array {
return [
'type' => [
'label' => __( 'Type', 'gk-gravityview' ),
'type' => 'radio',
'choices' => [
'csv' => 'CSV',
'tsv' => 'TSV',
],
],
'title' => [
'type' => 'text',
'class' => 'widefat',
'label' => __( 'Label', 'gk-gravityview' ),
'desc' => __( 'Enter the label of the link.', 'gk-gravityview' ),
'value' => '',
'merge_tags' => false,
'required' => true,
],
'in_paragraph' => [
'type' => 'checkbox',
'label' => __( 'Wrap link in paragraph', 'gk-gravityview' ),
'desc' => __( 'Will wrap the link in a <code>&lt;p&gt;</code> tag.', 'gk-gravityview' ),
],
];
}

/**
* Returns the default settings.
*
* @since $ver$
*/
private static function defaults(): array {
return [
'label' => __( 'Download CSV', 'gk-gravityview' ),
'type' => 'csv',
];
}

/**
* @inheritDoc
* @since $ver$
*/
public function __construct() {
$this->widget_description = 'Insert a link to a CSV / TSV download.';

parent::__construct( 'CSV download Link', 'csv_link', self::defaults(), self::settings() );
}

/**
* @inheritDoc
* @since $ver$
*/
public function render_frontend( $widget_args, $content = '', $context = '' ): void {
if (
! $context instanceof Template_Context
|| ! $this->pre_render_frontend( $context )
) {
return;
}

$view = $context->view;

if (
! $view instanceof View
|| false === (bool) $view->settings->get( 'csv_enable', false )
) {
return;
}

$type = rgar( $widget_args, 'type', 'csv' );
$label = rgar( $widget_args, 'title', 'Download CSV' );
$in_paragraph = (bool) rgar( $widget_args, 'in_paragraph', false );

$permalink = get_permalink( $view->id );

if ( ! $permalink ) {
return;
}

$link = sprintf(
'<a href="%s/%s">%s</a>',
esc_attr( rtrim( $permalink, '/' ) ),
esc_attr( $type ),
esc_attr( $label )
);

$in_paragraph
? printf( '<p>%s</p>', $link )
: print( $link );
}
}

new GravityView_Widget_Csv_Link();
1 change: 1 addition & 0 deletions includes/widgets/register-gravityview-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function gravityview_register_gravityview_widgets() {

include_once GRAVITYVIEW_DIR . 'includes/widgets/search-widget/class-search-widget.php';
include_once GRAVITYVIEW_DIR . 'includes/widgets/class-gravityview-widget-custom-content.php';
include_once GRAVITYVIEW_DIR . 'includes/widgets/class-gravityview-widget-csv-link.php';
include_once GRAVITYVIEW_DIR . 'includes/widgets/class-gravityview-widget-gravityforms.php';
include_once GRAVITYVIEW_DIR . 'includes/widgets/class-gravityview-widget-page-size.php';
include_once GRAVITYVIEW_DIR . 'includes/widgets/class-gravityview-widget-pagination-info.php';
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Beautifully display your Gravity Forms entries. Learn more on [gravitykit.com](h

== Changelog ==

= develop =

* Added a widget to quickly download as a CSV.

= 2.20 on February 22, 2024 =

This release introduces new settings for better control over View caching, adds support for the Advanced Post Creation Add-On when editing entries, fixes a fatal error when exporting entries to CSV, and updates internal components for better performance and compatibility.
Expand Down

0 comments on commit 8b5d14c

Please sign in to comment.