PluginProbe
TablePress – Tables in WordPress made easy / trunk
TablePress – Tables in WordPress made easy vtrunk
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 2.4.4 All 44 releases
tablepress / classes / class-elementor-widget-table.php

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

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