| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentCart\Framework\Foundation\RequestGuard; |
| 6 |
|
| 7 |
class AttrGroupRequest extends RequestGuard |
| 8 |
{ |
| 9 |
public function rules() |
| 10 |
{ |
| 11 |
// Only trust group_id from the URL on PUT requests (updateGroup). On |
| 12 |
// POST createGroup there is no URL group_id, and we MUST NOT honour a |
| 13 |
// body-supplied value — a malicious client could POST {"group_id": N, |
| 14 |
// "slug": "color"} to force the unique-slug validator to exclude row |
| 15 |
// N from its check and slip past validation. The DB-level UNIQUE on |
| 16 |
// slug backstops the insert either way, but ignoring body-supplied |
| 17 |
// identifiers here keeps the validator's contract honest. |
| 18 |
$groupId = strtoupper((string) $this->method()) === 'PUT' |
| 19 |
? $this->get('group_id') |
| 20 |
: null; |
| 21 |
$tbl = 'fct_atts_groups'; |
| 22 |
|
| 23 |
// Build the slug rule conditionally. On PUT the client sends the |
| 24 |
// existing slug; it may be absent on POST because the UI omits the |
| 25 |
// slug field and the backend auto-generates it from the title instead. |
| 26 |
// When present on PUT, enforce uniqueness while excluding the current row. |
| 27 |
$slugRule = $groupId |
| 28 |
? 'nullable|sanitizeText|maxLength:50|unique:' . $tbl . ',slug,' . (int) $groupId . ',id' |
| 29 |
: 'nullable|sanitizeText|maxLength:50|unique:' . $tbl . ',slug'; |
| 30 |
|
| 31 |
return [ |
| 32 |
'title' => 'required|sanitizeText|maxLength:50', |
| 33 |
'slug' => $slugRule, |
| 34 |
'description' => 'nullable|sanitizeTextArea', |
| 35 |
'settings' => 'nullable', |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
public function messages() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'title' => esc_html__('Group title can not be empty.', 'fluent-cart'), |
| 43 |
'slug' => esc_html__('Group slug can not be empty and must be unique.', 'fluent-cart'), |
| 44 |
'description' => esc_html__('Group description should be long text.', 'fluent-cart'), |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
public function sanitize() |
| 49 |
{ |
| 50 |
return [ |
| 51 |
'title' => 'sanitize_text_field', |
| 52 |
// sanitize_textarea_field (not sanitize_text_field) so newlines survive. |
| 53 |
// rules() declares the field as sanitizeTextArea — using the single-line |
| 54 |
// sanitizer would silently flatten multi-line descriptions to one line. |
| 55 |
'description' => 'sanitize_textarea_field', |
| 56 |
// sanitize_title (not sanitize_text_field) so user-typed slugs end up |
| 57 |
// URL-safe ("My Color" → "my-color"). Slugs are POSTed as both title |
| 58 |
// AND slug from the product editor; without this, slugs end up with |
| 59 |
// raw spaces and break anything that round-trips them through URLs. |
| 60 |
'slug' => 'sanitize_title', |
| 61 |
'settings' => function ($value) { |
| 62 |
if (!is_array($value)) { |
| 63 |
return []; |
| 64 |
} |
| 65 |
return array_map('sanitize_text_field', $value); |
| 66 |
}, |
| 67 |
]; |
| 68 |
} |
| 69 |
} |
| 70 |
|