| @@ -1,0 +1,168 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking - Libraries | |
| 4 | + * @subpackage system | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2018 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Helper class to handle the body of the page. | |
| 16 | + * | |
| 17 | + * @since 1.0 | |
| 18 | + */ | |
| 19 | +class VikBookingBody | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * The response processed by the request. | |
| 23 | + * | |
| 24 | + * @var string | |
| 25 | + */ | |
| 26 | + protected static $response = null; | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Executes the MVC internal framework to obtain | |
| 30 | + * the requested HTML page body. | |
| 31 | + * | |
| 32 | + * Note: HEADERS NOT SENT | |
| 33 | + * | |
| 34 | + * @return void | |
| 35 | + */ | |
| 36 | + public static function process() | |
| 37 | + { | |
| 38 | + jimport('joomla.application.component.controller'); | |
| 39 | + | |
| 40 | + // start capturing the buffer | |
| 41 | + ob_start(); | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Fires before the controller of VikBooking is dispatched. | |
| 45 | + * Useful to require libraries and to check user global permissions. | |
| 46 | + * | |
| 47 | + * @since 1.0 | |
| 48 | + */ | |
| 49 | + // do_action('vikbooking_before_dispatch'); | |
| 50 | + VBOFactory::getPlatform()->getDispatcher()->trigger('onBeforeDispatchVikBooking'); | |
| 51 | + | |
| 52 | + $task = JFactory::getApplication()->input->get('task'); | |
| 53 | + | |
| 54 | + // execute the controller | |
| 55 | + $controller = JController::getInstance('VikBooking', VIKBOOKING_BASE); | |
| 56 | + $controller->execute($task); | |
| 57 | + | |
| 58 | + // redirect if set by the controller | |
| 59 | + $controller->redirect(); | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * Fires after the controller of VikBooking is dispatched. | |
| 63 | + * Useful to include web resources (CSS and JS). | |
| 64 | + * | |
| 65 | + * If the controller terminates the process (exit or die), | |
| 66 | + * this hook won't be fired. | |
| 67 | + * | |
| 68 | + * @since 1.0 | |
| 69 | + */ | |
| 70 | + // do_action('vikbooking_after_dispatch'); | |
| 71 | + VBOFactory::getPlatform()->getDispatcher()->trigger('onAfterDispatchVikBooking'); | |
| 72 | + | |
| 73 | + // capture the response echoed by the controller | |
| 74 | + static::$response = ob_get_contents(); | |
| 75 | + | |
| 76 | + // clean the buffer | |
| 77 | + ob_end_clean(); | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * We prepend the system messages to the body as we are displaying HTML contents. | |
| 81 | + * We no longer do this in the getHtml() method to avoid issues with the PHP session. | |
| 82 | + * | |
| 83 | + * @since 1.3.5 | |
| 84 | + */ | |
| 85 | + static::$response = VikBookingLayoutHelper::renderSystemMessages($queue = null, $echo = false) . static::$response; | |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Renders the obtained response in HTML format. | |
| 90 | + * Note: HEADERS ALREADY SENT | |
| 91 | + * | |
| 92 | + * @param boolean $return True to return the contents. | |
| 93 | + * False to echo them directly. | |
| 94 | + * | |
| 95 | + * @return void|string | |
| 96 | + * | |
| 97 | + * @uses process() | |
| 98 | + */ | |
| 99 | + public static function getHtml($return = false) | |
| 100 | + { | |
| 101 | + // check if the response is set | |
| 102 | + if (is_null(static::$response)) | |
| 103 | + { | |
| 104 | + // obtain the response | |
| 105 | + static::process(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // get response | |
| 109 | + $body = static::$response; | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Never JSON encode the HTML when the caller explictly | |
| 113 | + * requested to this method to return it instead of | |
| 114 | + * directly echoing it. | |
| 115 | + * | |
| 116 | + * This is needed in case the shortcode is executed by | |
| 117 | + * a third-party plugin to display a preview of the | |
| 118 | + * page, which could be made via AJAX (see Elementor). | |
| 119 | + * | |
| 120 | + * @since 1.5.5 | |
| 121 | + */ | |
| 122 | + if (wp_doing_ajax() && !$return) | |
| 123 | + { | |
| 124 | + /** | |
| 125 | + * Include the AJAX scripts. | |
| 126 | + * | |
| 127 | + * @since 1.3.9 | |
| 128 | + */ | |
| 129 | + $body .= JFactory::getDocument()->getAjaxScripts(); | |
| 130 | + | |
| 131 | + // if we are doing AJAX, encode the response in JSON format | |
| 132 | + $body = json_encode(array($body)); | |
| 133 | + } | |
| 134 | + // otherwise render the body | |
| 135 | + else | |
| 136 | + { | |
| 137 | + $body = VikBookingLayoutHelper::renderBody(static::$response, false); | |
| 138 | + } | |
| 139 | + | |
| 140 | + if ($return) | |
| 141 | + { | |
| 142 | + return $body; | |
| 143 | + } | |
| 144 | + else | |
| 145 | + { | |
| 146 | + echo $body; | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Returns the HTML response without altering it. | |
| 152 | + * | |
| 153 | + * @return string The HTML response. | |
| 154 | + * | |
| 155 | + * @uses process() | |
| 156 | + */ | |
| 157 | + public static function getResponse() | |
| 158 | + { | |
| 159 | + // check if the response is set | |
| 160 | + if (is_null(static::$response)) | |
| 161 | + { | |
| 162 | + // obtain the response | |
| 163 | + static::process(); | |
| 164 | + } | |
| 165 | + | |
| 166 | + return static::$response; | |
| 167 | + } | |
| 168 | +} | |