-
Notifications
You must be signed in to change notification settings - Fork 2
/
subpost.php
236 lines (161 loc) · 8.28 KB
/
subpost.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
<?php
/*
Plugin Name: Subordinate Post Type Helpers
Description: This plugin provides a number of helpers for registering a custom post type that is subordinate to another post type.
Version: 0.2.3.1
Author: randyhoyt
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
function register_sub_post_type($post_type,$args,$parent_post_type) {
$current_sub_post_type = array (
'post_type' => $post_type,
'args' => $args,
'parent_post_type' => $parent_post_type
);
register_post_type($post_type,$args);
$sub_post_type_factory = SubPostTypeFactory::getInstance();
$sub_post_type_factory->addPostType($current_sub_post_type);
add_action('init',array($sub_post_type_factory,'wp_init'),99999);
}
class SubPostTypeFactory
{
private static $sub_post_type_factory;
private static $sub_post_types;
private static $sub_post_type_current;
private function __construct() {}
public static function getInstance() {
if (!isset(self::$sub_post_type_factory)) {
self::$sub_post_type_factory = new SubPostTypeFactory();
}
return self::$sub_post_type_factory;
}
public static function addPostType($post_type_new) {
self::$sub_post_types[$post_type_new["post_type"]] = $post_type_new;
}
public function getPostTypes() {
$return = self::$sub_post_types;
return $return;
}
public function setCurrentPostType($post_type) {
self::$sub_post_type_current = $post_type;
}
public function getCurrentPostType() {
return self::$sub_post_type_current;
}
public function wp_init() {
$sub_post_type_factory = self::getInstance();
$post_types = $sub_post_type_factory->getPostTypes();
if ($post_types) {
add_action("admin_menu", "subpost_add_to_submenu",10,2);
add_action("add_meta_boxes","subpost_add_meta_box");
}
}
}
function subpost_add_to_submenu() {
global $menu,$submenu;
$sub_post_type_factory = SubPostTypeFactory::getInstance();
$post_types = $sub_post_type_factory->getPostTypes();
if ($post_types) {
foreach ($post_types as $post_type) {
if ($post_type["parent_post_type"] != "post") {
$parent_query_string = '?post_type=' . $post_type["parent_post_type"];
} else {
$parent_query_string = "";
}
if ($post_type['args']['show_in_menu']!== false) {
add_submenu_page('edit.php' . $parent_query_string, $post_type['args']['labels']['name'], $post_type['args']['labels']['name'], 'edit_posts', 'edit.php?post_type=' . $post_type["post_type"]);
add_submenu_page('edit.php' . $parent_query_string, $post_type['args']['labels']['all_items'], "— " . $post_type['args']['labels']['all_items'], 'edit_posts', 'edit.php?post_type=' . $post_type["post_type"]);
add_submenu_page('edit.php' . $parent_query_string, $post_type['args']['labels']['add_new_item'], "— " . $post_type['args']['labels']['add_new_item'], 'edit_posts', 'post-new.php?post_type=' . $post_type["post_type"]);
}
remove_menu_page('edit.php?post_type=' . $post_type["post_type"]);
}
}
}
function subpost_add_meta_box() {
global $post;
$sub_post_type_factory = SubPostTypeFactory::getInstance();
$post_types = $sub_post_type_factory->getPostTypes();
if ($post_types) {
foreach($post_types as $post_type) {
if ($post->post_type == $post_type["parent_post_type"]) {
$sub_post_type_factory->setCurrentPostType($post_type["post_type"]);
add_meta_box(
'custom_meta_box_' . $post_type["post_type"],
$post_type['args']['labels']['name'],
'subpost_render_meta_box',
$post_type["parent_post_type"],
'normal',
'high',
array("sub_post_type" => $post_type)
);
}
}
}
}
function subpost_render_meta_box($post,$metabox) {
//global $post;
//$sub_post_type_factory = SubPostTypeFactory::getInstance();
//$post_types = $sub_post_type_factory->getPostTypes();
//$current_sub_post_type = $post_types[$sub_post_type_factory->getCurrentPostType()];
$current_sub_post_type = $metabox['args']['sub_post_type'];
echo '<div id="subpost_list_children_' . $current_sub_post_type["post_type"] . '">';
$output = subpost_display_all_children($post->ID,$current_sub_post_type["post_type"]);
if ($output) {
echo $output;
} else {
echo '<p>' . $current_sub_post_type["args"]['labels']['not_found'] . '</p>';
}
echo '</div>';
// $new_link = plugin_dir_url(__FILE__) . 'children.php?&form_title=' . $current_sub_post_type["args"]['labels']['add_new_item'] . '&post_parent=' . $post->ID;
$new_link = admin_url('admin-post.php?action=subpost_form_children&children.php&form_title=' . $current_sub_post_type["args"]['labels']['add_new_item']) . '&post_parent=' . $post->ID;
echo '<a title="' . $current_sub_post_type["args"]['labels']['add_new_item'] . '" class="button thickbox" href="'. $new_link . '&post_type=' . $current_sub_post_type["post_type"] . '&TB_iframe=1&width=480&height=440">' . $current_sub_post_type["args"]['labels']['add_new_item'] . '</a>';
}
add_action('admin_post_subpost_form_children','subpost_form_children');
function subpost_form_children() {
include("children.php");
}
function subpost_display_all_children($post_id,$sub_post_type_name) {
$output = "";
$sub_post_type_factory = SubPostTypeFactory::getInstance();
$post_types = $sub_post_type_factory->getPostTypes();
$sub_post_type = $post_types[$sub_post_type_name];
$subposts = get_posts(array(
'post_type' => $sub_post_type["post_type"],
'post_parent' => $post_id,
'numberposts' => -1,
'order' => 'ASC'
));
if ($subposts) {
$output .= '<table class="wp-list-table widefat fixed posts" style="margin: 12px 0; border-bottom: 0;" cellspacing="0"><tbody id="the-list">';
foreach($subposts as $subpost) {
$output .= subpost_display_one_child($subpost,$sub_post_type);
}
$output .= '</tbody></table>';
return $output;
} else {
return false;
}
}
function subpost_display_one_child($post,$sub_post_type) {
$output = "";
// $edit_link = plugin_dir_url(__FILE__) . 'children.php?&form_title=' . $sub_post_type["args"]['labels']['edit_item'] . '&post=' . $post->ID . '&post_parent=' . $post->post_parent . '&post_type=' . $sub_post_type["post_type"] . '&TB_iframe=1&width=480&height=440';
$edit_link = admin_url('admin-post.php?action=subpost_form_children&form_title=' . $sub_post_type["args"]['labels']['edit_item'] . '&post=' . $post->ID . '&post_parent=' . $post->post_parent . '&post_type=' . $sub_post_type["post_type"]) . '&TB_iframe=1&width=480&height=440';
$output .= '<tr id="post-' . $post->ID . '" class="post-' . $post->ID . '" valign="top">';
$output .= '<td class="post-title page-title column-title"><strong><a class="row-title thickbox" href="' . $edit_link . '" title="' . $sub_post_type["args"]['labels']['edit_item'] . '">' . esc_attr($post->post_title) . '</a></strong>';
$output .= '<span class="row-actions"><span class="edit"><a class="thickbox" href="' . $edit_link . '" title="' . $sub_post_type["args"]['labels']['edit_item'] . '">' . $sub_post_type["args"]['labels']['edit_item'] . '</a></span></td>';
$output .= apply_filters("subpost_display_column","",$post,$sub_post_type);
$output .= "</tr>";
return $output;
}
add_action( 'admin_footer', 'subpost_javascript_footer');
function subpost_javascript_footer() {
?>
<script>
function subpost_list_children(html_display,sub_post_type) {
jQuery('#subpost_list_children_' + sub_post_type).empty().append(html_display);
tb_remove();
}
</script>
<?php
}