Sindbad~EG File Manager

Current Path : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/
Upload File :
Current File : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/ScheduleTourController.php

<?php

namespace RealPress\Controllers;

use RealPress\Helpers\RestApi;
use RealPress\Models\UserModel;
use WP_REST_Server;

class ScheduleTourController {

	public function __construct() {
		add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
	}

	/**
	 * @return void
	 */
	public function register_rest_routes() {
		do_action( 'realpress/rest-api/before-register' );

		register_rest_route(
			RestApi::generate_namespace(),
			'/schedule-tour',
			array(
				'methods'             => WP_REST_Server::CREATABLE,
				'callback'            => array( $this, 'send_message' ),
				'permission_callback' => '__return_true',
			),
		);
	}

	/**
	 * @param \WP_REST_Request $request
	 *
	 * @return \WP_REST_Response
	 */
	public function send_message( \WP_REST_Request $request ) {
		$nonce = $request->get_header( 'X-WP-Nonce' );
		if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
			return RestApi::error( esc_html__( 'Invalid.', 'realpress' ), 403 );
		}

		$params = $request->get_params();
		if ( empty( $params['id'] ) ) {
			return RestApi::error( esc_html__( 'Failed.', 'realpress' ), 400 );
		}

		$agent_id = $params['agent_id'] ?? '';
		$user     = get_userdata( $agent_id );

		if ( empty( $user ) ) {
			return RestApi::error( esc_html__( 'The agent does not exist.', 'realpress' ), 400 );
		}

		$email_target = $user->user_email;

		if ( empty( $params['date'] ) ) {
			return RestApi::error( esc_html__( 'The date is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['tour_type'] ) ) {
			return RestApi::error( esc_html__( 'The tour type is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['time'] ) ) {
			return RestApi::error( esc_html__( 'The time is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['name'] ) ) {
			return RestApi::error( esc_html__( 'The name is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['phone'] ) ) {
			return RestApi::error( esc_html__( 'The phone is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['email'] ) ) {
			return RestApi::error( esc_html__( 'The email is required.', 'realpress' ), 400 );
		}

		if ( ! is_email( $params['email'] ) ) {
			return RestApi::error( esc_html__( 'The email is invalid.', 'realpress' ), 400 );
		}

		if ( empty( $params['message'] ) ) {
			return RestApi::error( esc_html__( 'The message is required.', 'realpress' ), 400 );
		}

		if ( empty( $params['terms_and_conditions'] ) ) {
			return RestApi::error( esc_html__( 'Please accept the Terms and Conditions.', 'realpress' ), 400 );
		}

		$args = array(
			'user_id'   => $params['user_id'],
			'id'        => $params['id'],
			'date'      => $params['date'],
			'time'      => $params['time'],
			'tour_type' => $params['tour_type'],
			'name'      => $params['name'],
			'phone'     => $params['phone'],
			'email'     => $params['email'],
			'message'   => $params['message'],
		);

		$subject = $this->generate_email_subject( $args );
		$content = $this->generate_email_content( $args );

		$header = $this->get_email_header();

		if ( $params['cc_admin'] === 'on' ) {
			$header[] = 'Cc: ' . get_option( 'admin_email' );
		}

		$send_message = wp_mail( $email_target, $subject, $content, $header );

		if ( $send_message ) {
			return RestApi::success( esc_html__( 'Email sent successfully.', 'realpress' ) );
		} else {
			return RestApi::error( esc_html__( 'Make sure the Email function is working on your server.', 'realpress' ), 422 );
		}
	}

	/**
	 * @return mixed|void
	 */
	public function get_email_header() {
		return apply_filters( 'realpress/filter/schedule-tour/email/headers', array( 'Content-Type: text/html;' ) );
	}

	/**
	 * @param $args
	 *
	 * @return mixed|void
	 */
	public function generate_email_subject( $args ) {
		$subject = '';

		if ( ! empty( $args['name'] ) ) {
			$subject = sprintf( esc_html__( 'A new message Schedule Tour was sent by %s.', 'realpress' ), $args['name'] );
		}

		return apply_filters( 'realpress/filter/schedule-tour/email/subject', $subject, $args );
	}

	public function generate_email_content( $args ) {
		$content = '';

		if ( ! empty( $args['name'] ) ) {
			$content .= sprintf( esc_html__( 'You have received a new message from: %s', 'realpress' ), $args['name'] );
			$content .= '<br>';
		}
		if ( ! empty( $args['date'] ) ) {
			$content .= sprintf( esc_html__( 'Date: %s', 'realpress' ), $args['date'] );
			$content .= '<br>';
		}
		if ( ! empty( $args['time'] ) ) {
			$content .= sprintf( esc_html__( 'Time: %s', 'realpress' ), $args['time'] );
			$content .= '<br>';
		}
		if ( ! empty( $args['tour_type'] ) ) {
			$content .= sprintf( esc_html__( 'Tour Type: %s', 'realpress' ), $args['tour_type'] );
			$content .= '<br>';
		}
		if ( ! empty( $args['phone'] ) ) {
			$content .= sprintf( esc_html__( 'Phone: %s', 'realpress' ), $args['phone'] );
			$content .= '<br>';
		}

		if ( ! empty( $args['email'] ) ) {
			$content .= sprintf( esc_html__( 'Email: %s', 'realpress' ), $args['email'] );
			$content .= '<br>';
		}

		if ( ! empty( $args['message'] ) ) {
			$content .= sprintf( esc_html__( 'Message: %s', 'realpress' ), $args['message'] );
			$content .= '<br>';
		}

		return apply_filters( 'realpress/filter/schedule-tour/email/content', $content, $args );
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists