-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
860 lines (717 loc) · 29.1 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
<?php
define("ELEGANT_GRUNGE_FRAME_MAX_WIDTH", 415);
define("ELEGANT_GRUNGE_FRAME_SMALL_WIDTH", 140);
define("ELEGANT_GRUNGE_FRAME_SMALL_HEIGHT", 140);
define("ELEGANT_GRUNGE_FRAME_MIN_WIDTH", 25);
define("ELEGANT_GRUNGE_FRAME_MIN_HEIGHT", 25);
if ( function_exists('register_sidebar') ) {
if ( get_option('page_setup') != 'no-sidebar' ) {
register_sidebar(array(
'name' => 'Sidebar',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
}
if ( get_option('page_setup') == 'double-right-sidebar' ) {
register_sidebar(array(
'name' => 'Sidebar 2',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
}
register_sidebar(array(
'name' => 'Footer',
'before_widget' => '<div class="widget-wrap"><div class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
if( !function_exists('array_combine') ) {
function array_combine($a, $b) {
$c = array();
$at = array_values($a);
$bt = array_values($b);
foreach($at as $key=>$aval) $c[$aval] = $bt[$key];
return $c;
}
}
/**
* Add frames
*
* Filters content, adding frames to all objects with class 'frame', and all images if
* corresponding option is set.
*/
function elegant_grunge_filter( $content , $arg2=null, $arg3=null ) {
global $post;
// Not for feeds
if ( is_feed() ) return $content;
// Look-ahead for class 'frame'
$frameClass = '(?=[^>]+class=["\'][^"\']*frame)';
// Skipped classes
$classes = "(?:".join("|", array_map("trim", explode(",",get_option('frame_class_skip')))).")";
$skippedClasses = '(?![^>]+class=["\'][^"\']*'.$classes.')';
// Content which we want to include inside the frame
$aStart = '(?:<\s*a[^>]+>\s*)?';
$aEnd = '(?:\s*</a\s*>)?';
$caption = '(?:\s*<p class="wp-caption-text">.*?</p>)?';
// Beginning tag, including class check
$startTagWithFrameClass = "<\s*((?:(img)|[a-zA-Z]+))${skippedClasses}${frameClass}[^>]*";
// End of tag: Short form
$endSingleTag = '(?(2)\s*/?>|\s*/>)';
// End of tag: Long form
$endOriginalTag = '</\s*\\g{1}\s*>';
// Any tag
$anyStartTag = '<\s*([a-z]+)[^>]*';
$endLastTag = '</\s*\\g{-1}\s*>';
// Nested content - tags within tags
$nestedContent = "([^<]+|(?:$anyStartTag(?:$endSingleTag|>(?-2)*$endLastTag)))*";
// Composite expression - look for a block with class of 'frame', and include all content
$regex = "$aStart$startTagWithFrameClass(?:$endSingleTag|>$nestedContent$endOriginalTag)$aEnd$caption";
$regexes = array("@".$regex."@is");
// Also replace all images
$frame_all_images = get_option("frame_all_images");
$frame_all_images_post = get_post_meta($post->ID, 'frame_all_images', true);
if ( $frame_all_images_post == "true" )
$frame_all_images = true;
else if ( $frame_all_images_post == "false" )
$frame_all_images = false;
if ( $frame_all_images ) {
// Look-ahead for not class of frame (becuase we caught these above)
$notFrameClass = '(?![^>]+class=["\'][^"\']*frame)';
// Composite expression - any images not with class of 'frame'
$regex = "$aStart\s*<\s*(img)${notFrameClass}${skippedClasses}[^>]+>\s*$aEnd$caption";
$regexes[] = "@".$regex."@is";
}
// Perform replacement with helper function below
$newContent = @preg_replace_callback($regexes, 'elegant_grunge_replace', $content);
if ( !$newContent ) {
return $content;
}
return $newContent;
}
/**
* Filter helper function
*
* Perform replacements for blocks discovered in the filter function above.
* Adds surrounding divs for frame, styled according to original block,
* resizes too-large images, and ignores entire block if it is a too-small
* image (because it will look weird, otherwise)
*/
function elegant_grunge_replace($matches) {
$inner = $matches[0];
// Look for align and style attributes
$align = '(^(?:<\s*a[^>]+>)?\s*<[^>]*?)align=["\'](left|right)["\']';
$style = '(^(?:<\s*a[^>]+>)?\s*<[^>]*?)style=["\']([^"\']+)["\']';
$class = '(^(?:<\s*a[^>]+>)?\s*<[^>]*?)class=["\']([^"\']+)["\']';
$styles = "";
if ( preg_match( "@$align@is", $inner, $newmatch ) ) {
// Align attribute found: Add an equivalent float
$styles .= "float: ".$newmatch[2].";";
}
if ( preg_match( "@$style@is", $inner, $newmatch ) ) {
// Style attribute found: Remember content, but minus some undesirable elements
$styles .= preg_replace("@(border(-[a-z]+)?|padding(-[a-z]+)?|margin-([a-z]+)?)\s*:[^;\"']*;?@is", "", $newmatch[2]);
}
if ( preg_match( "@$class@is", $inner, $newmatch ) ) {
// Style attribute found: Remember content
$classes = trim(preg_replace("@(?<![a-z])frame(?!=[a-z])@is", "", $newmatch[2]));
}
// Check width and height
$widthRegex = '@(^(?:<\s*a[^>]+>)?\s*<[^>]*?(?:width=[\'"]|width:\s*))([0-9]+)@is';
$heightRegex = '@(^(?:<\s*a[^>]+>)?\s*<[^>]*?(?:height=[\'"]|height:\s*))([0-9]+)@is';
if ( preg_match( $widthRegex, $inner, $newmatch ) ) {
$width = $newmatch[2];
}
if ( preg_match( $heightRegex, $inner, $newmatch ) ) {
$height = $newmatch[2];
}
if ( ( $width && $width < ELEGANT_GRUNGE_FRAME_MIN_WIDTH ) || ( $height && $height < ELEGANT_GRUNGE_FRAME_MIN_HEIGHT ) ) {
// Image is too small - just skip this one: return original content
return $inner;
}
if ( $width && $width > ELEGANT_GRUNGE_FRAME_MAX_WIDTH ) {
// Image is too large - scale down proportionately
if ( $height ) {
$ratio = $width / $height;
$height = round(ELEGANT_GRUNGE_FRAME_MAX_WIDTH / $ratio);
// Replace height value
$inner = preg_replace( $heightRegex, "\${1}$height", $inner );
}
$width = ELEGANT_GRUNGE_FRAME_MAX_WIDTH;
// Replace width value
$inner = preg_replace( $widthRegex, "\${1}$width", $inner );
}
$small = '';
if ( ( $width && $width < ELEGANT_GRUNGE_FRAME_SMALL_WIDTH ) || ( $height && $height < ELEGANT_GRUNGE_FRAME_SMALL_HEIGHT ) ) {
// Image is too small for the large frame - use the small frame
$small = ' small';
}
// Wrap content, and remove align/style from inner tag
return '<span class="frame-outer '.$small.' '.$classes.'"'.($styles ? ' style="'.trim($styles).'"' : '' ).'><span><span><span><span>' .
preg_replace(array("@${align}@is","@${style}@is"), '\\1', $inner).
'</span></span></span></span></span>';
}
// Photoblog routines
/**
* Prepare image from post
*
* Scans post for images and sets a few variables on post object
*/
function image_setup(&$post) {
if ( !preg_match("@(?:<a([^>]+?)/?>\s*)?<img([^>]+?)/?>(?:\s*</a>)?@", $post->post_content, $matches) ) {
return;
}
$tagRegex = "@([a-zA-Z]+)(?:=([\"'])(.*?)\\2)@";
if ( !preg_match_all($tagRegex, $matches[2], $tag) ) {
return;
}
$image = array_combine($tag[1], $tag[3]);
if ( $matches[1] && preg_match_all($tagRegex, $matches[1], $tag) ) {
$link = array_combine($tag[1], $tag[3]);
}
if ( !$image["src"] ) {
return;
}
$post->thumb_url = $post->image_url = clean_url( $image["src"], 'raw' );
$post->image_tag = $matches[0];
if ( $link["href"] && preg_match("/jpg|jpeg|jpe|png|gif/i", $link["href"]) ) {
$post->image_url = $link["href"];
}
$post->image_dimensions = array("width" => $image["width"], "height" => $image["height"]);
$post->image_info = $image;
$post->image_link_info = $link;
$description = trim(strip_tags(preg_replace("/\[[a-zA-Z][^\]]+\]/", "", $post->post_content)));
if ( strlen($description) > 250 ) $description = substr($description, 0, 250)."...";
$post->image_info["description"] = $description;
}
/**
* Template tag: Get the image from the post
*
* @param return boolean If true, returns the image tag instead of printing it
*/
function the_image($return = null) {
global $post;
if(!$post->image_tag) {
image_setup($post);
}
if ($return)
return $post->image_tag;
else
echo $post->image_tag;
}
/**
* Template tag: Get the image URL from the post
*
* @param return boolean If true, returns the image URL instead of printing it
*/
function the_image_url($return = null) {
global $post;
if(!$post->image_url) {
image_setup($post);
}
if ($return)
return $post->image_url;
else
echo $post->image_url;
}
/**
* Template tag: Get the thumbnail URL from the post
*
* @param return boolean If true, returns the thumb URL instead of printing it
*/
function the_image_thumb_url($return = false) {
global $post;
if(!$post->thumb_url) {
image_setup($post);
}
if ($return)
return $post->thumb_url;
else
echo $post->thumb_url;
}
/**
* Get post image information
*
* @returns Information about the image
*/
function the_image_info() {
global $post;
if(!$post->thumb_url) {
image_setup($post);
}
return $post->image_info;
}
/**
* Template tag: Determine if post has an image
*
* @returns True if an image exists, false otherwise
*/
function has_image() {
return (the_image_info() != null);
}
/**
* Template tag: Get the scaled thumbnail
*
* Attempts to create a new image derived from the original image and
* scaled down to width x height. Will crop out of center of image if
* aspect ratio does not match
*
* @param width int Width of thumbnail
* @param height int Height of thumbnail
* @returns Path to scaled thumbnail, or false on failure
*/
function the_image_scaled_thumb_url($width, $height) {
global $post;
$thumb = the_image_thumb_url(true);
if ( !$thumb ) return false;
if ( substr($thumb, 0, strlen(WP_CONTENT_URL)) == WP_CONTENT_URL ) {
if ( file_exists($f = WP_CONTENT_DIR."/".substr($thumb, strlen(WP_CONTENT_URL))) )
$thumb = $f;
}
$path = "elegant-grunge-thumbnails/".preg_replace("/[^a-zA-Z0-9]/", "-", $thumb)."-$width.$height.jpg";
if ( file_exists(WP_CONTENT_DIR."/".$path) ) {
return clean_url(WP_CONTENT_URL."/".$path, 'raw');
}
if ( !get_option('create_photoblog_thumbnails') ) return false;
// Check for GD support
if ( !function_exists('imagejpeg') ) return false;
require_once("Image.class.php");
$image = Image::Load($thumb);
if ( !$image ) return false;
$image->Scale($width, $height, true);
if ( !file_exists(WP_CONTENT_DIR."/elegant-grunge-thumbnails") ) {
mkdir(WP_CONTENT_DIR."/elegant-grunge-thumbnails", 0755);
}
if ( !$image->Save(WP_CONTENT_DIR."/".$path) ) {
return false;
}
return clean_url(WP_CONTENT_URL."/".$path, 'raw');
}
/**
* Template tag: Get the thumbnail
*
* @param width int Width of thumbnail (optional)
* @param height int Height of thumbnail (optional)
* @param return boolean If true, returns the thumb URL instead of printing it (optional)
*/
function the_thumbnail($width = 0, $height = 0, $return = false) {
global $post;
$url = the_image_url(true);
if ( !$url ) return;
$info = the_image_info();
if ( !$width && !$height ) {
$width = 100;
$height = 80;
if ( $info["width"] && $info["height"] ) {
if ( $info["width"] > $info["height"] ) {
$height = 100;
$width = ($info["width"] / $info["height"]) * $height;
if ( $width > 300 ) {
$width = 300;
$height = ($info["height"] / $info["width"]) * $width;
}
} else {
$height = 100;
$width = ($info["width"] / $info["height"]) * $height;
}
}
$width = round($width);
$height = round($height);
}
else if ( $width && !$height ) {
$height = (3/4) * $width;
if ( $info["width"] && $info["height"] ) {
$height = ($info["height"] / $info["width"]) * $width;
}
}
$thumb = the_image_scaled_thumb_url($width, $height);
if ( !$thumb )
$thumb = the_image_thumb_url(true);
$link = (get_option('photoblog_lightbox') ? $url : get_permalink());
ob_start();
?>
<div class="photoblog-thumbnail">
<a href="<?php echo $link ?>" rel="lightbox[photoblog]"><img src="<?php echo $thumb; ?>" width="<?php echo $width; ?>" <?php echo ($height ? "height=\"$height\"" : ""); ?> alt="<?php the_title(); ?>" title="<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php echo ($info["description"]?" - ".$info["description"]:""); ?>" /></a>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
if ( $return )
return $content;
echo $content;
}
/**
* Favicon template tag
*/
function elegant_grunge_the_favicon() {
if ( file_exists(WP_CONTENT_DIR."/favicon.ico") ) {
?><link rel="icon" href="<?php echo WP_CONTENT_URL?>/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="<?php echo WP_CONTENT_URL?>/favicon.ico" type="image/x-icon" /><?php
}
}
/**
* Administration
*/
function elegant_grunge_admin() {
?>
<div class="wrap">
<h2>Elegant Grunge</h2>
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Header image:', 'elegant-grunge') ?></th>
<td>
<input type="text" class="text" style="width: 400px;" name="header_image" value="<?php echo htmlspecialchars(get_option('header_image')) ?>" /><br/>
<small><?php _e('If specified, the image (typically a transparent PNG) at the above URL will be used for the header.', 'elegant-grunge') ?></small>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('RSS subscription:', 'elegant-grunge') ?></th>
<td>
<input type="checkbox" name="show_rss" <?php echo (get_option('show_rss') ? "checked" : ""); ?> />
<?php _e('Display RSS subscription link', 'elegant-grunge') ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Post info:', 'elegant-grunge') ?></th>
<td>
<input type="checkbox" name="show_author" <?php echo (get_option('show_author') ? "checked" : ""); ?> />
<?php _e('Display post author', 'elegant-grunge') ?>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Page setup:', 'elegant-grunge') ?></th>
<td>
<select name="page_setup">
<option value="right-sidebar" <?php echo (get_option('page_setup')=='right-sidebar' ? 'selected' : '') ?>>Right sidebar</option>
<option value="double-right-sidebar" <?php echo (get_option('page_setup')=='double-right-sidebar' ? 'selected' : '') ?>>Double Right sidebar</option>
<option value="no-sidebar" <?php echo (get_option('page_setup')=='no-sidebar' ? 'selected' : '') ?>>No sidebar</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Copyright message:', 'elegant-grunge') ?></th>
<td>
<input type="text" class="text" style="width: 300px;" name="copyright" value="<?php echo htmlspecialchars(get_option('copyright')) ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Image frames:', 'elegant-grunge') ?></th>
<td>
<input type="checkbox" name="frame_all_images" <?php echo (get_option('frame_all_images') ? "checked" : ""); ?> />
<?php _e('Apply frame to all images', 'elegant-grunge') ?><br/>
<small><?php printf(__('If enabled, all images larger than %1$d x %2$d
will have a frame with drop shadow applied. Otherwise, only images and other elements with a class
of \'frame\' will have this style applied.<br/>
This setting can be configured per-post and per-page, also.', 'elegant-grunge'), ELEGANT_GRUNGE_FRAME_MIN_WIDTH, ELEGANT_GRUNGE_FRAME_MIN_HEIGHT) ?></small>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e("Don't frame images with class:", 'elegant-grunge') ?></th>
<td>
<input type="text" class="text" style="width: 300px;" name="frame_class_skip" value="<?php echo htmlspecialchars(get_option('frame_class_skip')) ?>" />
<br/><small><?php _e('Separate multiple classes with commas \',\'', 'elegant-grunge') ?></small>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Extra header content:', 'elegant-grunge') ?></th>
<td>
<textarea style="width: 300px; height: 100px;" name="extra_header"><?php echo htmlspecialchars(get_option('extra_header')) ?></textarea><br/>
<small><?php _e('This can be used to add extra RSS feed links from your page, for example, such as a Twitter feed.', 'elegant-grunge') ?></small>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Photoblog thumbnails:', 'elegant-grunge') ?></th>
<td>
<p>
<input type="checkbox" name="create_photoblog_thumbnails" <?php echo (get_option('create_photoblog_thumbnails') ? "checked" : ""); ?> />
<?php _e('Create scaled thumbnails', 'elegant-grunge') ?><br/>
<small><?php _e('If enabled, will generate thumbnail files. Otherwise, will use the original images, resulting in slower loading times.
Note that the first photoblog page load after this is enabled will be slow, while images are being created, so you should
<a href="/tag/photoblog" target="_blank">load this</a> yourself.', 'elegant-grunge') ?></small>
</p>
<p>
<?php _e('Thumbnail size:', 'elegant-grunge') ?><br/>
<input type="text" class="text" size="5" name="photoblog_thumb_width" value="<?php echo get_option('photoblog_thumb_width') ?>" /> x
<input type="text" class="text" size="5" name="photoblog_thumb_height" value="<?php echo get_option('photoblog_thumb_height') ?>" /><br />
<small><?php _e('Leave blank for flexible size', 'elegant-grunge') ?></small>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Photoblog display:', 'elegant-grunge') ?></th>
<td>
<p>
<?php _e('Number of thumbnails per page:', 'elegant-grunge') ?><br/>
<input type="text" class="text" size="5" name="photoblog_thumb_count" value="<?php echo get_option('photoblog_thumb_count') ?>" />
</p>
<p>
<input type="checkbox" name="photoblog_lightbox" <?php echo (get_option('photoblog_lightbox') ? "checked" : ""); ?> />
<?php _e('Use lightbox mode', 'elegant-grunge') ?><br/>
<small><?php _e('Requires a lightbox plugin to be installed, such as <a href="http://www.stimuli.ca/lightbox/">Lightbox 2</a>.', 'elegant-grunge') ?></small>
</p>
<p>
<input type="checkbox" name="photoblog_frames" <?php echo (get_option('photoblog_frames') ? "checked" : ""); ?> />
<?php _e('Draw frames around photoblog items', 'elegant-grunge') ?><br/>
</p>
</td>
</tr>
</table>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="header_image,show_rss,show_author,page_setup,copyright,frame_all_images,frame_class_skip,extra_header,create_photoblog_thumbnails,photoblog_thumb_count,photoblog_thumb_width,photoblog_thumb_height,photoblog_lightbox,photoblog_frames" />
<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Save Changes', 'elegant-grunge') ?>" />
</p>
</form>
</div>
<?php
}
/**
* Per-post setup
*/
function elegant_grunge_post_options() {
global $post;
$post_id = $post;
if (is_object($post_id)) {
$post_id = $post_id->ID;
}
$frame_all_images = get_option("frame_all_images");
$frame_all_images_post = get_post_meta($post_id, 'frame_all_images', true);
if ( $frame_all_images_post == "true" )
$frame_all_images = true;
else if ( $frame_all_images_post == "false" )
$frame_all_images = false;
?>
<div class="postbox closed">
<h3><?php _e('Elegant Grunge Theme Options', 'elegant-grunge') ?></h3>
<div class="inside">
<input value="eg_edit" type="hidden" name="eg_edit" />
<table class="form-table">
<tr>
<th style="text-align:left;" colspan="2"><?php _e('Image frames:', 'elegant-grunge') ?></th>
<td>
<input type="hidden" name="frame_all_images" id="eg_frame_all_images" value="" />
<input type="checkbox" name="frame_all_images_disabled" <?php echo ($frame_all_images ? "checked" : ""); ?>
onchange="document.getElementById('eg_frame_all_images').value = (this.checked ? 'true' : 'false');" />
<?php _e('Apply frame to all images', 'elegant-grunge') ?><br/>
<small><?php printf(__('If enabled, all images larger than %1$d x %2$d
will have a frame with drop shadow applied. Otherwise, only images and other elements with a class
of \'frame\' will have this style applied.', 'elegant-grunge'), ELEGANT_GRUNGE_FRAME_MIN_WIDTH, ELEGANT_GRUNGE_FRAME_MIN_HEIGHT) ?></small>
</td>
</tr>
</table>
</div>
</div>
<?php
}
/**
* Per-page setup
*/
function elegant_grunge_page_options() {
global $post;
$post_id = $post;
if (is_object($post_id)) {
$post_id = $post_id->ID;
}
$frame_all_images = get_option("frame_all_images");
$frame_all_images_post = get_post_meta($post_id, 'frame_all_images', true);
if ( $frame_all_images_post == "true" )
$frame_all_images = true;
else if ( $frame_all_images_post == "false" )
$frame_all_images = false;
$relatedTitle = get_post_meta($post_id, 'related_title', true);
if ( !$relatedTitle ) $relatedTitle = "Related posts";
?>
<div class="postbox closed">
<h3><?php _e('Elegant Grunge Theme Options', 'elegant-grunge') ?></h3>
<div class="inside">
<input value="eg_edit" type="hidden" name="eg_edit" />
<table class="form-table">
<tr>
<th style="text-align:left;" colspan="2"><?php _e('Image frames:', 'elegant-grunge') ?></th>
<td>
<input type="hidden" name="frame_all_images" id="eg_frame_all_images" value="" />
<input type="checkbox" name="frame_all_images_disabled" <?php echo ($frame_all_images ? "checked" : ""); ?>
onchange="document.getElementById('eg_frame_all_images').value = (this.checked ? 'true' : 'false');" />
<?php _e('Apply frame to all images', 'elegant-grunge') ?><br/>
<small><?php printf(__('If enabled, all images larger than %1$d x %2$d
will have a frame with drop shadow applied. Otherwise, only images and other elements with a class
of \'frame\' will have this style applied.', 'elegant-grunge'), ELEGANT_GRUNGE_FRAME_MIN_WIDTH, ELEGANT_GRUNGE_FRAME_MIN_HEIGHT) ?></small>
</td>
</tr>
<tr>
<th style="text-align:left;" colspan="2"><?php _e('Related tag(s):', 'elegant-grunge') ?></th>
<td>
<input value="<?php echo htmlspecialchars(get_post_meta($post_id, 'related_tags', true)); ?>" name="related_tags" /><br />
<small>
<?php _e("If specified and 'Page with custom sidebar' page template is selected, will display posts from these tags
in the sidebar. Specify multiple tags by separating with a comma ','.", 'elegant-grunge') ?>
</small>
</td>
</tr>
<tr>
<th style="text-align:left;" colspan="2"><?php _e('Related posts title:', 'elegant-grunge') ?></th>
<td>
<input type="text" class="text" name="related_title" value="<?php echo htmlspecialchars($relatedTitle); ?>" /><br />
<small>
<?php _e("If 'Page with custom sidebar' page template is selected, and one or more related tags are provided above, this will be the title
above a list of related posts.", 'elegant-grunge') ?>
</small>
</td>
</tr>
<tr>
<th style="text-align:left;" colspan="2"><?php _e('Extra sidebar content:', 'elegant-grunge') ?></th>
<td>
<textarea name="sidebar_content" style="width:300px; height:100px;"><?php echo htmlspecialchars(get_post_meta($post_id, 'sidebar_content', true)); ?></textarea><br/>
<small>
<?php _e("If 'Page with custom sidebar' page template is selected, this text/HTML will be displayed in the sidebar.", 'elegant-grunge') ?>
</small>
</td>
</tr>
</table>
</div>
</div>
<?php
}
/**
* Save setup from post/page
*/
function elegant_grunge_save_post($id) {
if ( !isset($_REQUEST['eg_edit']) ) return;
if ( isset($_REQUEST['frame_all_images']) && $_REQUEST['frame_all_images'] != '' ) {
delete_post_meta($id, 'frame_all_images');
add_post_meta($id, 'frame_all_images', $_REQUEST['frame_all_images']);
}
if ( isset($_REQUEST['related_tags']) ) {
delete_post_meta($id, 'related_tags');
if ( $_REQUEST['related_tags'] )
add_post_meta($id, 'related_tags', stripcslashes($_REQUEST['related_tags']));
}
if ( isset($_REQUEST['related_title']) ) {
delete_post_meta($id, 'related_title');
if ( $_REQUEST['related_title'] )
add_post_meta($id, 'related_title', stripcslashes($_REQUEST['related_title']));
}
if ( isset($_REQUEST['sidebar_content']) ) {
delete_post_meta($id, 'sidebar_content');
if ( $_REQUEST['sidebar_content'] )
add_post_meta($id, 'sidebar_content', stripcslashes($_REQUEST['sidebar_content']));
}
}
function elegant_grunge_setup_admin() {
add_theme_page('Elegant Grunge Setup', 'Elegant Grunge', 8, __FILE__, 'elegant_grunge_admin');
}
function elegant_grunge_photoblog($args) {
$thumbs = array();
$count = get_option('elegant_grunge_photoblog_entries');
$width = get_option('elegant_grunge_photoblog_width');
$height = get_option('elegant_grunge_photoblog_height');
global $post;
$posts = get_posts('tag='.get_option('elegant_grunge_photoblog_tags').'&numberposts='.$count.(get_option('elegant_grunge_photoblog_order')=='random' ? '&orderby=rand' : ''));
foreach ( $posts as $post ) {
$thumb = the_thumbnail($width, $height, true);
if ( $thumb ) {
$thumbs[] = $thumb;
}
if ( count($thumbs) == $count ) break;
}
if ( count($thumbs) == 0 ) return;
echo $args["before_widget"];
if ( get_option('elegant_grunge_photoblog_heading') ) {
echo $args["before_title"].htmlspecialchars(get_option('elegant_grunge_photoblog_heading')).$args["after_title"];
}
echo '<div>';
foreach ( $thumbs as $thumb ) {
echo $thumb;
}
echo '</div>';
echo $args["after_widget"];
}
function elegant_grunge_photoblog_setup() {
$options = array("elegant_grunge_photoblog_heading",
"elegant_grunge_photoblog_entries",
"elegant_grunge_photoblog_order",
"elegant_grunge_photoblog_tags",
"elegant_grunge_photoblog_width",
"elegant_grunge_photoblog_height");
foreach ( $options as $option ) {
if ( isset($_REQUEST[$option]) ) {
update_option($option, stripslashes($_REQUEST[$option]));
}
}
?>
<p>
<label for="photoblog_heading"><?php _e('Heading:', 'elegant-grunge') ?></label>
<input name="elegant_grunge_photoblog_heading" id="photoblog_heading" type="text" class="widefat text" value="<?php echo get_option('elegant_grunge_photoblog_heading') ?>" />
</p>
<p>
<label for="photoblog_entries"><?php _e('Display:', 'elegant-grunge') ?></label>
<input name="elegant_grunge_photoblog_entries" id="photoblog_entries" type="text" class="text" size="5" value="<?php echo get_option('elegant_grunge_photoblog_entries') ?>" /> <?php _e('entries', 'elegant-grunge')?><br/>
</p>
<p>
<input type="radio" name="elegant_grunge_photoblog_order" id="photoblog_order_random" value="random" <?php echo ((get_option('elegant_grunge_photoblog_order')=='random') ? 'checked' : '') ?> />
<label for="photoblog_order_random"><?php _e('Display random entries', 'elegant-grunge') ?></label><br />
<input type="radio" name="elegant_grunge_photoblog_order" id="photoblog_order_recent" value="recent" <?php echo ((get_option('elegant_grunge_photoblog_order')=='recent') ? 'checked' : '') ?> />
<label for="photoblog_order_recent"><?php _e('Display recent entries', 'elegant-grunge') ?></label><br />
</p>
<p>
<label for="photoblog_tags"><?php _e('Tags to display:', 'elegant-grunge') ?></label>
<input name="elegant_grunge_photoblog_tags" id="photoblog_tags" type="text" class="widefat text" value="<?php echo get_option('elegant_grunge_photoblog_tags') ?>" /><br/>
<small><?php _e('Separate multiple tags with commas', 'elegant-grunge') ?></small>
</p>
<p>
<label for="photoblog_width"><?php _e('Image width:', 'elegant-grunge') ?></label>
<input name="elegant_grunge_photoblog_width" id="photoblog_width" type="text" class="text" size="5" value="<?php echo get_option('elegant_grunge_photoblog_width') ?>" />
</p>
<p>
<label for="photoblog_height"><?php _e('Image height:', 'elegant-grunge') ?></label>
<input name="elegant_grunge_photoblog_height" id="photoblog_height" type="text" class="text" size="5" value="<?php echo get_option('elegant_grunge_photoblog_height') ?>" />
</p>
<?php
}
function elegant_grunge_widget_init() {
register_sidebar_widget('Photoblog', 'elegant_grunge_photoblog');
register_widget_control('Photoblog', 'elegant_grunge_photoblog_setup');
add_option('elegant_grunge_photoblog_tags', 'photoblog');
add_option('elegant_grunge_photoblog_heading', 'Photoblog');
add_option('elegant_grunge_photoblog_entries', '4');
add_option('elegant_grunge_photoblog_order', 'random');
add_option('elegant_grunge_photoblog_width', '100');
add_option('elegant_grunge_photoblog_height', '70');
}
load_theme_textdomain('elegant-grunge');
add_filter( 'the_content', 'elegant_grunge_filter', 15 );
add_option( 'header_image', '' );
add_option( 'show_rss', true );
add_option( 'show_author', false );
add_option( 'copyright', 'Copyright © '.strftime('%Y').' '.get_bloginfo( 'name' ) );
add_option( 'frame_all_images', true );
add_option( 'frame_class_skip', 'noframe,wp-smiley' );
add_option( 'create_photoblog_thumbnails', false );
add_option( 'photoblog_thumb_count', 30 );
add_option( 'photoblog_thumb_width', '' );
add_option( 'photoblog_thumb_height', '' );
add_option( 'photoblog_lightbox', function_exists('autoexpand_rel_wlightbox') );
add_option( 'photoblog_frames', false );
add_option( 'page_setup', 'right-sidebar' );
add_action( 'admin_menu', 'elegant_grunge_setup_admin' );
add_action( 'edit_form_advanced', 'elegant_grunge_post_options' );
add_action( 'edit_page_form', 'elegant_grunge_page_options' );
add_action( 'save_post', 'elegant_grunge_save_post' );
add_action( 'widgets_init', 'elegant_grunge_widget_init' );
/**
* Debug
*/
function EGDEBUG($text) {
global $__EGDEBUG_FD;
if ( !$__EGDEBUG_FD ) {
$__EGDEBUG_FD = fopen("eg_debug.txt", "a");
}
fwrite($__EGDEBUG_FD, $text."\n");
}
?>