PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.8
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.8
2.7.9 2.7.8 2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 All 30 releases
← All changes | admin/ajax/ajax.php +6 -88 2.7.5 → 2.7.8 View file →
@@ -305,103 +305,21 @@
305 305 WINP_SNIPPET_TYPE_JS !== $snippet_type &&
306 306 WINP_SNIPPET_TYPE_HTML !== $snippet_type ) {
307 307
308 308 $snippet_code = stripslashes( $snippet_code );
309 -
310 - if ( empty( $snippet_code ) ) {
311 - wp_send_json_success( [ 'valid' => true ] );
312 - }
313 309
314 - // Validate using the same logic as validate_code method.
315 - $validation_errors = [];
316 -
317 - // Set custom error handler to catch warnings and notices.
318 - set_error_handler( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
319 - function ( $errno, $errstr, $errfile, $errline ) use ( &$validation_errors ) {
320 - // Extract line number from eval'd code if present.
321 - if ( strpos( $errfile, "eval()'d code" ) !== false ) {
322 - // translators: %1$d is the line number, %2$s is the error message.
323 - $validation_errors[] = sprintf( __( 'Line %1$d: %2$s', 'insert-php' ), $errline, $errstr );
324 - } else {
325 - $validation_errors[] = $errstr;
326 - }
327 - return true; // Don't execute PHP internal error handler.
328 - }
329 - );
310 + $validation = WINP_Code_Validator::validate_code( $snippet_code, $snippet_type );
330 311
331 - ob_start();
332 -
333 - try {
334 - $result = WINP_SNIPPET_TYPE_UNIVERSAL === $snippet_type
335 - ? eval( '?> ' . $snippet_code . ' <?php ' )
336 - : eval( $snippet_code );
337 -
338 - // Discard any output (echo/print statements are normal for snippets).
339 - ob_end_clean();
340 -
341 - // Restore error handler.
342 - restore_error_handler();
343 -
344 - // Check if any errors were caught.
345 - if ( ! empty( $validation_errors ) ) {
346 - // Show all errors, separated by line breaks.
347 - $error_message = implode( '<br>', $validation_errors );
348 - wp_send_json_error(
349 - [
350 - 'valid' => false,
351 - 'message' => $error_message,
352 - ]
353 - );
354 - }
355 -
356 - if ( false === $result ) {
357 - wp_send_json_error(
358 - [
359 - 'valid' => false,
360 - 'message' => __( 'The code contains syntax errors. Please review and fix them before saving.', 'insert-php' ),
361 - ]
362 - );
363 - }
364 -
365 - wp_send_json_success( [ 'valid' => true ] );
366 -
367 - } catch ( ParseError $e ) {
368 - ob_end_clean();
369 - restore_error_handler();
312 + if ( ! $validation['valid'] ) {
370 313 wp_send_json_error(
371 314 [
372 315 'valid' => false,
373 - // translators: %1$d is the line number, %2$s is the error message.
374 - 'message' => sprintf( __( 'Syntax error on line %1$d: %2$s', 'insert-php' ), $e->getLine(), $e->getMessage() ),
375 - ]
316 + 'message' => $validation['message'],
317 + ]
376 318 );
377 - } catch ( Throwable $e ) {
378 - ob_end_clean();
379 - restore_error_handler();
380 -
381 - // Try to extract line number from the error message.
382 - $error_message = $e->getMessage();
383 - $line = $e->getLine();
384 -
385 - // For fatal errors in eval'd code, extract the actual line number.
386 - if ( strpos( $e->getFile(), "eval()'d code" ) !== false ) {
387 - wp_send_json_error(
388 - [
389 - 'valid' => false,
390 - // translators: %1$d is the line number, %2$s is the error message.
391 - 'message' => sprintf( __( 'Error on line %1$d: %2$s', 'insert-php' ), $line, $error_message ),
392 - ]
393 - );
394 - } else {
395 - wp_send_json_error(
396 - [
397 - 'valid' => false,
398 - // translators: %s is the error message.
399 - 'message' => sprintf( __( 'Error: %s', 'insert-php' ), $error_message ),
400 - ]
401 - );
402 - }
403 319 }
320 +
321 + wp_send_json_success( [ 'valid' => true ] );
404 322 } else {
405 323 // No validation needed for this type.
406 324 wp_send_json_success( [ 'valid' => true ] );
407 325 }