wp/wp-content/plugins/option-tree/includes/ot-meta-box-api.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
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) 2013, 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'] ) && ! empty( $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 standard value */
       
    83           if ( isset( $field['std'] ) ) {  
       
    84             $field_value = ot_filter_std_value( $field_value, $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_min_max_step'=> isset( $field['min_max_step'] ) && ! empty( $field['min_max_step'] ) ? $field['min_max_step'] : '0,100,1',
       
    99             'field_class'       => isset( $field['class'] ) ? $field['class'] : '',
       
   100             'field_choices'     => isset( $field['choices'] ) ? $field['choices'] : array(),
       
   101             'field_settings'    => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(),
       
   102             'post_id'           => $post->ID,
       
   103             'meta'              => true
       
   104           );
       
   105           
       
   106           /* only allow simple textarea due to DOM issues with wp_editor() */
       
   107           if ( $_args['type'] == 'textarea' )
       
   108             $_args['type'] = 'textarea-simple';
       
   109           
       
   110           /* option label */
       
   111           echo '<div class="format-settings">';
       
   112             
       
   113             /* don't show title with textblocks */
       
   114             if ( $_args['type'] != 'textblock' && ! empty( $field['label'] ) ) {
       
   115               echo '<div class="format-setting-label">';
       
   116                 echo '<label for="' . $_args['field_id'] . '" class="label">' . $field['label'] . '</label>';
       
   117               echo '</div>';
       
   118             }
       
   119       
       
   120             /* get the option HTML */
       
   121             echo ot_display_by_type( $_args );
       
   122             
       
   123           echo '</div>';
       
   124           
       
   125         }
       
   126       
       
   127       echo '</div>';
       
   128 
       
   129     }
       
   130     
       
   131     /**
       
   132      * Saves the meta box values
       
   133      *
       
   134      * @return    void
       
   135      *
       
   136      * @access    public
       
   137      * @since     1.0
       
   138      */
       
   139     function save_meta_box( $post_id, $post_object ) {
       
   140       global $pagenow;
       
   141 
       
   142       /* don't save if $_POST is empty */
       
   143       if ( empty( $_POST ) )
       
   144         return $post_id;
       
   145       
       
   146       /* don't save during quick edit */
       
   147       if ( $pagenow == 'admin-ajax.php' )
       
   148         return $post_id;
       
   149         
       
   150       /* don't save during autosave */
       
   151       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
       
   152         return $post_id;
       
   153 
       
   154       /* don't save if viewing a revision */
       
   155       if ( $post_object->post_type == 'revision' || $pagenow == 'revision.php' )
       
   156         return $post_id;
       
   157   
       
   158       /* verify nonce */
       
   159       if ( isset( $_POST[ $this->meta_box['id'] . '_nonce'] ) && ! wp_verify_nonce( $_POST[ $this->meta_box['id'] . '_nonce'], $this->meta_box['id'] ) )
       
   160         return $post_id;
       
   161     
       
   162       /* check permissions */
       
   163       if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
       
   164         if ( ! current_user_can( 'edit_page', $post_id ) )
       
   165           return $post_id;
       
   166       } else {
       
   167         if ( ! current_user_can( 'edit_post', $post_id ) )
       
   168           return $post_id;
       
   169       }
       
   170       
       
   171       foreach ( $this->meta_box['fields'] as $field ) {
       
   172         
       
   173         $old = get_post_meta( $post_id, $field['id'], true );
       
   174         $new = '';
       
   175         
       
   176         /* there is data to validate */
       
   177         if ( isset( $_POST[$field['id']] ) ) {
       
   178         
       
   179           /* slider and list item */
       
   180           if ( in_array( $field['type'], array( 'list-item', 'slider' ) ) ) {
       
   181               
       
   182             /* required title setting */
       
   183             $required_setting = array(
       
   184               array(
       
   185                 'id'        => 'title',
       
   186                 'label'     => __( 'Title', 'option-tree' ),
       
   187                 'desc'      => '',
       
   188                 'std'       => '',
       
   189                 'type'      => 'text',
       
   190                 'rows'      => '',
       
   191                 'class'     => 'option-tree-setting-title',
       
   192                 'post_type' => '',
       
   193                 'choices'   => array()
       
   194               )
       
   195             );
       
   196             
       
   197             /* get the settings array */
       
   198             $settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( ot_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array();
       
   199             
       
   200             /* settings are empty for some odd ass reason get the defaults */
       
   201             if ( empty( $settings ) ) {
       
   202               $settings = 'slider' == $field['type'] ? 
       
   203               ot_slider_settings( $field['id'] ) : 
       
   204               ot_list_item_settings( $field['id'] );
       
   205             }
       
   206             
       
   207             /* merge the two settings array */
       
   208             $settings = array_merge( $required_setting, $settings );
       
   209             
       
   210             foreach( $_POST[$field['id']] as $k => $setting_array ) {
       
   211   
       
   212               foreach( $settings as $sub_setting ) {
       
   213                 
       
   214                 /* verify sub setting has a type & value */
       
   215                 if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) {
       
   216                   
       
   217                   $_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'] );
       
   218                   
       
   219                 }
       
   220                 
       
   221               }
       
   222             
       
   223             }
       
   224             
       
   225             /* set up new data with validated data */
       
   226             $new = $_POST[$field['id']];
       
   227 
       
   228           } else {
       
   229             
       
   230             /* run through validattion */
       
   231             $new = ot_validate_setting( $_POST[$field['id']], $field['type'], $field['id'] );
       
   232             
       
   233           }
       
   234           
       
   235           /* insert CSS */
       
   236           if ( $field['type'] == 'css' ) {
       
   237             
       
   238             /* insert CSS into dynamic.css */
       
   239             if ( '' !== $new ) {
       
   240               
       
   241               ot_insert_css_with_markers( $field['id'], $new, true );
       
   242             
       
   243             /* remove old CSS from dynamic.css */
       
   244             } else {
       
   245             
       
   246               ot_remove_old_css( $field['id'] );
       
   247               
       
   248             }
       
   249           
       
   250           }
       
   251         
       
   252         }
       
   253         
       
   254         if ( $new && $new !== $old ) {
       
   255           update_post_meta( $post_id, $field['id'], $new );
       
   256         } else if ( '' == $new && $old ) {
       
   257           delete_post_meta( $post_id, $field['id'], $old );
       
   258         }
       
   259       }
       
   260   
       
   261     }
       
   262   
       
   263   }
       
   264 
       
   265 }
       
   266 
       
   267 /**
       
   268  * This method instantiates the meta box class & builds the UI.
       
   269  *
       
   270  * @uses     OT_Meta_Box()
       
   271  *
       
   272  * @param    array    Array of arguments to create a meta box
       
   273  * @return   void
       
   274  *
       
   275  * @access   public
       
   276  * @since    2.0
       
   277  */
       
   278 if ( ! function_exists( 'ot_register_meta_box' ) ) {
       
   279 
       
   280   function ot_register_meta_box( $args ) {
       
   281     if ( ! $args )
       
   282       return;
       
   283       
       
   284     $ot_meta_box = new OT_Meta_Box( $args );
       
   285   }
       
   286 
       
   287 }
       
   288 
       
   289 /* End of file ot-meta-box-api.php */
       
   290 /* Location: ./includes/ot-meta-box-api.php */