Bez popisu

base-widget.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. class BeRocket_Brand_Base_Widget extends WP_Widget {
  3. protected $defaults, $form_fields, $shortcode_args;
  4. public function __construct( $widget_name, $widget_title, $args ) {
  5. $this->defaults = array(
  6. 'title' => '',
  7. );
  8. $this->form_fields = array(
  9. 'title' => array(
  10. "title" => __( 'Title:', 'brands-for-woocommerce' ),
  11. 'type' => 'text',
  12. 'class' => 'width100',
  13. ),
  14. );
  15. $this->shortcode_args = array();
  16. parent::__construct( $widget_name, $widget_title, $args );
  17. }
  18. protected function set_cache_key( $instance ) {
  19. if ( !empty( $instance['cache_key'] ) ) return $instance;
  20. $instance['cache_key'] = $this->id . brfr_language_prefix();
  21. return $instance;
  22. }
  23. protected function replace_shortcode_keys( $instance ) {
  24. foreach ( $this->shortcode_args as $old => $new ) {
  25. if ( !isset( $instance[ $new ] ) && isset( $instance[ $old ] ) ) {
  26. $instance[ $new ] = $instance[ $old ];
  27. }
  28. }
  29. return $instance;
  30. }
  31. protected function get_size( $side, $atts ) {
  32. if ( empty( $atts[$side] ) ) return $atts;
  33. $size = $atts[$side];
  34. if ( is_numeric( $size ) ) {
  35. if ( empty( $atts["{$side}_units"] ) ) {
  36. $atts["{$side}_units"] = 'px';
  37. }
  38. } else {
  39. $size_numeric = intval( $size );
  40. $atts["{$side}_units"] = str_replace( $size_numeric, '', $size );
  41. $atts[$side] = $size_numeric;
  42. }
  43. return $atts;
  44. }
  45. // $args = array( 'attributes' => [ options for <select> as array( 'value' => 'title' ); or placeholder, min/max for <input> ]
  46. private function form_field_text( $name, $instance, $args = array() ) {
  47. $field_id = $this->get_field_id( $name );
  48. $field_name = $this->get_field_name( $name );
  49. $field_value = esc_attr( $instance[$name] );
  50. $class = empty( $args['class'] ) ? '' : $args['class'];
  51. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  52. $input_class = empty( $args['input_class'] ) ? '' : $args['input_class'];
  53. $placeholder = empty( $args['placeholder'] ) ? '' : "placeholder='{$args['placeholder']}'";
  54. $html = "<input class='widefat $input_class' name='$field_name' type='text' value='$field_value' $placeholder />";
  55. if ( empty( $args['title'] ) ) return $html;
  56. return "<p class='br_brands_textfield $class' $id><label for='$field_id'>{$args['title']}</label> $html</p>";
  57. }
  58. private function form_field_checkbox( $name, $instance, $args = array() ) {
  59. $field_id = $this->get_field_id( $name );
  60. $field_name = $this->get_field_name( $name );
  61. $checked = $instance[$name] ? 'checked' : '';
  62. $class = empty( $args['class'] ) ? '' : $args['class'];
  63. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  64. $input_class = empty( $args['input_class'] ) ? '' : "class='{$args['input_class']}'";
  65. $html = "<input type='checkbox' value='1' $input_class name='$field_name' $checked />";
  66. if ( empty( $args['title'] ) ) return $html;
  67. return "<p class='br_brands_checkbox $class' $id>$html <label for='$field_id'>{$args['title']}</label></p>";
  68. }
  69. private function form_field_select( $name, $instance, $args = array() ) {
  70. if ( empty( $args['options'] ) ) return '';
  71. $field_id = $this->get_field_id( $name );
  72. $field_name = $this->get_field_name( $name );
  73. $class = empty( $args['class'] ) ? '' : $args['class'];
  74. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  75. $input_class = empty( $args['input_class'] ) ? '' : "class='{$args['input_class']}'";
  76. $saved_value = $instance[$name];
  77. $html = "<select $input_class name='$field_name'>";
  78. foreach ( $args['options'] as $option_value => $option ) {
  79. if ( empty( $option['name'] ) ) continue;
  80. $selected = ( $option_value == $saved_value ) ? 'selected' : '';
  81. $option_class = empty( $option['class'] ) ? '' : "class='{$option['class']}'";
  82. $html .= "<option value='$option_value' $option_class $selected>{$option['name']}</option>";
  83. }
  84. $html .= '</select>';
  85. if ( empty( $args['title'] ) ) return $html;
  86. return "<p class='br_brands_selectbox $class' $id><label for='$field_id'>{$args['title']}</label> $html</p>";
  87. }
  88. private function form_field_number( $name, $instance, $args = array() ) {
  89. $field_id = $this->get_field_id( $name );
  90. $field_name = $this->get_field_name( $name );
  91. $class = empty( $args['class'] ) ? '' : $args['class'];
  92. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  93. $input_class = empty( $args['input_class'] ) ? '' : $args['input_class'];
  94. $placeholder = empty( $args['placeholder'] ) ? '' : "placeholder='{$args['placeholder']}'";
  95. $min = empty( $args['min'] ) ? "min='0'" : "min='{$args['min']}'";
  96. $max = empty( $args['max'] ) ? '' : "max='{$args['max']}'";
  97. $html = "<input type='number' $min $max class='br_brand_number $input_class' value='{$instance[$name]}' name='$field_name' $placeholder />";
  98. if ( empty( $args['title'] ) ) return $html;
  99. return "<p class='br_brands_numberbox $class' $id><label for='$field_id'>{$args['title']}</label> $html</p>";
  100. }
  101. private function form_field_size( $name, $instance, $args = array() ) {
  102. $field_id = $this->get_field_id( $name );
  103. $field_name = $this->get_field_name( $name );
  104. $class = empty( $args['class'] ) ? '' : $args['class'];
  105. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  106. $html = $this->form_field_number( $name, $instance );
  107. $html .= $this->form_field_select( "{$name}_units", $instance, array( 'options' => array(
  108. 'px' => array( 'name' => 'px' ),
  109. '%' => array( 'name' => '%' ),
  110. ) ) );
  111. if ( empty( $args['title'] ) ) return $html;
  112. return "<p class='br_brands_sizebox $class' $id><label for='$field_id'>{$args['title']}</label> $html</p>";
  113. }
  114. private function form_field_image( $name, $instance, $args = array() ) {
  115. $title = empty( $args['title'] ) ? '' : $args['title'];
  116. $html = "<fieldset class='br_brand_fieldset'><legend>$title</legend>";
  117. $html .= $this->form_field_checkbox( "{$name}_display", $instance, array(
  118. "title" => __( 'Display', 'brands-for-woocommerce' ),
  119. 'class' => 'width100 br_brand_show_more_options',
  120. 'id' => "{$name}_display_depending",
  121. ) );
  122. $html .= $this->form_field_size( "{$name}_width", $instance, array(
  123. "title" => __( 'Width', 'brands-for-woocommerce' ),
  124. 'class' => "width50 {$name}_display_depending",
  125. ) );
  126. $html .= $this->form_field_size( "{$name}_height", $instance, array(
  127. "title" => __( 'Height', 'brands-for-woocommerce' ),
  128. 'class' => "width50 {$name}_display_depending",
  129. ) );
  130. $html .= $this->form_field_select( "{$name}_fit", $instance, array(
  131. "title" => __( 'Fit', 'brands-for-woocommerce' ),
  132. 'class' => "width50 {$name}_display_depending",
  133. 'options' => array(
  134. "cover" => array( 'name' => __( 'Cover', 'brands-for-woocommerce' ) ),
  135. "contain" => array( 'name' => __( 'Contain', 'brands-for-woocommerce' ) ),
  136. "fill" => array( 'name' => __( 'Fill', 'brands-for-woocommerce' ) ),
  137. "none" => array( 'name' => __( 'None', 'brands-for-woocommerce' ) ),
  138. ),
  139. ) );
  140. if ( !empty( $args['align_options'] ) ) {
  141. $html .= $this->form_field_select( "{$name}_align", $instance, array(
  142. "title" => __( 'Align', 'brands-for-woocommerce' ),
  143. 'class' => "width50 {$name}_display_depending",
  144. 'options' => $args['align_options'],
  145. ) );
  146. }
  147. $html .= "</fieldset>";
  148. return $html;
  149. }
  150. private function form_field_color( $name, $instance, $args = array() ) {
  151. $field_id = $this->get_field_id( $name );
  152. $field_name = $this->get_field_name( $name );
  153. $title = empty( $args['title'] ) ? '' : "<label for='$field_id'>{$args['title']}</label>";
  154. $class = empty( $args['class'] ) ? '' : $args['class'];
  155. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  156. return
  157. "<p class='br_brands_colorbox $class' $id>
  158. $title
  159. <input class='widefat br_brand_colorpicker' id='$field_id' name='$field_name' type='text' value='{$instance[$name]}' data-default-color='#000'/>
  160. </p>";
  161. }
  162. private function form_field_taxonomyselect( $name, $instance, $args = array() ) {
  163. if ( empty( $args['walker'] ) ) return '';
  164. $field_id = $this->get_field_id( $name );
  165. $field_name = $this->get_field_name( $name );
  166. $title = empty( $args['title'] ) ? '' : "<label for='$field_id'>{$args['title']}</label>";
  167. $class = empty( $args['class'] ) ? '' : $args['class'];
  168. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  169. $brands = empty( $instance[$name] ) ? array() : array_map( function($v) {
  170. $term = get_term_by( 'term_id', $v, BeRocket_product_brand::$taxonomy_name ); return $term->term_id; },
  171. unserialize( $instance[$name] ) );
  172. return "<p class='br_brand_product_categories_container $class' $id>$title
  173. <ul class='br_brand_product_categories $class'>"
  174. . wp_terms_checklist( 0, array(
  175. 'taxonomy' => BeRocket_product_brand::$taxonomy_name,
  176. 'walker' => new $args['walker'],
  177. 'selected_cats' => $brands,
  178. 'checked_ontop' => false,
  179. 'echo' => false,
  180. ) ) . '</ul></p>';
  181. }
  182. private function form_field_autocomplete( $name, $instance, $args = array() ) {
  183. $field_id = $this->get_field_id( $name );
  184. $field_name = $this->get_field_name( $name );
  185. $field_value = esc_attr( $instance[$name] );
  186. $title = empty( $args['title'] ) ? '' : "<label for='$field_id'>{$args['title']}</label>";
  187. $class = empty( $args['class'] ) ? '' : $args['class'];
  188. $id = empty( $args['id'] ) ? '' : "id='{$args['id']}'";
  189. $input_class = empty( $args['input_class'] ) ? '' : $args['input_class'];
  190. $placeholder = empty( $args['placeholder'] ) ? '' : "placeholder='{$args['placeholder']}'";
  191. // $selected_brands = empty( $instance[$name] ) ? array() : array_map( function($v) {
  192. // $term = get_term_by( 'term_id', $v, BeRocket_product_brand::$taxonomy_name ); return $term->term_id; },
  193. // unserialize( $instance[$name] ) );
  194. return
  195. "<p class='ui-widget br_brand_autocomplete_container $class' $id>
  196. $title
  197. <input class='widefat $input_class br_brand_autocomplete' name='$field_name' type='text' value='$field_value' $placeholder />
  198. </p>";
  199. }
  200. private function form_field_fieldset( $name, $instance, $args = array() ) {
  201. if ( empty( $args['items'] ) ) return;
  202. $title = empty( $args['title'] ) ? '' : $args['title'];
  203. $this->render_form_fields(
  204. "<fieldset class='br_brand_fieldset'><legend>$title</legend>", "</fieldset>", $args['items'], $instance );
  205. }
  206. private function render_form_fields( $before, $after, $fields_array, $instance ) {
  207. echo $before;
  208. foreach ( $fields_array as $name => $fields ) {
  209. $function = "form_field_{$fields['type']}";
  210. // $options = empty( $fields['attributes'] ) ? array() : $fields['attributes'];
  211. echo $this->$function( $name, $instance, $fields );
  212. }
  213. echo $after;
  214. }
  215. public function form($instance) {
  216. $instance = $this->replace_shortcode_keys( $instance );
  217. $instance = wp_parse_args( (array)$instance, $this->defaults );
  218. $this->render_form_fields( "<div class='br_brand_widget_oprions'>", "</div>", $this->form_fields, $instance );
  219. }
  220. public function update( $new_instance, $old_instance ) {
  221. // $instance = $old_instance;
  222. foreach ( array_keys( $this->defaults ) as $name ) {
  223. $instance[$name] = empty( $new_instance[$name] ) ? '' : strip_tags( $new_instance[$name] );
  224. }
  225. return $instance;
  226. }
  227. public function widget( $args, $instance ) {
  228. if ( empty( $args['template'] ) ) return;
  229. ob_start();
  230. $BeRocket_product_brand = BeRocket_product_brand::getInstance();
  231. $BeRocket_product_brand->br_get_template_part( $args['template'] );
  232. $content = ob_get_clean();
  233. if( $content ) {
  234. echo $args['before_widget'];
  235. if ( !empty( $instance['title'] ) ) echo $args['before_title'], $instance['title'], $args['after_title'];
  236. echo $content;
  237. echo $args['after_widget'];
  238. }
  239. }
  240. }