ID; } $values = get_post_meta( $post_id, 'ct_post_meta_options' ); if ( empty( $values ) ) { $values = [ [] ]; } return $values[0]; } class Blocksy_Meta_Boxes { public function __construct() { add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); } public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'setup_meta_box' ) ); add_action( 'save_post', array( $this, 'save_meta_box' ) ); } function setup_meta_box() { // Get all public posts. $post_types = get_post_types( [ 'public' => true ] ); foreach ( $post_types as $type ) { if ( 'attachment' === $type ) { continue; } add_meta_box( 'blocksy_settings_meta_box', sprintf( // Translators: %s is the theme name. __( '%s Settings', 'ct' ), __( 'Blocksy', 'ct' ) ), function ( $post ) { $values = get_post_meta( $post->ID, 'ct_post_meta_options' ); if ( empty( $values ) ) { $values = [ [] ]; } $options = ct_get_options( 'meta/' . get_post_type( $post ) ); if ( ! $options ) { $options = ct_get_options( 'meta/default' ); } echo ct_output_options_panel( [ 'options' => $options, 'values' => $values[0], 'id_prefix' => 'ct-post-meta-options', 'name_prefix' => 'ct_post_meta_options', ] ); wp_nonce_field( basename( __FILE__ ), 'blocksy_settings_meta_box' ); }, $type, 'normal', 'default' ); } } public function save_meta_box( $post_id ) { // Checks save status. $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_POST['blocksy_settings_meta_box'] ) && wp_verify_nonce( $_POST['blocksy_settings_meta_box'], basename( __FILE__ ) ) ) ? true : false; if ( $is_autosave || $is_revision || ! $is_valid_nonce ) { return; } $values = json_decode( stripslashes( $_POST['ct_post_meta_options']['ct_options'] ), true ); update_post_meta( $post_id, 'ct_post_meta_options', $values ); } } new Blocksy_Meta_Boxes();