1
|
|
|
<?php
|
|
|
|
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* Sensei Lesson Component Widget
|
6
|
|
|
*
|
7
|
|
|
* A WooThemes standardized component widget.
|
8
|
|
|
*
|
9
|
|
|
* @package Views
|
10
|
|
|
* @subpackage Widgets
|
11
|
|
|
* @author Automattic
|
12
|
|
|
*
|
13
|
|
|
* @since 1.0.0
|
14
|
|
|
*/
|
15
|
|
|
class WooThemes_Sensei_Lesson_Component_Widget extends WP_Widget {
|
16
|
|
|
protected $woo_widget_cssclass;
|
17
|
|
|
protected $woo_widget_description;
|
18
|
|
|
protected $woo_widget_idbase;
|
19
|
|
|
protected $woo_widget_title;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Constructor function.
|
23
|
|
|
* @since 1.0.0
|
24
|
|
|
* @return void
|
|
|
|
|
25
|
|
|
*/
|
26
|
|
|
public function __construct() {
|
27
|
|
|
/* Widget variable settings. */
|
28
|
|
|
$this->woo_widget_cssclass = 'widget_sensei_lesson_component';
|
29
|
|
|
$this->woo_widget_description = __( 'This widget will output a list of the latest Lessons.', 'woothemes-sensei' );
|
30
|
|
|
$this->woo_widget_idbase = 'sensei_lesson_component';
|
31
|
|
|
$this->woo_widget_title = __( 'Sensei - Lesson Component', 'woothemes-sensei' );
|
32
|
|
|
|
33
|
|
|
$this->woo_widget_componentslist = array(
|
34
|
|
|
'newlessons' => __( 'New Lessons', 'woothemes-sensei' ),
|
35
|
|
|
);
|
36
|
|
|
|
37
|
|
|
/* Widget settings. */
|
38
|
|
|
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
|
39
|
|
|
|
40
|
|
|
/* Widget control settings. */
|
41
|
|
|
$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => $this->woo_widget_idbase );
|
42
|
|
|
|
43
|
|
|
/* Create the widget. */
|
44
|
|
|
parent::__construct( $this->woo_widget_idbase, $this->woo_widget_title, $widget_ops, $control_ops );
|
45
|
|
|
} // End __construct()
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* Display the widget on the frontend.
|
49
|
|
|
* @since 1.0.0
|
50
|
|
|
* @param array $args Widget arguments.
|
51
|
|
|
* @param array $instance Widget settings for this instance.
|
52
|
|
|
* @return void
|
53
|
|
|
*/
|
54
|
|
|
public function widget( $args, $instance ) {
|
55
|
|
|
|
56
|
|
|
$before_widget = $args[ 'before_widget' ];
|
57
|
|
|
$before_title = $args[ 'before_title' ];
|
58
|
|
|
$after_title = $args[ 'after_title' ];
|
59
|
|
|
$after_widget = $args[ 'after_widget' ];
|
60
|
|
|
|
61
|
|
|
if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) && ( 'activecourses' == $instance['component'] || 'completedcourses' == $instance['component'] ) && !is_user_logged_in() ) {
|
|
|
|
|
62
|
|
|
// No Output
|
63
|
|
|
} else {
|
64
|
|
|
/* Our variables from the widget settings. */
|
65
|
|
|
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
|
66
|
|
|
|
67
|
|
|
/* Before widget (defined by themes). */
|
68
|
|
|
echo $before_widget;
|
69
|
|
|
|
70
|
|
|
/* Display the widget title if one was input (before and after defined by themes). */
|
71
|
|
|
if ( $title ) { echo $before_title . $title . $after_title; }
|
72
|
|
|
|
73
|
|
|
/* Widget content. */
|
74
|
|
|
// Add actions for plugins/themes to hook onto.
|
75
|
|
|
do_action( $this->woo_widget_cssclass . '_top' );
|
76
|
|
|
|
77
|
|
|
if ( in_array( $instance['component'], array_keys( $this->woo_widget_componentslist ) ) ) {
|
78
|
|
|
$this->load_component( $instance );
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
// Add actions for plugins/themes to hook onto.
|
82
|
|
|
do_action( $this->woo_widget_cssclass . '_bottom' );
|
83
|
|
|
|
84
|
|
|
/* After widget (defined by themes). */
|
85
|
|
|
echo $after_widget;
|
86
|
|
|
} // End If Statement
|
87
|
|
|
|
88
|
|
|
} // End widget()
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* Method to update the settings from the form() method.
|
92
|
|
|
* @since 1.0.0
|
93
|
|
|
* @param array $new_instance New settings.
|
94
|
|
|
* @param array $old_instance Previous settings.
|
95
|
|
|
* @return array Updated settings.
|
96
|
|
|
*/
|
97
|
|
View Code Duplication |
public function update ( $new_instance, $old_instance ) {
|
|
|
|
|
98
|
|
|
$instance = $old_instance;
|
99
|
|
|
|
100
|
|
|
/* Strip tags for title and name to remove HTML (important for text inputs). */
|
101
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] );
|
102
|
|
|
|
103
|
|
|
/* The select box is returning a text value, so we escape it. */
|
104
|
|
|
$instance['component'] = esc_attr( $new_instance['component'] );
|
105
|
|
|
|
106
|
|
|
/* The select box is returning a text value, so we escape it. */
|
107
|
|
|
$instance['limit'] = esc_attr( $new_instance['limit'] );
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
return $instance;
|
111
|
|
|
} // End update()
|
112
|
|
|
|
113
|
|
|
/**
|
114
|
|
|
* The form on the widget control in the widget administration area.
|
115
|
|
|
* Make use of the get_field_id() and get_field_name() function when creating your form elements. This handles the confusing stuff.
|
116
|
|
|
* @since 1.0.0
|
117
|
|
|
* @param array $instance The settings for this instance.
|
118
|
|
|
* @return void
|
119
|
|
|
*/
|
120
|
|
View Code Duplication |
public function form( $instance ) {
|
|
|
|
|
121
|
|
|
|
122
|
|
|
/* Set up some default widget settings. */
|
123
|
|
|
/* Make sure all keys are added here, even with empty string values. */
|
124
|
|
|
$defaults = array(
|
125
|
|
|
'title' => '',
|
126
|
|
|
'component' => '',
|
127
|
|
|
'limit' => 3
|
128
|
|
|
);
|
129
|
|
|
|
130
|
|
|
$instance = wp_parse_args( (array) $instance, $defaults );
|
131
|
|
|
?>
|
132
|
|
|
<!-- Widget Title: Text Input -->
|
133
|
|
|
<p>
|
134
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title (optional):', 'woothemes-sensei' ); ?></label>
|
135
|
|
|
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" />
|
136
|
|
|
</p>
|
137
|
|
|
<!-- Widget Component: Select Input -->
|
138
|
|
|
<p>
|
139
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>"><?php _e( 'Component:', 'woothemes-sensei' ); ?></label>
|
140
|
|
|
<select name="<?php echo esc_attr( $this->get_field_name( 'component' ) ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'component' ) ); ?>">
|
141
|
|
|
<?php foreach ( $this->woo_widget_componentslist as $k => $v ) { ?>
|
142
|
|
|
<option value="<?php echo esc_attr( $k ); ?>"<?php selected( $instance['component'], $k ); ?>><?php echo $v; ?></option>
|
143
|
|
|
<?php } ?>
|
144
|
|
|
</select>
|
145
|
|
|
</p>
|
146
|
|
|
<!-- Widget Limit: Text Input -->
|
147
|
|
|
<p>
|
148
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Number of Lessons (optional):', 'woothemes-sensei' ); ?></label>
|
149
|
|
|
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" value="<?php echo esc_attr( $instance['limit'] ); ?>" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>" />
|
150
|
|
|
</p>
|
151
|
|
|
|
152
|
|
|
<?php
|
153
|
|
|
} // End form()
|
154
|
|
|
|
155
|
|
|
/**
|
156
|
|
|
* Load the desired component, if a method is available for it.
|
157
|
|
|
* @param string $instance The component to potentially be loaded.
|
158
|
|
|
* @since 5.0.8
|
159
|
|
|
* @return void
|
160
|
|
|
*/
|
161
|
|
|
protected function load_component ( $instance ) {
|
162
|
|
|
|
163
|
|
|
global $current_user;
|
164
|
|
|
|
165
|
|
|
/*
|
166
|
|
|
newlessons
|
167
|
|
|
*/
|
168
|
|
|
$posts_array = array();
|
|
|
|
|
169
|
|
|
|
170
|
|
|
$post_args = array( 'post_type' => 'lesson',
|
171
|
|
|
'posts_per_page' => intval( $instance[ 'limit' ] ),
|
172
|
|
|
'orderby' => 'menu_order date',
|
173
|
|
|
'order' => 'DESC',
|
174
|
|
|
'post_status' => 'publish',
|
175
|
|
|
'suppress_filters' => 0
|
176
|
|
|
);
|
177
|
|
|
$posts_array = get_posts( $post_args );
|
178
|
|
|
|
179
|
|
|
if ( count( $posts_array ) > 0 ) { ?>
|
180
|
|
|
<ul>
|
181
|
|
|
<?php foreach ($posts_array as $post_item){
|
182
|
|
|
$post_id = absint( $post_item->ID );
|
183
|
|
|
$post_title = $post_item->post_title;
|
184
|
|
|
$user_info = get_userdata( absint( $post_item->post_author ) );
|
185
|
|
|
$author_link = get_author_posts_url( absint( $post_item->post_author ) );
|
186
|
|
|
$author_display_name = $user_info->display_name;
|
187
|
|
|
$author_id = $post_item->post_author;
|
|
|
|
|
188
|
|
|
$lesson_course_id = get_post_meta( $post_id, '_lesson_course', true );
|
189
|
|
|
?>
|
190
|
|
|
<li class="fix">
|
191
|
|
|
<?php do_action( 'sensei_lesson_image', $post_id, '100', '100', false, true ); ?>
|
192
|
|
|
<a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>" title="<?php echo esc_attr( $post_title ); ?>"><?php echo $post_title; ?></a>
|
193
|
|
|
<br />
|
194
|
|
|
<?php if ( isset( Sensei()->settings->settings[ 'lesson_author' ] ) && ( Sensei()->settings->settings[ 'lesson_author' ] ) ) { ?>
|
195
|
|
|
<span class="course-author"><?php _e( 'by ', 'woothemes-sensei' ); ?><a href="<?php echo esc_url( $author_link ); ?>" title="<?php echo esc_attr( $author_display_name ); ?>"><?php echo esc_html( $author_display_name ); ?></a></span>
|
196
|
|
|
<br />
|
197
|
|
|
<?php } // End If Statement ?>
|
198
|
|
|
<?php if ( 0 < $lesson_course_id ) { ?>
|
199
|
|
|
<span class="lesson-course"><?php echo ' ' . sprintf( __( 'Part of: %s', 'woothemes-sensei' ), '<a href="' . esc_url( get_permalink( $lesson_course_id ) ) . '" title="' . esc_attr( __( 'View course', 'woothemes-sensei' ) ) . '"><em>' . get_the_title( $lesson_course_id ) . '</em></a>' ); ?></span>
|
200
|
|
|
<?php } ?>
|
201
|
|
|
<br />
|
202
|
|
|
</li>
|
203
|
|
|
<?php } // End For Loop ?>
|
204
|
|
|
<?php echo '<li class="my-account fix"><a class="button" href="'. esc_url( get_post_type_archive_link( 'lesson' ) ) .'">'.__('More Lessons', 'woothemes-sensei').'</a></li>'; ?>
|
205
|
|
|
</ul>
|
206
|
|
|
<?php } // End If Statement
|
207
|
|
|
} // End load_component()
|
208
|
|
|
} // End Class |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.