wp/wp-content/plugins/option-tree/includes/ot-meta-box-api.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    28      * @since     1.0
    28      * @since     1.0
    29      */
    29      */
    30     function __construct( $meta_box ) {
    30     function __construct( $meta_box ) {
    31       if ( ! is_admin() )
    31       if ( ! is_admin() )
    32         return;
    32         return;
    33         
    33 
       
    34       global $ot_meta_boxes;
       
    35 
       
    36       if ( ! isset( $ot_meta_boxes ) ) {
       
    37         $ot_meta_boxes = array();
       
    38       }
       
    39 
       
    40       $ot_meta_boxes[] = $meta_box;
       
    41 
    34       $this->meta_box = $meta_box;
    42       $this->meta_box = $meta_box;
    35       
    43 
    36       add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    44       add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    37       
    45 
    38       add_action( 'save_post', array( $this, 'save_meta_box' ), 1, 2 );
    46       add_action( 'save_post', array( $this, 'save_meta_box' ), 1, 2 );
    39       
    47 
    40     }
    48     }
    41     
    49     
    42     /**
    50     /**
    43      * Adds meta box to any post type
    51      * Adds meta box to any post type
    44      *
    52      *
    62      *
    70      *
    63      * @access    public
    71      * @access    public
    64      * @since     1.0
    72      * @since     1.0
    65      */
    73      */
    66     function build_meta_box( $post, $metabox ) {
    74     function build_meta_box( $post, $metabox ) {
    67       
    75 
    68       echo '<div class="ot-metabox-wrapper">';
    76       echo '<div class="ot-metabox-wrapper">';
    69            
    77 
    70         /* Use nonce for verification */
    78         /* Use nonce for verification */
    71         echo '<input type="hidden" name="' . $this->meta_box['id'] . '_nonce" value="' . wp_create_nonce( $this->meta_box['id'] ) . '" />';
    79         echo '<input type="hidden" name="' . $this->meta_box['id'] . '_nonce" value="' . wp_create_nonce( $this->meta_box['id'] ) . '" />';
    72         
    80         
    73         /* meta box description */
    81         /* 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>' : '';
    82         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>' : '';
    95             'field_rows'        => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10,
   103             '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',
   104             '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',
   105             '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',
   106             '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'] : '',
   107             'field_class'       => isset( $field['class'] ) ? $field['class'] : '',
       
   108             'field_condition'   => isset( $field['condition'] ) ? $field['condition'] : '',
       
   109             'field_operator'    => isset( $field['operator'] ) ? $field['operator'] : 'and',
   100             'field_choices'     => isset( $field['choices'] ) ? $field['choices'] : array(),
   110             'field_choices'     => isset( $field['choices'] ) ? $field['choices'] : array(),
   101             'field_settings'    => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(),
   111             'field_settings'    => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(),
   102             'post_id'           => $post->ID,
   112             'post_id'           => $post->ID,
   103             'meta'              => true
   113             'meta'              => true
   104           );
   114           );
   105           
   115           
       
   116           $conditions = '';
       
   117           
       
   118           /* setup the conditions */
       
   119           if ( isset( $field['condition'] ) && ! empty( $field['condition'] ) ) {
       
   120   
       
   121             $conditions = ' data-condition="' . $field['condition'] . '"';
       
   122             $conditions.= isset( $field['operator'] ) && in_array( $field['operator'], array( 'and', 'AND', 'or', 'OR' ) ) ? ' data-operator="' . $field['operator'] . '"' : '';
       
   123   
       
   124           }
       
   125           
   106           /* only allow simple textarea due to DOM issues with wp_editor() */
   126           /* only allow simple textarea due to DOM issues with wp_editor() */
   107           if ( $_args['type'] == 'textarea' )
   127           if ( apply_filters( 'ot_override_forced_textarea_simple', false, $field['id'] ) == false && $_args['type'] == 'textarea' )
   108             $_args['type'] = 'textarea-simple';
   128             $_args['type'] = 'textarea-simple';
       
   129 
       
   130           // Build the setting CSS class
       
   131           if ( ! empty( $_args['field_class'] ) ) {
       
   132             
       
   133             $classes = explode( ' ', $_args['field_class'] );
       
   134 
       
   135             foreach( $classes as $key => $value ) {
       
   136             
       
   137               $classes[$key] = $value . '-wrap';
       
   138               
       
   139             }
       
   140 
       
   141             $class = 'format-settings ' . implode( ' ', $classes );
       
   142             
       
   143           } else {
       
   144           
       
   145             $class = 'format-settings';
       
   146             
       
   147           }
   109           
   148           
   110           /* option label */
   149           /* option label */
   111           echo '<div class="format-settings">';
   150           echo '<div id="setting_' . $field['id'] . '" class="' . $class . '"' . $conditions . '>';
   112             
   151             
   113             /* don't show title with textblocks */
   152             echo '<div class="format-setting-wrap">';
   114             if ( $_args['type'] != 'textblock' && ! empty( $field['label'] ) ) {
   153             
   115               echo '<div class="format-setting-label">';
   154               /* don't show title with textblocks */
   116                 echo '<label for="' . $_args['field_id'] . '" class="label">' . $field['label'] . '</label>';
   155               if ( $_args['type'] != 'textblock' && ! empty( $field['label'] ) ) {
   117               echo '</div>';
   156                 echo '<div class="format-setting-label">';
   118             }
   157                   echo '<label for="' . $field['id'] . '" class="label">' . $field['label'] . '</label>';
   119       
   158                 echo '</div>';
   120             /* get the option HTML */
   159               }
   121             echo ot_display_by_type( $_args );
   160       
       
   161               /* get the option HTML */
       
   162               echo ot_display_by_type( $_args );
       
   163               
       
   164             echo '</div>';
   122             
   165             
   123           echo '</div>';
   166           echo '</div>';
   124           
   167           
   125         }
   168         }
       
   169 
       
   170         echo '<div class="clear"></div>';
   126       
   171       
   127       echo '</div>';
   172       echo '</div>';
   128 
   173 
   129     }
   174     }
   130     
   175     
   138      */
   183      */
   139     function save_meta_box( $post_id, $post_object ) {
   184     function save_meta_box( $post_id, $post_object ) {
   140       global $pagenow;
   185       global $pagenow;
   141 
   186 
   142       /* don't save if $_POST is empty */
   187       /* don't save if $_POST is empty */
   143       if ( empty( $_POST ) )
   188       if ( empty( $_POST ) || ( isset( $_POST['vc_inline'] ) && $_POST['vc_inline'] == true ) )
   144         return $post_id;
   189         return $post_id;
   145       
   190       
   146       /* don't save during quick edit */
   191       /* don't save during quick edit */
   147       if ( $pagenow == 'admin-ajax.php' )
   192       if ( $pagenow == 'admin-ajax.php' )
   148         return $post_id;
   193         return $post_id;
   222             
   267             
   223             }
   268             }
   224             
   269             
   225             /* set up new data with validated data */
   270             /* set up new data with validated data */
   226             $new = $_POST[$field['id']];
   271             $new = $_POST[$field['id']];
       
   272           
       
   273           } else if ( $field['type'] == 'social-links' ) {
       
   274             
       
   275             /* get the settings array */
       
   276             $settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( ot_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array();
       
   277             
       
   278             /* settings are empty get the defaults */
       
   279             if ( empty( $settings ) ) {
       
   280               $settings = ot_social_links_settings( $field['id'] );
       
   281             }
       
   282             
       
   283             foreach( $_POST[$field['id']] as $k => $setting_array ) {
       
   284   
       
   285               foreach( $settings as $sub_setting ) {
       
   286                 
       
   287                 /* verify sub setting has a type & value */
       
   288                 if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) {
       
   289                   
       
   290                   $_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'] );
       
   291                   
       
   292                 }
       
   293                 
       
   294               }
       
   295             
       
   296             }
       
   297             
       
   298             /* set up new data with validated data */
       
   299             $new = $_POST[$field['id']];
   227 
   300 
   228           } else {
   301           } else {
   229             
   302             
   230             /* run through validattion */
   303             /* run through validattion */
   231             $new = ot_validate_setting( $_POST[$field['id']], $field['type'], $field['id'] );
   304             $new = ot_validate_setting( $_POST[$field['id']], $field['type'], $field['id'] );
   249           
   322           
   250           }
   323           }
   251         
   324         
   252         }
   325         }
   253         
   326         
   254         if ( $new && $new !== $old ) {
   327         if ( isset( $new ) && $new !== $old ) {
   255           update_post_meta( $post_id, $field['id'], $new );
   328           update_post_meta( $post_id, $field['id'], $new );
   256         } else if ( '' == $new && $old ) {
   329         } else if ( '' == $new && $old ) {
   257           delete_post_meta( $post_id, $field['id'], $old );
   330           delete_post_meta( $post_id, $field['id'], $old );
   258         }
   331         }
   259       }
   332       }