fictioneer-email-notifications/ajax.php

267 lines
8.4 KiB
PHP
Raw Normal View History

2024-03-11 02:51:58 +01:00
<?php
/**
* AJAX callback to retrieve the modal content
*
2024-03-13 14:18:53 +01:00
* Note: The "fictioneer_ajax_" prefix enables the plugin skipping
* in the theme's must-use-plugin.
*
2024-03-11 02:51:58 +01:00
* @since 0.1.0
*/
2024-03-13 14:18:53 +01:00
function fictioneer_ajax_fcnen_get_form_content() {
2024-03-11 02:51:58 +01:00
// Verify
if ( ! wp_doing_ajax() ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( __( 'Invalid request.', 'fcnen' ) );
2024-03-11 02:51:58 +01:00
}
// Get form
2024-03-11 22:11:50 +01:00
$html = fictioneer_minify_html( fcnen_get_modal_content() );
2024-03-11 02:51:58 +01:00
// Response
wp_send_json_success( array( 'html' => $html ) );
}
2024-03-13 14:18:53 +01:00
add_action( 'wp_ajax_fictioneer_ajax_fcnen_get_form_content', 'fictioneer_ajax_fcnen_get_form_content' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_fcnen_get_form_content', 'fictioneer_ajax_fcnen_get_form_content' );
2024-03-11 02:51:58 +01:00
/**
* AJAX callback to subscribe
*
2024-03-13 14:18:53 +01:00
* Note: The "fictioneer_ajax_" prefix enables the plugin skipping
* in the theme's must-use-plugin.
*
2024-03-11 02:51:58 +01:00
* @since 0.1.0
*/
2024-03-13 14:18:53 +01:00
function fictioneer_ajax_fcnen_subscribe_or_update() {
2024-03-11 02:51:58 +01:00
// Verify
if ( ! wp_doing_ajax() ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( __( 'Invalid request.', 'fcnen' ) );
2024-03-11 02:51:58 +01:00
}
2024-03-11 22:11:50 +01:00
if ( ! check_ajax_referer( 'fcnen-subscribe', 'nonce', false ) ) {
2024-03-11 10:22:59 +01:00
wp_send_json_error(
2024-03-11 22:11:50 +01:00
array( 'notice' => __( 'Nonce verification failed. Please reload and try again.', 'fcnen' ) )
2024-03-11 10:22:59 +01:00
);
}
2024-03-11 02:51:58 +01:00
// Setup
$email = sanitize_email( $_POST['email'] ?? '' );
$code = sanitize_text_field( $_POST['code'] ?? '' );
2024-03-12 02:16:04 +01:00
$scope_everything = boolval( absint( $_POST['scope-everything'] ?? 1 ) );
$scope_posts = boolval( absint( $_POST['scope-posts'] ?? 0 ) );
$scope_stories = boolval( absint( $_POST['scope-stories'] ?? 0 ) );
$scope_chapters = boolval( absint( $_POST['scope-chapters'] ?? 0 ) );
2024-03-13 12:14:24 +01:00
$post_ids = fcnen_get_array_from_post_string( 'post_id' );
2024-03-11 22:11:50 +01:00
$default_notice = __( 'Submission successful. If everything was in order, you will get an email.', 'fcnen' );
2024-03-11 02:51:58 +01:00
$result = false;
// Validate email
if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( array( 'notice' => __( 'Invalid email address.', 'fcnen' ) ) );
2024-03-11 02:51:58 +01:00
}
2024-03-13 03:30:50 +01:00
// Sanitize
$post_ids = array_map( 'trim', $post_ids );
$post_ids = array_unique( $post_ids );
$post_ids = array_map( 'absint', $post_ids );
$post_ids = array_map( 'strval', $post_ids );
2024-03-11 10:22:59 +01:00
// Arguments
$args = array(
2024-03-12 01:45:16 +01:00
'scope-everything' => $scope_everything,
'scope-posts' => $scope_posts,
'scope-stories' => $scope_stories,
2024-03-13 03:30:50 +01:00
'scope-chapters' => $scope_chapters,
'post_ids' => $post_ids
2024-03-11 10:22:59 +01:00
);
2024-03-11 02:51:58 +01:00
// New or update?
2024-03-11 22:11:50 +01:00
$is_new_subscriber = ! fcnen_subscriber_exists( $email );
2024-03-11 10:22:59 +01:00
// New subscriber!
if ( $is_new_subscriber ) {
2024-03-11 22:11:50 +01:00
$result = fcnen_add_subscriber( $email, $args );
2024-03-11 10:22:59 +01:00
}
// Update subscriber!
if ( ! $is_new_subscriber ) {
// Code?
if ( ! $code ) {
2024-03-11 22:11:50 +01:00
$notice = WP_DEBUG ? __( 'Code missing.', 'fcnen' ) : $default_notice;
2024-03-11 10:22:59 +01:00
wp_send_json_error( array( 'notice' => $notice ) );
}
// Query subscriber
2024-03-11 22:11:50 +01:00
$subscriber = fcnen_get_subscriber_by_email_and_code( $email, $code );
2024-03-11 10:22:59 +01:00
// Code did not match email
if ( empty( $subscriber ) ) {
2024-03-11 22:11:50 +01:00
$notice = WP_DEBUG ? __( 'Code did not match email.', 'fcnen' ) : $default_notice;
2024-03-11 10:22:59 +01:00
wp_send_json_error( array( 'notice' => $notice ) );
}
// Update
2024-03-11 22:11:50 +01:00
$result = fcnen_update_subscriber( $email, $args );
2024-03-11 02:51:58 +01:00
}
// Response
if ( $result ) {
2024-03-11 10:22:59 +01:00
wp_send_json_success( array( 'notice' => $default_notice ) );
2024-03-11 02:51:58 +01:00
} else {
2024-03-11 22:11:50 +01:00
$notice = WP_DEBUG ? __( 'Could not create or update subscription.', 'fcnen' ) : $default_notice;
2024-03-11 14:08:41 +01:00
// Do not expose informative errors to strangers
wp_send_json_success( array( 'notice' => $notice ) );
2024-03-11 02:51:58 +01:00
}
}
2024-03-13 14:18:53 +01:00
add_action( 'wp_ajax_fictioneer_ajax_fcnen_subscribe_or_update', 'fictioneer_ajax_fcnen_subscribe_or_update' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_fcnen_subscribe_or_update', 'fictioneer_ajax_fcnen_subscribe_or_update' );
2024-03-11 14:08:41 +01:00
/**
* AJAX callback to unsubscribe
*
2024-03-13 14:18:53 +01:00
* Note: The "fictioneer_ajax_" prefix enables the plugin skipping
* in the theme's must-use-plugin.
*
2024-03-11 14:08:41 +01:00
* @since 0.1.0
*/
2024-03-13 14:18:53 +01:00
function fictioneer_ajax_fcnen_unsubscribe() {
2024-03-11 14:08:41 +01:00
// Verify
if ( ! wp_doing_ajax() ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( __( 'Invalid request.', 'fcnen' ) );
2024-03-11 14:08:41 +01:00
}
2024-03-11 22:11:50 +01:00
if ( ! check_ajax_referer( 'fcnen-subscribe', 'nonce', false ) ) {
2024-03-11 14:08:41 +01:00
wp_send_json_error(
2024-03-11 22:11:50 +01:00
array( 'notice' => __( 'Nonce verification failed. Please reload and try again.', 'fcnen' ) )
2024-03-11 14:08:41 +01:00
);
}
// Setup
$email = sanitize_email( $_POST['email'] ?? '' );
$code = sanitize_text_field( $_POST['code'] ?? '' );
2024-03-11 22:11:50 +01:00
$default_notice = __( 'Successfully unsubscribed.', 'fcnen' );
2024-03-11 14:08:41 +01:00
$result = false;
// Validate email
if ( empty( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( array( 'notice' => __( 'Invalid email address.', 'fcnen' ) ) );
2024-03-11 14:08:41 +01:00
}
// Email and code present?
if ( empty( $email ) || empty( $code ) ) {
2024-03-11 22:11:50 +01:00
wp_send_json_error( array( 'notice' => __( 'Email or code missing.', 'fcnen' ) ) );
2024-03-11 14:08:41 +01:00
}
// Query subscriber
2024-03-11 22:11:50 +01:00
$subscriber = fcnen_get_subscriber_by_email_and_code( $email, $code );
2024-03-11 14:08:41 +01:00
// Match found...
if ( ! $subscriber ) {
// ... no match
2024-03-11 22:11:50 +01:00
$notice = WP_DEBUG ? __( 'No matching subscription found.', 'fcnen' ) : $default_notice;
2024-03-11 14:08:41 +01:00
// Do not expose informative errors to strangers
wp_send_json_success( array( 'notice' => $notice ) );
} else {
// ... found
2024-03-11 22:11:50 +01:00
$result = fcnen_delete_subscriber( $email );
2024-03-11 14:08:41 +01:00
}
// Response
if ( $result ) {
wp_send_json_success( array( 'notice' => $default_notice ) );
} else {
2024-03-11 22:11:50 +01:00
$notice = WP_DEBUG ? __( 'Could not delete subscription.', 'fcnen' ) : $default_notice;
2024-03-11 14:08:41 +01:00
// Do not expose informative errors to strangers
wp_send_json_success( array( 'notice' => $notice ) );
}
}
2024-03-13 14:18:53 +01:00
add_action( 'wp_ajax_fictioneer_ajax_fcnen_unsubscribe', 'fictioneer_ajax_fcnen_unsubscribe' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_fcnen_unsubscribe', 'fictioneer_ajax_fcnen_unsubscribe' );
2024-03-13 00:25:05 +01:00
/**
* AJAX callback to search content
*
2024-03-13 14:18:53 +01:00
* Note: The "fictioneer_ajax_" prefix enables the plugin skipping
* in the theme's must-use-plugin.
*
2024-03-13 00:25:05 +01:00
* @since 0.1.0
*/
2024-03-13 14:18:53 +01:00
function fictioneer_ajax_fcnen_search_content() {
2024-03-13 00:25:05 +01:00
// Verify
if ( ! wp_doing_ajax() ) {
wp_send_json_error( __( 'Invalid request.', 'fcnen' ) );
}
if ( ! check_ajax_referer( 'fcnen-subscribe', 'nonce', false ) ) {
wp_send_json_error(
array( 'notice' => __( 'Nonce verification failed. Please reload and try again.', 'fcnen' ) )
);
}
// Setup
2024-03-13 14:09:43 +01:00
$filter = sanitize_text_field( $_REQUEST['filter'] ?? '' );
2024-03-13 00:25:05 +01:00
$search = sanitize_text_field( $_REQUEST['search'] ?? '' );
$page = absint( $_REQUEST['page'] ?? 1 );
2024-03-13 14:47:22 +01:00
$query = null;
2024-03-13 00:25:05 +01:00
$output = [];
2024-03-13 16:01:34 +01:00
// Query stories
2024-03-13 14:09:43 +01:00
if ( $filter === 'story' ) {
2024-03-13 01:21:09 +01:00
$query = new WP_Query(
array(
'post_type' => 'fcn_story',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => 10,
'paged' => $page,
's' => $search,
2024-03-13 16:01:34 +01:00
'update_post_meta_cache' => true, // We might need that
2024-03-13 01:21:09 +01:00
'update_post_term_cache' => false // Improve performance
)
);
foreach ( $query->posts as $item ) {
// Chapter setup
$title = fictioneer_get_safe_title( $item, 'fcnen-search-stories' );
// Build and append item
2024-03-13 15:24:37 +01:00
$item = "<li class='fcnen-dialog-modal__advanced-li' data-click-action='fcnen-add' data-name='post_id[]' data-type='post_id' data-compare='story-{$item->ID}' data-id='{$item->ID}'><span class='fcnen-item-label'>" . _x( 'Story', 'List item label.', 'fcnen' ) . "</span> <span class='fcnen-item-name'>{$title}</span></li>";
2024-03-13 01:21:09 +01:00
// Add to output
$output[] = $item;
}
}
2024-03-13 10:40:34 +01:00
// Add observer?
2024-03-13 14:47:22 +01:00
if ( $query && $page < $query->max_num_pages ) {
2024-03-13 10:40:34 +01:00
$page++;
$observer = '<li class="fcnen-dialog-modal__advanced-li _observer" data-target="fcnen-observer-item" data-page="' . $page . '"><i class="fa-solid fa-spinner fa-spin" style="--fa-animation-duration: .8s;"></i> ' . __( 'Loading…', 'fcnen' ) . '<span></span></li>';
$output[] = $observer;
}
// No results?
if ( empty( $output ) ) {
$no_matches = '<li class="fcnen-dialog-modal__advanced-li _disabled _no-match"><span>' . __( 'No matches found.', 'fcnen' ) . '</span></li>';
$output[] = $no_matches;
}
2024-03-13 00:25:05 +01:00
// Response
wp_send_json_success(
array(
'html' => implode( '', $output )
)
);
}
2024-03-13 14:18:53 +01:00
add_action( 'wp_ajax_fictioneer_ajax_fcnen_search_content', 'fictioneer_ajax_fcnen_search_content' );
add_action( 'wp_ajax_nopriv_fictioneer_ajax_fcnen_search_content', 'fictioneer_ajax_fcnen_search_content' );