*/ private const ADMIN_WORTHY = [ 'processing', 'cancelled', 'failed', 'on-hold', 'refunded' ]; /** * Every order notification, keyed by id. * * @return array */ public static function all(): array { $definitions = []; foreach ( self::statuses() as $slug => $label ) { $definitions[ 'order.' . $slug . '.customer' ] = new EventDefinition( 'order.' . $slug . '.customer', self::GROUP, sprintf( /* translators: %s: order status label */ __( 'سفارش — %s', 'rahkar-toolkit' ), $label ), EventDefinition::AUDIENCE_CUSTOMER, self::variables(), self::defaultCustomerText( $slug, $label ), __( 'به مشتری، هنگام تغییر وضعیت سفارش به این حالت.', 'rahkar-toolkit' ), $label ); if ( in_array( $slug, self::adminWorthy(), true ) ) { $definitions[ 'order.' . $slug . '.admin' ] = new EventDefinition( 'order.' . $slug . '.admin', self::GROUP, sprintf( /* translators: %s: order status label */ __( 'اطلاع به مدیر — %s', 'rahkar-toolkit' ), $label ), EventDefinition::AUDIENCE_ADMIN, self::variables(), self::defaultAdminText( $slug, $label ), __( 'به شماره‌های مدیران فروشگاه.', 'rahkar-toolkit' ), $label ); } } return $definitions; } /** * Order statuses worth notifying on, as `slug => label`. * * @return array */ public static function statuses(): array { if ( ! function_exists( 'wc_get_order_statuses' ) ) { return []; } $statuses = []; foreach ( wc_get_order_statuses() as $key => $label ) { // WooCommerce prefixes its own statuses with `wc-`; plugins registering // a status sometimes do and sometimes do not. $slug = str_starts_with( $key, 'wc-' ) ? substr( $key, 3 ) : $key; if ( '' === $slug || in_array( $slug, self::INTERNAL, true ) ) { continue; } $statuses[ $slug ] = (string) $label; } /** * Filters the order statuses that can send a notification. * * Unset a key to suppress a status entirely. * * @param array $statuses Status slug => label. */ return (array) apply_filters( 'rahkar_wp_order_statuses', $statuses ); } /** * Statuses that also notify the shop's staff. * * @return list */ public static function adminWorthy(): array { /** * Filters which statuses produce a staff notification. * * @param list $statuses Status slugs. */ return (array) apply_filters( 'rahkar_wp_order_admin_statuses', self::ADMIN_WORTHY ); } /** * Placeholders available in an order message. * * Shown in the editor as clickable chips. Kept to what a shop actually puts * in an SMS — a list of forty tokens is as unhelpful as none. * * @return array */ public static function variables(): array { return [ 'order_number' => __( 'شماره سفارش', 'rahkar-toolkit' ), 'order_total' => __( 'مبلغ سفارش', 'rahkar-toolkit' ), 'order_status' => __( 'وضعیت سفارش', 'rahkar-toolkit' ), 'first_name' => __( 'نام مشتری', 'rahkar-toolkit' ), 'last_name' => __( 'نام خانوادگی', 'rahkar-toolkit' ), 'full_name' => __( 'نام و نام خانوادگی', 'rahkar-toolkit' ), 'items_count' => __( 'تعداد اقلام', 'rahkar-toolkit' ), 'items' => __( 'فهرست محصولات', 'rahkar-toolkit' ), 'payment_method' => __( 'روش پرداخت', 'rahkar-toolkit' ), 'shipping_city' => __( 'شهر گیرنده', 'rahkar-toolkit' ), 'customer_phone' => __( 'موبایل مشتری', 'rahkar-toolkit' ), 'site_name' => __( 'نام فروشگاه', 'rahkar-toolkit' ), 'site_url' => __( 'آدرس سایت', 'rahkar-toolkit' ), 'order_url' => __( 'لینک پیگیری سفارش', 'rahkar-toolkit' ), ]; } /** * Other names a provider pattern commonly gives the same value. * * Patterns are written in the provider's panel, often for another platform — * a PrestaShop pattern says `order_reference` and `customer_firstname`. When * a pattern is picked, each of its variables is matched here after an exact * name match, so the usual case needs no choice from the store owner at all. * Compared lower-case with everything but letters and digits removed. * * @return array> Plugin variable => synonyms. */ public static function synonyms(): array { return [ 'order_number' => [ 'orderid', 'orderno', 'order', 'orderreference', 'reference', 'ordercode', 'id', 'refid', 'factor', 'invoice', 'invoiceid', 'invoiceno' ], 'order_total' => [ 'total', 'totalpaid', 'totalpaidformatted', 'amount', 'price', 'sum', 'payable', 'mablagh', 'totalprice', 'ordertotal' ], 'order_status' => [ 'status', 'state', 'orderstate' ], 'first_name' => [ 'name', 'firstname', 'customerfirstname', 'customername', 'fname', 'customer', 'user', 'username', 'bfirstname', 'billingfirstname', 'clientname', 'client' ], 'last_name' => [ 'lastname', 'customerlastname', 'family', 'lname', 'surname', 'blastname', 'billinglastname' ], 'full_name' => [ 'fullname', 'customerfullname', 'receiver', 'recipient', 'recipientname', 'receivername' ], 'items_count' => [ 'count', 'qty', 'quantity', 'productcount', 'productquantity', 'itemcount', 'itemscount' ], 'items' => [ 'products', 'orderproducts', 'product', 'productname', 'productslist', 'productlist', 'orderitems', 'allitems' ], 'payment_method' => [ 'payment', 'paymentmethod', 'gateway', 'paymethod' ], 'shipping_city' => [ 'city', 'shippingcity', 'deliverycity' ], 'customer_phone' => [ 'phone', 'mobile', 'customerphone', 'customermobile', 'tel', 'cellphone' ], 'site_name' => [ 'site', 'sitename', 'shop', 'shopname', 'store', 'storename', 'brand', 'company' ], 'site_url' => [ 'domain', 'website', 'siteurl', 'websiteurl', 'weburl', 'web', 'homeurl', 'shopurl' ], 'order_url' => [ 'url', 'link', 'orderurl', 'orderlink', 'trackingurl', 'track', 'trackurl' ], ]; } /** * Starting message for a customer notification. * * @param string $slug Status slug. * @param string $label Status label. */ private static function defaultCustomerText( string $slug, string $label ): string { $texts = [ 'pending' => __( '{site_name} سفارش {order_number} ثبت شد و در انتظار پرداخت است.', 'rahkar-toolkit' ), 'processing' => __( '{site_name} سفارش {order_number} پرداخت شد و در حال آماده‌سازی است.', 'rahkar-toolkit' ), 'on-hold' => __( '{site_name} سفارش {order_number} در انتظار بررسی است.', 'rahkar-toolkit' ), 'completed' => __( '{site_name} سفارش {order_number} تکمیل شد. از خرید شما سپاسگزاریم.', 'rahkar-toolkit' ), 'cancelled' => __( '{site_name} سفارش {order_number} لغو شد.', 'rahkar-toolkit' ), 'refunded' => __( '{site_name} مبلغ سفارش {order_number} بازگردانده شد.', 'rahkar-toolkit' ), 'failed' => __( '{site_name} پرداخت سفارش {order_number} ناموفق بود.', 'rahkar-toolkit' ), ]; if ( isset( $texts[ $slug ] ) ) { return $texts[ $slug ]; } // A status this plugin has never heard of still gets a usable message // rather than an empty box the admin has to notice and fill in. return sprintf( /* translators: %s: order status label */ __( '{site_name} وضعیت سفارش {order_number} به «%s» تغییر کرد.', 'rahkar-toolkit' ), $label ); } /** * Starting message for a staff notification. * * @param string $slug Status slug. * @param string $label Status label. */ private static function defaultAdminText( string $slug, string $label ): string { if ( 'processing' === $slug ) { return __( 'سفارش جدید {order_number} مبلغ: {order_total} مشتری: {full_name}', 'rahkar-toolkit' ); } return sprintf( /* translators: %s: order status label */ __( 'سفارش {order_number} به «%s» تغییر کرد. مشتری: {full_name}', 'rahkar-toolkit' ), $label ); } }