Skip to content

Commit b420c0f

Browse files
committed
Refactor of Template Resolver and added test.
1 parent 6d32947 commit b420c0f

File tree

15 files changed

+438
-140
lines changed

15 files changed

+438
-140
lines changed

.github/actions/codeception/action.yml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,6 @@ runs:
6767
WP_VERSION: ${{ inputs.wordpress }}
6868
PHP_VERSION: ${{ inputs.php }}
6969

70-
- name: Run Acceptance Tests w/ Docker
71-
working-directory: ${{ inputs.working-directory }}
72-
shell: bash
73-
run: |
74-
docker exec \
75-
--env DEBUG=${{ env.DEBUG }} \
76-
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
77-
--env SUITES=acceptance \
78-
$(docker compose ps -q wordpress) \
79-
bash -c "cd wp-content/plugins/$(basename $(echo ${{ inputs.working-directory }} | sed 's:/*$::')) && bin/run-codeception.sh"
80-
env:
81-
DEBUG: ${{ env.ACTIONS_STEP_DEBUG }}
82-
SKIP_TESTS_CLEANUP: "true"
83-
continue-on-error: true
84-
85-
- name: Run Functional Tests w/ Docker
86-
working-directory: ${{ inputs.working-directory }}
87-
shell: bash
88-
run: |
89-
docker exec \
90-
--env DEBUG=${{ env.DEBUG }} \
91-
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
92-
--env SUITES=functional \
93-
$(docker compose ps -q wordpress) \
94-
bash -c "cd wp-content/plugins/$(basename ${{ inputs.working-directory }}) && bin/run-codeception.sh"
95-
env:
96-
DEBUG: ${{ env.ACTIONS_STEP_DEBUG }}
97-
SKIP_TESTS_CLEANUP: "true"
98-
continue-on-error: true
99-
10070
- name: Run WPUnit Tests w/ Docker
10171
working-directory: ${{ inputs.working-directory }}
10272
shell: bash

plugins/hwp-previews/TESTING.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,7 @@ tests/
128128
├── _envs/ # Environment configs
129129
├── _output/ # Test output (logs, coverage)
130130
├── _support/ # Helper classes, modules
131-
├── acceptance/ # Acceptance test cases
132-
├── functional/ # Functional test cases
133-
├── unit/ # Unit test cases
134131
├── wpunit/ # WPUnit (WordPress-aware unit/integration) test cases
135-
├── acceptance.suite.dist.yml
136-
├── functional.suite.dist.yml
137-
├── unit.suite.dist.yml
138132
├── wpunit.suite.dist.yml
139133
└── wpunit/
140134
└── bootstrap.php # Bootstrap for WPUnit tests

