get_sku( 'edit' ); return '' !== $sku ? $sku : (string) $product->get_id(); } public static function product_refs( array $products ) { $refs = array(); foreach ( $products as $product ) { if ( $product ) { $refs[] = rawurlencode( self::product_ref( $product ) ); } } return implode( ';', $refs ); } public static function enabled() { return RoleWcAdcell::option_bool( self::OPTION, false ); } public static function path() { $dir = wp_upload_dir( null, false ); return trailingslashit( $dir['basedir'] ) . self::DIR . '/' . self::FILE; } public static function url() { $dir = wp_upload_dir( null, false ); return trailingslashit( $dir['baseurl'] ) . self::DIR . '/' . self::FILE; } public static function sync_schedule() { if ( self::enabled() ) { if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + 60, 'daily', self::CRON_HOOK ); } return; } wp_clear_scheduled_hook( self::CRON_HOOK ); self::delete_file(); } public static function delete_file() { if ( file_exists( self::path() ) ) { wp_delete_file( self::path() ); } delete_option( self::STATS ); } public static function handle_build() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Keine Berechtigung.', 'adcell-tracking-und-remarketing' ), 403 ); } check_admin_referer( self::ACTION ); $built = self::build(); wp_safe_redirect( add_query_arg( array( 'page' => RoleWcAdcellAdmin::PAGE, 'adcell_tab' => 'feed', 'adcell_feed' => $built ? 'built' : 'failed', ), admin_url( 'options-general.php' ) ) ); exit; } public static function build() { if ( ! self::enabled() || ! function_exists( 'wc_get_products' ) ) { return false; } $path = self::path(); if ( ! wp_mkdir_p( dirname( $path ) ) ) { RoleWcAdcell::log( 'Produktfeed: Verzeichnis nicht beschreibbar', array( 'pfad' => dirname( $path ) ) ); return false; } if ( function_exists( 'wc_set_time_limit' ) ) { wc_set_time_limit( 0 ); } $tmp = $path . '.' . getmypid() . '.tmp'; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $handle = fopen( $tmp, 'wb' ); if ( ! $handle ) { RoleWcAdcell::log( 'Produktfeed: Datei nicht beschreibbar', array( 'pfad' => $tmp ) ); return false; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite fwrite( $handle, implode( ';', self::COLUMNS ) . "\n" ); $count = 0; $page = 1; do { $products = wc_get_products( array( 'status' => 'publish', 'type' => array( 'simple', 'variable' ), 'limit' => self::PAGE_SIZE, 'page' => $page, 'orderby' => 'ID', 'order' => 'ASC', ) ); foreach ( $products as $product ) { if ( 'hidden' === $product->get_catalog_visibility() || '' !== (string) $product->get_post_password() ) { continue; } foreach ( self::items( $product ) as $item ) { $fields = self::fields( $item, $product ); if ( $fields ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite fwrite( $handle, self::row( $fields ) . "\n" ); ++$count; } } } ++$page; } while ( count( $products ) === self::PAGE_SIZE ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename if ( ! rename( $tmp, $path ) ) { wp_delete_file( $tmp ); RoleWcAdcell::log( 'Produktfeed: Datei konnte nicht ersetzt werden', array( 'pfad' => $path ) ); return false; } update_option( self::STATS, array( 'time' => time(), 'count' => $count ), false ); RoleWcAdcell::log( 'Produktfeed erzeugt', array( 'artikel' => $count ) ); return true; } public static function stats() { $stats = get_option( self::STATS ); if ( ! is_array( $stats ) || ! file_exists( self::path() ) ) { return null; } $stats['size'] = (int) filesize( self::path() ); return $stats; } private static function items( $product ) { if ( ! $product->is_type( 'variable' ) ) { return array( $product ); } $items = array( $product ); foreach ( $product->get_children() as $child_id ) { $variation = wc_get_product( $child_id ); if ( $variation && 'publish' === $variation->get_status() ) { $items[] = $variation; } } return $items; } private static function fields( $item, $parent ) { $price = (float) wc_get_price_including_tax( $item ); if ( $price <= 0 ) { return null; } $image_id = $item->get_image_id() ? $item->get_image_id() : $parent->get_image_id(); $image = $image_id ? wp_get_attachment_url( $image_id ) : ''; return array( 'url' => $item->get_permalink(), 'title' => html_entity_decode( $item->get_name(), ENT_QUOTES, 'UTF-8' ), 'description' => self::description( $item, $parent ), 'price' => number_format( $price, 2, '.', '' ), 'waehrung' => get_woocommerce_currency(), 'ean' => method_exists( $item, 'get_global_unique_id' ) ? $item->get_global_unique_id() : '', 'id' => self::product_ref( $item ), 'hersteller' => self::brand( $parent ), 'image' => $image ? $image : '', 'kategiorie' => self::category_path( self::category_names( $parent ) ), 'lagerstand' => $item->is_in_stock() ? 'in_stock' : 'out_of_stock', ); } private static function description( $item, $parent ) { foreach ( array( $item, $parent ) as $source ) { foreach ( array( $source->get_short_description(), $source->get_description() ) as $text ) { $text = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( strip_shortcodes( (string) $text ) ) ) ); if ( '' !== $text ) { return html_entity_decode( $text, ENT_QUOTES, 'UTF-8' ); } } } return ''; } private static function brand( $product ) { if ( ! taxonomy_exists( 'product_brand' ) ) { return ''; } $names = wp_get_post_terms( $product->get_id(), 'product_brand', array( 'fields' => 'names' ) ); return ( ! is_wp_error( $names ) && $names ) ? html_entity_decode( (string) $names[0], ENT_QUOTES, 'UTF-8' ) : ''; } private static function category_names( $product ) { $best = array(); foreach ( $product->get_category_ids() as $term_id ) { $ids = array_reverse( get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ); $ids[] = $term_id; if ( count( $ids ) <= count( $best ) ) { continue; } $names = array(); foreach ( $ids as $id ) { $term = get_term( $id, 'product_cat' ); if ( $term && ! is_wp_error( $term ) ) { $names[] = html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ); } } $best = $names; } return $best; } public static function category_path( array $names ) { if ( ! $names ) { return ''; } $path = implode( '/', $names ) . '/'; return function_exists( 'mb_strtolower' ) ? mb_strtolower( $path, 'UTF-8' ) : strtolower( $path ); } public static function row( array $fields ) { $cells = array(); foreach ( self::COLUMNS as $column ) { $value = isset( $fields[ $column ] ) ? str_replace( array( "\r", "\n" ), ' ', (string) $fields[ $column ] ) : ''; $cells[] = in_array( $column, self::UNQUOTED, true ) ? str_replace( array( ';', '"' ), array( '%3B', '%22' ), $value ) : '"' . str_replace( '"', '""', $value ) . '"'; } return implode( ';', $cells ); } }