| 1 | <?php |
|---|
| 2 | include_once('RC_Format.php'); |
|---|
| 3 | |
|---|
| 4 | class RCCWP_CustomField |
|---|
| 5 | { |
|---|
| 6 | function Create($customWritePanelId, $name, $description, $order = 1, $type, $options = null, $default_value = null, $properties = null) |
|---|
| 7 | { |
|---|
| 8 | global $wpdb; |
|---|
| 9 | |
|---|
| 10 | $sql = sprintf( |
|---|
| 11 | "INSERT INTO " . RC_CWP_TABLE_PANEL_CUSTOM_FIELD . |
|---|
| 12 | " (panel_id, name, description, display_order, type, CSS) values (%d, %s, %s, %d, %d, %s)", |
|---|
| 13 | $customWritePanelId, |
|---|
| 14 | RC_Format::TextToSql($name), |
|---|
| 15 | RC_Format::TextToSql($description), |
|---|
| 16 | $order, |
|---|
| 17 | $type, |
|---|
| 18 | "'".$_POST['custom-field-css']."'" |
|---|
| 19 | ); |
|---|
| 20 | $wpdb->query($sql); |
|---|
| 21 | |
|---|
| 22 | $customFieldId = $wpdb->insert_id; |
|---|
| 23 | |
|---|
| 24 | $field_type = RCCWP_CustomField::GetCustomFieldTypes($type); |
|---|
| 25 | if ($field_type->has_options == "true") |
|---|
| 26 | { |
|---|
| 27 | $options = explode("\n", $options); |
|---|
| 28 | array_walk($options, array(RC_Format, TrimArrayValues)); |
|---|
| 29 | |
|---|
| 30 | $default_value = explode("\n", $default_value); |
|---|
| 31 | array_walk($default_value, array(RC_Format, TrimArrayValues)); |
|---|
| 32 | |
|---|
| 33 | $sql = sprintf( |
|---|
| 34 | "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . |
|---|
| 35 | " (custom_field_id, options, default_option) values (%d, %s, %s)", |
|---|
| 36 | $customFieldId, |
|---|
| 37 | RC_Format::TextToSql(serialize($options)), |
|---|
| 38 | RC_Format::TextToSql(serialize($default_value)) |
|---|
| 39 | ); |
|---|
| 40 | $wpdb->query($sql); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | if ($field_type->has_properties == "true") |
|---|
| 44 | { |
|---|
| 45 | $sql = sprintf( |
|---|
| 46 | "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . |
|---|
| 47 | " (custom_field_id, properties) values (%d, %s)", |
|---|
| 48 | $customFieldId, |
|---|
| 49 | RC_Format::TextToSql(serialize($properties)) |
|---|
| 50 | ); |
|---|
| 51 | $wpdb->query($sql); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | function Delete($customFieldId = null) |
|---|
| 56 | { |
|---|
| 57 | global $wpdb; |
|---|
| 58 | |
|---|
| 59 | $customField = RCCWP_CustomField::Get($customFieldId); |
|---|
| 60 | |
|---|
| 61 | $sql = sprintf( |
|---|
| 62 | "DELETE FROM " . RC_CWP_TABLE_PANEL_CUSTOM_FIELD . |
|---|
| 63 | " WHERE id = %d", |
|---|
| 64 | $customFieldId |
|---|
| 65 | ); |
|---|
| 66 | $wpdb->query($sql); |
|---|
| 67 | |
|---|
| 68 | if ($customField->has_options == "true") |
|---|
| 69 | { |
|---|
| 70 | $sql = sprintf( |
|---|
| 71 | "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . |
|---|
| 72 | " WHERE custom_field_id = %d", |
|---|
| 73 | $customFieldId |
|---|
| 74 | ); |
|---|
| 75 | $wpdb->query($sql); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | function Get($customFieldId) |
|---|
| 80 | { |
|---|
| 81 | global $wpdb; |
|---|
| 82 | $sql = "SELECT cf.id, cf.name, cf.CSS, tt.name AS type, cf.description, cf.display_order, co.options, co.default_option AS default_value, tt.has_options, cp.properties, tt.has_properties, tt.allow_multiple_values FROM " . RC_CWP_TABLE_PANEL_CUSTOM_FIELD . |
|---|
| 83 | " cf LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" . |
|---|
| 84 | " LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" . |
|---|
| 85 | " JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" . |
|---|
| 86 | " WHERE cf.id = " . $customFieldId; |
|---|
| 87 | $results = $wpdb->get_row($sql); |
|---|
| 88 | |
|---|
| 89 | $results->options = unserialize($results->options); |
|---|
| 90 | $results->properties = unserialize($results->properties); |
|---|
| 91 | $results->default_value = unserialize($results->default_value); |
|---|
| 92 | return $results; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | function GetCustomFieldNames($customFieldTypeId = null) |
|---|
| 96 | { |
|---|
| 97 | $customFieldNames = array(); |
|---|
| 98 | $customFields = RCCWP_CustomField::GetCustomFieldTypes($customFieldTypeId); |
|---|
| 99 | foreach ($customFields as $field) |
|---|
| 100 | { |
|---|
| 101 | $customFieldNames[] = $field->name; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | return $customFieldNames; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | function GetCustomFieldTypes($customFieldTypeId = null) |
|---|
| 108 | { |
|---|
| 109 | global $wpdb; |
|---|
| 110 | |
|---|
| 111 | if (isset($customFieldTypeId)) |
|---|
| 112 | { |
|---|
| 113 | $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . |
|---|
| 114 | " WHERE id = " . (int)$customFieldTypeId; |
|---|
| 115 | $results = $wpdb->get_row($sql); |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | $sql = "SELECT id, name, description, has_options, has_properties, allow_multiple_values FROM " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES; |
|---|
| 120 | $results = $wpdb->get_results($sql); |
|---|
| 121 | if (!isset($results)) |
|---|
| 122 | $results = array(); |
|---|
| 123 | } |
|---|
| 124 | return $results; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | function GetCustomFieldValue($postId, $customFieldName) |
|---|
| 128 | { |
|---|
| 129 | return get_post_meta($postId, $customFieldName, true); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | function GetCustomFieldValues($postId, $customFieldName) |
|---|
| 133 | { |
|---|
| 134 | return get_post_meta($postId, $customFieldName, false); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | function GetDefaultCustomFieldType() |
|---|
| 138 | { |
|---|
| 139 | return 'Textbox'; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | function GetOptions() |
|---|
| 143 | { |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | function GetProperties() |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | function Update($customFieldId, $name, $description, $order = 1, $type, $options = null, $default_value = null, $properties = null) |
|---|
| 153 | { |
|---|
| 154 | global $wpdb; |
|---|
| 155 | |
|---|
| 156 | $oldCustomField = RCCWP_CustomField::Get($customFieldId); |
|---|
| 157 | |
|---|
| 158 | if ($oldCustomField->name != $name) |
|---|
| 159 | { |
|---|
| 160 | $sql = sprintf( |
|---|
| 161 | "UPDATE $wpdb->postmeta" . |
|---|
| 162 | " SET meta_key = %s" . |
|---|
| 163 | " WHERE meta_key = %s", |
|---|
| 164 | RC_Format::TextToSql($name), |
|---|
| 165 | RC_Format::TextToSql($oldCustomField->name) |
|---|
| 166 | ); |
|---|
| 167 | |
|---|
| 168 | $wpdb->query($sql); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | $sql = sprintf( |
|---|
| 172 | "UPDATE " . RC_CWP_TABLE_PANEL_CUSTOM_FIELD . |
|---|
| 173 | " SET name = %s" . |
|---|
| 174 | " , description = %s" . |
|---|
| 175 | " , display_order = %d" . |
|---|
| 176 | " , type = %d" . |
|---|
| 177 | " , CSS = '%s'" . |
|---|
| 178 | " WHERE id = %d", |
|---|
| 179 | RC_Format::TextToSql($name), |
|---|
| 180 | RC_Format::TextToSql($description), |
|---|
| 181 | $order, |
|---|
| 182 | $type, |
|---|
| 183 | $_POST['custom-field-css'], |
|---|
| 184 | $customFieldId |
|---|
| 185 | ); |
|---|
| 186 | $wpdb->query($sql); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | $field_type = RCCWP_CustomField::GetCustomFieldTypes($type); |
|---|
| 190 | if ($field_type->has_options == "true") |
|---|
| 191 | { |
|---|
| 192 | $options = explode("\n", $options); |
|---|
| 193 | array_walk($options, array(RC_Format, TrimArrayValues)); |
|---|
| 194 | |
|---|
| 195 | $default_value = explode("\n", $default_value); |
|---|
| 196 | array_walk($default_value, array(RC_Format, TrimArrayValues)); |
|---|
| 197 | |
|---|
| 198 | $sql = sprintf( |
|---|
| 199 | "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . |
|---|
| 200 | " (custom_field_id, options, default_option) values (%d, %s, %s)" . |
|---|
| 201 | " ON DUPLICATE KEY UPDATE options = %s, default_option = %s", |
|---|
| 202 | $customFieldId, |
|---|
| 203 | RC_Format::TextToSql(serialize($options)), |
|---|
| 204 | RC_Format::TextToSql(serialize($default_value)), |
|---|
| 205 | RC_Format::TextToSql(serialize($options)), |
|---|
| 206 | RC_Format::TextToSql(serialize($default_value)) |
|---|
| 207 | ); |
|---|
| 208 | $wpdb->query($sql); |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | $sql = sprintf( |
|---|
| 213 | "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . |
|---|
| 214 | " WHERE custom_field_id = %d", |
|---|
| 215 | $customFieldId |
|---|
| 216 | ); |
|---|
| 217 | $wpdb->query($sql); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | if ($field_type->has_properties == "true") |
|---|
| 221 | { |
|---|
| 222 | $sql = sprintf( |
|---|
| 223 | "INSERT INTO " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . |
|---|
| 224 | " (custom_field_id, properties) values (%d, %s)" . |
|---|
| 225 | " ON DUPLICATE KEY UPDATE properties = %s", |
|---|
| 226 | $customFieldId, |
|---|
| 227 | RC_Format::TextToSql(serialize($properties)), |
|---|
| 228 | RC_Format::TextToSql(serialize($properties)) |
|---|
| 229 | ); |
|---|
| 230 | $wpdb->query($sql); |
|---|
| 231 | } |
|---|
| 232 | else |
|---|
| 233 | { |
|---|
| 234 | $sql = sprintf( |
|---|
| 235 | "DELETE FROM " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . |
|---|
| 236 | " WHERE custom_field_id = %d", |
|---|
| 237 | $customFieldId |
|---|
| 238 | ); |
|---|
| 239 | $wpdb->query($sql); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | ?> |
|---|