| [21] | 1 | <?php |
|---|
| 2 | class RCCWP_Post |
|---|
| 3 | { |
|---|
| 4 | |
|---|
| 5 | function SaveCustomFields($postId){ |
|---|
| 6 | if(!wp_verify_nonce($_REQUEST['rc-custom-write-panel-verify-key'], 'rc-custom-write-panel')) |
|---|
| 7 | return $postId; |
|---|
| 8 | |
|---|
| 9 | if (!current_user_can('edit_post', $postId)) |
|---|
| 10 | return $postId; |
|---|
| 11 | |
|---|
| 12 | RCCWP_Post::SetCustomWritePanel($postId); |
|---|
| 13 | RCCWP_Post::PrepareFieldsValues($postId); |
|---|
| 14 | RCCWP_Post::SetMetaValues($postId); |
|---|
| 15 | |
|---|
| 16 | return $postId; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | /* |
|---|
| 21 | * Attach a custom write panel to the current post by saving the custom write panel id |
|---|
| 22 | * as a meta value for the post |
|---|
| 23 | */ |
|---|
| 24 | function SetCustomWritePanel($postId) |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id']; |
|---|
| 28 | if (isset($customWritePanelId)) |
|---|
| 29 | { |
|---|
| 30 | if (!empty($customWritePanelId)) |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | if (!update_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId)) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | add_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY, $customWritePanelId); |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | else |
|---|
| 40 | { |
|---|
| 41 | delete_post_meta($postId, RC_CWP_POST_WRITE_PANEL_ID_META_KEY); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Save all custom field values meta values for the post, this function assumes that |
|---|
| 48 | * $_POST['rc_cwp_meta_keys'] contains the names of the fields, while $_POST[{FIELD_NAME}] |
|---|
| 49 | * contains the value of the field named {FIELD_NAME} |
|---|
| 50 | * |
|---|
| 51 | * @param unknown_type $postId |
|---|
| 52 | * @return unknown |
|---|
| 53 | */ |
|---|
| 54 | function SetMetaValues($postId) |
|---|
| 55 | { |
|---|
| 56 | global $wpdb; |
|---|
| 57 | |
|---|
| 58 | $customWritePanelId = $_POST['rc-cwp-custom-write-panel-id']; |
|---|
| 59 | $customFieldKeys = $_POST['rc_cwp_meta_keys']; |
|---|
| 60 | |
|---|
| 61 | if (!empty($customWritePanelId) && !empty($customFieldKeys) ) |
|---|
| 62 | { |
|---|
| 63 | |
|---|
| 64 | // --- Delete old values |
|---|
| 65 | foreach ($customFieldKeys as $key) |
|---|
| 66 | { |
|---|
| 67 | if (!empty($key)) |
|---|
| 68 | { |
|---|
| 69 | list($customFieldId, $groupCounter, $fieldCounter, $rawCustomFieldName) = split("_", $key, 4); |
|---|
| 70 | $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($rawCustomFieldName)))); |
|---|
| 71 | delete_post_meta($postId, $customFieldName); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | $wpdb->query("DELETE FROM ". RC_CWP_TABLE_POST_META . |
|---|
| 75 | " WHERE post_id=$postId"); |
|---|
| 76 | |
|---|
| 77 | // --- Make sure all groups/fields duplicates are in sequence, |
|---|
| 78 | // i.e. there is no gap due to removing items |
|---|
| 79 | |
|---|
| 80 | $arr = ARRAY(); |
|---|
| 81 | foreach($customFieldKeys as $key=>$value) |
|---|
| 82 | { |
|---|
| 83 | list($customFieldId, $groupCounter, $fieldCounter, $rawCustomFieldName) = split("_", $value, 4); |
|---|
| 84 | $arr[$key]->id = $customFieldId ; |
|---|
| 85 | $arr[$key]->gc = $groupCounter ; |
|---|
| 86 | $arr[$key]->fc = $fieldCounter ; |
|---|
| 87 | $arr[$key]->fn = $rawCustomFieldName ; |
|---|
| 88 | $arr[$key]->ov = $value ; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | for($i=0;$i<$key;$i++){ |
|---|
| 93 | for($j=0;$j<$key;$j++){ |
|---|
| 94 | if( $arr[$i]->id == $arr[$j]->id ) |
|---|
| 95 | { |
|---|
| 96 | if( $arr[$i]->gc == $arr[$j]->gc ) |
|---|
| 97 | { |
|---|
| 98 | if( $arr[$i]->fc < $arr[$j]->fc ) |
|---|
| 99 | { |
|---|
| 100 | $t = $arr[$i] ; |
|---|
| 101 | $arr[$i] = $arr[$j] ; |
|---|
| 102 | $arr[$j] = $t ; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | else if( $arr[$i]->gc < $arr[$j]->gc ) |
|---|
| 106 | { |
|---|
| 107 | $t = $arr[$i] ; |
|---|
| 108 | $arr[$i] = $arr[$j] ; |
|---|
| 109 | $arr[$j] = $t ; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | else if( $arr[$i]->id < $arr[$j]->id ) |
|---|
| 113 | { |
|---|
| 114 | $t = $arr[$i] ; |
|---|
| 115 | $arr[$i] = $arr[$j] ; |
|---|
| 116 | $arr[$j] = $t ; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | for($i=0;$i<$key;$i++) |
|---|
| 122 | { |
|---|
| 123 | if( $arr[$i]->id != $currentFieldID ) |
|---|
| 124 | { |
|---|
| 125 | $currentFieldID = $arr[$i]->id ; |
|---|
| 126 | $currentG = $arr[$i]->gc ; |
|---|
| 127 | $GC = 1 ; |
|---|
| 128 | $FC = 1 ; |
|---|
| 129 | } |
|---|
| 130 | else if( $arr[$i]->gc != $currentG ) |
|---|
| 131 | { |
|---|
| 132 | $GC ++ ; |
|---|
| 133 | $FC = 1 ; |
|---|
| 134 | $currentG = $arr[$i]->gc ; |
|---|
| 135 | } |
|---|
| 136 | else $FC ++ ; |
|---|
| 137 | $arr[$i]->fc = $FC ; |
|---|
| 138 | $arr[$i]->gc = $GC ; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | // --- Add new meta data |
|---|
| 143 | foreach ($arr as $key) |
|---|
| 144 | { |
|---|
| 145 | if (!empty($key)) |
|---|
| 146 | { |
|---|
| 147 | // list($customFieldId, $groupCounter, $fieldCounter, $rawCustomFieldName) = split("_", $key, 4); |
|---|
| 148 | $customFieldValue = $_POST[$key->ov]; |
|---|
| 149 | |
|---|
| 150 | $customFieldName = $wpdb->escape(stripslashes(trim(RC_Format::GetFieldName($key->fn)))); |
|---|
| 151 | |
|---|
| 152 | // Prepare field value |
|---|
| 153 | if (is_array($customFieldValue)) |
|---|
| 154 | { |
|---|
| 155 | $finalValue = array(); |
|---|
| 156 | foreach ($customFieldValue as $value) |
|---|
| 157 | { |
|---|
| 158 | $value = stripslashes(trim($value)); |
|---|
| 159 | array_push($finalValue, $value); |
|---|
| 160 | //add_post_meta($postId, $customFieldName, $value); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | else |
|---|
| 166 | { |
|---|
| 167 | $finalValue = stripslashes(trim($customFieldValue)); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // Add field value meta data |
|---|
| 171 | add_post_meta($postId, $customFieldName, $finalValue); |
|---|
| 172 | $fieldMetaID = $wpdb->insert_id; |
|---|
| 173 | |
|---|
| 174 | // Add field extended properties |
|---|
| 175 | $wpdb->query("INSERT INTO ". RC_CWP_TABLE_POST_META . |
|---|
| 176 | " (id, field_name, group_count, field_count, post_id) ". |
|---|
| 177 | " VALUES ($fieldMetaID, '$customFieldName', ".$key->gc.", ".$key->fc.", $postId)"); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | /** |
|---|
| 184 | * This function prepares some custom fields before saving it. It reads $_REQUEST and: |
|---|
| 185 | * 1. Adds params to photos uploaded (Image field) |
|---|
| 186 | * 2. Formats dates (Date Field) |
|---|
| 187 | * |
|---|
| 188 | */ |
|---|
| 189 | function PrepareFieldsValues($postId) |
|---|
| 190 | { |
|---|
| 191 | |
|---|
| 192 | // Add params to photos |
|---|
| 193 | if( isset( $_REQUEST['rc_cwp_meta_photos'] ) ) |
|---|
| 194 | { |
|---|
| 195 | foreach( $_REQUEST['rc_cwp_meta_photos'] as $meta_name ) |
|---|
| 196 | { |
|---|
| 197 | $slashPos = strrpos($_POST[$meta_name], "/"); |
|---|
| 198 | if (!($slashPos === FALSE)) |
|---|
| 199 | $_POST[$meta_name] = substr($_POST[$meta_name], $slashPos+1); |
|---|
| 200 | |
|---|
| 201 | // if photo is new, add params |
|---|
| 202 | /*if( isset( $_REQUEST[ $meta_name . '_params' ] ) && $_REQUEST[ $meta_name . '_params' ] ) |
|---|
| 203 | { |
|---|
| 204 | if( ! strpos( $_POST[$meta_name], $_REQUEST[ $meta_name . '_params' ] ) ) |
|---|
| 205 | { |
|---|
| 206 | $_POST[$meta_name] .= $_REQUEST[$meta_name . '_params']; |
|---|
| 207 | } |
|---|
| 208 | }*/ |
|---|
| 209 | |
|---|
| 210 | //Rename photo if it is edited using editnplace to avoid phpthumb cache |
|---|
| 211 | if ($_POST[$meta_name.'_dorename'] == 1){ |
|---|
| 212 | $oldFilename = $_POST[$meta_name]; |
|---|
| 213 | $newFilename = time().substr($oldFilename, 10); |
|---|
| 214 | rename(FLUTTER_UPLOAD_FILES_DIR.$oldFilename, FLUTTER_UPLOAD_FILES_DIR.$newFilename); |
|---|
| 215 | $_POST[$meta_name] = $newFilename; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | // Format Dates |
|---|
| 222 | if( isset( $_REQUEST['rc_cwp_meta_date'] ) ) |
|---|
| 223 | { |
|---|
| 224 | foreach( $_REQUEST['rc_cwp_meta_date'] as $meta_name ) |
|---|
| 225 | { |
|---|
| 226 | $metaDate = strtotime($_POST[$meta_name]); |
|---|
| 227 | $formatted_date = strftime("%Y-%m-%d", $metaDate); |
|---|
| 228 | $_POST[$meta_name] = $formatted_date; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Get a custom write panel by reading $_REQUEST['custom-write-panel-id'] or the |
|---|
| 235 | * To see whether $_GET['post'] has a custom write panel associated to it. |
|---|
| 236 | * |
|---|
| 237 | * @return Custom Write Panel as an object, returns null if there is no write panels. |
|---|
| 238 | */ |
|---|
| 239 | function GetCustomWritePanel() |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | if (isset($_GET['post'])) |
|---|
| 243 | { |
|---|
| 244 | |
|---|
| 245 | $customWritePanelId = get_post_meta((int)$_GET['post'], RC_CWP_POST_WRITE_PANEL_ID_META_KEY, true); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | if (empty($customWritePanelId)) |
|---|
| 249 | { |
|---|
| 250 | $customWritePanelId = (int)$_REQUEST['custom-write-panel-id']; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | else if (isset($_REQUEST['custom-write-panel-id'])) |
|---|
| 254 | { |
|---|
| 255 | $customWritePanelId = (int)$_REQUEST['custom-write-panel-id']; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | if (isset($customWritePanelId)) |
|---|
| 259 | { |
|---|
| 260 | include_once('RCCWP_Application.php'); |
|---|
| 261 | $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | return $customWritePanel; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * |
|---|
| 270 | * |
|---|
| 271 | */ |
|---|
| 272 | function DeletePostMetaData($postId) |
|---|
| 273 | { |
|---|
| 274 | global $wpdb; |
|---|
| 275 | $wpdb->query("DELETE FROM " . RC_CWP_TABLE_POST_META . " WHERE post_id =" . $postId) ; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | } |
|---|
| 281 | ?> |
|---|