@@ -443,7 +443,7 @@ public function get_wc_categories( $current_values ) {
443
443
444
444
$ used_categories = array ();
445
445
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 ' ] );
447
447
}
448
448
449
449
foreach ( $ product_categories as $ category ) {
@@ -620,21 +620,10 @@ function ppom_attach_ppoms() {
620
620
$ updated_cat = count ( $ categories_to_attach );
621
621
622
622
// +----- 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 ;
625
625
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 );
638
627
639
628
$ response = array (
640
629
'message ' => "PPOM updated for {$ updated_products } Products, {$ updated_cat } Categories and {$ updated_tags } Tags. " ,
@@ -644,6 +633,36 @@ function ppom_attach_ppoms() {
644
633
wp_send_json ( $ response );
645
634
}
646
635
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
+ }
647
666
648
667
/*
649
668
* Plugin Validation
0 commit comments