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-elementor-widget-table.php

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

283 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Elementor Widget
4 *
5 * @package TablePress
6 * @subpackage Elementor Widget
7 * @author Tobias Bäthge
8 * @since 3.1.0
9 */
10
11 declare(strict_types=1);
12
13 namespace TablePress\Elementor;
14
15 // Prohibit direct script loading.
16 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
17
18 /**
19 * TablePress Elementor Widget Class
20 *
21 * @package TablePress
22 * @subpackage Elementor Widget
23 * @author Tobias Bäthge
24 * @since 3.1.0
25 */
26 class TablePressTableWidget extends \Elementor\Widget_Base {
27
28 /**
29 * Gets the widget name.
30 *
31 * @since 3.1.0
32 *
33 * @return string Widget name.
34 */
35 public function get_name(): string {
36 return 'tablepress-table';
37 }
38
39 /**
40 * Gets the widget title.
41 *
42 * @since 3.1.0
43 *
44 * @return string Widget title.
45 */
46 public function get_title(): string {
47 return esc_html__( 'TablePress table', 'tablepress' );
48 }
49
50 /**
51 * Gets the widget icon.
52 *
53 * @since 3.1.0
54 *
55 * @return string Widget icon.
56 */
57 public function get_icon(): string {
58 return 'tablepress-elementor-icon';
59 }
60
61 /**
62 * Gets the widget categories.
63 *
64 * @since 3.1.0
65 *
66 * @return string[] Widget categories.
67 */
68 public function get_categories(): array {
69 return array( 'general' );
70 }
71
72 /**
73 * Gets the widget keywords.
74 *
75 * @since 3.1.0
76 *
77 * @return string[] Widget keywords.
78 */
79 public function get_keywords(): array {
80 return array( 'table', 'spreadsheet', 'csv', 'excel', 'data' );
81 }
82
83 /**
84 * Gets the custom help URL.
85 *
86 * @since 3.1.0
87 *
88 * @return string Widget help URL.
89 */
90 public function get_custom_help_url(): string {
91 return 'https://tablepress.org/support/';
92 }
93
94 /**
95 * Gets the widget stack and hides the "Advanced" tab.
96 *
97 * @since 3.1.0
98 *
99 * @param bool $with_common_controls Whether to include common controls.
100 * @return array<string, array<string, string>> Widget stack.
101 */
102 public function get_stack( $with_common_controls = true ) {
103 $stack = parent::get_stack( $with_common_controls ); // @phpstan-ignore staticMethod.notFound (Elementor methods are not in the stubs.)
104 unset( $stack['tabs']['advanced'] );
105 return $stack;
106 }
107
108 /**
109 * Gets the widget upsale data.
110 *
111 * @since 3.1.0
112 *
113 * @return array<string, string|bool> Widget upsale data.
114 */
115 protected function get_upsale_data(): array {
116 return array(
117 'condition' => tb_tp_fs()->is_free_plan(),
118 'image' => plugins_url( 'admin/img/tablepress.svg', TABLEPRESS__FILE__ ),
119 'image_alt' => esc_attr__( 'Upgrade to TablePress Pro!', 'tablepress' ),
120 'title' => esc_html__( 'Upgrade to TablePress Pro!', 'tablepress' ),
121 'description' => esc_html__( 'Check out the TablePress premium versions and give your tables super powers!', 'tablepress' ),
122 'upgrade_url' => 'https://tablepress.org/premium/?utm_source=plugin&utm_medium=button&utm_content=elementor-widget',
123 'upgrade_text' => esc_html__( 'Upgrade Now', 'tablepress' ),
124 );
125 }
126
127 /**
128 * Gets whether the widget requires an inner wrapper.
129 *
130 * This is used to determine whether to optimize the DOM size.
131 *
132 * @since 3.1.0
133 *
134 * @return bool Whether to optimize the DOM size.
135 */
136 public function has_widget_inner_wrapper(): bool {
137 return false;
138 }
139
140 /**
141 * Gets whether the element returns dynamic content.
142 *
143 * This is used to determine whether to cache the element output or not.
144 *
145 * @since 3.1.0
146 *
147 * @return bool Whether to cache the element output.
148 */
149 protected function is_dynamic_content(): bool {
150 return true;
151 }
152
153 /**
154 * Registers the widget controls.
155 *
156 * Adds input fields to allow the user to customize the widget settings.
157 *
158 * @since 3.1.0
159 */
160 protected function register_controls(): void {
161 $this->start_controls_section( // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
162 'table',
163 array(
164 'label' => esc_html__( 'Table', 'tablepress' ),
165 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, // @phpstan-ignore classConstant.notFound (Elementor constants are not in the stubs.)
166 ),
167 );
168
169 $tables = array();
170 // Load all table IDs without priming the post meta cache, as table options/visibility are not needed.
171 $table_ids = \TablePress::$model_table->load_all( false );
172 foreach ( $table_ids as $table_id ) {
173 // Load table, without table data, options, and visibility settings.
174 $table = \TablePress::$model_table->load( $table_id, false, false );
175
176 // Skip tables that could not be loaded.
177 if ( is_wp_error( $table ) ) {
178 continue;
179 }
180
181 if ( '' === trim( $table['name'] ) ) {
182 $table['name'] = __( '(no name)', 'tablepress' );
183 }
184 /* translators: %1$s: Table ID, %2$s: Table name */
185 $tables[ $table_id ] = esc_html( sprintf( __( 'ID %1$s: %2$s', 'tablepress' ), $table_id, $table['name'] ) );
186 }
187
188 /**
189 * Filters the list of table IDs and names that is passed to the block editor, and is then used in the dropdown of the TablePress table block.
190 *
191 * @since 2.0.0
192 *
193 * @param array<string, string> $tables List of table names, the table ID is the array key.
194 */
195 $tables = apply_filters( 'tablepress_block_editor_tables_list', $tables );
196
197 $this->add_control( // @phpstan-ignore method.notFound
198 'table_id',
199 array(
200 'label' => esc_html__( 'Table:', 'tablepress' ),
201 'show_label' => false,
202 'label_block' => true,
203 'description' => esc_html__( 'Select the TablePress table that you want to embed.', 'tablepress' )
204 . ( current_user_can( 'tablepress_list_tables' ) ?
205 /* translators: %1$s: URL to tables list, %2$s: Link text */
206 sprintf( ' <a href="%1$s" target="_blank">%2$s</a>', esc_url( \TablePress::url( array( 'action' => 'list' ) ) ), esc_html__( 'Manage your tables.', 'tablepress' ) )
207 : ''
208 ),
209 'type' => \Elementor\Controls_Manager::SELECT2, // @phpstan-ignore classConstant.notFound (Elementor constants are not in the stubs.)
210 'ai' => array( 'active' => false ),
211 'dynamic' => array( 'active' => true ), // Elementor does not support this for the "Select2" control yet, but maybe in the future?
212 'options' => $tables,
213 ),
214 );
215
216 $this->end_controls_section(); // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
217
218 $this->start_controls_section( // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
219 'advanced',
220 array(
221 'label' => esc_html__( 'Advanced', 'tablepress' ),
222 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, // @phpstan-ignore classConstant.notFound (Elementor constants are not in the stubs.)
223 'condition' => array(
224 'table_id!' => '',
225 ),
226 ),
227 );
228
229 $this->add_control( // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
230 'parameters',
231 array(
232 'label' => esc_html__( 'Configuration parameters:', 'tablepress' ),
233 'label_block' => true,
234 'description' => esc_html( __( 'These additional parameters can be used to modify specific table features.', 'tablepress' ) . ' ' . __( 'See the TablePress Documentation for more information.', 'tablepress' ) ),
235 'type' => \Elementor\Controls_Manager::TEXT, // @phpstan-ignore classConstant.notFound (Elementor constants are not in the stubs.)
236 'input_type' => 'text',
237 'placeholder' => '',
238 'ai' => array( 'active' => false ),
239 'dynamic' => array( 'active' => true ),
240 ),
241 );
242
243 $this->end_controls_section(); // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
244 }
245
246 /**
247 * Render oEmbed widget output on the frontend.
248 *
249 * Written in PHP and used to generate the final HTML.
250 *
251 * @since 3.1.0
252 */
253 protected function render(): void {
254 $settings = $this->get_settings_for_display(); // @phpstan-ignore method.notFound (Elementor methods are not in the stubs.)
255
256 // Don't return anything if no table was selected.
257 if ( empty( $settings['table_id'] ) ) {
258 /*
259 * In TablePress 3.1 (before 3.1.1), the widget control was named "id" instead of "table_id", which however caused problems.
260 * To ensure that tables will continue to be shown, if the widget was created with 3.1, the "table_id" is set to the "id" value, if only that exists.
261 */
262 if ( empty( $settings['id'] ) ) {
263 return;
264 } else {
265 $settings['table_id'] = $settings['id'];
266 }
267 }
268
269 $render_attributes = shortcode_parse_atts( $settings['parameters'] );
270 $render_attributes['id'] = $settings['table_id'];
271
272 /*
273 * It would be nice to print only the Shortcode, for better data portability, e.g. if a site switches away from Elementor.
274 * However, the editor will then only render the Shortcode itself, which is not very helpful.
275 * Due to this, the table HTML code is rendered.
276 * echo '[' . \TablePress::$shortcode . " id={$settings['table_id']} {$settings['parameters']} /]";
277 */
278
279 echo \TablePress::$controller->shortcode_table( $render_attributes );
280 }
281
282 } // class TablePressTableWidget
283