其他
开发 WordPress/WooCommerce…
适用于基于 WordPress 和 WooCommerce 的 PHP 插件/主题开发,涵盖钩子、安全、自定义产品、REST API、数据库、性能与测试。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 38/100 ×0.15
适用场景
开发 WordPress/WooComm特定
解决思路
- 遵循 WordPress 编码标准。
- 正确使用 WooCommerce hooks 和 filters。
- 所有代码优先考虑安全性。
- 保持向后兼容性。
- 编写高性能、可扩展的代码。
- 遵循 WordPress PHP Coding Standards。
- 使用有意义的函数名和变量名。
- 为所有函数和类添加前缀以避免冲突。
- 使用 PHPDoc 注释文档化代码。
- 使用命名空间,例如 namespace MyPlugin\WooCommerce; class ProductHandler 在 __construct 中用 add_action('woocommerce_before_add_to_cart_form', [$this, 'custom_content']); 注册钩子。
- 使用 add_action('woocommerce_after_single_product_summary', 'custom_product_content', 15); 在单品摘要后追加内容,回调输出 class 为 custom-content 的 div。
- 使用 add_action('woocommerce_order_status_completed', 'process_completed_order', 10, 1); 处理已完成订单,在回调内用 wc_get_order($order_id) 获取订单并处理。
- 使用 add_filter('woocommerce_get_price_html', 'custom_price_html', 10, 2); 修改商品价格显示,在回调内检查 $product->is_on_sale() 并追加 sale-badge。
- 使用 add_filter('woocommerce_checkout_fields', 'custom_checkout_fields'); 添加自定义结账字段,例如设置 $fields['billing']['billing_custom_field'] 的 type、label、required、priority。
- 输入清洗:$product_id = absint($_POST['product_id']); $quantity = wc_stock_amount($_POST['quantity']); $email = sanitize_email($_POST['email']);
- 输出转义:echo esc_html($product->get_name()); echo esc_url($product->get_permalink()); echo wp_kses_post($product->get_description());
- Nonce 处理:用 wp_nonce_field('custom_action', 'custom_nonce'); 创建 nonce,并用 if (!wp_verify_nonce($_POST['custom_nonce'], 'custom_action')) { wp_die(__('Security check failed', 'textdomain')); } 验证。
- 权限检查:用 if (!current_user_can('manage_woocommerce')) { wp_die(__('Unauthorized access', 'textdomain')); } 阻止未授权访问。
- 创建自定义产品类型:class WC_Product_Custom extends WC_Product,实现 public function get_type() { return 'custom'; } 并添加自定义方法。
- 注册自定义产品类型:用 add_filter('product_type_selector', function($types) { $types['custom'] = __('Custom Product', 'textdomain'); return $types; }); 加入产品类型选择器。
- 扩展 REST API:用 add_action('rest_api_init', function() { register_rest_route('custom/v1', '/products/featured', [ 'methods' => 'GET', 'callback' => 'get_featured_products', 'permission_callback' => '__return_true' ]); }); 注册路由。
- REST 回调:在 get_featured_products($request) 中用 wc_get_products(['status' => 'publish', 'featured' => true, 'limit' => 10]) 获取商品,并 return rest_ensure_response($products);。
- 数据库查询:使用 global $wpdb; 对带变量查询使用 $wpdb->prepare,例如 $wpdb->prepare('SELECT * FROM {$wpdb->prefix}wc_orders WHERE status = %s', 'completed');。
- 尽可能使用 WooCommerce data stores:例如 $product = new WC_Product(); $product->set_name('New Product'); $product->set_regular_price('29.99'); $product->save();。
- 性能优化:使用 transients 做缓存。
- 性能优化:优化数据库查询。
- 性能优化:尽可能懒加载。
- 性能优化:减少 HTTP 请求。
- 性能优化:使用 object caching。
- 缓存实现:用 get_transient('custom_product_data'); 读取;如果 false === $cached_data,则执行 expensive_query(),并用 set_transient('custom_product_data', $cached_data, HOUR_IN_SECONDS); 写入缓存。
- 按插件结构组织代码:plugin-name/plugin-name.php、includes/class-main.php、includes/class-admin.php、includes/class-frontend.php、admin/css、admin/js、public/css、public/js、templates、languages。
- 使用 PHPUnit 编写单元测试。
- 使用 WP_UnitTestCase 进行 WordPress 测试。
- 使用 WooCommerce test helpers 测试。
- 用 PHPCS WordPress standards 验证代码。
适用边界 · 注意事项
- 不适用于非 WordPress/WooCommerce 的 PHP 或电商项目。
- 不适用于仅改前端样式且不涉及 WooCommerce 数据、钩子或扩展的场景。
- 没有 WordPress 与 WooCommerce 运行环境时,无法直接套用这些 API。