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

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