AdminPage.Menu.class.php
4 years ago
AdminPage.Submenu.class.php
4 years ago
AdminPage.Themes.class.php
4 years ago
AdminPage.class.php
4 years ago
AdminPageSection.class.php
4 years ago
AdminPageSetting.Address.class.php
4 years ago
AdminPageSetting.Checkbox.class.php
4 years ago
AdminPageSetting.ColorPicker.class.php
4 years ago
AdminPageSetting.Count.class.php
4 years ago
AdminPageSetting.Editor.class.php
4 years ago
AdminPageSetting.FileUpload.class.php
4 years ago
AdminPageSetting.HTML.class.php
4 years ago
AdminPageSetting.Image.class.php
4 years ago
AdminPageSetting.InfiniteTable.class.php
4 years ago
AdminPageSetting.McApiKey.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
4 years ago
AdminPageSetting.Number.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
4 years ago
AdminPageSetting.Ordering.class.php
4 years ago
AdminPageSetting.Radio.class.php
4 years ago
AdminPageSetting.Scheduler.class.php
4 years ago
AdminPageSetting.Select.class.php
4 years ago
AdminPageSetting.SelectMenu.class.php
4 years ago
AdminPageSetting.SelectPost.class.php
4 years ago
AdminPageSetting.SelectTaxonomy.class.php
4 years ago
AdminPageSetting.Text.class.php
4 years ago
AdminPageSetting.Textarea.class.php
4 years ago
AdminPageSetting.Time.class.php
4 years ago
AdminPageSetting.Toggle.class.php
4 years ago
AdminPageSetting.WarningTip.class.php
4 years ago
AdminPageSetting.class.php
4 years ago
Library.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register, display and save a series of fields to specify the opening hours |
| 5 | * of a business/company. |
| 6 | * |
| 7 | * This setting accepts the following arguments in its constructor function. |
| 8 | * |
| 9 | * $args = array( |
| 10 | * 'id' => 'setting_id', // Unique id |
| 11 | * 'title' => 'My Setting', // Title or label for the setting |
| 12 | * 'description' => 'Description', // Help text description |
| 13 | * 'weekday_names' => array( // Optional array of custom |
| 14 | * 'monday' => 'Monday', // weekday names. These can be |
| 15 | * 'tuesday' => 'Tuesday', // passed in any order to |
| 16 | * 'wednesday' => 'Wednesday', // set a new start of the week. |
| 17 | * 'thursday' => 'Thursday', |
| 18 | * 'friday' => 'Friday', |
| 19 | * 'saturday' => 'Saturday', |
| 20 | * 'sunday' => 'Sunday' |
| 21 | * ); |
| 22 | * ); |
| 23 | * |
| 24 | * @since 1.0 |
| 25 | * @package Simple Admin Pages |
| 26 | */ |
| 27 | |
| 28 | class sapAdminPageSettingOpeningHours_2_6_1 extends sapAdminPageSetting_2_6_1 { |
| 29 | |
| 30 | public $sanitize_callback = 'sanitize_text_field'; |
| 31 | |
| 32 | /** |
| 33 | * Scripts that must be loaded for this component |
| 34 | * @since 2.0.a.4 |
| 35 | */ |
| 36 | public $scripts = array( |
| 37 | 'sap-opening-hours' => array( |
| 38 | 'path' => 'js/opening-hours.js', |
| 39 | 'dependencies' => array( 'jquery' ), |
| 40 | 'version' => SAP_VERSION, |
| 41 | 'footer' => true, |
| 42 | ), |
| 43 | ); |
| 44 | |
| 45 | // Array of days of the week |
| 46 | public $weekdays = array( |
| 47 | 'monday' => 'Monday', |
| 48 | 'tuesday' => 'Tuesday', |
| 49 | 'wednesday' => 'Wednesday', |
| 50 | 'thursday' => 'Thursday', |
| 51 | 'friday' => 'Friday', |
| 52 | 'saturday' => 'Saturday', |
| 53 | 'sunday' => 'Sunday' |
| 54 | ); |
| 55 | |
| 56 | /** |
| 57 | * Parse the arguments passed in the construction and assign them to |
| 58 | * internal variables. |
| 59 | * @since 1.0 |
| 60 | */ |
| 61 | private function parse_args( $args ) { |
| 62 | foreach ( $args as $key => $val ) { |
| 63 | switch ( $key ) { |
| 64 | |
| 65 | case 'id' : |
| 66 | $this->{$key} = esc_attr( $val ); |
| 67 | |
| 68 | case 'weekdays' : |
| 69 | |
| 70 | $this->weekdays = $val; |
| 71 | |
| 72 | default : |
| 73 | $this->{$key} = $val; |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Escape the value to display it in text fields and other input fields |
| 81 | * |
| 82 | * @since 1.0 |
| 83 | */ |
| 84 | public function esc_value( $val ) { |
| 85 | |
| 86 | $value = array(); |
| 87 | |
| 88 | // Loop over the values and sanitize them |
| 89 | for ( $i = 0; $i < 7; $i++ ) { |
| 90 | $value[$i]['day'] = isset( $val[$i] ) && isset( $val[$i]['day'] ) ? esc_attr( $val[$i]['day'] ) : ''; |
| 91 | $value[$i]['hours'] = isset( $val[$i] ) && isset( $val[$i]['hours'] ) ? esc_attr( $val[$i]['hours'] ) : ''; |
| 92 | } |
| 93 | |
| 94 | return $value; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get a day's display name |
| 99 | * @since 1.0 |
| 100 | */ |
| 101 | private function get_day_name( $day ) { |
| 102 | foreach ( $this->weekdays as $id => $name ) { |
| 103 | if ( $day == $id ) { |
| 104 | return $name; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return ''; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Display this setting |
| 113 | * @since 1.0 |
| 114 | * @todo integrate time picker |
| 115 | */ |
| 116 | public function display_setting() { |
| 117 | |
| 118 | $this->display_description(); |
| 119 | |
| 120 | for ($i = 0; $i < 7; $i++) { |
| 121 | |
| 122 | ?> |
| 123 | |
| 124 | <fieldset <?php $this->print_conditional_data(); ?>> |
| 125 | |
| 126 | <table class="sap-opening-hours <?php echo ( $this->disabled ? 'disabled' : ''); ?>"> |
| 127 | <tr> |
| 128 | <td> |
| 129 | <input type="hidden" id="sap-opening-hours-day-<?php echo $i; ?>-name" name="<?php echo $this->get_input_name(); ?>[<?php echo $i; ?>][day_name]" value="<?php echo esc_attr( $this->get_day_name( $this->value[$i]['day'] ) ); ?>"> |
| 130 | <select name="<?php echo $this->get_input_name(); ?>[<?php echo $i; ?>][day]" id="<?php echo $this->id . '-' . $i; ?>-day" class="sap-opening-hours-day" data-target="#sap-opening-hours-day-<?php echo $i; ?>-name"> |
| 131 | <option value=""></option> |
| 132 | |
| 133 | <?php foreach ( $this->weekdays as $id => $name ) : ?> |
| 134 | |
| 135 | <option value="<?php echo $id; ?>" data-name="<?php echo esc_attr( $name ); ?>"<?php if ( $this->value[$i]['day'] == $id ) : ?> selected<?php endif; ?>> |
| 136 | <?php echo $name; ?> |
| 137 | </option> |
| 138 | |
| 139 | <?php endforeach; ?> |
| 140 | |
| 141 | </select> |
| 142 | </td> |
| 143 | <td> |
| 144 | <input name="<?php echo $this->get_input_name(); ?>[<?php echo $i; ?>][hours]" type="text" id="<?php echo $this->id . '-' . $i; ?>-hours" value="<?php echo $this->value[$i]['hours']; ?>" class="regular-text sap-opening-hours-hours" /> |
| 145 | </td> |
| 146 | </tr> |
| 147 | </table> |
| 148 | |
| 149 | <?php $this->display_disabled(); ?> |
| 150 | |
| 151 | </fieldset> |
| 152 | |
| 153 | <?php |
| 154 | |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sanitize the array of text inputs for this setting |
| 161 | * @since 1.0 |
| 162 | */ |
| 163 | public function sanitize_callback_wrapper( $values ) { |
| 164 | |
| 165 | // If no sanitization callback exists, don't register the setting. |
| 166 | if ( !isset( $this->sanitize_callback ) || !trim( $this->sanitize_callback ) ) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // If this isn't an array, just sanitize it as a string |
| 171 | if (!is_array( $values ) ) { |
| 172 | return call_user_func( $this->sanitize_callback, $values ); |
| 173 | } |
| 174 | |
| 175 | // Loop over the values and sanitize them |
| 176 | for ( $i = 0; $i < 7; $i++ ) { |
| 177 | if ( isset( $values[ $i ] ) && is_array( $values[ $i ] ) ) { |
| 178 | $values[$i]['day'] = call_user_func( $this->sanitize_callback, $values[$i]['day'] ); |
| 179 | $values[$i]['hours'] = call_user_func( $this->sanitize_callback, $values[$i]['hours'] ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return $values; |
| 184 | } |
| 185 | |
| 186 | } |
| 187 |