forked from gambitph/Titan-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-option-select-posts.php
124 lines (108 loc) · 3.26 KB
/
class-option-select-posts.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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class TitanFrameworkOptionSelectPosts extends TitanFrameworkOption {
public $defaultSecondarySettings = array(
'default' => '0', // show this when blank
'post_type' => 'post',
'num' => -1,
'post_status' => 'any',
'orderby' => 'post_date',
'order' => 'DESC',
);
/*
* Display for options and meta
*/
public function display() {
$this->echoOptionHeader();
$args = array(
'post_type' => $this->settings['post_type'],
'posts_per_page' => $this->settings['num'],
'post_status' => $this->settings['post_status'],
'orderby' => $this->settings['orderby'],
'order' => $this->settings['order'],
);
$posts = get_posts( $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 ( $posts as $post ) {
printf( "<option value='%s' %s>%s</option>",
esc_attr( $post->ID ),
selected( $this->getValue(), $post->ID, false ),
$post->post_title
);
}
echo "</select>";
$this->echoOptionFooter();
}
/*
* Display for theme customizer
*/
public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) {
$wp_customize->add_control( new TitanFrameworkOptionSelectPostsControl( $wp_customize, $this->getID(), array(
'label' => $this->settings['name'],
'section' => $section->settings['id'],
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'post_type' => $this->settings['post_type'],
'posts_per_page' => $this->settings['num'],
'post_status' => $this->settings['post_status'],
'orderby' => $this->settings['orderby'],
'order' => $this->settings['order'],
'priority' => $priority,
) ) );
}
}
/*
* WP_Customize_Control with description
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionSelectPostsControl', 1 );
function registerTitanFrameworkOptionSelectPostsControl() {
class TitanFrameworkOptionSelectPostsControl extends WP_Customize_Control {
public $description;
public $post_type;
public $num;
public $post_status;
public $orderby;
public $order;
public function render_content() {
$args = array(
'post_type' => $this->post_type,
'posts_per_page' => $this->num,
'post_status' => $this->post_status,
'orderby' => $this->orderby,
'order' => $this->order,
);
$posts = get_posts( $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 ( $posts as $post ) {
printf( "<option value='%s' %s>%s</option>",
esc_attr( $post->ID ),
selected( $this->value(), $post->ID, false ),
$post->post_title
);
}
?>
</select>
</label>
<?php
echo "<p class='description'>{$this->description}</p>";
}
}
}