categories = new CategoryRepository(); $this->codes = new CodeRepository(); $this->campaigns = new CampaignRepository( $this->categories, $this->codes ); $this->issuer = new Issuer( $this->campaigns, $this->codes ); } public function boot(): void { if ( $this->booted ) { return; } $this->booted = true; if ( ! class_exists( 'WooCommerce' ) ) { add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) ); return; } Installer::maybe_upgrade(); $products = new ProductRules( $this->campaigns, $this->codes ); $products->register(); ( new VirtualCoupons( $this->campaigns, $this->codes, $products ) )->register(); ( new Redemptions( $this->campaigns, $this->codes ) )->register(); ( new Privacy( $this->codes ) )->register(); $promotions = new Promotions(); $dashboard = new Dashboard( $this->campaigns, $this->categories ); ( new RestController( $this->campaigns, $this->categories, $this->codes, $this->issuer, new CampaignStats(), $promotions, $dashboard ) )->register(); if ( is_admin() ) { ( new AdminPage( $this->campaigns, $this->codes, $promotions, $dashboard ) )->register(); } add_filter( 'cnwscoupons_issue_code', array( $this, 'issue_code_filter' ), 10, 2 ); add_filter( 'cnwscoupons_active_campaigns', array( $this, 'active_campaigns_filter' ) ); add_filter( 'cnwscoupons_active_series', array( $this, 'active_campaigns_filter' ) ); add_filter( 'cnwscoupons_campaign_for_code', array( $this, 'campaign_for_code_filter' ), 10, 2 ); if ( defined( 'WP_CLI' ) && WP_CLI ) { \WP_CLI::add_command( 'webstyle-coupons', new Command( $this->campaigns, $this->categories, $this->codes, $this->issuer ) ); } } /** * For add-ons: the campaign repository, e.g. to read a campaign or save_extension(). */ public function campaigns(): CampaignRepository { return $this->campaigns; } public function woocommerce_missing_notice(): void { if ( current_user_can( 'activate_plugins' ) ) { printf( '

%s

', esc_html__( 'WebStyle Personal Coupons needs WooCommerce to be installed and active.', 'webstyle-coupons' ) ); } } /** * Public API: apply_filters( 'cnwscoupons_issue_code', '', array( 'campaign' => ..., 'email' => ..., 'source' => ... ) ). * 'series' is accepted as an older name for 'campaign'. * * @param string $code Code from an earlier filter. * @param array $args campaign (slug), email, source. * @return string The recipient's personal code, or '' when none can be issued. */ public function issue_code_filter( $code, $args ) { if ( '' !== (string) $code || ! is_array( $args ) ) { return $code; } $slug = (string) ( $args['campaign'] ?? $args['series'] ?? '' ); $result = $this->issuer->issue( $slug, (string) ( $args['email'] ?? '' ), (string) ( $args['source'] ?? 'api' ) ); return is_string( $result ) ? $result : ''; } /** * Public API: apply_filters( 'cnwscoupons_campaign_for_code', null, $code ) returns the Campaign a * code belongs to, or null, e.g. for an add-on that reads its settings with $campaign->extension(). * * @param Campaign|null $campaign Campaign from an earlier filter. * @param string $code Coupon code, any case. */ public function campaign_for_code_filter( $campaign, $code ) { if ( $campaign instanceof Campaign || ! is_string( $code ) || '' === $code ) { return $campaign; } $row = $this->codes->find_by_code( $code ); return $row ? $this->campaigns->find( (int) $row->campaign_id ) : null; } /** * Public API: apply_filters( 'cnwscoupons_active_campaigns', array() ) returns slug => name for * active campaigns with personal codes, i.e. the ones another plugin can issue codes from. * * @param array $campaigns Campaigns from an earlier filter. */ public function active_campaigns_filter( $campaigns ) { $campaigns = is_array( $campaigns ) ? $campaigns : array(); foreach ( $this->campaigns->all( 'active', Campaign::TYPE_PERSONAL ) as $campaign ) { $campaigns[ $campaign->slug ] = $campaign->name; } return $campaigns; } }