Skip to content

Commit

Permalink
Solve #13
Browse files Browse the repository at this point in the history
  • Loading branch information
carlomanf committed Feb 21, 2021
1 parent ed67a89 commit 6312b4b
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 158 deletions.
171 changes: 99 additions & 72 deletions src/funnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class Funnel_Type
{
private $slug;
private $template;
private $editor_role;
private $author_role;
private $contributor_role;
protected $editor_role;
protected $author_role;
protected $contributor_role;
protected $interior_args;
protected $exterior_args;

public function __construct( $slug, $template )
{
Expand Down Expand Up @@ -61,18 +63,89 @@ public function __construct( $slug, $template )

public function register()
{
$this->exterior_args = array(
'label' => $this->template->post_title,
'public' => true,
'hierarchical' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => $GLOBALS['wpfunnel']->is_legacy() ? 'edit.php?post_type=funnel' : 'post-new.php?post_type=wp_template&wpfunnel=1',
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'capability_type' => array( $this->slug, $this->slug . '_any' ),
'map_meta_cap' => true,
'supports' => array( 'title', 'editor', 'comments', 'revisions', 'author', 'excerpt', 'thumbnail', 'custom-fields' ),
'has_archive' => false,
'query_var' => true
);

$this->interior_args = array(
'label' => $this->template->post_title . ' ' . __( 'Interiors', 'wpfunnel' ),
'labels' => array(
'name' => $this->template->post_title . ' ' . __( 'Interiors', 'wpfunnel' ),
'singular_name' => $this->template->post_title . ' ' . __( 'Interior', 'wpfunnel' )
),
'public' => true,
'hierarchical' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
'capability_type' => array( $this->slug, $this->slug . '_any' ),
'map_meta_cap' => true,
'supports' => array( 'title', 'editor', 'comments', 'revisions', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields' ),
'has_archive' => false,
'query_var' => true
);

add_action( 'init', array( $this, 'register_taxonomies' ) );
add_filter( 'map_meta_cap', array( $this, 'assign_admin' ), 10, 4 );
add_filter( 'map_meta_cap', array( $this, 'assign_editor_to_owner' ), 10, 4 );
add_action( 'wp_roles_init', array( $this, 'add_role' ) );
add_filter( 'editable_roles', array( $this, 'make_role_editable' ) );
add_filter( 'post_row_actions', array( $this, 'funnel_interior_edit' ), 10, 2 );
add_action( 'init', array( __CLASS__, 'post_parent_query_var' ) );
add_action( 'admin_menu', array( $this, 'remove_interiors' ) );
add_filter( 'wp_insert_post_data', array( $this, 'setup_interior' ) );
add_filter( 'wp_insert_post_data', array( __CLASS__, 'validate_template_slug' ), 10, 2 );
add_action( 'save_post', array( $this, 'update_post_author' ), 10, 2 );
add_filter( 'save_post_wp_template', array( __CLASS__, 'declare_template' ) );
add_filter( 'admin_url', array( $this, 'new_interior' ), 10, 2 );
add_action( 'wp_trash_post', array( $this, 'trash_exterior_promote_interior' ) );
add_filter( 'single_template_hierarchy', array( $this, 'apply_template_to_interior' ) );
add_action( 'after_switch_theme', array( $this, 'update_theme' ) );
}

public static function declare_template( $post_id )
{
if ( isset( $_GET['wpfunnel'] ) )
{
update_post_meta( $post_id, 'wpfunnel', '1' );
}
}

public static function validate_template_slug( $data, $postarr )
{
if ( 'wp_template' === $data['post_type'] )
{
if ( isset( $_GET['wpfunnel'] ) || !empty( get_post_meta( $postarr['ID'], 'wpfunnel' ) ) )
{
if ( empty( $data['post_name'] ) )
{
$data['post_name'] = uniqid();
}

if ( strpos( $data['post_name'], 'single-' ) !== 0 )
{
$data['post_name'] = 'single-' . $data['post_name'];
}
}
}

return $data;
}

public function get_all_funnels()
Expand All @@ -98,60 +171,8 @@ public function get_all_funnels()

public function register_taxonomies()
{
$labels = array(
"name" => __( "Interiors", "wpfunnel" ),
"singular_name" => __( "Interior", "wpfunnel" ),
);

$args = array(
"label" => __( "Interiors", "wpfunnel" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => array( $this->slug, $this->slug . '_any' ),
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => $this->slug . "_int", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "comments", "revisions", "page-attributes", "custom-fields" ),
);

register_post_type( $this->slug . "_int", $args );

$args = array(
"label" => $this->slug,
"labels" => array(
"name" => $this->slug,
"singular_name" => $this->slug,
),
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => array( $this->slug, $this->slug . '_any' ),
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => $this->slug, "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "comments", "revisions", "author", "custom-fields" ),
"menu_icon" => "dashicons-filter"
);

register_post_type( $this->slug, $args );
register_post_type( $this->slug, $this->exterior_args );
register_post_type( $this->slug . '_int', $this->interior_args );
}

public function apply_template_to_interior( $templates )
Expand All @@ -162,6 +183,11 @@ public function apply_template_to_interior( $templates )
return $templates;
}

public function update_theme()
{
wp_set_post_terms( $this->template->ID, wp_get_theme()->get_stylesheet(), 'wp_theme', true );
}

/**
* Determine whether a user is the owner of this funnel type
* They are an owner if they can edit the original template
Expand All @@ -182,7 +208,7 @@ public function user_is_owner( $user )
*
* @since 1.2.0
*/
public function assign_admin( $caps, $cap, $user, $args )
public function assign_editor_to_owner( $caps, $cap, $user, $args )
{
foreach ( $caps as &$capability )
{
Expand Down Expand Up @@ -229,11 +255,6 @@ public function make_role_editable( $roles )
return $roles;
}

public function remove_interiors()
{
remove_menu_page('edit.php?post_type=' . $this->slug . '_int');
}

/**
* Add a link to view and edit funnel interiors
*
Expand Down Expand Up @@ -319,14 +340,20 @@ public function setup_interior( $data )

public function update_post_author( $post_id, $post )
{
if ( $this->slug != $post->post_type )
return;

$interiors = get_posts( 'numberposts=-1&post_status=any,trash,auto-draft&post_type=' . $this->slug . '_int&post_parent=' . $post_id );

foreach ( $interiors as $interior )
if ( $this->template->ID === $post_id )
{
wp_update_post( array( 'ID' => $interior->ID, 'post_author' => $post->post_author ) );
// In case theme was changed while plugin was inactive
$this->update_theme();
}

if ( $this->slug === $post->post_type )
{
$interiors = get_posts( 'numberposts=-1&post_status=any,trash,auto-draft&post_type=' . $this->slug . '_int&post_parent=' . $post_id );

foreach ( $interiors as $interior )
{
wp_update_post( array( 'ID' => $interior->ID, 'post_author' => $post->post_author ) );
}
}
}

Expand Down
104 changes: 35 additions & 69 deletions src/legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,40 @@ class Legacy_Funnel_Type extends Funnel_Type
{
public function __construct()
{
parent::__construct( 'funnel', null );
parent::__construct( 'funnel', (object) array( 'post_title' => '' ) );

$this->editor_role = null;
$this->author_role = null;
$this->contributor_role = null;
}

public function register_taxonomies()
public function register()
{
// Only register as hierarchical in admin
$hierarchical = is_admin();

/**
* Post Type: Funnel Interiors.
* Temporary until funnel infrastructure is ready
*/

$labels = array(
"name" => __( "Interiors", "wpfunnel" ),
"singular_name" => __( "Interior", "wpfunnel" ),
);
parent::register();

$args = array(
"label" => __( "Interiors", "wpfunnel" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => $hierarchical,
"rewrite" => array( "slug" => "funnel_int", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "comments", "revisions", "author", "page-attributes", "custom-fields" ),
);
$this->exterior_args['label'] = __( 'Funnels', 'wpfunnel' );

register_post_type( "funnel_int", $args );
$this->exterior_args['labels'] = array(
'name' => __( 'Funnels', 'wpfunnel' ),
'singular_name' => __( 'Funnel', 'wpfunnel' )
);

/**
* Post Type: Funnel Exteriors.
* Temporary until funnel infrastructure is ready
*/
$this->exterior_args['hierarchical'] = true;
$this->exterior_args['show_in_menu'] = false;
$this->exterior_args['capability_type'] = 'post';
$this->exterior_args['supports'][] = 'page-attributes';

$labels = array(
"name" => __( "Funnels", "wpfunnel" ),
"singular_name" => __( "Funnel", "wpfunnel" ),
);
$this->interior_args['label'] = __( 'Interiors', 'wpfunnel' );

$args = array(
"label" => __( "Funnels", "wpfunnel" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "funnel", "with_front" => false ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail", "comments", "revisions", "author", "page-attributes", "custom-fields" ),
"menu_icon" => "dashicons-filter"
$this->interior_args['labels'] = array(
'name' => __( 'Interiors', 'wpfunnel' ),
'singular_name' => __( 'Interior', 'wpfunnel' )
);

register_post_type( "funnel", $args );
}

public function register()
{
parent::register();
$this->interior_args['hierarchical'] = is_admin();
$this->interior_args['show_in_nav_menus'] = true;
$this->interior_args['capability_type'] = 'post';
$this->interior_args['supports'][] = 'author';

add_filter( 'post_type_link', array( $this, 'funnel_interior_permalink' ), 10, 2 );
add_filter( 'quick_edit_dropdown_pages_args', array( $this, 'funnel_post_parent' ) );
Expand All @@ -103,6 +58,12 @@ public function user_is_owner( $user )
return false;
}

// Legacy funnel type has no editors
public function assign_editor_to_owner( $caps, $cap, $user, $args )
{
return $caps;
}

// Legacy funnel type can't register roles because it borrows post capabilities
public function add_role( $roles )
{
Expand Down Expand Up @@ -159,4 +120,9 @@ public function update_post_author( $post_id, $post )
{
return;
}

public function update_theme()
{
return;
}
}
Loading

0 comments on commit 6312b4b

Please sign in to comment.