PluginProbe
TablePress – Tables in WordPress made easy / 3.0
TablePress – Tables in WordPress made easy v3.0
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
← All changes | controllers/controller-admin_ajax.php +53 -36 2.1.73.0 View file →
@@ -42,9 +42,9 @@
42 42 * Hides a header message on an admin screen.
43 43 *
44 44 * @since 1.0.0
45 45 */
46 - public function ajax_action_hide_message() {
46 + public function ajax_action_hide_message(): void {
47 47 if ( empty( $_GET['item'] ) ) {
48 48 wp_die( '0' );
49 49 }
50 50
@@ -65,9 +65,9 @@
65 65 * Saves the table after the "Save Changes" button on the "Edit" screen has been clicked.
66 66 *
67 67 * @since 1.0.0
68 68 */
69 - public function ajax_action_save_table() {
69 + public function ajax_action_save_table(): void {
70 70 if ( empty( $_POST['tablepress']['id'] ) ) {
71 71 wp_die( '-1' );
72 72 }
73 73
@@ -84,12 +84,12 @@
84 84 // Default response data.
85 85 $success = false;
86 86 $message = 'error_save';
87 87 $error_details = '';
88 - do { // to be able to "break;" (allows for better readable code)
88 + do { // To be able to "break;" (allows for better readable code).
89 89 // Load table, without table data, but with options and visibility settings.
90 90 $existing_table = TablePress::$model_table->load( $edit_table['id'], false, true );
91 - if ( is_wp_error( $existing_table ) ) { // maybe somehow load a new table here? (TablePress::$model_table->get_table_template())?
91 + if ( is_wp_error( $existing_table ) ) {
92 92 $error = new WP_Error( 'ajax_save_table_load', '', $edit_table['id'] );
93 93 $error->merge_from( $existing_table );
94 94 $error_details = TablePress::get_wp_error_string( $error );
95 95 break;
@@ -94,19 +94,24 @@
94 94 $error_details = TablePress::get_wp_error_string( $error );
95 95 break;
96 96 }
97 97
98 - // Check and convert data that was transmitted as JSON.
99 - if ( empty( $edit_table['data'] )
100 - || empty( $edit_table['options'] )
101 - || empty( $edit_table['visibility'] ) ) {
102 - $error = new WP_Error( 'ajax_save_table_data_empty', '', $edit_table['id'] );
103 - $error_details = TablePress::get_wp_error_string( $error );
104 - break;
98 + // Check and convert all data that was transmitted as valid JSON.
99 + $keys = array( 'data', 'options', 'visibility' );
100 + foreach ( $keys as $key ) {
101 + if ( empty( $edit_table[ $key ] ) ) {
102 + $error = new WP_Error( "ajax_save_table_{$key}_empty", '', $edit_table['id'] );
103 + $error_details = TablePress::get_wp_error_string( $error );
104 + break 2;
105 + }
106 + $edit_table[ $key ] = json_decode( $edit_table[ $key ], true );
107 + if ( is_null( $edit_table[ $key ] ) ) {
108 + $error = new WP_Error( "ajax_save_table_{$key}_invalid_json", '', $edit_table['id'] );
109 + $error_details = TablePress::get_wp_error_string( $error );
110 + break 2;
111 + }
112 + $edit_table[ $key ] = (array) $edit_table[ $key ]; // Cast to array again, to catch strings, etc.
105 113 }
106 - $edit_table['data'] = (array) json_decode( $edit_table['data'], true );
107 - $edit_table['options'] = (array) json_decode( $edit_table['options'], true );
108 - $edit_table['visibility'] = (array) json_decode( $edit_table['visibility'], true );
109 114
110 115 // Check consistency of new table, and then merge with existing table.
111 116 $table = TablePress::$model_table->prepare_table( $existing_table, $edit_table, true );
112 117 if ( is_wp_error( $table ) ) {
@@ -155,8 +160,10 @@
155 160 } else {
156 161 $message = 'success_save_error_id_change';
157 162 $error_details = 'table_id_could_not_be_changed: capability_check_failed';
158 163 }
164 +
165 + // @phpstan-ignore doWhile.alwaysFalse
159 166 } while ( false ); // Do-while-loop through this exactly once, to be able to "break;" early.
160 167
161 168 // Generate the response.
162 169
@@ -165,13 +172,16 @@
165 172 'success' => $success,
166 173 'message' => $message,
167 174 );
168 175 if ( $success ) {
169 - $response['table_id'] = $table['id'];
170 - $response['new_edit_nonce'] = wp_create_nonce( TablePress::nonce( 'edit', $table['id'] ) );
171 - $response['new_preview_nonce'] = wp_create_nonce( TablePress::nonce( 'preview_table', $table['id'] ) );
172 - $response['last_modified'] = TablePress::format_datetime( $table['last_modified'] );
173 - $response['last_editor'] = TablePress::get_user_display_name( $table['options']['last_editor'] );
176 + // For the phpstan ignores in the next lines: If this is reached, $table is guaranteed to exist and is a valid array.
177 + $response['table_id'] = $table['id']; // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
178 + $response['new_edit_nonce'] = wp_create_nonce( TablePress::nonce( 'edit', $table['id'] ) ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
179 + $response['new_preview_nonce'] = wp_create_nonce( TablePress::nonce( 'preview_table', $table['id'] ) ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
180 + $response['new_copy_nonce'] = wp_create_nonce( TablePress::nonce( 'copy_table', $table['id'] ) ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
181 + $response['new_delete_nonce'] = wp_create_nonce( TablePress::nonce( 'delete_table', $table['id'] ) ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
182 + $response['last_modified'] = TablePress::format_datetime( $table['last_modified'] ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
183 + $response['last_editor'] = TablePress::get_user_display_name( $table['options']['last_editor'] ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
174 184 }
175 185 if ( ! empty( $error_details ) ) {
176 186 $response['error_details'] = esc_html( $error_details );
177 187 }
@@ -189,9 +199,9 @@
189 199 * Returns the live preview data of table that has non-saved changes.
190 200 *
191 201 * @since 1.0.0
192 202 */
193 - public function ajax_action_preview_table() {
203 + public function ajax_action_preview_table(): void {
194 204 if ( empty( $_POST['tablepress']['id'] ) ) {
195 205 wp_die( '-1' );
196 206 }
197 207
@@ -206,24 +216,27 @@
206 216 }
207 217
208 218 // Default response data.
209 219 $success = false;
210 - do { // to be able to "break;" (allows for better readable code)
220 + do { // To be able to "break;" (allows for better readable code).
211 221 // Load table, without table data, but with options and visibility settings.
212 222 $existing_table = TablePress::$model_table->load( $preview_table['id'], false, true );
213 - if ( is_wp_error( $existing_table ) ) { // maybe somehow load a new table here? (TablePress::$model_table->get_table_template())?
223 + if ( is_wp_error( $existing_table ) ) {
214 224 break;
215 225 }
216 226
217 - // Check and convert data that was transmitted as JSON.
218 - if ( empty( $preview_table['data'] )
219 - || empty( $preview_table['options'] )
220 - || empty( $preview_table['visibility'] ) ) {
221 - break;
227 + // Check and convert all data that was transmitted as valid JSON.
228 + $keys = array( 'data', 'options', 'visibility' );
229 + foreach ( $keys as $key ) {
230 + if ( empty( $preview_table[ $key ] ) ) {
231 + break 2;
232 + }
233 + $preview_table[ $key ] = json_decode( $preview_table[ $key ], true );
234 + if ( is_null( $preview_table[ $key ] ) ) {
235 + break 2;
236 + }
237 + $preview_table[ $key ] = (array) $preview_table[ $key ]; // Cast to array again, to catch strings, etc.
222 238 }
223 - $preview_table['data'] = (array) json_decode( $preview_table['data'], true );
224 - $preview_table['options'] = (array) json_decode( $preview_table['options'], true );
225 - $preview_table['visibility'] = (array) json_decode( $preview_table['visibility'], true );
226 239
227 240 // Check consistency of new table, and then merge with existing table.
228 241 $table = TablePress::$model_table->prepare_table( $existing_table, $preview_table, true );
229 242 if ( is_wp_error( $table ) ) {
@@ -246,8 +259,10 @@
246 259 }
247 260
248 261 // At this point, the table data is valid and sanitized and can be rendered.
249 262 $success = true;
263 +
264 + // @phpstan-ignore doWhile.alwaysFalse
250 265 } while ( false ); // Do-while-loop through this exactly once, to be able to "break;" early.
251 266
252 267 if ( $success ) {
253 268 // Create a render class instance.
@@ -255,13 +270,15 @@
255 270 // Merge desired options with default render options (see TablePress_Controller_Frontend::shortcode_table()).
256 271 $default_render_options = $_render->get_default_render_options();
257 272 /** This filter is documented in controllers/controller-frontend.php */
258 273 $default_render_options = apply_filters( 'tablepress_shortcode_table_default_shortcode_atts', $default_render_options );
259 - $render_options = shortcode_atts( $default_render_options, $table['options'] );
274 + // For the phpstan ignores in the next lines: If this is reached, $table is guaranteed to exist and is a valid array.
275 + $render_options = shortcode_atts( $default_render_options, $table['options'] ); // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
260 276 /** This filter is documented in controllers/controller-frontend.php */
261 277 $render_options = apply_filters( 'tablepress_shortcode_table_shortcode_atts', $render_options );
262 - $render_options['html_id'] = "tablepress-{$table['id']}";
263 - $_render->set_input( $table, $render_options );
278 + $render_options['html_id'] = "tablepress-{$table['id']}"; // @phpstan-ignore offsetAccess.nonOffsetAccessible, variable.undefined
279 + $render_options['block_preview'] = true;
280 + $_render->set_input( $table, $render_options ); // @phpstan-ignore variable.undefined
264 281 $head_html = $_render->get_preview_css();
265 282 $custom_css = TablePress::$model_options->get( 'custom_css' );
266 283 $use_custom_css = ( TablePress::$model_options->get( 'use_custom_css' ) && '' !== $custom_css );
267 284 if ( $use_custom_css ) {
@@ -270,9 +287,9 @@
270 287
271 288 $body_html = '<div id="tablepress-page"><p>'
272 289 . __( 'This is a preview of your table.', 'tablepress' ) . ' '
273 290 . __( 'Because of CSS styling in your theme, the table might look different on your page!', 'tablepress' ) . ' '
274 - . __( 'The Table Features for Site Visitors, like sorting, filtering, and pagination, are also not available in this preview!', 'tablepress' ) . '<br />';
291 + . __( 'The Table Features for Site Visitors, like sorting, filtering, and pagination, are also not available in this preview!', 'tablepress' ) . '<br>';
275 292 // Show the instructions string depending on whether the Block Editor is used on the site or not.
276 293 if ( TablePress::site_uses_block_editor() ) {
277 294 $body_html .= sprintf( __( 'To insert a table into a post or page, add a “%1$s” block in the block editor and select the desired table.', 'tablepress' ), __( 'TablePress table', 'tablepress' ) );
278 295 } else {
@@ -278,9 +295,9 @@
278 295 } else {
279 296 $body_html .= __( 'To insert a table into a post or page, paste its Shortcode at the desired place in the editor.', 'tablepress' ) . ' '
280 297 . __( 'Each table has a unique ID that needs to be adjusted in that Shortcode.', 'tablepress' );
281 298 }
282 - $body_html .= '</p>' . $_render->get_output() . '</div>';
299 + $body_html .= '</p>' . $_render->get_output( 'html' ) . '</div>';
283 300 } else {
284 301 $head_html = '';
285 302 $body_html = __( 'The preview could not be loaded.', 'tablepress' );
286 303 }
@@ -305,9 +322,9 @@
305 322 * Saves the screen options on the "Edit" screen when they are changed.
306 323 *
307 324 * @since 2.1.0
308 325 */
309 - public function ajax_action_save_screen_options() {
326 + public function ajax_action_save_screen_options(): void {
310 327 // Check if the submitted nonce matches the generated nonce we created earlier, dies -1 on failure.
311 328 TablePress::check_nonce( 'screen_options', false, '_ajax_nonce', true );
312 329
313 330 if ( empty( $_POST['tablepress'] ) ) {