File: /disk001/machen/www/support/wp-content/themes/wp-moose/inc/metabox.php
<?php
if ( ! function_exists( 'wp_moose_add_theme_meta_box' ) ) {
/**
* Add theme meta box
*/
function wp_moose_add_theme_meta_box() {
$post_types = [ 'post', 'page' ];
foreach ( $post_types as $type ) {
add_meta_box(
'wp-moose-theme-settings',
esc_html__( 'Moose Theme Settings', 'wp-moose' ),
'wp_moose_render_theme_settings_metabox',
$type
);
}
}
}
add_action( 'add_meta_boxes', 'wp_moose_add_theme_meta_box' );
if ( ! function_exists( 'wp_moose_render_theme_settings_metabox' ) ) {
/**
* Render theme settings meta box.
*/
function wp_moose_render_theme_settings_metabox() {
global $post;
$post_id = $post->ID;
// Meta box nonce for verification.
wp_nonce_field( basename( __FILE__ ), 'wp_moose_settings_meta_box_nonce' );
// Fetch values of current post meta.
$values = get_post_meta( $post_id, 'wp_moose_settings', true );
$fields = wp_parse_args( $values, [
'sidebar_layout' => '',
'container_style' => '',
] ); ?>
<p><strong><?php esc_html_e( 'Choose Sidebar Layout', 'wp-moose' ); ?></strong></p>
<?php
$dropdown_args = [
'id' => 'wp_moose_settings_sidebar_layout',
'name' => 'wp_moose_settings[sidebar_layout]',
'selected' => $fields['sidebar_layout'],
'add_default' => true,
];
wp_moose_render_image_radio( $dropdown_args, 'wp_moose_get_sidebar_layout_options' );
?>
<p><strong><?php esc_html_e( 'Choose Container Style', 'wp-moose' ); ?></strong></p>
<?php
$dropdown_args = [
'id' => 'wp_moose_settings_container_style',
'name' => 'wp_moose_settings[container_style]',
'selected' => $fields['container_style'],
'add_default' => true,
];
wp_moose_render_select_dropdown( $dropdown_args, 'wp_moose_get_container_style_options' );
}
}
if ( ! function_exists( 'wp_moose_save_theme_settings_meta' ) ) {
/**
* Save theme settings meta box value.
*
* @param int $post_id
* @param WP_Post $post
*
* @return void
*/
function wp_moose_save_theme_settings_meta( $post_id, $post ) {
// Verify nonce.
if (
! ( isset( $_POST['wp_moose_settings_meta_box_nonce'] )
&& wp_verify_nonce( sanitize_key( $_POST['wp_moose_settings_meta_box_nonce'] ), basename( __FILE__ ) ) )
) {
return;
}
// Bail if auto save or revision.
if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
return;
}
// Check the post being saved == the $post_id to prevent triggering this call for other save_post events.
if ( empty( $_POST['post_ID'] ) || absint( $_POST['post_ID'] ) !== $post_id ) {
return;
}
// Check permission.
if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST['wp_moose_settings'] ) && is_array( $_POST['wp_moose_settings'] ) ) {
$post_value = wp_unslash( $_POST['wp_moose_settings'] );
if ( ! array_filter( $post_value ) ) {
delete_post_meta( $post_id, 'wp_moose_settings' );
return;
}
$meta_fields = [
'sidebar_layout' => [
'type' => 'select',
],
'container_style' => [
'type' => 'select',
],
];
$sanitized_values = [];
foreach ( $post_value as $mk => $mv ) {
if ( isset( $meta_fields[ $mk ]['type'] ) ) {
switch ( $meta_fields[ $mk ]['type'] ) {
case 'select':
$sanitized_values[ $mk ] = sanitize_key( $mv );
break;
case 'checkbox':
$sanitized_values[ $mk ] = absint( $mv ) > 0 ? 1 : 0;
break;
default:
$sanitized_values[ $mk ] = sanitize_text_field( $mv );
break;
}
}
}
update_post_meta( $post_id, 'wp_moose_settings', $sanitized_values );
}
}
}
add_action( 'save_post', 'wp_moose_save_theme_settings_meta', 10, 2 );