Custom Field Template - Version 2.1.2

Version Description

  • Post preview support.
  • Sort output by the input order.
Download this release

Release Info

Developer Hiroaki Miyashita
Plugin Icon 128x128 Custom Field Template
Version 2.1.2
Comparing to
See all releases

Code changes from version 2.1.1 to 2.1.2

Files changed (2) hide show
  1. custom-field-template.php +66 -25
  2. readme.txt +6 -2
custom-field-template.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Field Template
4
  Plugin URI: http://wpgogo.com/development/custom-field-template.html
5
  Description: This plugin adds the default custom fields on the Write Post/Page.
6
  Author: Hiroaki Miyashita
7
- Version: 2.1.1
8
  Author URI: http://wpgogo.com/
9
  */
10
 
@@ -67,6 +67,8 @@ class custom_field_template {
67
  add_shortcode( 'cft', array(&$this, 'output_custom_field_values') );
68
  add_shortcode( 'cftsearch', array(&$this, 'search_custom_field_values') );
69
  endif;
 
 
70
  }
71
 
72
  function custom_field_template_init() {
@@ -247,9 +249,10 @@ class custom_field_template {
247
  return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id FROM $wpdb->postmeta WHERE post_id = %d ORDER BY meta_key,meta_id", $postid), ARRAY_A );
248
  }
249
 
250
- function get_post_meta($post_id, $key, $single = false) {
251
- if ( !$post_id || !$key )
252
- return '';
 
253
 
254
  $post_id = (int) $post_id;
255
 
@@ -283,14 +286,16 @@ class custom_field_template {
283
  $meta_cache = wp_cache_get($post_id, 'cft_post_meta');
284
  endif;
285
  }
286
-
287
- if ( isset($meta_cache[$key]) ) {
288
- if ( $single ) {
289
  return maybe_unserialize( $meta_cache[$key][0] );
290
- } else {
291
  return array_map('maybe_unserialize', $meta_cache[$key]);
292
- }
293
- }
 
 
294
 
295
  return '';
296
  }
@@ -616,7 +621,7 @@ class custom_field_template {
616
  }
617
 
618
  function add_manage_posts_custom_column($column_name, $post_id) {
619
- $data = get_post_custom($post_id);
620
 
621
  if( is_array($data) && $column_name == 'custom-fields' ) :
622
  $flag = 0;
@@ -2972,16 +2977,34 @@ jQuery("#edButtonPreview").trigger("click"); }' . "\n";*/
2972
 
2973
  if( isset($_REQUEST['custom-field-template-verify-key']) && !wp_verify_nonce($_REQUEST['custom-field-template-verify-key'], 'custom-field-template') )
2974
  return $id;
2975
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2976
  if ( !isset($_REQUEST['custom-field-template-id']) ) :
2977
  if ( isset($options['posts'][$id]) ) unset($options['posts'][$id]);
2978
  update_option('custom_field_template_data', $options);
2979
  return $id;
2980
  endif;
2981
-
2982
- if ($post->post_type == 'revision')
2983
- return;
2984
-
2985
  if ( !empty($_REQUEST['custom-field-template-id']) && is_array($_REQUEST['custom-field-template-id']) ) :
2986
  foreach ( $_REQUEST['custom-field-template-id'] as $cft_id ) :
2987
  $fields = $this->get_custom_fields($cft_id);
@@ -3132,23 +3155,23 @@ jQuery("#edButtonPreview").trigger("click"); }' . "\n";*/
3132
  echo 'get_post_custom';
3133
  print_r(get_post_custom($id));
3134
  exit();*/
3135
-
3136
  foreach( $save_value as $title => $values ) :
3137
  unset($delete);
3138
  if ( count($values) == 1 ) :
3139
- if ( !add_post_meta( $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]), true ) ) :
3140
  if ( count($this->get_post_meta($id, $title, false))>1 ) :
3141
- delete_post_meta($id, $title);
3142
- add_post_meta( $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]) );
3143
  else :
3144
- update_post_meta( $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]) );
3145
  endif;
3146
  endif;
3147
  elseif ( count($values) > 1 ) :
3148
  $tmp = $this->get_post_meta( $id, $title, false );
3149
- if ( $tmp ) delete_post_meta($id, $title);
3150
  foreach($values as $val)
