| 1 |
<?php |
| 2 |
|
| 3 |
// The Widget |
| 4 |
Class WPC_New_Stuff_Widget extends WP_Widget { |
| 5 |
function __construct(){ |
| 6 |
parent::__construct( |
| 7 |
// Base ID |
| 8 |
'WPC_New_Stuff_Widget', |
| 9 |
__('WP Courses Widget', 'wp-courses'), |
| 10 |
// Widget description |
| 11 |
array( 'description' => __('Displays a list of specific courses, lessons or teachers', 'wp-courses') ) |
| 12 |
); |
| 13 |
} |
| 14 |
public function widget($args, $instance){ |
| 15 |
|
| 16 |
$title = apply_filters('widget_title', $instance['title']); |
| 17 |
|
| 18 |
echo $args['before_widget']; |
| 19 |
|
| 20 |
if(!empty($title)){ |
| 21 |
echo $args['before_title'] . esc_textarea( $title ) . $args['after_title']; |
| 22 |
} |
| 23 |
|
| 24 |
$query_args = array( |
| 25 |
'posts_per_page' => $instance['posts_per_page'], |
| 26 |
'post_type' => $instance['post_type'], |
| 27 |
'orderby' => $instance['orderby'], |
| 28 |
'order' => $instance['order'], |
| 29 |
'post_status' => 'publish', |
| 30 |
); |
| 31 |
|
| 32 |
|
| 33 |
$query = new WP_Query($query_args); |
| 34 |
echo '<ul class="wpc-widget-ul">'; |
| 35 |
|
| 36 |
if($query->have_posts()){ |
| 37 |
|
| 38 |
$logged_in = is_user_logged_in(); |
| 39 |
$user_id = get_current_user_id(); |
| 40 |
$viewed_tracking = wpc_get_tracked_lessons_by_user($user_id, 0); |
| 41 |
$completed_tracking = wpc_get_tracked_lessons_by_user($user_id, 1); |
| 42 |
|
| 43 |
while($query->have_posts()){ |
| 44 |
|
| 45 |
$query->the_post(); |
| 46 |
|
| 47 |
$class = ''; |
| 48 |
$icon = ''; |
| 49 |
|
| 50 |
if($instance['post_type'] == 'lesson'){ |
| 51 |
$pid = get_the_ID(); |
| 52 |
|
| 53 |
if( wpc_has_done( $pid, $viewed_tracking ) ) { |
| 54 |
$icon = '<i class="fa-regular fa-square"></i>'; |
| 55 |
} |
| 56 |
|
| 57 |
if( wpc_has_done( $pid, $completed_tracking) ) { |
| 58 |
$icon = '<i class="fa-regular fa-square-check"></i>'; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$allowed = array( |
| 63 |
'i' => array( |
| 64 |
'class' => array() |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
echo '<li class="wpc-widget-li"><a href="'. esc_url(get_the_permalink()) . '" class="' . esc_attr($class) . '">' . wp_kses($icon, $allowed) . ' ' . esc_html(get_the_title()) . '</a></li>'; |
| 69 |
} |
| 70 |
} |
| 71 |
echo '</ul>'; |
| 72 |
wp_reset_postdata(); |
| 73 |
echo $args['after_widget']; |
| 74 |
|
| 75 |
} |
| 76 |
// Widget Backend |
| 77 |
public function form( $instance ) { |
| 78 |
|
| 79 |
if ( isset( $instance[ 'title' ] ) ) { |
| 80 |
$title = $instance[ 'title' ]; |
| 81 |
} |
| 82 |
else { |
| 83 |
$title = __( 'New title', 'wpb_widget_domain' ); |
| 84 |
} |
| 85 |
|
| 86 |
if( isset( $instance['posts_per_page'])) { |
| 87 |
$posts_per_page = $instance['posts_per_page']; |
| 88 |
} else { |
| 89 |
$posts_per_page = 10; |
| 90 |
} |
| 91 |
|
| 92 |
if( isset( $instance['post_type'])) { |
| 93 |
$post_type = $instance['post_type']; |
| 94 |
} else { |
| 95 |
$post_type = 'course'; |
| 96 |
} |
| 97 |
|
| 98 |
if(isset($instance['order'])){ |
| 99 |
$order = $instance['order']; |
| 100 |
} else { |
| 101 |
$order = 'ASC'; |
| 102 |
} |
| 103 |
|
| 104 |
if(isset($instance['orderby'])){ |
| 105 |
$orderby = $instance['orderby']; |
| 106 |
} else { |
| 107 |
$orderby = 'none'; |
| 108 |
} |
| 109 |
|
| 110 |
if(isset($instance['course_id'])){ |
| 111 |
$course_id = $instance['course_id']; |
| 112 |
} else { |
| 113 |
$course_id = 'none'; |
| 114 |
} |
| 115 |
|
| 116 |
// Widget admin form |
| 117 |
?> |
| 118 |
|
| 119 |
<p> |
| 120 |
<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"><?php esc_html_e( 'Title:', 'wp-courses' ); ?></label> |
| 121 |
<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 ); ?>" /> |
| 122 |
<label for="<?php echo esc_attr($this->get_field_id( 'posts_per_page' )); ?>"><?php esc_html_e( 'Number of Lessons, Courses or Teachers to Show:', 'wp-courses' ); ?></label> |
| 123 |
<input class="widefat" id="<?php echo esc_attr($this->get_field_id( 'posts_per_page' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'posts_per_page' )); ?>" type="number" value="<?php echo esc_attr( $posts_per_page ); ?>" /> |
| 124 |
|
| 125 |
<?php |
| 126 |
|
| 127 |
$style = ($post_type != 'lesson') ? 'display: none;' : ''; |
| 128 |
|
| 129 |
?> |
| 130 |
|
| 131 |
<label for="<?php echo esc_attr($this->get_field_id( 'post_type' )); ?>"><?php esc_html_e( 'Post Type:', 'wp-courses' ); ?></label> |
| 132 |
<select class="widefat wpc-widget-post-type-select" id="<?php echo esc_attr($this->get_field_id( 'post_type' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'post_type' )); ?>"> |
| 133 |
<option value="course" <?php selected(esc_attr($post_type), 'course'); ?>><?php esc_html_e('Course', 'wp-courses'); ?></option> |
| 134 |
<option value="lesson" <?php selected(esc_attr($post_type), 'lesson'); ?>><?php esc_html_e('Lesson', 'wp-courses'); ?></option> |
| 135 |
<option value="teacher" <?php selected(esc_attr($post_type), 'teacher'); ?>><?php esc_html_e('Teacher', 'wp-courses'); ?></option> |
| 136 |
</select> |
| 137 |
|
| 138 |
<label for="<?php echo esc_attr($this->get_field_id( 'order' )); ?>"><?php esc_html_e( 'Order:', 'wp-courses' ); ?></label> |
| 139 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id( 'order' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'order' )); ?>"> |
| 140 |
<option value="ASC" <?php selected(esc_attr($order), 'ASC'); ?>><?php esc_html_e('Ascending', 'wp-courses'); ?></option> |
| 141 |
<option value="DESC" <?php selected(esc_attr($order), 'DESC'); ?>><?php esc_html_e('Descending', 'wp-courses'); ?></option> |
| 142 |
</select> |
| 143 |
|
| 144 |
<label for="<?php echo esc_attr($this->get_field_id( 'orderby' )); ?>"><?php esc_html_e( 'Order By:', 'wp-courses' ); ?></label> |
| 145 |
<select class="widefat" id="<?php echo esc_attr($this->get_field_id( 'orderby' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'orderby' )); ?>"> |
| 146 |
<option value="none" <?php selected(esc_attr($orderby), 'none'); ?>><?php esc_html_e('None', 'wp-courses'); ?></option> |
| 147 |
<option value="author" <?php selected(esc_attr($orderby), 'author'); ?>><?php esc_html_e('Author', 'wp-courses'); ?></option> |
| 148 |
<option value="date" <?php selected(esc_attr($orderby), 'date'); ?>><?php esc_html_e('Date', 'wp-courses'); ?></option> |
| 149 |
<option value="ID" <?php selected(esc_attr($orderby), 'ID'); ?>><?php esc_html_e('ID', 'wp-courses'); ?></option> |
| 150 |
<option value="menu_order" <?php selected(esc_attr($orderby), 'menu_order'); ?>><?php esc_html_e('Menu Order', 'wp-courses'); ?></option> |
| 151 |
<option value="name" <?php selected(esc_attr($orderby), 'name'); ?>><?php esc_html_e('Name', 'wp-courses'); ?></option> |
| 152 |
<option value="rand" <?php selected(esc_attr($orderby), 'rand'); ?>><?php esc_html_e('Random', 'wp-courses'); ?></option> |
| 153 |
<option value="title" <?php selected(esc_attr($orderby), 'title'); ?>><?php esc_html_e('Title', 'wp-courses'); ?></option> |
| 154 |
</select> |
| 155 |
|
| 156 |
</p> |
| 157 |
<?php |
| 158 |
} |
| 159 |
// Updating widget replacing old instances with new |
| 160 |
public function update( $new_instance, $old_instance ) { |
| 161 |
$instance = array(); |
| 162 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 163 |
$instance['posts_per_page'] = ( ! empty( $new_instance['posts_per_page'] ) ) ? strip_tags( $new_instance['posts_per_page'] ) : ''; |
| 164 |
$instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? strip_tags( $new_instance['post_type'] ) : ''; |
| 165 |
$instance['order'] = ( ! empty( $new_instance['order'] ) ) ? strip_tags( $new_instance['order'] ) : ''; |
| 166 |
$instance['orderby'] = ( ! empty( $new_instance['orderby'] ) ) ? strip_tags( $new_instance['orderby'] ) : ''; |
| 167 |
$instance['course_id'] = ( ! empty( $new_instance['course_id'] ) ) ? strip_tags( $new_instance['course_id'] ) : ''; |
| 168 |
return $instance; |
| 169 |
} |
| 170 |
} |
| 171 |
function wpc_new_widget() { |
| 172 |
register_widget( 'WPC_New_Stuff_Widget' ); |
| 173 |
} |
| 174 |
add_action( 'widgets_init', 'wpc_new_widget' ); |
| 175 |
?> |