class-custom-fields.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. class Booked_WC_Custom_Fields {
  3. private function __construct() {
  4. if (
  5. (
  6. is_admin()
  7. && isset($_GET['page'])
  8. && $_GET['page'] !== 'booked-setting'
  9. ) ||
  10. (
  11. !empty($_POST['action'])
  12. && $_POST['action']==='booked_admin_load_full_customfields'
  13. )
  14. ) {
  15. $this->admin_hooks();
  16. } else {
  17. $this->front_end_hooks();
  18. }
  19. }
  20. protected function admin_hooks() {
  21. // add Paid Service Selector button
  22. add_action('booked_custom_fields_add_buttons', array($this, 'booked_custom_fields_add_buttons'));
  23. // add Paid Service Selector button
  24. add_action('booked_custom_fields_add_template', array($this, 'booked_custom_fields_add_template'));
  25. // add prepopulated template with options
  26. add_filter('booked_custom_fields_add_template_main', array($this, 'booked_custom_fields_add_template_main'), 10, 7);
  27. add_filter('booked_custom_fields_add_template_subs', array($this, 'booked_custom_fields_add_template_subs'), 10, 4);
  28. // close the html if the last field has type booked appointment
  29. add_action('booked_custom_fields_add_template_subs_end', array($this, 'booked_custom_fields_add_template_subs_end'), 10, 2);
  30. }
  31. protected function front_end_hooks() {
  32. // add prepopulated template with options
  33. add_filter('booked_custom_fields_add_template_main', array($this, '_booked_custom_fields_add_template_main'), 10, 8);
  34. add_filter('booked_custom_fields_add_template_subs', array($this, '_booked_custom_fields_add_template_subs'), 10, 7);
  35. // close the html if the last field has type booked appointment
  36. add_action('booked_custom_fields_add_template_subs_end', array($this, '_booked_custom_fields_add_template_subs_end'), 10, 2);
  37. }
  38. public static function setup() {
  39. return new self();
  40. }
  41. # ------------------
  42. # Administration
  43. # ------------------
  44. public function booked_custom_fields_add_buttons() {
  45. Booked_WC_Fragments::load('booked-administration-fields/buttons');
  46. }
  47. public function booked_custom_fields_add_template() {
  48. Booked_WC_Fragments::load('booked-administration-fields/templates');
  49. }
  50. public function booked_custom_fields_add_template_subs($field_type='', $name='', $value='', $look_for_subs='') {
  51. $reset_subs = true;
  52. if ( $field_type==='single-paid-service' ) {
  53. $reset_subs = false;
  54. $template_path = Booked_WC_Fragments::get_path('booked-administration-fields/templates-subs');
  55. include($template_path);
  56. } else if ( $look_for_subs==='paid-service' ) {
  57. $this->booked_custom_fields_add_template_subs_end($field_type, $look_for_subs);
  58. }
  59. return $reset_subs;
  60. }
  61. public function booked_custom_fields_add_template_main($default_return=false, $field_type='', $name='', $value='', $is_required=false, $look_for_subs='', $numbers_only=0) {
  62. $template_path = Booked_WC_Fragments::get_path('booked-administration-fields/templates-main');
  63. $look_for_subs = include($template_path); // echo + return
  64. return $look_for_subs;
  65. }
  66. public function booked_custom_fields_add_template_subs_end($field_type='', $look_for_subs='') {
  67. $template_path = Booked_WC_Fragments::get_path('booked-administration-fields/templates-subs-end');
  68. include($template_path);
  69. }
  70. # ------------------
  71. # Front End
  72. # ------------------
  73. public function _booked_custom_fields_add_template_subs($field_type='', $name='', $value='', $is_required=false, $look_for_subs='', $numbers_only=0, $data_attributes='') {
  74. $reset_subs = true;
  75. if ( $field_type==='single-paid-service' ):
  76. $reset_subs = false;
  77. $template_path = Booked_WC_Fragments::get_path('booked-frontend-fields/templates-subs');
  78. include($template_path);
  79. elseif ( $look_for_subs==='paid-service' ):
  80. $this->_booked_custom_fields_add_template_subs_end($field_type, $look_for_subs);
  81. endif;
  82. return $reset_subs;
  83. }
  84. public function _booked_custom_fields_add_template_main($default_return=false, $field_type='', $name='', $value='', $is_required=false, $look_for_subs='', $numbers_only=0, $data_attributes='') {
  85. if ( $field_type==='paid-service-label' ):
  86. $template_path = Booked_WC_Fragments::get_path('booked-frontend-fields/templates-main');
  87. include($template_path);
  88. $look_for_subs = 'paid-service';
  89. endif;
  90. return $look_for_subs;
  91. }
  92. public function _booked_custom_fields_add_template_subs_end($field_type='', $look_for_subs='') {
  93. $template_path = Booked_WC_Fragments::get_path('booked-frontend-fields/templates-subs-end');
  94. include($template_path);
  95. }
  96. }