| @@ -785,17 +785,22 @@ | ||
| 785 | 785 | if ( empty( $snippet ) || $snippet->post_type !== WINP_SNIPPETS_POST_TYPE ) { |
| 786 | 786 | return false; |
| 787 | 787 | } |
| 788 | 788 | |
| 789 | - $snippet_type = WINP_Helper::getMetaOption( $id, 'snippet_type', true ); | |
| 789 | + $snippet_type = WINP_Helper::getMetaOption( $id, 'snippet_type', true ); | |
| 790 | + $is_executable = in_array( $snippet_type, [ WINP_SNIPPET_TYPE_PHP, WINP_SNIPPET_TYPE_UNIVERSAL ], true ); | |
| 790 | 791 | |
| 791 | - // Set current snippet ID for error handling. | |
| 792 | - self::$current_snippet_id = $id; | |
| 792 | + if ( $is_executable ) { | |
| 793 | + // Preserve the active snippet context if execution terminates the request. | |
| 794 | + self::$current_snippet_id = $id; | |
| 795 | + WINP_Error_Handler::init(); | |
| 796 | + WINP_Error_Handler::set_current_snippet( $id, $snippet->post_title ); | |
| 793 | 797 | |
| 794 | - // Register shutdown function once to catch fatal errors. | |
| 795 | - if ( ! self::$shutdown_registered ) { | |
| 796 | - register_shutdown_function( [ $this, 'handle_snippet_shutdown' ] ); | |
| 797 | - self::$shutdown_registered = true; | |
| 798 | + // Register shutdown function once to catch fatal errors. | |
| 799 | + if ( ! self::$shutdown_registered ) { | |
| 800 | + register_shutdown_function( [ $this, 'handle_snippet_shutdown' ] ); | |
| 801 | + self::$shutdown_registered = true; | |
| 802 | + } | |
| 798 | 803 | } |
| 799 | 804 | |
| 800 | 805 | if ( $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ) { |
| 801 | 806 | $result = eval( '?>' . $code . '<?php ' ); |
| @@ -802,8 +807,13 @@ | ||
| 802 | 807 | } elseif ( $snippet_type == WINP_SNIPPET_TYPE_PHP ) { |
| 803 | 808 | $result = eval( $code ); |
| 804 | 809 | } else { |
| 805 | 810 | $result = ! empty( $code ); |
| 811 | + } | |
| 812 | + | |
| 813 | + if ( $is_executable ) { | |
| 814 | + self::$current_snippet_id = 0; | |
| 815 | + WINP_Error_Handler::clear_current_snippet(); | |
| 806 | 816 | } |
| 807 | 817 | |
| 808 | 818 | if ( $catch_output ) { |
| 809 | 819 | ob_end_clean(); |