PluginProbe
Timed Content / 2.1.3
Timed Content v2.1.3
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.3, at lib/customFieldsInterface.php

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