Custom Post Types and Custom Fields creator – WCK - Version 2.0.1

Version Description

  • Fixed issue with Custom Fields Creator when fields had the same name as the meta name
Download this release

Release Info

Developer madalin.ungureanu
Plugin Icon 128x128 Custom Post Types and Custom Fields creator – WCK
Version 2.0.1
Comparing to
See all releases

Code changes from version 2.0.0 to 2.0.1

readme.txt CHANGED
@@ -6,7 +6,7 @@ Tags: custom fields, custom field, wordpress custom fields, custom post type, cu
6
 
7
  Requires at least: 3.1
8
  Tested up to: 4.6.1
9
- Stable tag: 2.0.0
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
@@ -139,6 +139,9 @@ Creating a taxonomy generally automatically creates a special query variable usi
139
  10. Taxonomy listing
140
 
141
  == Changelog ==
 
 
 
142
  = 2.0.0 =
143
  * We now save every custom field in it's own separate post meta with an editable meta_key
144
  * UI improvements to repeater sortable table
6
 
7
  Requires at least: 3.1
8
  Tested up to: 4.6.1
9
+ Stable tag: 2.0.1
10
  License: GPLv2 or later
11
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
12
 
139
  10. Taxonomy listing
140
 
141
  == Changelog ==
142
+ = 2.0.1 =
143
+ * Fixed issue with Custom Fields Creator when fields had the same name as the meta name
144
+
145
  = 2.0.0 =
146
  * We now save every custom field in it's own separate post meta with an editable meta_key
147
  * UI improvements to repeater sortable table
wck-cfc.php CHANGED
@@ -402,12 +402,12 @@ function wck_cfc_check_meta_name( $bool, $value, $post_id, $field, $meta, $field
402
 
403
  if( empty( $wck_cfc_args ) ){
404
  //this is the add case
405
- $check_meta_existance = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_key) FROM $wpdb->postmeta WHERE meta_key = %s", $value ) );
406
  }
407
  else{
408
  //this is the update case
409
  if( $wck_cfc_args[0]['meta-name'] != $value ){
410
- $check_meta_existance = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_key) FROM $wpdb->postmeta WHERE meta_key = %s", $value ) );
411
  }
412
  else
413
  $check_meta_existance = false;
@@ -420,12 +420,12 @@ function wck_cfc_check_meta_name( $bool, $value, $post_id, $field, $meta, $field
420
  $wck_cfc_fields = get_post_meta( $post_id, 'wck_cfc_fields', true );
421
  if( empty( $wck_cfc_fields ) || $elemet_id === false ){
422
  //this is the add case
423
- $check_meta_existance = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_key) FROM $wpdb->postmeta WHERE meta_key = %s", $value ) );
424
  }
425
  else if( $elemet_id !== false ){
426
  //this is the update case
427
  if( !empty( $wck_cfc_fields[$elemet_id]['field-slug'] ) && $wck_cfc_fields[$elemet_id]['field-slug'] != $value ){
428
- $check_meta_existance = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_key) FROM $wpdb->postmeta WHERE meta_key = %s", $value ) );
429
  }
430
  else
431
  $check_meta_existance = false;
@@ -472,6 +472,67 @@ function wck_cfc_change_meta_message( $message, $value, $required_field ){
472
  return sprintf( __( "Choose a different %s as this one already exists\n", "wck" ), $field_name );
473
  }
474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
 
476
  /* Field Name Verification */
477
  add_filter( 'wck_required_test_wck_cfc_fields_field-title', 'wck_cfc_ceck_field_title', 10, 6 );
@@ -802,11 +863,33 @@ function wck_cfc_make_options_required( $meta_array, $meta, $values, $id ) {
802
  }
803
 
804
  /* handle Field Slug backwards compatibility */
