wp/wp-content/themes/IN-MOTION-package-u1/option-tree/includes/ot-meta-box-api.php
changeset 0 d970ebf37754
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php if ( ! defined( 'OT_VERSION' ) ) exit( 'No direct script access allowed' );
       
     2 /**
       
     3  * OptionTree Meta Box API
       
     4  *
       
     5  * This class loads all the methods and helpers specific to build a meta box.
       
     6  *
       
     7  * @package   OptionTree
       
     8  * @author    Derek Herman <derek@valendesigns.com>
       
     9  * @copyright Copyright (c) 2012, Derek Herman
       
    10  */
       
    11 if ( ! class_exists( 'OT_Meta_Box' ) ) {
       
    12 
       
    13   class OT_Meta_Box {
       
    14   
       
    15     /* variable to store the meta box array */
       
    16     private $meta_box;
       
    17   
       
    18     /**
       
    19      * PHP5 constructor method.
       
    20      *
       
    21      * This method adds other methods of the class to specific hooks within WordPress.
       
    22      *
       
    23      * @uses      add_action()
       
    24      *
       
    25      * @return    void
       
    26      *
       
    27      * @access    public
       
    28      * @since     1.0
       
    29      */
       
    30     function __construct( $meta_box ) {
       
    31       if ( ! is_admin() )
       
    32         return;
       
    33         
       
    34       $this->meta_box = $meta_box;
       
    35       
       
    36       add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );
       
    37       
       
    38       add_action( 'save_post', array( &$this, 'save_meta_box' ), 1, 2 );
       
    39       
       
    40     }
       
    41     
       
    42     /**
       
    43      * Adds meta box to any post type
       
    44      *
       
    45      * @uses      add_meta_box()
       
    46      *
       
    47      * @return    void
       
    48      *
       
    49      * @access    public
       
    50      * @since     1.0
       
    51      */
       
    52     function add_meta_boxes() {
       
    53       foreach ( (array) $this->meta_box['pages'] as $page ) {
       
    54         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'] );
       
    55       }
       
    56     }
       
    57     
       
    58     /**
       
    59      * Meta box view
       
    60      *
       
    61      * @return    string
       
    62      *
       
    63      * @access    public
       
    64      * @since     1.0
       
    65      */
       
    66     function build_meta_box( $post, $metabox ) {
       
    67       
       
    68       echo '<div class="ot-metabox-wrapper">';
       
    69            
       
    70         /* Use nonce for verification */
       
    71         echo '<input type="hidden" name="' . $this->meta_box['id'] . '_nonce" value="' . wp_create_nonce( $this->meta_box['id'] ) . '" />';
       
    72         
       
    73         /* meta box description */
       
    74         echo isset( $this->meta_box['desc'] ) ? '<div class="description" style="padding-top:10px;">' . htmlspecialchars_decode( $this->meta_box['desc'] ) . '</div>' : '';
       
    75       
       
    76         /* loop through meta box fields */
       
    77         foreach ( $this->meta_box['fields'] as $field ) {
       
    78         
       
    79           /* get current post meta data */
       
    80           $field_value = get_post_meta( $post->ID, $field['id'], true );
       
    81           
       
    82           /* set default to standard value */
       
    83           if ( '' == $field_value && isset( $field['std'] ) ) {  
       
    84             $field_value = trim( $field['std'] );
       
    85           }
       
    86           
       
    87           /* build the arguments array */
       
    88           $_args = array(
       
    89             'type'              => $field['type'],
       
    90             'field_id'          => $field['id'],
       
    91             'field_name'        => $field['id'],
       
    92             'field_value'       => $field_value,
       
    93             'field_desc'        => isset( $field['desc'] ) ? $field['desc'] : '',
       
    94             'field_std'         => isset( $field['std'] ) ? $field['std'] : '',
       
    95             'field_rows'        => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10,
       
    96             'field_post_type'   => isset( $field['post_type'] ) && ! empty( $field['post_type'] ) ? $field['post_type'] : 'post',
       
    97             'field_taxonomy'    => isset( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ? $field['taxonomy'] : 'category',
       
    98             'field_class'       => isset( $field['class'] ) ? $field['class'] : '',
       
    99             'field_choices'     => isset( $field['choices'] ) ? $field['choices'] : array(),
       
   100             'field_settings'    => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(),
       
   101             'post_id'           => $post->ID,
       
   102             'meta'              => true
       
   103           );
       
   104           
       
   105           /* only allow simple textarea due to DOM issues with wp_editor() */
       
   106           if ( $_args['type'] == 'textarea' )
       
   107             $_args['type'] = 'textarea-simple';
       
   108           
       
   109           /* option label */
       
   110           echo '<div class="format-settings">';
       
   111             
       
   112             /* don't show title with textblocks */
       
   113             if ( $_args['type'] != 'textblock' ) {
       
   114               echo '<div class="format-setting-label">';
       
   115                 echo '<label for="' . $_args['field_id'] . '" class="label">' . $field['label'] . '</label>';
       
   116               echo '</div>';
       
   117             }
       
   118       
       
   119             /* get the option HTML */
       
   120             echo ot_display_by_type( $_args );
       
   121             
       
   122           echo '</div>';
       
   123           
       
   124         }
       
   125       
       
   126       echo '</div>';
       
   127 
       
   128     }
       
   129     
       
   130     /**
       
   131      * Saves the meta box values
       
   132      *
       
   133      * @return    void
       
   134      *
       
   135      * @access    public
       
   136      * @since     1.0
       
   137      */
       
   138     function save_meta_box( $post_id, $post_object ) {
       
   139       global $pagenow;
       
   140       
       
   141       /* don't save during quick edit */
       
   142       if ( $pagenow == 'admin-ajax.php' )
       
   143         return $post_id;
       
   144         
       
   145       /* don't save during autosave */
       
   146       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
       
   147         return $post_id;
       
   148       
       
   149       /* don't save if viewing a revision */
       
   150       if ( $post_object->post_type == 'revision' )
       
   151         return $post_id;
       
   152   
       
   153       /* verify nonce */
       
   154       if ( isset( $_POST[ $this->meta_box['id'] . '_nonce'] ) && ! wp_verify_nonce( $_POST[ $this->meta_box['id'] . '_nonce'], $this->meta_box['id'] ) )
       
   155         return $post_id;
       
   156     
       
   157       /* check permissions */
       
   158       if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
       
   159         if ( ! current_user_can( 'edit_page', $post_id ) )
       
   160           return $post_id;
       
   161       } else {
       
   162         if ( ! current_user_can( 'edit_post', $post_id ) )
       
   163           return $post_id;
       
   164       }
       
   165     	
       
   166       foreach ( $this->meta_box['fields'] as $field ) {
       
   167         
       
   168         $old = get_post_meta( $post_id, $field['id'], true );
       
   169         $new = '';
       
   170         
       
   171         /* there is data to validate */
       
   172         if ( isset( $_POST[$field['id']] ) ) {
       
   173         
       
   174           /* slider and list item */
       
   175           if ( in_array( $field['type'], array( 'list-item', 'slider' ) ) ) {
       
   176               
       
   177             /* required title setting */
       
   178             $required_setting = array(
       
   179               array(
       
   180                 'id'        => 'title',
       
   181                 'label'     => __( 'Title', 'option-tree' ),
       
   182                 'desc'      => '',
       
   183                 'std'       => '',
       
   184                 'type'      => 'text',
       
   185                 'rows'      => '',
       
   186                 'class'     => 'option-tree-setting-title',
       
   187                 'post_type' => '',
       
   188                 'choices'   => array()
       
   189               )
       
   190             );
       
   191             
       
   192             /* get the settings array */
       
   193             $settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( base64_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array();
       
   194             
       
   195             /* settings are empty for some odd ass reason get the defaults */
       
   196             if ( empty( $settings ) ) {
       
   197               $settings = 'slider' == $field['type'] ? 
       
   198               ot_slider_settings( $field['id'] ) : 
       
   199               ot_list_item_settings( $field['id'] );
       
   200             }
       
   201             
       
   202             /* merge the two settings array */
       
   203             $settings = array_merge( $required_setting, $settings );
       
   204             
       
   205             foreach( $_POST[$field['id']] as $k => $setting_array ) {
       
   206   
       
   207               foreach( $settings as $sub_setting ) {
       
   208                 
       
   209                 /* verify sub setting has a type & value */
       
   210                 if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) {
       
   211                   
       
   212                   $_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'] );
       
   213                   
       
   214                 }
       
   215                 
       
   216               }
       
   217             
       
   218             }
       
   219             
       
   220             /* set up new data with validated data */
       
   221             $new = $_POST[$field['id']];
       
   222 
       
   223           } else {
       
   224             
       
   225             /* run through validattion */
       
   226             $new = ot_validate_setting( $_POST[$field['id']], $field['type'], $field['id'] );
       
   227             
       
   228           }
       
   229           
       
   230           /* insert CSS */
       
   231           if ( $field['type'] == 'css' ) {
       
   232             
       
   233             /* insert CSS into dynamic.css */
       
   234             if ( '' !== $new ) {
       
   235               
       
   236               ot_insert_css_with_markers( $field['id'], $new, true );
       
   237             
       
   238             /* remove old CSS from dynamic.css */
       
   239             } else {
       
   240             
       
   241               ot_remove_old_css( $field['id'] );
       
   242               
       
   243             }
       
   244           
       
   245           }
       
   246         
       
   247         }
       
   248         
       
   249         if ( $new && $new !== $old ) {
       
   250           update_post_meta( $post_id, $field['id'], $new );
       
   251         } else if ( '' == $new && $old ) {
       
   252           delete_post_meta( $post_id, $field['id'], $old );
       
   253         }
       
   254       }
       
   255   
       
   256     }
       
   257   
       
   258   }
       
   259 
       
   260 }
       
   261 
       
   262 /**
       
   263  * This method instantiates the meta box class & builds the UI.
       
   264  *
       
   265  * @uses     OT_Meta_Box()
       
   266  *
       
   267  * @param    array    Array of arguments to create a meta box
       
   268  * @return   void
       
   269  *
       
   270  * @access   public
       
   271  * @since    2.0
       
   272  */
       
   273 if ( ! function_exists( 'ot_register_meta_box' ) ) {
       
   274 
       
   275   function ot_register_meta_box( $args ) {
       
   276     if ( ! $args )
       
   277       return;
       
   278       
       
   279     $ot_meta_box =& new OT_Meta_Box( $args );
       
   280   }
       
   281 
       
   282 }
       
   283 
       
   284 /* End of file ot-meta-box-api.php */
       
   285 /* Location: ./includes/ot-meta-box-api.php */