|
| 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