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
|
<?php
/**
* A multi-checkbox Customizer control for use with Jetpack Search configuration
*
* @package @automattic/jetpack-search
*/
namespace Automattic\Jetpack\Search;
use Automattic\Jetpack\Assets;
use WP_Customize_Control;
if ( ! class_exists( 'WP_Customize_Control' ) ) {
return;
}
/**
* Label Control class.
*/
class Excluded_Post_Types_Control extends WP_Customize_Control {
/**
* Control type.
*
* @since 8.8.0
* @var string
*/
public $type = 'excluded-post-types';
/**
* Enqueue styles related to this control.
*/
public function enqueue() {
Assets::register_script(
'jetpack-instant-search-customizer-excluded-post-types',
'class-excluded-post-types-control.js',
__FILE__,
array(
'css_path' => 'class-excluded-post-types-control.css',
'dependencies' => array( 'customize-controls' ),
'in_footer' => true,
'textdomain' => 'jetpack-search-pkg',
)
);
Assets::enqueue_script( 'jetpack-instant-search-customizer-excluded-post-types' );
}
/**
* Checks if the post type has been selected.
*
* @since 8.8.0
* @return array $post_types An array of strings representing post type names.
*/
public function get_arrayed_value() {
return explode( ',', $this->value() );
}
/**
* Generates a customizer settings ID for a given post type.
*
* @since 8.8.0
* @param object $post_type Post type object returned from get_post_types.
* @return string $customizer_id Customizer setting ID.
*/
public function generate_post_type_customizer_id( $post_type ) {
return '_customize-post-type-input-' . $post_type->name;
}
/**
* Checks if the post type has been selected.
*
* @since 8.8.0
* @param object $post_type Post type object returned from get_post_types.
* @return array $ids Post type => post type customizer ID object.
*/
public function is_checked( $post_type ) {
return in_array( $post_type->name, $this->get_arrayed_value(), true );
}
/**
* Override rendering for custom class name; omit element ID.
*/
protected function render() {
$id = 'customize-control-' . str_replace( array( '[', ']' ), array( '-', '' ), $this->id );
$class = 'customize-control customize-control-excluded-post-types';
printf( '<li id="%s" class="%s">', esc_attr( $id ), esc_attr( $class ) );
$this->render_content();
echo '</li>';
}
/**
* Override content rendering.
*/
protected function render_content() {
$post_types = get_post_types( array( 'exclude_from_search' => false ), 'objects' );
if ( count( $post_types ) === 0 ) {
return;
}
?>
<label class="customize-control-title">
<?php echo esc_html( $this->label ); ?>
</label>
<?php if ( ! empty( $this->description ) ) : ?>
<span class="description customize-control-description">
<?php echo esc_html( $this->description ); ?>
</span>
<?php endif ?>
<input
class="customize-control-excluded-post-types"
id="<?php echo esc_attr( $this->id ); ?>"
name="<?php echo esc_attr( $this->id ); ?>"
type="hidden"
value="<?php echo esc_attr( $this->value() ); ?>"
<?php $this->link(); ?>
/>
<?php
$is_only_one_unchecked = ( count( $post_types ) - 1 ) === count( $this->get_arrayed_value() );
foreach ( $post_types as $post_type ) {
$input_id = Helper::generate_post_type_customizer_id( $post_type );
?>
<div class="customize-control-excluded-post-type-checkbox-container">
<input
class="customize-control-excluded-post-type-checkbox"
id="<?php echo esc_attr( $input_id ); ?>"
type="checkbox"
value="<?php echo esc_attr( $post_type->name ); ?>"
<?php checked( $this->is_checked( $post_type ) ); ?>
<?php disabled( ! $this->is_checked( $post_type ) && $is_only_one_unchecked ); ?>
/>
<label for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_html( $post_type->label ); ?></label>
</div>
<?php
}
}
}
|