ld_course.php
3.93 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
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
<?php
/**
* LearnDash `Courses` Widget Class.
*
* @since 2.1.0
* @package LearnDash\Widgets
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'Course_Widget' ) ) && ( class_exists( 'WP_Widget' ) ) ) {
/**
* Class for LearnDash `Courses` Widget.
*
* @since 2.1.0
* @uses WP_Widget
*/
class Course_Widget extends WP_Widget /* phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound */ {
/**
* Post type
*
* @var string $post_type.
*/
protected $post_type = 'sfwd-courses';
/**
* Post name
*
* @var string $post_name.
*/
protected $post_name = 'Course';
/**
* Post arguments
*
* @var object $post_args.
*/
protected $post_args;
/**
* Public constructor for Widget Class.
*
* @since 2.1.0
*/
public function __construct() {
$args = array();
$this->post_name = LearnDash_Custom_Label::get_label( 'course' );
if ( empty( $args['description'] ) ) {
// translators: placeholder: Course.
$args['description'] = sprintf( esc_html_x( 'Displays a list of %s', 'placeholder: Course', 'learndash' ), $this->post_name );
}
if ( empty( $this->post_args ) ) {
$this->post_args = array(
'post_type' => $this->post_type,
'numberposts' => -1,
'order' => 'DESC',
'orderby' => 'date',
);
}
parent::__construct( "{$this->post_type}-widget", $this->post_name, $args );
}
/**
* Displays widget
*
* @since 2.1.0
*
* @param array $args widget arguments.
* @param array $instance widget instance.
*/
public function widget( $args, $instance ) {
global $learndash_shortcode_used;
extract( $args, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
/* Before Widget content */
$buf = $before_widget;
/** This filter is documented in https://developer.wordpress.org/reference/hooks/widget_title/ */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
if ( ! empty( $title ) ) {
$buf .= $before_title . $title . $after_title;
}
$buf .= '<ul>';
/* Display Widget Data */
$args = $this->post_args;
$args['posts_per_page'] = $args['numberposts'];
$args['wrapper'] = 'li';
global $shortcode_tags, $post;
if ( ! empty( $shortcode_tags[ $this->post_type ] ) ) {
$buf .= call_user_func( $shortcode_tags[ $this->post_type ], $args, null, $this->post_type );
}
/* After Widget content */
$buf .= '</ul>' . $after_widget;
echo $buf; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML.
$learndash_shortcode_used = true;
}
/**
* Handles widget updates in admin
*
* @since 2.1.0
*
* @param array $new_instance New instance.
* @param array $old_instance Old instance.
*
* @return array $instance
*/
public function update( $new_instance, $old_instance ) {
/* Updates widget title value */
$instance = $old_instance;
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
return $instance;
}
/**
* Display widget form in admin
*
* @since 2.1.0
*
* @param array $instance widget instance.
*
* @return string Default return is 'noform'.
*/
public function form( $instance ) {
if ( $instance ) {
$title = esc_attr( $instance['title'] );
} else {
$title = $this->post_name;
}
learndash_replace_widgets_alert();
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'learndash' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
return '';
}
}
add_action(
'widgets_init',
function() {
return register_widget( 'Course_Widget' );
}
);
}