Skip to content

Commit

Permalink
Workaround for core ticket #52043
Browse files Browse the repository at this point in the history
  • Loading branch information
carlomanf committed Jan 23, 2023
1 parent 13c6123 commit 9880c88
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
80 changes: 76 additions & 4 deletions src/funnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ class Funnel_Type extends Legacy_Funnel_Type
private $contributor_role;
private $is_owner = array();

/**
* If a workaround is needed for core ticket #52043, one funnel type is stored here to be assigned as the parent menu.
*
* @since 1.4.0
*/
private static $type_for_parent_menu = null;

/**
* Returns the type that is assigned to the parent menu, if set.
*
* @since 1.4.0
*/
public static function get_type_for_parent_menu()
{
return self::$type_for_parent_menu;
}

/**
* Getter.
*
* @since 1.4.0
*/
public function __get( $key )
{
if ( $key === 'slug' ) return $this->slug;
if ( $key === 'title' ) return $this->title;
}

public function __construct( $slug, $wp_id, $title, $blocks, $author )
{
parent::__construct();
Expand All @@ -31,7 +59,6 @@ public function __construct( $slug, $wp_id, $title, $blocks, $author )
unset( $this->exterior_args['labels'] );

$this->exterior_args['hierarchical'] = false;
$this->exterior_args['show_in_menu'] = $GLOBALS['wpfunnel']->is_legacy() ? 'edit.php?post_type=funnel' : 'wpfunnel';
$this->exterior_args['capability_type'] = array( $this->slug, $this->slug . '_any' );
$this->exterior_args['supports'] = array_diff( $this->exterior_args['supports'], array( 'page-attributes' ) );

Expand Down Expand Up @@ -100,11 +127,53 @@ public function register()
add_filter( 'single_template_hierarchy', array( $this, 'apply_template_to_interior' ) );
add_action( 'wp_roles_init', array( $this, 'add_role' ) );
add_filter( 'after_setup_theme', array( __CLASS__, 'regenerate_roles' ), 11 );
add_action( 'init', array( $this, 'assign_menu' ), 9 );

remove_filter( 'page_row_actions', array( $this, 'funnel_interior_edit' ), 10, 2 );
add_filter( 'post_row_actions', array( $this, 'funnel_interior_edit' ), 10, 2 );
}

/**
* Assign the admin menu for this funnel type.
*
* This must be called after the `wpfunnel_type` post type is registered at priority 8, and before this type is registered at priority 10.
*
* @since 1.4.0
*/
public function assign_menu()
{
if ( isset( self::$type_for_parent_menu ) )
{
$this->exterior_args['show_in_menu'] = 'edit.php?post_type=' . self::$type_for_parent_menu->slug;
}
else
{
$is_legacy = $GLOBALS['wpfunnel']->is_legacy();
$capability = $is_legacy ? 'edit_posts' : 'author_funnels';

if ( current_user_can( $capability ) )
{
$this->exterior_args['show_in_menu'] = $is_legacy ? 'edit.php?post_type=funnel' : 'wpfunnel';
}
else
{
if ( $is_legacy && current_user_can( 'author_funnels' ) )
{
$this->exterior_args['show_in_menu'] = 'wpfunnel';
}
else
{
if ( current_user_can( 'edit_' . $this->slug . '_any' ) )
{
self::$type_for_parent_menu = $this;
}

$this->exterior_args['show_in_menu'] = false;
}
}
}
}

public static function regenerate_roles()
{
$roles = wp_roles();
Expand Down Expand Up @@ -157,14 +226,17 @@ public function get_all_funnels()
}

/**
* Determine whether a user is the owner of this funnel type
* They are an owner if they can edit the original template
* Determine whether a user is the owner of this funnel type.
* They are an owner if they can edit the original template.
*
* Note: Since this function checks the `edit_post` meta capability, it will never return true if called before post types are registered at `init`.
*
* @since 1.2.0
*/
public function user_is_owner( $user )
{
if ( !did_action( 'init' ) || doing_action( 'init' ) )
// Avoid caching the result if post type is not registered yet.
if ( !post_type_exists( 'wpfunnel_type' ) )
{
return false;
}
Expand Down
16 changes: 15 additions & 1 deletion src/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,21 @@ public function menu()
$new_type_capability = 'author_funnels';
$parent_capability = $this->is_legacy ? 'edit_posts' : $new_type_capability;

// Workaround for core ticket #52043. If the bug is encountered, the menu for one of the funnel types needs to be registered separately here.
$type = Funnel_Type::get_type_for_parent_menu();

if ( !is_null( $type ) || $this->is_legacy && $this->is_templated && !current_user_can( $parent_capability ) && current_user_can( $new_type_capability ) )
{
$parent_capability = is_null( $type ) ? $new_type_capability : 'edit_' . $type->slug . '_any';
$parent = is_null( $type ) ? $new_type : 'edit.php?post_type=' . $type->slug;
add_menu_page( 'Funnels', 'Funnels', $parent_capability, $parent, '', 'dashicons-filter', 25 );
is_null( $type ) || add_submenu_page( $parent, $type->title, $type->title, $parent_capability, $parent, '', 20 );
}
else
{
// No workaround needed.
add_menu_page( 'Funnels', 'Funnels', $parent_capability, $parent, '', 'dashicons-filter', 25 );
}

if ( $this->is_templated )
{
Expand Down Expand Up @@ -222,7 +236,7 @@ public function run()
{
add_action( 'admin_footer', array( $this, 'no_funnels_notice' ) );
add_action( 'admin_menu', array( $this, 'menu' ), 9 );
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'init', array( $this, 'register_post_types' ), 8 );
add_action( 'init', array( $this, 'database_upgrade' ), 20 );
add_action( 'after_setup_theme', array( $this, 'register_funnel_types' ) );
add_action( 'wp_roles_init', array( $this, 'add_role' ) );
Expand Down

0 comments on commit 9880c88

Please sign in to comment.