PluginProbe
YayMail – WooCommerce Email Customizer / 4.3.2
YayMail – WooCommerce Email Customizer v4.3.2
4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 3.4.2 3.4.3 All 55 releases
yaymail / src / TemplatePatterns / convert-tool.php

convert-tool.php in YayMail – WooCommerce Email Customizer 4.3.2, at src/TemplatePatterns/convert-tool.php

211 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function convert_element_type_to_class_name( $element_type ) {
4 $element_class_name = str_replace( '_', ' ', $element_type );
5 $element_class_name = ucwords( $element_class_name );
6 $element_class_name = str_replace( ' ', '', $element_class_name );
7 return $element_class_name;
8 }
9
10 function convert_json_elements_to_php( $json, $class_name, $section_type, $pattern_type, $pattern_name, $position = 10 ) {
11 $elements = json_decode( $json, true );
12
13 // Start building the PHP code
14 $php = "<?php\nnamespace YayMail\\TemplatePatterns\\Patterns;\n\n";
15
16 // Add imports
17 $php .= "use YayMail\\Abstracts\\BasePattern;\n";
18
19 // Collect all element types for imports (including nested elements)
20 $element_types = collect_all_element_types( $elements );
21 foreach ( $element_types as $element_type ) {
22 $element_class_name = convert_element_type_to_class_name( $element_type );
23 $php .= "use YayMail\\Elements\\{$element_class_name};\n";
24 }
25
26 $php .= "use YayMail\\TemplatePatterns\\SectionTemplates\\{$section_type};\n";
27 $php .= "use YayMail\\Utils\\SingletonTrait;\n\n";
28
29 // Class definition
30 $php .= "/**\n * {$class_name} Class\n */\n";
31 $php .= "class {$class_name} extends BasePattern {\n";
32 $php .= " use SingletonTrait;\n\n";
33 $php .= " public const TYPE = '{$pattern_type}';\n\n";
34
35 // Constructor
36 $php .= " public function __construct() {\n";
37 $php .= " \$this->id = uniqid();\n";
38 $php .= " \$this->section = {$section_type}::TYPE;\n";
39 $php .= " \$this->position = {$position};\n";
40 $php .= " \$this->name = __('{$pattern_name}', 'yaymail');\n";
41 $php .= " \$this->elements = [\n";
42
43 // Add each element
44 foreach ( $elements as $element ) {
45 $php .= process_element( $element, 3 );
46 }
47
48 // Close elements array and class
49 $php .= " ];\n";
50 $php .= " }\n";
51 $php .= "}\n";
52
53 return $php;
54 }
55
56 function collect_all_element_types( $elements ) {
57 $types = [];
58
59 foreach ( $elements as $element ) {
60 // Add this element's type
61 if ( ! in_array( $element['type'], $types ) ) {
62 $types[] = $element['type'];
63 }
64
65 // Process children if they exist
66 if ( isset( $element['children'] ) && is_array( $element['children'] ) ) {
67 $child_types = collect_all_element_types( $element['children'] );
68 foreach ( $child_types as $type ) {
69 if ( ! in_array( $type, $types ) ) {
70 $types[] = $type;
71 }
72 }
73 }
74 }
75
76 return $types;
77 }
78
79 function process_element( $element, $indent_level ) {
80 $element_class_name = convert_element_type_to_class_name( $element['type'] );
81 $indentation = str_repeat( ' ', $indent_level );
82 $php = '';
83
84 // Special case for ColumnLayout
85 if ( $element['type'] === 'column_layout' ) {
86 $columns_count = $element['data']['amount_of_columns'] ?? count( $element['children'] ?? [] );
87 $php .= "{$indentation}{$element_class_name}::get_object_data(\n";
88 $php .= "{$indentation} {$columns_count},\n";
89 $php .= "{$indentation} [\n";
90
91 // Process normal attributes
92 foreach ( $element['data'] as $key => $value ) {
93 if ( $key !== 'amount_of_columns' && $key !== 'children' ) {
94 $php .= convert_value_to_php( $key, $value, $indent_level + 1 );
95 }
96 }
97
98 // Process children if they exist
99 if ( isset( $element['children'] ) && ! empty( $element['children'] ) ) {
100 $php .= "{$indentation} 'children' => [\n";
101 foreach ( $element['children'] as $child ) {
102 $php .= process_element( $child, $indent_level + 3 );
103 }
104 $php .= "{$indentation} ],\n";
105 }
106
107 $php .= "{$indentation} ]\n";
108 $php .= "{$indentation}),\n";
109 }//end if
110 // Special case for Column
111 elseif ( $element['type'] === 'column' ) {
112 $width = $element['data']['width'] ?? 50;
113 // Default width if not provided
114 $php .= "{$indentation}{$element_class_name}::get_object_data(\n";
115 $php .= "{$indentation} {$width},\n";
116 $php .= "{$indentation} [\n";
117
118 // Process children if they exist
119 if ( isset( $element['children'] ) && ! empty( $element['children'] ) ) {
120 $php .= "{$indentation} 'children' => [\n";
121 foreach ( $element['children'] as $child ) {
122 $php .= process_element( $child, $indent_level + 3 );
123 }
124 $php .= "{$indentation} ],\n";
125 }
126
127 $php .= "{$indentation} ]\n";
128 $php .= "{$indentation}),\n";
129 }
130 // Regular elements
131 else {
132 $php .= "{$indentation}{$element_class_name}::get_object_data(\n";
133 $php .= "{$indentation} [\n";
134
135 // Process data attributes
136 if ( isset( $element['data'] ) ) {
137 foreach ( $element['data'] as $key => $value ) {
138 $php .= convert_value_to_php( $key, $value, $indent_level + 1 );
139 }
140 }
141
142 $php .= "{$indentation} ]\n";
143 $php .= "{$indentation}),\n";
144 }
145
146 return $php;
147 }
148
149 function convert_value_to_php( $key, $value, $indent ) {
150 $indentation = str_repeat( ' ', $indent * 4 );
151 $php = '';
152
153 if ( is_array( $value ) ) {
154 $php .= "{$indentation}'{$key}' => [\n";
155
156 foreach ( $value as $sub_key => $sub_value ) {
157 if ( is_array( $sub_value ) ) {
158 $php .= convert_value_to_php( $sub_key, $sub_value, $indent + 1 );
159 } else {
160 if ( is_string( $sub_value ) ) {
161 // Escape apostrophes and backslashes for PHP single-quoted strings
162 $escaped_value = str_replace( [ '\\', "'" ], [ '\\\\', "\\'" ], $sub_value );
163 $formatted_value = "'{$escaped_value}'";
164 } else {
165 $formatted_value = $sub_value;
166 }
167 $php .= str_repeat( ' ', ( $indent + 1 ) * 4 ) . "'{$sub_key}' => {$formatted_value},\n";
168 }
169 }
170
171 $php .= "{$indentation}],\n";
172 } else {
173 if ( is_string( $value ) ) {
174 // Escape apostrophes and backslashes for PHP single-quoted strings
175 $escaped_value = str_replace( [ '\\', "'" ], [ '\\\\', "\\'" ], $value );
176 $formatted_value = "'{$escaped_value}'";
177 } else {
178 $formatted_value = $value;
179 }
180 $php .= "{$indentation}'{$key}' => {$formatted_value},\n";
181 }//end if
182
183 return $php;
184 }
185
186 function create_pattern_file( $json_string, $section_type, $pattern_type, $pattern_name, $position = 10 ) {
187 // Generate the PHP code
188 $class_name = convert_element_type_to_class_name( $pattern_type );
189 $php_code = convert_json_elements_to_php( $json_string, $class_name, $section_type, $pattern_type, $pattern_name, $position );
190
191 // Define the file path
192 $patterns_dir = './Patterns';
193 $file_path = $patterns_dir . '/' . $class_name . '.php';
194
195 // Write the PHP code to file
196 if ( file_put_contents( $file_path, $php_code ) ) {
197 return true;
198 } else {
199 return "Failed to write pattern file: {$file_path}";
200 }
201 }
202
203 // Example usage (uncomment to use)
204 create_pattern_file(
205 file_get_contents( './tool-json.json' ),
206 'Banner',
207 'banner_23',
208 'Banner 23',
209 $position = 20
210 );
211