Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Created September 29, 2015 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bappi-d-great/3491ad7e335beedf9271 to your computer and use it in GitHub Desktop.
Save bappi-d-great/3491ad7e335beedf9271 to your computer and use it in GitHub Desktop.
WPMU New Blog Template: Protect a page from editing in site created using a template
<?php
add_action( 'init', 'is_nbt_enabled' );
add_filter( 'page_row_actions', 'remove_page_row_actions', 10, 2 );
add_filter( 'get_sample_permalink_html', 'remove_edit_permalink', 10, 2 );
add_filter( 'admin_body_class', 'admin_body_class' );
add_filter( 'user_has_cap', 'lock_deletion', 0, 3 );
function is_nbt_enabled() {
if( defined( 'NBT_PLUGIN_VERSION' ) ){
if( is_super_admin() ){
add_action( 'add_meta_boxes', 'nbp_protect_page' );
}
}
}
function nbp_protect_page() {
add_meta_box( 'protect_page', 'Protect This Page', 'protect_page_cb', 'page', 'side', 'high' );
}
function protect_page_cb( $post ) {
wp_nonce_field( 'nbpt_save_meta_box_data', 'nbpt_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_nbpt_meta_value_key', true );
?>
<label for="protect_page">Protect this page from editing?</label><br><br>
<input <?php echo isset( $value ) && $value == 1 ? 'checked' : '' ?> type="radio" name="protect_page" value="1"> Yes
<input <?php echo ! isset( $value ) || $value == 0 ? 'checked' : '' ?> type="radio" name="protect_page" value="0"> No
<?php
}
add_action( 'save_post', 'nbpt_save_meta_box_data' );
function nbpt_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['nbpt_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['nbpt_meta_box_nonce'], 'nbpt_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if( ! is_super_admin() ){
return;
}
update_post_meta( $post_id, '_nbpt_meta_value_key', $_POST['protect_page'] );
}
function is_locked( $post = 0 ){
if( ! is_object( $post ) ){
if( $post == 0 ) {
global $post;
$post_id = $post->ID;
}else{
$post_id = $post;
}
}else{
$post_id = $post->ID;
}
$value = get_post_meta( $post_id, '_nbpt_meta_value_key', true );
return ( isset( $value ) && $value == 1 ) && ! is_super_admin();
}
function remove_page_row_actions( $actions, $post ) {
if( is_locked( $post ) ) {
foreach ( array( 'inline', 'inline hide-if-no-js', 'trash', 'edit' ) as $action_key ) {
if ( array_key_exists( $action_key, $actions ) ) {
unset ( $actions[ $action_key ] );
}
}
}
return $actions;
}
function remove_edit_permalink( $html, $post_id ) {
if( is_locked( $post_id ) ) {
$element_start = strpos( $html, '<span id="edit-slug-buttons"' );
$element_end = strpos( $html, '</span>', $element_start ) + 8;
$html = substr_replace( $html, '', $element_start, $element_end - $element_start );
$html = preg_replace( '#<span id="editable-post-name"[^>]*>([^<]+)</span>#', '$1', $html );
}
return $html;
}
function admin_body_class( $class ) {
if( is_locked() ) {
$class .= ' page-locked';
}
return $class;
}
function lock_deletion( $allcaps, $caps, $args ) {
$cap_check = count( $args ) ? $args[0] : '';
$user_id = count( $args ) > 1 ? $args[1] : 0;
$object_id = count( $args ) > 2 ? $args[2] : 0;
if( is_locked() ) {
foreach( $allcaps as $cap => $value ) {
if ( strpos( $cap, "delete_" ) !== false || strpos( $cap, "edit_" ) !== false ) {
unset( $allcaps[ $cap ] );
}
}
}
return $allcaps;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment