Exemple #1
0
 /**
  * Save all custom field values meta values for the post, this function assumes that 
  * $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}]
  * contains the value of the field named {FIELD_NAME}
  *
  * @param integer $postId
  * @return void
  */
 function SetMetaValues($postId)
 {
     global $wpdb;
     $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
     $customFieldKeys = $_POST['rc_cwp_meta_keys'];
     if (!empty($customWritePanelId) && !empty($customFieldKeys)) {
         // --- Delete old values
         foreach ($customFieldKeys as $key) {
             if (!empty($key)) {
                 list($customFieldId, $groupCounter, $fieldCounter, $groupId, $rawCustomFieldName) = split("_", $key, 5);
                 $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($rawCustomFieldName))));
                 delete_post_meta($postId, $customFieldName);
             }
         }
         if ($the_post = wp_is_post_revision($postId)) {
             $postId = $the_post;
         }
         $wpdb->query("DELETE FROM " . MF_TABLE_POST_META . " WHERE post_id={$postId}");
         $arr = array();
         foreach ($customFieldKeys as $key => $value) {
             list($customFieldId, $groupCounter, $fieldCounter, $groupId, $rawCustomFieldName) = split("_", $value, 5);
             $arr[$key]->id = $customFieldId;
             $arr[$key]->gc = $groupCounter;
             $arr[$key]->fc = $fieldCounter;
             $arr[$key]->gi = $groupId;
             $arr[$key]->fn = $rawCustomFieldName;
             $arr[$key]->ov = $value;
         }
         // --- Add new meta data
         foreach ($arr as $key) {
             if (!empty($key)) {
                 //order
                 if ($key->gi == 1) {
                     $order = 1;
                 } else {
                     if (!empty($_POST['order_' . $key->gi . '_' . $key->gc])) {
                         $order = $_POST['order_' . $key->gi . '_' . $key->gc];
                     } else {
                         $order = 1;
                     }
                 }
                 $customFieldValue = $_POST[$key->ov];
                 $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($key->fn))));
                 // Prepare field value
                 if (is_array($customFieldValue)) {
                     $finalValue = array();
                     foreach ($customFieldValue as $value) {
                         $value = stripslashes(trim($value));
                         array_push($finalValue, $value);
                     }
                 } else {
                     $finalValue = stripslashes(trim($customFieldValue));
                 }
                 // Add field value meta data
                 add_post_meta($postId, $customFieldName, $finalValue);
                 // make sure meta is added to the post, not a revision
                 if ($the_post = wp_is_post_revision($postId)) {
                     $postId = $the_post;
                 }
                 $fieldMetaID = $wpdb->insert_id;
                 // Add field extended properties
                 $wpdb->query("INSERT INTO " . MF_TABLE_POST_META . " (id, field_name, group_count, field_count, post_id,order_id) " . " VALUES ({$fieldMetaID}, '{$customFieldName}', " . $key->gc . ", " . $key->fc . ", {$postId},{$order})");
             }
         }
     }
 }
