earned-user-points-widget.php
5.34 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
<?php
/**
* Points Widget
*
* @package badgeos/includes/widgets
*/
/**
* Class earned_user_points_widget.
*
* @package badgeos
*/
class Earned_User_Points_Widget extends WP_Widget {
/**
* Constructor
*/
public function __construct() {
$widget_ops = array(
'classname' => 'earned_user_points_class',
'description' => __( 'Displays Total points earned by the logged in user', 'badgeos' ),
);
parent::__construct( 'earned_user_points_widget', __( 'BadgeOS Earned User Points', 'badgeos' ), $widget_ops );
}
/**
* Form
*
* @param array $instance args.
*/
public function form( $instance ) {
$defaults = array(
'title' => __( 'My Points', 'badgeos' ),
'set_point_type' => '',
'point_total' => '',
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
$number = isset( $instance['number'] ) ? $instance['number'] : '';
$point_total = $instance['point_total'];
$set_point_type = ( isset( $instance['total_points_type'] ) ) ? $instance['total_points_type'] : '';
?>
<p><label><?php esc_attr_e( 'Title', 'badgeos' ); ?>: <input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<p><label><input type="checkbox" id="<?php echo esc_attr( $this->get_field_name( 'point_total' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'point_total' ) ); ?>" <?php checked( $point_total, 'on' ); ?> /> <?php esc_attr_e( 'Display user\'s total points', 'badgeos' ); ?></label></p>
<p>
<?php
/**
* Get all credit types.
*/
$point_types = badgeos_get_point_types();
if ( is_array( $point_types ) && ! empty( $point_types ) ) {
?>
<select id="total_points_type" name="<?php echo esc_attr( $this->get_field_name( 'total_points_type' ) ); ?>" class="widget-total-points-type">
<option value=""><?php esc_attr_e( 'Select Points Type' ); ?></option>
<?php
foreach ( $point_types as $key => $point_type ) {
?>
<option value="<?php echo esc_attr( $point_type->ID ); ?>" <?php echo esc_attr( selected( $set_point_type, $point_type->ID ) ); ?> ><?php echo esc_html( $point_type->post_title ); ?></option>
<?php
}
?>
</select>
<br />
<span class="tool-hint"><?php esc_attr_e( 'Total points of selected type will be displayed on frontend.', 'badgeos' ); ?></span>
<?php } ?>
</p>
<?php
}
/**
* Widget update.
*
* @param array $new_instance selected args.
* @param array $old_instance $args.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['total_points_type'] = sanitize_text_field( $new_instance['total_points_type'] );
$instance['point_total'] = ( ! empty( $new_instance['point_total'] ) ) ? sanitize_text_field( $new_instance['point_total'] ) : '';
return $instance;
}
/**
* Widget
*
* @param array $args selected args.
* @param array $instance args.
*/
public function widget( $args, $instance ) {
$args = array_map( 'sanitize_text_field', $args );
if ( array_key_exists( 'before_widget', $args ) ) {
echo esc_html( $args['before_widget'] );
}
$badgeos_settings = ! empty( badgeos_utilities::get_option( 'badgeos_settings' ) ) ? badgeos_utilities::get_option( 'badgeos_settings' ) : array();
$title = apply_filters( 'widget_title', $instance['title'] );
if ( ! empty( $title ) ) {
echo esc_html( $args['before_title'] . $title . $args['after_title'] );
};
// user must be logged in to view earned badges and points.
if ( is_user_logged_in() ) {
// display user's points if widget option is enabled.
if ( 'on' === $instance['point_total'] && ! empty( $instance['total_points_type'] ) ) {
$earned_points = badgeos_get_points_by_type( $instance['total_points_type'], get_current_user_id() );
$plural_name = badgeos_utilities::get_post_meta( $instance['total_points_type'], '_point_plural_name', true );
if ( ! empty( $plural_name ) ) {
$point_title = $plural_name;
} else {
$point_title = get_the_title( $instance['total_points_type'] );
}
$badge_image = badgeos_get_point_image( $instance['total_points_type'], 50, 50 );
$badge_image = apply_filters( 'badgeos_profile_points_image', $badge_image, 'front-widget', $instance['total_points_type'] );
?>
<div class="badgeos_earned_points_only">
<table>
<tr>
<td width="100%">
<div class="badgeos_earned_points_widget_image">
<?php echo esc_html( $badge_image ); ?>
</div>
<div class="badgeos_earned_points_widget">
<div class="badgeos-earned-credit">
<span><?php echo esc_html( number_format( $earned_points ) ); ?></span>
</div>
</div>
</td>
</tr>
<tr>
<td>
<div class="points_widget_title">
<?php echo esc_html( $point_title ); ?>
</div>
</td>
</tr>
</table>
</div>
<?php
}
} else {
// user is not logged in so display a message.
echo '<p>' . esc_attr_e( 'You must be logged in to view earned points', 'badgeos' ) . '</p>';
}
if ( array_key_exists( 'after_widget', $args ) ) {
echo esc_html( $args['after_widget'] );
}
}
}