PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.13.1
WpStream – Live Streaming, Video on Demand, Pay Per View v4.13.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.13.1, at includes/class-wpstream-ajax.php

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