1
0

abstract.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Abstract Class
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! class_exists( 'CSF_Abstract' ) ) {
  11. abstract class CSF_Abstract {
  12. public $abstract = '';
  13. public $output_css = '';
  14. public function __construct() {
  15. // Collect output css and typography
  16. if ( ! empty( $this->args['output_css'] ) || ! empty( $this->args['enqueue_webfont'] ) ) {
  17. add_action( 'wp_enqueue_scripts', array( $this, 'collect_output_css_and_typography' ), 10 );
  18. Sakurairo_CSF::$css = apply_filters( "csf_{$this->unique}_output_css", Sakurairo_CSF::$css, $this );
  19. }
  20. }
  21. public function collect_output_css_and_typography() {
  22. $this->recursive_output_css( $this->pre_fields );
  23. }
  24. public function recursive_output_css( $fields = array(), $combine_field = array() ) {
  25. if ( ! empty( $fields ) ) {
  26. foreach ( $fields as $field ) {
  27. $field_id = ( ! empty( $field['id'] ) ) ? $field['id'] : '';
  28. $field_type = ( ! empty( $field['type'] ) ) ? $field['type'] : '';
  29. $field_output = ( ! empty( $field['output'] ) ) ? $field['output'] : '';
  30. $field_check = ( $field_type === 'typography' || $field_output ) ? true : false;
  31. $field_class = 'CSF_Field_' . $field_type;
  32. if ( $field_type && $field_id ) {
  33. if( $field_type === 'fieldset' ) {
  34. if ( ! empty( $field['fields'] ) ) {
  35. $this->recursive_output_css( $field['fields'], $field );
  36. }
  37. }
  38. if( $field_type === 'accordion' ) {
  39. if ( ! empty( $field['accordions'] ) ) {
  40. foreach ( $field['accordions'] as $accordion ) {
  41. $this->recursive_output_css( $accordion['fields'], $field );
  42. }
  43. }
  44. }
  45. if( $field_type === 'tabbed' ) {
  46. if ( ! empty( $field['tabs'] ) ) {
  47. foreach ( $field['tabs'] as $accordion ) {
  48. $this->recursive_output_css( $accordion['fields'], $field );
  49. }
  50. }
  51. }
  52. if ( class_exists( $field_class ) ) {
  53. if ( method_exists( $field_class, 'output' ) || method_exists( $field_class, 'enqueue_google_fonts' ) ) {
  54. $field_value = '';
  55. if ( $field_check && ( $this->abstract === 'options' || $this->abstract === 'customize' ) ) {
  56. if( ! empty( $combine_field ) ) {
  57. $field_value = ( isset( $this->options[$combine_field['id']][$field_id] ) ) ? $this->options[$combine_field['id']][$field_id] : '';
  58. } else {
  59. $field_value = ( isset( $this->options[$field_id] ) ) ? $this->options[$field_id] : '';
  60. }
  61. } else if ( $field_check && ( $this->abstract === 'metabox' && is_singular() || $this->abstract === 'taxonomy' && is_archive() ) ) {
  62. if( ! empty( $combine_field ) ) {
  63. $meta_value = $this->get_meta_value( $combine_field );
  64. $field_value = ( isset( $meta_value[$field_id] ) ) ? $meta_value[$field_id] : '';
  65. } else {
  66. $meta_value = $this->get_meta_value( $field );
  67. $field_value = ( isset( $meta_value ) ) ? $meta_value : '';
  68. }
  69. }
  70. $instance = new $field_class( $field, $field_value, $this->unique, 'wp/enqueue', $this );
  71. // typography enqueue and embed google web fonts
  72. if ( $field_type === 'typography' && $this->args['enqueue_webfont'] && ! empty( $field_value['font-family'] ) ) {
  73. $method = ( ! empty( $this->args['async_webfont'] ) ) ? 'async' : 'enqueue';
  74. $instance->enqueue_google_fonts( $method );
  75. }
  76. // output css
  77. if ( $field_output && $this->args['output_css'] ) {
  78. Sakurairo_CSF::$css .= $instance->output();
  79. }
  80. unset( $instance );
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. public function pre_tabs( $sections ) {
  88. $count = 100;
  89. $result = array();
  90. $parents = array();
  91. foreach ( $sections as $key => $section ) {
  92. if ( ! empty( $section['parent'] ) ) {
  93. $section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
  94. $parents[$section['parent']][] = $section;
  95. unset( $sections[$key] );
  96. }
  97. $count++;
  98. }
  99. foreach ( $sections as $key => $section ) {
  100. $section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
  101. if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) {
  102. $section['subs'] = wp_list_sort( $parents[$section['id']], array( 'priority' => 'ASC' ), 'ASC', true );
  103. }
  104. $result[] = $section;
  105. $count++;
  106. }
  107. return wp_list_sort( $result, array( 'priority' => 'ASC' ), 'ASC', true );
  108. }
  109. public function pre_sections( $sections ) {
  110. $result = array();
  111. foreach ( $this->pre_tabs( $sections ) as $section ) {
  112. if ( ! empty( $section['subs'] ) ) {
  113. foreach ( $section['subs'] as $sub ) {
  114. $sub['ptitle'] = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
  115. $result[] = $sub;
  116. }
  117. }
  118. if ( empty( $section['subs'] ) ) {
  119. $result[] = $section;
  120. }
  121. }
  122. return $result;
  123. }
  124. public function pre_fields( $sections ) {
  125. $result = array();
  126. foreach ( $sections as $key => $section ) {
  127. if ( ! empty( $section['fields'] ) ) {
  128. foreach ( $section['fields'] as $field ) {
  129. $result[] = $field;
  130. }
  131. }
  132. }
  133. return $result;
  134. }
  135. }
  136. }