| @@ -1,0 +1,282 @@ | ||
| 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 setup the WordPress Screen. | |
| 16 | + * | |
| 17 | + * @since 1.2.5 | |
| 18 | + */ | |
| 19 | +class VikBookingScreen | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Creates the option section within the WP Screen for VikBooking. | |
| 23 | + * | |
| 24 | + * @return void | |
| 25 | + */ | |
| 26 | + public static function options() | |
| 27 | + { | |
| 28 | + $app = JFactory::getApplication(); | |
| 29 | + | |
| 30 | + // make sure we are in VikBooking (back-end) | |
| 31 | + if (!$app->isAdmin() || $app->input->get('page') != 'vikbooking') | |
| 32 | + { | |
| 33 | + // abort | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + | |
| 37 | + // extract view from request | |
| 38 | + $view = $app->input->get('view', null); | |
| 39 | + | |
| 40 | + if (empty($view)) | |
| 41 | + { | |
| 42 | + // no view, try to check 'task' | |
| 43 | + $view = $app->input->get('task', 'dashboard'); | |
| 44 | + } | |
| 45 | + | |
| 46 | + // allowed views to display screen options | |
| 47 | + $allowed_views = array( | |
| 48 | + 'dashboard', | |
| 49 | + 'orders', | |
| 50 | + 'seasons', | |
| 51 | + 'restrictions', | |
| 52 | + 'trackings', | |
| 53 | + 'rooms', | |
| 54 | + ); | |
| 55 | + | |
| 56 | + if (!in_array($view, $allowed_views)) | |
| 57 | + { | |
| 58 | + // abort | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + // create pagination option | |
| 63 | + $args = array( | |
| 64 | + 'label' => __('Number of items per page:'), | |
| 65 | + 'default' => 20, | |
| 66 | + 'option' => 'vikbooking_list_limit', | |
| 67 | + ); | |
| 68 | + | |
| 69 | + add_screen_option('per_page', $args); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Filters a screen option value before it is set. | |
| 74 | + * | |
| 75 | + * @param boolean $skip Whether to save or skip saving the screen option value. Default false. | |
| 76 | + * @param string $option The option name. | |
| 77 | + * @param mixed $value The option value. | |
| 78 | + * | |
| 79 | + * @return mixed Returning false to the filter will skip saving the current option. | |
| 80 | + */ | |
| 81 | + public static function saveOption($skip, $option, $value) | |
| 82 | + { | |
| 83 | + $lookup = array( | |
| 84 | + 'vikbooking_list_limit', | |
| 85 | + ); | |
| 86 | + | |
| 87 | + if (in_array($option, $lookup)) | |
| 88 | + { | |
| 89 | + /** | |
| 90 | + * We also update the global list limit fallback option | |
| 91 | + * | |
| 92 | + * @since 1.4.0 | |
| 93 | + */ | |
| 94 | + if ($option == 'vikbooking_list_limit') | |
| 95 | + { | |
| 96 | + // cannot have a value lower than 1 | |
| 97 | + $value = max(array(1, (int) $value)); | |
| 98 | + // refresh cached value | |
| 99 | + JFactory::getApplication()->setUserState('com_vikbooking.limit', $value); | |
| 100 | + | |
| 101 | + update_option($option, (int) $value); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // return value to save it | |
| 105 | + return $value; | |
| 106 | + } | |
| 107 | + | |
| 108 | + // skip otherwise | |
| 109 | + return $skip; | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * Creates the Help tabs within the WP Screen for VikBooking. | |
| 114 | + * | |
| 115 | + * @param WP_Screen $screen The current screen instance. | |
| 116 | + * | |
| 117 | + * @return void | |
| 118 | + */ | |
| 119 | + public static function help($screen = null) | |
| 120 | + { | |
| 121 | + $app = JFactory::getApplication(); | |
| 122 | + | |
| 123 | + // make sure we are in VikBooking (back-end) | |
| 124 | + if (!$app->isAdmin() || $app->input->get('page') != 'vikbooking') | |
| 125 | + { | |
| 126 | + // abort | |
| 127 | + return; | |
| 128 | + } | |
| 129 | + | |
| 130 | + // make sure $screen is a valid instance | |
| 131 | + if (!class_exists('WP_Screen') || !$screen instanceof WP_Screen) | |
| 132 | + { | |
| 133 | + if (VIKBOOKING_DEBUG) | |
| 134 | + { | |
| 135 | + // trigger warning in case debug is enabled | |
| 136 | + trigger_error('Method ' . __METHOD__ . ' has been called too early', E_USER_WARNING); | |
| 137 | + } | |
| 138 | + // abort | |
| 139 | + return; | |
| 140 | + } | |
| 141 | + | |
| 142 | + // extract view from request | |
| 143 | + $view = $app->input->get('view', null); | |
| 144 | + | |
| 145 | + if (empty($view)) | |
| 146 | + { | |
| 147 | + // no view, try to check 'task' | |
| 148 | + $view = $app->input->get('task', 'dashboard'); | |
| 149 | + } | |
| 150 | + | |
| 151 | + // make sure the view is supported | |
| 152 | + if (!isset(static::$lookup[$view])) | |
| 153 | + { | |
| 154 | + // view not supported | |
| 155 | + return; | |
| 156 | + } | |
| 157 | + | |
| 158 | + // check if we have a link to an existing item | |
| 159 | + if (is_string(static::$lookup[$view])) | |
| 160 | + { | |
| 161 | + // use the linked element | |
| 162 | + $view = static::$lookup[$view]; | |
| 163 | + } | |
| 164 | + | |
| 165 | + // check if the view documentation has been already cached | |
| 166 | + $doc = get_transient('vikbooking_screen_' . $view); | |
| 167 | + | |
| 168 | + if (!$doc) | |
| 169 | + { | |
| 170 | + // evaluate if we should stop using HELP tabs after 3 failed attempts | |
| 171 | + $fail = (int) get_option('vikbooking_screen_failed_attempts', 0); | |
| 172 | + | |
| 173 | + if ($fail >= 5) | |
| 174 | + { | |
| 175 | + // Do not proceed as we hit too many failure attempts contiguously. | |
| 176 | + // Reset 'vikbooking_screen_failed_attempts' option to restart using HELP tabs. | |
| 177 | + return; | |
| 178 | + } | |
| 179 | + | |
| 180 | + // create POST arguments | |
| 181 | + $args = array( | |
| 182 | + 'documentation_alias' => 'vik-booking', | |
| 183 | + 'lang' => substr(JFactory::getLanguage()->getTag(), 0, 2), | |
| 184 | + ); | |
| 185 | + | |
| 186 | + $args = array_merge($args, static::$lookup[$view]); | |
| 187 | + | |
| 188 | + $http = new JHttp(); | |
| 189 | + | |
| 190 | + // make HTTP post | |
| 191 | + $response = $http->post('https://vikwp.com/index.php?option=com_vikhelpdesk&format=json', $args); | |
| 192 | + | |
| 193 | + if ($response->code != 200) | |
| 194 | + { | |
| 195 | + // increase total number of failed attempts | |
| 196 | + update_option('vikbooking_screen_failed_attempts', $fail + 1); | |
| 197 | + | |
| 198 | + return; | |
| 199 | + } | |
| 200 | + | |
| 201 | + // try to decode JSON | |
| 202 | + $doc = json_decode($response->body); | |
| 203 | + | |
| 204 | + if (!is_array($doc)) | |
| 205 | + { | |
| 206 | + // increase total number of failed attempts | |
| 207 | + update_option('vikbooking_screen_failed_attempts', $fail + 1); | |
| 208 | + | |
| 209 | + return; | |
| 210 | + } | |
| 211 | + | |
| 212 | + // reset total number of failed attempts | |
| 213 | + update_option('vikbooking_screen_failed_attempts', 0); | |
| 214 | + | |
| 215 | + // cache retrieved documentation (for one week only) | |
| 216 | + set_transient('vikbooking_screen_' . $view, json_encode($doc), WEEK_IN_SECONDS); | |
| 217 | + } | |
| 218 | + else | |
| 219 | + { | |
| 220 | + // JSON decode the cached documentation | |
| 221 | + $doc = json_decode($doc); | |
| 222 | + } | |
| 223 | + | |
| 224 | + // iterate category sections | |
| 225 | + foreach ($doc as $i => $cat) | |
| 226 | + { | |
| 227 | + // add subcategory as help tab | |
| 228 | + $screen->add_help_tab(array( | |
| 229 | + 'id' => 'vikbooking-' . $view . '-' . ($i + 1), | |
| 230 | + 'title' => $cat->contentTitle, | |
| 231 | + 'content' => $cat->content, | |
| 232 | + )); | |
| 233 | + } | |
| 234 | + | |
| 235 | + // add help sidebar | |
| 236 | + $screen->set_help_sidebar( | |
| 237 | + '<p><strong>' . __('For more information:') . '</strong></p>' . | |
| 238 | + '<p><a href="https://vikwp.com/documentation/vik-booking/" target="_blank">VikWP.com</a></p>' | |
| 239 | + ); | |
| 240 | + } | |
| 241 | + | |
| 242 | + /** | |
| 243 | + * Clears the cache for the specified view, if specified. | |
| 244 | + * | |
| 245 | + * @param string|null $view Clear the cache for the specified view (if specified) | |
| 246 | + * or for all the existing views. | |
| 247 | + * | |
| 248 | + * @return void | |
| 249 | + */ | |
| 250 | + public static function clearCache($view = null) | |
| 251 | + { | |
| 252 | + if ($view) | |
| 253 | + { | |
| 254 | + delete_transient('vikbooking_screen_' . $view); | |
| 255 | + } | |
| 256 | + else | |
| 257 | + { | |
| 258 | + foreach (static::$lookup as $view => $args) | |
| 259 | + { | |
| 260 | + if (is_array($args)) | |
| 261 | + { | |
| 262 | + delete_transient('vikbooking_screen_' . $view); | |
| 263 | + } | |
| 264 | + } | |
| 265 | + | |
| 266 | + // delete settings too | |
| 267 | + delete_option('vikbooking_screen_failed_attempts'); | |
| 268 | + delete_option('vikbooking_list_limit'); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * Lookup used to retrieve the arguments for the HTTP request. | |
| 274 | + * | |
| 275 | + * @var array | |
| 276 | + */ | |
| 277 | + protected static $lookup = array( | |
| 278 | + /** | |
| 279 | + * @todo define documentation lookup here | |
| 280 | + */ | |
| 281 | + ); | |
| 282 | +} | |