plugins/hwp-previews/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"php:psalm": "psalm",
130130
"php:psalm:info": "psalm --show-info=true",
131131
"php:psalm:fix": "psalm --alter",
132+
"qa": "sh bin/local/run-qa.sh",
132133
"test": [
133134
"sh bin/local/run-unit-tests.sh",
134135
"sh bin/local/run-e2e-tests.sh"

plugins/hwp-previews/src/Admin/Settings/Fields/Field/URL_Input_Field.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ private function fix_url( string $value ): string {
5252
// Remove <script> tags and their content to prevent XSS.
5353
$value = preg_replace( '#<script.*?>.*?</script>#is', '', $value );
5454

55+
if ( ! is_string( $value ) || '' === trim( $value ) ) {
56+
return '';
57+
}
58+
5559
// Remove HTML tags except curly braces, trim, encode spaces, add protocol.
5660
$value = preg_replace( '/<(?!\{)[^>]+>/', '', $value );
5761
$value = trim( str_replace( ' ', '%20', (string) $value ) );

plugins/hwp-previews/src/Admin/Settings_Page.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function register_settings_pages(): void {
8080
__( 'HWP Previews Settings', 'hwp-previews' ),
8181
'HWP Previews',
8282
self::PLUGIN_MENU_SLUG,
83-
trailingslashit(HWP_PREVIEWS_PLUGIN_DIR) . 'src/Templates/admin.php',
83+
trailingslashit( HWP_PREVIEWS_PLUGIN_DIR ) . 'src/Templates/admin.php',
8484
[
8585
'hwp_previews_main_page_config' => [
8686
'tabs' => $post_types,

plugins/hwp-previews/src/Hooks/Preview_Hooks.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
declare( strict_types=1 );
3+
declare(strict_types=1);
44

55
namespace HWP\Previews\Hooks;
66

@@ -59,6 +59,9 @@ class Preview_Hooks {
5959
* Initializes the settings helper, post types and statuses configurations, and the preview link service.
6060
*/
6161
public function __construct() {
62+
63+
// @TODO - Refactor to use a factory or service locator pattern and ananlyze what is is actually needed.
64+
6265
$this->settings_helper = Settings_Helper::get_instance();
6366

6467
$this->types_config = apply_filters(
@@ -190,8 +193,25 @@ public function filter_rest_prepare_link( WP_REST_Response $response, WP_Post $p
190193
*/
191194
public function add_iframe_preview_template( string $template ): string {
192195

193-
$template_resolver = new Template_Resolver();
196+
if ( ! is_preview() ) {
197+
return $template;
198+
}
194199

200+
$post = get_post();
201+
if ( ! is_object( $post ) || ! ( $post instanceof WP_Post ) ) {
202+
return $template;
203+
}
204+
205+
// @TODO - Refactor as should be part of the main preview settings.
206+
$settings_helper = Settings_Helper::get_instance();
207+
if ( ! $settings_helper->in_iframe( $post->post_type ) ) {
208+
return $template;
209+
}
210+
211+
// @TODO - Refactor as part of types and post statuses config refactor.
212+
$post_types = $this->types_config->get_post_types();
213+
$post_statuses = $this->statuses_config->get_post_statuses();
214+
$template_resolver = new Template_Resolver( $post, $post_types, $post_statuses );
195215

196216
if ( ! $template_resolver->is_allowed() ) {
197217
return $template;
@@ -202,13 +222,8 @@ public function add_iframe_preview_template( string $template ): string {
202222
return $template;
203223
}
204224

205-
// @TODO remove
206-
// @TODO how do we get this URL with Preview_URL_Generator?
207-
$post = $template_resolver->get_post();
208-
set_query_var( $template_resolver::HWP_PREVIEWS_IFRAME_PREVIEW_URL, self::generate_preview_url( $post ) );
209-
210-
// @TODO - Add back in.
211-
// $template_resolver->set_query_variable( $iframe_template );
225+
// @TODO - Refactor this as part of URL generation refactor.
226+
$template_resolver->set_query_variable( self::generate_preview_url( $post ) );
212227

213228
return $iframe_template;
214229
}

plugins/hwp-previews/src/Preview/Template_Resolver.php

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace HWP\Previews\Preview;
66

7-
use HWP\Previews\Preview\Helper\Settings_Helper;
87
use WP_Post;
98

109
class Template_Resolver {
@@ -18,54 +17,69 @@ class Template_Resolver {
1817
/**
1918
* The current post object.
2019
*
21-
* @var \WP_Post|null
20+
* @var \WP_Post
2221
*/
23-
protected ?WP_Post $post = null;
22+
protected WP_Post $post;
2423

25-
public function __construct() {
26-
$post = get_post();
27-
if ( $post instanceof WP_Post ) {
28-
$this->post = $post;
29-
}
24+
/**
25+
* Allowed post types for preview links.
26+
*
27+
* @var array<string>
28+
*/
29+
private array $post_types = [];
30+
31+
/**
32+
* Allowed post statuses for preview links.
33+
*
34+
* @var array<string>
35+
*/
36+
private array $post_statuses = [];
37+
38+
/**
39+
* Constructor.
40+
*
41+
* @param \WP_Post $post The post object.
42+
* @param array<string> $post_types Post types that are applicable for preview links.
43+
* @param array<string> $post_statuses Post statuses that are applicable for preview links.
44+
*/
45+
public function __construct( WP_Post $post, array $post_types = [], array $post_statuses = [] ) {
46+
$this->post = $post;
47+
$this->post_types = $post_types;
48+
$this->post_statuses = $post_statuses;
3049
}
3150

51+
/**
52+
* Check if the current post is allowed for preview in an iframe.
53+
*
54+
* @return bool True if the post is allowed, false otherwise.
55+
*/
3256
public function is_allowed(): bool {
3357

34-
if ( ! is_preview() ) {
58+
$post_type = $this->post->post_type;
59+
$post_types = $this->post_types;
60+
if ( ! in_array( $post_type, $post_types, true ) ) {
3561
return false;
3662
}
3763

38-
if ( ! $this->post instanceof WP_Post ) {
39-
return false;
40-
}
64+
$post_status = $this->post->post_status;
65+
$post_statuses = $this->post_statuses;
4166

42-
// @TODO check
43-
// if (
44-
// empty( $template_path ) ||
45-
// ! $this->types->is_post_type_applicable( $post->post_type ) ||
46-
// ! $this->statuses->is_post_status_applicable( $post->post_status ) ||
47-
// ! is_preview()
48-
// ) {
49-
// return '';
50-
// }
51-
52-
$settings_helper = Settings_Helper::get_instance();
53-
if ( ! $settings_helper->in_iframe( $this->post->post_type ) ) {
67+
if ( ! in_array( $post_status, $post_statuses, true ) ) {
5468
return false;
5569
}
5670

57-
58-
return is_preview();
71+
return true;
5972
}
6073

61-
public function get_post(): ?WP_Post {
62-
return $this->post;
63-
}
64-
65-
public function get_iframe_template(): string {
74+
/**
75+
* Get the path to the iframe template.
76+
*
77+
* @return string|null The path to the iframe template, or null if it does not exist.
78+
*/
79+
public function get_iframe_template(): ?string {
6680

6781
/**
68-
* The filter 'hwp_previews_template_path' allows to change the template directory path.
82+
* The filter 'hwp_previews_template_path' allows changing the template file path.
6983
*/
7084
$template_dir_path = (string) apply_filters(
7185
'hwp_previews_template_path',

plugins/hwp-previews/src/Preview/URL_Generator.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

plugins/hwp-previews/src/Templates/iframe.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
declare(strict_types=1);
1111

1212
$hwp_previews_url_template = \HWP\Previews\Preview\Template_Resolver::get_query_variable();
13-
1413
?>
1514

1615
<!DOCTYPE html>

plugins/hwp-previews/tests/acceptance.suite.dist.yml

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)