3151
- add_post_meta( $id, $title, apply_filters('cft_'.rawurlencode($title), $val) );
3152
  endif;
3153
  endforeach;
3154
 
@@ -3176,7 +3199,7 @@ jQuery("#edButtonPreview").trigger("click"); }' . "\n";*/
3176
 
3177
  endforeach;
3178
  endif;
3179
-
3180
  update_option('custom_field_template_data', $options);
3181
  wp_cache_flush();
3182
 
@@ -3416,7 +3439,7 @@ jQuery("#edButtonPreview").trigger("click"); }' . "\n";*/
3416
  endif;
3417
 
3418
  if ( is_numeric($format) && $output = $options['shortcode_format'][$format] ) :
3419
- $data = get_post_custom($post_id);
3420
  $output = stripcslashes($output);
3421
 
3422
  if( $data == null)
@@ -3971,6 +3994,24 @@ jQuery("#edButtonPreview").trigger("click"); }' . "\n";*/
3971
  return ( $limit ? "LIMIT $offset, $limit" : '' );
3972
  }
3973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3974
  function EvalBuffer($string) {
3975
  ob_start();
3976
  eval('?>'.$string);
4
  Plugin URI: http://wpgogo.com/development/custom-field-template.html
5
  Description: This plugin adds the default custom fields on the Write Post/Page.
6
  Author: Hiroaki Miyashita
7
+ Version: 2.1.2
8
  Author URI: http://wpgogo.com/
9
  */
10
 
67
  add_shortcode( 'cft', array(&$this, 'output_custom_field_values') );
68
  add_shortcode( 'cftsearch', array(&$this, 'search_custom_field_values') );
69
  endif;
70
+
71
+ add_filter( 'get_post_metadata', array(&$this, 'get_preview_postmeta'), 10, 4 );
72
  }
73
 
74
  function custom_field_template_init() {
249
  return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id FROM $wpdb->postmeta WHERE post_id = %d ORDER BY meta_key,meta_id", $postid), ARRAY_A );
250
  }
251
 
252
+ function get_post_meta($post_id, $key = '', $single = false) {
253
+ if ( !$post_id ) return '';
254
+
255
+ if ( $preview_id = $this->get_preview_id( $post_id ) ) $post_id = $preview_id;
256
 
257
  $post_id = (int) $post_id;
258
 
286
  $meta_cache = wp_cache_get($post_id, 'cft_post_meta');
287
  endif;
288
  }
289
+
290
+ if ( $key ) :
291
+ if ( $single ) :
292
  return maybe_unserialize( $meta_cache[$key][0] );
293
+ else :
294
  return array_map('maybe_unserialize', $meta_cache[$key]);
295
+ endif;
296
+ else :
297
+ return array_map('maybe_unserialize', $meta_cache);
298
+ endif;
299
 
300
  return '';
301
  }
621
  }
622
 
623
  function add_manage_posts_custom_column($column_name, $post_id) {
624
+ $data = $this->get_post_meta($post_id);
625
 
626
  if( is_array($data) && $column_name == 'custom-fields' ) :
627
  $flag = 0;
2977
 
2978
  if( isset($_REQUEST['custom-field-template-verify-key']) && !wp_verify_nonce($_REQUEST['custom-field-template-verify-key'], 'custom-field-template') )
2979
  return $id;
2980
+ if ( !empty($_POST['wp-preview']) && $id != $post->ID ) :
2981
+ $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $id ) );
2982
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE post_id IN (" . implode( ',', $revision_ids ) . ")" );
2983
+
2984
+ wp_cache_flush();
2985
+ $original_data = $this->get_post_meta($id);
2986
+
2987
+ if ( !empty($original_data) && is_array($original_data) ) :
2988
+ foreach ( $original_data as $key => $val ) :
2989
+ if ( is_array($val) ) :
2990
+ foreach ( $val as $val2 ) :
2991
+ add_metadata( 'post', $post->ID, $key, $val2 );
2992
+ endforeach;
2993
+ else :
2994
+ add_metadata( 'post', $post->ID, $key, $val );
2995
+ endif;
2996
+ endforeach;
2997
+ endif;
2998
+
2999
+ $id = $post->ID;
3000
+ endif;
3001
+
3002
  if ( !isset($_REQUEST['custom-field-template-id']) ) :
3003
  if ( isset($options['posts'][$id]) ) unset($options['posts'][$id]);
