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 +5 -98 2.7.7 → 2.7.8 View file →
@@ -305,114 +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 - $redeclaration = WINP_Code_Validator::find_function_redeclaration( $snippet_code, $snippet_type );
315 - if ( null !== $redeclaration ) {
310 + $validation = WINP_Code_Validator::validate_code( $snippet_code, $snippet_type );
311 +
312 + if ( ! $validation['valid'] ) {
316 313 wp_send_json_error(
317 314 [
318 315 'valid' => false,
319 - // translators: %1$d is the line number, %2$s is the fully qualified function name.
320 - 'message' => sprintf( __( 'Line %1$d: Cannot redeclare function %2$s(). Rename the function or guard its declaration with function_exists().', 'insert-php' ), $redeclaration['line'], $redeclaration['name'] ),
316 + 'message' => $validation['message'],
321 317 ]
322 318 );
323 319 }
324 320
325 - // Validate using the same logic as validate_code method.
326 - $validation_errors = [];
327 -
328 - // Set custom error handler to catch warnings and notices.
329 - set_error_handler( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
330 - function ( $errno, $errstr, $errfile, $errline ) use ( &$validation_errors ) {
331 - // Extract line number from eval'd code if present.
332 - if ( strpos( $errfile, "eval()'d code" ) !== false ) {
333 - // translators: %1$d is the line number, %2$s is the error message.
334 - $validation_errors[] = sprintf( __( 'Line %1$d: %2$s', 'insert-php' ), $errline, $errstr );
335 - } else {
336 - $validation_errors[] = $errstr;
337 - }
338 - return true; // Don't execute PHP internal error handler.
339 - }
340 - );
341 -
342 - ob_start();
343 -
344 - try {
345 - $result = WINP_SNIPPET_TYPE_UNIVERSAL === $snippet_type
346 - ? eval( '?> ' . $snippet_code . ' <?php ' )
347 - : eval( $snippet_code );
348 -
349 - // Discard any output (echo/print statements are normal for snippets).
350 - ob_end_clean();
351 -
352 - // Restore error handler.
353 - restore_error_handler();
354 -
355 - // Check if any errors were caught.
356 - if ( ! empty( $validation_errors ) ) {
357 - // Show all errors, separated by line breaks.
358 - $error_message = implode( '<br>', $validation_errors );
359 - wp_send_json_error(
360 - [
361 - 'valid' => false,
362 - 'message' => $error_message,
363 - ]
364 - );
365 - }
366 -
367 - if ( false === $result ) {
368 - wp_send_json_error(
369 - [
370 - 'valid' => false,
371 - 'message' => __( 'The code contains syntax errors. Please review and fix them before saving.', 'insert-php' ),
372 - ]
373 - );
374 - }
375 -
376 - wp_send_json_success( [ 'valid' => true ] );
377 -
378 - } catch ( ParseError $e ) {
379 - ob_end_clean();
380 - restore_error_handler();
381 - wp_send_json_error(
382 - [
383 - 'valid' => false,
384 - // translators: %1$d is the line number, %2$s is the error message.
385 - 'message' => sprintf( __( 'Syntax error on line %1$d: %2$s', 'insert-php' ), $e->getLine(), $e->getMessage() ),
386 - ]
387 - );
388 - } catch ( Throwable $e ) {
389 - ob_end_clean();
390 - restore_error_handler();
391 -
392 - // Try to extract line number from the error message.
393 - $error_message = $e->getMessage();
394 - $line = $e->getLine();
395 -
396 - // For fatal errors in eval'd code, extract the actual line number.
397 - if ( strpos( $e->getFile(), "eval()'d code" ) !== false ) {
398 - wp_send_json_error(
399 - [
400 - 'valid' => false,
401 - // translators: %1$d is the line number, %2$s is the error message.
402 - 'message' => sprintf( __( 'Error on line %1$d: %2$s', 'insert-php' ), $line, $error_message ),
403 - ]
404 - );
405 - } else {
406 - wp_send_json_error(
407 - [
408 - 'valid' => false,
409 - // translators: %s is the error message.
410 - 'message' => sprintf( __( 'Error: %s', 'insert-php' ), $error_message ),
411 - ]
412 - );
413 - }
414 - }
321 + wp_send_json_success( [ 'valid' => true ] );
415 322 } else {
416 323 // No validation needed for this type.
417 324 wp_send_json_success( [ 'valid' => true ] );
418 325 }