0 && self::opens() ? self::url( $offer_id, '', $step ) : ''; } const DATES_STEP = 5; public static function is_on(): bool { $all = wp_load_alloptions(); if ( ! isset( $all[ self::OPTION ] ) ) { return false; } $on = get_option( self::OPTION, '' ); return is_scalar( $on ) && '1' === (string) $on && self::store_is_unified(); } private static function store_is_unified(): bool { $all = wp_load_alloptions(); return isset( $all[ self::STORE_OPTION ] ) && self::STORE_MODE === get_option( self::STORE_OPTION ); } public static function switch_on(): bool { if ( ! self::store_is_unified() ) { return false; } update_option( self::OPTION, '1', true ); if ( ! self::is_on() && function_exists( 'wp_set_option_autoload' ) ) { wp_set_option_autoload( self::OPTION, true ); } return self::is_on(); } public static function switch_off(): void { delete_option( self::OPTION ); } public static function url( int $offer_id = 0, string $kind = '', int $step = 0, string $entry = '' ): string { $args = array( 'page' => class_exists( 'AIEO_DMM' ) ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm', 'dmm_tab' => 'offer_wizard', ); $kind = sanitize_key( $kind ); if ( $offer_id > 0 ) { $args['offer'] = $offer_id; if ( 0 !== self::opens_on( $step ) ) { $args['step'] = $step; } } elseif ( '' !== $kind ) { $args['kind'] = $kind; } if ( $offer_id <= 0 && 1 === preg_match( '/^(competition|form):[1-9]\d{0,18}$/', $entry ) ) { $args[ self::ENTRY_ARG ] = $entry; } return add_query_arg( $args, admin_url( 'admin.php' ) ); } const ENTRY_ARG = 'entry'; public static function entry_of_request(): string { $raw = isset( $_GET[ self::ENTRY_ARG ] ) && is_scalar( $_GET[ self::ENTRY_ARG ] ) ? sanitize_text_field( wp_unslash( (string) $_GET[ self::ENTRY_ARG ] ) ) : ''; return array() !== AIEO_DMM_Offer_Wizard_Draft::entry_prefill( $raw ) ? $raw : ''; } public static function back_url( int $offer_id = 0 ): string { $args = array( 'page' => class_exists( 'AIEO_DMM' ) ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm', 'dmm_tab' => 'offers', ); if ( $offer_id > 0 ) { $args['offer'] = $offer_id; } return add_query_arg( $args, admin_url( 'admin.php' ) ); } public static function can_open( array $row ): bool { self::load(); $opened = AIEO_DMM_Offer_Wizard_Draft::from_row( $row ); return '' === $opened['why'] && array() === AIEO_DMM_Offer_Wizard_Draft::read( $opened['offer'] )['unsaid']; } public static function opens_on( int $step ): int { return $step >= 2 && $step <= 5 ? $step : 0; } public static function request(): array { $offer = isset( $_GET['offer'] ) && is_scalar( $_GET['offer'] ) ? absint( wp_unslash( (string) $_GET['offer'] ) ) : 0; $kind = isset( $_GET['kind'] ) && is_scalar( $_GET['kind'] ) ? sanitize_key( wp_unslash( (string) $_GET['kind'] ) ) : ''; $step = isset( $_GET['step'] ) && is_scalar( $_GET['step'] ) ? absint( wp_unslash( (string) $_GET['step'] ) ) : 0; return array( 'offer' => $offer, 'kind' => $kind, 'step' => $offer > 0 ? self::opens_on( $step ) : 0, ); } public static function footer_wizard(): bool { return class_exists( '\Rooster\Admin\Library' ) && version_compare( \Rooster\Admin\Library::VERSION, '0.91.0', '>=' ); } private static function load(): void { $base = dirname( __DIR__ ) . '/'; $files = array( 'AIEO_DMM_Pattern_Match' => 'services/class-aieo-dmm-pattern-match.php', 'AIEO_DMM_Offer_Registry' => 'services/class-aieo-dmm-offer-registry.php', 'AIEO_DMM_Offer_Conditions' => 'services/class-aieo-dmm-offer-conditions.php', 'AIEO_DMM_Offer_Definition' => 'services/class-aieo-dmm-offer-definition.php', 'AIEO_DMM_Schedule' => 'services/class-aieo-dmm-schedule.php', 'AIEO_DMM_Offer_Store' => 'services/class-aieo-dmm-offer-store.php', 'AIEO_DMM_Bundles' => 'services/class-aieo-dmm-bundles.php', 'AIEO_DMM_Bundle_Words' => 'services/class-aieo-dmm-bundle-words.php', 'AIEO_DMM_Offer_Texts' => 'services/class-aieo-dmm-offer-texts.php', 'AIEO_DMM_Offer_Wizard_Words' => 'offers/wizard/class-aieo-dmm-offer-wizard-words.php', 'AIEO_DMM_Offer_Wizard_Draft' => 'offers/wizard/class-aieo-dmm-offer-wizard-draft.php', 'AIEO_DMM_Offer_Wizard_Fields' => 'offers/wizard/class-aieo-dmm-offer-wizard-fields.php', 'AIEO_DMM_Offer_List' => 'offers/class-aieo-dmm-offer-list.php', ); foreach ( $files as $class => $file ) { if ( ! class_exists( $class, false ) && is_readable( $base . $file ) ) { require_once $base . $file; } } } public function register_hooks() { add_action( 'wp_ajax_' . self::PREVIEW, array( __CLASS__, 'ajax_preview' ) ); add_action( 'wp_ajax_' . self::SAVE, array( __CLASS__, 'ajax_save' ) ); } public static function ajax_preview() { self::hand_over( 'ajax_preview' ); } public static function ajax_save() { self::hand_over( 'ajax_save' ); } private static function hand_over( string $method ): void { self::load(); $file = dirname( __DIR__ ) . '/offers/wizard/class-aieo-dmm-offer-wizard-saver.php'; if ( ! class_exists( 'AIEO_DMM_Offer_Wizard_Saver', false ) && is_readable( $file ) ) { require_once $file; } if ( is_callable( array( 'AIEO_DMM_Offer_Wizard_Saver', $method ) ) ) { call_user_func( array( 'AIEO_DMM_Offer_Wizard_Saver', $method ) ); return; } AIEO_DMM::verify_ajax_request(); wp_send_json_error( array( 'message' => __( 'The offer wizard cannot save on this shop yet: a part of it is not installed.', 'ai-eshop-optimizer' ) ), 501 ); } public function enqueue_admin_assets() { self::load(); $js = 'assets/js/aieo-offer-wizard.js'; $needs = array( 'aieo-dmm' ); if ( class_exists( '\Rooster\Admin\States' ) ) { \Rooster\Admin\States::enqueue(); $needs[] = \Rooster\Admin\States::HANDLE; } wp_enqueue_script( 'aieo-offer-wizard', AIEO_URI . $js, $needs, file_exists( AIEO_DIR . $js ) ? (string) filemtime( AIEO_DIR . $js ) : '1', true ); wp_localize_script( 'aieo-offer-wizard', 'aieoOfferWizard', array( 'preview' => self::PREVIEW, 'save' => self::SAVE, 'back' => self::back_url(), 'i18n' => AIEO_DMM_Offer_Wizard_Fields::script_words(), ) ); } public function render_subtab() { if ( ! current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'aieo_access' ) ) { return; } self::load(); if ( class_exists( 'AIEO_DMM_Offers' ) ) { echo AIEO_DMM_Offers::outcome_notice(); } $req = self::request(); if ( $req['offer'] > 0 ) { $this->render_stored( $req['offer'] ); return; } if ( ! self::is_on() ) { $this->render_off(); return; } $offer = self::starts( $req['kind'] ) ? AIEO_DMM_Offer_Wizard_Draft::blank( $req['kind'] ) : null; if ( null === $offer ) { $this->render_kinds(); return; } $this->render_wizard( $offer, null ); } public static function starts( string $kind ): bool { $kind = sanitize_key( $kind ); if ( '' === $kind || in_array( $kind, self::NOT_STARTED_HERE, true ) ) { return false; } self::load(); return class_exists( 'AIEO_DMM_Offer_Registry' ) && is_array( AIEO_DMM_Offer_Registry::instance()->kind( $kind ) ); } private function render_kinds() { $registry = AIEO_DMM_Offer_Registry::instance(); $icons = array( 'discount' => 'tags', 'gift' => 'package', 'free_shipping' => 'truck', 'customer_pricing' => 'award', ); list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'offer-wizard-new', 'icon' => 'plus', 'title' => __( 'New offer', 'ai-eshop-optimizer' ), 'description' => __( 'Which kind of offer? Its six steps open. A customer discount class is made on its own screen, with its members.', 'ai-eshop-optimizer' ), 'tools' => $this->back_link(), ) ); echo $open; $entry = self::entry_of_request(); if ( '' !== $entry && class_exists( 'AIEO_Audience_Rules' ) ) { list( $of, $post_id ) = explode( ':', $entry ); $who = AIEO_Audience_Rules::describe_entry( AIEO_DMM_Offer_Wizard_Words::ENTRY_KINDS[ $of ], array( 'op' => 'in', 'value' => array( (int) $post_id ), ) ); echo \Rooster\Admin\Ui::notice( esc_html( sprintf( __( 'The offer will be for these people: %s. You will find them chosen in step 4, To whom.', 'ai-eshop-optimizer' ), $who ) ), 'info', array( 'inline' => true ) ); } foreach ( $registry->kinds() as $kind ) { $kind = (string) $kind; $here = self::starts( $kind ); $href = $here ? self::url( 0, $kind, 0, $entry ) : AIEO_DMM_Offer_List::form_url( $kind, true ); if ( '' === $href ) { continue; } echo \Rooster\Admin\Ui::button( $registry->kind_label( $kind ), array( 'href' => $href, 'icon' => $icons[ $kind ] ?? 'plus', 'attrs' => array( ( $here ? 'data-aieo-wizard-kind' : 'data-aieo-wizard-form' ) => $kind ), ) ) . ' '; } echo $close; } private function render_off() { list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'offer-wizard-off', 'icon' => 'inbox', 'title' => __( 'New offers are made in All offers', 'ai-eshop-optimizer' ), ) ); echo $open; echo \Rooster\Admin\Ui::empty_state( __( 'The offer wizard is not switched on for this shop', 'ai-eshop-optimizer' ), __( 'A new offer is made from All offers, each kind in its own form. An offer that has no form of its own still opens here, from its Edit.', 'ai-eshop-optimizer' ), array( 'compact' => true, 'action' => $this->back_link(), ) ); echo $close; } private function render_stored( int $id ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aieo_dmm_offers WHERE id = %d", $id ), ARRAY_A ); if ( ! is_array( $row ) || 'archived' === (string) ( $row['state'] ?? '' ) ) { $this->render_gone( is_array( $row ) ); return; } $opened = AIEO_DMM_Offer_Wizard_Draft::from_row( $row ); $unsaid = '' === $opened['why'] ? AIEO_DMM_Offer_Wizard_Draft::read( $opened['offer'] )['unsaid'] : array(); if ( '' !== $opened['why'] || $unsaid ) { $this->render_kept( $row, $opened['why'], $unsaid ); return; } $this->render_wizard( $opened['offer'], $row ); } private function render_gone( bool $archived ) { list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'offer-wizard-gone', 'icon' => 'inbox', 'title' => __( 'Offer not open', 'ai-eshop-optimizer' ), ) ); echo $open; echo \Rooster\Admin\Ui::empty_state( $archived ? __( 'This offer is archived', 'ai-eshop-optimizer' ) : __( 'This offer is not there any more', 'ai-eshop-optimizer' ), $archived ? __( 'An archived offer cannot run again. In All offers it can be copied into a draft.', 'ai-eshop-optimizer' ) : __( 'It may have been removed since the link was made.', 'ai-eshop-optimizer' ), array( 'compact' => true, 'action' => $this->back_link(), ) ); echo $close; } private function render_kept( array $row, string $why, array $unsaid ) { $id = (int) $row['id']; $type = (string) ( $row['type'] ?? '' ); $registry = AIEO_DMM_Offer_Registry::instance(); $states = AIEO_DMM_Offer_List::state_labels(); $state = (string) ( $row['state'] ?? '' ); $window = AIEO_DMM_Schedule::local_window( $row ); $say = static function ( string $day, string $time ): string { return '' === $day ? '' : trim( AIEO_DMM_Schedule::day_label( $day ) . ' ' . $time ); }; $start = $say( (string) $window['start_date'], (string) $window['start_time'] ); $end = $say( (string) $window['end_date'], (string) $window['end_time'] ); $label = trim( (string) ( $row['label'] ?? '' ) ); list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => 'offer-wizard-kept', 'icon' => 'eye', 'title' => __( 'Offer kept as it is', 'ai-eshop-optimizer' ), 'description' => sprintf( __( 'This offer is shown and not changed here. Its days and times are in the shop\'s zone (%s).', 'ai-eshop-optimizer' ), (string) wp_timezone_string() ), 'meta' => AIEO_DMM_UI::title_badge( '#' . $id ), ) ); echo $open; if ( '' !== $why ) { echo \Rooster\Admin\Ui::notice( esc_html__( 'It was made by a newer version, or with an add-on that is not active here. It keeps applying as it was saved where the shop can read it, and nothing here changes it.', 'ai-eshop-optimizer' ), 'warning', array( 'inline' => true, 'title' => __( 'Needs a newer version of Rooster One', 'ai-eshop-optimizer' ), 'attrs' => array( 'data-aieo-wizard-kept' => $why ), ) ); } else { echo \Rooster\Admin\Ui::notice( esc_html( sprintf( __( 'What this screen cannot show: %s.', 'ai-eshop-optimizer' ), AIEO_DMM_Offer_Wizard_Fields::part_words( $unsaid ) ) ), 'info', array( 'inline' => true, 'title' => __( 'This offer has parts this screen cannot show. It is kept as it is.', 'ai-eshop-optimizer' ), 'attrs' => array( 'data-aieo-wizard-kept' => implode( ' ', $unsaid ) ), ) ); } echo \Rooster\Admin\Ui::facts( array( array( 'label' => __( 'Name', 'ai-eshop-optimizer' ), 'value' => '' !== $label ? $label : '#' . $id, ), array( 'label' => __( 'Kind', 'ai-eshop-optimizer' ), 'value' => $registry->kind_label( $type ), ), array( 'label' => __( 'State', 'ai-eshop-optimizer' ), 'value' => $states[ $state ] ?? ( ! empty( $row['enabled'] ) ? $states['running'] : $states['paused'] ), ), array( 'label' => __( 'Starts', 'ai-eshop-optimizer' ), 'value' => '' !== $start ? $start : __( 'From its first save', 'ai-eshop-optimizer' ), ), array( 'label' => __( 'Ends', 'ai-eshop-optimizer' ), 'value' => '' !== $end ? $end : __( 'No end', 'ai-eshop-optimizer' ), ), ), array( 'columns' => 2 ) ); $form = AIEO_DMM_Offer_List::form_url( $type, false, $id ); if ( '' !== $form ) { echo \Rooster\Admin\Ui::button( __( 'Open in the old form', 'ai-eshop-optimizer' ), array( 'href' => $form ) ) . ' '; } echo $this->back_link(); echo $close; } private function render_wizard( array $offer, $row ) { $id = (int) ( $offer['id'] ?? 0 ); $type = (string) $offer['type']; $phase3 = class_exists( 'AIEO_DMM_Offer_Access' ) && AIEO_DMM_Offer_Access::is_on(); $context = array( 'phase3' => $phase3, 'accesses' => array(), 'placements' => array(), 'started' => AIEO_DMM_Offer_Wizard_Draft::started_at( $row ), 'prefill' => null === $row ? AIEO_DMM_Offer_Wizard_Draft::entry_prefill( self::entry_of_request() ) : array(), ); if ( $phase3 ) { $context['accesses'] = $id > 0 ? AIEO_DMM_Offer_Access::for_offer( $id ) : array(); $context['placements'] = $id > 0 && class_exists( 'AIEO_DMM_Placements' ) ? AIEO_DMM_Placements::for_offer( $id ) : array(); $offer['accesses'] = self::only( $context['accesses'], array( 'id', 'kind', 'code', 'state', 'life_days', 'max_uses', 'max_uses_per_customer', 'combines' ) ); $offer['placements'] = self::only( $context['placements'], array( 'id', 'carrier', 'carrier_ref', 'access_id', 'state' ) ); } $steps = AIEO_DMM_Offer_Wizard_Fields::panels( $offer, $context ); $draft = null === $row || 'draft' === AIEO_DMM_Schedule::state_of( $row ) ? \Rooster\Admin\Ui::button( __( 'Save as draft', 'ai-eshop-optimizer' ), array( 'attrs' => array( 'data-aieo-wizard-draft' => '' ) ) ) : ''; $footer = self::footer_wizard(); $opts = array( 'finish' => __( 'Save offer', 'ai-eshop-optimizer' ), 'label' => __( 'Steps of the offer', 'ai-eshop-optimizer' ), 'reached' => null !== $row ? count( $steps ) : 1, ); if ( $footer ) { $opts += array( 'footer' => true, 'submit' => true, ); if ( '' !== $draft ) { $opts['actions'] = $draft; } } $label = trim( (string) ( $offer['label'] ?? '' ) ); $kind = AIEO_DMM_Offer_Registry::instance()->kind_label( $type ); $tools = ''; if ( null !== $row ) { $form = AIEO_DMM_Offer_List::form_url( $type, false, $id ); $tools = '' !== $form ? \Rooster\Admin\Ui::button( __( 'Open in the old form', 'ai-eshop-optimizer' ), array( 'href' => $form, 'size' => 'sm', 'variant' => 'ghost', ) ) : ''; } $card = array( 'anchor' => 'offer-wizard-steps', 'icon' => 'list-checks', 'title' => __( 'Offer in six steps', 'ai-eshop-optimizer' ), 'description' => null !== $row ? sprintf( '' !== $draft ? __( 'Open: %1$s (#%2$d). Nothing changes until you press Save offer or Save as draft.', 'ai-eshop-optimizer' ) : __( 'Open: %1$s (#%2$d). Nothing changes until you press Save offer.', 'ai-eshop-optimizer' ), '' !== $label ? $label : $kind, $id ) : sprintf( __( 'A new offer of the kind %s. Nothing is saved until you press Save offer or Save as draft.', 'ai-eshop-optimizer' ), $kind ), 'meta' => AIEO_DMM_UI::title_badge( $id > 0 ? '#' . $id : $kind ), 'tools' => $tools, 'span' => 'all', ); if ( ! $footer && '' !== $draft ) { $card['footer'] = $draft; } list( $open, $close ) = AIEO_DMM_UI::card( $card ); $json = (string) wp_json_encode( $offer, JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ); echo '
'; if ( class_exists( 'AIEO_DMM_Product_Picker' ) && method_exists( 'AIEO_DMM_Product_Picker', 'dialogs' ) ) { echo AIEO_DMM_Product_Picker::dialogs(); } } private function back_link(): string { return \Rooster\Admin\Ui::button( __( 'Back to All offers', 'ai-eshop-optimizer' ), array( 'href' => self::back_url(), 'size' => 'sm', 'variant' => 'ghost', 'icon' => 'list', ) ); } private static function only( array $rows, array $keys ): array { $keys = array_flip( $keys ); $out = array(); foreach ( $rows as $row ) { if ( is_array( $row ) ) { $out[] = array_intersect_key( $row, $keys ); } } return $out; } }