Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/addons/course-bundle/src/Utils.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Utils class
4
+
*
5
+
* @package TutorPro\CourseBundle
6
+
* @author Themeum <support@themeum.com>
7
+
* @link https://themeum.com
8
+
* @since 2.2.0
9
+
*/
10
+
11
+
namespace TutorPro\CourseBundle;
12
+
13
+
use TUTOR\Input;
14
+
use TutorPro\CourseBundle\CustomPosts\CourseBundle;
15
+
use TutorPro\CourseBundle\Frontend\BundleBuilder;
16
+
use TutorPro\CourseBundle\Models\BundleModel;
17
+
18
+
/**
19
+
* Utils Class.
20
+
*
21
+
* @since 2.2.0
22
+
*/
23
+
class Utils {
24
+
/**
25
+
* Get view path.
26
+
*
27
+
* @since 2.2.0
28
+
*
29
+
* @param string $path path.
30
+
*
31
+
* @return string
32
+
*/
33
+
public static function view_path( $path = null ) {
34
+
$final_path = TUTOR_COURSE_BUNDLE_DIR . 'views';
35
+
if ( $path ) {
36
+
$final_path .= '/' . $path;
37
+
}
38
+
return $final_path;
39
+
}
40
+
41
+
/**
42
+
* Get template path.
43
+
*
44
+
* @since 2.2.0
45
+
*
46
+
* @param string $path path.
47
+
*
48
+
* @return string
49
+
*/
50
+
public static function template_path( $path = null ) {
51
+
$final_path = TUTOR_COURSE_BUNDLE_DIR . 'templates';
52
+
if ( $path ) {
53
+
$final_path .= '/' . $path;
54
+
}
55
+
return $final_path;
56
+
}
57
+
58
+
/**
59
+
* Get asset URL.
60
+
*
61
+
* @since 2.2.0
62
+
*
63
+
* @param string $url url of assets.
64
+
*
65
+
* @return string
66
+
*/
67
+
public static function asset_url( $url = null ) {
68
+
$final_url = plugin_dir_url( TUTOR_COURSE_BUNDLE_FILE ) . 'assets';
69
+
if ( $url ) {
70
+
$final_url .= '/' . $url;
71
+
}
72
+
return $final_url;
73
+
}
74
+
75
+
/**
76
+
* Get asset file path.
77
+
*
78
+
* @since 3.2.0
79
+
*
80
+
* @param string $path Relative path from assets dir with file ext.
81
+
*
82
+
* @return string
83
+
*/
84
+
public static function asset_path( $path = null ) {
85
+
$final_url = TUTOR_COURSE_BUNDLE_DIR . 'assets';
86
+
if ( $path ) {
87
+
$final_url .= '/' . $path;
88
+
}
89
+
return $final_url;
90
+
}
91
+
92
+
/**
93
+
* Get bundle author avatars.
94
+
*
95
+
* @since 2.2.0
96
+
*
97
+
* @param int $bundle_id course bundle id.
98
+
* @param bool $print_names print names along with avatars.
99
+
*
100
+
* @return void
101
+
*/
102
+
public static function get_bundle_author_avatars( $bundle_id, $print_names = false ) {
103
+
$authors = BundleModel::get_bundle_course_authors( $bundle_id );
104
+
$total_authors = count( $authors );
105
+
106
+
if ( 0 === $total_authors ) {
107
+
echo esc_html( '...' );
108
+
return;
109
+
}
110
+
111
+
$first_author = $authors[0];
112
+
$avatar_authors = array_slice( $authors, 0, 3 );
113
+
?>
114
+
<div class="tutor-bundle-authors">
115
+
<?php
116
+
foreach ( $avatar_authors as $author ) {
117
+
echo wp_kses(
118
+
tutor_utils()->get_tutor_avatar( $author->user_id, 'sm' ),
119
+
tutor_utils()->allowed_avatar_tags()
120
+
);
121
+
}
122
+
123
+
?>
124
+
<div>
125
+
<?php
126
+
if ( $print_names ) :
127
+
// Print Jhon & 2 Others.
128
+
echo esc_html( $first_author->display_name );
129
+
if ( $total_authors > 1 ) {
130
+
echo esc_html( ' & ' . ( $total_authors - 1 ) . ' ' . _n( 'Other', 'Others', $total_authors - 1, 'tutor-pro' ) );
131
+
}
132
+
endif;
133
+
?>
134
+
</div>
135
+
<!-- end of author names -->
136
+
</div>
137
+
<!-- end of tutor-bundle-authors -->
138
+
<?php
139
+
}
140
+
141
+
/**
142
+
* Get current bundle id
143
+
*
144
+
* It will first look at the query string, then the post data.
145
+
*
146
+
* @return int
147
+
*/
148
+
public static function get_bundle_id() {
149
+
$id = Input::get( 'id', 0, Input::TYPE_INT );
150
+
return (int) $id ? $id : get_the_ID();
151
+
}
152
+
153
+
/**
154
+
* Check if user is bundle author
155
+
*
156
+
* @since 2.2.0
157
+
*
158
+
* @param int $bundle_id bundle id.
159
+
* @param int $user_id user id, default to current user.
160
+
*
161
+
* @return bool
162
+
*/
163
+
public static function is_bundle_author( int $bundle_id, int $user_id = 0 ): bool {
164
+
$post_type = get_post_type( $bundle_id );
165
+
166
+
if ( CourseBundle::POST_TYPE !== $post_type ) {
167
+
return false;
168
+
}
169
+
170
+
if ( ! $user_id ) {
171
+
$user_id = get_current_user_id();
172
+
}
173
+
174
+
$post_author = (int) get_post_field( 'post_author', $bundle_id );
175
+
return $user_id === $post_author;
176
+
}
177
+
178
+
/**
179
+
* Check is bundle single page
180
+
*
181
+
* @since 2.2.0
182
+
*
183
+
* @return boolean
184
+
*/
185
+
public static function is_bundle_single_page() {
186
+
global $wp_query;
187
+
if ( $wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && CourseBundle::POST_TYPE === $wp_query->query_vars['post_type'] ) {
188
+
return true;
189
+
} else {
190
+
return false;
191
+
}
192
+
}
193
+
194
+
/**
195
+
* Construct course bundle add-new/edit page url
196
+
*
197
+
* @since 3.2.0
198
+
*
199
+
* @param string $page Page type, should be either 'add-new' or 'edit'.
200
+
* @param int $bundle_id Bundle ID required for edit URL but optional for add new.
201
+
*
202
+
* @throws \InvalidArgumentException If $page is not 'add-new' or 'edit'.
203
+
* @throws \InvalidArgumentException If $bundle_id is 0 or empty for edit page.
204
+
*
205
+
* @return string Constructed URL
206
+
*/
207
+
public static function construct_page_url( $page = 'add-new', $bundle_id = 0 ) {
208
+
$url = '';
209
+
210
+
// Validate input parameters.
211
+
if ( ! in_array( $page, array( BundleBuilder::ACTION_TYPE_ADD, BundleBuilder::ACTION_TYPE_EDIT ) ) ) {
212
+
throw new \InvalidArgumentException( 'Page type must be either add-new or edit' );
213
+
}
214
+
215
+
if ( BundleBuilder::ACTION_TYPE_EDIT === $page && empty( $bundle_id ) ) {
216
+
throw new \InvalidArgumentException( 'Bundle ID is required for edit page' );
217
+
}
218
+
219
+
if ( is_admin() ) {
220
+
if ( BundleBuilder::ACTION_TYPE_ADD === $page ) {
221
+
$url = get_admin_url( null, 'admin.php?page=course-bundle&action=add-new' );
222
+
} else {
223
+
$url = get_admin_url( null, 'admin.php?page=course-bundle&action=edit&id=' . $bundle_id );
224
+
}
225
+
} else {
226
+
// Keep the page url create-bundle for backward compatibility.
227
+
if ( BundleBuilder::ACTION_TYPE_ADD === $page ) {
228
+
$url = tutor_utils()->tutor_dashboard_url( 'create-bundle?action=add-new' );
229
+
} else {
230
+
$url = tutor_utils()->tutor_dashboard_url( 'create-bundle?action=edit&id=' . $bundle_id );
231
+
}
232
+
}
233
+
234
+
return $url;
235
+
}
236
+
237
+
/**
238
+
* Construct bundle builder page url for the frontend only
239
+
*
240
+
* @since 3.2.0
241
+
*
242
+
* @param string $action Builder action type add-new or edit.
243
+
* @param int $bundle_id Bundle id.
244
+
*
245
+
* @return string
246
+
*/
247
+
public static function construct_front_url( $action = 'add-new', $bundle_id = 0 ) {
248
+
$page_slug = BundleBuilder::QUERY_PARAM;
249
+
250
+
$url = tutor_utils()->tutor_dashboard_url( "{$page_slug}?action={$action}" );
251
+
if ( $bundle_id ) {
252
+
return $url . '&id=' . $bundle_id;
253
+
}
254
+
255
+
return $url;
256
+
}
257
+
258
+
/**
259
+
* Check if bundle editor page
260
+
*
261
+
* @since 3.2.0
262
+
*
263
+
* @return boolean
264
+
*/
265
+
public static function is_bundle_editor() {
266
+
$is_editor = false;
267
+
if ( is_admin() ) {
268
+
$page_slug = Input::get( 'page', '', Input::TYPE_STRING );
269
+
$action = Input::get( 'action', '', Input::TYPE_STRING );
270
+
$is_editor = BundleBuilder::ADMIN_PAGE_SLUG === $page_slug && ( BundleBuilder::ACTION_TYPE_EDIT === $action || BundleBuilder::ACTION_TYPE_ADD === $action );
271
+
} else {
272
+
$is_editor = get_query_var( 'tutor_dashboard_page' ) === BundleBuilder::QUERY_PARAM;
273
+
}
274
+
275
+
return ! empty( $is_editor ) ? true : false;
276
+
277
+
}
278
+
/**
279
+
* Check if current user can create bundle
280
+
*
281
+
* @since 3.2.0
282
+
*
283
+
* @return boolean
284
+
*/
285
+
public static function current_user_can_create_bundle(): bool {
286
+
return (bool) current_user_can( tutor()->instructor_role ) || current_user_can( 'administrator' );
287
+
}
288
+
289
+
/**
290
+
* Check if current user can update bundle
291
+
*
292
+
* @since 3.2.0
293
+
*
294
+
* @param integer $bundle_id Course bundle id.
295
+
*
296
+
* @return boolean
297
+
*/
298
+
public static function current_user_can_update_bundle( int $bundle_id ): bool {
299
+
$can = current_user_can( 'administrator' );
300
+
if ( ! $can ) {
301
+
$can = current_user_can( tutor()->instructor_role ) && self::is_bundle_author( $bundle_id );
302
+
}
303
+
304
+
return $can;
305
+
}
306
+
307
+
/**
308
+
* Check if bundle is free
309
+
*
310
+
* @since 3.2.0
311
+
*
312
+
* @param integer $bundle_id Course bundle id.
313
+
*
314
+
* @return boolean
315
+
*/
316
+
public static function is_free( $bundle_id ) {
317
+
$price = tutor_utils()->get_raw_course_price( $bundle_id );
318
+
$is_free = ! $price->regular_price && ! $price->sale_price;
319
+
320
+
return $is_free;
321
+
}
322
+
323
+
}
324
+