| @@ -1,16 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Code_Snippets\Integration; |
| 4 | 4 | |
| 5 | -use Code_Snippets\Core\DB; | |
| 5 | +use WP_Post; | |
| 6 | 6 | use Code_Snippets\Flat_Files\Snippet_Files; |
| 7 | 7 | use Code_Snippets\Model\Snippet; |
| 8 | 8 | use Code_Snippets\Utils\Code_Highlighter; |
| 9 | -use WP_Post; | |
| 10 | 9 | use function Code_Snippets\code_snippets; |
| 11 | 10 | use function Code_Snippets\get_snippet; |
| 12 | 11 | use function Code_Snippets\Settings\get_setting; |
| 12 | +use function Code_Snippets\Utils\validate_network_param; | |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * This class manages the shortcodes included with the plugin, |
| 16 | 16 | * |
| @@ -142,9 +142,9 @@ | ||
| 142 | 142 | if ( ! Snippet_Files::is_active() ) { |
| 143 | 143 | return $this->evaluate_shortcode_from_db( $snippet, $atts ); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - $network = DB::validate_network_param( $snippet->network ); | |
| 146 | + $network = validate_network_param( $snippet->network ); | |
| 147 | 147 | $table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $network ) ); |
| 148 | 148 | $filepath = $this->build_snippet_flat_file_path( $table_name, $snippet ); |
| 149 | 149 | |
| 150 | 150 | return file_exists( $filepath ) |
| @@ -177,8 +177,18 @@ | ||
| 177 | 177 | return ob_get_clean(); |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | + * Check if being rendered within the block editor. | |
| 182 | + * | |
| 183 | + * @return bool True if in the block editor, false otherwise. | |
| 184 | + */ | |
| 185 | + private function is_block_editor(): bool { | |
| 186 | + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
| 187 | + return $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor(); | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 181 | 191 | * Evaluate a snippet by loading its code from the filesystem. |
| 182 | 192 | * |
| 183 | 193 | * @param string $filepath Path to file to evaluate. |
| 184 | 194 | * @param array $atts Shortcode attributes. |
| @@ -209,14 +219,14 @@ | ||
| 209 | 219 | * @param bool $network Whether the snippet is network-wide. |
| 210 | 220 | * |
| 211 | 221 | * @return Snippet|null |
| 212 | 222 | */ |
| 213 | - private function get_content_snippet( int $id, bool $network ): ?Snippet { | |
| 223 | + private function get_snippet( int $id, bool $network ): ?Snippet { | |
| 214 | 224 | if ( ! Snippet_Files::is_active() ) { |
| 215 | 225 | return get_snippet( $id, $network ); |
| 216 | 226 | } |
| 217 | 227 | |
| 218 | - $validated_network = DB::validate_network_param( $network ); | |
| 228 | + $validated_network = validate_network_param( $network ); | |
| 219 | 229 | $table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $validated_network ) ); |
| 220 | 230 | $handler = code_snippets()->snippet_handler_registry->get_handler( 'html' ); |
| 221 | 231 | $config_filepath = Snippet_Files::get_base_dir( $table_name, $handler->get_dir_name() ) . '/index.php'; |
| 222 | 232 | |
| @@ -251,9 +261,9 @@ | ||
| 251 | 261 | 'network' => false, |
| 252 | 262 | 'php' => false, |
| 253 | 263 | 'format' => false, |
| 254 | 264 | 'shortcodes' => false, |
| 255 | - 'debug' => false, | |
| 265 | + 'debug' => $this->is_block_editor(), | |
| 256 | 266 | ], |
| 257 | 267 | $atts, |
| 258 | 268 | self::CONTENT_SHORTCODE |
| 259 | 269 | ); |
| @@ -262,9 +272,9 @@ | ||
| 262 | 272 | if ( ! $id ) { |
| 263 | 273 | return $this->invalid_id_warning( $id ); |
| 264 | 274 | } |
| 265 | 275 | |
| 266 | - $snippet = $this->get_content_snippet( $id, (bool) $atts['network'] ); | |
| 276 | + $snippet = $this->get_snippet( $id, (bool) $atts['network'] ); | |
| 267 | 277 | |
| 268 | 278 | // Render the source code if this is not a shortcode snippet. |
| 269 | 279 | if ( 'content' !== $snippet->scope ) { |
| 270 | 280 | return $snippet->id ? $this->render_snippet_source( $snippet ) : $this->invalid_id_warning( $snippet->id ); |
| @@ -271,26 +281,28 @@ | ||
| 271 | 281 | } |
| 272 | 282 | |
| 273 | 283 | // If the snippet is inactive, either display a message or render nothing. |
| 274 | 284 | if ( ! $snippet->active ) { |
| 275 | - if ( ! $atts['debug'] ) { | |
| 285 | + if ( $atts['debug'] ) { | |
| 286 | + /* translators: 1: snippet name, 2: snippet edit link */ | |
| 287 | + $text = __( '%1$s is currently inactive. You can <a href="%2$s">edit this snippet</a> to activate it and make it visible. This message will not appear in the published post.', 'code-snippets' ); | |
| 288 | + $snippet_name = '<strong>' . $snippet->name . '</strong>'; | |
| 289 | + $edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) ); | |
| 290 | + | |
| 291 | + $message = wp_kses( | |
| 292 | + sprintf( $text, $snippet_name, $edit_url ), | |
| 293 | + [ | |
| 294 | + 'strong' => [], | |
| 295 | + 'a' => [ | |
| 296 | + 'href' => [], | |
| 297 | + ], | |
| 298 | + ] | |
| 299 | + ); | |
| 300 | + | |
| 301 | + return "<p>$message</p><p>" . esc_html__( 'This message will not appear in the published post.', 'code-snippets' ) . '</p>'; | |
| 302 | + } else { | |
| 276 | 303 | return ''; |
| 277 | 304 | } |
| 278 | - | |
| 279 | - /* translators: 1: snippet name, 2: snippet edit link */ | |
| 280 | - $text = __( '%1$s is currently inactive. You can <a href="%2$s">edit this snippet</a> to activate it and make it visible. This message will not appear in the published post.', 'code-snippets' ); | |
| 281 | - $snippet_name = '<strong>' . $snippet->name . '</strong>'; | |
| 282 | - $edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) ); | |
| 283 | - | |
| 284 | - return wp_kses( | |
| 285 | - sprintf( $text, $snippet_name, $edit_url ), | |
| 286 | - [ | |
| 287 | - 'strong' => [], | |
| 288 | - 'a' => [ | |
| 289 | - 'href' => [], | |
| 290 | - ], | |
| 291 | - ] | |
| 292 | - ); | |
| 293 | 305 | } |
| 294 | 306 | |
| 295 | 307 | $content = $this->evaluate_shortcode_content( $snippet, $original_atts ); |
| 296 | 308 | |
| @@ -404,8 +416,8 @@ | ||
| 404 | 416 | if ( ! $id ) { |
| 405 | 417 | return $this->invalid_id_warning( $id ); |
| 406 | 418 | } |
| 407 | 419 | |
| 408 | - $snippet = $this->get_content_snippet( $id, (bool) $atts['network'] ); | |
| 420 | + $snippet = $this->get_snippet( $id, (bool) $atts['network'] ); | |
| 409 | 421 | return $this->render_snippet_source( $snippet, $atts ); |
| 410 | 422 | } |
| 411 | 423 | } |