ProductTemplates
4 weeks ago
BlockRegistry.php
4 weeks ago
BlockTemplateUtils.php
4 weeks ago
Init.php
4 weeks ago
ProductFormsController.php
4 weeks ago
ProductTemplate.php
4 weeks ago
RedirectionController.php
4 weeks ago
Tracks.php
4 weeks ago
BlockTemplateUtils.php
111 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\ProductBlockEditor; |
| 4 | |
| 5 | use Automattic\WooCommerce\LayoutTemplates\LayoutTemplateRegistry; |
| 6 | |
| 7 | /** |
| 8 | * Utils for block templates. |
| 9 | * |
| 10 | * @deprecated 10.9.0 Product editor extension APIs will be removed in WooCommerce 11.0. |
| 11 | */ |
| 12 | class BlockTemplateUtils { |
| 13 | /** |
| 14 | * Directory which contains all templates |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | const TEMPLATES_ROOT_DIR = 'templates'; |
| 19 | |
| 20 | /** |
| 21 | * Directory names. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | const DIRECTORY_NAMES = array( |
| 26 | 'TEMPLATES' => 'product-form', |
| 27 | 'TEMPLATE_PARTS' => 'product-form/parts', |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Gets the directory where templates of a specific template type can be found. |
| 32 | * |
| 33 | * @param string $template_type wp_template or wp_template_part. |
| 34 | * @return string |
| 35 | */ |
| 36 | private static function get_templates_directory( $template_type = 'wp_template' ) { |
| 37 | $root_path = dirname( __DIR__, 4 ) . '/' . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR; |
| 38 | $templates_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATES']; |
| 39 | $template_parts_directory = $root_path . self::DIRECTORY_NAMES['TEMPLATE_PARTS']; |
| 40 | |
| 41 | if ( 'wp_template_part' === $template_type ) { |
| 42 | return $template_parts_directory; |
| 43 | } |
| 44 | |
| 45 | return $templates_directory; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Return the path to a block template file. |
| 50 | * Otherwise, False. |
| 51 | * |
| 52 | * @param string $slug - Template slug. |
| 53 | * @return string|bool Path to the template file or false. |
| 54 | */ |
| 55 | public static function get_block_template_path( $slug ) { |
| 56 | $directory = self::get_templates_directory(); |
| 57 | $path = trailingslashit( $directory ) . $slug . '.php'; |
| 58 | |
| 59 | if ( ! file_exists( $path ) ) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return $path; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get the template data from the headers. |
| 68 | * |
| 69 | * @param string $file_path - File path. |
| 70 | * @return array Template data. |
| 71 | */ |
| 72 | public static function get_template_file_data( $file_path ) { |
| 73 | if ( ! file_exists( $file_path ) ) { |
| 74 | return array(); |
| 75 | } |
| 76 | |
| 77 | $file_data = get_file_data( |
| 78 | $file_path, |
| 79 | array( |
| 80 | 'title' => 'Title', |
| 81 | 'slug' => 'Slug', |
| 82 | 'description' => 'Description', |
| 83 | 'product_types' => 'Product Types', |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | $file_data['product_types'] = explode( ',', trim( $file_data['product_types'] ) ); |
| 88 | |
| 89 | return $file_data; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the template content from the file. |
| 94 | * |
| 95 | * @param string $file_path - File path. |
| 96 | * @return string Content. |
| 97 | */ |
| 98 | public static function get_template_content( $file_path ) { |
| 99 | if ( ! file_exists( $file_path ) ) { |
| 100 | return ''; |
| 101 | } |
| 102 | |
| 103 | ob_start(); |
| 104 | include $file_path; |
| 105 | $content = ob_get_contents(); |
| 106 | ob_end_clean(); |
| 107 | |
| 108 | return $content; |
| 109 | } |
| 110 | } |
| 111 |