/** * 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})"); } } } }
/** * Updates the basic information of a module * * @param integer $customWriteModuleId the id of the module that will be updated * @param string $name new name * @param string $description new description * @return the id of the module or -1 if the module name already exist */ function Update($customWriteModuleId, $name, $description) { require_once 'RC_Format.php'; global $wpdb; //$capabilityName = RCCWP_CustomWriteModule::GetCapabilityName($name); $special_chars = array(' ', '`', '"', '\'', '\\', '/', " ", "#", "\$", "%", "^", "&", "*", "!", "~", "‘", "\"", "’", "'", "=", "?", "/", "[", "]", "(", ")", "|", "<", ">", ";", "\\", ","); $name = str_replace($special_chars, '', $name); //Make sure the module doesn't already exist $sql = "SELECT * FROM " . RC_CWP_TABLE_MODULES . " WHERE name = '" . $name . "' AND id <> {$customWriteModuleId}"; if ($wpdb->get_row($sql)) { return -1; } //Get old name $sql = "SELECT name FROM " . RC_CWP_TABLE_MODULES . " WHERE id = {$customWriteModuleId}"; $originalModName = $wpdb->get_var($sql); $oldModuleTemplateFolder = FLUTTER_MODULES_DIR . $originalModName; // Update name $sql = sprintf("UPDATE " . RC_CWP_TABLE_MODULES . " SET name = %s," . " description = %s" . " where id = %d", RC_Format::TextToSql($name), RC_Format::TextToSql($description), $customWriteModuleId); $wpdb->query($sql); //Rename module folder $newModuleTemplateFolder = FLUTTER_MODULES_DIR . $name; rename($oldModuleTemplateFolder, $newModuleTemplateFolder); return $customWriteModuleId; }
/** * Update the group * * @param unknown_type $customWritePanelId * @param unknown_type $name group name * @param unknown_type $duplicate a boolean indicating whether the group can be duplicated * @param unknown_type $at_right a boolean indicating whether the group should be placed at right side. */ function Update($customGroupId, $name, $duplicate, $at_right) { require_once 'RC_Format.php'; global $wpdb; //$capabilityName = RCCWP_CustomWriteModule::GetCapabilityName($name); $sql = sprintf("UPDATE " . RC_CWP_TABLE_PANEL_GROUPS . " SET name = %s , duplicate = %d, at_right = %d" . " where id = %d", RC_Format::TextToSql($name), $duplicate, $at_right, $customGroupId); $wpdb->query($sql); }
/** * Update the group * * @param unknown_type $customWritePanelId * @param unknown_type $name group name * @param unknown_type $duplicate a boolean indicating whether the group can be duplicated * @param unknown_type $at_right a boolean indicating whether the group should be placed at right side. */ function Update($customGroupId, $name, $duplicate, $expanded, $at_right) { require_once 'RC_Format.php'; global $wpdb; $sql = sprintf("UPDATE " . MF_TABLE_PANEL_GROUPS . " SET name = %s , duplicate = %d, expanded = %d, at_right = %d" . " where id = %d", RC_Format::TextToSql($name), $duplicate, $expanded, $at_right, $customGroupId); $wpdb->query($sql); }
/** * Updates the properties of a custom field. * * @param integer $customFieldId the id of the field to be updated * @param string $name the name of the field, the name is used to uniquely identify the field * when retrieving its value. * @param string $label the label of the field, the label is displayed beside the field * in Write tab. * @param integer $order the order of the field when it is displayed in * the Write tab. * @param integer $required_field whether this field is a required field. Required fields * doesn't allow users to save a post if they are null. * @param integer $type the type of the field. Use $FIELD_TYPES defined in RCCWP_Constant.php * @param array $options array of strings that represent the list of the field if * its type is list. * @param array $default_value array of strings that represent default value(s) of * of the field if its type is list. * @param array $properties an array containing extra properties of the field. */ function Update($customFieldId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null, $duplicate) { global $wpdb; $oldCustomField = RCCWP_CustomField::Get($customFieldId); if ($oldCustomField->name != $name) { $sql = sprintf("UPDATE {$wpdb->postmeta}" . " SET meta_key = %s" . " WHERE meta_key = %s", RC_Format::TextToSql($name), RC_Format::TextToSql($oldCustomField->name)); $wpdb->query($sql); } $sql = sprintf("UPDATE " . RC_CWP_TABLE_GROUP_FIELDS . " SET name = %s" . " , description = %s" . " , display_order = %d" . " , required_field = %d" . " , type = %d" . " , CSS = '%s'" . " , duplicate = %d" . " WHERE id = %d", RC_Format::TextToSql($name), RC_Format::TextToSql($label), $order, $required_field, $type, $_POST['custom-field-css'], $duplicate, $customFieldId); $wpdb->query($sql); $field_type = RCCWP_CustomField::GetCustomFieldTypes($type); if ($field_type->has_options == "true") { if (!is_array($options)) { $options = stripslashes($options); $options = explode("\n", $options); } array_walk($options, array(RC_Format, TrimArrayValues)); $options = addslashes(serialize($options)); if (!is_array($default_value)) { $default_value = stripslashes($default_value); $default_value = explode("\n", $default_value); } array_walk($default_value, array(RC_Format, TrimArrayValues)); $default_value = addslashes(serialize($default_value)); $sql = sprintf("INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " (custom_field_id, options, default_option) values (%d, %s, %s)" . " ON DUPLICATE KEY UPDATE options = %s, default_option = %s", $customFieldId, RC_Format::TextToSql($options), RC_Format::TextToSql($default_value), RC_Format::TextToSql($options), RC_Format::TextToSql($default_value)); $wpdb->query($sql); } else { $sql = sprintf("DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " WHERE custom_field_id = %d", $customFieldId); $wpdb->query($sql); } if ($field_type->has_properties == "true") { $sql = sprintf("INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " (custom_field_id, properties) values (%d, %s)" . " ON DUPLICATE KEY UPDATE properties = %s", $customFieldId, RC_Format::TextToSql(serialize($properties)), RC_Format::TextToSql(serialize($properties))); $wpdb->query($sql); } else { $sql = sprintf("DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " WHERE custom_field_id = %d", $customFieldId); $wpdb->query($sql); } }
/** * Updates the properties of a write panel * * @param integer $customWritePanelId panel id * @param string $name write panel name * @param string $description write panel description * @param array $standardFields a list of standard fields ids that are to be displayed in * in the panel. Use $STANDARD_FIELDS defined in RCCWP_Constant.php * @param array $categories array of category ids that are checked by default when the user * opens Write tab for that panel. * @param integer $display_order the order of the panel in Magic Fields > Write Panels tab * @param string $type 'post' or 'page' */ function Update($customWritePanelId, $name, $description = '', $standardFields = array(), $categories = array(), $display_order = 1, $type = FALSE, $createDefaultGroup = true, $single_post = 0, $default_theme_page = NULL, $default_parent_page = NULL) { include_once 'RC_Format.php'; global $wpdb; $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name); $sql = sprintf("UPDATE " . MF_TABLE_PANELS . " SET name = %s" . " , description = %s" . " , display_order = %d" . " , capability_name = %s" . " , type = %s" . " , single = %s" . " where id = %d", RC_Format::TextToSql($name), RC_Format::TextToSql($description), $display_order, RC_Format::TextToSql($capabilityName), RC_Format::TextToSql($_POST['radPostPage']), $single_post, $customWritePanelId); $wpdb->query($sql); if (!isset($categories) || empty($categories)) { $sql = sprintf("DELETE FROM " . MF_TABLE_PANEL_CATEGORY . " WHERE panel_id = %d", $customWritePanelId); $wpdb->query($sql); } else { $currentCategoryIds = array(); $currentCategoryIds = RCCWP_CustomWritePanel::GetAssignedCategoryIds($customWritePanelId); $keepCategoryIds = array_intersect($currentCategoryIds, $categories); $deleteCategoryIds = array_diff($currentCategoryIds, $keepCategoryIds); $insertCategoryIds = array_diff($categories, $keepCategoryIds); foreach ($insertCategoryIds as $cat_id) { $sql = sprintf("INSERT INTO " . MF_TABLE_PANEL_CATEGORY . " (panel_id, cat_id)" . " values (%d, %d)", $customWritePanelId, $cat_id); $wpdb->query($sql); } if (!empty($deleteCategoryIds)) { $sql = sprintf("DELETE FROM " . MF_TABLE_PANEL_CATEGORY . " WHERE panel_id = %d" . " AND cat_id IN (%s)", $customWritePanelId, implode(',', $deleteCategoryIds)); $wpdb->query($sql); } } if (!isset($standardFields) || empty($standardFields)) { $sql = sprintf("DELETE FROM " . MF_TABLE_PANEL_STANDARD_FIELD . " WHERE panel_id = %d", $customWritePanelId); $wpdb->query($sql); } else { $currentStandardFieldIds = array(); $currentStandardFieldIds = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId); $keepStandardFieldIds = array_intersect($currentStandardFieldIds, $standardFields); $deleteStandardFieldIds = array_diff($currentStandardFieldIds, $keepStandardFieldIds); $insertStandardFieldIds = array_diff($standardFields, $keepStandardFieldIds); foreach ($insertStandardFieldIds as $standard_field_id) { $sql = sprintf("INSERT INTO " . MF_TABLE_PANEL_STANDARD_FIELD . " (panel_id, standard_field_id)" . " values (%d, %d)", $customWritePanelId, $standard_field_id); $wpdb->query($sql); } if (!empty($deleteStandardFieldIds)) { $sql = sprintf("DELETE FROM " . MF_TABLE_PANEL_STANDARD_FIELD . " WHERE panel_id = %d" . " AND standard_field_id IN (%s)", $customWritePanelId, implode(',', $deleteStandardFieldIds)); $wpdb->query($sql); } } if ($default_theme_page) { $theme_key = "t_" . $name; //check if exist template in postmeta $check_template = "SELECT meta_id FROM " . $wpdb->postmeta . " WHERE meta_key='" . $theme_key . "' "; $query_template = $wpdb->query($check_template); if ($query_template) { $sql = "UPDATE " . $wpdb->postmeta . " SET meta_value = '" . $default_theme_page . "' " . " WHERE meta_key = '" . $theme_key . "' AND post_id = '0' "; } else { $sql = "INSERT INTO " . $wpdb->postmeta . " (meta_key, meta_value) " . " VALUES ('" . $theme_key . "', '" . $default_theme_page . "')"; } $wpdb->query($sql); } if ($default_parent_page && $default_parent_page >= 0) { $parent_key = "p_" . $name; //check if exist parent in postmeta $check_parent = "SELECT meta_id FROM " . $wpdb->postmeta . " WHERE meta_key='" . $parent_key . "' "; $query_parent = $wpdb->query($check_parent); if ($query_parent) { $sql = "UPDATE " . $wpdb->postmeta . " SET meta_value = '" . $default_parent_page . "' " . " WHERE meta_key = '" . $parent_key . "' AND post_id = '0' "; } else { $sql = "INSERT INTO " . $wpdb->postmeta . " (meta_key, meta_value) " . " VALUES ('" . $parent_key . "', '" . $default_parent_page . "')"; } $wpdb->query($sql); } elseif ($default_parent_page == -1) { delete_post_meta(0, "p_" . $name, $value); } }
/** * @todo Add documentation */ function CustomFieldInterface($customFieldId, $groupCounter = 1, $fieldCounter = 1, $customGroup_id = 0) { global $mf_domain; require_once "RC_Format.php"; $customField = RCCWP_CustomField::Get($customFieldId); $customFieldName = RC_Format::GetInputName(attribute_escape($customField->name)); $customFieldTitle = attribute_escape($customField->description); $customFieldHelp = htmlentities2($customField->help_text); $groupId = $customGroup_id; $inputName = $customFieldId . "_" . $groupCounter . "_" . $fieldCounter . "_" . $groupId . "_" . $customFieldName; // Create input tag name if ($fieldCounter > 1 && $customField->duplicate == 0) { return; } if ($fieldCounter > 1) { $titleCounter = " ({$fieldCounter})"; } $field_group = RCCWP_CustomGroup::Get($customField->group_id); ?> <div class="mf-field <?php echo str_replace(" ", "_", $customField->type); ?> " id="row_<?php echo $inputName; ?> "> <label for="<?php echo $inputName; ?> "> <?php if (empty($titleCounter)) { $titleCounter = ""; } ?> <?php echo $customFieldTitle . $titleCounter; ?> <?php if (!empty($customFieldHelp)) { ?> <small class="tip">(what's this?)<span class="field_help"><?php echo $customFieldHelp; ?> </span></small> <?php } ?> </label> <span> <p class="error_msg_txt" id="fieldcellerror_<?php echo $inputName; ?> " style="display:none"></p> <?php switch ($customField->type) { case 'Textbox': RCCWP_WritePostPage::TextboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Multiline Textbox': RCCWP_WritePostPage::MultilineTextboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Checkbox': RCCWP_WritePostPage::CheckboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Checkbox List': RCCWP_WritePostPage::CheckboxListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Radiobutton List': RCCWP_WritePostPage::RadiobuttonListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Dropdown List': RCCWP_WritePostPage::DropdownListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Listbox': RCCWP_WritePostPage::ListboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'File': RCCWP_WritePostPage::FileInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Image': RCCWP_WritePostPage::PhotoInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Date': RCCWP_WritePostPage::DateInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Audio': RCCWP_WritePostPage::AudioInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Color Picker': RCCWP_WritePostPage::ColorPickerInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Slider': RCCWP_WritePostPage::SliderInterface($customField, $inputName, $groupCounter, $fieldCounter); break; //eeble //eeble case 'Related Type': RCCWP_WritePostPage::RelatedTypeInterface($customField, $inputName, $groupCounter, $fieldCounter); break; default: } if ($fieldCounter == 1) { ?> <?php if ($customField->duplicate != 0) { ?> <br /> <a class ="typeHandler" href="javascript:void(0);" id="type_handler-<?php echo $inputName; ?> " > <img class="duplicate_image" src="<?php echo MF_URI; ?> images/duplicate.png" alt="<?php _e('Add field duplicate', $mf_domain); ?> "/> <?php _e('Duplicate', $mf_domain); ?> </a> <?php } ?> <?php } else { ?> <br /> <a class ="delete_duplicate_field" href="javascript:void(0)" id="delete_field_repeat-<?php echo $inputName; ?> "> <img class="duplicate_image" src="<?php echo MF_URI; ?> images/delete.png" alt="<?php _e('Remove field duplicate', $mf_domain); ?> "/> <?php _e('Remove', $mf_domain); ?> </a> <?php } ?> <input type="hidden" name="rc_cwp_meta_keys[]" value="<?php echo $inputName; ?> " /> </span> </div> <?php }
function CustomFieldInterface($customFieldId, $groupCounter = 1, $fieldCounter = 1, $customGroup_id = 0) { global $flutter_domain; require_once "RC_Format.php"; $customField = RCCWP_CustomField::Get($customFieldId); $customFieldName = RC_Format::GetInputName(attribute_escape($customField->name)); $customFieldTitle = attribute_escape($customField->description); $groupId = $customGroup_id; $inputName = $customFieldId . "_" . $groupCounter . "_" . $fieldCounter . "_" . $groupId . "_" . $customFieldName; // Create input tag name if ($fieldCounter > 1 && $customField->duplicate == 0) { return; } if ($fieldCounter > 1) { $titleCounter = " ({$fieldCounter})"; } $field_group = RCCWP_CustomGroup::Get($customField->group_id); ?> <tr class="form-field" id="row_<?php echo $inputName; ?> "> <?php // If the field is at right, put the header over the field if ($field_group->at_right) { ?> <td> <label style="font-weight:bold" for="<?php echo $inputName; ?> "><?php echo $customFieldTitle . $titleCounter; ?> </label> <br /> <?php } else { ?> <th valign="top" scope="row"> <label for="<?php echo $inputName; ?> "><?php echo $customFieldTitle . $titleCounter; ?> </label> </th> <td> <?php } ?> <p class="error_msg_txt" id="fieldcellerror_<?php echo $inputName; ?> " style="display:none"></p> <?php switch ($customField->type) { case 'Textbox': RCCWP_WritePostPage::TextboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Multiline Textbox': RCCWP_WritePostPage::MultilineTextboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Checkbox': RCCWP_WritePostPage::CheckboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Checkbox List': RCCWP_WritePostPage::CheckboxListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Radiobutton List': RCCWP_WritePostPage::RadiobuttonListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Dropdown List': RCCWP_WritePostPage::DropdownListInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Listbox': RCCWP_WritePostPage::ListboxInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'File': RCCWP_WritePostPage::FileInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Image': RCCWP_WritePostPage::PhotoInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Date': RCCWP_WritePostPage::DateInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Audio': RCCWP_WritePostPage::AudioInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Color Picker': RCCWP_WritePostPage::ColorPickerInterface($customField, $inputName, $groupCounter, $fieldCounter); break; case 'Slider': RCCWP_WritePostPage::SliderInterface($customField, $inputName, $groupCounter, $fieldCounter); break; default: } if ($fieldCounter == 1) { ?> <?php if ($customField->duplicate != 0) { ?> <br /> <a class ="typeHandler" href="javascript:void(0);" id="type_handler-<?php echo $inputName; ?> " > <img class="duplicate_image" src="<?php echo FLUTTER_URI; ?> images/duplicate.png" alt="<?php _e('Add field duplicate', $flutter_domain); ?> "/> <?php _e('Duplicate', $flutter_domain); ?> </a> <?php } ?> <?php } else { ?> <br /> <a class ="delete_duplicate_field" href="javascript:void(0)" id="delete_field_repeat-<?php echo $inputName; ?> "> <img class="duplicate_image" src="<?php echo FLUTTER_URI; ?> images/delete.png" alt="<?php _e('Remove field duplicate', $flutter_domain); ?> "/> <?php _e('Remove', $flutter_domain); ?> </a> <?php } ?> <input type="hidden" name="rc_cwp_meta_keys[]" value="<?php echo $inputName; ?> " /> </td> </tr> <?php }
/** * Updates the properties of a custom field. * * @param integer $customFieldId the id of the field to be updated * @param string $name the name of the field, the name is used to uniquely identify the field * when retrieving its value. * @param string $label the label of the field, the label is displayed beside the field * in Write tab. * @param integer $order the order of the field when it is displayed in * the Write tab. * @param integer $required_field whether this field is a required field. Required fields * doesn't allow users to save a post if they are null. * @param integer $type the type of the field. Use $FIELD_TYPES defined in MF_Constant.php * @param array $options array of strings that represent the list of the field if * its type is list. * @param array $default_value array of strings that represent default value(s) of * of the field if its type is list. * @param array $properties an array containing extra properties of the field. */ public static function Update($customFieldId, $name, $label, $order = 1, $required_field = 0, $type, $options = null, $default_value = null, $properties = null, $duplicate, $helptext = null) { global $wpdb; $oldCustomField = RCCWP_CustomField::Get($customFieldId); $name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); $name = str_replace(" ", "_", $name); $label = htmlspecialchars($label, ENT_QUOTES, 'UTF-8'); $helptext = htmlspecialchars($helptext, ENT_QUOTES, 'UTF-8'); if ($oldCustomField->name != $name) { $sql = $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_key = %s WHERE meta_key = %s", array($name, $oldCustomField->name)); $wpdb->query($sql); } $css = NULL; if (isset($_POST['custom-field-css'])) { $css = $_POST['custom-field-css']; } $sql = $wpdb->prepare("UPDATE " . MF_TABLE_GROUP_FIELDS . " SET name = %s" . " , description = %s" . " , display_order = %d" . " , required_field = %d" . " , type = %d" . " , CSS = '%s'" . " , duplicate = %d" . " , help_text = %s" . " WHERE id = %d", array(RC_Format::TextToSqlAlt($name), RC_Format::TextToSqlAlt($label), $order, $required_field, $type, $css, $duplicate, RC_Format::TextToSqlAlt($helptext), $customFieldId)); $wpdb->query($sql); $field_type = RCCWP_CustomField::GetCustomFieldTypes($type); if ($field_type->has_options == "true") { if (!is_array($options)) { $options = stripslashes($options); $options = explode("\n", $options); } array_walk($options, array("RC_Format", "TrimArrayValues")); $options = addslashes(serialize($options)); if (!is_array($default_value)) { $default_value = stripslashes($default_value); $default_value = explode("\n", $default_value); } array_walk($default_value, array("RC_Format", "TrimArrayValues")); $default_value = addslashes(serialize($default_value)); $sql = $wpdb->prepare("INSERT INTO " . MF_TABLE_CUSTOM_FIELD_OPTIONS . " (custom_field_id, options, default_option) values (%d, %s, %s)" . " ON DUPLICATE KEY UPDATE options = %s, default_option = %s", array($customFieldId, RC_Format::TextToSqlAlt($options), RC_Format::TextToSqlAlt($default_value), RC_Format::TextToSqlAlt($options), RC_Format::TextToSqlAlt($default_value))); $wpdb->query($sql); } else { $sql = $wpdb->prepare("DELETE FROM " . MF_TABLE_CUSTOM_FIELD_OPTIONS . " WHERE custom_field_id = %d", array($customFieldId)); $wpdb->query($sql); } if ($field_type->has_properties == "true") { $sql = $wpdb->prepare("INSERT INTO " . MF_TABLE_CUSTOM_FIELD_PROPERTIES . " (custom_field_id, properties) values (%d, %s)" . " ON DUPLICATE KEY UPDATE properties = %s", array($customFieldId, RC_Format::TextToSqlAlt(serialize($properties)), RC_Format::TextToSqlAlt(serialize($properties)))); $wpdb->query($sql); } else { $sql = $wpdb->prepare("DELETE FROM " . MF_TABLE_CUSTOM_FIELD_PROPERTIES . " WHERE custom_field_id = %d", array($customFieldId)); $wpdb->query($sql); } }
/** * 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})"); } } } }