PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / classes / class-export.php

class-export.php in TablePress – Tables in WordPress made easy 3.4, at classes/class-export.php

144 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Table Export Class
4 *
5 * @package TablePress
6 * @subpackage Export/Import
7 * @author Tobias Bäthge
8 * @since 1.0.0
9 */
10
11 declare(strict_types=1);
12
13 // Prohibit direct script loading.
14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
15
16 /**
17 * TablePress Table Export Class
18 *
19 * @package TablePress
20 * @subpackage Export/Import
21 * @author Tobias Bäthge
22 * @since 1.0.0
23 */
24 class TablePress_Export {
25
26 /**
27 * Available exporters for the export.
28 *
29 * @since 3.4.0
30 * @var array<string, array{label: string, class: string, file: string, folder: string}>
31 */
32 protected array $exporters = array();
33
34 /**
35 * File/Data Formats that are available for the export.
36 *
37 * @since 1.0.0
38 * @var array<string, string>
39 */
40 public array $export_formats = array();
41
42 /**
43 * Delimiters for the CSV export.
44 *
45 * @since 1.0.0
46 * @var array<string, string>
47 */
48 public array $csv_delimiters = array();
49
50 /**
51 * Whether ZIP archive support is available in the PHP installation on the server.
52 *
53 * @since 1.0.0
54 */
55 public bool $zip_support_available = false;
56
57 /**
58 * Initializes the Export class.
59 *
60 * @since 1.0.0
61 */
62 public function __construct() {
63 // Initiate here, because function call not possible outside a class method.
64 $this->exporters = array(
65 'csv' => array(
66 'label' => __( 'CSV - Character-Separated Values', 'tablepress' ),
67 'class' => \TablePress\Export\CSV_Exporter::class,
68 'file' => 'class-exporter-csv.php',
69 'folder' => 'classes/exporters',
70 ),
71 'html' => array(
72 'label' => __( 'HTML - Hypertext Markup Language', 'tablepress' ),
73 'class' => \TablePress\Export\HTML_Exporter::class,
74 'file' => 'class-exporter-html.php',
75 'folder' => 'classes/exporters',
76 ),
77 'json' => array(
78 'label' => __( 'JSON - JavaScript Object Notation', 'tablepress' ),
79 'class' => \TablePress\Export\JSON_Exporter::class,
80 'file' => 'class-exporter-json.php',
81 'folder' => 'classes/exporters',
82 ),
83 );
84
85 /**
86 * Filters the available export formats.
87 *
88 * @since 3.4.0
89 *
90 * @param array<string, array{label: string, class: string, file: string, folder: string}> $exporters Associative array of available export formats.
91 */
92 $this->exporters = apply_filters( 'tablepress_exporters', $this->exporters );
93
94 foreach ( $this->exporters as $format => $exporter ) {
95 $this->export_formats[ $format ] = $exporter['label'];
96 }
97
98 $this->csv_delimiters = array(
99 ';' => __( '; (semicolon)', 'tablepress' ),
100 ',' => __( ', (comma)', 'tablepress' ),
101 'tab' => __( '\t (tabulator)', 'tablepress' ),
102 );
103
104 if ( class_exists( 'ZipArchive', false ) ) {
105 $this->zip_support_available = true;
106 }
107 }
108
109 /**
110 * Exports a table to the specified format using the corresponding exporter class.
111 *
112 * @since 1.0.0
113 * @since 3.4.0 The $options parameter is now an array. If a string is passed, it is treated as the CSV delimiter (deprecated).
114 *
115 * @param array<string, mixed> $table Table to be exported.
116 * @param string $export_format Format for the export, e.g. 'csv', 'html', or 'json'.
117 * @param array<string, mixed>|string $options Options for the export if an array, or the CSV delimiter if a string. The latter is deprecated.
118 * @return string Exported table data.
119 */
120 public function export_table( array $table, string $export_format, /* array|string */ $options ): string {
121 if ( ! isset( $this->exporters[ $export_format ] ) ) {
122 return '';
123 }
124
125 if ( ! is_array( $options ) ) {
126 $options = array(
127 'csv_delimiter' => $options,
128 );
129 }
130
131 \TablePress::load_file( 'interface-exporter.php', 'classes/exporters' );
132
133 if ( '' !== $this->exporters[ $export_format ]['file'] ) {
134 \TablePress::load_file( $this->exporters[ $export_format ]['file'], $this->exporters[ $export_format ]['folder'] );
135 }
136
137 $exporter_class = $this->exporters[ $export_format ]['class'];
138 $exporter = new $exporter_class();
139
140 return $exporter->export( $table, $options );
141 }
142
143 } // class TablePress_Export
144