false, 'reason' => 'missing' ); wp_safe_redirect( add_query_arg( 'aieo_catalogue', $r['ok'] ? ( ! empty( $r['changed'] ) ? 'updated' : 'unchanged' ) : sanitize_key( (string) $r['reason'] ), $this->get_url() ) . '#aieo-card-overlaps-both' ); exit; } private static function cap() { return defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options'; } public function handle_decide() { if ( ! current_user_can( self::cap() ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'ai-eshop-optimizer' ) ); } check_admin_referer( self::ACTION_DECIDE ); $id = isset( $_POST['product'] ) ? sanitize_key( wp_unslash( $_POST['product'] ) ) : ''; $decision = isset( $_POST['decision'] ) ? sanitize_key( wp_unslash( $_POST['decision'] ) ) : ''; $ok = class_exists( 'AIEO_Overlap' ) && AIEO_Overlap::decide( $id, $decision, get_current_user_id() ); if ( $ok && 'switch' === $decision && ! empty( $_POST['enable_engine'] ) ) { AIEO_Overlap::enable_engine(); } if ( class_exists( 'AIEO_Wizard' ) ) { AIEO_Wizard::overlaps_state( true ); } $back = isset( $_POST['back'] ) ? esc_url_raw( wp_unslash( $_POST['back'] ) ) : ''; wp_safe_redirect( ( '' !== $back && 0 === strpos( $back, admin_url() ) ) ? $back : $this->get_url() . '#aieo-ov-card-' . $id ); exit; } public function render_subtab() { if ( ! class_exists( 'AIEO_Overlap' ) ) { list( $aieo_ov_open, $aieo_ov_close ) = AIEO_DMM_UI::card( array( 'icon' => 'layers', 'title' => $this->get_label() ) ); echo $aieo_ov_open; echo \Rooster\Admin\Ui::notice( esc_html__( 'This page is not available in this build.', 'ai-eshop-optimizer' ), 'destructive', array( 'inline' => true ) ); echo $aieo_ov_close; return; } $this->render_template( 'overlaps.php', $this->template_vars() ); } public function template_vars() { $replaces = array(); $alongside = array(); foreach ( AIEO_Overlap::present() as $id ) { $card = AIEO_Overlap::card( $id ); if ( 'alongside' === $card['family'] ) { $alongside[] = $card; } else { $replaces[] = $card; } } return array( 'replaces' => self::by_group( $replaces ), 'alongside' => self::by_group( $alongside ), 'simplify' => class_exists( 'AIEO_Simplify_Scan' ) ? AIEO_Simplify_Scan::card() : null, 'scan' => self::scan_groups(), 'catalogue' => array_merge( AIEO_Overlap::catalogue_state(), array( 'allowed' => AIEO_Overlap::catalogue_allowed(), 'refresh_url' => $this->get_form_action_url( self::ACTION_REFRESH ) ) ), 'decide_url' => $this->get_form_action_url( self::ACTION_DECIDE ), 'tab_url' => static function ( $tab, $anchor = '' ) { $u = AIEO_DMM::instance()->get_subtab_url( (string) $tab ); return '' !== $anchor ? $u . '#aieo-card-' . $anchor : $u; }, 'tab_label' => array( $this, 'dest_label' ), ); } public static function scan_groups() { if ( ! class_exists( 'AIEO_Simplify_Scan' ) ) { return null; } $v = AIEO_Simplify_Scan::verdict(); if ( ! $v || empty( $v['plugins'] ) || ! is_array( $v['plugins'] ) ) { return null; } $rows = AIEO_Overlap::rows(); $out = array( 'at' => (int) AIEO_Simplify_Scan::verdict_record()['at'], 'summary' => (array) ( $v['summary'] ?? array() ), 'verified' => array(), 'reviewing' => array(), 'unrelated' => array(), 'unknown' => array() ); foreach ( $v['plugins'] as $p ) { if ( ! is_array( $p ) ) { continue; } $rel = isset( $p['relationship'] ) ? (array) $p['relationship'] : array( 'unknown' ); $one = (string) reset( $rel ); $p['relationship'] = $rel; if ( 'unrelated' === $one ) { $out['unrelated'][] = $p; } elseif ( 'unknown' === $one ) { $out['unknown'][] = $p; } elseif ( 'reviewing' === $one || empty( $p['claim']['id'] ) || ! isset( $rows[ sanitize_key( (string) $p['claim']['id'] ) ] ) ) { $out['reviewing'][] = $p; } else { $p['claim_id'] = sanitize_key( (string) $p['claim']['id'] ); $out['verified'][] = $p; } } return $out; } private static function by_group( array $cards ) { $rank = array_flip( AIEO_Overlap::GROUPS ); $keys = array_keys( $cards ); usort( $keys, static function ( $x, $y ) use ( $cards, $rank ) { $gx = isset( $rank[ $cards[ $x ]['group'] ] ) ? $rank[ $cards[ $x ]['group'] ] : 99; $gy = isset( $rank[ $cards[ $y ]['group'] ] ) ? $rank[ $cards[ $y ]['group'] ] : 99; return $gx !== $gy ? $gx <=> $gy : $x <=> $y; } ); $out = array(); foreach ( $keys as $i ) { $out[] = $cards[ $i ]; } return $out; } public function dest_label( $id ) { static $labels = null; if ( null === $labels ) { $labels = array(); foreach ( (array) AIEO_DMM::instance()->get_subtab_tree( '' ) as $entry ) { if ( isset( $entry['kind'] ) && 'group' === $entry['kind'] ) { foreach ( (array) $entry['children'] as $child ) { $labels[ (string) $child['id'] ] = wp_specialchars_decode( (string) $child['label'], ENT_QUOTES ); } } elseif ( isset( $entry['id'] ) ) { $labels[ (string) $entry['id'] ] = wp_specialchars_decode( (string) $entry['label'], ENT_QUOTES ); } } } return isset( $labels[ $id ] ) ? $labels[ $id ] : (string) $id; } }