PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / libraries / system / body.php
vikappointments / libraries / system Last commit date
assets.php 1 month ago body.php 4 years ago builder.php 1 month ago cron.php 4 years ago feedback.php 4 years ago gutenberg.php 2 years ago install.php 1 month ago mce.php 4 years ago rssfeeds.php 5 months ago screen.php 4 years ago
body.php
167 lines
1 <?php
2 /**
3 * @package VikAppointments - Libraries
4 * @subpackage system
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 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 VikAppointmentsBody
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 $task = JFactory::getApplication()->input->get('task');
41
42 // start capturing the buffer
43 ob_start();
44
45 /**
46 * Fires before the controller of VikAppointments is dispatched.
47 * Useful to require libraries and to check user global permissions.
48 *
49 * @since 1.0
50 */
51 do_action('vikappointments_before_dispatch');
52
53 // execute the controller
54 $controller = JController::getInstance('VikAppointments', VIKAPPOINTMENTS_BASE);
55 $controller->execute($task);
56
57 // redirect if set by the controller
58 $controller->redirect();
59
60 /**
61 * Fires after the controller of VikAppointments 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('vikappointments_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 * Prepend system messages to the body as we are displaying HTML contents.
79 *
80 * @since 1.1.7 Moved within the process() method in order to avoid weird
81 * behaviors in case the headers are already sent.
82 */
83 static::$response = VikAppointmentsLayoutHelper::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.2.6
119 */
120 if (wp_doing_ajax() && !$return)
121 {
122 /**
123 * Include the AJAX scripts.
124 *
125 * @since 1.1.8
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 else
133 {
134 // otherwise render the body
135 $body = VikAppointmentsLayoutHelper::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