Advertisement
Guest User

meta box suggestion

a guest
Mar 13th, 2011
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.12 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Create meta box for editing pages in WordPress
  4.  *
  5.  * Compatible with custom post types since WordPress 3.0
  6.  * Support input types: text, textarea, checkbox, checkbox list, radio box, select, wysiwyg, file, image, date, time, color
  7.  *
  8.  * @author: Rilwis
  9.  * @url: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
  10.  * @usage: please read document at project homepage and meta-box-usage.php file
  11.  * @version: 3.0.1
  12.  */
  13.  
  14.  
  15. /**
  16. Updated by APS/Alex.
  17.  
  18. Added height-value for setting height in textarea/wysiwyg/select
  19. Updated lines are followed by the comment ALEX UPDATE
  20. Replaced lines are followed by the comment ORIGINAL
  21.  
  22.  
  23. */
  24.  
  25. // Ajax delete files on the fly. Modified from a function used by "Verve Meta Boxes" plugin (http://goo.gl/LzYSq)
  26. add_action('wp_ajax_rw_delete_file', 'rw_delete_file');
  27. function rw_delete_file() {
  28.     if (!isset($_POST['data'])) return;
  29.     list($post_id, $key, $attach_id, $src, $nonce) = explode('!', $_POST['data']);
  30.     if (!wp_verify_nonce($nonce, 'rw_ajax_delete_file')) {
  31.         _e('You don\'t have permission to delete this file.');
  32.     }
  33.     wp_delete_attachment($attach_id);
  34.     delete_post_meta($post_id, $key, $src);
  35.     _e('File has been successfully deleted.');
  36.     die();
  37. }
  38.  
  39. /**
  40.  * Meta Box class
  41.  */
  42. class RW_Meta_Box {
  43.  
  44.     protected $_meta_box;
  45.     protected $_fields;
  46.  
  47.     // Create meta box based on given data
  48.     function __construct($meta_box) {
  49.         if (!is_admin()) return;
  50.  
  51.         // assign meta box values to local variables and add it's missed values
  52.         $this->_meta_box = $meta_box;
  53.         $this->_fields = & $this->_meta_box['fields'];
  54.         $this->add_missed_values();
  55.  
  56.         add_action('admin_menu', array(&$this, 'add')); // add meta box
  57.         add_action('save_post', array(&$this, 'save')); // save meta box's data
  58.  
  59.         // check for some special fields and add needed actions for them
  60.         $this->check_field_upload();
  61.         $this->check_field_color();
  62.         $this->check_field_date();
  63.         $this->check_field_time();
  64.     }
  65.  
  66.     /******************** BEGIN UPLOAD **********************/
  67.  
  68.     // Check field upload and add needed actions
  69.     function check_field_upload() {
  70.         if ($this->has_field('image') || $this->has_field('file')) {
  71.             add_action('post_edit_form_tag', array(&$this, 'add_enctype'));             // add data encoding type for file uploading
  72.             add_action('admin_head-post.php', array(&$this, 'add_script_upload'));      // add scripts for handling add/delete images
  73.             add_action('admin_head-post-new.php', array(&$this, 'add_script_upload'));
  74.             add_action('delete_post', array(&$this, 'delete_attachments'));             // delete all attachments when delete post
  75.         }
  76.     }
  77.  
  78.     // Add data encoding type for file uploading
  79.     function add_enctype() {
  80.         echo ' enctype="multipart/form-data"';
  81.     }
  82.  
  83.     // Add scripts for handling add/delete images
  84.     function add_script_upload() {
  85.         echo '
  86.         <script type="text/javascript">
  87.         jQuery(document).ready(function($) {
  88.             // add more file
  89.             $(".rw-add-file").click(function(){
  90.                 var $first = $(this).parent().find(".file-input:first");
  91.                 $first.clone().insertAfter($first).show();
  92.                 return false;
  93.             });
  94.  
  95.             // delete file
  96.             $(".rw-delete-file").click(function(){
  97.                 var $parent = $(this).parent(),
  98.                     data = $(this).attr("rel");
  99.                 $.post(ajaxurl, {action: \'rw_delete_file\', data: data}, function(response){
  100.                     $parent.fadeOut("slow");
  101.                     alert(response);
  102.                 });
  103.                 return false;
  104.             });
  105.         });
  106.         </script>';
  107.     }
  108.  
  109.     // Delete all attachments when delete post
  110.     function delete_attachments($post_id) {
  111.         $attachments = get_posts(array(
  112.             'numberposts' => -1,
  113.             'post_type' => 'attachment',
  114.             'post_parent' => $post_id
  115.         ));
  116.         if (!empty($attachments)) {
  117.             foreach ($attachments as $att) {
  118.                 wp_delete_attachment($att->ID);
  119.             }
  120.         }
  121.     }
  122.  
  123.     /******************** END UPLOAD **********************/
  124.  
  125.     /******************** BEGIN COLOR PICKER **********************/
  126.  
  127.     // Check field color
  128.     function check_field_color() {
  129.         if ($this->has_field('color') && $this->is_edit_page()) {
  130.             wp_enqueue_style('farbtastic');                                 // enqueue built-in script and style for color picker
  131.             wp_enqueue_script('farbtastic');
  132.             add_action('admin_head', array(&$this, 'add_script_color'));    // add our custom script for color picker
  133.         }
  134.     }
  135.  
  136.     // Custom script for color picker
  137.     function add_script_color() {
  138.         $ids = array();
  139.         foreach ($this->_fields as $field) {
  140.             if ('color' == $field['type']) {
  141.                 $ids[] = $field['id'];
  142.             }
  143.         }
  144.         echo '
  145.         <script type="text/javascript">
  146.         jQuery(document).ready(function($){
  147.         ';
  148.         foreach ($ids as $id) {
  149.             echo "
  150.             $('#picker-$id').farbtastic('#$id');
  151.             $('#select-$id').click(function(){
  152.                 $('#picker-$id').toggle();
  153.                 return false;
  154.             });
  155.             ";
  156.         }
  157.         echo '
  158.         });
  159.         </script>
  160.         ';
  161.     }
  162.  
  163.     /******************** END COLOR PICKER **********************/
  164.  
  165.     /******************** BEGIN DATE PICKER **********************/
  166.  
  167.     // Check field date
  168.     function check_field_date() {
  169.         if ($this->has_field('date') && $this->is_edit_page()) {
  170.             // add style and script, must use jQuery UI 1.7.3 to get rid of confliction with WP admin scripts
  171.             wp_enqueue_style('rw-jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/themes/base/jquery-ui.css');
  172.             wp_enqueue_script('rw-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/jquery-ui.min.js', array('jquery'));
  173.             add_action('admin_head', array(&$this, 'add_script_date'));
  174.         }
  175.     }
  176.  
  177.     // Custom script for date picker
  178.     function add_script_date() {
  179.         $dates = array();
  180.         foreach ($this->_fields as $field) {
  181.             if ('date' == $field['type']) {
  182.                 $dates[$field['id']] = $field['format'];
  183.             }
  184.         }
  185.         echo '
  186.         <script type="text/javascript">
  187.         jQuery(document).ready(function($){
  188.         ';
  189.         foreach ($dates as $id => $format) {
  190.             echo "$('#$id').datepicker({
  191.                 dateFormat: '$format',
  192.                 showButtonPanel: true
  193.             });";
  194.         }
  195.         echo '
  196.         });
  197.         </script>
  198.         ';
  199.     }
  200.  
  201.     /******************** END DATE PICKER **********************/
  202.  
  203.     /******************** BEGIN TIME PICKER **********************/
  204.  
  205.     // Check field time
  206.     function check_field_time() {
  207.         if ($this->has_field('time') && $this->is_edit_page()) {
  208.             // add style and script, must use jQuery UI 1.7.3 to get rid of confliction with WP admin scripts
  209.             wp_enqueue_style('rw-jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/themes/base/jquery-ui.css');
  210.             wp_enqueue_script('rw-jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/jquery-ui.min.js', array('jquery'));
  211.             wp_enqueue_script('rw-timepicker', 'https://github.com/trentrichardson/jQuery-Timepicker-Addon/raw/master/jquery-ui-timepicker-addon.js', array('rw-jquery-ui'));
  212.             add_action('admin_head', array(&$this, 'add_script_time'));
  213.         }
  214.     }
  215.  
  216.     // Custom script and style for time picker
  217.     function add_script_time() {
  218.         // style
  219.         echo '
  220.         <style type="text/css">
  221.         .ui-timepicker-div {font-size: 0.9em;}
  222.         .ui-timepicker-div .ui-widget-header {margin-bottom: 8px;}
  223.         .ui-timepicker-div dl {text-align: left;}
  224.         .ui-timepicker-div dl dt {height: 25px;}
  225.         .ui-timepicker-div dl dd {margin: -25px 0 10px 65px;}
  226.         .ui-timepicker-div td {font-size: 90%;}
  227.         </style>
  228.         ';
  229.  
  230.         // script
  231.         $times = array();
  232.         foreach ($this->_fields as $field) {
  233.             if ('time' == $field['type']) {
  234.                 $times[$field['id']] = $field['format'];
  235.             }
  236.         }
  237.         echo '
  238.         <script type="text/javascript">
  239.         jQuery(document).ready(function($){
  240.         ';
  241.         foreach ($times as $id => $format) {
  242.             echo "$('#$id').timepicker({showSecond: true, timeFormat: '$format'})";
  243.         }
  244.         echo '
  245.         });
  246.         </script>
  247.         ';
  248.     }
  249.  
  250.     /******************** END TIME PICKER **********************/
  251.  
  252.     /******************** BEGIN META BOX PAGE **********************/
  253.  
  254.     // Add meta box for multiple post types
  255.     function add() {
  256.         foreach ($this->_meta_box['pages'] as $page) {
  257.             add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  258.         }
  259.     }
  260.  
  261.     // Callback function to show fields in meta box
  262.     function show() {
  263.         global $post;
  264.  
  265.         wp_nonce_field(basename(__FILE__), 'rw_meta_box_nonce');
  266.         echo '<table class="form-table">';
  267.  
  268.         foreach ($this->_fields as $field) {
  269.             $meta = get_post_meta($post->ID, $field['id'], !$field['multiple']);
  270.             $meta = !empty($meta) ? $meta : $field['std'];
  271.  
  272.             echo '<tr>';
  273.             // call separated methods for displaying each type of field
  274.             call_user_func(array(&$this, 'show_field_' . $field['type']), $field, $meta);
  275.             echo '</tr>';
  276.         }
  277.         echo '</table>';
  278.     }
  279.  
  280.     /******************** END META BOX PAGE **********************/
  281.  
  282.     /******************** BEGIN META BOX FIELDS **********************/
  283.  
  284.     function show_field_begin($field, $meta) {
  285.         echo "<th style='width:20%'><label for='{$field['id']}'>{$field['name']}</label></th><td>";
  286.     }
  287.  
  288.     function show_field_end($field, $meta) {
  289.         echo "<br />{$field['desc']}</td>";
  290.     }
  291.  
  292.     function show_field_text($field, $meta) {
  293.         $this->show_field_begin($field, $meta);
  294.         echo "<input type='text' name='{$field['id']}' id='{$field['id']}' value='$meta' size='30' style='width:97%' />";
  295.         $this->show_field_end($field, $meta);
  296.     }
  297.  
  298.     function show_field_textarea($field, $meta) {
  299.         $this->show_field_begin($field, $meta);
  300.         echo "<textarea name='{$field['id']}' cols='60' rows='15' style='width:97%" . (!empty($field['height']) ? '; height:' .$field['height'] . 'px' : '') . "'>$meta</textarea>"; // ALEX UPDATE
  301.         //echo "<textarea name='{$field['id']}' cols='60' rows='15' style='width:97%'>$meta</textarea>"; // ORIGINAL
  302.         $this->show_field_end($field, $meta);
  303.     }
  304.  
  305.     function show_field_select($field, $meta) {
  306.         if (!is_array($meta)) $meta = (array) $meta;
  307.         $this->show_field_begin($field, $meta);
  308.         echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:" . (!empty($field['height']) ? $field['height'] . 'px' : 'auto')   . "'" : "'") . ">"; // ALEX UPDATE
  309. //      echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">"; // ORIGINAL
  310.         foreach ($field['options'] as $key => $value) {
  311.             echo "<option value='$key'" . selected(in_array($key, $meta), true, false) . ">$value</option>";
  312.         }
  313.         echo "</select>";
  314.         $this->show_field_end($field, $meta);
  315.     }
  316.  
  317.     function show_field_radio($field, $meta) {
  318.         $this->show_field_begin($field, $meta);
  319.         foreach ($field['options'] as $key => $value) {
  320.             echo "<input type='radio' name='{$field['id']}' value='$key'" . checked($meta, $key, false) . " /> $value ";
  321.         }
  322.         $this->show_field_end($field, $meta);
  323.     }
  324.  
  325.     function show_field_checkbox($field, $meta) {
  326.         $this->show_field_begin($field, $meta);
  327.         echo "<input type='checkbox' name='{$field['id']}'" . checked(!empty($meta), true, false) . " /> {$field['desc']}</td>";
  328.     }
  329.  
  330.     function show_field_wysiwyg($field, $meta) {
  331.         $this->show_field_begin($field, $meta);
  332.         echo "<textarea name='{$field['id']}' class='theEditor' cols='60' rows='15' style='width:97%" . (!empty($field['height']) ? '; height:' .$field['height'] . 'px' : '') . "'>$meta</textarea>"; // ALEX
  333.         //echo "<textarea name='{$field['id']}' class='theEditor' cols='60' rows='15' style='width:97%'>$meta</textarea>";
  334.         $this->show_field_end($field, $meta);
  335.     }
  336.  
  337.     function show_field_file($field, $meta) {
  338.         global $post;
  339.  
  340.         if (!is_array($meta)) $meta = (array) $meta;
  341.        
  342.         $this->show_field_begin($field, $meta);
  343.         echo "{$field['desc']}<br />";
  344.  
  345.         if (!empty($meta)) {
  346.             // show attached files
  347.             $attachs = get_posts(array(
  348.                 'numberposts' => -1,
  349.                 'post_type' => 'attachment',
  350.                 'post_parent' => $post->ID
  351.             ));
  352.            
  353.             $nonce = wp_create_nonce('rw_ajax_delete_file');
  354.  
  355.             echo '<div style="margin-bottom: 10px"><strong>' . _('Uploaded files') . '</strong></div>';
  356.             echo '<ol>';
  357.             foreach ($attachs as $att) {
  358.                 if (wp_attachment_is_image($att->ID)) continue; // what's image uploader for?
  359.  
  360.                 $src = wp_get_attachment_url($att->ID);
  361.                 if (in_array($src, $meta)) {
  362.                     echo "<li>" . wp_get_attachment_link($att->ID) . " (<a class='rw-delete-file' href='javascript:void(0)' rel='{$post->ID}!{$field['id']}!{$att->ID}!{$src}!{$nonce}'>Delete</a>)</li>";
  363.                 }
  364.             }
  365.             echo '</ol>';
  366.         }
  367.  
  368.         // show form upload
  369.         echo "<div style='clear: both'><strong>" . _('Upload new files') . "</strong></div>
  370.             <div class='new-files'>
  371.                 <div class='file-input'><input type='file' name='{$field['id']}[]' /></div>
  372.                 <a class='rw-add-file' href='javascript:void(0)'>" . _('Add more file') . "</a>
  373.             </div>
  374.         </td>";
  375.     }
  376.  
  377.     function show_field_image($field, $meta) {
  378.         global $post;
  379.  
  380.         if (!is_array($meta)) $meta = (array) $meta;
  381.  
  382.         $this->show_field_begin($field, $meta);
  383.         echo "{$field['desc']}<br />";
  384.  
  385.         if (!empty($meta)) {
  386.             // show attached images
  387.             $attachs = get_posts(array(
  388.                 'numberposts' => -1,
  389.                 'post_type' => 'attachment',
  390.                 'post_parent' => $post->ID,
  391.                 'post_mime_type' => 'image', // get attached images only
  392.                 'output' => ARRAY_A
  393.             ));
  394.  
  395.             $nonce = wp_create_nonce('rw_ajax_delete_file');
  396.  
  397.             echo '<div style="margin-bottom: 10px"><strong>' . _('Uploaded images') . '</strong></div>';
  398.             foreach ($attachs as $att) {
  399.                 $src = wp_get_attachment_image_src($att->ID, 'full');
  400.                 $src = $src[0];
  401.  
  402.                 if (in_array($src, $meta)) {
  403.                     echo "<div style='margin: 0 10px 10px 0; float: left'><img width='150' src='$src' /><br />
  404.                             <a class='rw-delete-file' href='javascript:void(0)' rel='{$post->ID}!{$field['id']}!{$att->ID}!{$src}!{$nonce}'>Delete</a>
  405.                         </div>";
  406.                 }
  407.             }
  408.         }
  409.  
  410.         // show form upload
  411.         echo "<div style='clear: both'><strong>" . _('Upload new images') . "</strong></div>
  412.             <div class='new-files'>
  413.                 <div class='file-input'><input type='file' name='{$field['id']}[]' /></div>
  414.                 <a class='rw-add-file' href='javascript:void(0)'>" . _('Add more image') . "</a>
  415.             </div>
  416.         </td>";
  417.     }
  418.  
  419.     function show_field_color($field, $meta) {
  420.         if (empty($meta)) $meta = '#';
  421.         $this->show_field_begin($field, $meta);
  422.         echo "<input type='text' name='{$field['id']}' id='{$field['id']}' value='$meta' size='8' />
  423.               <a href='#' id='select-{$field['id']}'>" . _('Select a color') . "</a>
  424.               <div style='display:none' id='picker-{$field['id']}'></div>";
  425.         $this->show_field_end($field, $meta);
  426.     }
  427.  
  428.     function show_field_checkbox_list($field, $meta) {
  429.         if (!is_array($meta)) $meta = (array) $meta;
  430.         $this->show_field_begin($field, $meta);
  431.         $html = array();
  432.         foreach ($field['options'] as $key => $value) {
  433.             $html[] = "<input type='checkbox' name='{$field['id']}[]' value='$key'" . checked(in_array($key, $meta), true, false) . " /> $value";
  434.         }
  435.         echo implode('<br />', $html);
  436.         $this->show_field_end($field, $meta);
  437.     }
  438.  
  439.     function show_field_date($field, $meta) {
  440.         $this->show_field_text($field, $meta);
  441.     }
  442.  
  443.     function show_field_time($field, $meta) {
  444.         $this->show_field_text($field, $meta);
  445.     }
  446.  
  447.     /******************** END META BOX FIELDS **********************/
  448.  
  449.     /******************** BEGIN META BOX SAVE **********************/
  450.  
  451.     // Save data from meta box
  452.     function save($post_id) {
  453.         $post_type_object = get_post_type_object($_POST['post_type']);
  454.  
  455.         if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)                       // check autosave
  456.         || (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID'])         // check revision
  457.         || (!in_array($_POST['post_type'], $this->_meta_box['pages']))          // check if current post type is supported
  458.         || (!check_admin_referer(basename(__FILE__), 'rw_meta_box_nonce'))      // verify nonce
  459.         || (!current_user_can($post_type_object->cap->edit_post, $post_id))) {  // check permission
  460.             return $post_id;
  461.         }
  462.  
  463.         foreach ($this->_fields as $field) {
  464.             $name = $field['id'];
  465.             $type = $field['type'];
  466.             $old = get_post_meta($post_id, $name, !$field['multiple']);
  467.             $new = isset($_POST[$name]) ? $_POST[$name] : ($field['multiple'] ? array() : '');
  468.  
  469.             // validate meta value
  470.             if (class_exists('RW_Meta_Box_Validate') && method_exists('RW_Meta_Box_Validate', $field['validate_func'])) {
  471.                 $new = call_user_func(array('RW_Meta_Box_Validate', $field['validate_func']), $new);
  472.             }
  473.  
  474.             // call defined method to save meta value, if there's no methods, call common one
  475.             $save_func = 'save_field_' . $type;
  476.             if (method_exists($this, $save_func)) {
  477.                 call_user_func(array(&$this, 'save_field_' . $type), $post_id, $field, $old, $new);
  478.             } else {
  479.                 $this->save_field($post_id, $field, $old, $new);
  480.             }
  481.         }
  482.     }
  483.  
  484.     // Common functions for saving field
  485.     function save_field($post_id, $field, $old, $new) {
  486.         $name = $field['id'];
  487.  
  488.         // single value
  489.         if (!$field['multiple']) {
  490.             if ('' != $new && $new != $old) {
  491.                 update_post_meta($post_id, $name, $new);
  492.             } elseif ('' == $new) {
  493.                 delete_post_meta($post_id, $name, $old);
  494.             }
  495.             return;
  496.         }
  497.  
  498.         // multiple values
  499.         // get new values that need to add and get old values that need to delete
  500.         $add = array_diff($new, $old);
  501.         $delete = array_diff($old, $new);
  502.         foreach ($add as $add_new) {
  503.             add_post_meta($post_id, $name, $add_new, false);
  504.         }
  505.         foreach ($delete as $delete_old) {
  506.             delete_post_meta($post_id, $name, $delete_old);
  507.         }
  508.     }
  509.  
  510.     function save_field_textarea($post_id, $field, $old, $new) {
  511.         $new = htmlspecialchars($new);
  512.         $this->save_field($post_id, $field, $old, $new);
  513.     }
  514.  
  515.     function save_field_wysiwyg($post_id, $field, $old, $new) {
  516.         $new = wpautop($new);
  517.         $this->save_field($post_id, $field, $old, $new);
  518.     }
  519.  
  520.     function save_field_file($post_id, $field, $old, $new) {
  521.         $name = $field['id'];
  522.         if (empty($_FILES[$name])) return;
  523.  
  524.         $this->fix_file_array($_FILES[$name]);
  525.  
  526.         foreach ($_FILES[$name] as $position => $fileitem) {
  527.             $file = wp_handle_upload($fileitem, array('test_form' => false));
  528.  
  529.             if (empty($file['file'])) continue;
  530.             $filename = $file['file'];
  531.  
  532.             $attachment = array(
  533.                 'post_mime_type' => $file['type'],
  534.                 'guid' => $file['url'],
  535.                 'post_parent' => $post_id,
  536.                 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
  537.                 'post_content' => ''
  538.             );
  539.             $id = wp_insert_attachment($attachment, $filename, $post_id);
  540.             if (!is_wp_error($id)) {
  541.                 wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $filename));
  542.                 add_post_meta($post_id, $name, $file['url'], false);    // save file's url in meta fields
  543.             }
  544.         }
  545.     }
  546.  
  547.     // Save images, call save_field_file, cause they use the same mechanism
  548.     function save_field_image($post_id, $field, $old, $new) {
  549.         $this->save_field_file($post_id, $field, $old, $new);
  550.     }
  551.  
  552.     /******************** END META BOX SAVE **********************/
  553.  
  554.     /******************** BEGIN HELPER FUNCTIONS **********************/
  555.  
  556.     // Add missed values for meta box
  557.     function add_missed_values() {
  558.         // default values for meta box
  559.         $this->_meta_box = array_merge(array(
  560.             'context' => 'normal',
  561.             'priority' => 'high',
  562.             'pages' => array('post')
  563.         ), $this->_meta_box);
  564.  
  565.         // default values for fields
  566.         foreach ($this->_fields as $key => $field) {
  567.             $multiple = in_array($field['type'], array('checkbox_list', 'file', 'image')) ? true : false;
  568.             $std = $multiple ? array() : '';
  569.             $format = 'date' == $field['type'] ? 'yy-mm-dd' : ('time' == $field['type'] ? 'hh:mm' : '');
  570.             $this->_fields[$key] = array_merge(array(
  571.                 'multiple' => $multiple,
  572.                 'std' => $std,
  573.                 'desc' => '',
  574.                 'format' => $format,
  575.                 'validate_func' => ''
  576.             ), $field);
  577.         }
  578.     }
  579.  
  580.     // Check if field with $type exists
  581.     function has_field($type) {
  582.         foreach ($this->_fields as $field) {
  583.             if ($type == $field['type']) return true;
  584.         }
  585.         return false;
  586.     }
  587.  
  588.     // Check if current page is edit page
  589.     function is_edit_page() {
  590.         global $pagenow;
  591.         if (in_array($pagenow, array('post.php', 'post-new.php'))) return true;
  592.         return false;
  593.     }
  594.  
  595.     /**
  596.      * Fixes the odd indexing of multiple file uploads from the format:
  597.      *     $_FILES['field']['key']['index']
  598.      * To the more standard and appropriate:
  599.      *     $_FILES['field']['index']['key']
  600.      */
  601.     function fix_file_array(&$files) {
  602.         $output = array();
  603.         foreach ($files as $key => $list) {
  604.             foreach ($list as $index => $value) {
  605.                 $output[$index][$key] = $value;
  606.             }
  607.         }
  608.         $files = $output;
  609.     }
  610.  
  611.     /******************** END HELPER FUNCTIONS **********************/
  612. }
  613.  
  614. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement