array( 'name' => 'LatePoint', 'shortcodes' => array( 'latepoint_book_form', 'latepoint_book_button' ) ), 'amelia' => array( 'name' => 'Amelia', 'shortcodes' => array( 'ameliastepbooking', 'ameliacatalogbooking', 'ameliabooking', 'ameliacatalog' ) ), 'bookly' => array( 'name' => 'Bookly', 'shortcodes' => array( 'bookly-form', 'bookly-search-form', 'bookly-services-form' ) ), 'ssa' => array( 'name' => 'Simply Schedule Appointments', 'shortcodes' => array( 'ssa_booking' ) ), 'booking_calendar' => array( 'name' => 'Booking Calendar', 'shortcodes' => array( 'booking', 'bookingform', 'booking_popup' ) ), ); } function afroiabo_booking_provider_active( $provider ) { $connector = afroiabo_booking_connectors()[ $provider ] ?? null; if ( ! $connector ) { return false; } // Some providers register shortcodes only on frontend requests, not admin AJAX. $plugin_files = array( 'latepoint' => 'latepoint/latepoint.php', 'amelia' => 'ameliabooking/ameliabooking.php', 'bookly' => 'bookly-responsive-appointment-booking-tool/main.php', 'ssa' => 'simply-schedule-appointments/simply-schedule-appointments.php', 'booking_calendar' => 'booking/wpdev-booking.php', ); $plugin = $plugin_files[ $provider ]; if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || ( is_multisite() && array_key_exists( $plugin, (array) get_site_option( 'active_sitewide_plugins', array() ) ) ) ) { return true; } foreach ( $connector['shortcodes'] as $shortcode ) { if ( shortcode_exists( $shortcode ) ) { return true; } } return false; } function afroiabo_booking_provider_valid( $provider ) { return in_array( $provider, array( 'calendly', 'google' ), true ) || isset( afroiabo_booking_connectors()[ $provider ] ); } /** An administrator chooses the existing public page, including builder-created forms. */ function afroiabo_booking_public_page( $url ) { $parts = wp_parse_url( $url ); $home = wp_parse_url( home_url( '/' ) ); if ( ! is_array( $parts ) || 'https' !== ( $parts['scheme'] ?? '' ) || strtolower( $parts['host'] ?? '' ) !== strtolower( $home['host'] ?? '' ) || ( $parts['port'] ?? 443 ) !== ( $home['port'] ?? 443 ) || isset( $parts['user'] ) || isset( $parts['pass'] ) || isset( $parts['query'] ) ) { return ''; } $post = get_post( url_to_postid( $url ) ); if ( ! $post || ! is_post_publicly_viewable( $post ) || ! empty( $post->post_password ) ) { return ''; } return esc_url_raw( get_permalink( $post ) ); } function afroiabo_booking_connection( $provider, $url, $display = 'page' ) { if ( ! afroiabo_booking_provider_valid( $provider ) ) { return new WP_Error( 'booking_provider', 'Selecciona una agenda compatible.' ); } $native = isset( afroiabo_booking_connectors()[ $provider ] ); if ( $native && ! afroiabo_booking_provider_active( $provider ) ) { return new WP_Error( 'booking_inactive', 'Activa el plugin de citas antes de conectar su agenda.' ); } $safe_url = $native ? afroiabo_booking_public_page( $url ) : afroiabo_safe_external_url( $url ); if ( ! $safe_url ) { return new WP_Error( 'booking_page', $native ? 'Elige una página publicada de esta web, sin contraseña ni parámetros, que contenga tu formulario de reservas.' : 'Añade un enlace HTTPS válido para activar las reservas.' ); } return array( 'provider' => $provider, 'url' => $safe_url, 'display' => $native && 'embedded' === $display ? 'embedded' : 'page', 'mode' => 'native_form', 'conversationalBooking' => false, ); } /** Read only: suggestions never activate a connector or expose unpublished pages. */ function afroiabo_booking_connector_status() { $connectors = afroiabo_booking_connectors(); $pages = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 250, 'orderby' => 'title', 'order' => 'ASC', 'has_password' => false ) ); $result = array(); foreach ( $connectors as $id => $connector ) { $suggestions = array(); foreach ( $pages as $page ) { if ( ! is_post_publicly_viewable( $page ) || ! empty( $page->post_password ) ) { continue; } foreach ( $connector['shortcodes'] as $shortcode ) { if ( has_shortcode( $page->post_content, $shortcode ) ) { $suggestions[] = array( 'title' => get_the_title( $page ), 'url' => get_permalink( $page ) ); break; } } } $result[] = array( 'id' => $id, 'name' => $connector['name'], 'active' => afroiabo_booking_provider_active( $id ), 'pages' => $suggestions, 'mode' => 'native_form', 'conversationalBooking' => false ); } return $result; }