| [21] | 1 | <?php |
|---|
| 2 | class RCCWP_CustomWritePanel |
|---|
| 3 | { |
|---|
| 4 | function AssignToRole($customWritePanelId, $roleName) |
|---|
| 5 | { |
|---|
| 6 | $customWritePanel = RCCWP_CustomWritePanel::Get($customWritePanelId); |
|---|
| 7 | $capabilityName = $customWritePanel->capability_name; |
|---|
| 8 | $role = get_role($roleName); |
|---|
| 9 | $role->add_cap($capabilityName); |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | function Create($name, $description = '', $standardFields = array(), $hiddenExtFields = array(), $categories = array(), $display_order = 1) |
|---|
| 13 | { |
|---|
| 14 | include_once('RC_Format.php'); |
|---|
| 15 | global $wpdb; |
|---|
| 16 | |
|---|
| 17 | $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name); |
|---|
| 18 | |
|---|
| 19 | $sql = sprintf( |
|---|
| 20 | "INSERT INTO " . RC_CWP_TABLE_WRITE_PANELS . |
|---|
| 21 | " (name, description, display_order, capability_name, type)" . |
|---|
| 22 | " values" . |
|---|
| 23 | " (%s, %s, %d, %s, %s)", |
|---|
| 24 | RC_Format::TextToSql($name), |
|---|
| 25 | RC_Format::TextToSql($description), |
|---|
| 26 | $display_order, |
|---|
| 27 | RC_Format::TextToSql($capabilityName), |
|---|
| 28 | RC_Format::TextToSql($_POST['radPostPage']) |
|---|
| 29 | ); |
|---|
| 30 | |
|---|
| 31 | $wpdb->query($sql); |
|---|
| 32 | $customWritePanelId = $wpdb->insert_id; |
|---|
| 33 | |
|---|
| 34 | if (!isset($categories)) |
|---|
| 35 | $categories = array(); |
|---|
| 36 | foreach ($categories as $cat_id) |
|---|
| 37 | { |
|---|
| 38 | $sql = sprintf( |
|---|
| 39 | "INSERT INTO " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 40 | " (panel_id, cat_id)" . |
|---|
| 41 | " values (%d, %d)", |
|---|
| 42 | $customWritePanelId, |
|---|
| 43 | $cat_id |
|---|
| 44 | ); |
|---|
| 45 | $wpdb->query($sql); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | if (!isset($standardFields)) |
|---|
| 49 | $standardFields = array(); |
|---|
| 50 | foreach ($standardFields as $standard_field_id) |
|---|
| 51 | { |
|---|
| 52 | $sql = sprintf( |
|---|
| 53 | "INSERT INTO " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 54 | " (panel_id, standard_field_id)" . |
|---|
| 55 | " values (%d, %d)", |
|---|
| 56 | $customWritePanelId, |
|---|
| 57 | $standard_field_id |
|---|
| 58 | ); |
|---|
| 59 | $wpdb->query($sql); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | if (!empty($hiddenExtFields)) |
|---|
| 63 | { |
|---|
| 64 | foreach ($hiddenExtFields as $css_id) |
|---|
| 65 | { |
|---|
| 66 | if ($css_id != '') |
|---|
| 67 | { |
|---|
| 68 | $sql = sprintf( |
|---|
| 69 | "INSERT INTO " . RC_CWP_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD . |
|---|
| 70 | " (panel_id, css_id)" . |
|---|
| 71 | " values (%d, %s)", |
|---|
| 72 | $customWritePanelId, |
|---|
| 73 | RC_Format::TextToSql($css_id) |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | $wpdb->query($sql); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return $customWritePanelId; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | function Delete($customWritePanelId = null) |
|---|
| 85 | { |
|---|
| 86 | include_once ('RCCWP_CustomField.php'); |
|---|
| 87 | if (isset($customWritePanelId)) |
|---|
| 88 | { |
|---|
| 89 | global $wpdb; |
|---|
| 90 | |
|---|
| 91 | $customWritePanel = RCCWP_Application::GetCustomWritePanels($customWritePanelId); |
|---|
| 92 | $customFields = RCCWP_CustomWritePanel::GetCustomFields($customWritePanel->id); |
|---|
| 93 | foreach ($customFields as $field) |
|---|
| 94 | { |
|---|
| 95 | RCCWP_CustomField::Delete($field->id); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | $sql = sprintf( |
|---|
| 99 | "DELETE FROM " . RC_CWP_TABLE_WRITE_PANELS . |
|---|
| 100 | " WHERE id = %d", |
|---|
| 101 | $customWritePanel->id |
|---|
| 102 | ); |
|---|
| 103 | $wpdb->query($sql); |
|---|
| 104 | |
|---|
| 105 | $sql = sprintf( |
|---|
| 106 | "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 107 | " WHERE panel_id = %d", |
|---|
| 108 | $customWritePanel->id |
|---|
| 109 | ); |
|---|
| 110 | $wpdb->query($sql); |
|---|
| 111 | |
|---|
| 112 | $sql = sprintf( |
|---|
| 113 | "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 114 | " WHERE panel_id = %d", |
|---|
| 115 | $customWritePanelId |
|---|
| 116 | ); |
|---|
| 117 | $wpdb->query($sql); |
|---|
| 118 | |
|---|
| 119 | $sql = sprintf( |
|---|
| 120 | "DELETE FROM " . RC_CWP_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD . |
|---|
| 121 | " WHERE panel_id = %d", |
|---|
| 122 | $customWritePanelId |
|---|
| 123 | ); |
|---|
| 124 | $wpdb->query($sql); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | function Get($customWritePanelId) |
|---|
| 129 | { |
|---|
| 130 | global $wpdb; |
|---|
| 131 | |
|---|
| 132 | $sql = "SELECT id, name, description, display_order, capability_name, type FROM " . RC_CWP_TABLE_WRITE_PANELS . |
|---|
| 133 | " WHERE id = " . (int)$customWritePanelId; |
|---|
| 134 | |
|---|
| 135 | $results = $wpdb->get_row($sql); |
|---|
| 136 | |
|---|
| 137 | return $results; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | function GetAssignedCategoryIds($customWritePanelId) |
|---|
| 141 | { |
|---|
| 142 | $results = RCCWP_CustomWritePanel::GetAssignedCategories($customWritePanelId); |
|---|
| 143 | $ids = array(); |
|---|
| 144 | foreach ($results as $r) |
|---|
| 145 | { |
|---|
| 146 | $ids[] = $r->cat_id; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return $ids; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | function GetAssignedCategories($customWritePanelId) |
|---|
| 153 | { |
|---|
| 154 | global $wpdb; |
|---|
| 155 | /* |
|---|
| 156 | $sql = "SELECT rc.cat_id, cat_name FROM " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 157 | " rc JOIN $wpdb->categories wp ON rc.cat_ID = wp.cat_ID" . |
|---|
| 158 | " WHERE panel_id = " . $customWritePanelId; |
|---|
| 159 | */ |
|---|
| 160 | |
|---|
| 161 | if( $wpdb->terms != '' ) |
|---|
| 162 | { |
|---|
| 163 | $sql = "SELECT rc.cat_id, wp.name AS cat_name FROM " . |
|---|
| 164 | RC_CWP_TABLE_PANEL_CATEGORY . " |
|---|
| 165 | rc JOIN $wpdb->terms wp ON rc.cat_ID = wp.term_id" . " |
|---|
| 166 | WHERE panel_id = " . $customWritePanelId; |
|---|
| 167 | } |
|---|
| 168 | else |
|---|
| 169 | { |
|---|
| 170 | $sql = "SELECT rc.cat_id, cat_name FROM " . |
|---|
| 171 | RC_CWP_TABLE_PANEL_CATEGORY . " |
|---|
| 172 | rc JOIN $wpdb->categories wp ON rc.cat_ID = wp.cat_ID |
|---|
| 173 | WHERE panel_id = " . $customWritePanelId; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | $results = $wpdb->get_results($sql); |
|---|
| 178 | if (!isset($results)) |
|---|
| 179 | $results = array(); |
|---|
| 180 | |
|---|
| 181 | return $results; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | function GetCapabilityName($customWritePanelName) |
|---|
| 185 | { |
|---|
| 186 | // copied from WP's sanitize_title_with_dashes($title) (formatting.php) |
|---|
| 187 | $capabilityName = strip_tags($customWritePanelName); |
|---|
| 188 | // Preserve escaped octets. |
|---|
| 189 | $capabilityName = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $capabilityName); |
|---|
| 190 | // Remove percent signs that are not part of an octet. |
|---|
| 191 | $capabilityName = str_replace('%', '', $capabilityName); |
|---|
| 192 | // Restore octets. |
|---|
| 193 | $capabilityName = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $capabilityName); |
|---|
| 194 | |
|---|
| 195 | $capabilityName = remove_accents($capabilityName); |
|---|
| 196 | if (seems_utf8($capabilityName)) |
|---|
| 197 | { |
|---|
| 198 | if (function_exists('mb_strtolower')) |
|---|
| 199 | { |
|---|
| 200 | $capabilityName = mb_strtolower($capabilityName, 'UTF-8'); |
|---|
| 201 | } |
|---|
| 202 | $capabilityName = utf8_uri_encode($capabilityName, 200); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | $capabilityName = strtolower($capabilityName); |
|---|
| 206 | $capabilityName = preg_replace('/&.+?;/', '', $capabilityName); // kill entities |
|---|
| 207 | $capabilityName = preg_replace('/[^%a-z0-9 _-]/', '', $capabilityName); |
|---|
| 208 | $capabilityName = preg_replace('/\s+/', '_', $capabilityName); |
|---|
| 209 | $capabilityName = preg_replace('|-+|', '_', $capabilityName); |
|---|
| 210 | $capabilityName = trim($capabilityName, '_'); |
|---|
| 211 | |
|---|
| 212 | return $capabilityName; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | function GetCustomFields($customWritePanelId) |
|---|
| 216 | { |
|---|
| 217 | global $wpdb; |
|---|
| 218 | $sql = "SELECT cf.id, cf.name, 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 . |
|---|
| 219 | " cf LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_OPTIONS . " co ON cf.id = co.custom_field_id" . |
|---|
| 220 | " LEFT JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_PROPERTIES . " cp ON cf.id = cp.custom_field_id" . |
|---|
| 221 | " JOIN " . RC_CWP_TABLE_CUSTOM_FIELD_TYPES . " tt ON cf.type = tt.id" . |
|---|
| 222 | " WHERE panel_id = " . $customWritePanelId . |
|---|
| 223 | " ORDER BY cf.display_order"; |
|---|
| 224 | $results =$wpdb->get_results($sql); |
|---|
| 225 | if (!isset($results)) |
|---|
| 226 | $results = array(); |
|---|
| 227 | |
|---|
| 228 | for ($i = 0; $i < $wpdb->num_rows; ++$i) |
|---|
| 229 | { |
|---|
| 230 | $results[$i]->options = unserialize($results[$i]->options); |
|---|
| 231 | $results[$i]->properties = unserialize($results[$i]->properties); |
|---|
| 232 | $results[$i]->default_value = unserialize($results[$i]->default_value); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | return $results; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | function GetHiddenExternalFieldCssIds($customWritePanelId) |
|---|
| 239 | { |
|---|
| 240 | global $wpdb; |
|---|
| 241 | $sql = "SELECT css_id FROM " . RC_CWP_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD . |
|---|
| 242 | " WHERE panel_id = " . $customWritePanelId; |
|---|
| 243 | $results = $wpdb->get_col($sql); |
|---|
| 244 | if (!isset($results)) |
|---|
| 245 | $results = array(); |
|---|
| 246 | |
|---|
| 247 | return $results; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | function GetStandardFieldCssIds($customWritePanelId) |
|---|
| 251 | { |
|---|
| 252 | $results = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId); |
|---|
| 253 | $ids = array(); |
|---|
| 254 | foreach ($results as $r) |
|---|
| 255 | { |
|---|
| 256 | $ids[] = $r->css_id; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | return $ids; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | function GetStandardFieldIds($customWritePanelId) |
|---|
| 263 | { |
|---|
| 264 | $results = RCCWP_CustomWritePanel::GetStandardFields($customWritePanelId); |
|---|
| 265 | $ids = array(); |
|---|
| 266 | foreach ($results as $r) |
|---|
| 267 | { |
|---|
| 268 | $ids[] = $r->standard_field_id; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | return $ids; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | function GetStandardFields($customWritePanelId) |
|---|
| 275 | { |
|---|
| 276 | global $wpdb; |
|---|
| 277 | $sql = "SELECT ps.standard_field_id, name, css_id FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 278 | " ps JOIN " . RC_CWP_TABLE_STANDARD_FIELDS . " sf ON ps.standard_field_id = sf.id" . |
|---|
| 279 | " WHERE panel_id = " . $customWritePanelId; |
|---|
| 280 | $results = $wpdb->get_results($sql); |
|---|
| 281 | if (!isset($results)) |
|---|
| 282 | $results = array(); |
|---|
| 283 | |
|---|
| 284 | return $results; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | function Update($customWritePanelId, $name, $description = '', $standardFields = array(), $hiddenExtFields = array(), $categories = array(), $display_order = 1) |
|---|
| 288 | { |
|---|
| 289 | include_once('RC_Format.php'); |
|---|
| 290 | global $wpdb; |
|---|
| 291 | |
|---|
| 292 | $capabilityName = RCCWP_CustomWritePanel::GetCapabilityName($name); |
|---|
| 293 | |
|---|
| 294 | $sql = sprintf( |
|---|
| 295 | "UPDATE " . RC_CWP_TABLE_WRITE_PANELS . |
|---|
| 296 | " SET name = %s" . |
|---|
| 297 | " , description = %s" . |
|---|
| 298 | " , display_order = %d" . |
|---|
| 299 | " , capability_name = %s" . |
|---|
| 300 | " , type = %s" . |
|---|
| 301 | " where id = %d", |
|---|
| 302 | RC_Format::TextToSql($name), |
|---|
| 303 | RC_Format::TextToSql($description), |
|---|
| 304 | $display_order, |
|---|
| 305 | RC_Format::TextToSql($capabilityName), |
|---|
| 306 | RC_Format::TextToSql($_POST['radPostPage']), |
|---|
| 307 | $customWritePanelId ); |
|---|
| 308 | |
|---|
| 309 | $wpdb->query($sql); |
|---|
| 310 | |
|---|
| 311 | if (!isset($categories) || empty($categories)) |
|---|
| 312 | { |
|---|
| 313 | $sql = sprintf( |
|---|
| 314 | "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 315 | " WHERE panel_id = %d", |
|---|
| 316 | $customWritePanelId |
|---|
| 317 | ); |
|---|
| 318 | |
|---|
| 319 | $wpdb->query($sql); |
|---|
| 320 | } |
|---|
| 321 | else |
|---|
| 322 | { |
|---|
| 323 | $currentCategoryIds = array(); |
|---|
| 324 | $currentCategoryIds = RCCWP_CustomWritePanel::GetAssignedCategoryIds($customWritePanelId); |
|---|
| 325 | |
|---|
| 326 | $keepCategoryIds = array_intersect($currentCategoryIds, $categories); |
|---|
| 327 | $deleteCategoryIds = array_diff($currentCategoryIds, $keepCategoryIds); |
|---|
| 328 | $insertCategoryIds = array_diff($categories, $keepCategoryIds); |
|---|
| 329 | |
|---|
| 330 | foreach ($insertCategoryIds as $cat_id) |
|---|
| 331 | { |
|---|
| 332 | $sql = sprintf( |
|---|
| 333 | "INSERT INTO " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 334 | " (panel_id, cat_id)" . |
|---|
| 335 | " values (%d, %d)", |
|---|
| 336 | $customWritePanelId, |
|---|
| 337 | $cat_id |
|---|
| 338 | ); |
|---|
| 339 | $wpdb->query($sql); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | if (!empty($deleteCategoryIds)) |
|---|
| 343 | { |
|---|
| 344 | $sql = sprintf( |
|---|
| 345 | "DELETE FROM " . RC_CWP_TABLE_PANEL_CATEGORY . |
|---|
| 346 | " WHERE panel_id = %d" . |
|---|
| 347 | " AND cat_id IN (%s)", |
|---|
| 348 | $customWritePanelId, |
|---|
| 349 | implode(',', $deleteCategoryIds) |
|---|
| 350 | ); |
|---|
| 351 | |
|---|
| 352 | $wpdb->query($sql); |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | if (!isset($standardFields) || empty($standardFields)) |
|---|
| 357 | { |
|---|
| 358 | $sql = sprintf( |
|---|
| 359 | "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 360 | " WHERE panel_id = %d", |
|---|
| 361 | $customWritePanelId |
|---|
| 362 | ); |
|---|
| 363 | $wpdb->query($sql); |
|---|
| 364 | } |
|---|
| 365 | else |
|---|
| 366 | { |
|---|
| 367 | $currentStandardFieldIds = array(); |
|---|
| 368 | $currentStandardFieldIds = RCCWP_CustomWritePanel::GetStandardFieldIds($customWritePanelId); |
|---|
| 369 | |
|---|
| 370 | $keepStandardFieldIds = array_intersect($currentStandardFieldIds, $standardFields); |
|---|
| 371 | $deleteStandardFieldIds = array_diff($currentStandardFieldIds, $keepStandardFieldIds); |
|---|
| 372 | $insertStandardFieldIds = array_diff($standardFields, $keepStandardFieldIds); |
|---|
| 373 | |
|---|
| 374 | foreach ($insertStandardFieldIds as $standard_field_id) |
|---|
| 375 | { |
|---|
| 376 | $sql = sprintf( |
|---|
| 377 | "INSERT INTO " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 378 | " (panel_id, standard_field_id)" . |
|---|
| 379 | " values (%d, %d)", |
|---|
| 380 | $customWritePanelId, |
|---|
| 381 | $standard_field_id |
|---|
| 382 | ); |
|---|
| 383 | $wpdb->query($sql); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | if (!empty($deleteStandardFieldIds)) |
|---|
| 387 | { |
|---|
| 388 | $sql = sprintf( |
|---|
| 389 | "DELETE FROM " . RC_CWP_TABLE_PANEL_STANDARD_FIELD . |
|---|
| 390 | " WHERE panel_id = %d" . |
|---|
| 391 | " AND standard_field_id IN (%s)", |
|---|
| 392 | $customWritePanelId, |
|---|
| 393 | implode(',', $deleteStandardFieldIds) |
|---|
| 394 | ); |
|---|
| 395 | |
|---|
| 396 | $wpdb->query($sql); |
|---|
| 397 | } |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | $sql = sprintf( |
|---|
| 401 | "DELETE FROM " . RC_CWP_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD . |
|---|
| 402 | " WHERE panel_id = %d", |
|---|
| 403 | $customWritePanelId |
|---|
| 404 | ); |
|---|
| 405 | |
|---|
| 406 | $wpdb->query($sql); |
|---|
| 407 | |
|---|
| 408 | if (!empty($hiddenExtFields)) |
|---|
| 409 | { |
|---|
| 410 | foreach ($hiddenExtFields as $css_id) |
|---|
| 411 | { |
|---|
| 412 | if ($css_id != '') |
|---|
| 413 | { |
|---|
| 414 | $sql = sprintf( |
|---|
| 415 | "INSERT INTO " . RC_CWP_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD . |
|---|
| 416 | " (panel_id, css_id)" . |
|---|
| 417 | " values (%d, %s)", |
|---|
| 418 | $customWritePanelId, |
|---|
| 419 | RC_Format::TextToSql($css_id) |
|---|
| 420 | ); |
|---|
| 421 | |
|---|
| 422 | $wpdb->query($sql); |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | } |
|---|
| 426 | } |
|---|
| 427 | } |
|---|
| 428 | ?> |
|---|