Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. class Avada_Widget_Style {
  2.  
  3.  
  4. public function __construct() {
  5.  
  6. add_filter( 'in_widget_form', array( $this, 'add_widget_styling_options' ), 10, 3 );
  7. add_filter( 'widget_update_callback', array( $this, 'save_widget_styling_options' ), 10, 2 );
  8. // add_filter( 'widget_display_callback', array( $this, 'widget_display_callback' ), 10, 3 );
  9. add_filter( 'dynamic_sidebar_params', array( $this, 'add_widget_styles' ) );
  10. }
  11.  
  12.  
  13. public function add_widget_styling_options( $widget, $return, $instance ) {
  14.  
  15. $widget_area = $this->get_sidebar_id_from_widget_id( $widget->id );
  16.  
  17. if ( null !== $widget_area && false !== strpos( $widget_area, 'avada-footer-widget' ) ) {
  18.  
  19. $padding = isset( $instance['fusion_padding'] ) ? $instance['fusion_padding'] : '';
  20. ?>
  21. <p>
  22. <label for="<?php echo $widget->get_field_id( 'fusion_padding' ); ?>">
  23. <?php _e( 'Padding', 'Avada' ); ?>
  24. </label>
  25. <input class="widefat" type="text" id="<?php echo $widget->get_field_id( 'fusion_padding' ); ?>"
  26. name="<?php echo $widget->get_field_name( 'fusion_padding' ); ?>"
  27. value="<?php echo esc_attr( $padding ); ?>"
  28. />
  29. </p>
  30. <?php
  31. }
  32. }
  33.  
  34. public function get_sidebar_id_from_widget_id( $widget_id ) {
  35. $sidebars = wp_get_sidebars_widgets();
  36.  
  37. foreach( (array) $sidebars as $sidebar_id => $sidebar ) {
  38. if( in_array( $widget_id, (array) $sidebar, true ) )
  39. return $sidebar_id;
  40. }
  41.  
  42. return null; // not found case
  43. }
  44.  
  45. public function save_widget_styling_options( $instance, $new_instance ) {
  46.  
  47. if (! empty( $new_instance['fusion_padding'] ) ) {
  48. $new_instance['fusion_padding'] = sanitize_text_field( $new_instance['fusion_padding'] );
  49. }
  50.  
  51. return $new_instance;
  52. }
  53.  
  54. public function widget_display_callback( $instance, $widget, $args ) {
  55.  
  56. return $instance;
  57. }
  58.  
  59.  
  60. public function add_widget_styles( $params ) {
  61.  
  62. global $wp_registered_widgets;
  63.  
  64. if ( ! isset( $params[0] ) ) {
  65. return $params;
  66. }
  67.  
  68. if ( null !== $params[0]['id'] && false !== strpos( $params[0]['id'], 'avada-footer-widget' ) ) {
  69.  
  70. $sidebar_id = $params[0]['id']; // Get the id for the current sidebar we're processing
  71. $widget_id = $params[0]['widget_id'];
  72. $widget_obj = $wp_registered_widgets[ $widget_id ];
  73. $widget_num = $widget_obj['params'][0]['number'];
  74. $widget_opt = $this->get_widget_opt( $widget_obj );
  75.  
  76. // error_log( 'widget_opts' );
  77. // error_log( print_r( $widget_opt [ $widget_num ], true ) );
  78.  
  79. if ( ! empty( $widget_opt [ $widget_num ]['fusion_padding'] ) ) {
  80. $params[0]['before_widget'] = str_replace( '>', ' style="padding: ' . esc_attr( $widget_opt [ $widget_num ]['fusion_padding'] ) . ';">', $params[0]['before_widget'] );
  81. }
  82. }
  83.  
  84. return $params;
  85. }
  86.  
  87.  
  88. public function get_widget_opt( $widget_obj ) {
  89. $widget_opt = get_option( $widget_obj['callback'][0]->option_name );
  90.  
  91. return $widget_opt;
  92. }
  93.  
  94. }
  95.  
  96.  
  97. new Avada_Widget_Style();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement