diff -r 372f2766ea20 -r bf1778c34b9a wp/wp-content/plugins/option-tree/includes/class-ot-meta-box.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wp/wp-content/plugins/option-tree/includes/class-ot-meta-box.php Mon Oct 14 18:35:50 2019 +0200 @@ -0,0 +1,375 @@ +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() + * + * @access public + * @since 1.0 + */ + public function add_meta_boxes() { + global $wp_version; + + $is_wp_5 = version_compare( $wp_version, '5.0', '>=' ); + + 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'] ); + + if ( $is_wp_5 ) { + add_filter( + 'postbox_classes_' . $page . '_' . $this->meta_box['id'], + function( $classes ) { + array_push( $classes, 'ot-meta-box' ); + return $classes; + } + ); + } + } + } + + /** + * Meta box view. + * + * @access public + * @since 1.0 + * + * @param object $post The WP_Post object. + * @param array $fields The meta box fields. + */ + public function build_meta_box( $post, $fields ) { + unset( $fields ); // @todo Check if the loop can use this param. + + echo '
'; + } + + /** + * Saves the meta box values + * + * @access public + * @since 1.0 + * + * @param int $post_id The post ID. + * @param object $post_object The WP_Post object. + * @return int|void + */ + public function save_meta_box( $post_id, $post_object ) { + global $pagenow; + + // Verify nonce. + if ( isset( $_POST[ $this->meta_box['id'] . '_nonce' ] ) && ! wp_verify_nonce( $_POST[ $this->meta_box['id'] . '_nonce' ], $this->meta_box['id'] ) ) { // phpcs:ignore + return $post_id; + } + + // Store the post global for use later. + $post_global = $_POST; + + // Don't save if $_POST is empty. + if ( empty( $post_global ) || ( isset( $post_global['vc_inline'] ) && true === $post_global['vc_inline'] ) ) { + return $post_id; + } + + // Don't save during quick edit. + if ( 'admin-ajax.php' === $pagenow ) { + 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 ( 'revision' === $post_object->post_type || 'revision.php' === $pagenow ) { + return $post_id; + } + + // Check permissions. + if ( isset( $post_global['post_type'] ) && 'page' === $post_global['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_global[ $field['id'] ] ) ) { + + // Slider and list item. + if ( in_array( $field['type'], array( 'list-item', 'slider' ), true ) ) { + + // 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(), + ), + ); + + // Convert the settings to an array. + $settings = isset( $post_global[ $field['id'] . '_settings_array' ] ) ? ot_decode( $post_global[ $field['id'] . '_settings_array' ] ) : array(); + + // Settings are empty for some odd 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_global[ $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_global[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) { + + $post_global[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $post_global[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] ); + } + } + } + + // Set up new data with validated data. + $new = $post_global[ $field['id'] ]; + + } elseif ( 'social-links' === $field['type'] ) { + + // Convert the settings to an array. + $settings = isset( $post_global[ $field['id'] . '_settings_array' ] ) ? ot_decode( $post_global[ $field['id'] . '_settings_array' ] ) : array(); + + // Settings are empty get the defaults. + if ( empty( $settings ) ) { + $settings = ot_social_links_settings( $field['id'] ); + } + + foreach ( $post_global[ $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_global[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) { + $post_global[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $post_global[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] ); + } + } + } + + // Set up new data with validated data. + $new = $post_global[ $field['id'] ]; + } else { + + // Run through validation. + $new = ot_validate_setting( $post_global[ $field['id'] ], $field['type'], $field['id'] ); + } + + // Insert CSS. + if ( 'css' === $field['type'] ) { + + if ( '' !== $new ) { + + // insert CSS into dynamic.css. + ot_insert_css_with_markers( $field['id'], $new, true ); + } else { + + // Remove old CSS from dynamic.css. + ot_remove_old_css( $field['id'] ); + } + } + } + + if ( isset( $new ) && $new !== $old ) { + update_post_meta( $post_id, $field['id'], $new ); + } elseif ( '' === $new && $old ) { + delete_post_meta( $post_id, $field['id'], $old ); + } + } + } + + } + +} + +if ( ! function_exists( 'ot_register_meta_box' ) ) { + + /** + * This method instantiates the meta box class & builds the UI. + * + * @uses OT_Meta_Box() + * + * @param array $args Meta box arguments. + * + * @access public + * @since 2.0 + */ + function ot_register_meta_box( $args ) { + if ( ! $args ) { + return; + } + + new OT_Meta_Box( $args ); + } +}