Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 260 lines (203 sloc) 8.31 kb
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
<?php if ( ! defined( 'TEMPLATEPATH') ) exit( 'No direct script access allowed' );
/**
* OptionTree Meta Box API
*
* This class loads all the methods and helpers specific to build a meta box.
*
* @package OptionTree
* @author Derek Herman <derek@valendesigns.com>
* @copyright Copyright (c) 2012, Derek Herman
*/
if ( ! class_exists( 'OT_Meta_Box' ) ) {

  class OT_Meta_Box {
  
    /* variable to store the meta box array */
    private $meta_box;
  
    /**
* PHP5 constructor method.
*
* This method adds other methods of the class to specific hooks within WordPress.
*
* @uses add_action()
*
* @return void
*
* @access public
* @since 1.0
*/
    function __construct( $meta_box ) {
      if ( ! is_admin() )
        return;
        
      $this->meta_box = $meta_box;
      
      add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );
      
      add_action( 'save_post', array( &$this, 'save_meta_box' ), 1, 2 );
      
    }
    
    /**
* Adds meta box to any post type
*
* @uses add_meta_box()
*
* @return void
*
* @access public
* @since 1.0
*/
    function add_meta_boxes() {
      foreach ( (array) $this->meta_box['pages'] as $page ) {
        add_meta_box( $this->meta_box['id'], $this->meta_box['title'], array( &$this, 'build_meta_box' ), $page, $this->meta_box['context'], $this->meta_box['priority'], $this->meta_box['fields'] );
      }
    }
    
    /**
* Meta box view
*
* @return string
*
* @access public
* @since 1.0
*/
    function build_meta_box( $post, $metabox ) {
      
      echo '<div class="ot-metabox-wrapper">';
           
        /* Use nonce for verification */
        echo '<input type="hidden" name="' . $this->meta_box['id'] . '_nonce" value="' . wp_create_nonce( $this->meta_box['id'] ) . '" />';
      
        /* loop through meta box fields */
        foreach ( $this->meta_box['fields'] as $field ) {
        
          /* get current post meta data */
          $field_value = get_post_meta( $post->ID, $field['id'], true );
          
          /* set default to standard value */
          if ( '' == $field_value && isset( $field['std'] ) ) {
            $field_value = trim( $field['std'] );
          }
          
          /* build the arguments array */
          $_args = array(
            'type' => $field['type'],
            'field_id' => $field['id'],
            'field_name' => $field['id'],
            'field_value' => $field_value,
            'field_desc' => isset( $field['desc'] ) ? $field['desc'] : '',
            'field_std' => isset( $field['std'] ) ? $field['std'] : '',
            'field_rows' => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10,
            'field_post_type' => '',
            'field_class' => isset( $field['class'] ) ? ' ' . $field['class'] : '',
            'field_choices' => isset( $field['choices'] ) ? $field['choices'] : array(),
            'field_settings' => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(),
            'post_id' => $post->ID,
            'meta' => true
          );
          
          /* only allow simple textarea due to DOM issues with wp_editor() */
          if ( $_args['type'] == 'textarea' )
            $_args['type'] = 'textarea-simple';
          
          /* option label */
          echo '<div class="format-settings">';
            echo '<div class="format-setting-label">';
              echo '<label for="' . $_args['field_id'] . '" class="label">' . $field['label'] . '</label>';
            echo '</div>';
  
            /* get the option HTML */
            echo ot_display_by_type( $_args );
            
          echo '</div>';
          
        }
      
      echo '</div>';

    }
    
    /**
* Saves the meta box values
*
* @return void
*
* @access public
* @since 1.0
*/
    function save_meta_box( $post_id, $post_object ) {
      global $pagenow;
      
      /* don't save during quick edit */
      if ( $pagenow == 'admin-ajax.php' )
        return $post_id;
        
      /* don't save during autosave */
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;
      
      /* don't save if viewing a revision */
      if ( $post_object->post_type == 'revision' )
        return $post_id;
  
      /* verify nonce */
      if ( isset( $_POST[ $this->meta_box['id'] . '_nonce'] ) && ! wp_verify_nonce( $_POST[ $this->meta_box['id'] . '_nonce'], $this->meta_box['id'] ) )
        return $post_id;
    
      /* check permissions */
      if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
          return $post_id;
      } else {
        if ( ! current_user_can( 'edit_post', $post_id ) )
          return $post_id;
      }
    
      foreach ( $this->meta_box['fields'] as $field ) {
        
        $old = get_post_meta( $post_id, $field['id'], true );
        $new = '';
        
        /* there is data to validate */
        if ( isset( $_POST[$field['id']] ) ) {
        
          /* slider and list item */
          if ( in_array( $field['type'], array( 'list-item', 'slider' ) ) ) {
              
            /* required title setting */
            $required_setting = array(
              array(
                'id' => 'title',
                'label' => __( 'Title', 'option-tree' ),
                'desc' => '',
                'std' => '',
                'type' => 'text',
                'rows' => '',
                'class' => 'option-tree-setting-title',
                'post_type' => '',
                'choices' => array()
              )
            );
            
            /* get the settings array */
            $settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( base64_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array();
            
            /* settings are empty for some odd ass reason get the defaults */
            if ( empty( $settings ) ) {
              $settings = 'slider' == $field['type'] ?
              ot_slider_settings( $field['id'] ) :
              ot_list_item_settings( $field['id'] );
            }
            
            /* merge the two settings array */
            $settings = array_merge( $required_setting, $settings );
            
            foreach( $_POST[$field['id']] as $k => $setting_array ) {
  
              foreach( $settings as $sub_setting ) {
                
                /* verify sub setting has a type & value */
                if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) {
                  
                  $_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'] );
                  
                }
                
              }
            
            }
            
            /* set up new data with validated data */
            $new = $_POST[$field['id']];

          } else {
            
            /* run through validattion */
            $new = ot_validate_setting( $_POST[$field['id']], $field['type'] );
            
          }
        
        }
        
        if ( $new && $new !== $old ) {
          update_post_meta( $post_id, $field['id'], $new );
        } else if ( '' == $new && $old ) {
          delete_post_meta( $post_id, $field['id'], $old );
        }
      }
  
    }
  
  }

}

/**
* This method instantiates the meta box class & builds the UI.
*
* @uses OT_Meta_Box()
*
* @param array Array of arguments to create a meta box
* @return void
*
* @access public
* @since 2.0
*/
if ( ! function_exists( 'ot_register_meta_box' ) ) {

  function ot_register_meta_box( $args ) {
    if ( ! $args )
      return;
      
    $ot_meta_box =& new OT_Meta_Box( $args );
  }

}

/* End of file ot-meta-box-api.php */
/* Location: ./includes/ot-meta-box-api.php */
Something went wrong with that request. Please try again.