Skip to content

Commit 6eac8e7

Browse files
fix: categories backward compatibility
1 parent 9538c80 commit 6eac8e7

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

classes/admin.class.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public function get_wc_categories( $current_values ) {
443443

444444
$used_categories = array();
445445
if ( ! empty( $current_values['productmeta_categories'] ) ) {
446-
$used_categories = explode( "\n", $current_values['productmeta_categories'] );
446+
$used_categories = preg_split('/\r\n|\n/', $current_values['productmeta_categories'] );
447447
}
448448

449449
foreach ( $product_categories as $category ) {
@@ -620,21 +620,10 @@ function ppom_attach_ppoms() {
620620
$updated_cat = count( $categories_to_attach );
621621

622622
// +----- Attach Field to Tags -----+
623-
$categories_to_tags = isset( $_POST['ppom-attach-to-tags'] ) && is_array( $_POST['ppom-attach-to-tags'] ) ? array_map( 'sanitize_key', $_POST['ppom-attach-to-tags'] ) : array();
624-
$updated_tags = count( $categories_to_tags );
623+
$tags_to_attach = isset( $_POST['ppom-attach-to-tags'] ) && is_array( $_POST['ppom-attach-to-tags'] ) ? array_map( 'sanitize_key', $_POST['ppom-attach-to-tags'] ) : false;
624+
$updated_tags = is_array( $tags_to_attach) ? count( $tags_to_attach ) : 0;
625625

626-
global $wpdb;
627-
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
628-
$wpdb->update(
629-
$ppom_table,
630-
array(
631-
'productmeta_categories' => implode( "\n", $categories_to_attach ), // NOTE: Keep the backward compatible format.
632-
'productmeta_tags' => serialize( $categories_to_tags )
633-
), // Data to update
634-
array( 'productmeta_id' => $ppom_id ), // Where clause
635-
array( '%s' ), // Data format
636-
array( '%d' ) // Where format
637-
);
626+
self::save_categories_and_tags( $ppom_id, $categories_to_attach, $tags_to_attach );
638627

639628
$response = array(
640629
'message' => "PPOM updated for {$updated_products} Products, {$updated_cat} Categories and {$updated_tags} Tags.",
@@ -644,6 +633,36 @@ function ppom_attach_ppoms() {
644633
wp_send_json( $response );
645634
}
646635

636+
/**
637+
* Save the categories and tags to the given PPOM field.
638+
*
639+
* @param int $ppom_id The ID of the PPOM field.
640+
* @param array $categories An array of categories to save.
641+
* @param array|bool $tags An array of tags to save.
642+
*
643+
* @global wpdb $wpdb WordPress database abstraction object.
644+
*
645+
*/
646+
public static function save_categories_and_tags( $ppom_id, $categories, $tags ) {
647+
global $wpdb;
648+
$ppom_table = $wpdb->prefix . PPOM_TABLE_META;
649+
650+
$data_to_update = array(
651+
'productmeta_categories' => implode( "\r\n", $categories ), // NOTE: Keep the backward compatible format.
652+
);
653+
654+
if ( is_array( $tags ) ) {
655+
$data_to_update['productmeta_tags'] = serialize( $tags );
656+
}
657+
658+
$wpdb->update(
659+
$ppom_table,
660+
$data_to_update,
661+
array( 'productmeta_id' => $ppom_id ), // Where clause
662+
array( '%s' ), // Data format
663+
array( '%d' ) // Where format
664+
);
665+
}
647666

648667
/*
649668
* Plugin Validation

classes/ppom.class.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ function ppom_has_category_meta($product_id ) {
374374
$meta_found[] = $row->productmeta_id;
375375
} else {
376376
// making array of meta cats
377-
$meta_cat_array = explode( "\r\n", $row->productmeta_categories );
377+
378+
$meta_cat_array = preg_split('/\r\n|\n/', $row->productmeta_categories);
378379
// Now iterating the product_categories to check it's slug in meta cats
379380
foreach ( $product_categories as $cat ) {
380381
if ( in_array( $cat->slug, $meta_cat_array ) ) {

0 commit comments

Comments
 (0)