/
better-art-direction.php
204 lines (175 loc) · 7.09 KB
/
better-art-direction.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
<?php
/*
Plugin Name: Better Art Direction
Plugin URI: http://github.com/davatron5000/better-art-direction
Description: A fork of the Art Direction Plugin by Noël Jackson (http://noel.io).
Author: David Rupert
Version: 1.0
Author URI: http://daverupert.com
*/
load_plugin_textdomain('art-direction', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/* Display */
add_action('wp_head', 'art_inline');
function art_inline( $data ) {
global $post, $global_styles, $single_styles;
if( is_single() or is_page() )
$single_styles .= str_replace( '#postid', $post->ID, get_post_meta($post->ID, 'art_direction_single', true) )."\n";
$global_styles .= str_replace( '#postid', $post->ID, get_post_meta($post->ID, 'art_direction_global', true) )."\n";
$data = "<!-- Better Art Direction Styles -->\n".$single_styles.$global_styles;
echo $data;
}
/* Publish */
add_action( 'publish_page','art_save_postdata' );
add_action( 'publish_post','art_save_postdata' );
add_action( 'save_post','art_save_postdata' );
add_action( 'edit_post','art_save_postdata' );
/* Save Data */
function art_save_postdata( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['art-direction-nonce'], plugin_basename(__FILE__) ) ) :
return $post_id;
endif;
if ( 'page' == $_POST['post_type'] ) :
if ( !current_user_can( 'edit_page', $post_id ) ) :
return $post_id;
endif;
else :
if ( !current_user_can( 'edit_post', $post_id ) ) :
return $post_id;
endif;
endif;
// OK, we're authenticated: we need to find and save the data
delete_post_meta( $post_id, 'art_direction_single' );
delete_post_meta( $post_id, 'art_direction_global' );
if( trim( $_POST['single-code'] ) != '' ) :
add_post_meta( $post_id, 'art_direction_single', stripslashes( $_POST['single-code']) );
endif;
if(trim($_POST['global-code']) != '') :
add_post_meta( $post_id, 'art_direction_global', stripslashes( $_POST['global-code']) );
return true;
endif;
}
/* admin interface */
add_action( 'admin_menu', 'art_add_meta_box' );
add_action( 'admin_head', 'art_admin_head' );
function art_admin_head() { ?>
<style>
.clear {
clear: both;
}
#global-code,
#single-code {
width: 100%;
height: 300px;
}
.global, .single {
width: 48%; float: left;
}
.global {
margin-right: 3%;
}
.tellmemore {
display: none;
}
.art-submit {
clear: both;
}
#art-direction-box h4 span {
font-weight: normal;
}
</style>
<?php
}
function art_add_meta_box() {
if( function_exists( 'add_meta_box' ) ) {
if( current_user_can('edit_posts') )
add_meta_box( 'art-direction-box', __( 'Art Direction', 'art-direction' ),
'art_meta_box', 'post', 'normal' );
if( current_user_can('edit_pages') )
add_meta_box( 'art-direction-box', __( 'Art Direction', 'art-direction' ),
'art_meta_box', 'page', 'normal' );
}
}
function art_meta_box() {
global $post;
?>
<form action="art-direction_submit" method="get" accept-charset="utf-8">
<?php
// Use nonce for verification
echo '<input type="hidden" name="art-direction-nonce" id="art-direction-nonce" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; ?>
<script>
/* <![CDATA[ */
jQuery(document).ready(function() {
jQuery('.help').click(function() {
var anchor = this.href.substr( this.href.indexOf('#') );
jQuery(this).hide();
jQuery(anchor).toggle();
return false;
});
jQuery('#art-direction-box textarea').focus(function() {
jQuery('#location').attr('class', this.id);
var location = jQuery('#location').attr('class');
});
jQuery('#style-insert').click(function() {
var location = jQuery('#location').attr('class');
edInsertContent(location, '<' + 'style'+'>'+"\n\n"+'<'+'/style'+'>');
});
jQuery('#script-insert').click(function() {
var location = jQuery('#location').attr('class');
edInsertContent(location, '<'+'script'+'>'+"\n\n"+'<'+'/script'+'>');
});
function edInsertContent(which, myValue) {
myField = document.getElementById(which);
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
myField.focus();
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var scrollTop = myField.scrollTop;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.scrollTop = scrollTop;
} else {
myField.value += myValue;
myField.focus();
}
}
});
/* ]]> */
</script>
<p><em><code>#postid</code> <?php echo "will be replaced with this entry's post ID."; ?></em></p>
<!-- hidden field-->
<p><?php _e( "Example:", 'art-direction' );?> <code>.post-#postid</code> <?php _e( "will become", 'art-direction' ); ?> <code>.post-<?php echo $post->ID; ?></code>.</p>
<input type="hidden" name="location" value="" id="location" />
<!-- style and script inserts -->
<p>
<input type="button" name="style-insert" class="button" value="Insert <style> Tag" id="style-insert" />
<input type="button" name="script-insert" class="button" value="Insert <script> Tag" id="script-insert" />
</p>
<!-- global code block -->
<div class="global">
<h4><?php _e( 'Global Code', 'art-direction' ); ?> <a class="help" href="#tellmemore-global">(?)</a> <span class="tellmemore" id="tellmemore-global"><?php _e( "will be inserted on every archive this entry appears on.", 'art-direction' ); ?></span></h4>
<textarea id="global-code" name="global-code" rows="8" cols="40"><?php echo esc_attr( get_post_meta( $post->ID,'art_direction_global', true ) ); ?></textarea>
</div>
<!-- single page code -->
<div class="single">
<h4><?php _e( 'Single Page Code', 'art-direction'); ?> <a class="help" href="#tellmemore-single">(?)</a> <span class="tellmemore" id="tellmemore-single"> <?php _e( "will only show up on this entry's single page.", 'art-direction' ); ?></span></h4>
<textarea id="single-code" name="single-code" rows="8" cols="40"><?php echo esc_attr( get_post_meta( $post->ID,'art_direction_single', true ) ); ?></textarea>
</div>
<!-- float clearing -->
<div class="clear"></div>
</form>
<?php
}