| 1 |
<?php |
| 2 |
/** |
| 3 |
* Booking resource capacity settings page. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
require_once WPBC_PLUGIN_DIR . '/includes/page-resource-capacity/class-wpbc-settings-api-capacity.php'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Check whether the Resource Capacity page is active. |
| 16 |
* |
| 17 |
* @return bool |
| 18 |
*/ |
| 19 |
function wpbc_resource_capacity__is_page() { |
| 20 |
|
| 21 |
if ( ! is_admin() ) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 26 |
$page = isset( $_REQUEST['page'] ) ? sanitize_key( wp_unslash( $_REQUEST['page'] ) ) : ''; |
| 27 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 28 |
$tab = isset( $_REQUEST['tab'] ) ? sanitize_key( wp_unslash( $_REQUEST['tab'] ) ) : ''; |
| 29 |
|
| 30 |
return ( 'wpbc-resources' === $page && 'capacity' === $tab ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Redirect old dashboard links that target the removed General Settings capacity section. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
function wpbc_resource_capacity__redirect_legacy_settings_url() { |
| 39 |
|
| 40 |
if ( ! is_admin() ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 45 |
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; |
| 46 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 47 |
$scroll_to_section = isset( $_GET['scroll_to_section'] ) ? sanitize_key( wp_unslash( $_GET['scroll_to_section'] ) ) : ''; |
| 48 |
|
| 49 |
if ( 'wpbc-settings' !== $page || 'wpbc_general_settings_capacity_tab' !== $scroll_to_section ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
wp_safe_redirect( wpbc_get_resource_capacity_url() ); |
| 54 |
exit; |
| 55 |
} |
| 56 |
add_action( 'admin_init', 'wpbc_resource_capacity__redirect_legacy_settings_url', 1 ); |
| 57 |
|
| 58 |
/** |
| 59 |
* Manage capability for Capacity Rules. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
function wpbc_resource_capacity__get_manage_cap() { |
| 64 |
|
| 65 |
$min_user_role = get_bk_option( 'booking_user_role_settings' ); |
| 66 |
$capability = array( |
| 67 |
'administrator' => 'activate_plugins', |
| 68 |
'editor' => 'publish_pages', |
| 69 |
'author' => 'publish_posts', |
| 70 |
'contributor' => 'edit_posts', |
| 71 |
'subscriber' => 'read', |
| 72 |
); |
| 73 |
|
| 74 |
return isset( $capability[ $min_user_role ] ) ? $capability[ $min_user_role ] : 'manage_options'; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check if current user can manage Capacity Rules. |
| 79 |
* |
| 80 |
* @return bool |
| 81 |
*/ |
| 82 |
function wpbc_resource_capacity__user_can_manage() { |
| 83 |
|
| 84 |
return wpbc_is_mu_user_can_be_here( 'only_super_admin' ) && current_user_can( wpbc_resource_capacity__get_manage_cap() ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Block direct page rendering when the user does not have Capacity Rules access. |
| 89 |
* |
| 90 |
* @param bool $is_show_this_page Whether page should be shown. |
| 91 |
* @param string $page_tag Page tag. |
| 92 |
* @param string $active_page_tab Active tab. |
| 93 |
* @param string $active_page_subtab Active subtab. |
| 94 |
* |
| 95 |
* @return bool |
| 96 |
*/ |
| 97 |
function wpbc_resource_capacity__check_showing_page( $is_show_this_page, $page_tag, $active_page_tab, $active_page_subtab ) { |
| 98 |
|
| 99 |
if ( ( 'wpbc-resources' === $page_tag ) && ( 'capacity' === $active_page_tab ) && ! wpbc_resource_capacity__user_can_manage() ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
return $is_show_this_page; |
| 104 |
} |
| 105 |
add_filter( 'wpbc_before_showing_settings_page_is_show_page', 'wpbc_resource_capacity__check_showing_page', 20, 4 ); |
| 106 |
|
| 107 |
/** |
| 108 |
* Enqueue CSS for Capacity Rules. |
| 109 |
* |
| 110 |
* @param string $where_to_load Where assets are loaded. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
function wpbc_resource_capacity_enqueue_css_files( $where_to_load ) { |
| 115 |
|
| 116 |
if ( ( ! is_admin() ) || ( ! in_array( $where_to_load, array( 'admin', 'both' ), true ) ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! wpbc_resource_capacity__is_page() ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
wp_enqueue_style( |
| 125 |
'wpbc-resource-capacity-page', |
| 126 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/page_resource_capacity.css', |
| 127 |
array( 'wpbc-all-admin' ), |
| 128 |
WP_BK_VERSION_NUM |
| 129 |
); |
| 130 |
} |
| 131 |
add_action( 'wpbc_enqueue_css_files', 'wpbc_resource_capacity_enqueue_css_files', 66 ); |
| 132 |
|
| 133 |
/** |
| 134 |
* Enqueue JS for Capacity Rules. |
| 135 |
* |
| 136 |
* @param string $where_to_load Where assets are loaded. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
function wpbc_resource_capacity_enqueue_js_files( $where_to_load ) { |
| 141 |
|
| 142 |
if ( ( ! is_admin() ) || ( ! in_array( $where_to_load, array( 'admin', 'both' ), true ) ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if ( ! wpbc_resource_capacity__is_page() ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
wp_enqueue_script( |
| 151 |
'wpbc-resource-capacity-page', |
| 152 |
trailingslashit( plugins_url( '', __FILE__ ) ) . '_out/page_resource_capacity.js', |
| 153 |
array( 'jquery', 'wpbc_all' ), |
| 154 |
WP_BK_VERSION_NUM, |
| 155 |
array( 'in_footer' => WPBC_JS_IN_FOOTER ) |
| 156 |
); |
| 157 |
|
| 158 |
wp_localize_script( |
| 159 |
'wpbc-resource-capacity-page', |
| 160 |
'wpbc_resource_capacity_page', |
| 161 |
array( |
| 162 |
'i18n' => array( |
| 163 |
'auto_cancel_warning' => __( 'Warning!!! After you approved the specific booking(s), all your pending bookings of the same booking resource as an approved booking for the dates, which are intersect with dates of approved booking, will be automatically canceled!', 'booking' ), |
| 164 |
'always_available_warning' => __( 'Warning! You allow unlimited number of bookings per same dates, its can be a reason of double bookings on the same date. Do you really want to do this?', 'booking' ), |
| 165 |
), |
| 166 |
) |
| 167 |
); |
| 168 |
} |
| 169 |
add_action( 'wpbc_enqueue_js_files', 'wpbc_resource_capacity_enqueue_js_files', 66 ); |
| 170 |
|
| 171 |
/** |
| 172 |
* Capacity Rules tab on the Resources page. |
| 173 |
*/ |
| 174 |
class WPBC_Page_Resource_Capacity extends WPBC_Page_Structure { |
| 175 |
|
| 176 |
/** |
| 177 |
* Settings API instance. |
| 178 |
* |
| 179 |
* @var WPBC_Settings_API_Capacity|false |
| 180 |
*/ |
| 181 |
private $settings_api = false; |
| 182 |
|
| 183 |
/** |
| 184 |
* Whether settings were saved during this request. |
| 185 |
* |
| 186 |
* @var bool |
| 187 |
*/ |
| 188 |
private $is_updated = false; |
| 189 |
|
| 190 |
/** |
| 191 |
* Menu slug. |
| 192 |
* |
| 193 |
* @return string |
| 194 |
*/ |
| 195 |
public function in_page() { |
| 196 |
|
| 197 |
if ( ! wpbc_resource_capacity__user_can_manage() ) { |
| 198 |
return (string) wp_rand( 100000, 1000000 ); |
| 199 |
} |
| 200 |
|
| 201 |
return 'wpbc-resources'; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Register tab. |
| 206 |
* |
| 207 |
* @return array |
| 208 |
*/ |
| 209 |
public function tabs() { |
| 210 |
|
| 211 |
return array( |
| 212 |
'capacity' => array( |
| 213 |
'is_show_top_path' => true, |
| 214 |
'left_navigation__default_view_mode' => '', // FixIn:11.7.1.1: 'compact', |
| 215 |
'title' => __( 'Capacity Rules', 'booking' ), |
| 216 |
'hint' => __( 'Manage booking capacity, pending-day availability, and related resource rules.', 'booking' ), |
| 217 |
'page_title' => __( 'Capacity Rules', 'booking' ), |
| 218 |
'link' => '', |
| 219 |
'position' => '', |
| 220 |
'css_classes' => 'wpbc_top_tab__resource_capacity', |
| 221 |
'icon' => '', |
| 222 |
'font_icon' => 'wpbc_icn_filter_none', |
| 223 |
'font_icon_right' => 'wpbc-bi-question-circle', |
| 224 |
'default' => false, |
| 225 |
'disabled' => false, |
| 226 |
'hided' => false, |
| 227 |
'subtabs' => array(), |
| 228 |
'folder_style' => 'order:115;', |
| 229 |
), |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get Settings API class. |
| 235 |
* |
| 236 |
* @return WPBC_Settings_API_Capacity |
| 237 |
*/ |
| 238 |
public function settings_api() { |
| 239 |
|
| 240 |
if ( false === $this->settings_api ) { |
| 241 |
$this->settings_api = new WPBC_Settings_API_Capacity(); |
| 242 |
} |
| 243 |
|
| 244 |
return $this->settings_api; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Save settings. |
| 249 |
* |
| 250 |
* @return void |
| 251 |
*/ |
| 252 |
public function maybe_update() { |
| 253 |
|
| 254 |
$form_name = 'wpbc_resource_capacity_form'; |
| 255 |
|
| 256 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 257 |
if ( ! isset( $_POST[ 'is_form_sbmitted_' . $form_name ] ) ) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
check_admin_referer( 'wpbc_settings_page_' . $form_name ); |
| 262 |
|
| 263 |
if ( ! wpbc_resource_capacity__user_can_manage() ) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
$validated_fields = $this->settings_api()->validate_post(); |
| 268 |
|
| 269 |
foreach ( $validated_fields as $field_name => $field_value ) { |
| 270 |
if ( false !== strpos( $field_name, '__promote_upgrade' ) ) { |
| 271 |
unset( $validated_fields[ $field_name ] ); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
$this->settings_api()->save_to_db( $validated_fields ); |
| 276 |
$this->is_updated = true; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Page content. |
| 281 |
* |
| 282 |
* @return false|void |
| 283 |
*/ |
| 284 |
public function content() { |
| 285 |
|
| 286 |
do_action( 'wpbc_hook_settings_page_header', 'resource_capacity' ); |
| 287 |
|
| 288 |
if ( ! wpbc_resource_capacity__user_can_manage() ) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
if ( function_exists( 'wpbc_js_for_bookings_page' ) ) { |
| 293 |
wpbc_js_for_bookings_page(); |
| 294 |
} |
| 295 |
|
| 296 |
if ( $this->is_updated ) { |
| 297 |
wpbc_show_changes_saved_message(); |
| 298 |
} |
| 299 |
|
| 300 |
$submit_form_name = 'wpbc_resource_capacity_form'; |
| 301 |
?> |
| 302 |
<div class="wpbc_resource_capacity_page wpdevelop"> |
| 303 |
<span class="metabox-holder"> |
| 304 |
<form name="<?php echo esc_attr( $submit_form_name ); ?>" id="<?php echo esc_attr( $submit_form_name ); ?>" action="" method="post" autocomplete="off"> |
| 305 |
<?php wp_nonce_field( 'wpbc_settings_page_' . $submit_form_name ); ?> |
| 306 |
<input type="hidden" name="is_form_sbmitted_<?php echo esc_attr( $submit_form_name ); ?>" id="is_form_sbmitted_<?php echo esc_attr( $submit_form_name ); ?>" value="1" /> |
| 307 |
|
| 308 |
<div class="wpbc_settings_row wpbc_settings_row_full_width"> |
| 309 |
<?php |
| 310 |
wpbc_open_meta_box_section( |
| 311 |
'wpbc_resource_capacity_rules', |
| 312 |
__( 'Capacity Rules', 'booking' ), |
| 313 |
array( |
| 314 |
'is_section_visible_after_load' => true, |
| 315 |
'is_show_minimize' => false, |
| 316 |
) |
| 317 |
); |
| 318 |
$this->settings_api()->show( 'capacity' ); |
| 319 |
wpbc_close_meta_box_section(); |
| 320 |
|
| 321 |
if ( ! class_exists( 'wpdev_bk_personal' ) ) { |
| 322 |
$wpbc_metabox_id = 'wpbc_resource_capacity_upgrade'; |
| 323 |
|
| 324 |
wpbc_open_meta_box_section( |
| 325 |
$wpbc_metabox_id, |
| 326 |
__( 'Booking Quantity Control - Set limits for the number of bookings per day or time slot.', 'booking' ), |
| 327 |
array( |
| 328 |
'is_section_visible_after_load' => true, |
| 329 |
'is_show_minimize' => false, |
| 330 |
) |
| 331 |
); |
| 332 |
$this->settings_api()->show( 'capacity_upgrade' ); |
| 333 |
wpbc_close_meta_box_section(); |
| 334 |
} |
| 335 |
?> |
| 336 |
</div> |
| 337 |
|
| 338 |
<input type="submit" value="<?php esc_attr_e( 'Save Changes', 'booking' ); ?>" class="button button-primary wpbc_submit_button" /> |
| 339 |
</form> |
| 340 |
</span> |
| 341 |
</div> |
| 342 |
<?php |
| 343 |
|
| 344 |
do_action( 'wpbc_hook_settings_page_footer', 'resource_capacity' ); |
| 345 |
} |
| 346 |
} |
| 347 |
add_action( 'wpbc_menu_created', array( new WPBC_Page_Resource_Capacity(), '__construct' ) ); |
| 348 |
|