PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/storefront/inc/storefront-functions.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 226 lines | 101 code | 36 blank | 89 comment | 17 complexity | a7ecd631a73e3683d716d1196bfdca1f MD5 | raw file
  1. <?php
  2. /**
  3. * Storefront functions.
  4. *
  5. * @package storefront
  6. */
  7. if ( ! function_exists( 'storefront_is_woocommerce_activated' ) ) {
  8. /**
  9. * Query WooCommerce activation
  10. */
  11. function storefront_is_woocommerce_activated() {
  12. return class_exists( 'WooCommerce' ) ? true : false;
  13. }
  14. }
  15. /**
  16. * Call a shortcode function by tag name.
  17. *
  18. * @since 1.4.6
  19. *
  20. * @param string $tag The shortcode whose function to call.
  21. * @param array $atts The attributes to pass to the shortcode function. Optional.
  22. * @param array $content The shortcode's content. Default is null (none).
  23. *
  24. * @return string|bool False on failure, the result of the shortcode on success.
  25. */
  26. function storefront_do_shortcode( $tag, array $atts = array(), $content = null ) {
  27. global $shortcode_tags;
  28. if ( ! isset( $shortcode_tags[ $tag ] ) ) {
  29. return false;
  30. }
  31. return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
  32. }
  33. /**
  34. * Get the content background color
  35. * Accounts for the Storefront Designer and Storefront Powerpack content background option.
  36. *
  37. * @since 1.6.0
  38. * @return string the background color
  39. */
  40. function storefront_get_content_background_color() {
  41. if ( class_exists( 'Storefront_Designer' ) ) {
  42. $content_bg_color = get_theme_mod( 'sd_content_background_color' );
  43. $content_frame = get_theme_mod( 'sd_fixed_width' );
  44. }
  45. if ( class_exists( 'Storefront_Powerpack' ) ) {
  46. $content_bg_color = get_theme_mod( 'sp_content_frame_background' );
  47. $content_frame = get_theme_mod( 'sp_content_frame' );
  48. }
  49. $bg_color = str_replace( '#', '', get_theme_mod( 'background_color' ) );
  50. if ( class_exists( 'Storefront_Powerpack' ) || class_exists( 'Storefront_Designer' ) ) {
  51. if ( $content_bg_color && ( 'true' === $content_frame || 'frame' === $content_frame ) ) {
  52. $bg_color = str_replace( '#', '', $content_bg_color );
  53. }
  54. }
  55. return '#' . $bg_color;
  56. }
  57. /**
  58. * Apply inline style to the Storefront header.
  59. *
  60. * @uses get_header_image()
  61. * @since 2.0.0
  62. */
  63. function storefront_header_styles() {
  64. $is_header_image = get_header_image();
  65. $header_bg_image = '';
  66. if ( $is_header_image ) {
  67. $header_bg_image = 'url(' . esc_url( $is_header_image ) . ')';
  68. }
  69. $styles = array();
  70. if ( '' !== $header_bg_image ) {
  71. $styles['background-image'] = $header_bg_image;
  72. }
  73. $styles = apply_filters( 'storefront_header_styles', $styles );
  74. foreach ( $styles as $style => $value ) {
  75. echo esc_attr( $style . ': ' . $value . '; ' );
  76. }
  77. }
  78. /**
  79. * Apply inline style to the Storefront homepage content.
  80. *
  81. * @uses get_the_post_thumbnail_url()
  82. * @since 2.2.0
  83. */
  84. function storefront_homepage_content_styles() {
  85. $featured_image = get_the_post_thumbnail_url( get_the_ID() );
  86. $background_image = '';
  87. if ( $featured_image ) {
  88. $background_image = 'url(' . esc_url( $featured_image ) . ')';
  89. }
  90. $styles = array();
  91. if ( '' !== $background_image ) {
  92. $styles['background-image'] = $background_image;
  93. }
  94. $styles = apply_filters( 'storefront_homepage_content_styles', $styles );
  95. foreach ( $styles as $style => $value ) {
  96. echo esc_attr( $style . ': ' . $value . '; ' );
  97. }
  98. }
  99. /**
  100. * Given an hex colors, returns an array with the colors components.
  101. *
  102. * @param strong $hex Hex color e.g. #111111.
  103. * @return bool Array with color components (r, g, b).
  104. * @since 2.5.8
  105. */
  106. function get_rgb_values_from_hex( $hex ) {
  107. // Format the hex color string.
  108. $hex = str_replace( '#', '', $hex );
  109. if ( 3 === strlen( $hex ) ) {
  110. $hex = str_repeat( substr( $hex, 0, 1 ), 2 ) . str_repeat( substr( $hex, 1, 1 ), 2 ) . str_repeat( substr( $hex, 2, 1 ), 2 );
  111. }
  112. // Get decimal values.
  113. $r = hexdec( substr( $hex, 0, 2 ) );
  114. $g = hexdec( substr( $hex, 2, 2 ) );
  115. $b = hexdec( substr( $hex, 4, 2 ) );
  116. return array(
  117. 'r' => $r,
  118. 'g' => $g,
  119. 'b' => $b,
  120. );
  121. }
  122. /**
  123. * Returns true for light colors and false for dark colors.
  124. *
  125. * @param strong $hex Hex color e.g. #111111.
  126. * @return bool True if the average lightness of the three components of the color is higher or equal than 127.5.
  127. * @since 2.5.8
  128. */
  129. function is_color_light( $hex ) {
  130. $rgb_values = get_rgb_values_from_hex( $hex );
  131. $average_lightness = ( $rgb_values['r'] + $rgb_values['g'] + $rgb_values['b'] ) / 3;
  132. return $average_lightness >= 127.5;
  133. }
  134. /**
  135. * Adjust a hex color brightness
  136. * Allows us to create hover styles for custom link colors
  137. *
  138. * @since 2.5.8 Added $opacity argument.
  139. *
  140. * @param strong $hex Hex color e.g. #111111.
  141. * @param integer $steps Factor by which to brighten/darken ranging from -255 (darken) to 255 (brighten).
  142. * @param float $opacity Opacity factor between 0 and 1.
  143. * @return string Brightened/darkened color (hex by default, rgba if opacity is set to a valid value below 1).
  144. * @since 1.0.0
  145. */
  146. function storefront_adjust_color_brightness( $hex, $steps, $opacity = 1 ) {
  147. // Steps should be between -255 and 255. Negative = darker, positive = lighter.
  148. $steps = max( -255, min( 255, $steps ) );
  149. $rgb_values = get_rgb_values_from_hex( $hex );
  150. // Adjust number of steps and keep it inside 0 to 255.
  151. $r = max( 0, min( 255, $rgb_values['r'] + $steps ) );
  152. $g = max( 0, min( 255, $rgb_values['g'] + $steps ) );
  153. $b = max( 0, min( 255, $rgb_values['b'] + $steps ) );
  154. if ( $opacity >= 0 && $opacity < 1 ) {
  155. return 'rgba(' . $r . ',' . $g . ',' . $b . ',' . $opacity . ')';
  156. }
  157. $r_hex = str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT );
  158. $g_hex = str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT );
  159. $b_hex = str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT );
  160. return '#' . $r_hex . $g_hex . $b_hex;
  161. }
  162. /**
  163. * Sanitizes choices (selects / radios)
  164. * Checks that the input matches one of the available choices
  165. *
  166. * @param array $input the available choices.
  167. * @param array $setting the setting object.
  168. * @since 1.3.0
  169. */
  170. function storefront_sanitize_choices( $input, $setting ) {
  171. // Ensure input is a slug.
  172. $input = sanitize_key( $input );
  173. // Get list of choices from the control associated with the setting.
  174. $choices = $setting->manager->get_control( $setting->id )->choices;
  175. // If the input is a valid key, return it; otherwise, return the default.
  176. return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
  177. }
  178. /**
  179. * Checkbox sanitization callback.
  180. *
  181. * Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
  182. * as a boolean value, either TRUE or FALSE.
  183. *
  184. * @param bool $checked Whether the checkbox is checked.
  185. * @return bool Whether the checkbox is checked.
  186. * @since 1.5.0
  187. */
  188. function storefront_sanitize_checkbox( $checked ) {
  189. return ( ( isset( $checked ) && true === $checked ) ? true : false );
  190. }