<?php
declare(strict_types=1);
use MPBC\Data\Mapper\Mappers;
use MPBC\Data\{ Booking, Form, Notification, Property, Unit, Variation };
use MPBC\Shortcode\Booking_Calendar_Shortcode;
use MPBC\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function mpbc(): Plugin {
return Plugin::get_instance();
}
/**
* Unlike array_merge_recursive(), this function does not merge primitive types
* with the same keys into an array.
*
* @author Daniel <[email protected]>
* @author Gabriel Sobrinho <[email protected]>
*/
function mpbc_array_merge_recursive_distinct( array $array1, array $array2 ): array {
$merged = $array1;
foreach ( $array2 as $key => &$value ) {
$is_numeric_item = is_int( $key );
if ( $is_numeric_item ) {
$new_value = $value;
} elseif ( isset( $merged[ $key ] ) && ( is_array( $value ) || is_array( $merged[ $key ] ) ) ) {
$new_value = mpbc_array_merge_recursive_distinct( (array) $merged[ $key ], (array) $value );
} else {
$new_value = $value;
}
if ( $is_numeric_item ) {
$merged[] = $new_value;
} else {
$merged[ $key ] = $new_value;
}
}
unset( $value );
return $merged;
}
function mpbc_display_booking_calendar( int $property_id, array $atts = array() ): void {
$shortcode_atts = array( 'property_id' => $property_id ) + $atts;
$shortcode = new Booking_Calendar_Shortcode();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped HTML
echo $shortcode->render( $shortcode_atts );
}
function mpbc_display_template( ...$templates ): void {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped template
echo mpbc_render_template( ...$templates );
}
/**
* <pre>
* render_template( 'package/sub-package/template-file.php' );
* render_template( 'template-file-1.php', 'template-file-2.php', $template_args );
* </pre>
*
* @param array $templates One or more templates to locate and render (with file
* extension ".php" but without "templates/" prefix).
* @return string Template HTML or empty string.
*/
function mpbc_render_template( ...$templates ): string {
if ( empty( $templates ) ) {
return '';
}
$template_file = '';
$template_args = array();
if ( is_array( $templates[ count( $templates ) - 1 ] ) ) {
$template_args = array_pop( $templates );
}
// Locate the template file
foreach ( $templates as $template ) {
// Locate template in the theme
$template_file = locate_template( "motopress/booking-calendar/{$template}" );
// Locate template in the plugin
if ( $template_file === '' ) {
$plugin_file = mpbc()->get_file_path( "templates/{$template}" );
if ( is_file( $plugin_file ) ) {
$template_file = $plugin_file;
}
}
// Stop searching if found one
if ( $template_file !== '' ) {
break;
}
}
/**
* @param string $template_file Absolute path to the file.
* @param array $template_args
* @param string $templates All requested templates.
*/
$template_file = apply_filters( 'mpbc_locate_template', $template_file, $template_args, $templates );
// Render template
if ( $template_file !== '' ) {
if ( ! empty( $template_args ) ) {
extract( $template_args );
}
ob_start();
require $template_file;
$output = ob_get_clean();
return $output;
} else {
return '';
}
}
function mpbc_get_booking( int $booking_id, bool $force_reload = false ): ?Booking {
return Mappers::booking()->read( $booking_id, $force_reload );
}
function mpbc_get_form( int $form_id, bool $force_reload = false ): ?Form {
return Mappers::form()->read( $form_id, $force_reload );
}
function mpbc_get_notification( int $notification_id, bool $force_reload = false ): ?Notification {
return Mappers::notification()->read( $notification_id, $force_reload );
}
function mpbc_get_property( int $property_id, bool $force_reload = false ): ?Property {
return Mappers::property()->read( $property_id, $force_reload );
}
function mpbc_get_variation( int $variation_id, bool $force_reload = false ): ?Variation {
return Mappers::variation()->read( $variation_id, $force_reload );
}
function mpbc_get_unit( int $unit_id, bool $force_reload = false ): ?Unit {
return Mappers::unit()->read( $unit_id, $force_reload );
}
function mpbc_get_current_page_url( bool $strip_flag = false ): string {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return '';
}
$url = sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) );
if ( $strip_flag ) {
/** @var string|false $flag */
$flag = strstr( $url, '#' );
if ( $flag !== false ) {
$url = substr( $url, 0, -strlen( $flag ) );
}
}
return $url;
}
function mpbc_get_custom_fields( int $property_id ): array {
$custom_fields = array();
$property = mpbc_get_property( $property_id );
if ( ! is_null( $property ) ) {
$form = $property->get_form();
if ( ! is_null( $form ) ) {
$custom_fields = $form->get_structure();
}
}
/**
* @param array $custom_fields
* @param int $property_id
*/
$custom_fields = apply_filters( 'mpbc_get_custom_fields', $custom_fields, $property_id );
return $custom_fields;
}
function mpbc_prefix( string $string, string $prefix = 'mpbc_' ): string {
if ( strpos( $string, $prefix ) !== 0 ) {
return $prefix . $string;
} else {
return $string; // Already prefixed
}
}
function mpbc_unprefix( string $string, string $prefix = 'mpbc_' ): string {
if ( strpos( $string, $prefix ) === 0 ) {
// Remove the public prefix
return substr( $string, strlen( $prefix ) );
} elseif ( strpos( $string, "_{$prefix}" ) === 0 ) {
// Remove the private prefix
return substr( $string, strlen( $prefix ) + 1 );
} else {
// There is no such prefix in the string
return $string;
}
}
/**
* @param bool $wp_error Whether to return a WP_Error on failure. False by
* default.
* @return bool|\WP_Error True on success. False or WP_Error on failure.
*/
function mpbc_set_post_status( int $post_id, string $post_status, bool $wp_error = false ) {
$updated = wp_update_post(
array(
'ID' => $post_id,
'post_status' => $post_status,
),
true
);
if ( ! is_wp_error( $updated ) ) {
return true;
} else {
return $wp_error ? $updated : false;
}
}
function mpbc_uuid4(): string {
$uuid4 = sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
wp_rand( 0, 0xffff ),
wp_rand( 0, 0xffff ),
wp_rand( 0, 0xffff ),
wp_rand( 0, 0x0fff ) | 0x4000,
wp_rand( 0, 0x3fff ) | 0x8000,
wp_rand( 0, 0xffff ),
wp_rand( 0, 0xffff ),
wp_rand( 0, 0xffff )
);
return $uuid4;
}
function mpbc_verify_nonce( string $nonce_field, string $action ): bool {
if ( ! isset( $_REQUEST[ $nonce_field ] ) ) {
return false;
}
// phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput -- doing nonce verification
$nonce = $_REQUEST[ $nonce_field ];
return (bool) wp_verify_nonce( $nonce, $action );
}