Skip to content

Commit

Permalink
Add classes, allow field name headers, strip notice.
Browse files Browse the repository at this point in the history
  • Loading branch information
doekenorg committed Mar 4, 2024
1 parent 9275f01 commit 962bf19
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
32 changes: 17 additions & 15 deletions future/includes/rest/class-gv-rest-views-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace GV\REST;

use GravityView_Widget_Export_Link;
use WP_REST_Request;

/** If this file is called directly, abort. */
Expand Down Expand Up @@ -176,18 +177,6 @@ function ( $field ) use ( $allowed_field_ids ) {
$field_id = $field->ID;
$index = null;

if ( ! $this->headers_done ) {
$label = $field->get_label( $view, $source, $entry );
if ( ! $label ) {
$label = $field_id;
}

$this->headers[] = [
'field_id' => $field_id,
'label' => $label,
];
}

if ( ! isset( $used_ids[ $field_id ] ) ) {
$used_ids[ $field_id ] = 0;
} else {
Expand All @@ -212,6 +201,18 @@ function ( $field ) use ( $allowed_field_ids ) {
*/
$field_id = apply_filters( 'gravityview/api/field/key', $field_id, $view, $entry, $request, $context );

if ( ! $this->headers_done ) {
$label = $field->get_label( $view, $source, $entry );
if ( ! $label ) {
$label = $field_id;
}

$this->headers[] = [
'field_id' => $field_id,
'label' => $label,
];
}

if ( ! $class && in_array( $field->ID, array( 'custom' ) ) ) {
/**
* Custom fields (and perhaps some others) will require rendering as they don't
Expand Down Expand Up @@ -337,14 +338,15 @@ function ( $context ) use ( &$count, &$total ) {
$this->headers_done = false;
$this->headers = [];

// If not "tsv" then use comma
// If not "tsv" then use comma.
$delimiter = ( 'tsv' === $format ) ? "\t" : ',';

foreach ( $entries->all() as $entry ) {
$entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory', '\GV\Field_CSV_Template' );
$label = $request->get_param( 'use_labels' ) ? 'label' : 'field_id';

if ( ! $this->headers_done ) {
$this->headers_done = false !== fputcsv( $csv_or_tsv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_column( $this->headers, 'label' ) ), $delimiter );
$this->headers_done = false !== fputcsv( $csv_or_tsv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), array_column( $this->headers, $label ) ), $delimiter );
}

fputcsv( $csv_or_tsv, array_map( array( '\GV\Utils', 'strip_excel_formulas' ), $entry ), $delimiter );
Expand Down Expand Up @@ -585,7 +587,7 @@ public function get_sub_items_permissions_check( $request ) {
if (
'1' === $view->settings->get( 'csv_enable' )
&& in_array( $format, [ 'csv', 'tsv' ], true )
&& wp_verify_nonce( $nonce, sprintf( '%s.%d', 'csv_link', $view->ID ) )
&& wp_verify_nonce( $nonce, sprintf( '%s.%d', GravityView_Widget_Export_Link::WIDGET_ID, $view->ID ) )
) {
// All results.
$request->set_param( 'limit', 0 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@
use GV\Widget;

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

/**
* @inheritDoc
* The widget ID.
*
* @since $ver$
*/
public const WIDGET_ID = 'export_link_widget';

/**
* A short description.
*
* @since $ver$
* @var string
*/
protected $show_on_single = false;
private $widget_short;

/**
* Returns the settings for this widget.
Expand All @@ -29,6 +38,8 @@ final class GravityView_Widget_Csv_Link extends Widget {
* @return array[] The settings.
*/
private static function settings(): array {
$defaults = self::defaults();

return [
'type' => [
'label' => __( 'Type', 'gk-gravityview' ),
Expand All @@ -37,21 +48,33 @@ private static function settings(): array {
'csv' => 'CSV',
'tsv' => 'TSV',
],
'value' => $defaults['type'],
],
'title' => [
'type' => 'text',
'class' => 'widefat',
'label' => __( 'Label', 'gk-gravityview' ),
'desc' => __( 'Enter the label of the link.', 'gk-gravityview' ),
'value' => '',
'value' => $defaults['title'],
'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' ),
],
'use_labels' => [
'type' => 'checkbox',
'label' => __( 'Use labels instead of field ID\'s', 'gk-gravityview' ),
'desc' => __( 'The headers of the file will use the labels instead of the field ID\'s', 'gk-gravityview' ),
'value' => $defaults['use_labels'],
],
'classes' => [
'type' => 'text',
'class' => 'widefat',
'label' => __( 'Custom classes', 'gk-gravityview' ),
'desc' => __( 'These classes will be added to the <code>&lt;a&gt;</code> tag.', 'gk-gravityview' )
],
];
}

Expand All @@ -62,8 +85,9 @@ private static function settings(): array {
*/
private static function defaults(): array {
return [
'title' => __( 'Download CSV', 'gk-gravityview' ),
'type' => 'csv',
'title' => __( 'Download CSV', 'gk-gravityview' ),
'type' => 'csv',
'use_labels' => true,
];
}

Expand All @@ -72,13 +96,39 @@ private static function defaults(): array {
* @since $ver$
*/
public function __construct() {
$this->widget_short = esc_html__( 'Insert a button to a CSV / TSV download.', 'gk-gravityview' );
$this->widget_description = sprintf(
'<p>%s</p><p class="notice notice-alt notice-large notice-warning hidden csv-disabled-notice">%s</p>',
esc_html__( 'Insert a button to a CSV / TSV download.', 'gk-gravityview' ),
$this->widget_short,
esc_html__( 'In order to use this feature you need to Allow Export.', 'gk-gravityview' )
);

parent::__construct( 'CSV download button', 'csv_link', self::defaults(), self::settings() );
parent::__construct( 'Export Link', self::WIDGET_ID, self::defaults(), self::settings() );

add_filter( 'gravityview_admin_label_item_info', [ $this, 'hide_description_picker' ], 10, 2 );
}

/**
* Removes the notification part from the description.
*
* @since $ver$
*
* @param array $items The description items.
* @param GravityView_Admin_View_Item $view_item The view item.
*
* @return array The adjusted description items.
*/
public function hide_description_picker( array $items, GravityView_Admin_View_Item $view_item ): array {
if (
! $view_item instanceof GravityView_Admin_View_Widget
|| rgar( $items[0], 'value' ) !== $this->widget_description
) {
return $items;
}

$items[0]['value'] = $this->widget_short;

return $items;
}

/**
Expand Down Expand Up @@ -110,15 +160,21 @@ public function render_frontend( $widget_args, $content = '', $context = '' ): v

$label = rgar( $widget_args, 'title', 'Download CSV' );
$in_paragraph = (bool) rgar( $widget_args, 'in_paragraph', false );
$use_labels = (bool) rgar( $widget_args, 'use_labels', false );
$classes = (string) rgar( $widget_args, 'classes', '' );

$rest_url = add_query_arg(
[ '_nonce' => $nonce ],
[
'_nonce' => $nonce,
'use_labels' => $use_labels,
],
sprintf( '%sgravityview/v1/views/%d/entries.%s', get_rest_url(), $view->ID, $type )
);

$link = sprintf(
'<a href="%s" target="_blank" rel="noopener nofollow">%s</a>',
'<a href="%s" target="_blank" rel="noopener nofollow" class="%s">%s</a>',
esc_attr( $rest_url ),
esc_attr( $classes ),
esc_attr( $label )
);

Expand Down Expand Up @@ -156,4 +212,4 @@ private function get_nonce( $view ): string {
}
}

new GravityView_Widget_Csv_Link();
new GravityView_Widget_Export_Link();
2 changes: 1 addition & 1 deletion includes/widgets/register-gravityview-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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-export-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

0 comments on commit 962bf19

Please sign in to comment.