Skip to content

Commit be5c1d9

Browse files
authored
Form Post: Fix PHPStan and PHPCS issues. (#81)
* Fix PHPStan issues for form-post file, refactor to follow PHPCS standards * Add some tests * Some more refactor * More yoda conditions * Address feedback
1 parent 26521e6 commit be5c1d9

File tree

2 files changed

+179
-31
lines changed

2 files changed

+179
-31
lines changed

includes/forms/form-post.php

+34-31
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,22 @@
66

77
if ( ! class_exists( 'ACF_Form_Post' ) ) :
88

9+
/**
10+
* Class ACF_Form_Post
11+
*
12+
* Handles the functionality for adding custom fields to post edit screens.
13+
*
14+
* @package ACF
15+
* @since ACF 5.0.0
16+
*/
917
class ACF_Form_Post {
1018

11-
/** @var string The first field groups style CSS. */
12-
var $style = '';
19+
/**
20+
* Style.
21+
*
22+
* @var string The first field groups style CSS.
23+
*/
24+
public $style = '';
1325

1426
/**
1527
* __construct
@@ -21,20 +33,20 @@ class ACF_Form_Post {
2133
*
2234
* @return void
2335
*/
24-
function __construct() {
36+
public function __construct() {
2537

2638
// initialize on post edit screens
2739
add_action( 'load-post.php', array( $this, 'initialize' ) );
2840
add_action( 'load-post-new.php', array( $this, 'initialize' ) );
2941

3042
// save
31-
add_filter( 'wp_insert_post_empty_content', array( $this, 'wp_insert_post_empty_content' ), 10, 2 );
43+
add_filter( 'wp_insert_post_empty_content', array( $this, 'wp_insert_post_empty_content' ), 10, 1 );
3244
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
3345
}
3446

3547

3648
/**
37-
* initialize
49+
* Initialize.
3850
*
3951
* Sets up Form functionality.
4052
*
@@ -43,7 +55,7 @@ function __construct() {
4355
*
4456
* @return void
4557
*/
46-
function initialize() {
58+
public function initialize() {
4759

4860
// globals
4961
global $typenow;
@@ -72,7 +84,6 @@ function initialize() {
7284
}
7385

7486
/**
75-
* add_meta_boxes
7687
*
7788
* Adds ACF metaboxes for the given $post_type and $post.
7889
*
@@ -83,7 +94,7 @@ function initialize() {
8394
* @param WP_Post $post The post being edited.
8495
* @return void
8596
*/
86-
function add_meta_boxes( $post_type, $post ) {
97+
public function add_meta_boxes( $post_type, $post ) {
8798

8899
// Storage for localized postboxes.
89100
$postboxes = array();
@@ -107,7 +118,7 @@ function add_meta_boxes( $post_type, $post ) {
107118
$priority = 'high'; // high, core, default, low
108119

109120
// Reduce priority for sidebar metaboxes for best position.
110-
if ( $context == 'side' ) {
121+
if ( 'side' === $context ) {
111122
$priority = 'core';
112123
}
113124

@@ -176,7 +187,7 @@ function add_meta_boxes( $post_type, $post ) {
176187
public function edit_form_after_title() {
177188

178189
// globals
179-
global $post, $wp_meta_boxes;
190+
global $post;
180191

181192
// render post data
182193
acf_form_data(
@@ -199,18 +210,16 @@ public function edit_form_after_title() {
199210
}
200211

201212
/**
202-
* render_meta_box
203-
*
204213
* Renders the ACF metabox HTML.
205214
*
206215
* @date 19/9/18
207216
* @since ACF 5.7.6.7.6
208217
*
209-
* @param WP_Post $post The post being edited.
210-
* @param array metabox The add_meta_box() args.
218+
* @param WP_Post $post The post being edited.
219+
* @param array $metabox The add_meta_box() args.
211220
* @return void
212221
*/
213-
function render_meta_box( $post, $metabox ) {
222+
public function render_meta_box( $post, $metabox ) {
214223

215224
// vars
216225
$id = $metabox['id'];
@@ -222,25 +231,21 @@ function render_meta_box( $post, $metabox ) {
222231
}
223232

224233
/**
225-
* wp_insert_post_empty_content
226234
*
227235
* Allows WP to insert a new post without title or post_content if ACF data exists.
228236
*
229237
* @date 16/07/2014
230238
* @since ACF 5.0.1.0.1
231239
*
232240
* @param boolean $maybe_empty Whether the post should be considered "empty".
233-
* @param array $postarr Array of post data.
234241
* @return boolean
235242
*/
236-
function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
243+
public function wp_insert_post_empty_content( $maybe_empty ) {
237244

238-
// return false and allow insert if '_acf_changed' exists
245+
// Return false and allow insert if '_acf_changed' exists.
239246
if ( $maybe_empty && acf_maybe_get_POST( '_acf_changed' ) ) {
240247
return false;
241248
}
242-
243-
// return
244249
return $maybe_empty;
245250
}
246251

@@ -255,33 +260,31 @@ function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
255260
* @param WP_Post $post The post to check.
256261
* @return boolean
257262
*/
258-
function allow_save_post( $post ) {
263+
public function allow_save_post( $post ) {
259264

260-
// vars
261265
$allow = true;
262266

263-
// restrict post types
267+
// Restricted post types.
264268
$restrict = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
265-
if ( in_array( $post->post_type, $restrict ) ) {
269+
if ( in_array( $post->post_type, $restrict, true ) ) {
266270
$allow = false;
267271
}
268272

269-
// disallow if the $_POST ID value does not match the $post->ID
273+
// Disallow if the $_POST ID value does not match the $post->ID.
270274
$form_post_id = (int) acf_maybe_get_POST( 'post_ID' );
271275
if ( $form_post_id && $form_post_id !== $post->ID ) {
272276
$allow = false;
273277
}
274278

275-
// revision (preview)
276-
if ( $post->post_type == 'revision' ) {
279+
// Revision (preview).
280+
if ( 'revision' === $post->post_type ) {
277281

278282
// allow if doing preview and this $post is a child of the $_POST ID
279-
if ( acf_maybe_get_POST( 'wp-preview' ) == 'dopreview' && $form_post_id === $post->post_parent ) {
283+
if ( 'dopreview' === acf_maybe_get_POST( 'wp-preview' ) && $form_post_id === $post->post_parent ) {
280284
$allow = true;
281285
}
282286
}
283287

284-
// return
285288
return $allow;
286289
}
287290

@@ -306,7 +309,7 @@ public function save_post( $post_id, $post ) {
306309
}
307310

308311
// Validate for published post (allow draft to save without validation).
309-
if ( $post->post_status === 'publish' ) {
312+
if ( 'publish' === $post->post_status ) {
310313
// Bail early if validation fails.
311314
if ( ! acf_validate_save_post() ) {
312315
return;
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Test Secure Custom Fields main functionality
4+
*
5+
* @package wordpress/secure-custom-fields
6+
*/
7+
8+
use WorDBless\BaseTestCase;
9+
10+
// Load the ACF_Form_Post class.
11+
require_once dirname( __DIR__, 4 ) . '/includes/forms/form-post.php';
12+
13+
/**
14+
* Class Test_Form_Post
15+
*/
16+
class Test_Form_Post extends BaseTestCase {
17+
18+
/**
19+
* Test if the ACF_Form_Post class exists.
20+
*/
21+
public function test_form_post_class_exists() {
22+
$this->assertTrue( class_exists( 'ACF_Form_Post' ), 'ACF_Form_Post class should exist' );
23+
}
24+
25+
/**
26+
* Test if the ACF_Form_Post class is properly initialized.
27+
*/
28+
public function test_form_post_initialization() {
29+
$form_post = new ACF_Form_Post();
30+
31+
$this->assertInstanceOf( 'ACF_Form_Post', $form_post, 'ACF_Form_Post should be properly initialized' );
32+
}
33+
34+
/**
35+
* Test the allow_save_post method under different scenarios.
36+
*/
37+
public function test_allow_save_post() {
38+
$form_post = new ACF_Form_Post();
39+
40+
// Test auto-draft post type (should not be allowed).
41+
$auto_draft_post = (object) array(
42+
'ID' => 1,
43+
'post_type' => 'auto-draft',
44+
);
45+
$this->assertFalse( $form_post->allow_save_post( $auto_draft_post ), 'Auto-draft posts should not be allowed' );
46+
47+
// Test regular post (should be allowed).
48+
$regular_post = (object) array(
49+
'ID' => 2,
50+
'post_type' => 'post',
51+
);
52+
$this->assertTrue( $form_post->allow_save_post( $regular_post ), 'Regular posts should be allowed' );
53+
54+
// Test ACF field group (should not be allowed).
55+
$acf_field_group = (object) array(
56+
'ID' => 3,
57+
'post_type' => 'acf-field-group',
58+
);
59+
$this->assertFalse( $form_post->allow_save_post( $acf_field_group ), 'ACF field groups should not be allowed' );
60+
}
61+
62+
/**
63+
* Test wp_insert_post_empty_content method.
64+
*/
65+
public function test_wp_insert_post_empty_content() {
66+
$form_post = new ACF_Form_Post();
67+
68+
// Should return true when no ACF data is present.
69+
$this->assertTrue(
70+
$form_post->wp_insert_post_empty_content( true ),
71+
'Should return true when no ACF data is present'
72+
);
73+
74+
// Simulate POST data with ACF changes.
75+
$_POST['_acf_changed'] = '1';
76+
$this->assertFalse(
77+
$form_post->wp_insert_post_empty_content( true ),
78+
'Should return false when ACF data is present'
79+
);
80+
unset( $_POST['_acf_changed'] );
81+
}
82+
83+
/**
84+
* Test revision handling in allow_save_post.
85+
*/
86+
public function test_allow_save_post_revision_handling() {
87+
$form_post = new ACF_Form_Post();
88+
89+
// Test regular revision (should not be allowed).
90+
$revision = (object) array(
91+
'ID' => 4,
92+
'post_type' => 'revision',
93+
'post_parent' => 10,
94+
);
95+
$this->assertFalse(
96+
$form_post->allow_save_post( $revision ),
97+
'Regular revision should not be allowed'
98+
);
99+
100+
// Test preview revision (should be allowed).
101+
$_POST['wp-preview'] = 'dopreview';
102+
$_POST['post_ID'] = 10; // Set parent post ID.
103+
$preview_revision = (object) array(
104+
'ID' => 5,
105+
'post_type' => 'revision',
106+
'post_parent' => 10,
107+
);
108+
$this->assertTrue(
109+
$form_post->allow_save_post( $preview_revision ),
110+
'Preview revision should be allowed when parent matches'
111+
);
112+
113+
// Cleanup
114+
unset( $_POST['wp-preview'] );
115+
unset( $_POST['post_ID'] );
116+
}
117+
118+
/**
119+
* Test post ID validation in allow_save_post.
120+
*/
121+
public function test_allow_save_post_id_validation() {
122+
$form_post = new ACF_Form_Post();
123+
124+
// Test when POST ID doesn't match post ID
125+
$_POST['post_ID'] = 999;
126+
$post = (object) array(
127+
'ID' => 123,
128+
'post_type' => 'post',
129+
);
130+
$this->assertFalse(
131+
$form_post->allow_save_post( $post ),
132+
'Should not allow save when POST ID differs from post ID'
133+
);
134+
135+
// Test when POST ID matches post ID
136+
$_POST['post_ID'] = 123;
137+
$this->assertTrue(
138+
$form_post->allow_save_post( $post ),
139+
'Should allow save when POST ID matches post ID'
140+
);
141+
142+
// Cleanup
143+
unset( $_POST['post_ID'] );
144+
}
145+
}

0 commit comments

Comments
 (0)