Skip to content

Commit 4e414f0

Browse files
authored
Merge pull request #139 from boxuk/fix/template-persistance/filter
[FIX] Template Persistance Disabling
2 parents c9b70a7 + cc78235 commit 4e414f0

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

packages/editor-tools/src/TemplatePersistence.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ class TemplatePersistence {
2020
* @return void
2121
*/
2222
public function init(): void {
23-
if ( apply_filters( 'boxuk_disable_template_persistence', false ) ) {
24-
return;
25-
}
26-
2723
add_action( 'save_post', array( $this, 'persist_template' ), 10, 2 );
2824
}
2925

@@ -35,7 +31,13 @@ public function init(): void {
3531
*
3632
* @return void
3733
*/
38-
public function persist_template( int $post_id, \WP_Post $post ): void {
34+
public function persist_template( int $post_id, \WP_Post $post ): void {
35+
36+
$default = wp_get_environment_type() !== 'local';
37+
if ( apply_filters( 'boxuk_disable_template_persistence', $default ) ) {
38+
return;
39+
}
40+
3941
$filename = $this->get_template_filename( $post );
4042

4143
if ( false === $filename || ! $this->is_writeable( $filename ) ) {

packages/editor-tools/tests/TestTemplatePersistence.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ public function test_init(): void {
5151
* @return void
5252
*/
5353
public function test_init_with_disabled_filter(): void {
54+
\WP_Mock::userFunction( 'wp_get_environment_type' )
55+
->once()
56+
->andReturn( 'production' );
5457
\WP_Mock::onFilter( 'boxuk_disable_template_persistence' )
55-
->with( false )
58+
->with( true )
5659
->reply( true );
5760

58-
\WP_Mock::expectActionNotAdded( 'save_post', array( $this->class_in_test, 'persist_template' ), 10, 2 );
59-
$this->class_in_test->init();
61+
$this->class_in_test->persist_template( 0, $this->getMockPost( 'wp_template' ) );
6062
$this->assertConditionsMet();
6163
}
6264

@@ -76,6 +78,13 @@ public function test_persist_template(
7678
bool $expect_template_write,
7779
string|bool $template_path
7880
) {
81+
\WP_Mock::userFunction( 'wp_get_environment_type' )
82+
->once()
83+
->andReturn( 'local' );
84+
\WP_Mock::onFilter( 'boxuk_disable_template_persistence' )
85+
->with( false )
86+
->reply( false );
87+
7988
if ( $expect_template_write ) {
8089
\WP_Mock::userFunction( 'get_stylesheet_directory' )
8190
->once()

0 commit comments

Comments
 (0)