Skip to content

Commit

Permalink
Complete #17
Browse files Browse the repository at this point in the history
  • Loading branch information
carlomanf committed Aug 10, 2022
1 parent 67332f0 commit 4a31f6d
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 60 deletions.
109 changes: 70 additions & 39 deletions src/funnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@

class Funnel_Type extends Legacy_Funnel_Type
{
private $template;
private $wp_id;
private $title;
private $blocks;
private $author;
private $editor_role;
private $author_role;
private $contributor_role;
private $is_owner = array();

public function __construct( $slug, $template )
public function __construct( $slug, $wp_id, $title, $blocks, $author )
{
parent::__construct();

$this->slug = $slug;
$this->template = $template;
$this->wp_id = $wp_id;
$this->title = $title;
$this->blocks = $blocks;
$this->author = $author;

$this->exterior_args['label'] = $this->template->post_title;
$this->exterior_args['label'] = $this->title;

unset( $this->exterior_args['labels'] );

Expand All @@ -28,11 +35,11 @@ public function __construct( $slug, $template )
$this->exterior_args['capability_type'] = array( $this->slug, $this->slug . '_any' );
$this->exterior_args['supports'] = array_diff( $this->exterior_args['supports'], array( 'page-attributes' ) );

$this->interior_args['label'] = $this->template->post_title . ' ' . __( 'Interiors', 'wpfunnel' );
$this->interior_args['label'] = $this->title . ' ' . __( 'Interiors', 'wpfunnel' );

$this->interior_args['labels'] = array(
'name' => $this->template->post_title . ' ' . __( 'Interiors', 'wpfunnel' ),
'singular_name' => $this->template->post_title . ' ' . __( 'Interior', 'wpfunnel' )
'name' => $this->title . ' ' . __( 'Interiors', 'wpfunnel' ),
'singular_name' => $this->title . ' ' . __( 'Interior', 'wpfunnel' )
);

$this->interior_args['hierarchical'] = false;
Expand All @@ -41,7 +48,7 @@ public function __construct( $slug, $template )
$this->interior_args['supports'] = array_diff( $this->interior_args['supports'], array( 'author' ) );

$this->editor_role = array(
'name' => 'Editor for ' . $this->slug,
'name' => 'Editor for ' . $this->title,
'capabilities' => array(
'read' => true,
'upload_files' => true,
Expand All @@ -59,7 +66,7 @@ public function __construct( $slug, $template )
);

$this->author_role = array(
'name' => 'Author for ' . $this->slug,
'name' => 'Author for ' . $this->title,
'capabilities' => array(
'read' => true,
'upload_files' => true,
Expand All @@ -72,7 +79,7 @@ public function __construct( $slug, $template )
);

$this->contributor_role = array(
'name' => 'Contributor for ' . $this->slug,
'name' => 'Contributor for ' . $this->title,
'capabilities' => array(
'read' => true,
'delete_' . $this->slug . '_any' => true,
Expand All @@ -86,9 +93,9 @@ public function register()
parent::register();

add_filter( 'editable_roles', array( $this, 'make_role_editable' ) );
add_filter( 'get_the_terms', array( __CLASS__, 'localise_universal_template' ), 10, 3 );
add_filter( 'map_meta_cap', array( $this, 'assign_editor_to_owner' ), 10, 4 );
add_filter( 'pre_get_posts', array( __CLASS__, 'enable_universal_template' ) );
add_filter( 'get_block_templates', array( $this, 'add_template' ), 10, 3 );
add_filter( 'pre_get_block_template', array( $this, 'replace_template' ), 10, 3 );
add_action( 'save_post', array( $this, 'update_post_author' ), 10, 2 );
add_filter( 'single_template_hierarchy', array( $this, 'apply_template_to_interior' ) );
add_action( 'wp_roles_init', array( $this, 'add_role' ) );
Expand Down Expand Up @@ -157,15 +164,20 @@ public function get_all_funnels()
*/
public function user_is_owner( $user )
{
if ( !post_type_exists( $this->template->post_type ) )
return false;

return user_can( $user, 'edit_post', $this->template );
if ( isset( $this->is_owner[ $user ] ) && is_bool( $this->is_owner[ $user ] ) )
{
return $this->is_owner[ $user ];
}
else
{
$this->is_owner[ $user ] = user_can( $user, 'edit_post', $this->wp_id );
return $this->is_owner[ $user ];
}
}

public function make_role_editable( $roles )
{
if ( $this->user_is_owner( wp_get_current_user() ) )
if ( $this->user_is_owner( wp_get_current_user()->ID ) )
{
$roles[ $this->slug . '_editor' ] = $this->editor_role;
$roles[ $this->slug . '_author' ] = $this->author_role;
Expand All @@ -175,38 +187,57 @@ public function make_role_editable( $roles )
return $roles;
}

public static function localise_universal_template( $terms, $id, $taxonomy )
private function construct_template( &$template )
{
if ( empty( $terms ) && 'wp_theme' == $taxonomy )
{
$term = new \stdClass();
$term->name = wp_get_theme()->get_stylesheet();
$term->slug = wp_get_theme()->get_stylesheet();
$term->taxonomy = 'wp_theme';
$terms[] = new \WP_Term( $term );
}

return $terms;
$template = new \WP_Block_Template();
$template->wp_id = $this->wp_id;
$template->id = wp_get_theme()->get_stylesheet() . '//single-' . $this->slug;
$template->theme = wp_get_theme()->get_stylesheet();
$template->content = $this->blocks;
$template->slug = $this->slug;
$template->source = 'custom';
$template->type = 'wp_template';
$template->description = $this->title . ' is a WP Funnel Manager funnel type.';
$template->title = $this->title;
$template->status = 'publish';
$template->has_theme_file = false;
$template->is_custom = true;
$template->author = $this->author;
}

public static function enable_universal_template( $query )
public function add_template( $query_result, $query, $template_type )
{
if ( $query->get( 'post_type' ) === 'wp_template' )
if ( $template_type === 'wp_template' )
{
$tax_query = $query->get( 'tax_query' );
if ( !empty( $tax_query ) && 'wp_theme' === $tax_query[0]['taxonomy'] )
$id = wp_get_theme()->get_stylesheet() . '//single-' . $this->slug;

foreach ( array_keys( $query_result ) as $key )
{
$tax_query['relation'] = 'OR';
$tax_query[] = array(
'taxonomy' => 'wp_theme',
'operator' => 'NOT EXISTS'
);
if ( $query_result[ $key ]->id === $id )
{
break;
}
}

$query->set( 'tax_query', $tax_query );
if ( $query_result[ $key ]->id !== $id )
{
$key = count( $query_result );
}

$this->construct_template( $query_result[ $key ] );
}

return $query_result;
}

public function replace_template( $block_template, $id, $template_type )
{
if ( $template_type === 'wp_template' && $id === wp_get_theme()->get_stylesheet() . '//single-' . $this->slug )
{
$this->construct_template( $block_template );
}

return $query;
return $block_template;
}

/**
Expand Down
Loading

0 comments on commit 4a31f6d

Please sign in to comment.