restrict-dates-add-on-for-gravity-forms
Last commit date
admin
1 month ago
assets
1 month ago
vendor
1 month ago
class-gfrestrictdates.php
1 month ago
readme.txt
1 month ago
restrict-dates.php
1 month ago
class-gfrestrictdates.php
270 lines
| 1 | <?php |
| 2 | if (! defined('ABSPATH')) exit; |
| 3 | |
| 4 | GFForms::include_addon_framework(); |
| 5 | |
| 6 | class GFRestrictDatesAddOn extends GFAddOn { |
| 7 | |
| 8 | protected $_version = GF_RESTRICT_DATES_ADDON_VERSION; |
| 9 | protected $_min_gravityforms_version = '1.9'; |
| 10 | protected $_slug = 'gf-restrict-dates'; |
| 11 | protected $_path = 'gravityforms-restrict-dates/restrict-dates.php'; |
| 12 | protected $_full_path = __FILE__; |
| 13 | protected $_title = 'Gravity Forms Restrict Dates Add-On'; |
| 14 | protected $_short_title = 'GF Restrict Dates Add-On'; |
| 15 | |
| 16 | private static $_instance = null; |
| 17 | |
| 18 | /** |
| 19 | * Get an instance of this class. |
| 20 | * |
| 21 | * @return GFRestrictDatesAddOn |
| 22 | */ |
| 23 | public static function get_instance() { |
| 24 | if (self::$_instance == null) { |
| 25 | self::$_instance = new GFRestrictDatesAddOn(); |
| 26 | } |
| 27 | |
| 28 | return self::$_instance; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Handles hooks and loading of language files. |
| 33 | */ |
| 34 | public function init() { |
| 35 | parent::init(); |
| 36 | |
| 37 | add_filter('gform_tooltips', array($this, 'gfrda_add_tooltips')); |
| 38 | add_action('gform_editor_js', array($this, 'gfrda_editor_script')); |
| 39 | add_action('gform_enqueue_scripts', array($this, 'gfrda_frontend_enqueue_scripts'), 10, 2); |
| 40 | if (GFIC_GF_MIN_2_5) { |
| 41 | add_filter('gform_field_settings_tabs', array($this, 'gcafe_fields_settings_tab'), 10, 2); |
| 42 | add_action('gform_field_settings_tab_content_restrict_date_tab', array($this, 'gcafe_fields_settings_tab_content'), 10, 2); |
| 43 | } else { |
| 44 | add_action('gform_field_advanced_settings', array($this, 'gfrda_advanced_settings'), 10, 2); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // # SCRIPTS & STYLES ----------------------------------------------------------------------------------------------- |
| 50 | |
| 51 | /** |
| 52 | * Return the scripts which should be enqueued. |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | public function scripts() { |
| 57 | $scripts = array( |
| 58 | array( |
| 59 | 'handle' => 'gfrd_admin_script', |
| 60 | 'src' => $this->get_base_url() . '/assets/js/gfrd_admin_script.js', |
| 61 | 'version' => $this->_version, |
| 62 | 'deps' => array('jquery'), |
| 63 | 'enqueue' => array( |
| 64 | array('admin_page' => array('form_editor', 'plugin_settings', 'form_settings')), |
| 65 | ) |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | return array_merge(parent::scripts(), $scripts); |
| 70 | } |
| 71 | |
| 72 | public function gcafe_fields_settings_tab($tabs, $form) { |
| 73 | $tabs[] = array( |
| 74 | // Define the unique ID for your tab. |
| 75 | 'id' => 'restrict_date_tab', |
| 76 | // Define the title to be displayed on the toggle button your tab. |
| 77 | 'title' => 'Restrict Date', |
| 78 | // Define an array of classes to be added to the toggle button for your tab. |
| 79 | 'toggle_classes' => array('gcafe_toggle_1', 'gcafe_toggle_2'), |
| 80 | // Define an array of classes to be added to the body of your tab. |
| 81 | 'body_classes' => array('gcafe_toggle_class'), |
| 82 | ); |
| 83 | |
| 84 | return $tabs; |
| 85 | } |
| 86 | |
| 87 | public function gcafe_fields_settings_tab_content($form) { |
| 88 | ?> |
| 89 | |
| 90 | <li class="restrict_date_setting field_setting"> |
| 91 | <?php if (!GFIC_GF_MIN_2_5): ?> |
| 92 | <h3>Restrict Dates</h3> |
| 93 | <?php endif; ?> |
| 94 | <ul> |
| 95 | <li> |
| 96 | <input type="checkbox" id="gfrda_enable_restrict_value" onclick="SetFieldProperty('restrictDateGField', this.checked);" /> |
| 97 | <label for="gfrda_enable_restrict_value" class="inline"> |
| 98 | <?php esc_html_e("Enable Restrict date options", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 99 | <?php gform_tooltip("enable_rd"); ?> |
| 100 | </label> |
| 101 | </li> |
| 102 | </ul> |
| 103 | <ul id="rda_enable"> |
| 104 | <li class="rda_minimum_date field_setting"> |
| 105 | <label for="field_admin_label" class="section_label"> |
| 106 | <?php esc_html_e("Minimum Date", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 107 | <?php gform_tooltip("rda_min_date"); ?> |
| 108 | </label> |
| 109 | <select name="mini_date" id="mini_date_value" onChange="SetFieldProperty('rdaMinimumDateGField', this.value);"> |
| 110 | <option value="">Select One</option> |
| 111 | <option value="today">Current Date</option> |
| 112 | <option value="custom_date">Set Specific Date</option> |
| 113 | </select> |
| 114 | </li> |
| 115 | <li class="rda_minimum_date_picker field_setting"> |
| 116 | <label for="field_minimum_date_picker" class="section_label"> |
| 117 | <?php esc_html_e("Choose Date", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 118 | <?php gform_tooltip("rda_min_date"); ?> |
| 119 | </label> |
| 120 | <input type="date" id="field_minimum_date_picker" onChange="SetFieldProperty('rdaMinDatePickGField', this.value);"> |
| 121 | </li> |
| 122 | <li class="rda_maximum_date field_setting"> |
| 123 | <label for="field_admin_label" class="section_label"> |
| 124 | <?php esc_html_e("Maximum Date", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 125 | <?php gform_tooltip("rda_min_date"); ?> |
| 126 | </label> |
| 127 | <select name="maxi_date" id="max_date_value" onChange="SetFieldProperty('rdaMaximumDateGField', this.value);"> |
| 128 | <option value="">Select One</option> |
| 129 | <option value="today">Current Date</option> |
| 130 | <option value="custom_date">Set Specific Date</option> |
| 131 | </select> |
| 132 | </li> |
| 133 | <li class="rda_maximum_date_picker field_setting"> |
| 134 | <label for="field_maximum_date_picker" class="section_label"> |
| 135 | <?php esc_html_e("Choose Date", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 136 | <?php gform_tooltip("rda_max_date"); ?> |
| 137 | </label> |
| 138 | <input type="date" id="field_maximum_date_picker" onChange="SetFieldProperty('rdaMaxDatePickGField', this.value);"> |
| 139 | </li> |
| 140 | <li class="rda_weekly_off field_setting"> |
| 141 | <label for="field_admin_label" class="section_label"> |
| 142 | <?php esc_html_e("Disable Week/Off Day", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 143 | <?php gform_tooltip("rda_weekly_date"); ?> |
| 144 | </label> |
| 145 | <select name="weekly_date" id="weekly_date_value" onChange="SetFieldProperty('rdaWeeklyDateGField', this.value);"> |
| 146 | <option value="">Choose Day</option> |
| 147 | <option value="0">Sunday</option> |
| 148 | <option value="1">Monday</option> |
| 149 | <option value="2">Tuesday</option> |
| 150 | <option value="3">Wednesday</option> |
| 151 | <option value="4">Thursday</option> |
| 152 | <option value="5">Friday</option> |
| 153 | <option value="6">Saturday</option> |
| 154 | </select> |
| 155 | </li> |
| 156 | <li class="rda_start_day field_setting"> |
| 157 | <label for="field_admin_label" class="section_label"> |
| 158 | <?php esc_html_e("Week Start Day", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 159 | <?php gform_tooltip("rda_week_start_day"); ?> |
| 160 | </label> |
| 161 | <select name="week_start_day" id="week_start_day" onChange="SetFieldProperty('rdaWeekStartDayGField', this.value);"> |
| 162 | <option value="">Choose Day</option> |
| 163 | <option value="0">Sunday</option> |
| 164 | <option value="1">Monday</option> |
| 165 | <option value="2">Tuesday</option> |
| 166 | <option value="3">Wednesday</option> |
| 167 | <option value="4">Thursday</option> |
| 168 | <option value="5">Friday</option> |
| 169 | <option value="6">Saturday</option> |
| 170 | </select> |
| 171 | </li> |
| 172 | <li class="rda_disable_specific_dates field_setting"> |
| 173 | <label for="rda_disable_specific_dates" class="section_label"> |
| 174 | <?php esc_html_e("Type Specific Dates", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 175 | <?php gform_tooltip("rda_dis_sp_dates"); ?> |
| 176 | </label> |
| 177 | <input type="text" id="rda_disable_specific_dates" placeholder="06/25/2020,06/29/2020" onChange="SetFieldProperty('rdaDisableSDatesGField', this.value);"> |
| 178 | </li> |
| 179 | <li class="readonly_date_setting field_setting"> |
| 180 | <input type="checkbox" id="gfrda_enable_readonly" onclick="SetFieldProperty('readOnlyDateGField', this.checked);" /> |
| 181 | <label for="gfrda_enable_readonly" class="inline"> |
| 182 | <?php esc_html_e("Enable readonly", "restrict-dates-add-on-for-gravity-forms"); ?> |
| 183 | <?php gform_tooltip("gfrda_readonly"); ?> |
| 184 | </label> |
| 185 | </li> |
| 186 | </ul> |
| 187 | </li> |
| 188 | |
| 189 | <?php |
| 190 | } |
| 191 | |
| 192 | public function gfrda_advanced_settings($position, $form_id) { |
| 193 | if ($position == 550) { |
| 194 | $this->gcafe_fields_settings_tab_content(GFAPI::get_form($form_id)); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | function gfrda_editor_script() { |
| 200 | ?> |
| 201 | |
| 202 | <script type='text/javascript'> |
| 203 | //adding setting to fields of type "date" |
| 204 | |
| 205 | fieldSettings.date += ", .restrict_date_setting"; |
| 206 | fieldSettings.date += ", .rda_minimum_date"; |
| 207 | fieldSettings.date += ", .rda_minimum_date_picker"; |
| 208 | fieldSettings.date += ", .rda_maximum_date"; |
| 209 | fieldSettings.date += ", .rda_maximum_date_picker"; |
| 210 | fieldSettings.date += ", .readonly_date_setting"; |
| 211 | fieldSettings.date += ", .rda_disable_specific_dates"; |
| 212 | fieldSettings.date += ", .rda_weekly_off"; |
| 213 | fieldSettings.date += ", .rda_start_day"; |
| 214 | |
| 215 | //binding to the load field settings event to initialize the checkbox |
| 216 | |
| 217 | jQuery(document).bind("gform_load_field_settings", function(event, field, form) { |
| 218 | jQuery("#gfrda_enable_restrict_value").prop('checked', Boolean(rgar(field, 'restrictDateGField'))); |
| 219 | jQuery("#gfrda_enable_readonly").prop('checked', Boolean(rgar(field, 'readOnlyDateGField'))); |
| 220 | jQuery("#mini_date_value").val(field["rdaMinimumDateGField"]); |
| 221 | jQuery("#max_date_value").val(field["rdaMaximumDateGField"]); |
| 222 | jQuery("#field_minimum_date_picker").val(field["rdaMinDatePickGField"]); |
| 223 | jQuery("#field_maximum_date_picker").val(field["rdaMaxDatePickGField"]); |
| 224 | jQuery("#rda_disable_specific_dates").val(field["rdaDisableSDatesGField"]); |
| 225 | jQuery("#weekly_date_value").val(field["rdaWeeklyDateGField"]); |
| 226 | jQuery("#week_start_day").val(field["rdaWeekStartDayGField"]); |
| 227 | }); |
| 228 | </script> |
| 229 | |
| 230 | <?php |
| 231 | } |
| 232 | |
| 233 | |
| 234 | function gfrda_frontend_enqueue_scripts($form, $is_ajax) { |
| 235 | $form_id = $form['id']; |
| 236 | $fields_data = []; |
| 237 | |
| 238 | foreach ($form['fields'] as $field) { |
| 239 | if (property_exists($field, 'restrictDateGField') && $field->restrictDateGField) { |
| 240 | $form = (array) GFFormsModel::get_form_meta($field->formId); |
| 241 | $fields_data[] = json_encode(GFFormsModel::get_field($form, $field->id)); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if (count($fields_data) === 0) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | wp_enqueue_script('gfrd_data', $this->get_base_url() . '/assets/js/gf_rd_data.js', array('jquery'), $this->_version, true); |
| 250 | wp_localize_script( |
| 251 | 'gfrd_data', |
| 252 | 'gfrdMainJsVars_' . $form_id, |
| 253 | array( |
| 254 | 'elements' => $fields_data |
| 255 | ) |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | function gfrda_add_tooltips() { |
| 260 | $tooltips['enable_rd'] = "<h6>" . esc_html__("Enable restrict options", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Check this box to show date restrict options.", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 261 | $tooltips['gfrda_readonly'] = "<h6>" . esc_html__("Enable Readonly", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Check this box to disable manual date input change.", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 262 | $tooltips['rda_weekly_date'] = "<h6>" . esc_html__("Week/Off day", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Choose your day to disable in weekly.", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 263 | $tooltips['rda_max_date'] = "<h6>" . esc_html__("Maximum Date", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Choose maximum date for restricting in date-picker", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 264 | $tooltips['rda_min_date'] = "<h6>" . esc_html__("Minimum Date", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Choose minimum date for restricting in date-picker", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 265 | $tooltips['rda_dis_sp_dates'] = "<h6>" . esc_html__("Disable Specific Dates", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Type your dates to disable with comma spearate. Ex: mm/dd/yyyy", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 266 | $tooltips['rda_week_start_day'] = "<h6>" . esc_html__("Week Start Day", "restrict-dates-add-on-for-gravity-forms") . "</h6>" . esc_html__("Choose week start day for your calendar.", "restrict-dates-add-on-for-gravity-forms") . ""; |
| 267 | return $tooltips; |
| 268 | } |
| 269 | } |
| 270 |