PluginProbe
Timed Content / 2.6
Timed Content v2.6
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.6, at lib/customFieldsInterface.php

416 lines 22.0 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
88 include("jquery-ui-datetime-i18n.php" );
89 // Only enqueue scripts if we're dealing with 'timed-content-rule' pages
90 foreach ( $this->postTypes as $a_postType ) {
91 if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == $a_postType )
92 || ( isset( $post_type ) && $post_type == $a_postType )
93 || ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == $a_postType ) ) {
94
95 wp_enqueue_style( 'wp-color-picker' );
96 wp_enqueue_script( 'wp-color-picker' );
97 wp_enqueue_script( 'jquery-ui-spinner' );
98
99 wp_enqueue_style(TIMED_CONTENT_SLUG . '-jquery-ui-css', TIMED_CONTENT_JQUERY_UI_CSS);
100 wp_enqueue_script('jquery-ui-datepicker');
101 wp_register_style(TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS);
102 wp_enqueue_style(TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css');
103 wp_register_script(TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js', TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS, array('jquery', 'jquery-ui-datepicker'), TIMED_CONTENT_VERSION);
104 wp_enqueue_script(TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js');
105 if (!(wp_script_is(TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', 'registered'))) {
106 wp_register_script(TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', TIMED_CONTENT_PLUGIN_URL . "/js/content-protector-datetime-i18n.js", array('jquery', 'jquery-ui-datepicker', TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js'), TIMED_CONTENT_VERSION);
107 wp_enqueue_script(TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js');
108 wp_localize_script(TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', 'TimedContentJQDatepickerI18n', $jquery_ui_datetime_datepicker_i18n);
109 wp_localize_script(TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', 'TimedContentJQTimepickerI18n', $jquery_ui_datetime_timepicker_i18n);
110 }
111
112 if ( function_exists( 'add_meta_box' ) )
113 add_meta_box( $this->handle, $this->label, array( &$this, 'displayCustomFields' ), $a_postType, 'normal', 'high' );
114 }
115 }
116 }
117 /**
118 * Display the new Custom Fields meta box
119 */
120 function displayCustomFields() {
121 global $post;
122 ?>
123 <p><?php echo $this->desc; ?></p>
124 <div class="form-wrap">
125 <?php
126 wp_nonce_field( $this->handle, $this->handle . '_wpnonce', false, true );
127 foreach ( $this->customFields as $customField ) {
128 // Check scope
129 $scope = $customField[ 'scope' ];
130 $output = false;
131 foreach ( $scope as $scopeItem ) {
132 switch ( $scopeItem ) {
133 default: {
134 if ( $post->post_type == $scopeItem )
135 $output = true;
136 break;
137 }
138 }
139 if ( $output ) break;
140 }
141 // Check capability
142 if ( !current_user_can( $customField['capability'], $post->ID ) )
143 $output = false;
144 // Output if allowed
145 if ( $output ) { ?>
146 <div class="form-field form-required" id="<?php echo $this->prefix . $customField[ 'name' ]; ?>_div" style="display: <?php echo $customField[ 'display' ]; ?>">
147 <?php
148 switch ( $customField[ 'type' ] ) {
149 case "radio": {
150 // radio
151 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
152 echo "<strong>" . $customField[ 'title' ] . "</strong><br />\n";
153 foreach ( $customField['values'] as $value => $label ) {
154 echo "<input type=\"radio\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "_" . $value . "\" value=\"" . $value . "\"";
155 if ( $checked_value == $value )
156 echo " checked=\"checked\"";
157 echo " style=\"width: 30px;\" /><label for=\"" . $this->prefix . $customField[ 'name' ] . "_" . $value . "\" style=\"display: inline;\">" . $label . "</label><br />\n";
158 }
159 break;
160 }
161 case "menu": {
162 // menu
163 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label><br />\n";
164 if ( sizeof ( $customField['values'] ) == 0 )
165 echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n";
166 else {
167 $selected_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
168 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";
169 foreach ( $customField['values'] as $value => $label ) {
170 echo "\t<option value=\"" . $value . "\"";
171 if ( $selected_value == $value )
172 echo " selected=\"selected\"";
173 echo ">" . $label . "</option>\n";
174 }
175 echo "</select>\n";
176 }
177 break;
178 }
179 case "list": {
180 // list
181 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
182 if ( sizeof ( $customField['values'] ) == 0 )
183 echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n";
184 else {
185 $selected_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
186 echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n";
187 foreach ( $customField['values'] as $value => $label ) {
188 echo "\t<option value=\"" . $value . "\"";
189 if ( $selected_value == $value )
190 echo " selected=\"selected\"";
191 echo ">" . $label . "</option>\n";
192 }
193 echo "</select>\n";
194 }
195 break;
196 }
197 case "timezone-list": {
198 // timezone list
199 $selected_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 "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n";
203 echo customFieldsInterface::__generateTimezoneSelectOptions( $selected_value );
204 echo "</select>\n";
205 break;
206 }
207 case "checkbox": {
208 // Checkbox
209 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
210
211 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
212 echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"yes\"";
213 if ( $checked_value == "yes" )
214 echo " checked=\"checked\"";
215 echo " style=\"width: 30px;\" />\n";
216 break;
217 }
218 case "checkbox-list": {
219 // Checkbox list
220 $checked_value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
221
222 echo "<strong>" . $customField[ 'title' ] . "</strong><br />\n";
223 if ( sizeof ( $customField['values'] ) == 0 )
224 echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n";
225 else {
226 foreach ( $customField['values'] as $value => $label ) {
227 echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "[]\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\"";
228 if ( ( is_array( $checked_value ) ) && ( in_array( $value, $checked_value ) ) )
229 echo " checked=\"checked\"";
230 echo " style=\"width: 30px;\" /><label for=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" style=\"display: inline;\" >" . $label . "</label><br />\n";
231 }
232 }
233 break;
234 }
235 case "textarea":
236 case "wysiwyg": {
237 // Text area
238 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
239 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] ."\"><strong>" . $customField[ 'title' ] . "</strong></label>\n";
240 echo "<textarea name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" columns=\"30\" rows=\"3\">" . htmlspecialchars( $value ) . "</textarea>\n";
241 // WYSIWYG
242 if ( $customField[ 'type' ] == "wysiwyg" ) { ?>
243 <script type="text/javascript">
244 //<![CDATA[
245 jQuery( document ).ready( function() {
246 jQuery( "<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).addClass( "mceEditor" );
247 if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
248 tinyMCE.execCommand( "mceAddControl", false, "<?php echo $this->prefix . $customField[ 'name' ]; ?>" );
249 }
250 });
251 //]]>
252 </script>
253 <?php }
254 break;
255 }
256 case "color-picker": {
257 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
258 // Color picker using WP's built-in Iris jQuery plugin ?>
259 <script type="text/javascript">
260 //<![CDATA[
261 jQuery(document).ready(function() {
262 var <?php echo $this->prefix . $customField[ 'name' ]; ?>Options = {
263 defaultColor: false,
264 hide: true,
265 palettes: true
266 };
267
268 jQuery("#<?php echo $this->prefix . $customField[ 'name' ]; ?>").wpColorPicker(<?php echo $this->prefix . $customField[ 'name' ]; ?>Options);
269 });
270 //]]>
271 </script>
272 <?php
273 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
274 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" size=\"7\" maxlength=\"7\" style=\"width: 100px;\" />\n";
275 break;
276 }
277 case "date": {
278 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
279 // echo "<span style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></span>&nbsp;&nbsp;\n";
280 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
281 // Date picker using WP's built-in Datepicker jQuery plugin ?>
282 <script type="text/javascript">
283 //<![CDATA[
284 jQuery(document).ready(function() {
285 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).datepicker(
286 {
287 <?php if( isset( $customField[ 'custom_functions' ] ) ) echo $customField[ 'custom_functions' ]; ?>
288 changeMonth: true,
289 changeYear: true
290 }
291 );
292 });
293 //]]>
294 </script>
295 <?php
296 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . htmlspecialchars( $value ) . "\" style=\"width: 175px;\" />\n";
297 break;
298 }
299 case "datetime": {
300 echo "<span style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></span><br />\n";
301 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
302 foreach ( $value as $k => $v )
303 $value[$k] = htmlspecialchars( $v );
304 // Date picker using WP's built-in Datepicker jQuery plugin
305 // Time picker using jQuery UI Timepicker: http://fgelinas.com/code/timepicker
306 ?>
307 <script type="text/javascript">
308 //<![CDATA[
309 jQuery(document).ready(function() {
310 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>_date" ).datepicker(
311 {
312 changeMonth: true,
313 changeYear: true
314 }
315 );
316 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>_time" ).timepicker(
317 {
318 defaultTime: 'now'
319 }
320 );
321 });
322 //]]>
323 </script>
324 <?php
325 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "_date\" style=\"display:inline;\"><em>" . _x( 'Date', 'Date field label', 'timed-content' ) . ":</em></label>&nbsp;&nbsp;\n";
326 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "[date]\" id=\"" . $this->prefix . $customField[ 'name' ] . "_date\" value=\"" . $value['date'] . "\" style=\"width: 175px;\" />\n";
327 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] ."_time\" style=\"display:inline;\"><em>" . _x( 'Time', 'Time field label', 'timed-content' ) . ":</em></label>&nbsp;&nbsp;\n";
328 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "[time]\" id=\"" . $this->prefix . $customField[ 'name' ] . "_time\" value=\"" . $value['time'] . "\" style=\"width: 125px;\" />\n";
329 break;
330 }
331 case "number": {
332 (int)$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
333 // Number picker using WP's built-in Spinner jQuery plugin ?>
334 <script type="text/javascript">
335 //<![CDATA[
336 jQuery(document).ready(function() {
337 jQuery( "#<?php echo $this->prefix . $customField[ 'name' ]; ?>" ).spinner(
338 {
339 stop: function( event, ui ) { jQuery( this ).trigger("change"); },
340 <?php if ( isset( $customField[ 'min' ] ) ) { ?>min: <?php echo $customField[ 'min' ]; ?>, <?php } ?>
341 <?php if ( isset( $customField[ 'max' ] ) ) { ?>max: <?php echo $customField[ 'max' ]; ?> <?php } ?>
342 }
343 );
344 });
345 //]]>
346 </script>
347 <?php
348 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\" style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></label>&nbsp;&nbsp;\n";
349 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" size=\"2\" />\n";
350 break;
351 }
352 case "hidden": {
353 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
354 // Hidden field
355 echo "<input type=\"hidden\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" />\n";
356 break;
357 }
358 default: {
359 $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) ? $customField['default'] : get_post_meta( $post->ID, $this->prefix . $customField['name'], true ) );
360 // Plain text field
361 echo "<label for=\"" . $this->prefix . $customField[ 'name' ] . "\"><strong>" . $customField[ 'title' ] . "</strong></label>\n";
362 echo "<input type=\"text\" name=\"" . $this->prefix . $customField[ 'name' ] . "\" id=\"" . $this->prefix . $customField[ 'name' ] . "\" value=\"" . $value . "\" />\n";
363 break;
364 }
365 }
366 ?>
367 <?php if ( isset( $customField[ 'description' ] ) ) echo "<p>" . $customField[ 'description' ] . "</p>\n"; ?>
368 </div>
369 <?php
370 }
371 } ?>
372 </div>
373 <?php
374 }
375 /**
376 * Save the new Custom Fields values
377 */
378 function saveCustomFields( $post_id, $post ) {
379 // Only save if the user intends to save
380 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
381 return;
382 // Make sure the Save request is coming from the right place
383 if ( !isset( $_POST[ $this->handle . '_wpnonce' ] ) || !wp_verify_nonce( $_POST[ $this->handle . '_wpnonce' ], $this->handle ) )
384 return;
385 // Make sure the user can edit this specific post
386 if ( !current_user_can( 'edit_post', $post_id ) )
387 return;
388 // Make sure this meta box is associated with the right post types
389 if ( ! in_array( $post->post_type, $this->postTypes ) )
390 return;
391 foreach ( $this->customFields as $customField ) {
392 if ( current_user_can( $customField['capability'], $post_id ) ) {
393 if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) ) {
394 if ( is_array( $_POST[ $this->prefix . $customField['name'] ] ) ) {
395 foreach ( $_POST[ $this->prefix . $customField['name'] ] as $k => $v )
396 $_POST[ $this->prefix . $customField['name'] ][$k] = trim( $v );
397 } else
398 $_POST[ $this->prefix . $customField['name'] ] = trim( $_POST[ $this->prefix . $customField['name'] ] );
399 $value = $_POST[ $this->prefix . $customField['name'] ];
400 // Auto-paragraphs for any WYSIWYG
401 if ( $customField['type'] == "wysiwyg" ) $value = wpautop( $value );
402 if ( $customField['type'] == "checkbox" ) $value = "yes";
403 if ( $customField['type'] == "number" ) $value = intval( $value );
404 update_post_meta( $post_id, $this->prefix . $customField[ 'name' ], $value );
405 } else {
406 delete_post_meta( $post_id, $this->prefix . $customField[ 'name' ] );
407 }
408 }
409 }
410 }
411
412
413 } // End Class
414
415 } // End if class exists statement
416 ?>