This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-csv-table-block.php
More file actions
116 lines (104 loc) · 2.61 KB
/
class-csv-table-block.php
File metadata and controls
116 lines (104 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Name: CSV Table Block for Gutenberg
* Plugin URI: https://github.com/emrikol/CSV-Table-Block
* Description: A CSV block for the Gutenberg editor that renders HTML tables.
* Version: 0.1
* Author: Derrick Tennant
* Author URI: https://emrikol.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://github.com/emrikol/CSV-Table-Block
* Text Domain: csv-table-block
*
* @package WordPress
*/
/**
* Main Class for the CSV Table Block
*/
class CSV_Table_Block {
/**
* Registers the csv-table-block Block Type.
*
* @access public
* @return void
*/
public static function register_block_types() {
register_block_type(
'csvblock/table', array(
'render_callback' => array( 'CSV_Table_Block', 'render_table' ),
'attributes' => array(
'content' => array(
'type' => 'string',
),
'hasHeader' => array(
'type' => 'boolean',
'default' => false,
),
'hasDownload' => array(
'type' => 'boolean',
'default' => false,
),
),
)
);
}
/**
* Enqueues Block JS and CSS assets.
*
* @access public
* @return void
*/
public static function enqueue_block_editor_assets() {
wp_enqueue_script(
'csv-table-block',
plugins_url( 'assets/js/csv-table-block.js', __FILE__ ),
array( 'wp-blocks', 'wp-element' )
);
wp_enqueue_style(
'csv-table-block',
plugins_url( 'assets/css/csv-table-block.css', __FILE__ ),
array()
);
}
/**
* Renders the CSV into an HTML table.
*
* @param string $attributes Block attributes.
* @access public
* @return string
*/
public static function render_table( $attributes ) {
$cell_sep = ',';
$line_sep = "\n";
$has_header = isset( $attributes['hasHeader'] ) ? $attributes['hasHeader'] : false;
$data = $attributes['content'];
$output = '<table class="wp-block-csvblock-table">';
$lines = explode( $line_sep, $data );
$output .= '<tbody>';
if ( true === $has_header ) {
$output .= '<thead>';
$output .= '<tr>';
$cells = str_getcsv( $lines[0], $cell_sep );
foreach ( $cells as $cell ) {
$output .= '<th>' . esc_html( $cell ) . '</th>';
}
$output .= '</tr>';
$output .= '</thead>';
// Remove the header for further processing.
unset( $lines[0] );
}
$output .= '<tbody>';
foreach ( $lines as $line ) {
$output .= '<tr>';
$cells = str_getcsv( $line, $cell_sep );
foreach ( $cells as $cell ) {
$output .= '<td>' . esc_html( $cell ) . '</td>';
}
$output .= '</tr>';
}
$output .= '</tbody>';
$output .= '</table>';
return $output;
}
}