| 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 |
|
| 51 |
$task = JFactory::getApplication()->input->get('task'); |
| 52 |
|
| 53 |
// execute the controller |
| 54 |
$controller = JController::getInstance('VikBooking', VIKBOOKING_BASE); |
| 55 |
$controller->execute($task); |
| 56 |
|
| 57 |
// redirect if set by the controller |
| 58 |
$controller->redirect(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Fires after the controller of VikBooking is dispatched. |
| 62 |
* Useful to include web resources (CSS and JS). |
| 63 |
* |
| 64 |
* If the controller terminates the process (exit or die), |
| 65 |
* this hook won't be fired. |
| 66 |
* |
| 67 |
* @since 1.0 |
| 68 |
*/ |
| 69 |
do_action('vikbooking_after_dispatch'); |
| 70 |
|
| 71 |
// capture the response echoed by the controller |
| 72 |
static::$response = ob_get_contents(); |
| 73 |
|
| 74 |
// clean the buffer |
| 75 |
ob_end_clean(); |
| 76 |
|
| 77 |
/** |
| 78 |
* We prepend the system messages to the body as we are displaying HTML contents. |
| 79 |
* We no longer do this in the getHtml() method to avoid issues with the PHP session. |
| 80 |
* |
| 81 |
* @since 1.3.5 |
| 82 |
*/ |
| 83 |
static::$response = VikBookingLayoutHelper::renderSystemMessages($queue = null, $echo = false) . static::$response; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Renders the obtained response in HTML format. |
| 88 |
* Note: HEADERS ALREADY SENT |
| 89 |
* |
| 90 |
* @param boolean $return True to return the contents. |
| 91 |
* False to echo them directly. |
| 92 |
* |
| 93 |
* @return void|string |
| 94 |
* |
| 95 |
* @uses process() |
| 96 |
*/ |
| 97 |
public static function getHtml($return = false) |
| 98 |
{ |
| 99 |
// check if the response is set |
| 100 |
if (is_null(static::$response)) |
| 101 |
{ |
| 102 |
// obtain the response |
| 103 |
static::process(); |
| 104 |
} |
| 105 |
|
| 106 |
// get response |
| 107 |
$body = static::$response; |
| 108 |
|
| 109 |
/** |
| 110 |
* Never JSON encode the HTML when the caller explictly |
| 111 |
* requested to this method to return it instead of |
| 112 |
* directly echoing it. |
| 113 |
* |
| 114 |
* This is needed in case the shortcode is executed by |
| 115 |
* a third-party plugin to display a preview of the |
| 116 |
* page, which could be made via AJAX (see Elementor). |
| 117 |
* |
| 118 |
* @since 1.5.5 |
| 119 |
*/ |
| 120 |
if (wp_doing_ajax() && !$return) |
| 121 |
{ |
| 122 |
/** |
| 123 |
* Include the AJAX scripts. |
| 124 |
* |
| 125 |
* @since 1.3.9 |
| 126 |
*/ |
| 127 |
$body .= JFactory::getDocument()->getAjaxScripts(); |
| 128 |
|
| 129 |
// if we are doing AJAX, encode the response in JSON format |
| 130 |
$body = json_encode(array($body)); |
| 131 |
} |
| 132 |
// otherwise render the body |
| 133 |
else |
| 134 |
{ |
| 135 |
$body = VikBookingLayoutHelper::renderBody(static::$response, false); |
| 136 |
} |
| 137 |
|
| 138 |
if ($return) |
| 139 |
{ |
| 140 |
return $body; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
echo $body; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the HTML response without altering it. |
| 150 |
* |
| 151 |
* @return string The HTML response. |
| 152 |
* |
| 153 |
* @uses process() |
| 154 |
*/ |
| 155 |
public static function getResponse() |
| 156 |
{ |
| 157 |
// check if the response is set |
| 158 |
if (is_null(static::$response)) |
| 159 |
{ |
| 160 |
// obtain the response |
| 161 |
static::process(); |
| 162 |
} |
| 163 |
|
| 164 |
return static::$response; |
| 165 |
} |
| 166 |
} |
| 167 |
|