-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.php
497 lines (427 loc) · 12.9 KB
/
functions.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
$themecolors = array(
'bg' => '000000',
'text' => 'bfbfbf',
'link' => 'ffffff',
'border' => '000000'
);
// this varies but the single page content width seems to be 607px max
$content_width = 600;
class Hemingway
{
var $raw_blocks;
var $available_blocks;
var $style;
var $version;
function add_available_block($block_name, $block_ref)
{
$blocks = $this->available_blocks;
if (!$blocks[$block_ref]){
$blocks[$block_ref] = $block_name;
update_option('hem_available_blocks', $blocks);
wp_cache_flush();
}
}
function get_available_blocks()
// This function returns an array of available blocks
// in the format of $arr[block_ref] = block_name
{
$this->available_blocks = get_option('hem_available_blocks');
return $this->available_blocks;
}
function get_block_contents($block_place)
// Returns an array of block_refs in specififed block
{
if (!$this->raw_blocks){
$this->raw_blocks = get_option('hem_blocks');
}
return $this->raw_blocks[$block_place];
}
function add_block_to_place($block_place, $block_ref)
{
$block_contents = $this->get_block_contents($block_place);
if (in_array($block_ref, $block_contents))
return true;
$block_contents[] = $block_ref;
$this->raw_blocks[$block_place] = $block_contents;
update_option('hem_blocks', $this->raw_blocks);
wp_cache_flush(); // I was having caching issues
return true;
}
function remove_block_in_place($block_place, $block_ref)
{
$block_contents = $this->get_block_contents($block_place);
if (!in_array($block_ref, $block_contents))
return true;
$key = array_search($block_ref, $block_contents);
unset($block_contents[$key]);
$this->raw_blocks[$block_place] = $block_contents;
update_option('hem_blocks', $this->raw_blocks);
wp_cache_flush(); // I was having caching issues
return true;
}
// Templating functions
function get_block_output($block_place)
{
$blocks = $this->get_block_contents($block_place);
foreach($blocks as $key => $block ){
include (TEMPLATEPATH . '/blocks/' . $block . '.php');
}
}
function get_style(){
$this->style = get_option('hem_style');
}
}
$hemingway = new Hemingway();
$hemingway->get_available_blocks();
$hemingway->get_style();
$hemingway->version = "0.13";
// Options
$default_blocks = Array(
'recent_entries' => 'Recent Entries',
'about_page' => 'About Page',
'category_listing' => 'Category Listing',
'blogroll' => 'Blogroll',
'pages' => 'Pages',
'monthly_archives' => 'Monthly Archives'
);
$default_block_locations = Array(
'block_1' => Array('about_page'),
'block_2' => Array('recent_entries'),
'block_3' => Array('category_listing'),
'block_4' => Array(),
'block_5' => Array(),
'block_6' => Array()
);
if (!get_option('hem_version') || get_option('hem_version') < $hemingway->version){
// Hemingway isn't installed, so we'll need to add options
if (!get_option('hem_version') )
add_option('hem_version', $hemingway->version, 'Hemingway Version installed');
else
update_option('hem_version', $hemingway->version);
if (!get_option('hem_available_blocks') )
add_option('hem_available_blocks', $default_blocks, 'A list of available blocks for Hemingway');
if (!get_option('hem_blocks') )
add_option('hem_blocks', $default_block_locations, 'An array of blocks and their contents');
if (!get_option('hem_style') )
add_option('hem_style', '', 'Location of custom style sheet');
}
// Ajax Stuff
if ($_GET['hem_action'] == 'add_block'){
$block_ref = $_GET['block_ref'];
$block_place = $_GET['block_place'];
$block_name = $hemingway->available_blocks[$block_ref];
$hemingway->add_block_to_place($block_place, $block_ref);
ob_end_clean(); // Kill preceding output
$output = '<ul>';
foreach($hemingway->get_block_contents($block_place) as $key => $block_ref){
$block_name = $hemingway->available_blocks[$block_ref];
$output .= '<li>' . $block_name . ' (<a href="#" onclick="remove_block(\'' . $block_place . '\', \'' . $block_ref . '\');">remove</a>)</li>';
}
$output .= '</ul>';
echo $output;
exit(); // Kill any more output
}
if ($_GET['hem_action'] == 'remove_block'){
$block_ref = $_GET['block_ref'];
$block_place = $_GET['block_place'];
$hemingway->remove_block_in_place($block_place, $block_ref);
ob_end_clean(); // Kill preceding output
$output = '<ul>';
foreach($hemingway->get_block_contents($block_place) as $key => $block_ref){
$block_name = $hemingway->available_blocks[$block_ref];
$output .= '<li>' . $block_name . ' (<a href="#" onclick="remove_block(\'' . $block_place . '\', \'' . $block_ref . '\');">remove</a>)</li>';
}
$output .= '</ul>';
echo $output;
exit(); // Kill any more output
}
if ($_POST['custom_styles']){
update_option('hem_style', $_POST['custom_styles']);
wp_cache_flush();
$message = 'Styles updated!';
}
if ($_POST['block_ref']){
$hemingway->add_available_block($_POST['display_name'], $_POST['block_ref']);
$hemingway->get_available_blocks();
$message = 'Block added!';
}
// Stuff
add_action ('admin_menu', 'hemingway_menu');
$hem_loc = '../themes/' . basename(dirname($file));
function hemingway_scripts() {
$dir = get_bloginfo('template_directory');
wp_enqueue_script('prototype');
wp_enqueue_script('dragdrop', $dir . '/admin/js/dragdrop.js', false, 1);
wp_enqueue_script('effects', $dir . '/admin/js/effects.js', false, 1);
}
function hemingway_menu() {
$page = add_submenu_page('themes.php', 'Hemingway Options', 'Hemingway Options', 5, $hem_loc . 'functions.php', 'menu');
add_action('load-' . $page, 'hemingway_scripts');
}
function menu() {
global $hem_loc, $hemingway, $message;
?>
<!--
Okay, so I don't honestly know how legit this is, but I want a more intuitive interface
so I'm going to import scriptaculous. There's a good chance this is going to mess stuff up
for some people :)
-->
<script type="text/javascript">
function remove_block(block_place, block_ref){
url = 'themes.php?page=functions.php&hem_action=remove_block&block_place=' + block_place + '&block_ref=' + block_ref;
new Ajax.Updater(block_place, url,
{
evalScripts:true, asynchronous:true
}
)
}
</script>
<style>
.block{
width:200px;
height:200px;
border:1px solid #CCC;
float:left;
margin:20px 1em 20px 0;
padding:10px;
display:inline;
}
.block ul{
padding:0;
margin:0;
}
.block ul li{
margin:0 0 5px 0;
list-style-type:none;
}
.block-active{
border:1px solid #333;
background:#F2F8FF;
}
#addables li{
list-style-type:none;
margin:1em 1em 1em 0;
background:#EAEAEA;
border:1px solid #DDD;
padding:3px;
width:215px;
float:left;
cursor:move;
}
ul#addables{
margin:0;
padding:0;
width:720px;
position:relative;
}
</style>
<? if($message) : ?>
<div id="message" class="updated fade"><p><?=$message?></p></div>
<? endif; ?>
<div class="wrap" style="position:relative;">
<h2><?php _e('Hemingway Options'); ?></h2>
<h3>Color Options</h3>
<p>Choose a primary color for your site:</p>
<form name="dofollow" action="" method="post">
<input type="hidden" name="page_options" value="'dofollow_timeout'" />
<p><label><input name="custom_styles" type="radio" value="none" <?php if ($hemingway->style == 'none') echo 'checked="checked"'; ?> />
Black</label></p>
<p><label><input name="custom_styles" type="radio" value="white.css" <?php if ($hemingway->style == 'white.css') echo 'checked="checked"'; ?> /> White</label></p>
<input type="submit" value="Update Color »" />
</form>
<h3>Hemingway's Bottombar™</h3>
<p>Drag and drop the different blocks into their place below. After you drag the block to the area, it will update with the new contents automatically.</p>
<ul id="addables">
<? foreach($hemingway->available_blocks as $ref => $name) : ?>
<li id="<?= $ref ?>" class="blocks"><?= $name ?></li>
<script type="text/javascript">new Draggable('<?= $ref ?>', {revert:true})</script>
<? endforeach; ?>
</ul>
<div class="clear"></div>
<div class="block" id="block_1">
<ul>
<?
foreach($hemingway->get_block_contents('block_1') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_1', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_1', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_1', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_1&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
<div class="block" id="block_2">
<ul>
<?
foreach($hemingway->get_block_contents('block_2') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_2', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_2', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_2', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_2&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
<div class="block" id="block_3">
<ul>
<?
foreach($hemingway->get_block_contents('block_3') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_3', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_3', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_3', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_3&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
<!-- Maybe later...
<div class="clear"></div>
<div class="block" id="block_4">
Block 4
<ul>
<?
foreach($hemingway->get_block_contents('block_4') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_4', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_4', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_4', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_4&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
<div class="block" id="block_5">
Block 5
<ul>
<?
foreach($hemingway->get_block_contents('block_5') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_5', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_5', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_5', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_5&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
<div class="block" id="block_6">
Block 6
<ul>
<?
foreach($hemingway->get_block_contents('block_6') as $key => $block_ref) :
$block_name = $hemingway->available_blocks[$block_ref];
?>
<li><?= $block_name ?> (<a href="#" onclick="remove_block('block_6', '<?=$block_ref?>');">remove</a>)</li>
<? endforeach; ?>
</ul>
</div>
<script type="text/javascript">
Droppables.add(
'block_6', {
accept:'blocks',
onDrop:function(element){
new Ajax.Updater('block_6', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_6&block_ref=' + element.id,
{
evalScripts:true, asynchronous:true
}
)
},
hoverclass:'block-active'
}
)
</script>
-->
<div class="clear"></div>
<?php
$blocks_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_template() . '/blocks');
if ($blocks_dir) {
while(($file = $blocks_dir->read()) !== false) {
if (!preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file))
$blocks_files[] = $file;
}
}
if ($blocks_dir || $blocks_files) {
foreach($blocks_files as $blocks_file) {
$block_ref = preg_replace('/\.php/', '', $blocks_file);
if (!array_key_exists($block_ref, $hemingway->available_blocks)){
?>
<h3>You have uninstalled blocks!</h3>
<p>Give the block <strong><?=$block_ref ?></strong> a display name (such as "About Page")</p>
<form action="" name="dofollow" method="post">
<input type="hidden" name="block_ref" value="<?=$block_ref?>" />
<?=$block_ref ?> : <input type="text" name="display_name" />
<input type="submit" value="Save" />
</form>
<?
}
}
}
?>
</div>
<?php
}
?>