PluginProbe
Timed Content / 2.1.1
Timed Content v2.1.1
2.99 trunk 1.0 1.1 1.2 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.10 2.11 2.12 2.15 2.2 2.3 2.3.1 2.4 2.5 2.5.1 2.50 2.51 2.52 All 67 releases
timed-content / lib / customFieldsInterface.php

customFieldsInterface.php in Timed Content 2.1.1, at lib/customFieldsInterface.php

413 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( !class_exists('customFieldsInterface') ) {
3
4 class customFieldsInterface {
5 /**
6 * @var string $handle The handle for the box containing the custom fields
7 */
8 var $handle = "";
9 /**
10 * @var string $label The label for the box containing the custom fields
11 */
12 var $label = "";
13 /**
14 * @var string $desc The description/introduction for the box containing the custom fields
15 */
16 var $desc = "";
17 /**
18 * @var string $prefix The prefix for storing custom fields in the postmeta table
19 */
20 var $prefix = "";
21 /**
22 * @var array $postTypes An array of public custom post types, plus the standard "post" and "page" - add the custom types you want to include here
23 */
24 var $postTypes = array();
25 /**
26 * @var array $customFields Defines the custom fields available
27 */
28 var $customFields = array();
29 /**
30 * PHP 4 Compatible Constructor
31 */
32 function customFieldsInterface( $handle, $label, $desc, $prefix, $postTypes, $customFields ) { $this->__construct( $handle, $label, $desc, $prefix, $postTypes, $customFields ); }
33 /**
34 * PHP 5 Constructor
35 */
36 function __construct( $handle, $label, $desc, $prefix, $postTypes, $customFields ) {
37
38 $this->handle = $handle;
39 $this->label = $label;
40 $this->desc = $desc;
41 $this->prefix = $prefix;
42 $this->postTypes = $postTypes ;
43 $this->customFields = $customFields;
44 add_action( 'admin_menu', array( &$this, 'createCustomFields' ) );
45 add_action( 'save_post', array( &$this, 'saveCustomFields' ), 1, 2 );
46 // Comment this line out if you want to keep default custom fields meta box
47 add_action( 'do_meta_boxes', array( &$this, 'removeDefaultCustomFields' ), 10, 3 );
48 }
49
50 // Inspired by http://ca1.php.net/manual/en/function.timezone-identifiers-list.php#79284
51 static function __generateTimezoneSelectOptions( $default_tz ) {
52 $timezone_identifiers = timezone_identifiers_list();
53 sort( $timezone_identifiers );
54 $current_continent = "";
55 $options_list = "";
56
57 foreach ( $timezone_identifiers as $timezone_identifier ) {
58 list( $continent, ) = explode( "/", $timezone_identifier, 2);
59 if ( in_array( $continent, array( "Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic", "Australia", "Europe", "Indian", "Pacific" ) ) ) {
60 list( , $city ) = explode( "/", $timezone_identifier, 2);
61 if ( strlen( $current_continent ) === 0 ) {
62 $options_list .= "<optgroup label=\"" . $continent . "\">"; // Start first continent optgroup
63 }
64 elseif ( $current_continent != $continent ) {
65 $options_list .= "</optgroup><optgroup label=\"" . $continent . "\">"; // End old optgroup and start new continent optgroup
66 }
67 $options_list .= "<option" . ( ( $timezone_identifier == $default_tz ) ? " selected=\"selected\"" : "" )
68 . " value=\"" . $timezone_identifier . "\">" . str_replace( "_", " ", $city ). "</option>"; //Timezone
69 }
70 $current_continent = $continent;
71 }
72 $options_list .= "</optgroup>"; // End last continent optgroup
73
74 return $options_list;
75 }
76
77 /**
78 * Remove the default Custom Fields meta box
79 */
80 function removeDefaultCustomFields( $type, $context, $post ) {
81 foreach ( array( 'normal', 'advanced', 'side' ) as $context ) {
82 foreach ( $this->postTypes as $postType ) {
83 remove_meta_box( 'postcustom', $postType, $context );
84 }
85 }
86 }
87 /**
88 * Create the new Custom Fields meta box
89 */
90 function createCustomFields() {
91 // Only enqueue scripts if we're dealing with 'timed-content-rule' pages
92 foreach ( $this->postTypes as $a_postType ) {
93 if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == $a_postType )
94 || ( isset( $post_type ) && $post_type == $a_postType )
95 || ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == $a_postType ) ) {
96
97 wp_enqueue_style( 'wp-color-picker' );
98 wp_enqueue_script( 'wp-color-picker' );
99 wp_enqueue_style( 'timed-content-jquery-ui', TIMED_CONTENT_JQUERY_UI_CSS );
100 wp_enqueue_script( 'jquery-ui-datepicker' );
101 wp_enqueue_script( 'jquery-ui-spinner' );
102 wp_register_style( 'timed-content-jquery-ui-timepicker-css', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS );
103 wp_enqueue_style( 'timed-content-jquery-ui-timepicker-css' );
104 wp_register_script( 'timed-content-jquery-ui-timepicker-js', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS, array('jquery', 'jquery-ui-datepicker'), TIMED_CONTENT_VERSION );
105 wp_enqueue_script( 'timed-content-jquery-ui-timepicker-js' );
106 if ( function_exists( 'add_meta_box' ) )
107 add_meta_box( $this->handle, $this->label, array( &$this, 'displayCustomFields' ), $a_postType, 'normal', 'high' );
108 }
109 }
110 }
111 /**
112 * Display the new Custom Fields meta box
113 */
114 function displayCustomFields() {
115 global $post;
116 ?>
117 <p><?php echo $this->desc; ?></p>
118 <div class="form-wrap">
119 <?php
120 wp_nonce_field( $this->handle, $this->handle . '_wpnonce', false, true );
121 foreach ( $this->customFields as $customField ) {
122 // Check scope
123 $scope = $customField[ 'scope' ];
124 $output = false;
125 foreach ( $scope as $scopeItem ) {
126 switch ( $scopeItem ) {
127 default: {
128 if ( $post->post_type == $scopeItem )
129 $output = true;
130 break;
131 }
132 }
133 if ( $output ) break;
134 }
135 // Check capability
136 if ( !current_user_can( $customField['capability'], $post->ID ) )
137 $output = false;
138 // Output if allowed
139 if ( $output ) { ?>
140 <div class="form-field form-required" id="<?php echo $this->prefix . $customField[ 'name' ]; ?>_div" style="display: <?php echo $customField[ 'display' ]; ?>">
141 <?php
142 switch ( $customField[ 'type' ] ) {
143 case "radio": {
144 // radio
145 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
146 echo "<strong>" . $customField[ 'title' ] . "</strong><br />\n";
147 foreach ( $customField['values'] as $value => $label ) {
148 echo "<input type=\"radio\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "_" . $value . "\" value=\"" . $value . "\"";
149 if ( $checked_value == $value )
150 echo " checked=\"checked\"";
151 echo " style=\"width: 30px;\" /><label for=\"" . $this->prefix . $customField[ 'name' ] . "_" . $value . "\" style=\"display: inline;\">" . $label . "</label><br />\n";
152 }
153 break;
154 }
155 case "menu": {
156 // menu
157 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label><br />\n";
158 if ( sizeof ( $customField['values'] ) == 0 )
159 echo "<em>This menu is empty.</em>\n";
160 else {
161 $selected_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
162 echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto; height: auto; padding: 3px;\" size=\"" . $customField['size'] . "\" multiple=\"multiple\">\n";
163 foreach ( $customField['values'] as $value => $label ) {
164 echo "\t<option value=\"" . $value . "\"";
165 if ( $selected_value == $value )
166 echo " selected=\"selected\"";
167 echo ">" . $label . "</option>\n";
168 }
169 echo "</select>\n";
170 }
171 break;
172 }
173 case "list": {
174 // list
175 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
176 if ( sizeof ( $customField['values'] ) == 0 )
177 echo "<em>This list is empty.</em>\n";
178 else {
179 $selected_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
180 echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n";
181 foreach ( $customField['values'] as $value => $label ) {
182 echo "\t<option value=\"" . $value . "\"";
183 if ( $selected_value == $value )
184 echo " selected=\"selected\"";
185 echo ">" . $label . "</option>\n";
186 }
187 echo "</select>\n";
188 }
189 break;
190 }
191 case "timezone-list": {
192 // timezone list
193 $selected_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
194
195 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
196 echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n";
197 echo customFieldsInterface::__generateTimezoneSelectOptions( $selected_value );
198 echo "</select>\n";
199 break;
200 }
201 case "checkbox": {
202 // Checkbox
203 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
204
205 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
206 echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"yes\"";
207 if ( $checked_value == "yes" )
208 echo " checked=\"checked\"";
209 echo " style=\"width: 30px;\" />\n";
210 break;
211 }
212 case "checkbox-list": {
213 // Checkbox list
214 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
215
216 echo "<strong>" . $customField[ 'title' ] . "</strong><br />\n";
217 if ( sizeof ( $customField['values'] ) == 0 )
218 echo "<em>This list is empty.</em>\n";
219 else {
220 foreach ( $customField['values'] as $value => $label ) {
221 echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "[]\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\"";
222 if ( ( is_array( $checked_value ) ) && ( in_array( $value, $checked_value ) ) )
223 echo " checked=\"checked\"";
224 echo " style=\"width: 30px;\" /><label for=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" style=\"display: inline;\" >" . $label . "</label><br />\n";
225 }
226 }
227 break;
228 }
229 case "textarea":
230 case "wysiwyg": {
231 // Text area
232 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
233 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] ."\"><strong>" . $customField[ 'title' ] . "</strong></label>\n";
234 echo "<textarea name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" columns=\"30\" rows=\"3\">" . htmlspecialchars( $value ) . "</textarea>\n";
235 // WYSIWYG
236 if ( $customField[ 'type' ] == "wysiwyg" ) { ?>
237 <script type="text/javascript">
238 //<![CDATA[
239 jQuery( document ).ready( function() {
240 jQuery( "<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).addClass( "mceEditor" );
241 if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
242 tinyMCE.execCommand( "mceAddControl", false, "<?php echo $this->prefix . $customField[ 'name' ]; ?>" );
243 }
244 });
245 //]]>
246 </script>
247 <?php }
248 break;
249 }
250 case "color-picker": {
251 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
252 // Color picker using WP's built-in Iris jQuery plugin ?>
253 <script type="text/javascript">
254 //<![CDATA[
255 jQuery(document).ready(function() {
256 var <?php echo $this->prefix . $customField[ 'name' ]; ?>Options = {
257 defaultColor: false,
258 hide: true,
259 palettes: true
260 };
261
262 jQuery("#<?php echo $this->prefix . $customField[ 'name' ]; ?>").wpColorPicker(<?php echo $this->prefix . $customField[ 'name' ]; ?>Options);
263 });
264 //]]>
265 </script>
266 <?php
267 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
268 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" size=\"7\" maxlength=\"7\" style=\"width: 100px;\" />\n";
269 break;
270 }
271 case "date": {
272 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
273 // echo "<span style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></span>&nbsp;&nbsp;\n";
274 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
275 // Date picker using WP's built-in Datepicker jQuery plugin ?>
276 <script type="text/javascript">
277 //<![CDATA[
278 jQuery(document).ready(function() {
279 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).datepicker(
280 {
281 dateFormat: "<?php _ex( "MM d, yy", "Date format for jQuery UI Datepicker", 'timed-content' ); ?>",
282 changeMonth: true,
283 changeYear: true
284 }
285 );
286 });
287 //]]>
288 </script>
289 <?php
290 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . htmlspecialchars( $value ) . "\" style=\"width: 175px;\" />\n";
291 break;
292 }
293 case "datetime": {
294 echo "<span style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></span><br />\n";
295 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
296 foreach ( $value as $k => $v )
297 $value[$k] = htmlspecialchars( $v );
298 // Date picker using WP's built-in Datepicker jQuery plugin
299 // Time picker using jQuery UI Timepicker: http://fgelinas.com/code/timepicker
300 ?>
301 <script type="text/javascript">
302 //<![CDATA[
303 jQuery(document).ready(function() {
304 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>_date" ).datepicker(
305 {
306 dateFormat: "<?php _ex( "MM d, yy", "Date format for jQuery UI Datepicker", 'timed-content' ); ?>",
307 changeMonth: true,
308 changeYear: true
309 }
310 );
311 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>_time" ).timepicker(
312 {
313 showLeadingZero: false,
314 showPeriod: true,
315 defaultTime: 'now'
316 }
317 );
318 });
319 //]]>
320 </script>
321 <?php
322 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "_date\" style=\"display:inline;\"><em>" . _x( 'Date', 'Date field label', 'timed-content' ) . ":</em></label>&nbsp;&nbsp;\n";
323 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "[date]\" id=\"" . $this->prefix . $customField[ 'name' ] . "_date\" value=\"" . $value['date'] . "\" style=\"width: 175px;\" />\n";
324 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] ."_time\" style=\"display:inline;\"><em>" . _x( 'Time', 'Time field label', 'timed-content' ) . ":</em></label>&nbsp;&nbsp;\n";
325 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "[time]\" id=\"" . $this->prefix . $customField[ 'name' ] . "_time\" value=\"" . $value['time'] . "\" style=\"width: 125px;\" />\n";
326 break;
327 }
328 case "number": {
329 (int)$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
330 // Number picker using WP's built-in Spinner jQuery plugin ?>
331 <script type="text/javascript">
332 //<![CDATA[
333 jQuery(document).ready(function() {
334 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).spinner(
335 {
336 stop: function( event, ui ) { jQuery( this ).trigger("change"); },
337 <?php if ( isset( $customField[ 'min' ] ) ) { ?>min: <?php echo $customField[ 'min' ]; ?>, <?php } ?>
338 <?php if ( isset( $customField[ 'max' ] ) ) { ?>max: <?php echo $customField[ 'max' ]; ?> <?php } ?>
339 }
340 );
341 });
342 //]]>
343 </script>
344 <?php
345 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
346 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" size=\"2\" />\n";
347 break;
348 }
349 case "hidden": {
350 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
351 // Hidden field
352 echo "<input type=\"hidden\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" />\n";
353 break;
354 }
355 default: {
356 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
357 // Plain text field
358 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\"><strong>" . $customField[ 'title' ] . "</strong></label>\n";
359 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" />\n";
360 break;
361 }
362 }
363 ?>
364 <?php if ( isset( $customField[ 'description' ] ) ) echo "<p>" . $customField[ 'description' ] . "</p>\n"; ?>
365 </div>
366 <?php
367 }
368 } ?>
369 </div>
370 <?php
371 }
372 /**
373 * Save the new Custom Fields values
374 */
375 function saveCustomFields( $post_id, $post ) {
376 // Only save if the user intends to save
377 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
378 return;
379 // Make sure the Save request is coming from the right place
380 if ( !isset( $_POST[ $this->handle . '_wpnonce' ] ) || !wp_verify_nonce( $_POST[ $this->handle . '_wpnonce' ], $this->handle ) )
381 return;
382 // Make sure the user can edit this specific post
383 if ( !current_user_can( 'edit_post', $post_id ) )
384 return;
385 // Make sure this meta box is associated with the right post types
386 if ( ! in_array( $post->post_type, $this->postTypes ) )
387 return;
388 foreach ( $this->customFields as $customField ) {
389 if ( current_user_can( $customField['capability'], $post_id ) ) {
390 if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) ) {
391 if ( is_array( $_POST[ $this->prefix . $customField['name'] ] ) ) {
392 foreach ( $_POST[ $this->prefix . $customField['name'] ] as $k => $v )
393 $_POST[ $this->prefix . $customField['name'] ][$k] = trim( $v );
394 } else
395 $_POST[ $this->prefix . $customField['name'] ] = trim( $_POST[ $this->prefix . $customField['name'] ] );
396 $value = $_POST[ $this->prefix . $customField['name'] ];
397 // Auto-paragraphs for any WYSIWYG
398 if ( $customField['type'] == "wysiwyg" ) $value = wpautop( $value );
399 if ( $customField['type'] == "checkbox" ) $value = "yes";
400 if ( $customField['type'] == "number" ) $value = intval( $value );
401 update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $value );
402 } else {
403 delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
404 }
405 }
406 }
407 }
408
409
410 } // End Class
411
412 } // End if class exists statement
413 ?>