805
- add_filter( "wck_displayed_value_wck_cfc_fields_field-slug", "wck_cfc_handle_empty_field_slug", 10, 3 );
806
- add_filter( "wck_cfc_filter_edit_form_value_wck_cfc_fields_field-slug", "wck_cfc_handle_empty_field_slug", 10, 3 );
807
- function wck_cfc_handle_empty_field_slug( $value, $results, $element_id ){
808
  if( empty( $value ) ){
809
  $value = Wordpress_Creation_Kit::wck_generate_slug( $results[$element_id]['field-title'] );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
810
  }
811
 
812
  return $value;
402
 
403
  if( empty( $wck_cfc_args ) ){
404
  //this is the add case
405
+ $check_meta_existance = wck_cfc_check_existence_in_database( $value );
406
  }
407
  else{
408
  //this is the update case
409
  if( $wck_cfc_args[0]['meta-name'] != $value ){
410
+ $check_meta_existance = wck_cfc_check_existence_in_database( $value );
411
  }
412
  else
413
  $check_meta_existance = false;
420
  $wck_cfc_fields = get_post_meta( $post_id, 'wck_cfc_fields', true );
421
  if( empty( $wck_cfc_fields ) || $elemet_id === false ){
422
  //this is the add case
423
+ $check_meta_existance = wck_cfc_check_existence_in_database( $value );
424
  }
425
  else if( $elemet_id !== false ){
426
  //this is the update case
427
  if( !empty( $wck_cfc_fields[$elemet_id]['field-slug'] ) && $wck_cfc_fields[$elemet_id]['field-slug'] != $value ){
428
+ $check_meta_existance = wck_cfc_check_existence_in_database( $value );
429
  }
430
  else
431
  $check_meta_existance = false;
472
  return sprintf( __( "Choose a different %s as this one already exists\n", "wck" ), $field_name );
473
  }
474
 
475
+ /* function that check if a group name already exists */
476
+ function wck_cfc_check_group_name_exists( $name ){
477
+ $args = array(
478
+ 'posts_per_page' => -1,
479
+ 'numberposts' => -1,
480
+ 'post_type' => 'wck-meta-box',
481
+ 'post_status' => 'any'
482
+ );
483
+ $meta_boxes = get_posts( $args );
484
+ if( !empty( $meta_boxes ) ){
485
+ foreach( $meta_boxes as $meta_box ){
486
+ $wck_cfc_args = get_post_meta( $meta_box->ID, 'wck_cfc_args', true );
487
+ if( !empty( $wck_cfc_args ) ){
488
+ if( $wck_cfc_args[0]['meta-name'] == $name ){
489
+ return true;
490
+ }
491
+ }
492
+ }
493
+ }
494
+ return false;
495
+ }
496
+
497
+ /* function that check if a field slug already exists */
498
+ function wck_cfc_check_field_slug_exists( $name ){
499
+ $args = array(
500
+ 'posts_per_page' => -1,
501
+ 'numberposts' => -1,
502
+ 'post_type' => 'wck-meta-box',
503
+ 'post_status' => 'any'
504
+ );
505
+ $meta_boxes = get_posts( $args );
506
+ if( !empty( $meta_boxes ) ){
507
+ foreach( $meta_boxes as $meta_box ){
508
+ $wck_cfc_fields = get_post_meta( $meta_box->ID, 'wck_cfc_fields', true );
509
+ if( !empty( $wck_cfc_fields ) ){
510
+ foreach( $wck_cfc_fields as $wck_cfc_field ){
511
+ if( $wck_cfc_field['field-slug'] == $name ){
512
+ return true;
513
+ }
514
+ }
515
+ }
516
+ }
517
+ }
518
+ return false;
519
+ }
520
+
521
+ function wck_cfc_check_existence_in_database( $name ){
522
+ global $wpdb;
523
+ $check_meta_existance = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(meta_key) FROM $wpdb->postmeta WHERE meta_key = %s", $name ) );
524
+ /* if we did not find anything yet */
525
+ if( !$check_meta_existance ){
526
+ $check_meta_existance = wck_cfc_check_group_name_exists( $name );
527
+ }
528
+ /* if we still did not find anything yet */
529
+ if( !$check_meta_existance ){
530
+ $check_meta_existance = wck_cfc_check_field_slug_exists( $name );
531
+ }
532
+
533
+ return $check_meta_existance;
534
+ }
535
+
536
 
537
  /* Field Name Verification */
538
  add_filter( 'wck_required_test_wck_cfc_fields_field-title', 'wck_cfc_ceck_field_title', 10, 6 );
863
  }
864
 
865
  /* handle Field Slug backwards compatibility */
866
+ add_filter( "wck_displayed_value_wck_cfc_fields_field-slug", "wck_cfc_handle_empty_field_slug", 10, 4 );
867
+ add_filter( "wck_cfc_filter_edit_form_value_wck_cfc_fields_field-slug", "wck_cfc_handle_empty_field_slug", 10, 4 );
868
+ function wck_cfc_handle_empty_field_slug( $value, $results, $element_id, $id ){
869
  if( empty( $value ) ){
870
  $value = Wordpress_Creation_Kit::wck_generate_slug( $results[$element_id]['field-title'] );
871
+ $check_meta_existance = wck_cfc_check_group_name_exists( $value );
872
+ if( $check_meta_existance ){
873
+ $wck_cfc_args = get_post_meta( $id, 'wck_cfc_args', true );
874
+ if( !empty( $wck_cfc_args ) ){
875
+ if( current_filter() == 'wck_displayed_value_wck_cfc_fields_field-slug' ){
876
+ $value = '('.$wck_cfc_args[0]['meta-name'].'_)'.$value;
877
+ }
878
+ else
879
+ $value = $wck_cfc_args[0]['meta-name'].'_'.$value;
880
+ }
881
+ }
882
+ }
883
+ else{
884
+ if( current_filter() == 'wck_displayed_value_wck_cfc_fields_field-slug' ) {
885
+ $check_meta_existance = wck_cfc_check_group_name_exists($value);
886
+ if( $check_meta_existance ){
887
+ $wck_cfc_args = get_post_meta( $id, 'wck_cfc_args', true );
888
+ if( !empty( $wck_cfc_args ) ){
889
+ $value = '('.$wck_cfc_args[0]['meta-name'].'_)'.$value;
890
+ }
891
+ }
892
+ }
893
  }
894
 
895
  return $value;
wck.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: WCK - Custom Fields and Custom Post Types Creator
4
  Description: WordPress Creation Kit consists of three tools that can help you create and maintain custom post types, custom taxonomies and most importantly, custom fields and metaboxes for your posts, pages or CPT's.
5
  Author: Cozmoslabs, Madalin Ungureanu, Cristian Antohe
6
- Version: 2.0.0
7
  Author URI: http://www.cozmoslabs.com
8
 
9
  License: GPL2
3
  Plugin Name: WCK - Custom Fields and Custom Post Types Creator
4
  Description: WordPress Creation Kit consists of three tools that can help you create and maintain custom post types, custom taxonomies and most importantly, custom fields and metaboxes for your posts, pages or CPT's.
5
  Author: Cozmoslabs, Madalin Ungureanu, Cristian Antohe
6
+ Version: 2.0.1
7
  Author URI: http://www.cozmoslabs.com
8
 
9
  License: GPL2
wordpress-creation-kit-api/wordpress-creation-kit.php CHANGED
@@ -452,7 +452,7 @@ class Wordpress_Creation_Kit{
452
 
453
  $form .= '<li class="row-'. esc_attr( Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ) ) .'">';
454
 
455
- $form .= self::wck_output_form_field( $meta, $details, apply_filters( "wck_cfc_filter_edit_form_value_{$meta}_".Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ) , $value, $results, $element_id ), 'edit_form', $id );
456
 
457
  $form .= '</li>';
458
 
@@ -546,7 +546,7 @@ class Wordpress_Creation_Kit{
546
  /* filter display value */
547
  /* keep this one for backwards compatibility */
548
  $value = apply_filters( "wck_displayed_value_{$meta}_element_{$j}", $value );
549
- $value = apply_filters( "wck_displayed_value_{$meta}_".Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ), $value, $results, $element_id );
550
 
551
  /* display it differently based on field type*/
552
  if( $details['type'] == 'upload' ){
@@ -902,6 +902,8 @@ class Wordpress_Creation_Kit{
902
 
903
  if( !empty( $values ) ){
904
  foreach( $values as $name => $value ){
 
 
905
  update_post_meta($id, $name.$meta_suffix, $value);
906
  }
907
  }
@@ -981,7 +983,9 @@ class Wordpress_Creation_Kit{
981
 
982
  if( !empty( $values ) ){
983
  foreach( $values as $name => $value ){
984
- update_post_meta($id, $name.$meta_suffix, $value);
 
 
985
  }
986
  }
987
  }
@@ -1130,6 +1134,8 @@ class Wordpress_Creation_Kit{
1130
  if( !empty( $old_results ) ) {
1131
  foreach ( $old_results as $result ) {
1132
  foreach ( $result as $name => $value ) {
 
 
1133
  delete_post_meta( $id, $name . $meta_suffix );
1134
  }
1135
  $meta_counter ++;
@@ -1143,6 +1149,7 @@ class Wordpress_Creation_Kit{
1143
  if( !empty( $results ) && count( $results ) != 0 ){
1144
  foreach( $results as $result ){
1145
  foreach ( $result as $name => $value){
 
1146
  update_post_meta($id, $name.$meta_suffix, $value);
1147
  }
1148
  $meta_counter ++;
@@ -1235,6 +1242,8 @@ class Wordpress_Creation_Kit{
1235
  if( !empty( $new_results ) ){
1236
  foreach( $new_results as $result ){
1237
  foreach ( $result as $name => $value){
 
 
1238
  update_post_meta($id, $name.$meta_suffix, $value);
1239
  }
1240
  $meta_counter++;
@@ -1341,7 +1350,9 @@ class Wordpress_Creation_Kit{
1341
  if ($this->args['unserialize']) {
1342
  if (!empty($this->args['meta_array'])) {
1343
  foreach ($this->args['meta_array'] as $meta_field) {
1344
- update_post_meta($post_id, Wordpress_Creation_Kit::wck_generate_slug( $meta_field['title'], $meta_field ), $_POST[$this->args['meta_name'] . '_' . Wordpress_Creation_Kit::wck_generate_slug( $meta_field['title'], $meta_field )]);
 
 
1345
  }
1346
  }
1347
  }
@@ -1595,6 +1606,30 @@ class Wordpress_Creation_Kit{
1595
 
1596
  return $slug;
1597
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1598
  }
1599
 
1600
 
452
 
453
  $form .= '<li class="row-'. esc_attr( Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ) ) .'">';
454
 
455
+ $form .= self::wck_output_form_field( $meta, $details, apply_filters( "wck_cfc_filter_edit_form_value_{$meta}_".Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ) , $value, $results, $element_id, $id ), 'edit_form', $id );
456
 
457
  $form .= '</li>';
458
 
546
  /* filter display value */
547
  /* keep this one for backwards compatibility */
548
  $value = apply_filters( "wck_displayed_value_{$meta}_element_{$j}", $value );
549
+ $value = apply_filters( "wck_displayed_value_{$meta}_".Wordpress_Creation_Kit::wck_generate_slug( $details['title'], $details ), $value, $results, $element_id, $id );
550
 
551
  /* display it differently based on field type*/
552
  if( $details['type'] == 'upload' ){
902
 
903
  if( !empty( $values ) ){
904
  foreach( $values as $name => $value ){
905
+ /* check to see if we already have a meta name like this from the old structure to avoid conflicts */
906
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $id, $name, $meta );
907
  update_post_meta($id, $name.$meta_suffix, $value);
908
  }
909
  }
983
 
984
  if( !empty( $values ) ){
985
  foreach( $values as $name => $value ){
986
+ /* check to see if we already have a meta name like this from the old structure to avoid conflicts */
987
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $id, $name, $meta );
988
+ update_post_meta( $id, $name.$meta_suffix, $value );
989
  }
990
  }
991
  }
1134
  if( !empty( $old_results ) ) {
1135
  foreach ( $old_results as $result ) {
1136
  foreach ( $result as $name => $value ) {
1137
+ /* check to see if we already have a meta name like this from the old structure to avoid conflicts */
1138
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $id, $name, $meta );
1139
  delete_post_meta( $id, $name . $meta_suffix );
1140
  }
1141
  $meta_counter ++;
1149
  if( !empty( $results ) && count( $results ) != 0 ){
1150
  foreach( $results as $result ){
1151
  foreach ( $result as $name => $value){
1152
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $id, $name, $meta );
1153
  update_post_meta($id, $name.$meta_suffix, $value);
1154
  }
1155
  $meta_counter ++;
1242
  if( !empty( $new_results ) ){
1243
  foreach( $new_results as $result ){
1244
  foreach ( $result as $name => $value){
1245
+ /* check to see if we already have a meta name like this from the old structure to avoid conflicts */
1246
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $id, $name, $meta );
1247
  update_post_meta($id, $name.$meta_suffix, $value);
1248
  }
1249
  $meta_counter++;
1350
  if ($this->args['unserialize']) {
1351
  if (!empty($this->args['meta_array'])) {
1352
  foreach ($this->args['meta_array'] as $meta_field) {
1353
+ /* check to see if we already have a meta name like this from the old structure to avoid conflicts */
1354
+ $name = Wordpress_Creation_Kit::wck_generate_unique_meta_name_for_unserialized_field( $post_id, Wordpress_Creation_Kit::wck_generate_slug( $meta_field['title'], $meta_field ), $meta_name );
1355
+ update_post_meta($post_id, $name, $_POST[$this->args['meta_name'] . '_' . Wordpress_Creation_Kit::wck_generate_slug( $meta_field['title'], $meta_field )]);
1356
  }
1357
  }
1358
  }
1606
 
1607
  return $slug;
1608
  }
1609
+
1610
+ /**
1611
+ * Function that makes sure we have a unique meta name for the unserialized structure
1612
+ * @param $id
1613
+ * @param $meta_name
1614
+ * @param $group_name
1615
+ * @return string
1616
+ */
1617
+ static function wck_generate_unique_meta_name_for_unserialized_field( $id, $meta_name, $group_name ){
1618
+ if( function_exists('wck_cfc_check_group_name_exists') ){
1619
+ if( wck_cfc_check_group_name_exists( $meta_name ) ){
1620
+ $meta_name = $group_name.'_'.$meta_name;
1621
+ }
1622
+ }
1623
+ else{
1624
+ $existing_meta = get_post_meta( $id, $meta_name, true );
1625
+ if( !empty( $existing_meta ) ){
1626
+ if( is_array( $existing_meta ) ){
1627
+ $meta_name = $group_name.'_'.$meta_name;
1628
+ }
1629
+ }
1630
+ }
1631
+ return $meta_name;
1632
+ }
1633
  }
1634
 
1635