-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenericContentType.php
executable file
·457 lines (350 loc) · 15.9 KB
/
GenericContentType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<?php
global $wpc_content_types;
$wpc_content_types = array();
abstract class GenericContentType {
public $id = NULL;
public $fields = array();
public $generated_values = array();
public $relationships = array();
public $label = "";
public $slug = "";
public $singular_label = "";
public $menu_label = "";
public $supports = array('title','editor');
public $has_archive = false;
public $hierarchical = false;
public $exclude_from_search = false;
public $menu_position = 5;
public $show_in_menu = true;
public $auto_publish_from_rel_edit = true;
public $menu_item_url = "";
protected $is_first_metabox = true;
// is this still neccessary?
protected $current_post_data = array();
public $table;
public $id_col;
public $wpid_col;
public static $wp_keys = array('post_author', 'post_date',
'post_date_gmt', 'post_content', 'post_content_filtered',
'post_title', 'post_excerpt', 'post_status', 'post_type',
'comment_count', 'comment_status', 'ping_status', 'post_password',
'post_name', 'to_ping', 'pinged', 'post_modified',
'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type',
'guid');
function __construct () {
global $wpc_content_types;
// SET DEFAULTS
if ( empty($this->id) ) $this->id = strtolower ( get_class_name($this) );
if ( empty($this->table) ) $this->table = "wp_wpc_$this->id";
if ( empty($this->id_col) ) $this->id_col = 'post_id';
if ( empty($this->wpid_col) ) $this->wpid_col = 'post_id';
if ( empty($this->label) ) $this->label = $this->id . "s";
if ( empty($this->singular_label) ) $this->singular_label = $this->id;
if ( empty($this->menu_label) ) $this->menu_label = $this->label;
if ( empty($this->slug) ) $this->slug = $this->id;
if ( empty($this->taxonomies) ) $this->taxonomies = array ();
// REGISTER POST TYPE
if(!post_type_exists($this->id)) {
register_post_type ($this->id, array(
'label' => ucfirst($this->label),
'labels' => array(
'name' => ucfirst($this->label),
'singular_name' => ucfirst($this->singular_label),
'menu_name' => ucfirst($this->menu_label)
),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => $this->hierarchical,
'menu_position' => $this->menu_position,
'exclude_from_search' => $this->exclude_from_search,
'show_in_menu' => $this->show_in_menu,
'_builtin' => false,
'rewrite' => array("slug" => $this->slug),
'query_var' => $this->slug,
'supports' => $this->supports,
'has_archive' => $this->has_archive,
'taxonomies' => $this->taxonomies,
'register_meta_box_cb' => array($this, 'wp_register_meta_box')
) );
}
// ADD HOOKS
add_action ('admin_print_scripts', array($this, "custom_print_scripts") );
add_action ('admin_print_styles', array($this, "custom_print_styles") );
add_filter("manage_edit-{$this->slug}_columns",
array($this, "wp_manage_edit_columns"));
add_filter("manage_{$this->slug}_posts_custom_column",
array($this, "wp_manage_posts_custom_column"), 10, 2);
add_filter("manage_edit-{$this->slug}_sortable_columns",
array($this, "wp_manage_edit_sortable_columns"));
WPCRecord::make_specific_class(ucfirst($this->id)."Record", "$this->id");
$wpc_content_types[$this->id] = $this;
}
/**
* stub which does nothing. overwrite if needed
*/
public function wp_register_meta_box () {
}
public function echo_update_relation_item_metabox () {
return $this->echo_new_relation_item_metabox();
}
public function echo_new_relation_item_metabox () {
return "";
}
protected function get_field_type ($field_key) {
$ret = "";
if ( !empty($this->fields[$field_key]) )
$ret = $this->fields[$field_key]->type;
return $ret;
}
public function echo_update_relation_item_metabox_str () {
ob_start();
global $post;
$old_post = $post;
$post = null;
$this->echo_update_relation_item_metabox();
$html_str = ob_get_clean();
$html_str = preg_replace("/(id|for|name)\=('|\")wpc_/", "$1=$2wpc_".$this->id."_", $html_str);
$html_str = htmlspecialchars($html_str);
$html_str = preg_replace("/\n/", "\\\n", $html_str);
$post = $old_post;
return $html_str;
}
public function echo_new_relation_item_metabox_str () {
ob_start();
global $post;
$old_post = $post;
$post = null;
$this->echo_new_relation_item_metabox();
$html_str = ob_get_clean();
$html_str = preg_replace("/(id|for|name)\=('|\")wpc_/", "$1=$2wpc_".$this->id."_", $html_str);
$html_str = htmlspecialchars($html_str);
$html_str = preg_replace("/\n/", "\\\n", $html_str);
$post = $old_post;
return $html_str;
}
/**
* stub which does nothing. overwrite if needed
*/
public function wp_insert_post_data($data, $postarr) {
return $data;
}
/**
* wp callback to add columns in overview for this post type
*/
public function wp_manage_edit_columns($cols) {
$manage_cols = array_keys(array_filter($this->fields, function($col) {
return $col->edit_column;
}));
if (! empty($manage_cols))
$manage_cols = array_combine($manage_cols, $manage_cols);
$ret = $cols + $manage_cols;
// move date to end of array
if ( !empty($ret['date']) ) {
$date_val = $ret['date'];
unset($ret['date']);
$ret['date'] = $date_val;
}
return $ret;
}
/**
* wp callback to display columns in overview for this post type
*/
public function wp_manage_posts_custom_column($column, $id) {
// all columns with key edit_column
$manage_cols = array_filter($this->fields, function($col) {
return $col->edit_column;
});
if (! isset($manage_cols[$column]))
return;
$element = the_record($id);
echo $element->get($column);
}
/**
* wp callback to sort columns in overview for this post type
*/
public function wp_manage_edit_sortable_columns($cols) {
// all columns with key edit_column
$manage_cols = array_keys(array_filter($this->fields, function($col) {
return $col->sortable_column;
}));
// [$col => $col]
if (! empty($manage_cols))
$manage_cols = array_combine($manage_cols, $manage_cols);
return $cols + $manage_cols;
}
function custom_print_scripts () {
}
function custom_print_styles () {
}
function first_metabox () {
if ($this->is_first_metabox == true) {
$this->is_first_metabox = false;
?>
<script type="text/javascript">
var postID = <?php the_ID(); ?>;
function disable_posttitle () {
if ( <?php echo (!empty($this->generated_values['post_title']) ? "true" : "false"); ?> ) {
jQuery('input#title').attr("disabled", "disabled");
}
if ( <?php echo (!empty($this->generated_values['post_name']) ? "true" : "false"); ?> ) {
jQuery('#edit-slug-buttons a').attr("disabled", "disabled");
jQuery('#edit-slug-buttons a').removeAttr("onclick");
jQuery('#editable-post-name').unbind("click");
}
}
function check_text_input_value(event) {
var input = jQuery(this);
var label = jQuery("label.wpc_hint[for='" + input.attr('id') + "']");
if ( !(event.type == "keydown" && event.keyCode != 9) && input.val() == "" ){
label.addClass("show_full");
} else if ( event.keyCode >= 48
&& event.keyCode != 91
&& event.keyCode != 93) {
label.removeClass("show_full");
}
}
jQuery(document).ready(function () {
jQuery("body").delegate(".wpc_input_text", "focus keydown keyup change", check_text_input_value);
jQuery(".wpc_input_text").each(check_text_input_value);
disable_posttitle();
});
</script>
<?php
}
}
function admin_init() {
}
function delete_post ($post_id, $post=null) {
global $wpdb;
global $wpc_relationships;
#ButterLog::debug("deletion post with post_id $post_id");
$post_type = get_post_type($post_id);
if ( $wpdb->query( $wpdb->prepare("DELETE FROM $this->table WHERE post_id = %d", $post_id) ) === FALSE) {
ButterLog::error("Could not delete wpc data for $post_id in table $this->table.");
return false;
}
if( !empty($post_type) ) foreach ($wpc_relationships as $wpc_relationship) {
if ( $post_type == $wpc_relationship->post_type_from_id ) {
$wpdb->query( $wpdb->prepare("DELETE FROM $wpc_relationship->table WHERE post_from_id = %d", $post_id) );
}
if ( $post_type == $wpc_relationship->post_type_to_id ) {
$wpdb->query( $wpdb->prepare("DELETE FROM $wpc_relationship->table WHERE post_to_id = %d", $post_id) );
}
}
return true;
}
function create_post ($post = array(), $wpcpost = array(), $write_read_only_fields = false) {
#ButterLog::debug("creating new $this->id");
$post['post_type'] = $this->id;
$post['post_title'] = 'new $this->id';
$post_id = intval( wp_insert_post($post) );
if ( !empty($post_id) ) {
$this->update_post($post_id, $post, $wpcpost, $write_read_only_fields);
}
return $post_id;
}
function new_post ($post_id, $post, $wpcpost) {
global $wpdb;
#ButterLog::debug("new post with post_id $post_id");
$data = array($this->wpid_col => $post_id);
if (! $wpdb->insert($this->table, $data, '%d')) {
ButterLog::error("Could not insert initial row for $post_id.");
}
// this might be unneccessary.
// custom fields might always be unset at this point
// look for 'new post' not followed by 'nothing to save' log messages
$this->update_post($post_id, $post, $wpcpost);
}
function update_post ($post_id, $post, $wpcpost, $write_read_only_fields = false) {
#ButterLog::debug("saving post with post_id $post_id");
if (! $write_read_only_fields) {
$candidate_fields = array_filter($this->fields,
function($field) use ($post_id) {
return $field->may_write($post_id);
});
} else {
$candidate_fields = $this->fields;
}
$field_defaults = array_map(function ($field) {
return $field->default;
}, $candidate_fields);
$field_formats = array_map(function ($field) {
return $field->printf_specifier;
}, $candidate_fields);
// weed out invalid fields, add defaults
// XXX: handle unsetting fields
// see https://core.trac.wordpress.org/ticket/15158
$to_update = array_intersect_key($wpcpost, $candidate_fields);
if (! empty($to_update))
$this->update_dbs($post_id, $to_update, $field_formats);
// regenerate GeneratedValues
$to_update = array_map(function ($field) use ($post_id) {
return $field->value_uncached($post_id);
}, $this->generated_values);
$field_formats = array_map(function ($field) {
return $field->printf_specifier;
}, $this->generated_values);
$post_status = 'publish';
$post_type = $this->id;
$post_parent = 0;
if ( !empty($this->generated_values["post_name"]) )
$unsanatized_post_slug = $to_update["post_name"];
elseif ( !empty($this->generated_values["post_title"]) && (empty($post->post_name) || $post->post_name == "$post->ID") )
$unsanatized_post_slug = $to_update["post_title"];
if ( !empty($unsanatized_post_slug) ) {
$to_update["post_name"] = wp_unique_post_slug(
sanitize_title(str_replace("/", "-", str_replace(array("ä","ö","ü","ß"), array("ae","oe","ue","ss"), strtolower($unsanatized_post_slug)))),
$post_id,
$post_status,
$post_type,
$post_parent
);
$field_formats["post_name"] = "%s";
}
$this->update_dbs($post_id, $to_update, $field_formats);
}
protected function update_dbs ($post_id, $to_update, $field_formats) {
global $wpdb;
#ButterLog::debug("update_dbs $post_id.", $to_update);
#ButterLog::debug("update_dbs $post_id.", $field_formats);
$wp_keys = array_flip(self::$wp_keys);
$this->update_db($this->table, $post_id,
array_diff_key($to_update, $wp_keys), $field_formats);
$this->update_db($wpdb->posts, $post_id,
array_intersect_key($to_update, $wp_keys), $field_formats);
}
protected function update_db($table, $post_id, $to_update, $field_formats) {
global $wpdb;
if (empty($to_update))
return true;
$formats = array_intersect_key($field_formats, $to_update);
$id_row = $table == $wpdb->posts ? "ID" : $this->wpid_col;
$query = "SELECT $id_row FROM $table WHERE $id_row = $post_id";
$table_entry_test = $wpdb->get_var( $query );
if ( empty( $table_entry_test ) ) {
$formats[$id_row] = "%d";
$to_update[$id_row] = $post_id;
if ($wpdb->insert($table,
$to_update, // col = val
$formats, // printf formats for set
'%d' // printf format for where
) === false) {
ButterLog::error("Could not update $table for post_id $post_id.");
return false;
}
} else {
if ($wpdb->update($table,
$to_update, // col = val
array( ($table == $wpdb->posts ? "ID" : $this->wpid_col) => $post_id), // where
$formats, // printf formats for set
'%d' // printf format for where
) === false) {
ButterLog::error("Could not update $table for post_id $post_id.");
return false;
}
}
return true;
}
}
?>