widgets.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Custom widgets options.
  4. *
  5. * @package Pen
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. if ( ! function_exists( 'pen_widgets_color_scheme_option' ) ) {
  11. /**
  12. * Color scheme option for widgets.
  13. *
  14. * @global wp_registered_widgets $wp_registered_widgets
  15. * @global wp_registered_sidebars $wp_registered_sidebars
  16. *
  17. * @param WP_Widget $widget An instance of WP_Widget.
  18. * @param null $return Whether a field was added.
  19. * @param array $instance Widget settings.
  20. *
  21. * @since Pen 1.0.0
  22. * @return void
  23. */
  24. function pen_widgets_color_scheme_option( $widget, $return, $instance ) {
  25. $color_widget = isset( $instance['pen_theme_color_widget'] ) ? $instance['pen_theme_color_widget'] : '';
  26. $animation_widget = isset( $instance['pen_theme_animation_widget'] ) ? $instance['pen_theme_animation_widget'] : '';
  27. ?>
  28. <fieldset style="border:1px dashed #ccc;margin:0 0 1em;padding:0 1em">
  29. <legend style="font-weight:bold" title="<?php esc_attr_e( 'This is a part of the "Pen" theme so if you switch to another theme these settings will be no longer used. The rest of the settings that are here are either parts of the WordPress core or added via plugins and they are available with or without this theme.', 'pen' ); ?>">
  30. <?php
  31. esc_html_e( 'Pen', 'pen' );
  32. ?>
  33. </legend>
  34. <?php
  35. $field_id = $widget->get_field_id( 'pen_theme_animation_widget' );
  36. $field_name = $widget->get_field_name( 'pen_theme_animation_widget' );
  37. ?>
  38. <p>
  39. <label for="<?php echo esc_attr( $field_id ); ?>">
  40. <?php
  41. esc_html_e( 'Widget Animation', 'pen' );
  42. ?>
  43. </label>
  44. <select id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>">
  45. <?php
  46. $options = pen_animations();
  47. foreach ( $options as $key => $label ) {
  48. ?>
  49. <option value="<?php echo $key; /* phpcs:ignore */ ?>" <?php selected( $key, $animation_widget ); ?>>
  50. <?php
  51. echo esc_html( $label );
  52. ?>
  53. </option>
  54. <?php
  55. }
  56. ?>
  57. </select>
  58. </p>
  59. <?php
  60. $field_id = $widget->get_field_id( 'pen_theme_color_widget' );
  61. $field_name = $widget->get_field_name( 'pen_theme_color_widget' );
  62. ?>
  63. <p>
  64. <label for="<?php echo esc_attr( $field_id ); ?>">
  65. <?php
  66. esc_html_e( 'Widget Color Scheme:', 'pen' );
  67. ?>
  68. </label>
  69. <select id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>">
  70. <option value="transparent" <?php selected( 'transparent', $color_widget ); ?>>
  71. <?php
  72. esc_html_e( 'None', 'pen' );
  73. ?>
  74. </option>
  75. <?php
  76. $options = array(
  77. 'blue' => __( 'Blue', 'pen' ),
  78. 'dark' => __( 'Dark', 'pen' ),
  79. 'light' => __( 'Light', 'pen' ),
  80. 'orange' => __( 'Orange', 'pen' ),
  81. 'purple' => __( 'Purple', 'pen' ),
  82. 'red' => __( 'Red', 'pen' ),
  83. 'yellow' => __( 'Yellow', 'pen' ),
  84. );
  85. foreach ( $options as $key => $label ) {
  86. ?>
  87. <option value="<?php echo $key; /* phpcs:ignore */ ?>" <?php selected( $key, $color_widget ); ?>>
  88. <?php
  89. echo esc_html( $label );
  90. ?>
  91. </option>
  92. <?php
  93. }
  94. ?>
  95. </select>
  96. </p>
  97. </fieldset>
  98. <?php
  99. }
  100. add_filter( 'in_widget_form', 'pen_widgets_color_scheme_option', 10, 3 );
  101. }
  102. if ( ! function_exists( 'pen_widgets_save_color_scheme_option' ) ) {
  103. /**
  104. * Saves the widget color schemes.
  105. *
  106. * @param array $instance Widget settings.
  107. * @param array $new_instance The new widget settings.
  108. *
  109. * @since Pen 1.0.0
  110. * @return array
  111. */
  112. function pen_widgets_save_color_scheme_option( $instance, $new_instance ) {
  113. return $new_instance;
  114. }
  115. add_filter( 'widget_update_callback', 'pen_widgets_save_color_scheme_option', 10, 2 );
  116. }
  117. if ( ! function_exists( 'pen_widgets_custom_classes' ) ) {
  118. /**
  119. * Custom class names for widgets.
  120. *
  121. * @global wp_registered_widgets $wp_registered_widgets
  122. *
  123. * @param array $params Widget parameters.
  124. *
  125. * @since Pen 1.0.0
  126. * @return array
  127. */
  128. function pen_widgets_custom_classes( $params ) {
  129. global $wp_registered_widgets;
  130. $widget_id = $params[0]['widget_id'];
  131. $instance_id = $params[1]['number'];
  132. if ( ! is_object( $wp_registered_widgets[ $widget_id ]['callback'][0] ) ) {
  133. return;
  134. }
  135. $settings = $wp_registered_widgets[ $widget_id ]['callback'][0]->get_settings();
  136. $color_widget = 'transparent';
  137. $animation_widget = 'fadeIn';
  138. $class = 'class="';
  139. if ( isset( $settings[ $instance_id ]['title'] ) && '' !== trim( $settings[ $instance_id ]['title'] ) ) {
  140. $class .= 'pen_widget_has_title ';
  141. }
  142. if ( ! empty( $settings[ $instance_id ]['pen_theme_color_widget'] ) ) {
  143. $color_widget = $settings[ $instance_id ]['pen_theme_color_widget'];
  144. }
  145. $class .= 'pen_widget_' . $color_widget . ' ';
  146. if ( 'transparent' !== $color_widget ) {
  147. $class .= 'pen_widget_not_transparent ';
  148. }
  149. if ( ! empty( $settings[ $instance_id ]['pen_theme_animation_widget'] ) ) {
  150. $animation_widget = $settings[ $instance_id ]['pen_theme_animation_widget'];
  151. }
  152. if ( $animation_widget && 'none' !== $animation_widget ) {
  153. $class .= 'pen_animate_on_scroll pen_custom_animation_' . $animation_widget . ' ';
  154. }
  155. $params[0]['before_widget'] = str_ireplace( 'class="', $class, $params[0]['before_widget'] );
  156. return $params;
  157. }
  158. add_filter( 'dynamic_sidebar_params', 'pen_widgets_custom_classes' );
  159. }