PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.11.1
WpStream – Live Streaming, Video on Demand, Pay Per View v4.11.1
4.14.1 4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 All 181 releases
wpstream / includes / class-wpstream-ajax.php

class-wpstream-ajax.php in WpStream – Live Streaming, Video on Demand, Pay Per View 4.11.1, at includes/class-wpstream-ajax.php

922 lines 27.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WpStream_Ajax {
4
5 /**
6 * Store plugin main class to allow public access.
7 *
8 * @since 20180622
9 * @var object The main class.
10 */
11 public $main;
12
13 /**
14 * Constructor.
15 *
16 * @param object $plugin_main The main class.
17 */
18 public function __construct( $plugin_main ) {
19 $this->main = $plugin_main;
20
21 add_action( 'wp_ajax_wpstream_get_videos_list', [$this,'wpstream_get_videos_list'] );
22 add_action('wp_ajax_wpstream_get_broadcaster_info', array($this, 'wpstream_get_broadcaster_info'));
23
24 // Add the dashboard AJAX actions
25 add_action( 'wp_ajax_wpstream_dashboard_save_channel_data', [$this, 'wpstream_dashboard_save_channel_data'] );
26 add_action( 'wp_ajax_wpstream_dashboard_save_user_address', [$this, 'wpstream_dashboard_save_user_address'] );
27 add_action( 'wp_ajax_wpstream_delete_profile_attachment', [$this, 'wpstream_delete_profile_attachment'] );
28 add_action( 'wp_ajax_wpstream_dashboard_save_user_data', [$this, 'wpstream_dashboard_save_user_data'] );
29 add_action( 'wp_ajax_wpstream_handle_channel_selection', [$this, 'wpstream_handle_channel_selection'] );
30 add_action( 'wp_ajax_wpstream_handle_channel_creation', [$this, 'wpstream_handle_channel_creation'] );
31 add_action( 'wp_ajax_wpstream_handle_channel_details_saving', [$this, 'wpstream_handle_channel_details_saving'] );
32 add_action( 'wp_ajax_wpstream_remove_post_id', [$this, 'wpstream_remove_post_id_callback'] );
33 add_action( 'wp_ajax_wpstream_get_live_quota_data', [$this, 'wpstream_get_live_quota_data'] );
34
35 // Enqueue dashboard scripts
36 add_action( 'wp_enqueue_scripts', [$this, 'wpstream_enqueue_dashboard_scripts'] );
37 }
38
39 /**
40 * Enqueue dashboard scripts
41 */
42 public function wpstream_enqueue_dashboard_scripts() {
43 if ( function_exists('wpstream_is_dashboard_page') && wpstream_is_dashboard_page() ) {
44
45 wp_enqueue_script(
46 'wpstream-dashboard-script',
47 plugin_dir_url( dirname( __FILE__ ) ) . 'js/dashboard-script.js',
48 array( 'jquery' ),
49 $this->main->get_version(),
50 true
51 );
52
53 wp_localize_script( 'wpstream-dashboard-script', 'wpstream_dashboard_script_vars', array(
54 'ajaxurl' => admin_url( 'admin-ajax.php' ),
55 'currentPassEmpty' => esc_html__( 'Please enter your current password.', 'wpstream' ),
56 'passNoMatch' => esc_html__( 'Passwords do not match!', 'wpstream' ),
57 ));
58 }
59 }
60
61 /**
62 * Get videos list from WPStream API.
63 */
64 public function wpstream_get_videos_list() {
65 check_ajax_referer( 'wpstream_onboarding_video_list_nonce', 'security' );
66
67 $token = $this->main->wpstream_live_connection->wpstream_get_token();
68 $videos_list = $this->main->wpstream_live_connection->wpstream_get_videos();
69
70 // cleanup any previous echo before sending json
71 ob_end_clean();
72
73 if ( $token ) {
74 echo json_encode( array(
75 'success' => true,
76 'videos' => $videos_list,
77 ));
78 } else {
79 echo json_encode( array(
80 'success' => false,
81 'error' => 'Token not found',
82 ));
83 }
84 die();
85 }
86
87 /**
88 * AJAX handler to get broadcaster information
89 */
90 public function wpstream_get_broadcaster_info() {
91 // Verify nonce
92 check_ajax_referer('wpstream_broadcaster_nonce', 'nonce');
93
94 // Check permissions
95 if (!current_user_can('publish_posts')) {
96 wp_send_json_error('Insufficient permissions');
97 return;
98 }
99
100 $channel_id = isset($_POST['channel_id']) ? intval($_POST['channel_id']) : 0;
101
102 if (empty($channel_id)) {
103 wp_send_json_error('Invalid channel ID');
104 return;
105 }
106
107 // Get RTMP URL from post meta
108 $obs_uri = get_post_meta($channel_id, 'obs_uri', true);
109 $obs_stream = get_post_meta($channel_id, 'obs_stream', true);
110
111 if (empty($obs_uri) || empty($obs_stream)) {
112 wp_send_json_error('RTMP information not available');
113 return;
114 }
115
116 wp_send_json_success([
117 'rtmp_url' => $obs_uri,
118 'stream_key' => $obs_stream
119 ]);
120 }
121
122 /**
123 * Saves channel data from the dashboard.
124 *
125 * Handles the saving of channel data from the dashboard, including title, description,
126 * thumbnail ID, images, category terms, and whether the channel is paid.
127 */
128 public function wpstream_dashboard_save_channel_data() {
129 // Verify the nonce for security
130 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'wpstream_edit_channel_nonce' ) ) {
131 die( 'Permission denied.' );
132 }
133 if ( ! is_user_logged_in() ) {
134 wp_send_json_error( array( 'error' => 'User is not logged in.' ) );
135 die();
136 }
137
138 $thumb_id = isset( $_POST['thumb_id'] ) ? intval( $_POST['thumb_id'] ) : 0;
139 $title = sanitize_text_field( $_POST['title'] );
140 $description = sanitize_text_field( $_POST['description'] );
141 $channel_paid = intval( $_POST['channel_paid'] );
142 $images = sanitize_text_field( $_POST['images'] );
143 $images = trim($images,',');
144 $channel_price=0;
145 if ( isset( $_POST['channel_price'] ) ) {
146 $channel_price = floatval( $_POST['channel_price'] );
147 }
148 $postID = intval( $_POST['postID'] );
149
150 $new_post_type = 'wpstream_product';
151 if ( $channel_paid == 1 ) {
152 $new_post_type = 'product';
153 update_post_meta( $postID, '_price', $channel_price );
154 update_post_meta( $postID, '_regular_price', $channel_price );
155
156 }
157
158 if ( $postID != '0' ) {
159 $post_data = array(
160 'ID' => $postID,
161 'post_title' => $title,
162 'post_content' => $description,
163 'post_type' => $new_post_type,
164 );
165 wp_update_post( $post_data );
166 set_post_thumbnail( $postID, $thumb_id );
167
168
169 /*
170 * Manage images
171 */
172 if ( $channel_paid == 1 ) {
173 update_post_meta( $postID, '_product_type', 'live_stream' );
174 wp_set_post_terms( $postID, 'live_stream', 'product_type' );
175 update_post_meta( $postID, '_product_image_gallery', $images );
176 } else {
177
178 $images_array = explode( ',', $images );
179 delete_post_meta( $postID, 'wpstream_theme_gallery' );
180 foreach ( $images_array as $key => $value ) :
181 add_post_meta( $postID, 'wpstream_theme_gallery', $value, false );
182 endforeach;
183 }
184 $gallery_images = $this->wpstream_return_image_gallery( $postID );
185
186
187 /*
188 * Manage categories
189 */
190 if(isset( $_POST['selected_categories'])):
191 $categories = $_POST['selected_categories'];
192 foreach ( $categories as $taxonomy => $term_ids ) {
193
194 if ( ! is_array( $term_ids ) ) {
195 $term_ids = array( $term_ids );
196 }
197 $term_ids = array_map( 'intval', $term_ids );
198
199 wp_set_object_terms( $postID, $term_ids, $taxonomy );
200 }
201 endif;
202
203 $taxonomy_information = $this->wpstream_return_taxoomy_information( $postID );
204
205 $video_trailer = $this->wpstream_theme_return_trailer_video( $postID );
206
207 $video_preview = $this->wpstream_theme_return_preview_video( $postID );
208
209 wp_send_json_success(
210 array(
211 'succes' => true,
212 's' => $images,
213 'thumburl' => get_the_post_thumbnail_url( $postID, 'wpstream_featured_unit_cards' ),
214 'images' => $this->wpstream_build_html_gallery_dashboard( $gallery_images ),
215 'taxonomies' => $taxonomy_information['html'],
216 'channel_paid'=>$channel_paid,
217 'channel_price'=>$channel_price,
218 'video_trailer' => $video_trailer,
219 'video_preview' => $video_preview,
220 'message' => esc_html__( 'Changes saved successfully.', 'wpstream' ),
221 )
222 );
223 die();
224 }
225 }
226
227 /**
228 * Return the image gallery for a post.
229 *
230 * This function returns the image gallery for a post based on the post type.
231 *
232 * @param int $post_id The ID of the post.
233 * @return array The array of image gallery for the post.
234 */
235 public function wpstream_return_image_gallery( $post_id ) {
236 $post_type = get_post_type( $post_id );
237 $gallery_images = array();
238
239 if ( 'product' === $post_type ) {
240 $gallery_images_source = get_post_meta( $post_id, '_product_image_gallery', true );
241 $gallery_images = explode( ',', $gallery_images_source );
242 } else {
243 if(function_exists('rwmb_meta')){
244 $gallery_images = rwmb_meta( 'wpstream_theme_gallery', array(), $post_id );
245
246 if ( is_array( $gallery_images ) ) {
247 $gallery_images = array_keys( $gallery_images );
248 } elseif ( ! empty( $gallery_images ) ) {
249 $gallery_images = array( $gallery_images );
250 } else {
251 $gallery_images = array();
252 }
253 }
254 }
255
256 return array_filter( $gallery_images );
257 }
258
259 /**
260 * Returns information about taxonomies for the specified post.
261 *
262 * @param int $post_id The post ID.
263 * @return array An array containing information about taxonomies and HTML markup.
264 */
265 public function wpstream_return_taxoomy_information( $post_id ) {
266 $post_type = get_post_type( $post_id );
267 $taxonomies = get_object_taxonomies( $post_type );
268 $all_terms = array();
269 $return_array = array();
270
271 // Loop through each taxonomy and get terms attached to the post.
272 foreach ( $taxonomies as $taxonomy_slug ) {
273 if ( 'product_type' !== $taxonomy_slug && 'product_visibility' !== $taxonomy_slug ) {
274 $taxonomy_obj = get_taxonomy( $taxonomy_slug );
275 $taxonomy_name = $taxonomy_obj->labels->name; // This fetches the name of the taxonomy.
276 $terms = wp_get_post_terms( $post_id, $taxonomy_slug, array( 'fields' => 'all' ) ); // fetch all fields of the term.
277
278 if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
279 $all_terms[ $taxonomy_name ] = $terms;
280 }
281 }
282 }
283
284 $return_html = '';
285
286 foreach ( $all_terms as $taxonomy => $terms ) {
287 $return_html .= ' <div class="wpstream-dashboard-details" >';
288 $return_html .= '<div class="wpstream-dashboard-details-header">' . $taxonomy . '</div>';
289 $return_html .= '<div class="wpstream_account_details_value" id="wpstream_' . sanitize_key( $taxonomy ) . '">';
290
291 foreach ( $terms as $term ) {
292 $return_html .= '<span class="wpstream_term_selected">' . $term->name . '</span>';
293 }
294
295 $return_html .= ' </div>
296
297 </div> ';
298 }
299
300 $return_array['tax_information'] = $all_terms;
301 $return_array['html'] = $return_html;
302
303 return $return_array;
304 }
305
306 /**
307 * Return the trailer video for a post.
308 *
309 * This function returns the trailer video for a post based on the post type.
310 *
311 * @param int $post_id The ID of the post.
312 * @return string The URL of the trailer video.
313 */
314 function wpstream_theme_return_trailer_video( $post_id ) {
315 $trailer_video_id = get_post_meta( $post_id, 'video_trailer', true );
316 $attachment_url = wp_get_attachment_url( $trailer_video_id );
317
318 if ( ! empty( $attachment_url ) ) {;
319 return $attachment_url;
320 }
321
322 return '';
323 }
324
325 /**
326 * Build HTML for the video trailer in the dashboard.
327 *
328 * This function builds HTML for the video trailer in the dashboard based on the provided video URL.
329 *
330 * @param string $video_url The URL of the video.
331 * @return string The HTML string for the video trailer.
332 */
333 function wpstream_theme_build_html_video_trailer_dashboard( $video_url ) {
334 if ( ! empty( $video_url ) ) {
335 return '<div class="wpstream-video-trailer" id="wpstream-video-trailer"><video height="240" controls><source src="' . esc_url( $video_url ) . '" type="video/mp4"></video></div>';
336 }
337
338 return '';
339 }
340
341 /**
342 * Return the trailer video for a post.
343 *
344 * This function returns the trailer video for a post based on the post type.
345 *
346 * @param int $post_id The ID of the post.
347 * @return string The URL of the trailer video.
348 */
349 function wpstream_theme_return_preview_video( $post_id ) {
350 $preview_video_id = get_post_meta( $post_id, 'video_preview', true );
351 $attachment_url = wp_get_attachment_url( $preview_video_id );
352
353 if ( ! empty( $attachment_url ) ) {;
354 return $attachment_url;
355 }
356
357 return '';
358 }
359
360 /**
361 * Build HTML for the video preview in the dashboard.
362 *
363 * This function builds HTML for the video preview in the dashboard based on the provided video URL.
364 *
365 * @param string $video_url The URL of the video.
366 * @return string The HTML string for the video preview.
367 */
368 function wpstream_theme_build_html_video_preview_dashboard( $video_url ) {
369 if ( ! empty( $video_url ) ) {
370 return '<div class="wpstream-video-preview" id="wpstream-video-preview"><video height="240" controls><source src="' . esc_url( $video_url ) . '" type="video/mp4"></video></div>';
371 }
372
373 return '';
374 }
375
376 /**
377 * Build HTML for the gallery in the dashboard.
378 *
379 * This function builds HTML for the gallery in the dashboard based on the provided array of image IDs.
380 *
381 * @param array $gallery_images An array of image IDs.
382 * @return string The HTML string for the gallery.
383 */
384 public function wpstream_build_html_gallery_dashboard( $gallery_images ) {
385 $return_string = '';
386
387 if ( is_array( $gallery_images ) ) {
388 foreach ( $gallery_images as $attachment_id ) {
389 $preview = wp_get_attachment_image_src( $attachment_id, 'wpstream_featured_unit_cards' );
390
391 if ( $preview && '' !== $preview[0] ) {
392 $return_string .= '<div class="wpstream_uploaded_images" data-imageid="' . esc_attr( $attachment_id ) . '">';
393 $return_string .= '<img src="' . esc_url( $preview[0] ) . '" alt="' . esc_html__( 'thumb', 'wpstream' ) . '" /></div>';
394 }
395 }
396 }
397
398 return $return_string;
399 }
400
401 /**
402 * Save user address data from dashboard.
403 *
404 * This function handles saving user address data from the dashboard.
405 */
406 public function wpstream_dashboard_save_user_address() {
407 // Verify the nonce for security.
408 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpstream_edit_addr_nonce' ) ) {
409 die( 'Permission denied.' );
410 }
411
412 if ( ! is_user_logged_in() ) {
413 wp_send_json_error( array( 'error' => 'User is not logged in.' ) );
414 }
415
416 $userID = get_current_user_id();
417 foreach ($_POST['inputData'] as $item){
418 if(isset( $item['id'])){
419 update_user_meta($userID, sanitize_text_field( $item['id']) , sanitize_text_field( $item['value']) );
420 }
421 }
422
423
424 wp_send_json_success(
425 array(
426 'succes' => true,
427 'message' => esc_html__( 'Changes saved successfully.', 'wpstream' ),
428 )
429 );
430
431 die();
432 }
433
434 /**
435 * Delete profile attachment
436 */
437 public function wpstream_delete_profile_attachment() {
438 // Verify the nonce for security.
439 if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'wpstream_profile_image_upload' ) ) {
440 wp_send_json_error(
441 array(
442 'succes' => false,
443 'message' => esc_html__( 'Permission denied nonce', 'wpstream' ),
444 )
445 );
446
447 die();
448 }
449
450 if ( isset( $_POST['image_id'] ) ) {
451 $image_id = intval( $_POST['image_id'] );
452 }
453 $user_id = get_current_user_id();
454
455 // Get the attachment author.
456 $attachment = get_post( $image_id );
457
458 if ( empty( $attachment ) || intval( $attachment->post_author ) !== intval( $user_id ) ) {
459 wp_send_json_error(
460 array(
461
462 'succes' => false,
463
464 'author' => $attachment->post_author,
465
466 'userid' => $user_id,
467
468 'message' => esc_html__( 'Permission denied!!!', 'wpstream' ),
469
470 )
471 );
472
473 die();
474
475 }
476
477 // Delete the attachment (you can customize this part).
478 wp_delete_attachment( $image_id, true );
479 delete_user_meta( $user_id, 'custom_picture' );
480 delete_user_meta( $user_id, 'custom_picture_small' );
481
482 wp_send_json_success(
483 array(
484 'succes' => true,
485 'default' => function_exists('wpstream_get_author_profile_image_url_by_author_id') ? wpstream_get_author_profile_image_url_by_author_id($user_id) : '',
486 'message' => esc_html__( 'Changes saved successfully.', 'wpstream' ),
487 )
488 );
489
490 die();
491 }
492
493 /**
494 * Dashboard save user data
495 */
496 public function wpstream_dashboard_save_user_data() {
497 // Verify the nonce for security.
498
499 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'wpstream_edit_account_nonce' ) ) {
500 die( 'Permission denied.' );
501 }
502
503 if ( ! is_user_logged_in() ) {
504 wp_send_json_error( array( 'error' => 'User is not logged in.' ) );
505 }
506
507 // Get the user's ID.
508 $user_id = get_current_user_id();
509 $current_user = wp_get_current_user();
510
511 // Get the data from the AJAX request.
512 if ( isset( $_POST['firstName'] ) ) {
513 $first_name = sanitize_text_field( wp_unslash( $_POST['firstName'] ) );
514 }
515 if ( isset( $_POST['lastName'] ) ) {
516 $last_name = sanitize_text_field( wp_unslash( $_POST['lastName'] ) );
517 }
518 if ( isset( $_POST['displayName'] ) ) {
519 $display_name = sanitize_text_field( wp_unslash( $_POST['displayName'] ) );
520 }
521 if ( isset( $_POST['email'] ) ) {
522 $email = sanitize_email( wp_unslash( $_POST['email'] ) );
523 }
524 if (isset( $_POST['aboutMe'])){
525 $description = sanitize_textarea_field( wp_unslash( $_POST['aboutMe'] ) );
526 }
527 if ( isset( $_POST['newPassword1'] ) ) {
528 $new_password1 = sanitize_text_field( wp_unslash( $_POST['newPassword1'] ) );
529 }
530 if ( isset( $_POST['newPassword2'] ) ) {
531 $new_password2 = sanitize_text_field( wp_unslash( $_POST['newPassword2'] ) );
532 }
533 if ( isset( $_POST['currentPassword'] ) ) {
534 $current_password = sanitize_text_field( wp_unslash( $_POST['currentPassword'] ) );
535 }
536
537 $passwordchanged = false;
538
539 // Only update fields that are not empty.
540 $user_data = array();
541
542 if ( ! empty( $first_name ) ) {
543 $user_data['first_name'] = $first_name;
544 }
545
546 if ( ! empty( $last_name ) ) {
547 $user_data['last_name'] = $last_name;
548 }
549
550 if ( !empty( $description ) ){
551 $user_data['description'] = $description;
552 }
553
554 if ( ! empty( $display_name ) ) {
555 $user_data['display_name'] = $display_name;
556 }
557
558 $existing_user = get_user_by( 'email', $email );
559
560 if ( $existing_user && $existing_user->ID !== $user_id ) {
561 wp_send_json_error(
562 array(
563 'succes' => false,
564 'failaccount' => esc_html__( 'Email already exists.', 'wpstream' ),
565 )
566 );
567 }
568
569 if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
570 wp_send_json_error(
571 array(
572 'succes' => false,
573 'failaccount' => esc_html__( 'Invalid Email Format', 'wpstream' ),
574 )
575 );
576 }
577
578 if ( ! empty( $email ) ) {
579 $user_data['user_email'] = $email;
580 }
581
582 // Update the user's data.
583 if ( ! empty( $user_data ) ) {
584 $user_data['ID'] = $user_id;
585 wp_update_user( $user_data );
586 }
587
588 if ( ! empty( $new_password1 ) && ! empty( $new_password2 ) ) {
589 if ( $new_password1 !== $new_password2 ) {
590 wp_send_json_error(
591 array(
592 'succes' => false,
593 'failpass' => esc_html__( 'Passwords do not match!', 'wpstream' ),
594 )
595 );
596
597 die();
598
599 } elseif ( ! wp_check_password( $current_password, $current_user->data->user_pass, $current_user->ID ) ) {
600 wp_send_json_error(
601 array(
602 'succes' => false,
603 'failpass' => esc_html__( 'Current Password is not right!', 'wpstream' ),
604 )
605 );
606
607 die();
608
609 } else {
610 wp_set_password( $new_password1, $user_id );
611 $passwordchanged = true;
612 }
613 }
614
615 // Send a response to the client.
616 wp_send_json_success(
617 array(
618 'succes' => true,
619 'passwordchanged' => $passwordchanged,
620 'message' => esc_html__( 'Changes saved successfully.', 'wpstream' ),
621 )
622 );
623 }
624
625 /**
626 * Handle the selection of a channel.
627 *
628 * This function handles the AJAX request for selecting a channel.
629 * It checks if the user is logged in, validates the security nonce,
630 * and updates the user meta with the selected channel if the user is the owner of the channel.
631 * It returns a JSON response indicating success or failure.
632 *
633 * @return void
634 */
635 public function wpstream_handle_channel_selection() {
636 if ( ! is_user_logged_in() ) {
637 wp_die();
638 }
639
640 check_ajax_referer( 'wpstream_user_channel_list', 'security' );
641 if ( isset( $_POST['selected_value'] ) ) {
642 $selected_value = intval( $_POST['selected_value'] );
643 }
644 $current_user = wp_get_current_user();
645 $post_author_id = intval( get_post_field( 'post_author', $selected_value ) );
646
647 if ( $current_user->ID !== $post_author_id ) {
648 echo wp_json_encode(
649 array(
650 'success' => false,
651 'message' => esc_html__( 'You are not the owner of this channel', 'wpstream' ),
652 )
653 );
654
655 } else {
656 update_user_meta( $current_user->ID, 'wpstream_start_streaming_channel', $selected_value );
657
658 echo wp_json_encode(
659 array(
660 'success' => true,
661 'message' => esc_html__( 'Channel updated', 'wpstream' ),
662 )
663 );
664 }
665
666 wp_die();
667 }
668
669 /**
670 * Handle channel creation.
671 */
672 public function wpstream_handle_channel_creation() {
673 if ( ! is_user_logged_in() ) {
674 wp_die();
675 }
676
677 check_ajax_referer( 'wpstream_user_channel_list', 'security' );
678 if ( isset( $_POST['channel_type'] ) ) {
679 $channel_type = sanitize_text_field( wp_unslash( $_POST['channel_type'] ) );
680 }
681 $current_user = wp_get_current_user();
682
683 // These functions should be implemented in the plugin or be accessible
684 $maxim_channels_per_user = function_exists('wpstream_return_max_channels_per_user') ? wpstream_return_max_channels_per_user() : 100;
685 $allow_user_paid_channels = function_exists('wpstream_return_user_can_create_paid') ? wpstream_return_user_can_create_paid() : false;
686 $how_many_posts = function_exists('wpstream_theme_return_user_channel_list') ? wpstream_theme_return_user_channel_list( '', 'found_posts' ) : 0;
687
688 if ( ( $how_many_posts < $maxim_channels_per_user ) || current_user_can( 'manage_options' ) ) {
689 $post_type = 'wpstream_product';
690 $title = 'My New Free Channel';
691
692 if ( ( $allow_user_paid_channels || current_user_can( 'manage_options' ) ) && 'paid' === $channel_type ) {
693 $post_type = 'product';
694 $title = 'My New Paid Channel';
695 }
696
697 $post_data = array(
698 'post_title' => $title,
699 'post_status' => 'publish',
700 'post_author' => $current_user->ID,
701 'post_type' => $post_type,
702 );
703
704 $post_id = wp_insert_post( $post_data );
705
706 if ( ! is_wp_error( $post_id ) ) {
707 update_user_meta( $current_user->ID, 'wpstream_start_streaming_channel', $post_id );
708
709 echo wp_json_encode(
710 array(
711 'success' => true,
712 'message' => 'Post created with ID: ' . $post_id,
713 )
714 );
715 } else {
716 echo wp_json_encode(
717 array(
718 'success' => false,
719 'message' => 'Error creating post: ' . $post_id->get_error_message(),
720 )
721 );
722 }
723 } else {
724 echo wp_json_encode(
725 array(
726 'success' => false,
727 'message' => esc_html__( 'Your reached the maximum number of channels', 'wpstream' ),
728 )
729 );
730 }
731
732 wp_die();
733 }
734
735 /**
736 * Handle AJAX request to save channel details.
737 *
738 * This function handles the AJAX request to save the details of a channel, including its title, description,
739 * price, images, featured status, and taxonomies.
740 *
741 * @return void Outputs JSON-encoded response indicating success or failure of the operation.
742 */
743 public function wpstream_handle_channel_details_saving() {
744 if ( ! is_user_logged_in() ) {
745 wp_die();
746 }
747
748 check_ajax_referer( 'wpstream_user_channel_list', 'security' );
749
750 if ( isset( $_POST['postID'] ) ) {
751 $post_id = intval( $_POST['postID'] );
752 }
753 $current_user = wp_get_current_user();
754 $post_author_id = intval( get_post_field( 'post_author', $post_id ) );
755
756 if ( $current_user->ID !== $post_author_id ) {
757 echo wp_json_encode(
758 array(
759 'success' => false,
760 '$postID' => $post_id,
761 '$post_author_id' => $post_author_id,
762 'message' => esc_html__( 'You are not the owner of this channel', 'wpstream' ),
763 )
764 );
765 } else {
766 if ( isset( $_POST['title'] ) ) {
767 $title = sanitize_text_field( wp_unslash( $_POST['title'] ) );
768 }
769 if ( isset( $_POST['description'] ) ) {
770 $sanitized_content = wp_kses_post( wp_unslash( $_POST['description'] ) );
771 }
772 $price = 0;
773
774 if ( isset( $_POST['price'] ) ) {
775 $price = sanitize_text_field( wp_unslash( $_POST['price'] ) );
776 }
777
778 if ( isset( $_POST['images'] ) ) {
779 $images = sanitize_text_field( wp_unslash( $_POST['images'] ) );
780 }
781
782 if ( isset( $_POST['featured'] ) ) {
783 $featured = intval( $_POST['featured'] );
784 }
785
786 if ( isset( $_POST['taxonomies'] ) ) {
787 $taxonomies_raw = sanitize_text_field( wp_unslash( $_POST['taxonomies'] ) );
788 $taxonomies = is_array( $taxonomies_raw ) ? array_map( 'sanitize_text_field', $taxonomies_raw ) : array();
789 }
790 $images = rtrim( ltrim( trim( $images ), ',' ), ',' );
791
792 if ( get_post_type( $post_id ) === 'product' ) {
793 update_post_meta( $post_id, '_product_image_gallery', $images );
794 update_post_meta( $post_id, '_regular_price', $price );
795 } else {
796 $images_array = explode( ',', $images );
797 foreach ( $images_array as $key => $value ) :
798 add_post_meta( $post_id, 'wpstream_theme_gallery', $value );
799 endforeach;
800 }
801
802 set_post_thumbnail( $post_id, $featured );
803
804 $post_data = array(
805 'ID' => $post_id,
806 'post_title' => $title,
807 'post_content' => $sanitized_content,
808 );
809
810 // Update the post.
811 wp_update_post( $post_data );
812
813 foreach ( $taxonomies as $taxonomy => $term_ids ) {
814 wp_remove_object_terms( $post_id, '', $taxonomy );
815
816 if ( is_array( $term_ids ) ) {
817 foreach ( $term_ids as $key => $term_id ) {
818 if ( 'product_tag' === $taxonomy || 'post_tag' === $taxonomy ) {
819 $tagterm = get_term( $term_id );
820
821 if ( $tagterm && ! is_wp_error( $tagterm ) ) {
822 $tag_term_name = $tagterm->name;
823 wp_set_post_terms( $post_id, $tag_term_name, $taxonomy, true );
824 }
825 } elseif ( -1 !== $term_id ) {
826 wp_set_post_terms( $post_id, intval( $term_id ), $taxonomy, true );
827 }
828 }
829 }
830 }
831
832 echo wp_json_encode(
833 array(
834 'success' => true,
835 '$price' => $price,
836 'message' => esc_html__( 'Channel updated', 'wpstream' ),
837 'title' => $title,
838 '$sanitized_content' => $sanitized_content,
839 '$images' => $images,
840 '$featured' => $featured,
841 '$taxonomies' => $taxonomies,
842 )
843 );
844 }
845
846 wp_die();
847 }
848
849 /**
850 * Callback handler to remove a post ID from the "watch later" list.
851 */
852 public function wpstream_remove_post_id_callback() {
853 if ( ! is_user_logged_in() ) {
854 wp_send_json_error( 'You must be logged in to perform this action.' );
855 die();
856 }
857
858 // Verify the nonce.
859 if ( isset( $_POST['wpstream_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wpstream_nonce'] ) ), 'wpstream-watch-later-nonce' ) ) {
860 if ( isset( $_POST['postID'] ) ) {
861 $post_id_to_remove = intval( $_POST['postID'] );
862 $meta_key = 'wpstream_user_watch_later_items';
863 $current_user = wp_get_current_user();
864 $user_id = $current_user->ID;
865
866 // Get the current array of post IDs.
867 $watch_later_item_ids = get_user_meta( $user_id, $meta_key, true );
868
869 // Remove the specific ID from the array.
870 $watch_later_item_ids = array_filter(
871 $watch_later_item_ids,
872 function ( $id ) use ( $post_id_to_remove ) {
873 return $id !== $post_id_to_remove;
874 }
875 );
876
877 // Update the user's metadata with the modified array.
878 update_user_meta( $user_id, $meta_key, $watch_later_item_ids );
879
880 $response = array(
881 'success' => true,
882 'message' => 'Item removed',
883 );
884
885 } else {
886 $response = array(
887 'success' => false,
888 'message' => 'Invalid postID format.',
889 );
890 }
891 } else {
892 $response = array(
893 'success' => false,
894 'message' => 'Nonce verification failed.',
895 );
896 }
897
898 wp_send_json( $response );
899
900 wp_die();
901 }
902
903 public function wpstream_get_live_quota_data() {
904 if ( ! wp_verify_nonce( $_POST['security'] ?? '', 'wpstream_notice_nonce' ) ) {
905 wp_send_json_error( 'Nonce verification failed.' );
906 die();
907 }
908
909 if ( ! is_user_logged_in() ) {
910 wp_send_json_error( 'You must be logged in to perform this action.' );
911 die();
912 }
913
914 $quota_data = $this->main->quota_manager->get_live_quota_data( 'wpstream_get_live_quota_data' );
915
916 if ( $quota_data ) {
917 wp_send_json_success( $quota_data );
918 } else {
919 wp_send_json_error( 'Could not retrieve quota data.' );
920 }
921 }
922 }