| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ajax functions |
| 4 |
* |
| 5 |
* @package wpstream-theme |
| 6 |
*/ |
| 7 |
|
| 8 |
add_action( 'wp_ajax_wpstream_handle_watch_later_item_ajax', 'wpstream_handle_watch_later_item_ajax' ); |
| 9 |
/** |
| 10 |
* Watch later item ajax |
| 11 |
*/ |
| 12 |
function wpstream_handle_watch_later_item_ajax() { |
| 13 |
// Check if the user is logged in. |
| 14 |
if ( ! is_user_logged_in() ) { |
| 15 |
wp_send_json_error( 'You must be logged in to perform this action.' ); |
| 16 |
die(); |
| 17 |
} |
| 18 |
|
| 19 |
if ( isset( $_POST['security'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 20 |
if ( isset( $_POST['postID'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 21 |
$post_id = intval( $_POST['postID'] ); //phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 22 |
$user_id = get_current_user_id(); |
| 23 |
$watch_later_items = get_user_meta( $user_id, 'wpstream_user_watch_later_items', true ); |
| 24 |
if ( is_array( $watch_later_items ) ) { |
| 25 |
if ( in_array( $post_id, $watch_later_items, true ) ) { |
| 26 |
// Item is already in the watch later list, remove it. |
| 27 |
$watch_later_items = array_diff( $watch_later_items, array( $post_id ) ); |
| 28 |
$message = esc_html__( 'Removed from Watch Later', 'hello-wpstream' ); |
| 29 |
} else { |
| 30 |
// Add the item to the watch later list. |
| 31 |
$watch_later_items[] = $post_id; |
| 32 |
$message = esc_html__( 'Added to Watch Later', 'hello-wpstream' ); |
| 33 |
} |
| 34 |
} else { |
| 35 |
// Create a new watch later list if it doesn't exist. |
| 36 |
$watch_later_items = array( $post_id ); |
| 37 |
$message = esc_html__( 'Added to Watch Later', 'hello-wpstream' ); |
| 38 |
} |
| 39 |
|
| 40 |
update_user_meta( $user_id, 'wpstream_user_watch_later_items', $watch_later_items ); |
| 41 |
// Change the water later text and icon using toggle method. |
| 42 |
$content = wpstream_theme_show_watch_later( $post_id ); |
| 43 |
$response = array( |
| 44 |
'success' => true, |
| 45 |
'message' => $message, |
| 46 |
'content' => $content, |
| 47 |
); |
| 48 |
} else { |
| 49 |
$response = array( |
| 50 |
'success' => false, |
| 51 |
'message' => 'Invalid postID format.', |
| 52 |
); |
| 53 |
} |
| 54 |
} else { |
| 55 |
// Nonce verification failed, handle the error or exit. |
| 56 |
$response = array( |
| 57 |
'success' => false, |
| 58 |
'message' => 'Nonce verification failed.', |
| 59 |
); |
| 60 |
} |
| 61 |
wp_send_json( $response ); |
| 62 |
wp_die(); // Always include this at the end of an AJAX callback. |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! function_exists( 'wpstream_theme_create_channel' ) ) { |
| 66 |
/** |
| 67 |
* Create channel for the theme. |
| 68 |
*/ |
| 69 |
function wpstream_theme_create_channel() { |
| 70 |
$maxim_channels_per_user = wpstream_return_max_channels_per_user(); |
| 71 |
$allow_user_paid_channels = wpstream_return_user_can_create_paid(); |
| 72 |
$how_many_posts = wpstream_theme_return_user_channel_list( '', 'found_posts' ); |
| 73 |
|
| 74 |
|
| 75 |
?> |
| 76 |
|
| 77 |
<?php if ( ( $how_many_posts < $maxim_channels_per_user ) || current_user_can( 'manage_options' ) ) : ?> |
| 78 |
<div class="wpstream-dashboard-start-streaming__cta-wrapper_buttons"> |
| 79 |
<div class="wpstream_theme_button_dashboard wpstream_user_create_new_channel"> |
| 80 |
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 81 |
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 6.25C12.4142 6.25 12.75 6.58579 12.75 7V11.25H17C17.4142 11.25 17.75 11.5858 17.75 12C17.75 12.4142 17.4142 12.75 17 12.75H12.75V17C12.75 17.4142 12.4142 17.75 12 17.75C11.5858 17.75 11.25 17.4142 11.25 17V12.75H7C6.58579 12.75 6.25 12.4142 6.25 12C6.25 11.5858 6.58579 11.25 7 11.25H11.25V7C11.25 6.58579 11.5858 6.25 12 6.25Z" fill="#0F0F0F"/> |
| 82 |
</svg> |
| 83 |
|
| 84 |
<?php echo esc_html__( 'New Free Channel', 'hello-wpstream' ); ?> |
| 85 |
</div> |
| 86 |
|
| 87 |
<?php if ( $allow_user_paid_channels || current_user_can( 'manage_options' ) ) : ?> |
| 88 |
<div class="wpstream_theme_button_dashboard wpstream_user_create_new_paid_channel"> |
| 89 |
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 90 |
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 6.25C12.4142 6.25 12.75 6.58579 12.75 7V11.25H17C17.4142 11.25 17.75 11.5858 17.75 12C17.75 12.4142 17.4142 12.75 17 12.75H12.75V17C12.75 17.4142 12.4142 17.75 12 17.75C11.5858 17.75 11.25 17.4142 11.25 17V12.75H7C6.58579 12.75 6.25 12.4142 6.25 12C6.25 11.5858 6.58579 11.25 7 11.25H11.25V7C11.25 6.58579 11.5858 6.25 12 6.25Z" fill="#0F0F0F"/> |
| 91 |
</svg> |
| 92 |
|
| 93 |
<?php echo esc_html__( 'New Paid Channel', 'hello-wpstream' ); ?> |
| 94 |
</div> |
| 95 |
<?php endif; ?> |
| 96 |
</div> |
| 97 |
<?php |
| 98 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 99 |
/* translators: %1$s - number of channels, %2$s - maximum number of channels */ |
| 100 |
$translated_string = sprintf( esc_html__( 'You have, %1$s channels from %2$s possible', 'hello-wpstream' ), $how_many_posts, $maxim_channels_per_user ); |
| 101 |
echo esc_html( $translated_string ); |
| 102 |
} |
| 103 |
?> |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
<?php else : ?> |
| 108 |
<?php $translated_string = sprintf( esc_html__( 'You reach the maximum no of channels allowed.', 'hello-wpstream' ), $how_many_posts, $maxim_channels_per_user ); ?> |
| 109 |
|
| 110 |
<?php echo esc_html( $translated_string ); ?> |
| 111 |
<?php endif; ?> |
| 112 |
|
| 113 |
<?php |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|