GFBusinessHours

The Gravity Forms Business Hours by GravityView GFBusinessHours class.

Defined (1)

The class is defined in the following location(s).

/gravity-forms-business-hours.php  
  1. class GFBusinessHours extends GFAddOn { 
  2.  
  3. protected $_version = "1.2.1"; 
  4. protected $_min_gravityforms_version = "1.7.9999"; 
  5. protected $_slug = 'gravity-forms-business-hours'; 
  6. protected $_path = 'gravity-forms-business-hours/gravity-forms-business-hours.php'; 
  7. protected $_full_path = __FILE__; 
  8. protected $_title = 'Gravity Forms Business Hours by GravityView'; 
  9. protected $_short_title = 'Business Hours'; 
  10.  
  11. /** 
  12. * Register functions to be called on the frontend 
  13. * @return void 
  14. */ 
  15. public function init_frontend() { 
  16. parent::init_frontend(); 
  17.  
  18. add_action('gform_entry_field_value', array($this, 'display_entry_field_value'), 10, 4); 
  19.  
  20. add_filter('gform_save_field_value', array($this, 'save_field_value'), 10, 4); 
  21.  
  22. add_filter('gform_field_validation', array( $this, 'validate'), 10, 4 ); 
  23.  
  24. add_filter('gform_field_input', array( $this, 'business_hours_field'), 10, 5 ); 
  25.  
  26. /** 
  27. * Validate the field value. 
  28. * @param array $status Status array with `is_valid` and `message` keys 
  29. * @param mixed $value Field value 
  30. * @param array $form Gravity Forms form array 
  31. * @param array $field Gravity Forms field array 
  32. * @return array Status array 
  33. */ 
  34. function validate( $status, $value, $form, $field ) { 
  35.  
  36. if( $field['type'] !== 'business_hours' ) { 
  37. return $status; 
  38.  
  39. $return = $status; 
  40.  
  41. $json_value = json_decode( $value ); 
  42.  
  43. if( !empty( $field['isRequired'] ) && ( empty( $value ) || $value === 'null' || is_null( $json_value ) ) ) { 
  44.  
  45. $return = array( 
  46. 'is_valid' => false,  
  47. 'message' => __('This field is required.', 'gravity-forms-business-hours'),  
  48. ); 
  49.  
  50.  
  51. return $return; 
  52.  
  53. /** 
  54. * Register functions to be called when DOING_AJAX 
  55. * @return void 
  56. */ 
  57. public function init_ajax() { 
  58. parent::init_ajax(); 
  59.  
  60. add_filter('gform_field_content', array($this, 'business_hours_field_admin'), 10, 5); 
  61.  
  62. /** 
  63. * Register functions to be called in the admin 
  64. * @return void 
  65. */ 
  66. public function init_admin() { 
  67. parent::init_admin(); 
  68.  
  69. add_action('gform_entries_field_value', array($this, 'business_hours_entries'), 10, 3); 
  70.  
  71. add_action('gform_entry_field_value', array($this, 'display_entry_field_value'), 10, 4); 
  72. add_filter('gform_add_field_buttons', array($this, 'add_field_button')); 
  73.  
  74. add_action('gform_editor_js', array($this, 'editor_script')); 
  75.  
  76. add_action('gform_editor_js_set_default_values', array($this, 'set_defaults')); 
  77.  
  78. add_filter('gform_field_content', array($this, 'business_hours_field_admin'), 10, 5); 
  79.  
  80. add_filter('gform_field_type_title', array($this, 'field_type_title'), 10 ); 
  81.  
  82. /** 
  83. * Modify the name of the field type in the Form Editor 
  84. * @param string $type Field type string 
  85. * @return string Field type string 
  86. */ 
  87. public function field_type_title( $type = '' ) { 
  88.  
  89. if( $type === 'business_hours' ) { 
  90. return __('Business Hours', 'gravity-forms-business-hours'); 
  91.  
  92. return $type; 
  93.  
  94.  
  95. /** 
  96. * If on Edit Entry screen, show default editing fields. In Edit Form, show placeholder content. 
  97. * @param [type] $content [description] 
  98. * @param [type] $field [description] 
  99. * @param [type] $value [description] 
  100. * @param [type] $lead_id [description] 
  101. * @param [type] $form_id [description] 
  102. * @return [type] [description] 
  103. */ 
  104. public function business_hours_field_admin($content, $field, $value, $lead_id, $form_id) { 
  105.  
  106. if( $field['type'] !== 'business_hours' ) { 
  107. return $content; 
  108.  
  109. $alternative_content = ''; 
  110.  
  111. $edit_form_page = ( rgget('page') === 'gf_edit_forms' && !empty( $_GET['id'] ) ); 
  112.  
  113. $add_field_ajax = ( defined('DOING_AJAX') && DOING_AJAX ) && (rgpost('action') === 'rg_add_field'); 
  114.  
  115. // If on Edit Entry screen, show default editing fields 
  116. if ( rgget('page') === 'gf_entries' && rgget('view') === 'entry' && rgpost('screen_mode') === 'edit' ) { 
  117.  
  118. $alternative_content = $this->business_hours_field('', $field, $value, $lead_id, $form_id ); 
  119.  
  120. // A field is already saved, or the field is being added 
  121. else if( $edit_form_page || $add_field_ajax ) { 
  122.  
  123. $alternative_content = "<div class='gf-html-container gf-business-hours-container'><span class='gf_blockheader'><i class='dashicons dashicons-clock'></i> " . __('Business Hours', 'gravity-forms-business-hours') . '</span><span>' . __('This is a content placeholder. Preview this form to view the Business Hours field.', 'gravity-forms-business-hours') . "</span></div>"; 
  124.  
  125. return str_replace('</label>', '</label>' . $alternative_content, $content); 
  126.  
  127. /** 
  128. * Get an array of days 
  129. * @filter gravityforms_business_hours_days Modify the days array 
  130. * @return array Array of days of the week (displayed using PHP "D" formatting) 
  131. */ 
  132. private static function get_days() { 
  133.  
  134. /** 
  135. * Modify the date format for how the days appear, in PHP Date formatting 
  136. * @param string $date PHP Date format 
  137. */ 
  138. $day_format = apply_filters( 'gravityforms_business_hours_day_format', 'D' ); 
  139.  
  140. $days = array( 
  141. 'Monday' => date_i18n($day_format, strtotime('Monday this week')),  
  142. 'Tuesday' => date_i18n($day_format, strtotime('Tuesday this week')),  
  143. 'Wednesday' => date_i18n($day_format, strtotime('Wednesday this week')),  
  144. 'Thursday' => date_i18n($day_format, strtotime('Thursday this week')),  
  145. 'Friday' => date_i18n($day_format, strtotime('Friday this week')),  
  146. 'Saturday' => date_i18n($day_format, strtotime('Saturday this week')),  
  147. 'Sunday' => date_i18n($day_format, strtotime('Sunday this week')),  
  148. ); 
  149.  
  150. /** 
  151. * Modify the day values. Don't change the keys! 
  152. * @var array 
  153. */ 
  154. $days = apply_filters( 'gravityforms_business_hours_days', $days ); 
  155.  
  156. return $days; 
  157.  
  158. /** 
  159. * Set the field value on the Entries page 
  160. * @param [type] $value [description] 
  161. * @param [type] $form_id [description] 
  162. * @param [type] $field_id [description] 
  163. * @return [type] [description] 
  164. */ 
  165. public function business_hours_entries($value, $form_id, $field_id) { 
  166.  
  167. $form = GFAPI::get_form($form_id); 
  168. $field = RGFormsModel::get_field($form, $field_id); 
  169.  
  170. return self::display_entry_field_value( $value, $field, array(), $form ); 
  171.  
  172. /** 
  173. * Display the populated field value - not editable 
  174. * @param mixed $value Field value 
  175. * @param array $field Gravity Forms field array 
  176. * @param array $lead Gravity Forms entry array 
  177. * @param array $form Gravity Forms form array 
  178. * @return string HTML output of the field 
  179. */ 
  180. public static function display_entry_field_value($value, $field, $lead = array(), $form = array() ) { 
  181.  
  182. $return = $value; 
  183.  
  184. if ($field['type'] === 'business_hours') { 
  185.  
  186. $content = ''; 
  187.  
  188. $days = self::get_days(); 
  189.  
  190. $filled_days = array(); 
  191.  
  192. $list = self::get_list_from_value( $value ); 
  193.  
  194. if (!empty($list) && is_array($list)) { 
  195.  
  196. /** 
  197. * @link http://schema.org/LocalBusiness 
  198. */ 
  199. $content = '<div class="business_hours_list_item" itemscope itemtype="http://schema.org/LocalBusiness">'; 
  200.  
  201. foreach ($list as $time_span) { 
  202.  
  203. // Mark this day as open, so closed days can be processed below. 
  204. $filled_days[] = $time_span['day']; 
  205.  
  206. /** 
  207. * Generate schema.org markup 
  208. * @link http://schema.org/openingHours 
  209. */ 
  210. $datetime = sprintf( '%s %s-%s', substr($time_span['day'], 0, 2), $time_span['fromtime'], str_replace('+', '', $time_span['totime'] ) ); 
  211.  
  212. $output_template = ' 
  213. <div class="business-hours business-hours-open"> 
  214. <time itemprop="openingHoursSpecification" itemscope itemtype="http://schema.org/OpeningHoursSpecification" datetime="{{datetime}}"> 
  215. <strong itemprop="dayOfWeek" itemscope itemtype="http://schema.org/DayOfWeek" rel="{{daylabel}}"><span itemprop="name" content="{{day}}">{{daylabel}}</span></strong> 
  216. <span itemprop="opens" content="{{fromtime}}">{{fromtimelabel}}</span> - <span itemprop="closes" content="{{totime}}">{{totimelabel}}</span> 
  217. {{open_label}} 
  218. </time> 
  219. </div>'; 
  220.  
  221. $replacements = array( 
  222. '{{datetime}}' => $datetime,  
  223. '{{day}}' => $time_span['day'],  
  224. '{{daylabel}}' => (isset( $days[$time_span['day']] ) ? $days[$time_span['day']] : $time_span['day'] ),  
  225. '{{fromtime}}' => $time_span['fromtime'],  
  226. '{{fromtimelabel}}' => ( isset( $time_span['fromtimelabel'] ) ? $time_span['fromtimelabel'] : $time_span['fromtime'] ),  
  227. '{{totime}}' => $time_span['totime'],  
  228. '{{totimelabel}}' => ( isset( $time_span['totimelabel'] ) ? $time_span['totimelabel'] : $time_span['totime'] ),  
  229. '{{open_label}}' => self::open_label( $time_span ),  
  230. ); 
  231.  
  232. /** 
  233. * Modify the output of the open days. Data inside {{brackets}} will be replaced with the appropriate values. 
  234. * @param string $output_template HTML code 
  235. * @param array $time_span description 
  236. * @param array $replacements Default values to replace with 
  237. */ 
  238. $output_template = apply_filters( 'gravityforms_business_hours_output_template', $output_template, $time_span, $replacements ); 
  239.  
  240. // Replace the keys ({{placeholders}}) with the data values 
  241. $item_output = str_replace( array_keys( $replacements ), array_values( $replacements), $output_template ); 
  242.  
  243. // Add to output 
  244. $content .= $item_output; 
  245.  
  246.  
  247. // Array of days that are set 
  248. $filled_days = array_unique( $filled_days ); 
  249.  
  250. // Find what days aren't entered 
  251. $empty_days = array_diff( array_keys($days), $filled_days ); 
  252.  
  253. if( !empty( $empty_days ) ) { 
  254.  
  255. // And set them as closed 
  256. foreach( $empty_days as $day ) { 
  257.  
  258. $output_template = ' 
  259. <div class="business-hours business-hours-closed"> 
  260. <strong>{{day}}</strong> <span>{{closed_label}}</span> 
  261. </div>'; 
  262.  
  263. $replacements = array( 
  264. '{{day}}' => $days[ $day ], // Custom value at key of full day name 
  265. '{{closed_label}}' => __('Closed', 'gravity-forms-business-hours'),  
  266. ); 
  267.  
  268. /** 
  269. * Modify the output of the open days. Data inside {{brackets}} will be replaced with the appropriate values. 
  270. * @param string $output_template HTML code 
  271. * @param string $day Day of the week value 
  272. * @param array $replacements Default values to replace with 
  273. */ 
  274. $output_template = apply_filters( 'gravityforms_business_hours_output_closed_template', $output_template, $days[ $day ], $replacements ); 
  275.  
  276. // Replace the keys ({{placeholders}}) with the data values 
  277. $item_output = str_replace( array_keys( $replacements ), array_values( $replacements), $output_template ); 
  278.  
  279. // Add to output 
  280. $content .= $item_output; 
  281.  
  282.  
  283. $content .= "</div>"; 
  284.  
  285. $return = $content; 
  286.  
  287. return $return; 
  288.  
  289. /** 
  290. * Add a Business Hours field to the Advanced Fields group 
  291. * @param [type] $field_groups [description] 
  292. */ 
  293. public function add_field_button($field_groups) { 
  294.  
  295. foreach ($field_groups as &$group) { 
  296.  
  297. if ($group['name'] === 'advanced_fields' ) { 
  298.  
  299. $group['fields'][] = array( 
  300. 'class' => 'button',  
  301. 'value' => __('Business Hours', 'gravity-forms-business-hours'),  
  302. 'onclick' => "StartAddField('business_hours');" 
  303. ); 
  304.  
  305. break; 
  306.  
  307. return $field_groups; 
  308.  
  309. /** 
  310. * Change the default field label 
  311. */ 
  312. public function set_defaults() { 
  313. ?> 
  314. //this hook is fired in the middle of a switch statement,  
  315. //so we need to add a case for our new field type 
  316. case "business_hours" : 
  317. field.inputs = null; 
  318. field.label = "<?php echo esc_js( __('Business Hours', 'gravity-forms-business-hours') ); ?>"; //setting the default field label 
  319. break; 
  320. <?php 
  321.  
  322. /** 
  323. * Set the inputs for the field type 
  324. * @return void 
  325. */ 
  326. public function editor_script() { 
  327. ?> 
  328. <script type='text/javascript'> 
  329. fieldSettings['business_hours'] = ".label_setting, .visibility_setting, .admin_label_setting, .rules_setting, .description_setting, .conditional_logic_field_setting, .css_class_setting"; 
  330. </script> 
  331. <?php 
  332.  
  333. /** 
  334. * Convert the field value into an array of days 
  335. * @param string $value Value of the field 
  336. * @return array|NULL NULL if not valid or empty; array if exists and is JSON 
  337. */ 
  338. public static function get_list_from_value( $value ) { 
  339.  
  340. $list = json_decode( html_entity_decode( $value ), true ); 
  341.  
  342. // Sometimes it's double-encoded 
  343. if( is_string( $list ) ) { 
  344. $list = json_decode( $list, true ); 
  345.  
  346. if( empty( $list ) ) { 
  347. return NULL; 
  348.  
  349. // Sort the days of the week 
  350. usort( $list, array('GFBusinessHours', 'sort_days') ); 
  351.  
  352. return $list; 
  353.  
  354. /** 
  355. * Sort the list by the day and times entered 
  356. * @param array $a Item 1 to be compared 
  357. * @param array $b Item 2 to be compared 
  358. * @return int 0 no change; -1 move down; +1 move up 
  359. */ 
  360. static function sort_days( $a, $b ) { 
  361.  
  362. // Generate a timestamp for the different options 
  363. $a_time = self::get_timestamp_from_time_span( $a ); 
  364. $b_time = self::get_timestamp_from_time_span( $b ); 
  365.  
  366. // If same time, don't up/down sort 
  367. if( $a_time === $b_time ) { 
  368. return 0; 
  369.  
  370. // If A > B, move down the list (+1). Otherwise, move up (-1). 
  371. return ( $a_time > $b_time ) ? +1 : -1; 
  372.  
  373. /** 
  374. * Convert a timespan item into a timestamp for the blog's timezone 
  375. * @param array $time_span Timespan array with at least day, fromtime keys 
  376. * @return float Timestamp in float format, since that's what WP's `current_time()` returns 
  377. */ 
  378. public static function get_timestamp_from_time_span( $time_span, $from_or_to = 'from' ) { 
  379.  
  380. // Only allow from or to 
  381. if( $from_or_to !== 'from' ) { 
  382. $from_or_to = 'to'; 
  383.  
  384. // `fromtime` or `totime` 
  385. $time_value = $time_span[$from_or_to.'time']; 
  386.  
  387. // Full weekday in English 
  388. $day_value = $time_span['day']; 
  389.  
  390. // After midnight! 
  391. // We add a day to the strtotime value 
  392. // And strip the + from the time to use the standard `H:i` value 
  393. if( substr( $time_value, 0, 1 ) === '+' ) { 
  394. $day_value .= ' +1 day'; 
  395. $time_value = str_replace('+', '', $time_value); 
  396.  
  397. // strtotime sentence 
  398. $str_to_time_string = $day_value .' this week '.$time_value; 
  399.  
  400. // Blog timestamp 
  401. $current_time = current_time( 'timestamp' ); 
  402.  
  403. $timestamp = strtotime($str_to_time_string , $current_time ); 
  404.  
  405. return floatval( $timestamp ); 
  406.  
  407. /** 
  408. * Is the business open now for the passed time span? 
  409. * @param array $time_span Time span with `day` `fromtime` and `totime` 
  410. * @return boolean True: open; False: not open 
  411. */ 
  412. public static function is_open_now( $time_span ) { 
  413.  
  414. // Blog timestamp 
  415. $current_time = current_time( 'timestamp' ); 
  416.  
  417. $from_time = self::get_timestamp_from_time_span( $time_span, 'from' ); 
  418. $to_time = self::get_timestamp_from_time_span( $time_span, 'to' ); 
  419.  
  420. if( $current_time < $from_time ) { 
  421. return false; 
  422.  
  423. if( $current_time > $to_time ) { 
  424. return false; 
  425.  
  426. return true; 
  427.  
  428. /** 
  429. * Generate the Open Now lael 
  430. * @param array $time_span Time span array 
  431. * @return string HTML output, if open. Empty string if not. 
  432. */ 
  433. public static function open_label( $time_span ) { 
  434.  
  435. $output = ''; 
  436.  
  437. if( self::is_open_now( $time_span ) ) { 
  438.  
  439. $output = '<span class="open_label">'; 
  440. $output .= esc_html__('Open Now', 'gravity-forms-business-hours'); 
  441. $output .= '</span>'; 
  442.  
  443. /** 
  444. * Modify the label for Open now 
  445. * @var string 
  446. */ 
  447. $output = apply_filters('gravityforms_business_hours_open_label', $output, $time_span ); 
  448.  
  449. return $output; 
  450.  
  451. /** 
  452. * Display field to be shown in Form and when editing entry 
  453. * @param [type] $content [description] 
  454. * @param [type] $field [description] 
  455. * @param [type] $value [description] 
  456. * @param [type] $lead_id [description] 
  457. * @param [type] $form_id [description] 
  458. * @return [type] [description] 
  459. */ 
  460. function business_hours_field($content, $field, $value, $lead_id, $form_id) { 
  461.  
  462. if ($field['type'] !== 'business_hours') { 
  463. return $content; 
  464.  
  465. $return = $content; 
  466.  
  467. $list = self::get_list_from_value( $value ); 
  468.  
  469. $list_string = ''; 
  470.  
  471. // Populate existing list items 
  472. if (!empty($list)) { 
  473.  
  474. foreach ($list as $list_value) { 
  475. $list_string .= '<div class="business_hours_list_item">'; 
  476.  
  477. $list_string .= '<strong>' . $list_value['daylabel'] . '</strong>'; 
  478. $list_string .= '<span>' . $list_value['fromtimelabel'] . '</span>'; 
  479. $list_string .= ' - '; 
  480. $list_string .= '<span>' . $list_value['totimelabel'] . '</span>'; 
  481. $list_string .= '<a href="#" class="business_hours_remove_button"><i class="dashicons dashicons-dismiss"></i></a>'; 
  482. $list_string .= '</div>'; 
  483.  
  484. $return .= ' 
  485. <div rel="business_hours_' . $field['id'] . '" class="business_hours field_setting business_hours_item" > 
  486. <input type="hidden" name="input_' . $field['id'] . '" value=\'' . json_encode( $list ) . '\' /> 
  487. <div class="business_hours_list">' . $list_string . '</div> 
  488. <div class="business_hours_add_form"> 
  489. '; 
  490. $return .= self::get_day_select(); 
  491.  
  492. /** 
  493. * Change the default start time. 
  494. * @param string $time Time in 'H:i' format 
  495. */ 
  496. $default_start_time = apply_filters( 'gravityforms_business_hours_default_start_time', '09:00' ); 
  497.  
  498. /** 
  499. * Change the default end time 
  500. * @param string $time Time in 'H:i' format 
  501. */ 
  502. $default_end_time = apply_filters( 'gravityforms_business_hours_default_end_time', '17:00' ); 
  503.  
  504. $return .= self::get_times_select('item_fromtime', $default_start_time, false); 
  505. $return .= self::get_times_select('item_totime', $default_end_time, true); 
  506.  
  507. $return .= '<a href="#" class="button gform_button business_hours_add_button"><i class="dashicons dashicons-plus-alt"></i> ' . __('Add Hours', 'gravity-forms-business-hours') . '</a> 
  508. </div> 
  509. </div> 
  510. '; 
  511.  
  512. return $return; 
  513.  
  514. /** 
  515. * Generate the HTML for the times select 
  516. * @param string $class Class for the select input 
  517. * @param string $default Default value 
  518. * @param boolean $with_after_midnight Include the hours after midnight of the next day? 
  519. * @return string HTML <select> of time options 
  520. */ 
  521. static function get_times_select( $class = 'item_fromtime', $default = '', $with_after_midnight = false ) { 
  522.  
  523. $output_times = self::get_times( $with_after_midnight ); 
  524.  
  525. $output = '<select class="'.sanitize_html_class( $class ).'">'; 
  526.  
  527. foreach( $output_times as $value => $label ) { 
  528. $selected = selected( $default, $value, false ); 
  529.  
  530. $output .= '<option value="' . esc_attr( $value ) .'"'.$selected.'>' . $label . '</option>'; 
  531.  
  532. $output .= '</select>'; 
  533.  
  534. return $output; 
  535.  
  536. /** 
  537. * Generate array of times with key in `H:i` format and after-midnight in `+H:i` format 
  538. * @param boolean $with_after_midnight Include times for next day 
  539. * @return [type] [description] 
  540. */ 
  541. static function get_times( $with_after_midnight = false ) { 
  542.  
  543.  
  544. $key_format = 'H:i'; 
  545.  
  546. /** 
  547. * Modify the time format for the displayed value 
  548. * @param string 
  549. */ 
  550. $value_format = apply_filters( 'gravityforms_business_hours_time_format', 'g:i a' ); 
  551.  
  552. $starttime = '00:00'; 
  553. $time = new DateTime( $starttime ); 
  554.  
  555. /** 
  556. * Time interval for the time dropdown options 
  557. * @var int 
  558. */ 
  559. $interval_minutes = apply_filters( 'gravityforms_business_hours_interval', 30 ); 
  560. $interval_minutes = intval( $interval_minutes ); 
  561. $interval = new DateInterval('PT'.$interval_minutes.'M'); 
  562.  
  563. $temptime = ''; 
  564.  
  565. $times = array(); 
  566.  
  567. do { 
  568.  
  569. $key = $time->format( $key_format ); 
  570.  
  571. // 12:30 am 
  572. $value = $time->format( $value_format ); 
  573.  
  574. $times[ $key ] = $value; 
  575.  
  576. // Increase by 30 minute intervals 
  577. $time->add($interval); 
  578.  
  579. $temptime = $time->format( $key_format ); 
  580.  
  581. } while( $temptime !== $starttime ); 
  582.  
  583. // Build additional times for the next day closing times 
  584. if( $with_after_midnight ) { 
  585.  
  586. $next_day = __('next day', 'gravity-forms-business-hours'); 
  587.  
  588. foreach( $times as $key => $time ) { 
  589.  
  590. $times[ '+'.$key ] = sprintf( '%s (%s)', $time, $next_day ); 
  591.  
  592. // Only show "Next day" times until 7am 
  593. if( $key === '07:00' ) { 
  594. break; 
  595.  
  596.  
  597. return $times; 
  598.  
  599. /** 
  600. * Build a select field with the full name of the day as the value and abreviation as the label 
  601. * @return string HTML <select> field 
  602. */ 
  603. static function get_day_select() { 
  604.  
  605. $output = '<select class="item_day">'; 
  606.  
  607. $days = self::get_days(); 
  608.  
  609. foreach ($days as $key => $value) { 
  610. $output .= '<option value="'.esc_attr( $key ).'">'.esc_html( $value ).'</option>'; 
  611.  
  612. $output .= '</select>'; 
  613.  
  614. return $output; 
  615.  
  616. /** 
  617. * Encode the field value 
  618. * @param [type] $value [description] 
  619. * @param [type] $lead [description] 
  620. * @param [type] $field [description] 
  621. * @param [type] $form [description] 
  622. * @return [type] [description] 
  623. */ 
  624. public function save_field_value($value, $lead, $field, $form) { 
  625.  
  626. if (!empty($field['type']) && $field['type'] === 'business_hours') { 
  627.  
  628. $is_already_json = json_decode( $value ); 
  629.  
  630. // Don't double-encode 
  631. if( is_null( $is_already_json ) ) { 
  632. return json_encode( $value ); 
  633.  
  634. return $value; 
  635.  
  636. /** 
  637. * Enqueue scripts 
  638. * @return [type] [description] 
  639. */ 
  640. public function scripts() { 
  641.  
  642. $script_debug = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; 
  643.  
  644. $scripts = array( 
  645. array( 
  646. "handle" => "business_hours_app",  
  647. "src" => $this->get_base_url() . "/assets/js/public{$script_debug}.js",  
  648. "version" => $this->_version,  
  649. "deps" => array("jquery"),  
  650. 'callback' => array($this, 'localize_scripts'),  
  651. "enqueue" => array( 
  652. array( 
  653. "field_types" => array("business_hours"),  
  654. ),  
  655. array( 
  656. "admin_page" => array( 
  657. "entry_edit",  
  658. "entry_detail" 
  659. ),  
  660. ),  
  661. ),  
  662. ); 
  663.  
  664. return array_merge(parent::scripts(), $scripts); 
  665.  
  666. /** 
  667. * Provide translation for the scripts 
  668. * @return void 
  669. */ 
  670. public function localize_scripts() { 
  671.  
  672. $days = self::get_days(); 
  673.  
  674. $strings = array( 
  675. 'already_exists' => __('This combination already exists', 'gravity-forms-business-hours'),  
  676. ); 
  677.  
  678. wp_localize_script('business_hours_app', 'GFBusinessHours', $strings); 
  679.  
  680. wp_localize_script('business_hours_app_admin', 'GFBusinessHours', $strings); 
  681.  
  682.  
  683. /** 
  684. * Enqueue styles using the Gravity Forms Addons framework 
  685. * @see GFAddOn::styles() 
  686. * @return void 
  687. */ 
  688. public function styles() { 
  689. $styles = array( 
  690. array("handle" => "business_hours_admin_style",  
  691. "src" => $this->get_base_url() . "/assets/css/admin.css",  
  692. "version" => $this->_version,  
  693. "enqueue" => array( 
  694. array( 
  695. "admin_page" => array( 
  696. "entry_view",  
  697. "entry_detail",  
  698. "form_editor",  
  699. ),  
  700. ),  
  701. ),  
  702. array("handle" => "business_hours_frontend_style",  
  703. "src" => $this->get_base_url() . "/assets/css/public.css",  
  704. "version" => $this->_version,  
  705. "deps" => array("dashicons"),  
  706. "enqueue" => array( 
  707. array("field_types" => array("business_hours")),  
  708. array( 
  709. "admin_page" => array( 
  710. "entry_edit",  
  711. "entry_detail" 
  712. ),  
  713. ),  
  714. ),  
  715. ),  
  716. ); 
  717. return array_merge(parent::styles(), $styles);