Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 60 additions & 47 deletions includes/fbproductfeed.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
// phpcs:ignoreFile
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
Expand Down Expand Up @@ -32,8 +31,10 @@ class WC_Facebook_Product_Feed {
const FB_PRODUCT_GROUP_ID = 'fb_product_group_id';
const FB_VISIBILITY = 'fb_visibility';

/** @var int has default product count */
private $has_default_product_count = 0;
private $no_default_product_count = 0;
/** @var int no default product count */
private $no_default_product_count = 0;

/**
* Generates the product catalog feed.
Expand Down Expand Up @@ -75,7 +76,7 @@ public function generate_feed() {
)
);

do_action('wc_facebook_feed_generation_completed');
do_action( 'wc_facebook_feed_generation_completed' );

} catch ( \Exception $exception ) {
Logger::log(
Expand Down Expand Up @@ -210,7 +211,7 @@ private function get_product_ids() {
* Generates the product catalog feed file.
*
* @return bool
* @throws PluginException
* @throws PluginException If the feed file directory can't be created or the feed file can't be written.
*/
public function generate_productfeed_file() {

Expand Down Expand Up @@ -248,11 +249,9 @@ public function create_files_to_protect_product_feed_directory() {
);

foreach ( $files as $file ) {

if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {

if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {

$file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' );
if ( $file_handle ) {
fwrite( $file_handle, $file['content'] );
fclose( $file_handle );
}
Expand Down Expand Up @@ -306,7 +305,7 @@ public function write_product_feed_file( $wp_ids ) {

// delete the temporary file
if ( ! empty( $temp_file_path ) && file_exists( $temp_file_path ) ) {

// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
unlink( $temp_file_path );
}
}
Expand Down Expand Up @@ -347,14 +346,16 @@ public function prepare_temporary_feed_file() {
*
* @since 2.6.6
*
* @param array $wp_ids
* @param resource $temp_feed_file
* @return void
*/
public function write_products_feed_to_temp_file( $wp_ids, $temp_feed_file ) {
$product_group_attribute_variants = array();

foreach ( $wp_ids as $wp_id ) {

$product = wc_get_product( $wp_id );
$product = wc_get_product( $wp_id );
$fb_product_parent = null;
if ( $product instanceof WC_Product && $product->get_parent_id() ) {
$parent_product = wc_get_product( $product->get_parent_id() );
Expand Down Expand Up @@ -399,12 +400,13 @@ public function write_products_feed_to_temp_file( $wp_ids, $temp_feed_file ) {
* @since 2.6.6
*
* @return void
* @throws PluginException If we can't rename the temporary feed file.
*/
public function rename_temporary_feed_file_to_final_feed_file() {
$file_path = $this->get_file_path();
$temp_file_path = $this->get_temp_file_path();
if ( ! empty( $temp_file_path ) && ! empty( $file_path ) ) {

// phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename
$renamed = rename( $temp_file_path, $file_path );

if ( empty( $renamed ) ) {
Expand All @@ -417,9 +419,9 @@ public function get_product_feed_header_row() {
return 'id,title,description,image_link,link,product_type,' .
'brand,price,availability,item_group_id,checkout_url,' .
'additional_image_link,sale_price_effective_date,sale_price,condition,' .
'visibility,gender,color,size,pattern,google_product_category,default_product,'.
'variant,gtin,quantity_to_sell_on_facebook,rich_text_description,internal_label,external_update_time,'.
'external_variant_id, is_woo_all_products_sync'. PHP_EOL ;
'visibility,gender,color,size,pattern,google_product_category,default_product,' .
'variant,gtin,quantity_to_sell_on_facebook,rich_text_description,internal_label,external_update_time,' .
'external_variant_id, is_woo_all_products_sync' . PHP_EOL;
}


Expand Down Expand Up @@ -505,20 +507,21 @@ private function prepare_product_for_feed( $woo_product, &$attribute_variants )
$item_group_id = $parent_attribute_values['item_group_id'];
}

// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
$product_data['default_product'] = $parent_attribute_values['default_variant_id'] == $woo_product->id ? 'default' : '';

// If this group has default variant value, log this product item
if ( isset( $parent_attribute_values['default_variant_id'] ) && ! empty( $parent_attribute_values['default_variant_id'] ) ) {
$this->has_default_product_count++;
++$this->has_default_product_count;
} else {
$this->no_default_product_count++;
++$this->no_default_product_count;
}
}

// log simple product
if ( ! isset( $product_data['default_product'] ) ) {

$this->no_default_product_count++;
++$this->no_default_product_count;

$product_data['default_product'] = '';
}
Expand Down Expand Up @@ -549,32 +552,33 @@ private function prepare_product_for_feed( $woo_product, &$attribute_variants )
static::get_value_from_product_data( $product_data, 'url' ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'product_type' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'brand' ) ) . ',' .
static::format_string_for_feed( static::format_price_for_feed(
static::get_value_from_product_data( $product_data, 'price', 0 ),
static::get_value_from_product_data( $product_data, 'currency' )
)) . ',' .
static::format_string_for_feed(
static::format_price_for_feed(
static::get_value_from_product_data( $product_data, 'price', 0 ),
static::get_value_from_product_data( $product_data, 'currency' )
)
) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'availability' ) ) . ',' .
$item_group_id . ',' .
static::get_value_from_product_data( $product_data, 'checkout_url' ) . ',' .
static::format_additional_image_url( static::get_value_from_product_data( $product_data, 'additional_image_urls' ) ) . ',' .
static::format_string_for_feed( $sale_price_effective_date ) . ',' .
static::format_string_for_feed( $sale_price ) . ',' .
'new' . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'visibility' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gender' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'color' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'size' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'pattern' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'google_product_category' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'default_product' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'variant' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gtin' )) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'quantity_to_sell_on_facebook' )) . ',' .
static::format_string_for_feed( $sale_price ) . ',new,' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'visibility' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gender' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'color' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'size' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'pattern' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'google_product_category' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'default_product' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'variant' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'gtin' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'quantity_to_sell_on_facebook' ) ) . ',' .
static::format_string_for_feed( static::get_value_from_product_data( $product_data, 'rich_text_description' ) ) . ',' .
static::format_internal_labels_for_feed( static::get_value_from_product_data( $product_data, 'internal_label' ) ) . ',' .
static::get_value_from_product_data( $product_data, 'external_update_time' ) . ',' .
static::get_value_from_product_data( $product_data, 'external_variant_id' ) . ',' .
static::format_string_for_feed($is_woo_all_products_sync). PHP_EOL ;
static::format_string_for_feed( $is_woo_all_products_sync ) . PHP_EOL;
}

private static function format_additional_image_url( $product_image_urls ) {
Expand All @@ -601,21 +605,25 @@ private static function format_string_for_feed( $text ) {
}

private static function format_internal_labels_for_feed( $internal_labels ): string {
$quoted_internal_labels = array_map(function(string $label) {
return sprintf("'%s'", $label);
} , $internal_labels);
return sprintf("[%s]", implode( ',', $quoted_internal_labels ));
$quoted_internal_labels = array_map(
function ( string $label ) {
return sprintf( "'%s'", $label );
},
$internal_labels
);
return sprintf( '[%s]', implode( ',', $quoted_internal_labels ) );
}

private static function format_price_for_feed( $value, $currency ) {
return (string) ( round( $value / (float) 100, 2 ) ) . $currency;
}

private static function format_variant_for_feed(
$product_field,
$value,
$parent_attribute_values,
&$variant_feed_column ) {
$product_field,
$value,
$parent_attribute_values,
&$variant_feed_column
) {
if ( ! array_key_exists( $product_field, $parent_attribute_values ) ) {
return;
}
Expand Down Expand Up @@ -667,7 +675,7 @@ public function is_upload_complete( &$settings ) {

if ( isset( $result->end_time ) ) {
$settings['upload_end_time'] = $result->end_time;
$upload_status = 'complete';
$upload_status = 'complete';
} elseif ( 200 === (int) wp_remote_retrieve_response_code( $result ) ) {
$upload_status = 'in progress';
}
Expand All @@ -680,12 +688,17 @@ public function is_upload_complete( &$settings ) {
}


// Log progress in local log file and FB.
public function log_feed_progress( $msg, $object = array() ) {
$msg = empty( $object ) ? $msg : $msg . wp_json_encode( $object );
/**
* Log progress in local log file and FB.
*
* @param string $msg
* @param array $obj
*/
public function log_feed_progress( $msg, $obj = array() ) {
$msg = empty( $obj ) ? $msg : $msg . wp_json_encode( $obj );
Logger::log(
$msg,
$object,
$obj,
array(
'should_send_log_to_meta' => true,
'should_save_log_in_woocommerce' => true,
Expand Down