Skip to content
Open
240 changes: 151 additions & 89 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1157,25 +1157,25 @@ public function add_product_settings_tab_content() {
global $post;

// all products have sync enabled unless explicitly disabled
$sync_enabled = 'no' !== get_post_meta( $post->ID, Products::SYNC_ENABLED_META_KEY, true );
$visibility = get_post_meta( $post->ID, Products::VISIBILITY_META_KEY, true );
$is_visible = $visibility ? wc_string_to_bool( $visibility ) : true;
$product = wc_get_product( $post );

$rich_text_description = get_post_meta( $post->ID, \WC_Facebookcommerce_Integration::FB_RICH_TEXT_DESCRIPTION, true );
$price = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_PRICE, true );
$image_source = get_post_meta( $post->ID, Products::PRODUCT_IMAGE_SOURCE_META_KEY, true );
$image = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_IMAGE, true );
$video_urls = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_VIDEO, true );
$fb_brand = get_post_meta( $post->ID, \WC_Facebook_Product::FB_BRAND, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_BRAND, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_brand', true );
$fb_mpn = get_post_meta( $post->ID, \WC_Facebook_Product::FB_MPN, true );
$fb_condition = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_CONDITION, true );
$fb_age_group = get_post_meta( $post->ID, \WC_Facebook_Product::FB_AGE_GROUP, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_AGE_GROUP, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_age_group', true );
$fb_gender = get_post_meta( $post->ID, \WC_Facebook_Product::FB_GENDER, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_GENDER, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_gender', true );
$fb_size = get_post_meta( $post->ID, \WC_Facebook_Product::FB_SIZE, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_SIZE, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_size', true );
$fb_color = get_post_meta( $post->ID, \WC_Facebook_Product::FB_COLOR, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_COLOR, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_color', true );
$fb_material = get_post_meta( $post->ID, \WC_Facebook_Product::FB_MATERIAL, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_MATERIAL, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_material', true );
$fb_pattern = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PATTERN, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_PATTERN, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_pattern', true );
$sync_enabled = 'no' !== get_post_meta( $post->ID, Products::SYNC_ENABLED_META_KEY, true );
$visibility = get_post_meta( $post->ID, Products::VISIBILITY_META_KEY, true );
$is_visible = $visibility ? wc_string_to_bool( $visibility ) : true;
$product = wc_get_product( $post );
$fb_product_description = get_post_meta( $post->ID, \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, true );
$fb_mpn = get_post_meta( $post->ID, \WC_Facebook_Product::FB_MPN, true );
$rich_text_description = get_post_meta( $post->ID, \WC_Facebookcommerce_Integration::FB_RICH_TEXT_DESCRIPTION, true );
$price = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_PRICE, true );
$image_source = get_post_meta( $post->ID, Products::PRODUCT_IMAGE_SOURCE_META_KEY, true );
$image = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_IMAGE, true );
$video_urls = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_VIDEO, true );
$fb_brand = get_post_meta( $post->ID, \WC_Facebook_Product::FB_BRAND, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_BRAND, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_brand', true );
$fb_condition = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PRODUCT_CONDITION, true );
$fb_age_group = get_post_meta( $post->ID, \WC_Facebook_Product::FB_AGE_GROUP, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_AGE_GROUP, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_age_group', true );
$fb_gender = get_post_meta( $post->ID, \WC_Facebook_Product::FB_GENDER, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_GENDER, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_gender', true );
$fb_size = get_post_meta( $post->ID, \WC_Facebook_Product::FB_SIZE, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_SIZE, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_size', true );
$fb_color = get_post_meta( $post->ID, \WC_Facebook_Product::FB_COLOR, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_COLOR, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_color', true );
$fb_material = get_post_meta( $post->ID, \WC_Facebook_Product::FB_MATERIAL, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_MATERIAL, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_material', true );
$fb_pattern = get_post_meta( $post->ID, \WC_Facebook_Product::FB_PATTERN, true ) ? get_post_meta( $post->ID, \WC_Facebook_Product::FB_PATTERN, true ) : get_post_meta( $post->ID, '_wc_facebook_enhanced_catalog_attributes_pattern', true );

if ( $sync_enabled ) {
$sync_mode = $is_visible ? self::SYNC_MODE_SYNC_AND_SHOW : self::SYNC_MODE_SYNC_AND_HIDE;
Expand All @@ -1189,6 +1189,29 @@ public function add_product_settings_tab_content() {
<div class='options_group hide_if_variable'>
<?php

// Only show deprecation notice if any of the deprecated fields exist
if ( $post->ID && ( $fb_product_description || $image || $price ) ) {
?>
<div class="notice notice-warning inline is-dismissible" style="padding: 1px 12px; margin: 10px 10px 0; border-left: 4px solid #dba617; background-color: #fff;">
<p style="margin: 8px 0;">
<strong><?php esc_html_e( 'Some attributes are no longer supported', 'facebook-for-woocommerce' ); ?></strong><br/>
<?php esc_html_e( 'Facebook Description, Custom Image URL, and Facebook Price are no longer supported and have been removed. This update will not affect your ads or shops on Meta.', 'facebook-for-woocommerce' ); ?>
</p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'facebook-for-woocommerce' ); ?></span>
</button>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.notice.is-dismissible').on('click', '.notice-dismiss', function(e) {
e.preventDefault();
$(this).closest('.notice').fadeOut();
});
});
</script>
<?php
}

woocommerce_wp_select(
array(
'id' => 'wc_facebook_sync_mode',
Expand All @@ -1204,80 +1227,86 @@ public function add_product_settings_tab_content() {
)
);

echo '<div class="wp-editor-wrap">';
echo '<label for="' . esc_attr( \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION ) . '">' .
esc_html__( 'Facebook Description', 'facebook-for-woocommerce' ) .
'</label>';
wp_editor(
$rich_text_description,
\WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION,
array(
'id' => 'wc_facebook_sync_mode',
'textarea_name' => \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION,
'textarea_rows' => 10,
'media_buttons' => true,
'teeny' => true,
'quicktags' => false,
'tinymce' => array(
'toolbar1' => 'bold,italic,bullist,spellchecker,fullscreen',
),
)
);
echo '</div>';

woocommerce_wp_radio(
array(
'id' => 'fb_product_image_source',
'label' => __( 'Facebook Product Image', 'facebook-for-woocommerce' ),
'desc_tip' => true,
'description' => __( 'Choose the product image that should be synced to the Facebook catalog and displayed for this product.', 'facebook-for-woocommerce' ),
'options' => array(
Products::PRODUCT_IMAGE_SOURCE_PRODUCT => __( 'Use WooCommerce image', 'facebook-for-woocommerce' ),
Products::PRODUCT_IMAGE_SOURCE_CUSTOM => __( 'Use custom image', 'facebook-for-woocommerce' ),
),
'value' => $image_source ? $image_source : Products::PRODUCT_IMAGE_SOURCE_PRODUCT,
'class' => 'short enable-if-sync-enabled js-fb-product-image-source',
'wrapper_class' => 'fb-product-image-source-field',
)
);
// Check if this is an existing product with a Facebook description
if ( $post->ID && $fb_product_description ) {
echo '<div class="wp-editor-wrap">';
echo '<label for="' . esc_attr( \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION ) . '">' .
esc_html__( 'Facebook Description', 'facebook-for-woocommerce' ) .
'</label>';
wp_editor(
$rich_text_description,
\WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION,
array(
'id' => 'wc_facebook_sync_mode',
'textarea_name' => \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION,
'textarea_rows' => 10,
'media_buttons' => true,
'teeny' => true,
'quicktags' => false,
'tinymce' => array(
'toolbar1' => 'bold,italic,bullist,spellchecker,fullscreen',
),
)
);
echo '</div>';
}
if ( $post->ID && $image ) {
woocommerce_wp_radio(
array(
'id' => 'fb_product_image_source',
'label' => __( 'Facebook Product Image', 'facebook-for-woocommerce' ),
'desc_tip' => true,
'description' => __( 'Choose the product image that should be synced to the Facebook catalog and displayed for this product.', 'facebook-for-woocommerce' ),
'options' => array(
Products::PRODUCT_IMAGE_SOURCE_PRODUCT => __( 'Use WooCommerce image', 'facebook-for-woocommerce' ),
Products::PRODUCT_IMAGE_SOURCE_CUSTOM => __( 'Use custom image', 'facebook-for-woocommerce' ),
),
'value' => $image_source ? $image_source : Products::PRODUCT_IMAGE_SOURCE_PRODUCT,
'class' => 'short enable-if-sync-enabled js-fb-product-image-source',
'wrapper_class' => 'fb-product-image-source-field',
)
);

woocommerce_wp_text_input(
array(
'id' => \WC_Facebook_Product::FB_PRODUCT_IMAGE,
'label' => __( 'Custom Image URL', 'facebook-for-woocommerce' ),
'value' => $image,
'class' => sprintf( 'enable-if-sync-enabled product-image-source-field show-if-product-image-source-%s', Products::PRODUCT_IMAGE_SOURCE_CUSTOM ),
'desc_tip' => true,
'description' => __( 'Please enter an absolute URL (e.g. https://domain.com/image.jpg).', 'facebook-for-woocommerce' ),
)
);
woocommerce_wp_text_input(
array(
'id' => \WC_Facebook_Product::FB_PRODUCT_IMAGE,
'label' => __( 'Custom Image URL', 'facebook-for-woocommerce' ),
'value' => $image,
'class' => sprintf( 'enable-if-sync-enabled product-image-source-field show-if-product-image-source-%s', Products::PRODUCT_IMAGE_SOURCE_CUSTOM ),
'desc_tip' => true,
'description' => __( 'Please enter an absolute URL (e.g. https://domain.com/image.jpg).', 'facebook-for-woocommerce' ),
)
);
}

$this->render_facebook_product_video_field( $video_urls );

woocommerce_wp_text_input(
array(
'id' => \WC_Facebook_Product::FB_PRODUCT_PRICE,
'label' => sprintf(
/* translators: Placeholders %1$s - WC currency symbol */
__( 'Facebook Price (%1$s)', 'facebook-for-woocommerce' ),
get_woocommerce_currency_symbol()
),
'desc_tip' => true,
'description' => __( 'Custom price for product on Facebook. Please enter in monetary decimal (.) format without thousand separators and currency symbols. If blank, product price will be used.', 'facebook-for-woocommerce' ),
'cols' => 40,
'rows' => 60,
'value' => $price,
'class' => 'enable-if-sync-enabled',
)
);
if ( $post->ID && $price ) {
woocommerce_wp_text_input(
array(
'id' => \WC_Facebook_Product::FB_PRODUCT_PRICE,
'label' => sprintf(
/* translators: Placeholders %1$s - WC currency symbol */
__( 'Facebook Price (%1$s)', 'facebook-for-woocommerce' ),
get_woocommerce_currency_symbol()
),
'desc_tip' => true,
'description' => __( 'Custom price for product on Facebook. Please enter in monetary decimal (.) format without thousand separators and currency symbols. If blank, product price will be used.', 'facebook-for-woocommerce' ),
'cols' => 40,
'rows' => 60,
'value' => $price,
'class' => 'enable-if-sync-enabled',
)
);
}

woocommerce_wp_hidden_input(
array(
'id' => \WC_Facebook_Product::FB_REMOVE_FROM_SYNC,
'value' => '',
)
);
?>
?>
</div>

<div class='wc_facebook_commerce_fields'>
Expand Down Expand Up @@ -1480,6 +1509,8 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
$image_source = $variation->get_meta( Products::PRODUCT_IMAGE_SOURCE_META_KEY );
$fb_mpn = $this->get_product_variation_meta( $variation, \WC_Facebook_Product::FB_MPN, $parent );

$has_fb_specific_variation_fields = ! empty( $description ) || ! empty( $image_url ) || ! empty( $price );

if ( $sync_enabled ) {
$sync_mode = $is_visible ? self::SYNC_MODE_SYNC_AND_SHOW : self::SYNC_MODE_SYNC_AND_HIDE;
} else {
Expand All @@ -1494,6 +1525,28 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
</h3>
<div class="wc-metabox-content" style="display: none;">
<?php
// Show deprecation notice only if deprecated fields have values
if ( $variation->get_id() && $has_fb_specific_variation_fields ) {
?>
<div class="notice notice-warning inline is-dismissible" style="padding: 1px 12px; margin: 10px 10px 0; border-left: 4px solid #dba617; background-color: #fff;">
<p style="margin: 8px 0;">
<strong><?php esc_html_e( 'Some attributes are no longer supported', 'facebook-for-woocommerce' ); ?></strong><br/>
<?php esc_html_e( 'Facebook Description, Custom Image URL, and Facebook Price are no longer supported and have been removed. This update will not affect your ads or shops on Meta.', 'facebook-for-woocommerce' ); ?>
</p>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'facebook-for-woocommerce' ); ?></span>
</button>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.notice.is-dismissible').on('click', '.notice-dismiss', function(e) {
e.preventDefault();
$(this).closest('.notice').fadeOut();
});
});
</script>
<?php
}
// Sync Mode Select
woocommerce_wp_select(
array(
Expand All @@ -1513,6 +1566,9 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
)
);

// Only show deprecated fields if they have existing values
if ( $variation->get_id() && $has_fb_specific_variation_fields ) {
if ( ! empty( $description ) ) {
woocommerce_wp_textarea_input(
array(
'id' => sprintf( 'variable_%s%s', \WC_Facebookcommerce_Integration::FB_PRODUCT_DESCRIPTION, $index ),
Expand All @@ -1525,7 +1581,9 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
'wrapper_class' => 'form-row form-row-full',
)
);
}

if ( ! empty( $image_url ) ) {
woocommerce_wp_radio(
array(
'id' => "variable_fb_product_image_source$index",
Expand Down Expand Up @@ -1556,13 +1614,15 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
'description' => __( 'Please enter an absolute URL (e.g. https://domain.com/image.jpg).', 'facebook-for-woocommerce' ),
)
);
}

if ( ! empty( $price ) ) {
woocommerce_wp_text_input(
array(
'id' => sprintf( 'variable_%s%s', \WC_Facebook_Product::FB_PRODUCT_PRICE, $index ),
'name' => sprintf( "variable_%s[$index]", \WC_Facebook_Product::FB_PRODUCT_PRICE ),
'label' => sprintf(
/* translators: Placeholders %1$s - WC currency symbol */
/* translators: Placeholders %1$s - WC currency symbol */
__( 'Facebook Price (%1$s)', 'facebook-for-woocommerce' ),
get_woocommerce_currency_symbol()
),
Expand All @@ -1573,20 +1633,23 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
'wrapper_class' => 'form-row form-full',
)
);
}
}

// Always show MPN field as it's not deprecated
woocommerce_wp_text_input(
array(
'id' => sprintf( 'variable_%s%s', \WC_Facebook_Product::FB_MPN, $index ),
'name' => sprintf( "variable_%s[$index]", \WC_Facebook_Product::FB_MPN ),
'label' => __( 'Manufacturer Parts Number (MPN)', 'facebook-for-woocommerce' ),
'label' => __( 'Manufacturer Part Number (MPN)', 'facebook-for-woocommerce' ),
'desc_tip' => true,
'description' => __( 'Manufacturer Parts Number', 'facebook-for-woocommerce' ),
'value' => wc_format_decimal( $fb_mpn ),
'description' => __( 'Manufacturer Part Number', 'facebook-for-woocommerce' ),
'value' => $fb_mpn,
'class' => 'enable-if-sync-enabled',
'wrapper_class' => 'form-row form-full',
)
);
?>
?>
</div>
</div>

Expand All @@ -1611,10 +1674,9 @@ public function add_product_variation_edit_fields( $index, $variation_data, $pos
.hide();
});
</script>
<?php
<?php
}


/**
* Gets the stored value for the given meta of a product variation.
*
Expand Down
Loading
Loading