| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Flat_Files\Handlers; |
| 4 |
|
| 5 |
use Code_Snippets\Flat_Files\Interfaces\Snippet_Type_Handler; |
| 6 |
|
| 7 |
/** |
| 8 |
* Snippet type handler for content snippets. |
| 9 |
*/ |
| 10 |
class Content_Snippet_Handler implements Snippet_Type_Handler { |
| 11 |
|
| 12 |
/** |
| 13 |
* Set 'php' as the file extension for content snippets, as they can contain embedded PHP code. |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public function get_file_extension(): string { |
| 18 |
return 'php'; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Store content snippets in an 'html' directory. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function get_dir_name(): string { |
| 27 |
return 'html'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Wrap content snippets by adding a header that disallows direct access. |
| 32 |
* |
| 33 |
* @param string $code Content snippet code. |
| 34 |
* |
| 35 |
* @return string Content snippet code with header prepended. |
| 36 |
*/ |
| 37 |
public function wrap_code( string $code ): string { |
| 38 |
return "<?php\n\nif ( ! defined( 'ABSPATH' ) ) { return; }\n\n?>\n\n" . $code; |
| 39 |
} |
| 40 |
} |
| 41 |
|