Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Better ACF Datepicker (replaces jQuery's datepicker)
  5. */
  6. class acf_field_datepicker extends acf_field {
  7.  
  8. $css_path = get_template_directory_uri() . '/css/datepicker.css';
  9. $js_path = get_template_directory_uri() . '/js/datepicker.js';
  10.  
  11. function __construct() {
  12.  
  13. // vars
  14. $this->name = 'date_picker';
  15. $this->label = __('Date Picker');
  16. $this->category = __('jQuery', 'acf');
  17. $this->defaults = array(
  18. 'weekstart' => 0,
  19. 'multiple' => 0,
  20. 'inline' => 0,
  21. 'calendars' => 1,
  22. 'position' => 1,
  23. 'paginate' => false,
  24. );
  25.  
  26. // remove old date_picker
  27. remove_all_filters('acf/load_field_defaults/type=' . $this->name, 10);
  28. remove_all_actions('acf/create_field/type=' . $this->name, 10);
  29. remove_all_actions('acf/create_field_options/type=' . $this->name, 10);
  30.  
  31. // do not delete!
  32. parent::__construct();
  33.  
  34. // settings
  35. $this->settings = array(
  36. 'path' => apply_filters('acf/helpers/get_path', __FILE__),
  37. 'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
  38. 'version' => '1.0.0'
  39. );
  40. }
  41.  
  42. function create_options( $field ) {
  43. global $wp_locale;
  44.  
  45. $key = $field['name'];
  46.  
  47. ?>
  48. <tr class="field_option field_option_<?= $this->name; ?>">
  49. <td class="label">
  50. <label>Week Starts On</label>
  51. </td>
  52. <td>
  53. <?php do_action('acf/create_field', array(
  54. 'type' => 'select',
  55. 'name' => 'fields[' . $key . '][weekstart]',
  56. 'value' => $field['weekstart'],
  57. 'choices' => array_values( $wp_locale->weekday ),
  58. )); ?>
  59. </td>
  60. </tr>
  61. <tr class="field_option field_option_<?= $this->name; ?>">
  62. <td class="label">
  63. <label>Allow Multiple?</label>
  64. <p class="description">Allow multiple dates to be selected</p>
  65. </td>
  66. <td>
  67. <?php do_action('acf/create_field', array(
  68. 'type' => 'radio',
  69. 'name' => 'fields[' . $key . '][multiple]',
  70. 'value' => $field['multiple'],
  71. 'layout' => 'horizontal',
  72. 'choices' => array(
  73. true => 'Yes',
  74. false => 'No',
  75. ),
  76. )); ?>
  77.  
  78. </td>
  79. </tr>
  80. <tr class="field_option field_option_<?= $this->name; ?>">
  81. <td class="label">
  82. <label>Inline Calendar?</label>
  83. <p class="description">Hide the input and show only the calendar</p>
  84. </td>
  85. <td>
  86. <?php do_action('acf/create_field', array(
  87. 'type' => 'radio',
  88. 'name' => 'fields[' . $key . '][inline]',
  89. 'value' => $field['inline'],
  90. 'layout' => 'horizontal',
  91. 'choices' => array(
  92. true => 'Yes',
  93. false => 'No',
  94. ),
  95. )); ?>
  96. </td>
  97. </tr>
  98. <tr class="field_option field_option_<?= $this->name; ?>">
  99. <td class="label">
  100. <label>Number of Calendars</label>
  101. <p class="description">How many calendars to show at a time</p>
  102. <p>Useful with "Inline Calendar" enabled.</p>
  103. </td>
  104. <td>
  105. <?php do_action('acf/create_field', array(
  106. 'type' => 'number',
  107. 'name' => 'fields[' . $key . '][calendars]',
  108. 'value' => $field['calendars'],
  109. 'min' => 1,
  110. 'step' => 1,
  111. )); ?>
  112. </td>
  113. </tr>
  114. <tr class="field_option field_option_<?= $this->name; ?>">
  115. <td class="label">
  116. <label>Main Calendar Position</label>
  117. <p class="description">When using mutiple calendars, which calendar is considered the main calendar?</p>
  118. </td>
  119. <td>
  120. <?php do_action('acf/create_field', array(
  121. 'type' => 'number',
  122. 'name' => 'fields[' . $key . '][position]',
  123. 'value' => $field['position'],
  124. 'max' => $field['calendars'],
  125. 'min' => 1,
  126. 'step' => 1,
  127. )); ?>
  128. <script>
  129. jQuery('#acf-field-<?= $key; ?>_calendars').on('change', function() {
  130. jQuery('#acf-field-<?= $key; ?>_position').prop({
  131. max: this.value,
  132. value: function() {
  133. return Math.min(this.value, this.max);
  134. }
  135. });
  136. });
  137. </script>
  138. </td>
  139. </tr>
  140. <tr class="field_option field_option_<?= $this->name; ?>">
  141. <td class="label">
  142. <label>Arrow Pagination</label>
  143. <p class="description">Only applies to mutiple calendars</p>
  144. </td>
  145. <td>
  146. <?php do_action('acf/create_field', array(
  147. 'type' => 'radio',
  148. 'name' => 'fields[' . $key . '][paginate]',
  149. 'value' => $field['paginate'],
  150. 'layout' => 'horizontal',
  151. 'choices' => array(
  152. false => 'By Month',
  153. true => 'By Calendar Range',
  154. ),
  155. )); ?>
  156. </td>
  157. </tr>
  158. <?php
  159. }
  160.  
  161. function create_field( $field ) {
  162. $field = array_merge($this->defaults, $field);
  163.  
  164. ?>
  165. <input type="text" class="js-datepicker" id="<?= $field['id']; ?>" value="<?= $field['value']; ?>" name="<?= $field['name']; ?>"
  166. data-multiple="<?= $field['multiple']; ?>" data-calendars="<?= $field['calendars']; ?>" data-index="<?= $field['position'] - 1; ?>"
  167. data-week-start="<?= $field['weekstart']; ?>" data-inline="<?= $field['inline']; ?>" data-paginate="<?= $field['paginate'] ? $field['calendars'] : 1; ?>">
  168. <script>(window.datepickers = window.datepickers || []).push(new DatePicker('#<?= $field['id']; ?>'));</script>
  169. <?php
  170. }
  171.  
  172. function input_admin_enqueue_scripts() {
  173. wp_enqueue_script( 'datepicker', $this->js_path, false, $this->settings['version'] );
  174. wp_enqueue_style( 'datepicker', $this->css_path, false, $this->settings['version'] );
  175. }
  176. }
  177.  
  178. // create field
  179. new acf_field_datepicker();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement