config = json_decode( $this->config, true ); add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'admin_head', [ $this, 'admin_head' ] ); add_action( 'save_post', [ $this, 'save_post' ] ); } public function add_meta_boxes() { foreach ( $this->config['post-type'] as $screen ) { add_meta_box( sanitize_title( $this->config['title'] ), $this->config['title'], [ $this, 'add_meta_box_callback' ], $screen, $this->config['context'], $this->config['priority'] ); } } public function admin_enqueue_scripts() { global $typenow; if ( in_array( $typenow, $this->config['post-type'] ) ) { wp_enqueue_media(); } } public function admin_head() { global $typenow; if ( in_array( $typenow, $this->config['post-type'] ) ) { ?>config['fields'] as $field ) { switch ( $field['type'] ) { case 'url': if ( isset( $_POST[ $field['id'] ] ) ) { $sanitized = esc_url_raw( wp_unslash($_POST[ $field['id'] ]) ); update_post_meta( $post_id, $field['id'], $sanitized ); } break; default: if ( isset( wp_unslash($_POST)[ $field['id'] ] ) ) { $sanitized = sanitize_text_field( $_POST[ $field['id'] ] ); update_post_meta( $post_id, $field['id'], $sanitized ); } } } } public function add_meta_box_callback() { $this->fields_table(); } private function fields_table() { ?>config['fields'] as $field ) { ?>%s', $field['id'], $field['label'] ); break; default: printf( '', $field['id'], $field['label'] ); } } private function field( $field ) { switch ( $field['type'] ) { case 'media': $this->input( $field ); $this->media_button( $field ); break; default: $this->input( $field ); } } private function input( $field ) { if ( $field['type'] === 'media' ) { $field['type'] = 'text'; } printf( '', isset( $field['class'] ) ? $field['class'] : '', $field['id'], $field['id'], isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', $field['type'], $this->value( $field ) ); } private function media_button( $field ) { printf( ' ', isset( $field['modal-button'] ) ? $field['modal-button'] : __( 'Select this file', 'camp-school' ), isset( $field['modal-title'] ) ? $field['modal-title'] : __( 'Choose a file', 'camp-school' ), $field['return'], $field['id'], $field['id'], isset( $field['button-text'] ) ? $field['button-text'] : __( 'Upload', 'camp-school' ) ); } private function value( $field ) { global $post; if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { $value = get_post_meta( $post->ID, $field['id'], true ); } else if ( isset( $field['default'] ) ) { $value = $field['default']; } else { return ''; } return str_replace( '\u0027', "'", $value ); } } new author_picture; ?>