Exemple #2
0
 /**
  * Save all custom field values meta values for the post, this function assumes that 
  * $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}]
  * contains the value of the field named {FIELD_NAME}
  *
  * @param unknown_type $postId
  * @return unknown
  */
 function SetMetaValues($postId)
 {
     global $wpdb;
     $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id'];
     $customFieldKeys = $_POST['rc_cwp_meta_keys'];
     if (!empty($customWritePanelId) && !empty($customFieldKeys)) {
         // --- Delete old values
         foreach ($customFieldKeys as $key) {
             if (!empty($key)) {
                 list($customFieldId, $groupCounter, $fieldCounter, $groupId, $rawCustomFieldName) = split("_", $key, 5);
                 $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($rawCustomFieldName))));
                 delete_post_meta($postId, $customFieldName);
             }
         }
         if ($the_post = wp_is_post_revision($postId)) {
             $postId = $the_post;
         }
         $wpdb->query("DELETE FROM " . RC_CWP_TABLE_POST_META . " WHERE post_id={$postId}");
         // --- Make sure all groups/fields duplicates are in sequence,
         //		i.e. there is no gap due to removing items
         $arr = array();
         foreach ($customFieldKeys as $key => $value) {
             list($customFieldId, $groupCounter, $fieldCounter, $groupId, $rawCustomFieldName) = split("_", $value, 5);
             $arr[$key]->id = $customFieldId;
             $arr[$key]->gc = $groupCounter;
             $arr[$key]->fc = $fieldCounter;
             $arr[$key]->gi = $groupId;
             $arr[$key]->fn = $rawCustomFieldName;
             $arr[$key]->ov = $value;
         }
         /**
         			for($i=0;$i<$key;$i++){
         				for($j=0;$j<$key;$j++){
         					if( $arr[$i]->id == $arr[$j]->id )
         					{
         						if( $arr[$i]->gc == $arr[$j]->gc )
         						{
         							if( $arr[$i]->fc < $arr[$j]->fc )
         							{
         								$t = $arr[$i] ;
         								$arr[$i] = $arr[$j] ;
         								$arr[$j] = $t ;
         							}
         						}
         						else if( $arr[$i]->gc < $arr[$j]->gc )
         						{
         							$t = $arr[$i] ;
         							$arr[$i] = $arr[$j] ;
         							$arr[$j] = $t ;
         						}
         					}
         					else if( $arr[$i]->id < $arr[$j]->id )
         					{
         						$t = $arr[$i] ;
         						$arr[$i] = $arr[$j] ;
         						$arr[$j] = $t ;
         					}
         				}
         			}
         			
         			for($i=0;$i<$key;$i++)
         			{
         				if( $arr[$i]->id != $currentFieldID )
         				{
         					$currentFieldID = $arr[$i]->id ;
         					$currentG = $arr[$i]->gc ;
         					$GC = 1 ;
         					$FC = 1 ;
         				}
         				else if( $arr[$i]->gc != $currentG )
         				{
         					$GC ++ ;
         					$FC = 1 ;
         					$currentG = $arr[$i]->gc ;
         				}
         				else $FC ++ ;
         				$arr[$i]->fc = $FC ;
         				$arr[$i]->gc = $GC ;
         			}*/
         // --- Add new meta data
         foreach ($arr as $key) {
             if (!empty($key)) {
                 //order
                 if ($key->gi == 1) {
                     $order = 1;
                 } else {
                     if (!empty($_POST['order_' . $key->gi . '_' . $key->gc])) {
                         $order = $_POST['order_' . $key->gi . '_' . $key->gc];
                     } else {
                         $order = 1;
                     }
                 }
                 $customFieldValue = $_POST[$key->ov];
                 $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($key->fn))));
                 // Prepare field value
                 if (is_array($customFieldValue)) {
                     $finalValue = array();
                     foreach ($customFieldValue as $value) {
                         $value = stripslashes(trim($value));
                         array_push($finalValue, $value);
                         //add_post_meta($postId, $customFieldName, $value);
                     }
                 } else {
                     $finalValue = stripslashes(trim($customFieldValue));
                 }
                 // Add field value meta data
                 add_post_meta($postId, $customFieldName, $finalValue);
                 // make sure meta is added to the post, not a revision
                 if ($the_post = wp_is_post_revision($postId)) {
                     $postId = $the_post;
                 }
                 $fieldMetaID = $wpdb->insert_id;
                 // Add field extended properties
                 $wpdb->query("INSERT INTO " . RC_CWP_TABLE_POST_META . " (id, field_name, group_count, field_count, post_id,order_id) " . " VALUES ({$fieldMetaID}, '{$customFieldName}', " . $key->gc . ", " . $key->fc . ", {$postId},{$order})");
             }
         }
     }
 }