acl.php
2 days ago
feedback.php
2 days ago
license.php
2 days ago
override.php
2 days ago
rss.php
2 days ago
shortcode.php
2 days ago
shortcodes.php
2 days ago
shortcode.php
238 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 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 | VAPLoader::import('libraries.mvc.controllers.admin'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments plugin Shortcode controller. |
| 18 | * |
| 19 | * @since 1.0 |
| 20 | */ |
| 21 | class VikAppointmentsControllerShortcode extends VAPControllerAdmin |
| 22 | { |
| 23 | /** |
| 24 | * Save new record task. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function savenew() |
| 29 | { |
| 30 | if ($this->save()) |
| 31 | { |
| 32 | // get return URL |
| 33 | $encoded = JFactory::getApplication()->input->getBase64('return', ''); |
| 34 | |
| 35 | $this->setRedirect('admin.php?page=vikappointments&task=shortcodes.add&return=' . $encoded); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Save and close task. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function saveclose() |
| 45 | { |
| 46 | if ($this->save()) |
| 47 | { |
| 48 | // get return URL |
| 49 | $encoded = JFactory::getApplication()->input->getBase64('return', ''); |
| 50 | |
| 51 | $this->setRedirect('admin.php?page=vikappointments&view=shortcodes&return=' . $encoded); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Save (and stay) task. |
| 57 | * |
| 58 | * @return boolean |
| 59 | * |
| 60 | * @since 1.2.12 This task now supports also AJAX requests. |
| 61 | */ |
| 62 | public function save($close = 0) |
| 63 | { |
| 64 | $app = JFactory::getApplication(); |
| 65 | |
| 66 | // get return URL |
| 67 | $encoded = $app->input->getBase64('return', ''); |
| 68 | |
| 69 | // set up redirect url in case of error |
| 70 | $this->setRedirect('admin.php?page=vikappointments&view=shortcodes' . ($encoded ? '&return=' . $encoded : '')); |
| 71 | |
| 72 | /** |
| 73 | * Added token validation. |
| 74 | * |
| 75 | * @since 1.2.12 |
| 76 | */ |
| 77 | if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 78 | { |
| 79 | if (wp_doing_ajax()) |
| 80 | { |
| 81 | VAPHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 82 | } |
| 83 | |
| 84 | // missing CSRF token |
| 85 | $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error'); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | // make sure the user is authorised to change shortcodes |
| 90 | if (!JFactory::getUser()->authorise('core.admin', 'com_vikappointments')) |
| 91 | { |
| 92 | if (wp_doing_ajax()) |
| 93 | { |
| 94 | VAPHttpDocument::getInstance($app)->close(403, JText::translate('JERROR_ALERTNOAUTHOR')); |
| 95 | } |
| 96 | |
| 97 | // back to main list, not authorised to create/edit records |
| 98 | $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error'); |
| 99 | $this->cancel(); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // get item from request |
| 104 | $data = $this->model->getFormData(); |
| 105 | |
| 106 | // dispatch model to save the item |
| 107 | $id = $this->model->save($data); |
| 108 | |
| 109 | if (!$id) |
| 110 | { |
| 111 | // get string error |
| 112 | $error = $this->model->getError(null, true); |
| 113 | $error = JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error); |
| 114 | |
| 115 | if (wp_doing_ajax()) |
| 116 | { |
| 117 | VAPHttpDocument::getInstance($app)->close(500, $error); |
| 118 | } |
| 119 | |
| 120 | // display error message |
| 121 | $app->enqueueMessage($error, 'error'); |
| 122 | |
| 123 | $url = 'admin.php?page=vikappointments&view=shortcode'; |
| 124 | |
| 125 | if ($data->id) |
| 126 | { |
| 127 | $url .= '&cid[]=' . $data->id; |
| 128 | } |
| 129 | |
| 130 | // redirect to new/edit page |
| 131 | $this->setRedirect($url); |
| 132 | |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (wp_doing_ajax()) |
| 137 | { |
| 138 | $this->sendJSON($this->model->getItem($id)); |
| 139 | } |
| 140 | |
| 141 | // display generic successful message |
| 142 | $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 143 | |
| 144 | $this->setRedirect('admin.php?page=vikappointments&task=shortcodes.edit&cid[]=' . $id . '&return=' . $encoded); |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * AJAX end-point used to access the parameters of the shortcodes. |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | public function params() |
| 155 | { |
| 156 | $input = JFactory::getApplication()->input; |
| 157 | |
| 158 | $id = $input->getInt('id', 0); |
| 159 | $type = $input->getString('type', ''); |
| 160 | |
| 161 | $model = $this->getModel(); |
| 162 | |
| 163 | // dispatch model to get the item (an empty ITEM if not exists) |
| 164 | $item = $this->model->getItem($id); |
| 165 | |
| 166 | // inject the type to load the right form |
| 167 | $item->type = $type; |
| 168 | |
| 169 | // obtain the type form |
| 170 | $form = $this->model->getTypeForm($item); |
| 171 | |
| 172 | // if the form doesn't exist, the type is probably empty |
| 173 | if (!$form) |
| 174 | { |
| 175 | // return an empty HTML |
| 176 | $json = ""; |
| 177 | } |
| 178 | // render the form and encode the response |
| 179 | else |
| 180 | { |
| 181 | $args = json_decode($item->json); |
| 182 | $json = json_encode($form->renderForm($args)); |
| 183 | } |
| 184 | |
| 185 | $this->sendJSON($json); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Creates a page on WordPress with the requested Shortcode inside it. |
| 190 | * This is useful to automatically link Shortcodes in pages with no manual actions. |
| 191 | * |
| 192 | * @return void |
| 193 | * |
| 194 | * @since 1.1.9 |
| 195 | */ |
| 196 | public function addpage() |
| 197 | { |
| 198 | $app = JFactory::getApplication(); |
| 199 | $input = $app->input; |
| 200 | |
| 201 | // get return URL |
| 202 | $encoded = $input->getBase64('return', ''); |
| 203 | |
| 204 | // always back to shortcodes list |
| 205 | $this->setRedirect('admin.php?page=vikappointments&view=shortcodes' . ($encoded ? '&return=' . $encoded : '')); |
| 206 | |
| 207 | // make sure the user is authorised to change shortcodes |
| 208 | if (!JFactory::getUser()->authorise('core.admin', 'com_vikappointments')) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | // get selected shortcodes |
| 214 | $cid = $input->getUint('cid', array()); |
| 215 | |
| 216 | // attempt to assign the shortcodes to a page |
| 217 | if ($this->model->addPage($cid)) |
| 218 | { |
| 219 | // add success message and redirect |
| 220 | $app->enqueueMessage(JText::translate('VAP_SHORTCODE_CREATE_PAGE_SUCCESS')); |
| 221 | } |
| 222 | |
| 223 | // fetch all registered errors (if any) |
| 224 | $errors = $this->model->getErrors(); |
| 225 | |
| 226 | foreach ($errors as $error) |
| 227 | { |
| 228 | if ($error instanceof Exception) |
| 229 | { |
| 230 | $error = $error->getMessage(); |
| 231 | } |
| 232 | |
| 233 | // enqueue error message |
| 234 | $app->enqueueMessage($error, 'error'); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 |