prefix . 'aieo_dmm_complementary'; } public static function for_source( int $product_id ): ?array { global $wpdb; if ( $product_id <= 0 ) { return null; } $table = self::table(); if ( ! function_exists( 'aieo_dmm_table_exists' ) ) { $helpers = AIEO_DIR . 'includes/sql/sql-runtime-helpers.php'; if ( is_readable( $helpers ) ) { require_once $helpers; } } if ( function_exists( 'aieo_dmm_table_exists' ) && ! aieo_dmm_table_exists( $table ) ) { return null; } $read = static function () use ( $wpdb, $table, $product_id ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE source_product_id = %d AND enabled = 1 LIMIT 1", $product_id ), ARRAY_A ); return is_array( $row ) ? self::row( $row ) : null; }; if ( ! class_exists( 'AIEO_DMM_Cache' ) ) { return $read(); } return AIEO_DMM_Cache::remember( self::CACHE_DOMAIN, 'set_' . $product_id, $read, AIEO_DMM_Cache::TTL_SHORT ); } public static function row( array $row ): array { $members = self::members_of( $row ); $pids = array(); foreach ( self::listed( $row['product_ids'] ?? null ) as $pid ) { if ( is_scalar( $pid ) && (int) $pid > 0 ) { $pids[] = (int) $pid; } } $row['id'] = (int) ( $row['id'] ?? 0 ); $row['enabled'] = empty( $row['enabled'] ) ? 0 : 1; $row['label'] = (string) ( $row['label'] ?? '' ); $row['source_product_id'] = (int) ( $row['source_product_id'] ?? 0 ); $row['product_ids'] = $pids; $row['members'] = $members; $row['offer_id'] = (int) ( $row['offer_id'] ?? 0 ); $row['offer_title'] = (string) ( $row['offer_title'] ?? '' ); $row['button_text'] = (string) ( $row['button_text'] ?? '' ); $row['placement'] = (string) ( $row['placement'] ?? 'before_add_to_cart' ); $row['checkboxes_state'] = (string) ( $row['checkboxes_state'] ?? 'unchecked' ); return $row; } public static function members_of( array $row ): array { $members = self::clean( self::listed( $row['members'] ?? null ) ); if ( $members ) { return $members; } $list = array(); $source = ( isset( $row['source_product_id'] ) && is_scalar( $row['source_product_id'] ) ) ? (int) $row['source_product_id'] : 0; if ( $source > 0 ) { $list[] = array( 'p' => $source ); } foreach ( self::listed( $row['product_ids'] ?? null ) as $pid ) { $list[] = array( 'p' => is_scalar( $pid ) ? (int) $pid : 0 ); } return self::clean( $list ); } private static function listed( $value ): array { if ( is_string( $value ) && '' !== $value ) { $value = json_decode( $value, true ); } return is_array( $value ) ? $value : array(); } private static function clean( array $raw ): array { $out = array(); $seen = array(); foreach ( $raw as $member ) { if ( ! is_array( $member ) ) { continue; } $p = ( isset( $member['p'] ) && is_scalar( $member['p'] ) ) ? (int) $member['p'] : 0; if ( $p <= 0 || isset( $seen[ $p ] ) ) { continue; } $min = ( isset( $member['min'] ) && is_scalar( $member['min'] ) ) ? (int) $member['min'] : 1; $min = max( 1, min( self::MAX_UNITS, $min ) ); $max = ( isset( $member['max'] ) && is_scalar( $member['max'] ) ) ? (int) $member['max'] : $min; $max = max( $min, min( self::MAX_UNITS, $max ) ); $seen[ $p ] = true; $out[] = array( 'p' => $p, 'min' => $min, 'max' => $max, ); if ( count( $out ) >= self::MAX_MEMBERS ) { break; } } return $out; } } }