forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-option-select-categories.php
More file actions
122 lines (106 loc) · 3.43 KB
/
class-option-select-categories.php
File metadata and controls
122 lines (106 loc) · 3.43 KB
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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionSelectCategories extends TitanFrameworkOption {
public $defaultSecondarySettings = array(
'default' => '0', // show this when blank
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'category',
'hide_empty' => false,
'show_count' => false,
);
/*
* Display for options and meta
*/
public function display() {
$this->echoOptionHeader();
$args = array(
'orderby' => $this->settings['orderby'],
'order' => $this->settings['order'],
'taxonomy' => $this->settings['taxonomy'],
'hide_empty' => $this->settings['hide_empty'] ? '1' : '0',
);
$categories = get_categories( $args );
echo "<select name='" . esc_attr( $this->getID() ) . "'>";
// The default value (nothing is selected)
printf( "<option value='%s' %s>%s</option>",
'0',
selected( $this->getValue(), '0', false ),
"— " . __( "Select", TF_I18NDOMAIN ) . " —"
);
// Print all the other pages
foreach ( $categories as $category ) {
printf( "<option value='%s' %s>%s</option>",
esc_attr( $category->term_id ),
selected( $this->getValue(), $category->term_id, false ),
$category->name . ( $this->settings['show_count'] ? " (" . $category->count . ")" : '' )
);
}
echo "</select>";
$this->echoOptionFooter();
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionSelectCategoriesControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->settings['id'],
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'orderby' => $this->settings['orderby'],
'order' => $this->settings['order'],
'taxonomy' => $this->settings['taxonomy'],
'hide_empty' => $this->settings['hide_empty'],
'show_count' => $this->settings['show_count'],
'priority' => $priority,
) ) );
}
}
/*
* WP_Customize_Control with description
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionSelectCategoriesControl', 1 );
function registerTitanFrameworkOptionSelectCategoriesControl() {
class TitanFrameworkOptionSelectCategoriesControl extends WP_Customize_Control {
public $description;
public $orderby;
public $order;
public $taxonomy;
public $hide_empty;
public $show_count;
public function render_content() {
$args = array(
'orderby' => $this->orderby,
'order' => $this->order,
'taxonomy' => $this->taxonomy,
'hide_empty' => $this->hide_empty ? '1' : '0',
);
$categories = get_categories( $args );
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
// The default value (nothing is selected)
printf( "<option value='%s' %s>%s</option>",
'0',
selected( $this->value(), '0', false ),
"— " . __( "Select", TF_I18NDOMAIN ) . " —"
);
// Print all the other pages
foreach ( $categories as $category ) {
printf( "<option value='%s' %s>%s</option>",
esc_attr( $category->term_id ),
selected( $this->value(), $category->term_id, false ),
$category->name . ( $this->show_count ? " (" . $category->count . ")" : '' )
);
}
?>
</select>
</label>
<?php
echo "<p class='description'>{$this->description}</p>";
}
}
}