/
class-select.php
103 lines (88 loc) · 2.5 KB
/
class-select.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
<?php
/**
* AnsPress Select type field object.
*
* @package AnsPress
* @subpackage Fields
* @since 4.1.0
* @author Rahul Aryan<support@anspress.net>
* @copyright Copyright (c) 2017, Rahul Aryan
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
*/
namespace AnsPress\Form\Field;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The Select type field object.
*
* @since 4.1.0
*/
class Select extends \AnsPress\Form\Field {
/**
* The field type.
*
* @var string
*/
public $type = 'select';
/**
* Prepare field.
*
* @return void
*/
protected function prepare() {
$this->args = wp_parse_args(
$this->args,
array(
'label' => __( 'AnsPress Select Field', 'anspress-question-answer' ),
'options' => array(),
'terms_args' => array(
'taxonomy' => 'question_category',
'hide_empty' => false,
'fields' => 'id=>name',
'orderby' => ap_opt( 'form_category_orderby' ) ? ap_opt( 'form_category_orderby' ) : 'count',
),
)
);
// Call parent prepare().
parent::prepare();
if ( in_array( $this->get( 'options' ), array( 'posts', 'terms' ), true ) ) {
$this->sanitize_cb = array_merge( array( 'absint' ), $this->sanitize_cb );
} else {
$this->sanitize_cb = array_merge( array( 'text_field' ), $this->sanitize_cb );
}
}
/**
* Return options of a select field.
*
* @return array
*/
private function get_options() {
$options = $this->get( 'options' );
if ( is_string( $options ) && 'terms' === $options ) {
return get_terms( $this->get( 'terms_args', array() ) );
}
if ( is_string( $options ) && 'posts' === $options ) {
return wp_list_pluck( get_posts( $this->get( 'posts_args', array() ) ), 'post_title', 'ID' );
}
return (array) $options;
}
/**
* Field markup.
*
* @return void
*/
public function field_markup() {
parent::field_markup();
$this->add_html( '<select' . $this->common_attr() . $this->custom_attr() . '>' );
$this->add_html( '<option value="">' . __( 'Select an option', 'anspress-question-answer' ) . '</option>' );
foreach ( (array) $this->get_options() as $val => $label ) {
$selected = selected( $this->value(), $val, false );
$this->add_html( '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_html( $label ) . '</option>' );
}
$this->add_html( '</select>' );
/** This action is documented in lib/form/class-input.php */
do_action_ref_array( 'ap_after_field_markup', array( &$this ) );
}
}