Guest User

Untitled

a guest
Apr 20th, 2014
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /* ----------------------------------------------------------------------------
  5. background support
  6. */
  7.  
  8.  
  9. add_theme_support('custom-background', array(
  10. 'default-color' => '',
  11. 'default-image' => ''
  12. ));
  13.  
  14.  
  15.  
  16. class td_background {
  17.  
  18. var $is_boxed_layout = false; //if it's a the box version of the theme (we have bg color or a bg)
  19. var $is_stretched_bg = false; //if it's a stretched background
  20.  
  21.  
  22. var $theme_bg_color = ''; //the background color of the theme
  23.  
  24. var $category_background = ''; //use this bg from category and posts if not empty
  25. var $category_bg_style = '';
  26.  
  27.  
  28.  
  29.  
  30. function __construct() {
  31. add_action('wp_head', array($this, 'wp_head_hook'), 10);
  32.  
  33. //clean up the wp custom-background class if needed
  34. add_filter('body_class', array($this,'add_slug_to_body_class'));
  35. }
  36.  
  37.  
  38.  
  39.  
  40. //we have to hook our filters to wp_head action - for them to work in theme customizer
  41. function wp_head_hook() {
  42. if ((get_background_color() != '' and get_background_color() != 'ffffff' and get_background_color() != '#ffffff') or get_background_image() != '') {
  43. $this->is_boxed_layout = true;
  44. }
  45.  
  46. if (td_util::get_customizer_option('stretch_background') == 'yes') {
  47. $this->is_stretched_bg = false;
  48. }
  49.  
  50. $this->theme_bg_color = get_background_color();
  51.  
  52.  
  53.  
  54. $use_settings_from_category_id = ''; //if not empty, the theme will load the settings from this category id
  55.  
  56. //read the category background
  57. if (is_category()) {
  58. $use_settings_from_category_id = get_query_var('cat');
  59. }
  60.  
  61. if (is_single()) {
  62. $use_settings_from_category_id = td_global::get_primary_category_id();
  63. }
  64.  
  65.  
  66.  
  67. //echo $use_settings_from_category_id;
  68. if ($use_settings_from_category_id != '') {
  69. //get the bg
  70. $tdc_image = get_tax_meta($use_settings_from_category_id, 'tdc_image');
  71. if (!empty($tdc_image['src'])) {
  72. $this->is_boxed_layout = true;
  73. $this->category_background = $tdc_image['src'];
  74. }
  75.  
  76. //get the bg color
  77. $tdc_bg_color = get_tax_meta($use_settings_from_category_id, 'tdc_bg_color');
  78. if (!empty($tdc_bg_color) and $tdc_bg_color != '#') {
  79. $this->theme_bg_color = str_replace('#', '', $tdc_bg_color);
  80. }
  81.  
  82. //get the bg style
  83. $tdc_bg_repeat = get_tax_meta($use_settings_from_category_id, 'tdc_bg_repeat');
  84. if ($tdc_bg_repeat == '') {
  85. $this->is_stretched_bg = false;
  86. } else {
  87. $this->is_stretched_bg = false;
  88. }
  89. }
  90.  
  91.  
  92.  
  93. if ($this->is_stretched_bg == true) {
  94. //add the js filter for our custom bg
  95. add_filter( 'td_js_buffer_render', array($this, 'add_js_hook'));
  96.  
  97. //add custom color css
  98. add_filter( 'td_css_buffer_render', array($this, 'add_css_color_hook'));
  99. } else {
  100. //add custom bg function css - this is added from the category
  101. add_filter( 'td_css_buffer_render', array($this, 'add_css_custom_background'));
  102. }
  103.  
  104.  
  105. }
  106.  
  107. //custom background js
  108. function add_js_hook($js) {
  109.  
  110. if (!empty($this->category_background)) {
  111. $td_background = $this->category_background;
  112. } else {
  113. $td_background = get_background_image();
  114. }
  115.  
  116.  
  117.  
  118. if (!empty($td_background) and $this->is_stretched_bg == true) {
  119. $js .= "\n" . "
  120. //custom backstretch background
  121. jQuery().ready(function() {
  122. jQuery.backstretch('$td_background', {fade:1200});
  123. });
  124. ";
  125. }
  126.  
  127. return $js;
  128. }
  129.  
  130.  
  131. //custom background css hook - for color used by the stretched background
  132. function add_css_color_hook($css) {
  133. if (!empty($this->theme_bg_color)) {
  134. $css.= "\n" . '
  135. body {
  136. background-color:#' . $this->theme_bg_color . ';
  137. }
  138. ';
  139. }
  140. return $css;
  141. }
  142.  
  143. //called with a category custom bg (emulates the wp function)
  144. function add_css_custom_background($css) {
  145. //this is the custom css generated by the theme when a category bg is enabled
  146. if (!empty($this->theme_bg_color)) {
  147. $css.= "\n" . '
  148. body {
  149. background-color:#' . $this->theme_bg_color . ';
  150. }
  151. ';
  152. }
  153.  
  154. if (!empty($this->category_background)) {
  155. $css.= "\n" . "
  156. body {
  157. background-image:url('" . $this->category_background . "');
  158. background-repeat:repeat;
  159. }
  160. ";
  161. }
  162. return $css;
  163. }
  164.  
  165.  
  166. function add_slug_to_body_class($classes) {
  167. //custom stretching background
  168. if ($this->is_stretched_bg or $this->category_background != '') {
  169. //remove the default word press class if it's stretched or if it's coming from a category bg
  170. $i = 0;
  171. foreach ($classes as $key => $value) {
  172. if ($value == 'custom-background') {
  173. unset($classes[$i]);
  174. }
  175. $i++;
  176. }
  177. }
  178.  
  179. if ($this->is_boxed_layout) {
  180. $classes[] = 'td-boxed-layout';
  181. } else {
  182. $classes[] = 'td-full-layout';
  183. }
  184.  
  185. return $classes;
  186. }
  187.  
  188.  
  189. }
  190.  
  191. new td_background();
Advertisement
Add Comment
Please, Sign In to add comment