| @@ -66,12 +66,19 @@ | ||
| 66 | 66 | const STATUS_FAILED = 'failed'; |
| 67 | 67 | const STATUS_CANCELLED = 'cancelled'; |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | - * Constructor — register the CPT on init. | |
| 70 | + * Constructor — register the CPT on init, or at once when init has passed. | |
| 71 | + * | |
| 72 | + * On a storefront request this service is constructed lazily by the first | |
| 73 | + * order write, long after `init`; a hook added then would never fire. | |
| 71 | 74 | */ |
| 72 | 75 | public function __construct() { |
| 73 | - add_action( 'init', array( $this, 'register_post_type' ) ); | |
| 76 | + if ( did_action( 'init' ) ) { | |
| 77 | + $this->register_post_type(); | |
| 78 | + } else { | |
| 79 | + add_action( 'init', array( $this, 'register_post_type' ) ); | |
| 80 | + } | |
| 74 | 81 | // A static callback: several services construct Print_Job_Service on |
| 75 | 82 | // every request, and WordPress dedupes identical static callbacks, so |
| 76 | 83 | // the purge runs exactly once per cron event. |
| 77 | 84 | add_action( self::PURGE_HOOK, array( __CLASS__, 'run_purge' ) ); |