| @@ -14,17 +14,21 @@ | ||
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * Content render |
| 17 | 17 | * |
| 18 | - * @param array $attr | |
| 19 | - * @param string $content | |
| 20 | - * @param string $tag | |
| 18 | + * @param array $attr Shortcode attributes. | |
| 19 | + * @param string $content Enclosed shortcode content. | |
| 20 | + * @param string $tag Shortcode tag. | |
| 21 | + * @return mixed Rendered snippet output, if any. | |
| 21 | 22 | */ |
| 22 | 23 | public function html( $attr, $content, $tag ) { |
| 23 | - $id = $this->getSnippetId( $attr, WINP_SNIPPET_TYPE_PHP ); | |
| 24 | + $id = $this->get_snippet_id( $attr, WINP_SNIPPET_TYPE_PHP ); | |
| 24 | 25 | |
| 25 | 26 | if ( ! $id ) { |
| 26 | - echo '<span style="color:red">' . __( '[' . esc_html( $tag ) . ']: PHP snippets error (not passed the snippet ID)', 'insert-php' ) . '</span>'; | |
| 27 | + if ( current_user_can( 'manage_options' ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) { | |
| 28 | + /* translators: %s: Shortcode tag name */ | |
| 29 | + echo '<span style="color:red">' . sprintf( esc_html__( '[%s]: PHP snippets error (not passed the snippet ID)', 'insert-php' ), esc_html( $tag ) ) . '</span>'; | |
| 30 | + } | |
| 27 | 31 | |
| 28 | 32 | return; |
| 29 | 33 | } |
| 30 | 34 | |
| @@ -34,30 +38,60 @@ | ||
| 34 | 38 | if ( ! $snippet || empty( $snippet_meta ) ) { |
| 35 | 39 | return; |
| 36 | 40 | } |
| 37 | 41 | |
| 38 | - $attr = $this->filterAttributes( $attr, $id ); | |
| 42 | + $attr = $this->filter_attributes( $attr, $id ); | |
| 39 | 43 | |
| 40 | 44 | // Let users pass arbitrary variables, through shortcode attributes. |
| 41 | 45 | // @since 2.0.5 |
| 42 | 46 | extract( $attr, EXTR_SKIP ); |
| 43 | 47 | |
| 44 | - $is_activate = $this->getSnippetActivate( $snippet_meta ); | |
| 45 | - $snippet_scope = $this->getSnippetScope( $snippet_meta ); | |
| 46 | - $snippet_content = $this->getSnippetContent( $snippet, $snippet_meta, $id ); | |
| 48 | + $is_activate = $this->get_snippet_activate( $snippet_meta ); | |
| 49 | + $snippet_scope = $this->get_snippet_scope( $snippet_meta ); | |
| 50 | + $snippet_content = $this->get_snippet_content( $snippet, $snippet_meta, $id ); | |
| 47 | 51 | |
| 48 | 52 | if ( ! $is_activate || empty( $snippet_content ) || $snippet_scope != 'shortcode' || WINP_Helper::is_safe_mode() ) { |
| 49 | 53 | return; |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | - if( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) { | |
| 53 | - if ( is_user_logged_in() && WINP_Plugin::app()->currentUserCan() ) { | |
| 54 | - echo __( '[Woody snippet cannot be executed because you have disabled the insertion of unfiltered html!]', 'insert-php' ); | |
| 56 | + if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) { | |
| 57 | + if ( is_user_logged_in() && WINP_Plugin::app()->current_user_car() ) { | |
| 58 | + echo esc_html__( 'This Woody snippet cannot run because unfiltered HTML insertion is disabled.', 'insert-php' ); | |
| 55 | 59 | } |
| 56 | 60 | |
| 57 | 61 | return; |
| 58 | 62 | } |
| 59 | 63 | |
| 60 | - eval( $snippet_content ); | |
| 64 | + // Track shortcode execution. | |
| 65 | + WINP_Plugin::app()->get_execute_object()->track_shortcode_snippet( $id ); | |
| 66 | + | |
| 67 | + // Initialize error handler. | |
| 68 | + WINP_Error_Handler::init(); | |
| 69 | + | |
| 70 | + // Set current snippet context for error handler. | |
| 71 | + WINP_Error_Handler::set_current_snippet( $id, $snippet->post_title, $snippet_content ); | |
| 72 | + | |
| 73 | + $buffer_level = ob_get_level(); | |
| 74 | + ob_start(); | |
| 75 | + | |
| 76 | + try { | |
| 77 | + eval( $snippet_content ); | |
| 78 | + | |
| 79 | + // Preserve output from buffers opened by the snippet itself. | |
| 80 | + while ( ob_get_level() > $buffer_level + 1 ) { | |
| 81 | + ob_end_flush(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + $snippet_output = ob_get_clean(); | |
| 85 | + return false !== $snippet_output ? $snippet_output : ''; | |
| 86 | + } catch ( Throwable $exception ) { | |
| 87 | + while ( ob_get_level() > $buffer_level ) { | |
| 88 | + ob_end_clean(); | |
| 89 | + } | |
| 90 | + | |
| 91 | + return WINP_Error_Handler::handle_exception( $exception ); | |
| 92 | + } finally { | |
| 93 | + // Clear snippet context after execution. | |
| 94 | + WINP_Error_Handler::clear_current_snippet(); | |
| 95 | + } | |
| 61 | 96 | } |
| 62 | - | |
| 63 | -} | |
| 97 | +} | |