Smart Custom Fields - Version 2.2.0

Version Description

  • Refactoring tests.
  • Changed behavior of the default value of new field of the already saved object. Using the default value.
Download this release

Release Info

Developer inc2734
Plugin Icon wp plugin Smart Custom Fields
Version 2.2.0
Comparing to
See all releases

Code changes from version 2.1.1 to 2.2.0

classes/class.scf.php CHANGED
@@ -281,7 +281,7 @@ class SCF {
281
  $field_type = $Field->get_attribute( 'type' );
282
  $repeat_multiple_data = self::get_repeat_multiple_data( $object );
283
  if ( is_array( $repeat_multiple_data ) && isset( $repeat_multiple_data[$field_name] ) ) {
284
- if ( $Meta->is_saved() ) {
285
  $_meta = $Meta->get( $field_name );
286
  } else {
287
  $_meta = self::get_default_value( $Field );
@@ -304,7 +304,7 @@ class SCF {
304
  if ( $Field->get_attribute( 'allow-multiple-data' ) || $is_repeatable ) {
305
  $single = false;
306
  }
307
- if ( $Meta->is_saved() ) {
308
  $meta = $Meta->get( $field_name, $single );
309
  } else {
310
  $meta = self::get_default_value( $Field, $single );
@@ -615,7 +615,7 @@ class SCF {
615
  * @param boolean $multidimensional True if a multidimensional array is inclusion into associative array, the default value is false
616
  * @return boolean
617
  */
618
- public static function is_assoc( $data, $multidimensional=false ) {
619
  if ( !is_array( $data ) || empty( $data ) ) {
620
  return false;
621
  }
281
  $field_type = $Field->get_attribute( 'type' );
282
  $repeat_multiple_data = self::get_repeat_multiple_data( $object );
283
  if ( is_array( $repeat_multiple_data ) && isset( $repeat_multiple_data[$field_name] ) ) {
284
+ if ( $Meta->is_saved_the_key( $field_name ) ) {
285
  $_meta = $Meta->get( $field_name );
286
  } else {
287
  $_meta = self::get_default_value( $Field );
304
  if ( $Field->get_attribute( 'allow-multiple-data' ) || $is_repeatable ) {
305
  $single = false;
306
  }
307
+ if ( $Meta->is_saved_the_key( $field_name ) ) {
308
  $meta = $Meta->get( $field_name, $single );
309
  } else {
310
  $meta = self::get_default_value( $Field, $single );
615
  * @param boolean $multidimensional True if a multidimensional array is inclusion into associative array, the default value is false
616
  * @return boolean
617
  */
618
+ public static function is_assoc( $data, $multidimensional = false ) {
619
  if ( !is_array( $data ) || empty( $data ) ) {
620
  return false;
621
  }
classes/controller/class.controller-base.php CHANGED
@@ -176,7 +176,7 @@ class Smart_Custom_Fields_Controller_Base {
176
  * @param WP_Post|WP_User|WP_Term|stdClass $object
177
  * @param Smart_Custom_Fields_Field_Base $Field
178
  * @param int $index
179
- * @return array or null
180
  */
181
  public function get_multiple_data_field_value( $object, $Field, $index ) {
182
  $Meta = new Smart_Custom_Fields_Meta( $object );
@@ -186,7 +186,7 @@ class Smart_Custom_Fields_Controller_Base {
186
  return SCF::get_default_value( $Field );
187
  }
188
 
189
- if ( !$Meta->is_saved() ) {
190
  return SCF::get_default_value( $Field );
191
  }
192
 
@@ -220,7 +220,7 @@ class Smart_Custom_Fields_Controller_Base {
220
  * @param WP_Post|WP_User|WP_Term|stdClass $object
221
  * @param Smart_Custom_Fields_Field_Base $Field
222
  * @param int $index
223
- * @return string or null
224
  */
225
  public function get_single_data_field_value( $object, $Field, $index ) {
226
  $Meta = new Smart_Custom_Fields_Meta( $object );
@@ -230,7 +230,7 @@ class Smart_Custom_Fields_Controller_Base {
230
  return SCF::get_default_value( $Field, true );
231
  }
232
 
233
- if ( $Meta->is_saved() ) {
234
  $value = $Meta->get( $field_name );
235
  if ( isset( $value[$index] ) ) {
236
  return $value[$index];
176
  * @param WP_Post|WP_User|WP_Term|stdClass $object
177
  * @param Smart_Custom_Fields_Field_Base $Field
178
  * @param int $index
179
+ * @return array
180
  */
181
  public function get_multiple_data_field_value( $object, $Field, $index ) {
182
  $Meta = new Smart_Custom_Fields_Meta( $object );
186
  return SCF::get_default_value( $Field );
187
  }
188
 
189
+ if ( !$Meta->is_saved_the_key( $field_name ) ) {
190
  return SCF::get_default_value( $Field );
191
  }
192
 
220
  * @param WP_Post|WP_User|WP_Term|stdClass $object
221
  * @param Smart_Custom_Fields_Field_Base $Field
222
  * @param int $index
223
+ * @return string
224
  */
225
  public function get_single_data_field_value( $object, $Field, $index ) {
226
  $Meta = new Smart_Custom_Fields_Meta( $object );
230
  return SCF::get_default_value( $Field, true );
231
  }
232
 
233
+ if ( $Meta->is_saved_the_key( $field_name ) ) {
234
  $value = $Meta->get( $field_name );
235
  if ( isset( $value[$index] ) ) {
236
  return $value[$index];
classes/models/class.cache.php CHANGED
@@ -35,7 +35,7 @@ class Smart_Custom_Fields_Cache {
35
  * Using post_type as key.
36
  * @var array
37
  */
38
- public $settings = array();
39
 
40
  /**
41
  * Getting data proccesses is heavy. So saved getted data to $repeat_multiple_data.
@@ -151,8 +151,8 @@ class Smart_Custom_Fields_Cache {
151
  * Saving the Setting object to cache
152
  *
153
  * @param int $settings_post_id
154
- * @param WP_Post|WP_User|WP_Term|stdClass $object
155
  * @param Smart_Custom_Fields_Setting $Setting
 
156
  */
157
  public function save_settings( $settings_post_id, $Setting, $object = null ) {
158
  if ( !is_null( $object ) ) {
@@ -171,7 +171,7 @@ class Smart_Custom_Fields_Cache {
171
  * Getting the Setting object cache
172
  * If there isn't the custom field settings ... null
173
  * If there is custom field settings
174
- * If there is no data for the specified $ meta_type + $id
175
  * There is a thing of the General ... Smart_Custom_Fields_Setting
176
  * There isn't a thing of the General ... false
177
  * If there the data for the specified $meta_type + $id ... Smart_Custom_Fields_Setting
35
  * Using post_type as key.
36
  * @var array
37
  */
38
+ protected $settings = array();
39
 
40
  /**
41
  * Getting data proccesses is heavy. So saved getted data to $repeat_multiple_data.
151
  * Saving the Setting object to cache
152
  *
153
  * @param int $settings_post_id
 
154
  * @param Smart_Custom_Fields_Setting $Setting
155
+ * @param WP_Post|WP_User|WP_Term|stdClass $object
156
  */
157
  public function save_settings( $settings_post_id, $Setting, $object = null ) {
158
  if ( !is_null( $object ) ) {
171
  * Getting the Setting object cache
172
  * If there isn't the custom field settings ... null
173
  * If there is custom field settings
174
+ * If there is no data for the specified $meta_type + $id
175
  * There is a thing of the General ... Smart_Custom_Fields_Setting
176
  * There isn't a thing of the General ... false
177
  * If there the data for the specified $meta_type + $id ... Smart_Custom_Fields_Setting
classes/models/class.meta.php CHANGED
@@ -1,10 +1,10 @@
1
  <?php
2
  /**
3
  * Smart_Custom_Fields_Meta
4
- * Version : 1.3.0
5
  * Author : inc2734
6
  * Created : March 17, 2015
7
- * Modified : May 31, 2015
8
  * License : GPLv2 or later
9
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  */
@@ -81,16 +81,16 @@ class Smart_Custom_Fields_Meta {
81
  }
82
 
83
  /**
84
- * Getting object ID
85
  *
86
- * @return int
87
  */
88
  public function get_id() {
89
  return $this->id;
90
  }
91
 
92
  /**
93
- * Getting type ( Post type or Role or Taxonomy )
94
  *
95
  * @param bool $accept_revision If post type, whether allow revision post type
96
  * @return string
@@ -124,13 +124,13 @@ class Smart_Custom_Fields_Meta {
124
  /**
125
  * Object with this meta data is whether saved
126
  * Post ... If auto-draft, not saved (new posts in)
127
- * Profile or Taxonomy ... Since not display only after saving.
128
- * So if all of meta data is empty,
129
- * It is determined that not saved
130
  *
131
  * @return bool
132
  */
133
- public function is_saved() {
134
  if ( $this->meta_type === 'post' && get_post_status( $this->get_id() ) === 'auto-draft' ) {
135
  return false;
136
  }
@@ -141,78 +141,117 @@ class Smart_Custom_Fields_Meta {
141
  }
142
 
143
  /**
144
- * Getting the meta data
145
  *
146
  * @param string $key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  * @param bool $single false ... return array, true ... return string
148
  * @return string|array
149
  */
150
  public function get( $key = '', $single = false ) {
151
- // under WP 4.4 compatibility
152
- $maybe_4_3_term_meta = false;
153
- if ( $this->meta_type === 'term' ) {
154
- $meta = get_metadata( $this->meta_type, $this->id );
155
- if ( !$meta ) {
156
- $maybe_4_3_term_meta = true;
157
- }
158
  }
159
 
160
- if ( _get_meta_table( $this->meta_type ) && !$maybe_4_3_term_meta ) {
161
- $meta = get_metadata( $this->meta_type, $this->id, $key, $single );
 
162
 
163
- if ( $key === SCF_Config::PREFIX . 'repeat-multiple-data' ) {
164
- return $meta;
 
 
 
 
 
165
  }
166
-
167
- $settings = SCF::get_settings( $this->object );
168
- if ( $key ) {
169
  foreach ( $settings as $Setting ) {
170
  $fields = $Setting->get_fields();
171
- if ( !empty( $fields[$key] ) ) {
172
- return $meta;
173
- }
174
- }
175
- } else {
176
- if ( is_array( $meta ) ) {
177
- foreach ( $settings as $Setting ) {
178
- $fields = $Setting->get_fields();
179
- foreach ( $meta as $meta_key => $meta_value ) {
180
- if ( isset( $fields[$meta_key] ) ) {
181
- $metas[$meta_key] = $meta[$meta_key];
182
- }
183
  }
184
  }
185
  }
186
  }
187
- if ( isset( $metas ) ) {
188
- return $metas;
189
- }
190
- if ( $single ) {
191
- return '';
192
- }
193
- return array();
194
- } else {
195
- $option = get_option( $this->get_option_name() );
196
- if ( $key !=='' && isset( $option[$key] ) ) {
197
- if ( $single && is_array( $option[$key] ) ) {
198
- if ( isset( $option[$key][0] ) ) {
199
- return $option[$key][0];
200
- }
201
- } else {
202
- return $option[$key];
203
- }
204
- }
205
 
206
- if ( $key === '' && $option !== false ) {
207
- return $option;
208
- }
 
 
 
 
 
 
 
 
 
 
209
 
210
- // get_metadata() return entry string, so this method also same behavior
211
- if ( $single ) {
212
- return '';
 
 
 
 
213
  }
214
- return array();
215
  }
 
 
 
 
 
216
  }
217
 
218
  /**
@@ -231,32 +270,44 @@ class Smart_Custom_Fields_Meta {
231
  if ( _get_meta_table( $this->meta_type ) ) {
232
  $return = update_metadata( $this->meta_type, $this->id, $key, $value, $prev_value );
233
  } else {
234
- $option_name = $this->get_option_name();
235
- $option = get_option( $option_name );
236
- if ( isset( $option[$key] ) ) {
237
- if ( $prev_value !== '' ) {
238
- foreach( $option[$key] as $option_key => $option_value ) {
239
- if ( $prev_value === $option_value ) {
240
- $option[$key][$option_key] = $value;
241
- break;
242
- }
243
- }
244
- } else {
245
- foreach( $option[$key] as $option_key => $option_value ) {
246
- $option[$key][$option_key] = $value;
247
- }
248
- }
249
- } else {
250
- $option[$key][] = $value;
251
- }
252
- $option = stripslashes_deep( $option );
253
- $return = update_option( $option_name, $option, false );
254
  }
255
  }
256
  do_action( SCF_Config::PREFIX . '-after-save-' . $this->meta_type, $this->id, $key, $value );
257
  return $return;
258
  }
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  /**
261
  * Adding the meta data
262
  *
@@ -273,19 +324,32 @@ class Smart_Custom_Fields_Meta {
273
  if ( _get_meta_table( $this->meta_type ) ) {
274
  $return = add_metadata( $this->meta_type, $this->id, $key, $value, $unique );
275
  } else {
276
- $option_name = $this->get_option_name();
277
- $option = get_option( $option_name );
278
- if ( !$unique || !isset( $option[$key] ) ) {
279
- $option[$key][] = $value;
280
- $option = stripslashes_deep( $option );
281
- $return = update_option( $option_name, $option, false );
282
- }
283
  }
284
  }
285
  do_action( SCF_Config::PREFIX . '-after-save-' . $this->meta_type, $this->id, $key, $value );
286
  return $return;
287
  }
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  /**
290
  * Deleting the meta data
291
  *
@@ -299,27 +363,38 @@ class Smart_Custom_Fields_Meta {
299
  return delete_metadata( $this->meta_type, $this->id, $key, $value );
300
  }
301
  } else {
302
- if ( !$key ) {
303
- return false;
304
  }
 
 
 
305
 
306
- $option_name = $this->get_option_name();
307
- $option = get_option( $option_name );
 
 
 
 
 
 
 
 
308
 
309
- if ( isset( $option[$key] ) && $value === '' ) {
310
- unset( $option[$key] );
311
- return update_option( $option_name, $option );
312
- }
313
 
314
- if ( isset( $option[$key] ) && $value !== '' ) {
315
- foreach ( $option[$key] as $option_key => $option_value ) {
316
- if ( $option_value === $value ) {
317
- unset( $option[$key][$option_key] );
318
- }
319
  }
320
- return update_option( $option_name, $option );
321
  }
 
322
  }
 
323
  }
324
 
325
  /**
@@ -342,24 +417,7 @@ class Smart_Custom_Fields_Meta {
342
  // Retruning empty value when multi-value is empty, it doesn't save
343
  $multiple_data_fields = array();
344
 
345
- switch ( $this->meta_type ) {
346
- case 'post' :
347
- $object = get_post( $this->id );
348
- break;
349
- case 'user' :
350
- $object = get_userdata( $this->id );
351
- break;
352
- case 'term' :
353
- $object = get_term( $this->id, $this->type );
354
- break;
355
- case 'option' :
356
- $object = SCF::generate_option_object( $this->id );
357
- break;
358
- default :
359
- $object = null;
360
- }
361
-
362
- if ( is_null( $object ) ) {
363
  return;
364
  }
365
 
@@ -369,13 +427,16 @@ class Smart_Custom_Fields_Meta {
369
  return;
370
  }
371
 
372
- $settings = SCF::get_settings( $object );
373
  foreach ( $settings as $Setting ) {
374
  $groups = $Setting->get_groups();
375
  foreach ( $groups as $Group ) {
376
  $fields = $Group->get_fields();
377
  foreach ( $fields as $Field ) {
378
  $field_name = $Field->get( 'name' );
 
 
 
379
  $this->delete( $field_name );
380
  if ( $Field->get_attribute( 'allow-multiple-data' ) ) {
381
  $multiple_data_fields[] = $field_name;
@@ -420,19 +481,11 @@ class Smart_Custom_Fields_Meta {
420
  * @param WP_Post $revision
421
  */
422
  public function restore( $revision ) {
423
- switch ( $this->meta_type ) {
424
- case 'post' :
425
- $object = get_post( $this->id );
426
- break;
427
- default :
428
- $object = null;
429
- }
430
-
431
- if ( is_null( $object ) || !is_a( $revision, 'WP_Post' ) ) {
432
  return;
433
  }
434
 
435
- $settings = SCF::get_settings( $object );
436
  foreach ( $settings as $Setting ) {
437
  $fields = $Setting->get_fields();
438
  foreach ( $fields as $Field ) {
@@ -467,7 +520,7 @@ class Smart_Custom_Fields_Meta {
467
  /**
468
  * Getting option name for saved options table
469
  */
470
- protected function get_option_name() {
471
  return sprintf(
472
  '%s%s-%s-%d',
473
  SCF_Config::PREFIX,
1
  <?php
2
  /**
3
  * Smart_Custom_Fields_Meta
4
+ * Version : 2.0.0
5
  * Author : inc2734
6
  * Created : March 17, 2015
7
+ * Modified : July 1, 2016
8
  * License : GPLv2 or later
9
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  */
81
  }
82
 
83
  /**
84
+ * Getting object ID. When option, this is menu slug.
85
  *
86
+ * @return int|string
87
  */
88
  public function get_id() {
89
  return $this->id;
90
  }
91
 
92
  /**
93
+ * Getting type ( Post type or Role or Taxonomy or Menu slug )
94
  *
95
  * @param bool $accept_revision If post type, whether allow revision post type
96
  * @return string
124
  /**
125
  * Object with this meta data is whether saved
126
  * Post ... If auto-draft, not saved (new posts in)
127
+ * Profile or Taxonomy or option ... Since not display only after saving.
128
+ * So if all of meta data is empty,
129
+ * It is determined that not saved
130
  *
131
  * @return bool
132
  */
133
+ public function is_saved( $key = null ) {
134
  if ( $this->meta_type === 'post' && get_post_status( $this->get_id() ) === 'auto-draft' ) {
135
  return false;
136
  }
141
  }
142
 
143
  /**
144
+ * The metadata is wheter saved.
145
  *
146
  * @param string $key
147
+ * @return bool
148
+ */
149
+ public function is_saved_the_key( $key ) {
150
+ if ( $this->meta_type === 'post' && get_post_status( $this->get_id() ) === 'auto-draft' ) {
151
+ return false;
152
+ }
153
+
154
+ if ( _get_meta_table( $this->meta_type ) && !$this->maybe_4_3_term_meta() ) {
155
+ return metadata_exists( $this->meta_type, $this->id, $key );
156
+ }
157
+
158
+ $option = get_option( $this->get_option_name() );
159
+ if ( is_array( $option ) ) {
160
+ return array_key_exists( $key, $option );
161
+ }
162
+ return false;
163
+ }
164
+
165
+ /**
166
+ * Less than WordPress 4.4 compatibility for term meta
167
+ * More than 4.4 are saved in meta. So if that use the meta data.
168
+ *
169
+ * @return bool
170
+ */
171
+ public function maybe_4_3_term_meta() {
172
+ if ( $this->meta_type == 'term' ) {
173
+ if ( !get_metadata( $this->meta_type, $this->id ) && get_option( $this->get_option_name() ) ) {
174
+ return true;
175
+ }
176
+ }
177
+ return false;
178
+ }
179
+
180
+ /**
181
+ * Getting the meta data
182
+ *
183
+ * @param string|null $key
184
  * @param bool $single false ... return array, true ... return string
185
  * @return string|array
186
  */
187
  public function get( $key = '', $single = false ) {
188
+ if ( _get_meta_table( $this->meta_type ) && !$this->maybe_4_3_term_meta() ) {
189
+ $meta = get_metadata( $this->meta_type, $this->id, $key, $single );
190
+ } else {
191
+ $meta = $this->get_option_metadata( $key, $single );
 
 
 
192
  }
193
 
194
+ if ( $key === SCF_Config::PREFIX . 'repeat-multiple-data' ) {
195
+ return $meta;
196
+ }
197
 
198
+ $settings = SCF::get_settings( $this->object );
199
+ if ( $key ) {
200
+ foreach ( $settings as $Setting ) {
201
+ $fields = $Setting->get_fields();
202
+ if ( !empty( $fields[$key] ) ) {
203
+ return $meta;
204
+ }
205
  }
206
+ } else {
207
+ if ( is_array( $meta ) ) {
 
208
  foreach ( $settings as $Setting ) {
209
  $fields = $Setting->get_fields();
210
+ foreach ( $meta as $meta_key => $meta_value ) {
211
+ if ( isset( $fields[$meta_key] ) ) {
212
+ $metas[$meta_key] = $meta[$meta_key];
 
 
 
 
 
 
 
 
 
213
  }
214
  }
215
  }
216
  }
217
+ }
218
+ if ( isset( $metas ) ) {
219
+ return $metas;
220
+ }
221
+ if ( $single ) {
222
+ return '';
223
+ }
224
+ return array();
225
+ }
 
 
 
 
 
 
 
 
 
226
 
227
+ /**
228
+ * Getting option like meta data.
229
+ *
230
+ * @param string|null $key
231
+ * @param bool $single false ... return array, true ... return string
232
+ * @return string|array
233
+ */
234
+ protected function get_option_metadata( $key, $single ) {
235
+ $option = get_option( $this->get_option_name() );
236
+
237
+ if ( !$key ) {
238
+ return $option;
239
+ }
240
 
241
+ if ( isset( $option[$key] ) ) {
242
+ if ( $single && is_array( $option[$key] ) ) {
243
+ if ( isset( $option[$key][0] ) ) {
244
+ return $option[$key][0];
245
+ }
246
+ } else {
247
+ return $option[$key];
248
  }
 
249
  }
250
+
251
+ if ( $single ) {
252
+ return '';
253
+ }
254
+ return array();
255
  }
256
 
257
  /**
270
  if ( _get_meta_table( $this->meta_type ) ) {
271
  $return = update_metadata( $this->meta_type, $this->id, $key, $value, $prev_value );
272
  } else {
273
+ $return = $this->update_option_metadata( $key, $value, $prev_value );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  }
275
  }
276
  do_action( SCF_Config::PREFIX . '-after-save-' . $this->meta_type, $this->id, $key, $value );
277
  return $return;
278
  }
279
 
280
+ /**
281
+ * Updating the option like meta data. If the meta data not exist, adding it.
282
+ *
283
+ * @param string $key
284
+ * @param mixed $value
285
+ * @param mixed $prev_value If specified, it overwrites the only ones of this value
286
+ * @return bool
287
+ */
288
+ protected function update_option_metadata( $key, $value, $prev_value ) {
289
+ $option_name = $this->get_option_name();
290
+ $option = get_option( $option_name );
291
+ if ( isset( $option[$key] ) ) {
292
+ if ( $prev_value !== '' ) {
293
+ foreach( $option[$key] as $option_key => $option_value ) {
294
+ if ( $prev_value === $option_value ) {
295
+ $option[$key][$option_key] = $value;
296
+ break;
297
+ }
298
+ }
299
+ } else {
300
+ foreach( $option[$key] as $option_key => $option_value ) {
301
+ $option[$key][$option_key] = $value;
302
+ }
303
+ }
304
+ } else {
305
+ $option[$key][] = $value;
306
+ }
307
+ $option = stripslashes_deep( $option );
308
+ return update_option( $option_name, $option, false );
309
+ }
310
+
311
  /**
312
  * Adding the meta data
313
  *
324
  if ( _get_meta_table( $this->meta_type ) ) {
325
  $return = add_metadata( $this->meta_type, $this->id, $key, $value, $unique );
326
  } else {
327
+ $return = $this->add_option_metadata( $key, $value, $unique );
 
 
 
 
 
 
328
  }
329
  }
330
  do_action( SCF_Config::PREFIX . '-after-save-' . $this->meta_type, $this->id, $key, $value );
331
  return $return;
332
  }
333
 
334
+ /**
335
+ * Adding the option like meta data
336
+ *
337
+ * @param string $key
338
+ * @param mixed $value
339
+ * @param bool $unique Whether the key to the unique
340
+ * @return bool
341
+ */
342
+ protected function add_option_metadata( $key, $value, $unique ) {
343
+ $option_name = $this->get_option_name();
344
+ $option = get_option( $option_name );
345
+ if ( !$unique || !isset( $option[$key] ) ) {
346
+ $option[$key][] = $value;
347
+ $option = stripslashes_deep( $option );
348
+ $return = update_option( $option_name, $option, false );
349
+ }
350
+ return false;
351
+ }
352
+
353
  /**
354
  * Deleting the meta data
355
  *
363
  return delete_metadata( $this->meta_type, $this->id, $key, $value );
364
  }
365
  } else {
366
+ if ( $key ) {
367
+ return $this->delete_option_metadata( $key, $value );
368
  }
369
+ }
370
+ return false;
371
+ }
372
 
373
+ /**
374
+ * Deleting the option like meta data
375
+ *
376
+ * @param string $key
377
+ * @param mixed $value If specified, it deletes the only ones of this value
378
+ * @return bool
379
+ */
380
+ protected function delete_option_metadata( $key, $value ) {
381
+ $option_name = $this->get_option_name();
382
+ $option = get_option( $option_name );
383
 
384
+ if ( isset( $option[$key] ) && $value === '' ) {
385
+ unset( $option[$key] );
386
+ return update_option( $option_name, $option );
387
+ }
388
 
389
+ if ( isset( $option[$key] ) && $value !== '' ) {
390
+ foreach ( $option[$key] as $option_key => $option_value ) {
391
+ if ( $option_value === $value ) {
392
+ unset( $option[$key][$option_key] );
 
393
  }
 
394
  }
395
+ return update_option( $option_name, $option );
396
  }
397
+ return false;
398
  }
399
 
400
  /**
417
  // Retruning empty value when multi-value is empty, it doesn't save
418
  $multiple_data_fields = array();
419
 
420
+ if ( is_null( $this->object ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
  return;
422
  }
423
 
427
  return;
428
  }
429
 
430
+ $settings = SCF::get_settings( $this->object );
431
  foreach ( $settings as $Setting ) {
432
  $groups = $Setting->get_groups();
433
  foreach ( $groups as $Group ) {
434
  $fields = $Group->get_fields();
435
  foreach ( $fields as $Field ) {
436
  $field_name = $Field->get( 'name' );
437
+ if ( !isset( $POST[SCF_Config::NAME][$field_name] ) ) {
438
+ continue;
439
+ }
440
  $this->delete( $field_name );
441
  if ( $Field->get_attribute( 'allow-multiple-data' ) ) {
442
  $multiple_data_fields[] = $field_name;
481
  * @param WP_Post $revision
482
  */
483
  public function restore( $revision ) {
484
+ if ( $this->meta_type !== 'post' || is_null( $this->object ) || !is_a( $revision, 'WP_Post' ) ) {
 
 
 
 
 
 
 
 
485
  return;
486
  }
487
 
488
+ $settings = SCF::get_settings( $this->object );
489
  foreach ( $settings as $Setting ) {
490
  $fields = $Setting->get_fields();
491
  foreach ( $fields as $Field ) {
520
  /**
521
  * Getting option name for saved options table
522
  */
523
+ public function get_option_name() {
524
  return sprintf(
525
  '%s%s-%s-%d',
526
  SCF_Config::PREFIX,
classes/models/class.revisions.php CHANGED
@@ -1,10 +1,10 @@
1
  <?php
2
  /**
3
  * Smart_Custom_Fields_Revisions
4
- * Version : 1.1.4
5
  * Author : inc2734
6
  * Created : September 23, 2014
7
- * Modified : June 22, 2016
8
  * License : GPLv2 or later
9
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  */
@@ -84,12 +84,12 @@ class Smart_Custom_Fields_Revisions {
84
  * @return mixed $value
85
  */
86
  public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
87
- // $meta_key が SCF で定義されたものでなければスルー
88
  if ( is_null( SCF::get_field( get_post( $post_id ), $meta_key ) ) ) {
89
  return $value;
90
  }
91
 
92
- if ( $preview_id = $this->get_preview_id( $post_id ) && $meta_key !== '_thumbnail_id' ) {
 
93
  if ( $post_id !== $preview_id ) {
94
  $value = get_post_meta( $preview_id, $meta_key, $single );
95
  }
@@ -107,7 +107,7 @@ class Smart_Custom_Fields_Revisions {
107
  global $post;
108
  $preview_id = 0;
109
  if ( isset( $post->ID ) && intval( $post->ID ) === intval( $post_id ) ) {
110
- if ( is_preview() && $preview = wp_get_post_autosave( $post->ID ) ) {
111
  $preview_id = $preview->ID;
112
  }
113
  }
1
  <?php
2
  /**
3
  * Smart_Custom_Fields_Revisions
4
+ * Version : 1.1.5
5
  * Author : inc2734
6
  * Created : September 23, 2014
7
+ * Modified : July 1, 2016
8
  * License : GPLv2 or later
9
  * License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
  */
84
  * @return mixed $value
85
  */
86
  public function get_post_metadata( $value, $post_id, $meta_key, $single ) {
 
87
  if ( is_null( SCF::get_field( get_post( $post_id ), $meta_key ) ) ) {
88
  return $value;
89
  }
90
 
91
+ $preview_id = $this->get_preview_id( $post_id );
92
+ if ( $preview_id && $meta_key !== '_thumbnail_id' ) {
93
  if ( $post_id !== $preview_id ) {
94
  $value = get_post_meta( $preview_id, $meta_key, $single );
95
  }
107
  global $post;
108
  $preview_id = 0;
109
  if ( isset( $post->ID ) && intval( $post->ID ) === intval( $post_id ) ) {
110
+ if ( is_preview() && $preview = wp_get_post_autosave( $post_id ) ) {
111
  $preview_id = $preview->ID;
112
  }
113
  }
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: inc2734, toro_unit, mimosafa, hideokamoto, hisako-isaka, kurudrive
3
  Donate link: http://www.amazon.co.jp/registry/wishlist/39ANKRNSTNW40
4
  Tags: plugin, custom field, custom, field, meta, meta field, repeat, repeatable
5
  Requires at least: 3.9
6
- Tested up to: 4.5.2
7
- Stable tag: 2.1.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -125,6 +125,10 @@ You can send your own language pack to me.
125
 
126
  == Changelog ==
127
 
 
 
 
 
128
  = 2.1.1 =
129
  * Fix revision lines duplication (by [@hanamura](https://github.com/hanamura))
130
  * Fixed a bug that relation felds don't work in options page.
3
  Donate link: http://www.amazon.co.jp/registry/wishlist/39ANKRNSTNW40
4
  Tags: plugin, custom field, custom, field, meta, meta field, repeat, repeatable
5
  Requires at least: 3.9
6
+ Tested up to: 4.5.3
7
+ Stable tag: 2.2.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
125
 
126
  == Changelog ==
127
 
128
+ = 2.2.0 =
129
+ * Refactoring tests.
130
+ * Changed behavior of the default value of new field of the already saved object. Using the default value.
131
+
132
  = 2.1.1 =
133
  * Fix revision lines duplication (by [@hanamura](https://github.com/hanamura))
134
  * Fixed a bug that relation felds don't work in options page.
smart-custom-fields.php CHANGED
@@ -3,11 +3,11 @@
3
  * Plugin name: Smart Custom Fields
4
  * Plugin URI: https://github.com/inc2734/smart-custom-fields/
5
  * Description: Smart Custom Fields is a simple plugin that management custom fields.
6
- * Version: 2.1.1
7
  * Author: inc2734
8
  * Author URI: http://2inc.org
9
  * Created: October 9, 2014
10
- * Modified: June 22, 2016
11
  * Text Domain: smart-custom-fields
12
  * Domain Path: /languages
13
  * License: GPLv2 or later
3
  * Plugin name: Smart Custom Fields
4
  * Plugin URI: https://github.com/inc2734/smart-custom-fields/
5
  * Description: Smart Custom Fields is a simple plugin that management custom fields.
6
+ * Version: 2.2.0
7
  * Author: inc2734
8
  * Author URI: http://2inc.org
9
  * Created: October 9, 2014
10
+ * Modified: July 2, 2016
11
  * Text Domain: smart-custom-fields
12
  * Domain Path: /languages
13
  * License: GPLv2 or later