3004
  update_option('custom_field_template_data', $options);
3005
  return $id;
3006
  endif;
3007
+
 
 
 
3008
  if ( !empty($_REQUEST['custom-field-template-id']) && is_array($_REQUEST['custom-field-template-id']) ) :
3009
  foreach ( $_REQUEST['custom-field-template-id'] as $cft_id ) :
3010
  $fields = $this->get_custom_fields($cft_id);
3155
  echo 'get_post_custom';
3156
  print_r(get_post_custom($id));
3157
  exit();*/
3158
+
3159
  foreach( $save_value as $title => $values ) :
3160
  unset($delete);
3161
  if ( count($values) == 1 ) :
3162
+ if ( !add_metadata( 'post', $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]), true ) ) :
3163
  if ( count($this->get_post_meta($id, $title, false))>1 ) :
3164
+ delete_metadata( 'post', $id, $title );
3165
+ add_metadata( 'post', $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]) );
3166
  else :
3167
+ update_metadata( 'post', $id, $title, apply_filters('cft_'.rawurlencode($title), $values[0]) );
3168
  endif;
3169
  endif;
3170
  elseif ( count($values) > 1 ) :
3171
  $tmp = $this->get_post_meta( $id, $title, false );
3172
+ if ( $tmp ) delete_metadata( 'post', $id, $title );
3173
  foreach($values as $val)
3174
+ add_metadata( 'post', $id, $title, apply_filters('cft_'.rawurlencode($title), $val) );
3175
  endif;
3176
  endforeach;
3177
 
3199
 
3200
  endforeach;
3201
  endif;
3202
+
3203
  update_option('custom_field_template_data', $options);
3204
  wp_cache_flush();
3205
 
3439
  endif;
3440
 
3441
  if ( is_numeric($format) && $output = $options['shortcode_format'][$format] ) :
3442
+ $data = $this->get_post_meta($post_id);
3443
  $output = stripcslashes($output);
3444
 
3445
  if( $data == null)
3994
  return ( $limit ? "LIMIT $offset, $limit" : '' );
3995
  }
3996
 
3997
+ function get_preview_id( $post_id ) {
3998
+ global $post;
3999
+ $preview_id = 0;
4000
+ if ( $post->ID == $post_id && is_preview() && $preview = wp_get_post_autosave( $post->ID ) ) :
4001
+ $preview_id = $preview->ID;
4002
+ endif;
4003
+ return $preview_id;
4004
+ }
4005
+
4006
+ function get_preview_postmeta( $return, $post_id, $meta_key, $single ) {
4007
+ if ( $preview_id = $this->get_preview_id( $post_id ) ) :
4008
+ if ( $post_id != $preview_id ) :
4009
+ $return = get_post_meta( $preview_id, $meta_key, $single );
4010
+ endif;
4011
+ endif;
4012
+ return $return;
4013
+ }
4014
+
4015
  function EvalBuffer($string) {
4016
  ob_start();
4017
  eval('?>'.$string);
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: Hiroaki Miyashita
3
  Donate link: http://wpgogo.com/development/custom-field-template.html
4
  Tags: custom field, custom fields, custom, fields, field, template, meta, custom field template, custom post type
5
  Requires at least: 2.1
6
- Tested up to: 3.5
7
- Stable tag: 2.1.1
8
  License: GPLv2 or later
9
 
10
  The Custom Field Template plugin extends the functionality of custom fields.
@@ -112,6 +112,10 @@ See the default template and modify it.
112
 
113
  == Changelog ==
114
 
 
 
 
 
115
  = 2.1.1 =
116
  * Farsi.
117
  * Bugfix: key output with single quotes.
3
  Donate link: http://wpgogo.com/development/custom-field-template.html
4
  Tags: custom field, custom fields, custom, fields, field, template, meta, custom field template, custom post type
5
  Requires at least: 2.1
6
+ Tested up to: 3.6
7
+ Stable tag: 2.1.2
8
  License: GPLv2 or later
9
 
10
  The Custom Field Template plugin extends the functionality of custom fields.
112
 
113
  == Changelog ==
114
 
115
+ = 2.1.2 =
116
+ * Post preview support.
117
+ * Sort output by the input order.
118
+
119
  = 2.1.1 =
120
  * Farsi.
121
  * Bugfix: key output with single quotes.