-
Notifications
You must be signed in to change notification settings - Fork 4
/
upfront-template-builder.php
217 lines (185 loc) · 5.79 KB
/
upfront-template-builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
Plugin Name: Upfront Template Helper
Plugin URI:
Description: generates php for upfront layouts
Author: Mike Saunders
Author URI: mikesaunders.co.uk
Version: 0.1
License: GPL
*/
class Upfront_Template_Builder
{
public function __construct() {
add_action( 'admin_menu', array($this, 'register_menu' ) );
add_action( "admin_enqueue_scripts", array($this,"queue_scripts" ));
}
function register_menu() {
add_menu_page( 'Upfront Template Helper', 'Template Builder', 'manage_options', 'upfront-template-helper', array($this, 'upfront_template_helper_admin_page' ), '', "21.4" );
}
function queue_scripts($hook)
{
if ($hook == "toplevel_page_upfront-template-helper")
{
wp_enqueue_style('highlightjs', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.8.0/styles/default.min.css');
wp_enqueue_script('highlightjs', '//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.8.0/highlight.min.js');
}
}
function tidy_array($arr)
{
return preg_replace('(\d+\s=>)', "", var_export($arr, true));
}
//experimented with making it look nice but didn't bother
function format_php_export($arrayRep) {
$arrayRep = preg_replace('/[ ]{2}/', "\t", $arrayRep);
$arrayRep = preg_replace("/\=\>[ \n\t]+array[ ]+\(/", '=> array(', $arrayRep);
return $arrayRep = preg_replace("/\n/", "\n\t", $arrayRep);
}
function upfront_template_helper_admin_page()
{
$specificity = $_POST["template"];
$json_data = array();
$codeOutput = "";
$storageKey = "";
if ($specificity != "")
{
$layout_ids = array(
'specificity' => $specificity
);
$layout = Upfront_Layout::from_entity_ids($layout_ids);
$storageKey = $layout->get_storage_key();
// $layout_data = $layout->to_php();
// $properties = $layout->get_layout_properties();
$regionNames = array();
$regionForm = "";
// EDIT:: I have changed it to load directly from the database because the Upfront classes merge data with global settings and it causes trouble
$json_data = json_decode( get_option($storageKey.'-'.$specificity, json_encode(array())), true );
foreach($json_data["regions"] as $region)
{
$regionNames[$region["name"]] = $region["title"];
$regionForm .= '<div class="region-check"><label for="'.$region["name"].'">'.$region["title"].'</label><input type="checkbox" id="'.$region["name"].'" name="'.$region["name"].'"> </div>
';
}
$exportRegionNames = array();
foreach($_POST as $key => $value)
{
if ($value == 'on') $exportRegionNames[] = $key;
}
$exportRegions = array();
foreach($json_data["regions"] as $region)
{
if(in_array($region["name"],$exportRegionNames))
{
$exportRegions[] = $region;
}
}
$codeOutput = "";
// create php output
// create region args & properties
//
foreach($exportRegions as $region)
{
$properties = $region["properties"];
$modules = $region["modules"];
$wrappers = $region["wrappers"];
//unset($region["properties"]);
//unset($region["modules"]);
//unset($region["wrappers"]);
$codeOutput .= '$'.str_replace("-","_",$region["name"]).' = new Upfront_Virtual_Region_From_Existing('.var_export($region,true).');
';
// this doesn't work becuase it is expecting values to build its own wrappers, not pre-exisiting wrappers
// foreach($modules as $module)
// {
// $codeOutput .= '$'.str_replace("-","_",$region["name"]).'->add_element('.var_export($module,true).');
//';
// }
$codeOutput .= '$regions->add($'.str_replace("-","_",$region["name"]).');
';
}
$codeOutput = highlight_string($codeOutput,true);
}
?>
<script>hljs.initHighlightingOnLoad();</script>
<h2>Regions in this menu</h2>
<form method="post" action="admin.php?page=upfront-template-helper">
<table class="form-table">
<tr>
<td><label for="template-specificity">Template:</label></td>
<td><input type="text" id="template-specificity" name="template" class="regular-text" value="<?php echo $specificity; ?>"></td>
</tr>
<tr>
<td>
<p class="submit">
<input type="submit" id="submit" class="button button-primary" value="Load Data">
</p>
</td>
</tr>
</table>
<label for="template-specificity">
</label>
</form>
<table class="widefat">
<thead>
<tr>
<th class="row-title">Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td class="row-title"><label for="tablecell">Template: </label></td>
<td><?php echo $specificity ?> </td>
</tr>
<tr>
<td class="row-title"><label for="tablecell">Storage Key: </label></td>
<td><?php echo $storageKey ?> </td>
</tr>
<tr>
<td class="row-title"><label for="tablecell">Regions: </label></td>
<td><?php echo count($json_data["regions"]); ?> </td>
</tr>
<tr>
<td class="row-title"><label for="tablecell">Properties: </label></td>
<td><?php echo count($json_data["properties"]); ?> </td>
</tr>
<tr>
<td class="row-title"><label for="tablecell">Wrappers: </label></td>
<td><?php echo count($json_data["wrappers"]); ?> </td>
</tr>
</tbody>
</table>
<style>
.region-check{
display: inline-block;
margin: 5px;
padding: 5px;
background-color: #fff;
}
.upfront-regions > div > input[type="checkbox"]{
margin-left: 5px;
vertical-align: middle;
}
.upfront-regions > div > label{
margin: 5px;
padding:2px;
}
.upfront-regions{
}
</style>
<form class="upfront-regions" action="admin.php?page=upfront-template-helper" method="post">
<input type="hidden" name="template" value="<?php echo $specificity; ?>">
<?php echo $regionForm; ?>
<div><input class="button button-secondary" type="submit" id="submit" value="Export"></div>
</form>
<div>
<pre class="php">
<?php echo $codeOutput; ?>
</pre>
</div>
<?php
}
}
if(is_admin())
{
$utb = new Upfront_Template_Builder();
}