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
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;
// Make sure "select-advanced" field is loaded
require_once WCQD_METABOX_FIELDS_DIR . 'select-advanced.php';
if ( ! class_exists( 'WCQD_METABOX_Post_Field' ) )
{
class WCQD_METABOX_Post_Field extends WCQD_METABOX_Select_Advanced_Field
{
/**
* Enqueue scripts and styles
*
* @return void
*/
static function admin_enqueue_scripts()
{
WCQD_METABOX_Select_Field::admin_enqueue_scripts();
WCQD_METABOX_Select_Advanced_Field::admin_enqueue_scripts();
}
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$field['options'] = self::get_options( $field );
switch ( $field['field_type'] )
{
case 'select':
return WCQD_METABOX_Select_Field::html( $meta, $field );
case 'select_advanced':
default:
return WCQD_METABOX_Select_Advanced_Field::html( $meta, $field );
}
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
$field = wp_parse_args( $field, array(
'post_type' => 'post',
'field_type' => 'select_advanced',
'parent' => false,
'query_args' => array(),
) );
/**
* Set default placeholder
* - If multiple post types: show 'Select a post'
* - If single post type: show 'Select a %post_type_name%'
*/
if ( empty( $field['placeholder'] ) )
{
$label = __( 'Select a post', 'meta-box' );
if ( is_string( $field['post_type'] ) && post_type_exists( $field['post_type'] ) )
{
$post_type_object = get_post_type_object( $field['post_type'] );
$label = sprintf( __( 'Select a %s', 'meta-box' ), $post_type_object->labels->singular_name );
}
$field['placeholder'] = $label;
}
if ( $field['parent'] )
{
$field['multiple'] = false;
$field['field_name'] = 'parent_id';
}
$field['query_args'] = wp_parse_args( $field['query_args'], array(
'post_type' => $field['post_type'],
'post_status' => 'publish',
'posts_per_page' => - 1,
) );
switch ( $field['field_type'] )
{
case 'select':
return WCQD_METABOX_Select_Field::normalize_field( $field );
break;
case 'select_advanced':
default:
return WCQD_METABOX_Select_Advanced_Field::normalize_field( $field );
}
}
/**
* Get meta value
* If field is cloneable, value is saved as a single entry in DB
* Otherwise value is saved as multiple entries (for backward compatibility)
*
* @see "save" method for better understanding
*
* @param $post_id
* @param $saved
* @param $field
*
* @return array
*/
static function meta( $post_id, $saved, $field )
{
if ( isset( $field['parent'] ) && $field['parent'] )
{
$post = get_post( $post_id );
return $post->post_parent;
}
return parent::meta( $post_id, $saved, $field );
}
/**
* Get posts
*
* @param array $field
*
* @return array
*/
static function get_options( $field )
{
$options = array();
$query = new WP_Query( $field['query_args'] );
if ( $query->have_posts() )
{
while ( $query->have_posts() )
{
$post = $query->next_post();
$options[$post->ID] = $post->post_title;
}
}
return $options;
}
/**
* Get post link to display in the frontend
*
* @param int $value Option value, e.g. post ID
* @param int $index Array index
* @param array $field Field parameter
*
* @return string
*/
static function get_option_label( &$value, $index, $field )
{
$value = sprintf(
'<a href="%s" title="%s">%s</a>',
esc_url( get_permalink( $value ) ),
the_title_attribute( array(
'post' => $value,
'echo' => false,
) ),
get_the_title( $value )
);
}
}
}