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'] ] ) ) { // 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 ); } }