Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include sharable link in "favorite sessions" email #1056

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,11 @@ function flip_sessions_subarrays( $sessions ) {
*
* @param string $wordcamp_name WordCamp name to be used in the email.
* @param array $fav_sessions_lookup Mapping session _id -> 1 for favourite sessions.
* @param string $url_base The URL for schedule page, into which favourite sessions parameter will be added.
*
* @return string Plain text body of the email.
*/
function generate_email_body( $wordcamp_name, $fav_sessions_lookup ) {
function generate_email_body( $wordcamp_name, $fav_sessions_lookup, $url_base ) {
$date_format = get_option( 'date_format' );
$tracks = get_schedule_tracks( 'all' );
$tracks_explicitly_specified = false; // include all tracks in the email.
Expand Down Expand Up @@ -480,6 +481,9 @@ function( $date_ ) use ( $current_day, $date_format ) {
$email_message .= "\n\n";
}

$email_message .= esc_html__( 'Link to your favorite sessions on schedule', 'wordcamporg' );
$email_message .= ' ' . add_query_arg( 'fav-sessions', implode( ',', array_keys( $fav_sessions_lookup ) ), $url_base );

return $email_message;
}

Expand Down Expand Up @@ -522,6 +526,7 @@ function send_favourite_sessions_email( WP_REST_Request $request ) {
// Input sanitized by REST controller.
$email_address = $params['email-address'];
$fav_sessions = $params['session-list'];
$page_slug = $params['page-slug'];

// Don't send the email if no sessions were marked as favourite.
if ( count( explode( ',', $fav_sessions ) ) === 0 ) {
Expand All @@ -534,6 +539,16 @@ function send_favourite_sessions_email( WP_REST_Request $request ) {
);
}

// Page by slug existance validated in REST API.
$pages = get_posts( array(
'name' => $page_slug,
'post_type' => 'page',
'post_status' => 'publish',
'fields' => 'ids',
) );

$url_base = get_the_permalink( $pages[0] );

$fav_sessions_lookup = array_fill_keys( explode( ',', $fav_sessions ), 1 );

$wordcamp_name = get_wordcamp_name();
Expand All @@ -542,7 +557,7 @@ function send_favourite_sessions_email( WP_REST_Request $request ) {
$headers[] = 'Content-Type: text/plain; charset=' . get_bloginfo( 'charset' );

$subject = sprintf( __( 'My favorite sessions for %s', 'wordcamporg' ), $wordcamp_name );
$message = generate_email_body( $wordcamp_name, $fav_sessions_lookup );
$message = generate_email_body( $wordcamp_name, $fav_sessions_lookup, $url_base );

if ( wp_mail( $email_address, $subject, $message, $headers ) ) {
return new WP_REST_Response(
Expand Down
53 changes: 37 additions & 16 deletions public_html/wp-content/plugins/wc-post-types/inc/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function register_speaker_post_meta() {
'type' => 'string',
'single' => true,
'show_in_rest' => array(
'prepare_callback' => function( $value, $request, $args ) {
'prepare_callback' => function ( $value, $request, $args ) {
$user_id = get_post_meta( get_the_ID(), '_wcpt_user_id', true );
if ( $user_id ) {
$wporg_user = get_userdata( $user_id );
Expand All @@ -97,7 +97,7 @@ function register_speaker_post_meta() {
return $value;
},
),
'sanitize_callback' => function( $value ) {
'sanitize_callback' => function ( $value ) {
$wporg_user = wcorg_get_user_by_canonical_names( $value );
if ( ! $wporg_user ) {
return '';
Expand Down Expand Up @@ -149,7 +149,7 @@ function register_session_post_meta() {
'_wcpt_session_time',
array(
'show_in_rest' => array(
'prepare_callback' => function( $value, $request, $args ) {
'prepare_callback' => function ( $value, $request, $args ) {
if ( $request->get_param( 'wc_session_utc' ) ) {
$datetime = date_create( wp_date( 'Y-m-d\TH:i:s\Z', $value ) );
return $datetime->getTimestamp();
Expand Down Expand Up @@ -180,7 +180,7 @@ function register_session_post_meta() {
'show_in_rest' => true,
'single' => true,
'auth_callback' => __NAMESPACE__ . '\meta_auth_callback',
'sanitize_callback' => function( $value ) {
'sanitize_callback' => function ( $value ) {
if ( 'custom' === $value ) {
return $value;
}
Expand All @@ -204,7 +204,7 @@ function register_session_post_meta() {
'show_in_rest' => true,
'single' => true,
'auth_callback' => __NAMESPACE__ . '\meta_auth_callback',
'sanitize_callback' => function( $value ) {
'sanitize_callback' => function ( $value ) {
if ( $value ) {
$host = wp_parse_url( $value, PHP_URL_HOST );

Expand Down Expand Up @@ -254,7 +254,7 @@ function register_organizer_post_meta() {
'type' => 'string',
'single' => true,
'show_in_rest' => array(
'prepare_callback' => function( $value, $request, $args ) {
'prepare_callback' => function ( $value, $request, $args ) {
$user_id = get_post_meta( get_the_ID(), '_wcpt_user_id', true );
if ( $user_id ) {
$wporg_user = get_userdata( $user_id );
Expand All @@ -265,7 +265,7 @@ function register_organizer_post_meta() {
return $value;
},
),
'sanitize_callback' => function( $value ) {
'sanitize_callback' => function ( $value ) {
$wporg_user = wcorg_get_user_by_canonical_names( $value );
if ( ! $wporg_user ) {
return '';
Expand Down Expand Up @@ -318,7 +318,7 @@ function register_volunteer_post_meta() {
'type' => 'string',
'single' => true,
'show_in_rest' => array(
'prepare_callback' => function( $value, $request, $args ) {
'prepare_callback' => function ( $value, $request, $args ) {
$user_id = get_post_meta( get_the_ID(), '_wcpt_user_id', true );
if ( $user_id ) {
$wporg_user = get_userdata( $user_id );
Expand All @@ -329,7 +329,7 @@ function register_volunteer_post_meta() {
return $value;
},
),
'sanitize_callback' => function( $value ) {
'sanitize_callback' => function ( $value ) {
$wporg_user = wcorg_get_user_by_canonical_names( $value );
if ( ! $wporg_user ) {
return '';
Expand Down Expand Up @@ -400,7 +400,7 @@ function register_user_validation_route() {
'permission_callback' => '__return_true',
'args' => array(
'username' => array(
'validate_callback' => function( $value ) {
'validate_callback' => function ( $value ) {
$wporg_user = wcorg_get_user_by_canonical_names( $value );
return (bool) $wporg_user;
},
Expand Down Expand Up @@ -507,7 +507,7 @@ function register_additional_rest_fields() {
'wcb_session',
'session_speakers',
array(
'get_callback' => function( $post ) {
'get_callback' => function ( $post ) {
$speaker_ids = get_post_meta( $post['id'], '_wcpt_speaker_id', false );
$speakers = array();

Expand Down Expand Up @@ -555,7 +555,7 @@ function register_additional_rest_fields() {
'wcb_session',
'session_cats_rendered',
array(
'get_callback' => function( $post ) {
'get_callback' => function ( $post ) {
$terms = get_terms( 'wcb_session_category', array( 'object_ids' => $post['id'] ) );
if ( $terms ) {
return implode( ', ', wp_list_pluck( $terms, 'name' ) );
Expand Down Expand Up @@ -708,17 +708,17 @@ function register_fav_sessions_email() {
'args' => array(
'email-address' => array(
'required' => true,
'validate_callback' => function( $value, $request, $param ) {
'validate_callback' => function ( $value, $request, $param ) {
return is_email( $value );
},
'sanitize_callback' => function( $value, $request, $param ) {
'sanitize_callback' => function ( $value, $request, $param ) {
return sanitize_email( $value );
},
),

'session-list' => array(
'required' => true,
'validate_callback' => function( $value, $request, $param ) {
'validate_callback' => function ( $value, $request, $param ) {
$session_ids = explode( ',', $value );
$session_count = count( $session_ids );
for ( $i = 0; $i < $session_count; $i++ ) {
Expand All @@ -728,11 +728,32 @@ function register_fav_sessions_email() {
}
return true;
},
'sanitize_callback' => function( $value, $request, $param ) {
'sanitize_callback' => function ( $value, $request, $param ) {
$session_ids = explode( ',', $value );
return implode( ',', array_filter( $session_ids, 'is_numeric' ) );
},
),

'page-slug' => array(
'required' => true,
'validate_callback' => function ( $value, $request, $param ) {
$pages = get_posts( array(
'name' => $value,
'post_type' => 'page',
'post_status' => 'publish',
'fields' => 'ids',
) );

if ( empty( $pages ) ) {
return false;
}

return true;
},
'sanitize_callback' => function ( $value, $request, $param ) {
return sanitize_title( $value );
},
),
),
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ jQuery( document ).ready( function ( $ ) {
var data = {
'email-address': emailAddress,
'session-list': favSessions,
'page-slug': window.location.pathname.split('/').filter(n => n).pop(), // get last path = page slug
};

$.ajax( {
Expand Down
Loading