Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
Functions for getting, deleting, and setting the post access error me…
Browse files Browse the repository at this point in the history
…ssage.
  • Loading branch information
Justin Tadlock committed Aug 7, 2015
1 parent 5d7a728 commit ec6aca6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
51 changes: 13 additions & 38 deletions admin/meta-box-post-content-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,13 @@ function members_content_permissions_meta_box( $object, $box ) {
* @since 0.1.0
*/
function members_content_permissions_save_meta( $post_id, $post = '' ) {
global $wp_roles;

/* Fix for attachment save issue in WordPress 3.5. @link http://core.trac.wordpress.org/ticket/21963 */
// Fix for attachment save issue in WordPress 3.5. @link http://core.trac.wordpress.org/ticket/21963
if ( !is_object( $post ) )
$post = get_post();

/* Verify the nonce. */
if ( !isset( $_POST['content_permissions_meta_nonce'] ) || !wp_verify_nonce( $_POST['content_permissions_meta_nonce'], plugin_basename( __FILE__ ) ) )
return false;

if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
if ( defined('DOING_AJAX') && DOING_AJAX ) return;
if ( defined('DOING_CRON') && DOING_CRON ) return;

/* Get the post type object. */
$post_type = get_post_type_object( $post->post_type );

/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;

/* Don't save if the post is only a revision. */
if ( 'revision' == $post->post_type )
// Verify the nonce.
if ( ! isset( $_POST['content_permissions_meta_nonce'] ) || ! wp_verify_nonce( $_POST['content_permissions_meta_nonce'], plugin_basename( __FILE__ ) ) )
return;

// Get the current roles.
Expand All @@ -130,26 +114,17 @@ function members_content_permissions_save_meta( $post_id, $post = '' ) {
elseif ( !empty( $current_roles ) )
members_delete_post_roles( $post_id );

$meta = array(
'_members_access_error' => stripslashes( wp_filter_post_kses( addslashes( $_POST['members_access_error'] ) ) )
);
// Get the old access message.
$old_message = members_get_post_access_message( $post_id );

foreach ( $meta as $meta_key => $new_meta_value ) {
// Get the new message.
$new_message = isset( $_POST['members_access_error'] ) ? stripslashes( wp_filter_post_kses( addslashes( $_POST['members_access_error'] ) ) ) : '';

/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );

/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );

/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );

/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
// If we have don't have a new message but do have an old one, delete it.
if ( '' == $new_message && $old_message )
members_delete_post_access_message( $post_id );

// If the new message doesn't match the old message, set it.
else if ( $new_message !== $old_message )
members_set_post_access_message( $post_id, $new_message );
}
39 changes: 38 additions & 1 deletion inc/functions-content-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function members_content_permissions_comments( $template ) {
function members_get_post_error_message( $post_id ) {

// Get the error message for the specific post.
$message = get_post_meta( $post_id, '_members_access_error', true );
$message = members_get_post_access_message( $post_id );

// Use default error message if we don't have one for the post.
if ( ! $message )
Expand All @@ -198,6 +198,43 @@ function members_get_post_error_message( $post_id ) {
return apply_filters( 'members_post_error_message', sprintf( '<div class="members-access-error">%s</div>', $message ) );
}

/**
* Returns the post access message.
*
* @since 1.0.0
* @access public
* @param int $post_id
* @return string
*/
function members_get_post_access_message( $post_id ) {
return get_post_meta( $post_id, '_members_access_error', true );
}

/**
* Sets the post access message.
*
* @since 1.0.0
* @access public
* @param int $post_id
* @param string $message
* @return bool
*/
function members_set_post_access_message( $post_id, $message ) {
return update_post_meta( $post_id, '_members_access_error', $message );
}

/**
* Deletes the post access message.
*
* @since 1.0.0
* @access public
* @param int $post_id
* @return bool
*/
function members_delete_post_access_message( $post_id ) {
return delete_post_meta( $post_id, '_members_access_error' );
}

/**
* Converts the meta values of the old '_role' post meta key to the newer '_members_access_role' meta
* key. The reason for this change is to avoid any potential conflicts with other plugins/themes. We're
Expand Down

0 comments on commit ec6aca6

Please sign in to comment.