Skip to content

Commit 8a0ceb4

Browse files
authored
Forms: add docs (#46047)
* add comprehensive docs * changelog
1 parent 8a450fa commit 8a0ceb4

File tree

10 files changed

+3766
-0
lines changed

10 files changed

+3766
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: added
3+
4+
Forms: add docs on classes and usage
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Jetpack Forms Documentation
2+
3+
Welcome to the Jetpack Forms package documentation. This package provides WordPress form functionality including contact forms, feedback storage, and form processing.
4+
5+
## Core Classes
6+
7+
### Feedback System
8+
9+
The feedback system handles form submission data storage and retrieval:
10+
11+
- **[Feedback](feedback.md)** ([Class Reference](feedback-class.md)) - Main class representing a form submission stored as a custom WordPress post (`feedback` post type). Provides methods for creating, retrieving, and managing form responses including author info, field data, and submission metadata.
12+
13+
- **[Feedback_Field](feedback-field.md)** ([Class Reference](feedback-field-class.md)) - Represents an individual form field submission with its label, value, and type. Handles context-specific rendering for emails, CSV exports, API responses, and web display.
14+
15+
- **[Feedback_Author](feedback-author.md)** ([Class Reference](feedback-author-class.md)) - Manages submitter information including name, email, URL, and avatar. Integrates with WordPress comment filters for consistent data handling.
16+
17+
- **[Feedback_Source](feedback-source.md)** ([Class Reference](feedback-source-class.md)) - Tracks the source context of a form submission (post, page, widget, or block template). Provides permalinks and edit URLs based on submission origin.
18+
19+
### Form System
20+
21+
Classes that handle form rendering and processing:
22+
23+
- **Contact_Form** - Core form class that parses shortcodes, validates fields, and processes submissions. Handles form attributes, field definitions, and submission workflow.
24+
25+
- **Contact_Form_Field** - Represents a form field definition (not the submitted data). Defines field properties like type, label, validation rules, and rendering options.
26+
27+
- **Contact_Form_Shortcode** - Handles WordPress shortcode parsing for `[contact-form]` and `[contact-field]` shortcodes. Converts shortcode attributes into form objects.
28+
29+
- **Contact_Form_Plugin** - Main plugin class that initializes the forms system, registers hooks, and coordinates between components.
30+
31+
- **Contact_Form_Endpoint** - REST API endpoint handler for form submissions. Processes AJAX submissions and returns JSON responses.
32+
33+
### UI Components
34+
35+
Classes for admin and user-facing interfaces:
36+
37+
- **Admin** - Admin interface for viewing and managing form submissions. Provides list tables and detail views for feedback posts.
38+
39+
- **Editor_View** - Handles form display in the block editor. Provides live previews and editing capabilities.
40+
41+
- **Form_View** - Renders forms on the frontend. Handles HTML generation, validation display, and success messages.
42+
43+
### Utilities
44+
45+
Helper classes:
46+
47+
- **Util** - Utility functions for common operations like sanitization, validation, and data formatting.
48+
49+
- **Form_Submission_Error** - Exception class for handling form submission errors. Provides structured error information for validation failures and processing issues.
50+
51+
## Quick Start
52+
53+
### Creating a Form Submission
54+
55+
```php
56+
use Automattic\Jetpack\Forms\ContactForm\Feedback;
57+
use Automattic\Jetpack\Forms\ContactForm\Contact_Form;
58+
59+
$form = new Contact_Form( $attributes, $content );
60+
$feedback = Feedback::from_submission( $_POST, $form, get_post() );
61+
$post_id = $feedback->save();
62+
```
63+
64+
### Retrieving a Submission
65+
66+
```php
67+
$feedback = Feedback::get( $post_id );
68+
echo $feedback->get_author();
69+
echo $feedback->get_author_email();
70+
71+
foreach ( $feedback->get_fields() as $field ) {
72+
echo $field->get_label() . ': ' . $field->get_render_value();
73+
}
74+
```
75+
76+
## Testing
77+
78+
Tests are located in `tests/php/contact-form/`:
79+
- `Feedback_Test.php` - Comprehensive feedback system tests
80+
- `Feedback_Field_Test.php` - Field handling tests
81+
- `Contact_Form_Test.php` - Form processing tests
82+
83+
Run tests:
84+
```bash
85+
jetpack docker phpunit projects/packages/forms
86+
```
87+
88+
## File Structure
89+
90+
```
91+
src/contact-form/
92+
├── class-feedback.php # Main feedback class
93+
├── class-feedback-field.php # Field data class
94+
├── class-feedback-author.php # Author info class
95+
├── class-feedback-source.php # Source context class
96+
├── class-contact-form.php # Form definition class
97+
├── class-contact-form-field.php # Field definition class
98+
└── ...
99+
100+
tests/php/contact-form/
101+
├── Feedback_Test.php
102+
├── Feedback_Field_Test.php
103+
└── ...
104+
105+
docs/
106+
├── README.md # This file
107+
├── feedback.md # Feedback class docs
108+
├── feedback-field.md # Field class docs
109+
├── feedback-author.md # Author class docs
110+
└── feedback-source.md # Source class docs
111+
```
112+
113+
## Additional Resources
114+
115+
- [Jetpack Forms Block Editor Integration](../README.md)
116+
- [Form Submission Workflow](#) (TBD)
117+
- [Security and Validation](#) (TBD)
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Feedback_Author Class Reference
2+
3+
**[→ User Guide](feedback-author.md)** | **[← Back to Index](README.md)**
4+
5+
Technical reference for `Automattic\Jetpack\Forms\ContactForm\Feedback_Author`
6+
7+
**Source:** [`class-feedback-author.php`](../src/contact-form/class-feedback-author.php)
8+
9+
## Constructor
10+
11+
### `__construct()`
12+
[📍 Source](../src/contact-form/class-feedback-author.php#L61)
13+
14+
```php
15+
public function __construct(
16+
string $name = '',
17+
string $email = '',
18+
string $url = '',
19+
string $first_name = '',
20+
string $last_name = ''
21+
)
22+
```
23+
24+
**Parameters:**
25+
- `$name` (string) - The name of the author
26+
- `$email` (string) - The email of the author
27+
- `$url` (string) - The URL of the author
28+
- `$first_name` (string) - The first name of the author
29+
- `$last_name` (string) - The last name of the author
30+
31+
All parameters are optional and default to empty strings.
32+
33+
---
34+
35+
## Static Methods
36+
37+
### `from_submission()`
38+
[📍 Source](../src/contact-form/class-feedback-author.php#L76)
39+
40+
Create a Feedback_Author instance from the submission data.
41+
42+
```php
43+
public static function from_submission(
44+
array $post_data,
45+
Contact_Form $form
46+
): Feedback_Author
47+
```
48+
49+
**Parameters:**
50+
- `$post_data` (array) - The post data from the form submission
51+
- `$form` (Contact_Form) - The form object
52+
53+
**Returns:** `Feedback_Author` - New author instance with filtered data
54+
55+
**Note:** Automatically applies WordPress comment filters (`pre_comment_author_name`, `pre_comment_author_email`, `pre_comment_author_url`).
56+
57+
---
58+
59+
## Instance Methods
60+
61+
### `get_display_name()`
62+
[📍 Source](../src/contact-form/class-feedback-author.php#L129)
63+
64+
Get the display name of the author.
65+
66+
```php
67+
public function get_display_name(): string
68+
```
69+
70+
**Returns:** `string` - The author's name, or email if name is empty
71+
72+
**Behavior:** Falls back to email if name is not set, ensuring a non-empty display value.
73+
74+
---
75+
76+
### `get_name()`
77+
[📍 Source](../src/contact-form/class-feedback-author.php#L150)
78+
79+
Get the name of the author.
80+
81+
```php
82+
public function get_name(): string
83+
```
84+
85+
**Returns:** `string` - The author's name
86+
87+
**Behavior:**
88+
- If both `first_name` and `last_name` are set, combines them with space and applies `pre_comment_author_name` filter
89+
- Otherwise returns the stored `name` value (already filtered during construction)
90+
91+
---
92+
93+
### `get_email()`
94+
[📍 Source](../src/contact-form/class-feedback-author.php#L169)
95+
96+
Get the email of the author.
97+
98+
```php
99+
public function get_email(): string
100+
```
101+
102+
**Returns:** `string` - The author's email address
103+
104+
---
105+
106+
### `get_url()`
107+
[📍 Source](../src/contact-form/class-feedback-author.php#L178)
108+
109+
Get the URL of the author.
110+
111+
```php
112+
public function get_url(): string
113+
```
114+
115+
**Returns:** `string` - The author's website URL
116+
117+
---
118+
119+
### `get_first_name()`
120+
[📍 Source](../src/contact-form/class-feedback-author.php#L187)
121+
122+
Get the first name of the author (if provided separately).
123+
124+
```php
125+
public function get_first_name(): string
126+
```
127+
128+
**Returns:** `string` - The author's first name
129+
130+
---
131+
132+
### `get_last_name()`
133+
[📍 Source](../src/contact-form/class-feedback-author.php#L196)
134+
135+
Get the last name of the author (if provided separately).
136+
137+
```php
138+
public function get_last_name(): string
139+
```
140+
141+
**Returns:** `string` - The author's last name
142+
143+
---
144+
145+
### `get_avatar_url()`
146+
[📍 Source](../src/contact-form/class-feedback-author.php#L141)
147+
148+
Get the avatar URL of the author.
149+
150+
```php
151+
public function get_avatar_url(): string
152+
```
153+
154+
**Returns:** `string` - The Gravatar URL, or empty string if email is not set
155+
156+
**Note:** Uses WordPress `get_avatar_url()` function.
157+
158+
---
159+
160+
## Private Static Methods
161+
162+
### `get_computed_author_info()`
163+
[📍 Source](../src/contact-form/class-feedback-author.php#L98)
164+
165+
Gets the computed author information with filter application.
166+
167+
```php
168+
private static function get_computed_author_info(
169+
array $post_data,
170+
string $type,
171+
string $filter,
172+
Contact_Form $form
173+
): string
174+
```
175+
176+
**Parameters:**
177+
- `$post_data` (array) - The post data from the form submission
178+
- `$type` (string) - The type of author information to retrieve ('name', 'email', 'url')
179+
- `$filter` (string) - Filter to apply to the value
180+
- `$form` (Contact_Form) - The form object
181+
182+
**Returns:** `string` - Filtered value for the author information
183+
184+
**Applied Filters:**
185+
- `pre_comment_author_name`
186+
- `pre_comment_author_email`
187+
- `pre_comment_author_url`
188+
189+
---
190+
191+
## WordPress Filter Integration
192+
193+
The class integrates with WordPress comment filters during construction and when combining first/last names:
194+
195+
### Applied Filters
196+
197+
| Filter | Applied To | Purpose |
198+
|--------|-----------|---------|
199+
| `pre_comment_author_name` | Name field | Sanitize and validate author name |
200+
| `pre_comment_author_email` | Email field | Sanitize and validate email |
201+
| `pre_comment_author_url` | URL field | Sanitize and validate URL |
202+
203+
### Filter Pattern
204+
205+
Filters are applied using this pattern:
206+
```php
207+
Contact_Form_Plugin::strip_tags(
208+
stripslashes(
209+
apply_filters( $filter, addslashes( $value ) )
210+
)
211+
);
212+
```
213+
214+
This ensures compatibility with WordPress comment handling while preventing XSS.
215+
216+
---
217+
218+
## Usage Examples
219+
220+
### Basic Usage
221+
```php
222+
$author = new Feedback_Author(
223+
'John Doe',
224+
225+
'https://johndoe.com'
226+
);
227+
228+
echo $author->get_display_name(); // "John Doe"
229+
echo $author->get_email(); // "[email protected]"
230+
```
231+
232+
### With First/Last Name
233+
```php
234+
$author = new Feedback_Author(
235+
'', // name
236+
237+
'https://johndoe.com',
238+
'John', // first_name
239+
'Doe' // last_name
240+
);
241+
242+
echo $author->get_name(); // "John Doe" (combined and filtered)
243+
```
244+
245+
### Display Name Fallback
246+
```php
247+
// No name provided
248+
$author = new Feedback_Author( '', '[email protected]' );
249+
echo $author->get_display_name(); // "[email protected]"
250+
251+
// With name
252+
$author = new Feedback_Author( 'John Doe', '[email protected]' );
253+
echo $author->get_display_name(); // "John Doe"
254+
```
255+
256+
---
257+
258+
## See Also
259+
260+
- [Feedback_Author User Guide](feedback-author.md) - Usage examples and patterns
261+
- [Feedback Class Reference](feedback-class.md)
262+
- [Contact_Form Class](#) - Provides field IDs for author extraction

0 commit comments

Comments
 (0)