-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Post_Type_Page_Option and Post_Type_Page_State classes
- Loading branch information
Showing
9 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Types; | ||
|
||
use WP_Customize_Manager; | ||
|
||
/** | ||
* Class Post_Type_Page_Option | ||
* | ||
* Registers an option to select the page for your Custom Post Type in the Customizer. | ||
*/ | ||
class Post_Type_Page_Option { | ||
/** | ||
* Post type. | ||
* | ||
* @var string | ||
*/ | ||
private $post_type; | ||
|
||
/** | ||
* Customizer section. | ||
* | ||
* @var string | ||
*/ | ||
private $customizer_section; | ||
|
||
/** | ||
* Option name. | ||
* | ||
* @var string | ||
*/ | ||
private $option_name; | ||
|
||
/** | ||
* Post_Type_Page_Option constructor. | ||
* | ||
* @param string $post_type The post type to register the customizer section for. | ||
* @param string $customizer_section The name of the customizer section where the option to set | ||
* the page should be added to. The section already needs to | ||
* exist. | ||
*/ | ||
public function __construct( $post_type, $customizer_section ) { | ||
$this->post_type = $post_type; | ||
$this->customizer_section = $customizer_section; | ||
$this->option_name = "page_for_{$this->post_type}"; | ||
} | ||
|
||
/** | ||
* Inits hooks. | ||
*/ | ||
public function init() { | ||
if ( ! is_admin() && ! is_customize_preview() ) { | ||
return; | ||
} | ||
|
||
add_action( 'customize_register', [ $this, 'register_settings' ] ); | ||
|
||
/** | ||
* Rewrite rules need to be flushed in the next page load after the Custom Post Type was | ||
* registered. That’s why we first need to set a transient that we check on the next admin | ||
* page load. | ||
*/ | ||
add_action( | ||
"update_option_{$this->option_name}", | ||
[ $this, 'maybe_set_flush_transient' ], | ||
10, 2 | ||
); | ||
add_action( 'admin_init', [ $this, 'maybe_flush_rewrite_rules' ] ); | ||
} | ||
|
||
/** | ||
* Adds Customizer setting and control. | ||
* | ||
* @param \WP_Customize_Manager $wp_customize Customizer instance. | ||
*/ | ||
public function register_settings( WP_Customize_Manager $wp_customize ) { | ||
$post_type_object = get_post_type_object( $this->post_type ); | ||
|
||
$wp_customize->add_setting( $this->option_name, [ | ||
'type' => 'option', | ||
] ); | ||
|
||
$wp_customize->add_control( $this->option_name, [ | ||
'label' => sprintf( | ||
/* translators: Post type label. */ | ||
__( 'Page for %s', 'mind/types' ), | ||
$post_type_object->label | ||
), | ||
'section' => $this->customizer_section, | ||
'type' => 'dropdown-pages', | ||
'allow_addition' => true, | ||
] ); | ||
} | ||
|
||
/** | ||
* Sets transient to flush rewrite rules when option value changes. | ||
* | ||
* @param mixed $old_value The old option value. | ||
* @param mixed $value The new option value. | ||
*/ | ||
public function maybe_set_flush_transient( $old_value, $value ) { | ||
if ( $old_value !== $value ) { | ||
set_transient( "flush_{$this->option_name}", true ); | ||
} | ||
} | ||
|
||
/** | ||
* Flushes rewrite rules when transient is set. | ||
*/ | ||
public function maybe_flush_rewrite_rules() { | ||
$transient_name = "flush_{$this->option_name}"; | ||
|
||
if ( get_transient( $transient_name ) ) { | ||
delete_transient( $transient_name ); | ||
|
||
add_action( 'shutdown', function() { | ||
flush_rewrite_rules( false ); | ||
} ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Types; | ||
|
||
/** | ||
* Class Post_Type_Page_State | ||
*/ | ||
class Post_Type_Page_State { | ||
/** | ||
* Post type. | ||
* | ||
* @var string | ||
*/ | ||
private $post_type; | ||
|
||
/** | ||
* Option name. | ||
* | ||
* @var string | ||
*/ | ||
private $option_name; | ||
|
||
/** | ||
* Post_Type_Page_State constructor. | ||
* | ||
* @param string $post_type The post type to display the post state for. | ||
*/ | ||
public function __construct( $post_type ) { | ||
$this->post_type = $post_type; | ||
$this->option_name = "page_for_{$this->post_type}"; | ||
} | ||
|
||
/** | ||
* Inits hooks. | ||
*/ | ||
public function init() { | ||
if ( ! is_admin() ) { | ||
return; | ||
} | ||
|
||
add_filter( 'display_post_states', [ $this, 'update_post_states' ], 10, 2 ); | ||
} | ||
|
||
/** | ||
* Updates post states with page for event. | ||
* | ||
* @param string[] $post_states An array of post display states. | ||
* @param \WP_Post $post The current post object. | ||
* | ||
* @return string[] Updates post states. | ||
*/ | ||
public function update_post_states( $post_states, $post ) { | ||
$post_type_object = get_post_type_object( $this->post_type ); | ||
|
||
if ( 'page' === $post->post_type | ||
&& (int) get_option( $this->option_name ) === $post->ID | ||
) { | ||
$post_states[ $this->option_name ] = sprintf( | ||
/* translators: Post type label. */ | ||
__( 'Page for %s', 'mind/types' ), | ||
$post_type_object->label | ||
); | ||
} | ||
|
||
return $post_states; | ||
} | ||
} |