course-access-expiry.php
6.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace uncanny_pro_toolkit;
use uncanny_learndash_toolkit as toolkit;
if ( ! defined( 'WPINC' ) ) {
die;
}
class CourseAccessExpiry extends toolkit\Config implements toolkit\RequiredFunctions {
/**
* Class constructor
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'run_frontend_hooks' ) );
}
/*
* Initialize frontend actions and filters
*/
public static function run_frontend_hooks() {
if ( true === self::dependants_exist() ) {
//Expiration date shortcode
add_shortcode( 'uo_expiration_in', array( __CLASS__, 'expiration_in' ) );
}
}
/**
* Does the plugin rely on another function or plugin
*
* @return boolean || string Return either true or name of function or plugin
*
*/
public static function dependants_exist() {
/* Checks for LearnDash */
global $learndash_post_types;
if ( ! isset( $learndash_post_types ) ) {
return 'Plugin: LearnDash';
}
return true;
}
/**
* Description of class in Admin View
*
* @return array
*/
public static function get_details() {
$module_id = 'days-until-course-expiry';
$class_title = esc_attr__( 'Days Until Course Expiry', 'uncanny-pro-toolkit' );
$kb_link = 'https://www.uncannyowl.com/knowledge-base/days-until-course-expiry/';
/* Sample Simple Description with shortcode */
$class_description = esc_attr__( 'Use this shortcode to display the number of days until the learner\'s access expires for the current course. This is a useful shortcode to include on course pages.', 'uncanny-pro-toolkit' );
/* Icon as fontawesome icon */
$class_icon = '<i class="uo_icon_pro_fa uo_icon_fa fa fa-hourglass-end"></i><span class="uo_pro_text">PRO</span>';
$category = 'learndash';
$type = 'pro';
return array(
'id' => $module_id,
'title' => $class_title,
'type' => $type,
'category' => $category,
'kb_link' => $kb_link, // OR set as null not to display
'description' => $class_description,
'dependants_exist' => self::dependants_exist(),
'settings' => self::get_class_settings( $class_title ),
'icon' => $class_icon,
);
}
/*
* Shortcode [expiration_in]
*
*/
/**
* HTML for modal to create settings
*
* @static
*
* @param $class_title
*
* @return string|array
*/
public static function get_class_settings( $class_title ) {
// Create options
$options = array(
array(
'type' => 'text',
'label' => esc_attr__( 'Text displayed when one day remaining', 'uncanny-pro-toolkit' ),
'placeholder' => esc_attr__( 'Course access expires in 1 day', 'uncanny-pro-toolkit' ),
'option_name' => 'days_expiry_text_singular',
),
array(
'type' => 'text',
'label' => esc_attr__( 'Text displayed when multiple days remaining', 'uncanny-pro-toolkit' ),
'placeholder' => esc_attr__( 'Course access expires in %days% days', 'uncanny-pro-toolkit' ),
'description' => esc_attr__( 'Use the token %days% to output the number of days remaining.', 'uncanny-pro-toolkit' ),
'option_name' => 'days_expiry_text_plural',
),
);
// Build html
$html = self::settings_output( array(
'class' => __CLASS__,
'title' => $class_title,
'options' => $options,
) );
return $html;
}
public static function expiration_in( $attributes ) {
$a = shortcode_atts( array(
'pre-text' => '',
'course-id' => ''
), $attributes );
// Set default text
$text = (object) [
'singular' => esc_attr__( 'Course Access Expires in 1 Day', 'uncanny-pro-toolkit' ),
'plural' => esc_attr__( 'Course Access Expires in %s Days', 'uncanny-pro-toolkit' ),
];
// Get fields
$text_singular_field = self::get_settings_value( 'days_expiry_text_singular', __CLASS__ );
$text_plural_field = self::get_settings_value( 'days_expiry_text_plural', __CLASS__ );
// Overwrite the default values with the one in the fields, but only if those are defined
$text->singular = ! empty( $text_singular_field ) ? $text_singular_field : $text->singular;
$text->plural = ! empty( $text_plural_field ) ? $text_plural_field : $text->plural;
// Replace the %days% argument with an %s
// We need to do this so we can use sprintf to insert the value
$text->plural = str_replace( '%days%', '%s', $text->plural );
// Backward compability for the pre-text attribute
// Check if the user defined the pre-text attribute, but didn't set a value in the fields
if ( ! empty( $a['pre-text'] ) && ( empty( $text_singular_field ) && empty( $text_plural_field ) ) ) {
// In that case add the %days% argument
// We won't worry about the order since this solution was already working for the user
$text->singular = $a['pre-text'] . ' ' . esc_attr__( '1 Day', 'uncanny-pro-toolkit' );
$text->plural = $a['pre-text'] . ' %s ' . esc_attr__( 'Days', 'uncanny-pro-toolkit' );
}
$current_user_id = get_current_user_id();
$course_id = $a['course-id'];
if ( '' === $course_id ) {
global $post;
$post_object = $post;
// Get course id
if ( $post->post_type == 'sfwd-courses' ) {
$course_id = $post->ID;
}
// Get course id from related lesson, topic, or quiz
if ( $post_object->post_type == 'sfwd-lessons' || $post_object->post_type == 'sfwd-topic' || $post_object->post_type == 'sfwd-quiz' ) {
$course_id = learndash_get_course_id( $post_object->ID );
}
} else {
$post_object = get_post( (int) $course_id );
if ( $post_object ) {
$course_id = $post_object->ID;
}
}
// if course id not found
if ( '' === $course_id ) {
return '';
}
// Get expiration date
$course_access_up_to = ld_course_access_expires_on( $course_id, $current_user_id );
if ( ! empty( $course_access_up_to ) && 0 !== $course_access_up_to && sfwd_lms_has_access( $course_id, $current_user_id ) ) {
$current_time = current_time( 'timestamp' );
$amount_seconds_between = (int) $course_access_up_to - (int) $current_time;
$amount_days_between = floor( $amount_seconds_between / 60 / 60 / 24 );
if ( 0 > $amount_days_between ) {
return '';
}
if ( 1 == $amount_days_between ) {
$text = $text->singular;
// $text = apply_filters( '', $text,$a['pre-text'], $amount_days_between);
return $text;
} else {
$text = sprintf( $text->plural, $amount_days_between );
// $text = apply_filters( '', $text,$a['pre-text'], $amount_days_between);
return $text;
}
} else {
return '';
}
}
}