Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Menu Swapper adaption for Custom Bulk/Quick Edit
<?php
/**
* Menu Swapper adaption for Custom Bulk/Quick Edit Premium
*
* @author Michael Cannon <mc@aihr.us>
* @ref http://wordpress.org/plugins/menu-swapper/
* @ref http://wordpress.org/plugins/custom-bulkquick-edit/
* @ref http://aihr.us/products/custom-bulkquick-edit-premium-wordpress-plugin/
*/
add_filter( 'manage_page_posts_columns', 'bhc_manage_page_posts_columns' );
function bhc_manage_page_posts_columns( $columns ) {
$columns['mswp-target-theme-loc'] = esc_html__( 'Replace Theme Location' );
$columns['mswp-swap-theme-loc'] = esc_html__( 'With Theme Location' );
return $columns;
}
add_filter( 'manage_post_posts_columns', 'bhc_manage_post_posts_columns' );
function bhc_manage_post_posts_columns( $columns ) {
$columns['mswp-target-theme-loc'] = esc_html__( 'Replace Theme Location' );
$columns['mswp-swap-theme-loc'] = esc_html__( 'With Theme Location' );
return $columns;
}
add_filter( 'cbqe_post_types_ignore', 'bhc_cbqe_post_types_ignore' );
function bhc_cbqe_post_types_ignore( $post_types ) {
foreach ( $post_types as $key => $value ) {
if ( 'page' == $value )
unset( $post_types[ $key ] );
}
return $post_types;
}
add_filter( 'cbqe_settings_as_types', 'bhc_settings_as_types' );
function bhc_settings_as_types( $as_types ) {
$as_types['menu_swapper_target'] = esc_html__( 'As menu swapper target' );
$as_types['menu_swapper_swap'] = esc_html__( 'As menu swapper swap' );
return $as_types;
}
add_filter( 'cbqe_configuration_default', 'bhc_configuration_default', 10, 3 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function bhc_configuration_default( $default, $id, $type ) {
switch ( $type ) {
case 'menu_swapper_target':
$default = array();
$default[] = 'none|None (Swapper will not affect menu)';
$default[] = 'all|Any/All (will affect all menus)';
$theme_locations = get_registered_nav_menus();
foreach ( $theme_locations as $slug => $name ) {
$default[] = $slug . '|' . $name;
}
$default = implode( "\n", $default );
break;
case 'menu_swapper_swap':
$default = array();
$theme_locations = get_registered_nav_menus();
foreach ( $theme_locations as $slug => $name ) {
$default[] = $slug . '|' . $name;
}
$default = implode( "\n", $default );
break;
}
return $default;
}
add_filter( 'cbqe_quick_edit_custom_box_field', 'bhc_quick_edit_custom_box_field', 10, 5 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function bhc_quick_edit_custom_box_field( $input, $field_type, $field_name, $options, $bulk_mode ) {
$column_name = str_replace( Custom_Bulkquick_Edit::SLUG, '', $field_name );
$field_name_var = str_replace( '-', '_', $field_name );
$result = $input;
switch ( $field_type ) {
case 'menu_swapper_swap':
case 'menu_swapper_target':
$result = Custom_Bulkquick_Edit::custom_box_select( $column_name, $field_name, $field_name_var, $options, $bulk_mode );
break;
}
return $result;
}
add_filter( 'cbqe_manage_posts_custom_column_field_type', 'bhc_manage_posts_custom_column_field_type', 10, 4 );
/**
*
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
function bhc_manage_posts_custom_column_field_type( $current, $field_type, $column, $post_id ) {
global $post;
$result = $current;
switch ( $field_type ) {
case 'menu_swapper_swap':
case 'menu_swapper_target':
$details = Custom_Bulkquick_Edit::get_field_config( $post->post_type, $column );
$options = explode( "\n", $details );
$result = Custom_Bulkquick_Edit::column_select( $column, $current, $options, $field_type );
break;
}
return $result;
}
add_action( 'cbqe_save_post', 'bhc_save_post' );
function bhc_save_post( $post_id ) {
if ( ! isset( $_POST[ Custom_Bulkquick_Edit::SLUG . 'mswp-swap-theme-loc' ] ) )
return;
// check permissions
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;
// SWAP
$old = get_post_meta( $post_id, MSWP_LOC_POST_META, true );
$new = $_POST[ Custom_Bulkquick_Edit::SLUG . 'mswp-swap-theme-loc' ];
if ( $new && $new != $old ) {
update_post_meta( $post_id, MSWP_LOC_POST_META, $new );
} elseif ( '' == $new && $old ) {
delete_post_meta( $post_id, MSWP_LOC_POST_META, $old );
}
// TARGET
$old = get_post_meta( $post_id, MSWP_TARGET_POST_META, true );
$new = $_POST[ Custom_Bulkquick_Edit::SLUG . 'mswp-target-theme-loc' ];
if ( $new && $new != $old ) {
update_post_meta( $post_id, MSWP_TARGET_POST_META, $new );
} elseif ( '' == $new && $old ) {
delete_post_meta( $post_id, MSWP_TARGET_POST_META, $old );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment