@ai-kits/wp-ag-kit 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/README.md +12 -8
  2. package/STRUCTURE.md +9 -7
  3. package/agents/woocommerce-expert.md +32 -0
  4. package/agents/wordpress-expert.md +4 -2
  5. package/package.json +5 -3
  6. package/skills/woocommerce/SKILL.md +54 -0
  7. package/skills/woocommerce/references/backend-dev-guide.md +44 -0
  8. package/skills/woocommerce/references/code-entities.md +186 -0
  9. package/skills/woocommerce/references/code-quality.md +273 -0
  10. package/skills/woocommerce/references/code-review-guide.md +71 -0
  11. package/skills/woocommerce/references/coding-conventions.md +182 -0
  12. package/skills/woocommerce/references/copy-guidelines-guide.md +17 -0
  13. package/skills/woocommerce/references/data-integrity.md +164 -0
  14. package/skills/woocommerce/references/dependency-injection.md +102 -0
  15. package/skills/woocommerce/references/dev-cycle-guide.md +32 -0
  16. package/skills/woocommerce/references/file-entities.md +73 -0
  17. package/skills/woocommerce/references/hooks.md +87 -0
  18. package/skills/woocommerce/references/js-i18n-patterns.md +298 -0
  19. package/skills/woocommerce/references/markdown-guide.md +358 -0
  20. package/skills/woocommerce/references/markdown-linting.md +202 -0
  21. package/skills/woocommerce/references/php-i18n-patterns.md +83 -0
  22. package/skills/woocommerce/references/php-linting-patterns.md +304 -0
  23. package/skills/woocommerce/references/running-tests.md +249 -0
  24. package/skills/woocommerce/references/security-patterns.md +109 -0
  25. package/skills/woocommerce/references/sentence-case.md +177 -0
  26. package/skills/woocommerce/references/type-annotations.md +161 -0
  27. package/skills/woocommerce/references/unit-tests.md +362 -0
  28. package/skills/woocommerce/references/woocommerce-global-objects.md +89 -0
@@ -0,0 +1,273 @@
1
+ # Code Quality Commands
2
+
3
+ ## Table of Contents
4
+
5
+ - [Overview](#overview)
6
+ - [PHP Linting](#php-linting)
7
+ - [JavaScript Linting](#javascript-linting)
8
+ - [Markdown Linting](#markdown-linting)
9
+ - [Important Linting Guidelines](#important-linting-guidelines)
10
+ - [Example Workflow](#example-workflow)
11
+ - [Understanding Linting Output](#understanding-linting-output)
12
+ - [Pre-Commit Checklist](#pre-commit-checklist)
13
+ - [Integration with Development Cycle](#integration-with-development-cycle)
14
+ - [Additional Linting Tools](#additional-linting-tools)
15
+ - [Troubleshooting](#troubleshooting)
16
+ - [Notes](#notes)
17
+
18
+ ## Overview
19
+
20
+ When making changes to the WooCommerce codebase, run these commands to ensure code quality and adherence to coding standards.
21
+
22
+ For detailed PHP linting patterns and common issues, see [php-linting-patterns.md](php-linting-patterns.md).
23
+
24
+ For markdown linting rules and workflow, see [markdown-linting.md](markdown-linting.md).
25
+
26
+ ## PHP Linting
27
+
28
+ ### Check for PHP Linting Issues
29
+
30
+ ```bash
31
+ pnpm run lint:changes:branch:php
32
+ ```
33
+
34
+ Checks changed files for WordPress Coding Standards violations (read-only).
35
+
36
+ ### Fix PHP Code Style Issues
37
+
38
+ ```bash
39
+ # Automatically fix PHP code style issues
40
+ pnpm run lint:php:fix
41
+ ```
42
+
43
+ This command:
44
+
45
+ - Automatically fixes code style violations where possible
46
+ - Applies WordPress Coding Standards formatting
47
+ - Modifies files in place
48
+ - Should be run before committing
49
+
50
+ ### Advanced PHP Linting
51
+
52
+ If you need more control, you can use phpcs and phpcbf directly:
53
+
54
+ ```bash
55
+ # Check specific file or directory
56
+ vendor/bin/phpcs path/to/file.php
57
+
58
+ # Check with specific standard
59
+ vendor/bin/phpcs --standard=WordPress path/to/file.php
60
+
61
+ # Fix specific file
62
+ vendor/bin/phpcbf path/to/file.php
63
+
64
+ # Show all violations (including warnings)
65
+ vendor/bin/phpcs -s path/to/file.php
66
+ ```
67
+
68
+ ## JavaScript Linting
69
+
70
+ ### Check for JS Linting Issues
71
+
72
+ ```bash
73
+ # Run JS linting on changes in your branch
74
+ pnpm run lint:changes:branch:js
75
+ ```
76
+
77
+ This command:
78
+
79
+ - Checks only JavaScript/TypeScript files changed in your current branch
80
+ - Identifies code style and potential issues
81
+ - Does not modify files
82
+
83
+ For detailed JavaScript/TypeScript linting configuration and patterns, see `client/admin/CLAUDE.md`.
84
+
85
+ ## Markdown Linting
86
+
87
+ Always lint markdown files after making changes. See [markdown-linting.md](markdown-linting.md) for complete details.
88
+
89
+ **Quick commands:**
90
+
91
+ ```bash
92
+ # Auto-fix most issues
93
+ markdownlint --fix path/to/file.md
94
+
95
+ # Check for remaining errors
96
+ markdownlint path/to/file.md
97
+ ```
98
+
99
+ ## Important Linting Guidelines
100
+
101
+ ### Only Fix Code in Your Branch
102
+
103
+ **Important:** Only fix linting errors for code that has been added or modified in the branch you are working on.
104
+
105
+ Do not fix linting errors in unrelated code unless specifically asked to do so.
106
+
107
+ **Why?**
108
+
109
+ - Keeps pull requests focused on the actual changes
110
+ - Avoids merge conflicts with other branches
111
+ - Makes code review easier
112
+ - Maintains clear git history
113
+
114
+ ### Example Workflow
115
+
116
+ ```bash
117
+ # 1. Make your code changes
118
+ # ... edit files ...
119
+
120
+ # 2. Check what you've changed
121
+ git status
122
+ git diff
123
+
124
+ # 3. Run linting on your changes
125
+ pnpm run lint:changes:branch:php
126
+
127
+ # 4. Fix issues automatically
128
+ pnpm run lint:php:fix
129
+
130
+ # 5. Review the fixes
131
+ git diff
132
+
133
+ # 6. If needed, check JavaScript changes
134
+ pnpm run lint:changes:branch:js
135
+
136
+ # 7. Commit your changes
137
+ git add .
138
+ git commit -m "Your commit message"
139
+ ```
140
+
141
+ ## Understanding Linting Output
142
+
143
+ ### PHP CodeSniffer Output
144
+
145
+ ```text
146
+ FILE: /path/to/file.php
147
+ ----------------------------------------------------------------------
148
+ FOUND 2 ERRORS AFFECTING 2 LINES
149
+ ----------------------------------------------------------------------
150
+ 12 | ERROR | [x] Expected 1 space after opening parenthesis;
151
+ | | 0 found
152
+ 25 | ERROR | [ ] Variable "$orderID" is not in valid snake_case
153
+ | | format
154
+ ----------------------------------------------------------------------
155
+ ```
156
+
157
+ **Legend:**
158
+
159
+ - `[x]` - Can be fixed automatically with phpcbf/lint:php:fix
160
+ - `[ ]` - Requires manual fixing
161
+
162
+ ### Common PHP Issues
163
+
164
+ 1. **Spacing issues** - Usually auto-fixable
165
+
166
+ ```php
167
+ // Wrong
168
+ if($condition){
169
+
170
+ // Right
171
+ if ( $condition ) {
172
+ ```
173
+
174
+ 2. **Naming conventions** - Requires manual fix
175
+
176
+ ```php
177
+ // Wrong
178
+ $orderID
179
+
180
+ // Right
181
+ $order_id
182
+ ```
183
+
184
+ 3. **Yoda conditions** - Requires manual fix
185
+
186
+ ```php
187
+ // Wrong
188
+ if ( $value === 'active' )
189
+
190
+ // Right
191
+ if ( 'active' === $value )
192
+ ```
193
+
194
+ ## Pre-Commit Checklist
195
+
196
+ Before committing your changes:
197
+
198
+ - [ ] Run `pnpm run lint:changes:branch:php`
199
+ - [ ] Run `pnpm run lint:php:fix` if issues found
200
+ - [ ] Run `pnpm run lint:changes:branch:js` if you modified JS files
201
+ - [ ] Review all automatic fixes with `git diff`
202
+ - [ ] Address any remaining issues that can't be auto-fixed
203
+ - [ ] Run tests to ensure fixes didn't break functionality
204
+
205
+ ## Integration with Development Cycle
206
+
207
+ Code quality checks fit into the overall development workflow:
208
+
209
+ 1. Make code changes
210
+ 2. Run relevant tests (see running-tests.md)
211
+ 3. **Run linting checks** ← You are here
212
+ 4. **Fix code quality issues** ← You are here
213
+ 5. Commit changes only after tests pass and linting is clean
214
+
215
+ ## Additional Linting Tools
216
+
217
+ ### Running Other pnpm Scripts
218
+
219
+ WooCommerce may have additional linting scripts. Check available scripts:
220
+
221
+ ```bash
222
+ # See all available scripts
223
+ pnpm run
224
+
225
+ # Common additional scripts may include:
226
+ pnpm run lint # Lint all files
227
+ pnpm run lint:fix # Fix all auto-fixable issues
228
+ pnpm run lint:php # PHP linting only
229
+ pnpm run lint:js # JavaScript linting only
230
+ ```
231
+
232
+ ## Troubleshooting
233
+
234
+ ### Linting Command Not Found
235
+
236
+ **Problem:** Command fails with "command not found"
237
+
238
+ **Solution:** Install dependencies:
239
+
240
+ ```bash
241
+ pnpm install
242
+ ```
243
+
244
+ ### Too Many Issues Reported
245
+
246
+ **Problem:** Linting reports issues in files you didn't change
247
+
248
+ **Solution:** Make sure you're using the branch-specific commands:
249
+
250
+ ```bash
251
+ # Good - only checks your changes
252
+ pnpm run lint:changes:branch:php
253
+
254
+ # Avoid - checks entire codebase
255
+ pnpm run lint:php
256
+ ```
257
+
258
+ ### Conflicts After Auto-Fix
259
+
260
+ **Problem:** Git conflicts after running lint:php:fix
261
+
262
+ **Solution:**
263
+
264
+ 1. Review the automatic fixes: `git diff`
265
+ 2. If fixes are incorrect, revert: `git checkout -- path/to/file.php`
266
+ 3. Address the issues manually instead
267
+
268
+ ## Notes
269
+
270
+ - Code quality tools help maintain consistency across the codebase
271
+ - Automatic fixes save time but should always be reviewed
272
+ - Some issues require manual intervention and understanding of the context
273
+ - Linting is required before committing to ensure code quality standards
@@ -0,0 +1,71 @@
1
+
2
+ # WooCommerce Code Review
3
+
4
+ Review code changes against WooCommerce coding standards and conventions.
5
+
6
+ ## Critical Violations to Flag
7
+
8
+ ### Backend PHP Code
9
+
10
+ Consult the `woocommerce-backend-dev` skill for detailed standards. Using these standards as guidance, flag these violations and other similar ones:
11
+
12
+ **Architecture & Structure:**
13
+
14
+ - ❌ **Standalone functions** - Must use class methods ([file-entities.md](file-entities.md))
15
+ - ❌ **Using `new` for DI-managed classes** - Classes in `src/` must use `$container->get()` ([dependency-injection.md](dependency-injection.md))
16
+ - ❌ **Classes outside `src/Internal/`** - Default location unless explicitly public ([file-entities.md](file-entities.md))
17
+
18
+ **Naming & Conventions:**
19
+
20
+ - ❌ **camelCase naming** - Must use snake_case for methods/variables/hooks ([code-entities.md](code-entities.md))
21
+ - ❌ **Yoda condition violations** - Must follow WordPress Coding Standards ([coding-conventions.md](coding-conventions.md))
22
+
23
+ **Documentation:**
24
+
25
+ - ❌ **Missing `@since` annotations** - Required for public/protected methods and hooks ([code-entities.md](code-entities.md))
26
+ - ❌ **Missing docblocks** - Required for all hooks and methods ([code-entities.md](code-entities.md))
27
+ - ❌ **Verbose docblocks** - Keep concise, one line is ideal ([code-entities.md](code-entities.md))
28
+
29
+ **Data Integrity:**
30
+
31
+ - ❌ **Missing validation** - Must verify state before deletion/modification ([data-integrity.md](data-integrity.md))
32
+
33
+ **Testing:**
34
+
35
+ - ❌ **Using `$instance` in tests** - Must use `$sut` variable name ([unit-tests.md](unit-tests.md))
36
+ - ❌ **Missing `@testdox`** - Required in test method docblocks ([unit-tests.md](unit-tests.md))
37
+ - ❌ **Test file naming** - Must follow convention for `includes/` vs `src/` ([unit-tests.md](unit-tests.md))
38
+
39
+ ### UI Text & Copy
40
+
41
+ Consult the `woocommerce-copy-guidelines` skill. Flag:
42
+
43
+ - ❌ **Title Case in UI** - Must use sentence case ([sentence-case.md](sentence-case.md))
44
+ - Wrong: "Save Changes", "Order Details", "Payment Options"
45
+ - Correct: "Save changes", "Order details", "Payment options"
46
+ - Exceptions: Proper nouns (WooPayments), acronyms (API), brand names
47
+
48
+ ## Review Approach
49
+
50
+ 1. **Scan for critical violations** listed above
51
+ 2. **Cite specific skill files** when flagging issues
52
+ 3. **Provide correct examples** from the skill documentation
53
+ 4. **Group related issues** for clarity
54
+ 5. **Be constructive** - explain why the standard exists when relevant
55
+
56
+ ## Output Format
57
+
58
+ For each violation found:
59
+
60
+ ```text
61
+ ❌ [Issue Type]: [Specific problem]
62
+ Location: [File path and line number]
63
+ Standard: [Link to relevant skill file]
64
+ Fix: [Brief explanation or example]
65
+ ```
66
+
67
+ ## Notes
68
+
69
+ - All detailed standards are in the `woocommerce-backend-dev`, `woocommerce-dev-cycle`, and `woocommerce-copy-guidelines` skills
70
+ - Consult those skills for complete context and examples
71
+ - When in doubt, refer to the specific skill documentation linked above
@@ -0,0 +1,182 @@
1
+ # General Coding Conventions
2
+
3
+ ## Table of Contents
4
+
5
+ - [Code Clarity and Comments](#code-clarity-and-comments)
6
+ - [WordPress Coding Standards](#wordpress-coding-standards)
7
+ - [Internationalization (i18n)](#internationalization-i18n)
8
+ - [Modern CRUD (Getters/Setters)](#modern-crud-getterssetters)
9
+ - [Null Coalescing Operator](#null-coalescing-operator)
10
+ - [Ternary Operator](#ternary-operator)
11
+ - [call_user_func_array() Usage](#call_user_func_array-usage)
12
+ - [Linting](#linting)
13
+
14
+ ## Code Clarity and Comments
15
+
16
+ Write self-explanatory code. Use comments sparingly - only for non-obvious insights.
17
+
18
+ **Good - Code explains itself:**
19
+
20
+ ```php
21
+ if ( $order->is_draft() && $user->can_delete_drafts() ) {
22
+ $order->delete();
23
+ }
24
+ ```
25
+
26
+ **Avoid - Over-commented:**
27
+
28
+ ```php
29
+ // Check if order is draft
30
+ if ( $order->is_draft() ) {
31
+ // Check if user has permission
32
+ if ( $user->can_delete_drafts() ) {
33
+ // Delete the order
34
+ $order->delete();
35
+ }
36
+ }
37
+ ```
38
+
39
+ **When to add comments:**
40
+
41
+ - Unusual decisions, workarounds, or non-obvious business logic
42
+ - Performance considerations
43
+
44
+ **When NOT to add comments:**
45
+
46
+ - Explaining what code does (code should be self-explanatory)
47
+ - Restating the obvious
48
+
49
+ ## WordPress Coding Standards
50
+
51
+ Follow [WordPress Coding Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/):
52
+
53
+ - **Yoda conditions**: `'true' === $value` not `$value === 'true'`
54
+ - **Spacing**: Spaces around operators, inside parentheses
55
+ - **Braces**: Opening on same line, closing on new line
56
+ - **Naming**: snake_case for functions and variables
57
+
58
+ ## Internationalization (i18n)
59
+
60
+ **Mandatory for all user-facing strings.**
61
+
62
+ - Use `woocommerce` text domain for WooCommerce core-like features.
63
+ - Use `esc_html__`, `esc_attr__`, etc., for output.
64
+ - Avoid HTML in strings.
65
+ - See [php-i18n-patterns.md](php-i18n-patterns.md) for details.
66
+
67
+ ## Modern CRUD (Getters/Setters)
68
+
69
+ **Always prefer `WC_Data` methods over direct meta access.**
70
+
71
+ **Good:**
72
+ ```php
73
+ $order = wc_get_order( $id );
74
+ $total = $order->get_total();
75
+ $meta = $order->get_meta( '_my_key' );
76
+ $order->update_meta_data( '_my_key', 'value' );
77
+ $order->save();
78
+ ```
79
+
80
+ **Avoid:**
81
+ ```php
82
+ $total = get_post_meta( $id, '_order_total', true );
83
+ update_post_meta( $id, '_my_key', 'value' );
84
+ ```
85
+
86
+ Why? `WC_Data` handles High-Performance Order Storage (HPOS) and standard post meta automatically. Direct `get_post_meta` will fail when HPOS is enabled.
87
+
88
+ ## Null Coalescing Operator
89
+
90
+ Use `??` instead of `isset` checks for array access.
91
+
92
+ **Good:**
93
+
94
+ ```php
95
+ if ( 34 === ( $foo['bar'] ?? null ) ) {
96
+ // ...
97
+ }
98
+
99
+ $value = $options['setting'] ?? 'default';
100
+ ```
101
+
102
+ **Avoid:**
103
+
104
+ ```php
105
+ if ( isset( $foo['bar'] ) && 34 === $foo['bar'] ) {
106
+ // ...
107
+ }
108
+
109
+ if ( isset( $options['setting'] ) ) {
110
+ $value = $options['setting'];
111
+ } else {
112
+ $value = 'default';
113
+ }
114
+ ```
115
+
116
+ ## Ternary Operator
117
+
118
+ Prefer the ternary operator over if-else statements for simple conditional returns or assignments, except for very complex cases.
119
+
120
+ **Good:**
121
+
122
+ ```php
123
+ return $condition ? $true_value : $false_value;
124
+
125
+ $result = $is_enabled ? 'enabled' : 'disabled';
126
+
127
+ $price = $has_discount ? $product->get_sale_price() : $product->get_regular_price();
128
+ ```
129
+
130
+ **Avoid:**
131
+
132
+ ```php
133
+ if ( $condition ) {
134
+ return $true_value;
135
+ }
136
+ return $false_value;
137
+
138
+ if ( $is_enabled ) {
139
+ $result = 'enabled';
140
+ } else {
141
+ $result = 'disabled';
142
+ }
143
+ ```
144
+
145
+ **Exception:** Use traditional if-else for complex conditions or when multiple statements are needed in each branch.
146
+
147
+ ```php
148
+ // OK to use if-else when complex logic is involved
149
+ if ( $order->needs_shipping() && ! $order->has_valid_address() ) {
150
+ $this->logger->log( 'Invalid shipping address' );
151
+ $this->notify_customer( $order );
152
+ return false;
153
+ } else {
154
+ return true;
155
+ }
156
+ ```
157
+
158
+ ## call_user_func_array() Usage
159
+
160
+ When using `call_user_func_array()`, always use **positional arguments** (numerically indexed array) instead of named/associative arrays for the arguments parameter.
161
+
162
+ The function uses array values in order and ignores keys, so named keys are misleading.
163
+
164
+ **Correct:**
165
+
166
+ ```php
167
+ call_user_func_array( array( $obj, 'method' ), array( $code ) );
168
+ call_user_func_array( array( $obj, 'process' ), array( $id, $status, $data ) );
169
+ ```
170
+
171
+ **Wrong:**
172
+
173
+ ```php
174
+ call_user_func_array( array( $obj, 'method' ), array( 'country_code' => $code ) );
175
+ call_user_func_array( array( $obj, 'process' ), array( 'order_id' => $id, 'status' => $status ) );
176
+ ```
177
+
178
+ ## Linting
179
+
180
+ Only fix linting errors for code that has been added or modified in the branch you are working on.
181
+
182
+ Do not fix linting errors in unrelated code unless specifically asked to do so.
@@ -0,0 +1,17 @@
1
+
2
+ # WooCommerce Copy Guidelines
3
+
4
+ This skill provides guidelines for writing user-facing copy in WooCommerce, including UI text, labels, buttons, messages, and documentation.
5
+
6
+ ## Instructions
7
+
8
+ Follow these guidelines when writing any user-facing text:
9
+
10
+ 1. **Sentence case**: See [sentence-case.md](sentence-case.md) for rules on using sentence case for all UI text
11
+
12
+ ## Key Principles
13
+
14
+ - Always use sentence case for UI text, not title case
15
+ - Keep copy concise and action-oriented
16
+ - Use clear, simple language
17
+ - Be consistent with existing WooCommerce copy patterns
@@ -0,0 +1,164 @@
1
+ # Data Integrity Guidelines
2
+
3
+ ## Table of Contents
4
+
5
+ - [Preventing Accidental Data Loss](#preventing-accidental-data-loss)
6
+ - [Validation Before Deletion](#validation-before-deletion)
7
+ - [Consider Race Conditions](#consider-race-conditions)
8
+ - [Session-Based Operations](#session-based-operations)
9
+ - [When in Doubt, Ask](#when-in-doubt-ask)
10
+ - [Security Checklist for Data Operations](#security-checklist-for-data-operations)
11
+ - [Common Pitfalls to Avoid](#common-pitfalls-to-avoid)
12
+
13
+ ## Preventing Accidental Data Loss
14
+
15
+ Always verify entity state before deletion/modification to prevent accidental data loss.
16
+
17
+ ### Validation Before Deletion
18
+
19
+ Always verify the state of the entity before deleting or modifying it.
20
+
21
+ #### Example: Deleting a draft order
22
+
23
+ ```php
24
+ // GOOD: Verify order status before deletion
25
+ public function delete_draft_order( int $order_id ) {
26
+ $order = wc_get_order( $order_id );
27
+
28
+ if ( ! $order ) {
29
+ return false;
30
+ }
31
+
32
+ // Verify it's actually a draft or checkout-draft
33
+ if ( ! in_array( $order->get_status(), array( 'draft', 'checkout-draft' ), true ) ) {
34
+ throw new \Exception( 'Cannot delete non-draft order' );
35
+ }
36
+
37
+ return $order->delete( true );
38
+ }
39
+
40
+ // BAD: No verification
41
+ public function delete_draft_order( int $order_id ) {
42
+ $order = wc_get_order( $order_id );
43
+ return $order->delete( true ); // Could delete any order!
44
+ }
45
+ ```
46
+
47
+ ### Consider Race Conditions
48
+
49
+ Think about whether race conditions could occur that might affect the wrong data.
50
+
51
+ #### Example: User-specific data deletion
52
+
53
+ ```php
54
+ // GOOD: Verify ownership before deletion
55
+ public function delete_user_cart_item( int $item_id, int $user_id ) {
56
+ $item = $this->get_cart_item( $item_id );
57
+
58
+ if ( ! $item ) {
59
+ return false;
60
+ }
61
+
62
+ // Prevent race condition: verify item belongs to this user
63
+ if ( (int) $item->get_user_id() !== $user_id ) {
64
+ throw new \Exception( 'Cannot delete item belonging to another user' );
65
+ }
66
+
67
+ return $this->data_store->delete( $item_id );
68
+ }
69
+
70
+ // BAD: Race condition possible
71
+ public function delete_user_cart_item( int $item_id, int $user_id ) {
72
+ // Another user could have taken this item in the meantime
73
+ return $this->data_store->delete( $item_id );
74
+ }
75
+ ```
76
+
77
+ ### Session-Based Operations
78
+
79
+ For session-based operations (like cart or checkout), verify session ownership.
80
+
81
+ #### Example: Clearing checkout data
82
+
83
+ ```php
84
+ // GOOD: Verify session ownership
85
+ public function clear_checkout_data( int $session_id ) {
86
+ $current_session_id = WC()->session->get_customer_id();
87
+
88
+ if ( $session_id !== $current_session_id ) {
89
+ throw new \Exception( 'Cannot clear checkout data for another session' );
90
+ }
91
+
92
+ WC()->session->set( 'checkout_data', array() );
93
+ }
94
+ ```
95
+
96
+ ## When in Doubt, Ask
97
+
98
+ If unsure about data operations, ask for clarification about:
99
+
100
+ - Required state/ownership verifications
101
+ - Soft delete vs hard delete requirements
102
+ - Protected states that prevent deletion
103
+
104
+ ## Security Checklist for Data Operations
105
+
106
+ Before implementing code that modifies or deletes data:
107
+
108
+ - [ ] Verify entity exists
109
+ - [ ] Verify entity state (status, type, etc.)
110
+ - [ ] Verify ownership (user_id, session_id, etc.)
111
+ - [ ] Check for race conditions
112
+ - [ ] Consider using soft delete (trash) instead of hard delete
113
+ - [ ] Add appropriate error handling
114
+ - [ ] Log sensitive operations for audit trail
115
+ - [ ] Add capability checks (`current_user_can()`)
116
+
117
+ ## Common Pitfalls to Avoid
118
+
119
+ ### 1. Trusting User Input
120
+
121
+ ```php
122
+ // BAD
123
+ $order_id = $_POST['order_id'];
124
+ $order->delete( true );
125
+
126
+ // GOOD
127
+ $order_id = absint( $_POST['order_id'] );
128
+ if ( ! current_user_can( 'delete_order', $order_id ) ) {
129
+ wp_die( 'Unauthorized' );
130
+ }
131
+ // ... additional validation ...
132
+ ```
133
+
134
+ ### 2. Batch Operations Without Verification
135
+
136
+ ```php
137
+ // BAD: Deletes all without verification
138
+ foreach ( $order_ids as $order_id ) {
139
+ wc_delete_order( $order_id );
140
+ }
141
+
142
+ // GOOD: Verify each item
143
+ foreach ( $order_ids as $order_id ) {
144
+ $order = wc_get_order( $order_id );
145
+ if ( $order && $order->get_status() === 'draft' ) {
146
+ wc_delete_order( $order_id );
147
+ }
148
+ }
149
+ ```
150
+
151
+ ### 3. Ignoring Return Values
152
+
153
+ ```php
154
+ // BAD: Doesn't check if operation succeeded
155
+ $order->delete( true );
156
+ wp_send_json_success();
157
+
158
+ // GOOD: Check result
159
+ if ( $order->delete( true ) ) {
160
+ wp_send_json_success();
161
+ } else {
162
+ wp_send_json_error( 'Failed to delete order' );
163
+ }
164
+ ```