forked from CMB2/cmb2-attached-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmb2-attached-posts-field.php
179 lines (134 loc) · 5.02 KB
/
cmb2-attached-posts-field.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
<?php
/**
* Enqueue admin scripts for our attached posts field
*/
function cmb2_attached_posts_field_scripts_styles() {
$version = '1.1.1';
$dir = trailingslashit( dirname( __FILE__ ) );
if ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' ) {
// Windows
$content_dir = str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR );
$content_url = str_replace( $content_dir, WP_CONTENT_URL, $dir );
$url = str_replace( DIRECTORY_SEPARATOR, '/', $content_url );
} else {
$url = str_replace(
array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),
array( WP_CONTENT_URL, WP_PLUGIN_URL ),
$dir
);
}
$url = set_url_scheme( $url );
$requirements = array(
'jquery-ui-core',
'jquery-ui-widget',
'jquery-ui-mouse',
'jquery-ui-draggable',
'jquery-ui-droppable',
'jquery-ui-sortable',
);
wp_enqueue_script( 'cmb2-attached-posts-field', $url . 'js/attached-posts.js', $requirements, $version, true );
wp_enqueue_style( 'cmb2-attached-posts-field', $url . 'css/attached-posts-admin.css', array(), $version );
}
add_action( 'admin_enqueue_scripts', 'cmb2_attached_posts_field_scripts_styles' );
/**
* Add a CMB custom field to allow for the selection of multiple posts
* attached to a single page
*/
function cmb2_attached_posts_fields_render( $field, $escaped_value, $object_id, $object_type, $field_type ) {
// Setup our args
$args = wp_parse_args( (array) $field->options( 'query_args' ), array(
'post_type' => 'post',
'posts_per_page' => 100,
'orderby' => 'name',
'order' => 'ASC',
) );
// Get post type object for attached post type
$attached_post_type = get_post_type_object( $args['post_type'] );
// Get our posts
$posts = get_posts( $args );
// If there are no posts found, just stop
if ( ! $posts ) {
return;
}
// Check to see if we have any meta values saved yet
$attached = (array) $escaped_value;
// Set our count class
$count = 0;
// Wrap our lists
echo '<div class="attached-posts-wrap widefat" data-fieldname="'. $field_type->_name() .'">';
// Open our retrieved, or found posts, list
echo '<div class="retrieved-wrap column-wrap">';
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Available %s', 'cmb' ), $attached_post_type->labels->name ) . '</h4>';
echo '<ul class="retrieved connected">';
// Loop through our posts as list items
foreach ( $posts as $post ) {
// Increase our count
$count++;
// Set our zebra stripes
$zebra = $count % 2 == 0 ? 'even' : 'odd';
// Set a class if our post is in our attached post meta
$added = ! empty ( $attached ) && in_array( $post->ID, $attached ) ? ' added' : '';
// Build our list item
echo '<li data-id="', $post->ID ,'" class="' . $zebra . $added . '"><a title="'. __( 'Edit' ) .'" href="', get_edit_post_link( $post->ID ) ,'">', $post->post_title ,'</a><span class="dashicons dashicons-plus add-remove"></span></li>';
}
// Close our retrieved, or found, posts
echo '</ul><!-- .retrieved -->';
echo '</div><!-- .retrieved-wrap -->';
// Open our attached posts list
echo '<div class="attached-wrap column-wrap">';
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Attached %s', 'cmb' ), $attached_post_type->labels->name ) . '</h4>';
echo '<ul class="attached connected">';
// If we have any posts saved already, display them
$post_ids = cmb2_attached_posts_fields_display_attached( $field, $attached );
$value = ! empty( $post_ids ) ? implode( ',', $post_ids ) : '';
// Close up shop
echo '</ul><!-- #attached -->';
echo '</div><!-- .attached-wrap -->';
echo $field_type->input( array(
'type' => 'hidden',
'class' => 'attached-posts-ids',
'value' => $value,
'desc' => '',
) );
echo '</div><!-- .attached-posts-wrap -->';
// Display our description if one exists
$field_type->_desc( true, true );
}
add_action( 'cmb2_render_custom_attached_posts', 'cmb2_attached_posts_fields_render', 10, 5 );
function cmb2_attached_posts_fields_sanitize( $sanitized_val, $val ) {
if ( ! empty( $val ) ) {
return explode( ',', $val );
}
return $sanitized_val;
}
add_action( 'cmb2_sanitize_custom_attached_posts', 'cmb2_attached_posts_fields_sanitize', 10, 2 );
/**
* Helper function to grab and filter our post meta
*/
function cmb2_attached_posts_fields_display_attached( $field, $attached ) {
// Start with nothing
$output = '';
// If we do, then we need to display them as items in our attached list
if ( ! $attached ) {
return;
}
// Set our count to zero
$count = 0;
// Remove any empty values
$attached = array_filter( $attached );
$post_ids = array();
// Loop through and build our existing display items
foreach ( $attached as $post_id ) {
if ( ! get_post( $post_id ) ) {
continue;
}
// Increase our count
$count++;
// Set our zebra stripes
$zebra = $count % 2 == 0 ? 'even' : 'odd';
// Build our list item
echo '<li data-id="' . $post_id . '" class="' . $zebra . '"><a title="'. __( 'Edit' ) .'" href="', get_edit_post_link( $post_id ) ,'">'. get_the_title( $post_id ) .'</a><span class="dashicons dashicons-minus add-remove"></span></li>';
$post_ids[] = $post_id;
}
return $post_ids;
}