Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/fded4e4714295df85e4a6f21363f2a9b to your computer and use it in GitHub Desktop.
Save mlbd/fded4e4714295df85e4a6f21363f2a9b to your computer and use it in GitHub Desktop.
/**
* Enable support for post tags on pages.
*/
function owf_add_tags_to_pages() {
register_taxonomy_for_object_type( 'post_tag', 'page' );
}
add_action( 'init', 'owf_add_tags_to_pages' );
// Add action to both front-end and admin to conditionally hide the Beaver Builder publish button.
add_action( 'admin_head', 'owf_hide_bb_publish_button' );
add_action( 'wp_head', 'owf_hide_bb_publish_button' );
/**
* Conditionally hides the Beaver Builder "Publish" button if the current user cannot skip workflow
* and the post/page does not have the 'skip' tag.
*/
function owf_hide_bb_publish_button() {
// Only proceed if on admin screen or a single post/page on front-end
if ( ! is_admin() && ! is_singular() ) {
return;
}
global $post;
// Ensure post object is available and has an ID
if ( empty( $post ) || ! isset( $post->ID ) ) {
return;
}
$post_id = $post->ID;
// If user has permission to skip workflow or post has 'skip' tag, do not hide the button
if ( current_user_can( 'ow_skip_workflow' ) || has_tag( 'skip', $post_id ) ) {
error_log( "ow_skip_workflow $post_id" );
return;
}
// Output inline CSS to hide the Beaver Builder publish button
echo '<style>
.fl-builder-actions .fl-builder-button.fl-builder-publish-button,
.fl-builder-button-group span.fl-builder-button[data-action="publish"] {
display: none !important;
}
</style>';
}
/**
* Dynamically grants the 'ow_skip_workflow' capability if the post or page has the 'skip' tag.
*
* @param array $allcaps All the capabilities of the user.
* @param array $caps Required capabilities for the requested action.
* @param array $args Additional arguments (includes the capability name and post ID).
* @param WP_User $user The user object.
*
* @return array Modified capabilities.
*/
function owf_dynamic_skip_workflow_capability( $allcaps, $caps, $args, $user ) {
// Ensure required arguments are present.
if ( empty( $args[0] ) || empty( $args[2] ) ) {
return $allcaps;
}
$capability = $args[0];
// List of post-related capabilities to check.
$post_related_caps = array(
'edit_post',
'read_post',
'edit_posts',
'edit_page',
'read_page',
'edit_pages',
'publish_pages',
'publish_posts',
);
// Only process post-related capabilities.
if ( ! in_array( $capability, $post_related_caps ) ) {
return $allcaps;
}
$post_id = $args[2];
$post = get_post( $post_id );
// Ensure the post exists and is a valid type.
if ( ! $post || ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $allcaps;
}
// Grant skip workflow capability if the 'skip' tag is present.
if ( has_tag( 'skip', $post_id ) ) {
$allcaps['ow_skip_workflow'] = true;
}
return $allcaps;
}
add_filter( 'user_has_cap', 'owf_dynamic_skip_workflow_capability', 30, 4 );
/**
* Callback to allow skipping workflow if the post or page has the 'skip' tag.
*
* @param int $post_id The post ID.
*
* @return bool True if the workflow should be skipped.
*/
function owf_skip_workflow_cb( $post_id ) {
$post = get_post( $post_id );
if ( ! $post || ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return false;
}
if ( has_tag( 'skip', $post_id ) ) {
return true;
}
return false;
}
add_filter( 'owf_skip_workflow', 'owf_skip_workflow_cb', 10, 1 );
/**
* Modifies the workflow role applicability response to allow skipping workflow
* when the post or page has the 'skip' tag.
*
* @param array $response The original response.
* @param int $post_id The post ID.
*
* @return array Modified response with 'can_skip_workflow' set to true if applicable.
*/
function owf_filter_workflow_role_applicable_response( $response, $post_id ) {
$post = get_post( $post_id );
if ( ! $post || ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $response;
}
if ( has_tag( 'skip', $post_id ) ) {
$response['can_skip_workflow'] = true;
}
return $response;
}
add_filter( 'ow_is_role_applicable', 'owf_filter_workflow_role_applicable_response', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment