__( 'You do not have permission to add videos.', 'addonn-shoppable-video' ) ) ); } $video_attachment_id = isset( $_POST['video_attachment_id'] ) ? absint( $_POST['video_attachment_id'] ) : 0; if ( ! $video_attachment_id || 0 !== strpos( (string) get_post_mime_type( $video_attachment_id ), 'video/' ) ) { wp_send_json_error( array( 'message' => __( 'Please choose a valid video file.', 'addonn-shoppable-video' ) ) ); } $product_ids = isset( $_POST['product_ids'] ) ? array_map( 'absint', (array) $_POST['product_ids'] ) : array(); $product_ids = array_filter( $product_ids ); if ( empty( $product_ids ) ) { wp_send_json_error( array( 'message' => __( 'Tag at least one product.', 'addonn-shoppable-video' ) ) ); } // Only keep IDs that are real, purchasable products. $valid_product_ids = array(); foreach ( $product_ids as $pid ) { $product = wc_get_product( $pid ); if ( $product && $product->is_purchasable() ) { $valid_product_ids[] = $pid; } } if ( empty( $valid_product_ids ) ) { wp_send_json_error( array( 'message' => __( 'None of the tagged products are valid/purchasable.', 'addonn-shoppable-video' ) ) ); } $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; if ( '' === $title ) { $attachment_title = get_the_title( $video_attachment_id ); $title = $attachment_title ? $attachment_title : sprintf( /* translators: %s: current date and time, used as a fallback title for an untitled video. */ __( 'Video — %s', 'addonn-shoppable-video' ), current_time( 'Y-m-d H:i' ) ); } $cover_attachment_id = isset( $_POST['cover_attachment_id'] ) ? absint( $_POST['cover_attachment_id'] ) : 0; if ( $cover_attachment_id && 0 !== strpos( (string) get_post_mime_type( $cover_attachment_id ), 'image/' ) ) { $cover_attachment_id = 0; // Ignore anything that isn't actually an image. } $whatsapp_enabled = ! empty( $_POST['whatsapp_enabled'] ); // Digits only — this gets concatenated straight into a wa.me/{number} // URL, and a stray space/dash/"+" would break that link. $whatsapp_number = isset( $_POST['whatsapp_number'] ) ? preg_replace( '/\D+/', '', sanitize_text_field( wp_unslash( $_POST['whatsapp_number'] ) ) ) : ''; $post_id = wp_insert_post( array( 'post_type' => ADDOSHVI_POST_TYPE, 'post_status' => 'publish', 'post_title' => $title, ), true ); if ( is_wp_error( $post_id ) ) { wp_send_json_error( array( 'message' => $post_id->get_error_message() ) ); } update_post_meta( $post_id, '_addoshvi_video_attachment_id', $video_attachment_id ); update_post_meta( $post_id, '_addoshvi_products', $valid_product_ids ); update_post_meta( $post_id, '_addoshvi_whatsapp_enabled', $whatsapp_enabled ? '1' : '' ); update_post_meta( $post_id, '_addoshvi_whatsapp_number', $whatsapp_number ); if ( $cover_attachment_id ) { set_post_thumbnail( $post_id, $cover_attachment_id ); } $video_data = ADDOSHVI_Shortcode::get_video_data( $post_id ); if ( ! $video_data ) { // Shouldn't happen given the validation above, but fail safe. wp_send_json_error( array( 'message' => __( 'Video was saved but could not be rendered — please check it in Shoppable Videos.', 'addonn-shoppable-video' ) ) ); } ob_start(); $video = $video_data; // Expected variable name inside the template. include ADDOSHVI_PLUGIN_DIR . 'templates/video-card.php'; $card_html = ob_get_clean(); wp_send_json_success( array( 'message' => __( 'Video added!', 'addonn-shoppable-video' ), 'video_id' => $post_id, 'card_html' => $card_html, ) ); } }