/wp-content/themes/Avada/includes/class-avada-layout.php

https://gitlab.com/webkod3r/tripolis · PHP · 511 lines · 324 code · 65 blank · 122 comment · 101 complexity · 09713af1570e8e3c326c69820e48f874 MD5 · raw file

  1. <?php
  2. class Avada_Layout {
  3. public $sidebars = array();
  4. /**
  5. * The class constructor
  6. */
  7. public function __construct() {
  8. add_action( 'wp', array( $this, 'add_sidebar' ), 20 );
  9. // add_action( 'wp', array( $this, 'get_content_width' ), 20 ); WIP ITEM FOR #746
  10. }
  11. /**
  12. * Add sidebar(s) to the pages
  13. *
  14. * @return void
  15. */
  16. public function add_sidebar() {
  17. // Get the sidebars and assign to public variable
  18. $this->sidebars = $this->get_sidebar_settings( $this->sidebar_options() );
  19. // Set styling to content and sidebar divs
  20. $this->layout_structure_styling( $this->sidebars );
  21. // Append sidebar to after content div
  22. if ( Avada()->template->has_sidebar() && ! Avada()->template->double_sidebars() ) {
  23. add_action( 'fusion_after_content', array( $this, 'append_sidebar_single' ) );
  24. } elseif ( Avada()->template->double_sidebars() ) {
  25. add_action( 'fusion_after_content', array( $this, 'append_sidebar_double' ) );
  26. } elseif ( ! Avada()->template->has_sidebar() && ( is_page_template( 'side-navigation.php') || is_singular( 'tribe_events' ) ) ) {
  27. add_action( 'fusion_after_content', array( $this, 'append_sidebar_single' ) );
  28. }
  29. }
  30. /**
  31. * Get sidebar settings based on the page type
  32. *
  33. * @return array
  34. */
  35. public function sidebar_options() {
  36. if ( is_home() ) {
  37. $sidebars = array(
  38. 'global' => '1',
  39. 'sidebar_1' => Avada()->settings->get( 'blog_archive_sidebar' ),
  40. 'sidebar_2' => Avada()->settings->get( 'blog_archive_sidebar_2' ),
  41. 'position' => Avada()->settings->get( 'blog_sidebar_position' ),
  42. );
  43. } elseif ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
  44. $sidebars = array(
  45. 'global' => Avada()->settings->get( 'bbpress_global_sidebar' ),
  46. 'sidebar_1' => Avada()->settings->get( 'ppbress_sidebar' ),
  47. 'sidebar_2' => Avada()->settings->get( 'ppbress_sidebar_2' ),
  48. 'position' => Avada()->settings->get( 'bbpress_sidebar_position' ),
  49. );
  50. if ( bbp_is_forum_archive() || bbp_is_topic_archive() || bbp_is_user_home() || bbp_is_search() ) {
  51. $sidebars = array(
  52. 'global' => '1',
  53. 'sidebar_1' => Avada()->settings->get( 'ppbress_sidebar' ),
  54. 'sidebar_2' => Avada()->settings->get( 'ppbress_sidebar_2' ),
  55. 'position' => Avada()->settings->get( 'bbpress_sidebar_position' ),
  56. );
  57. }
  58. } elseif ( function_exists( 'is_buddypress' ) && is_buddypress() ) {
  59. $sidebars = array(
  60. 'global' => Avada()->settings->get( 'bbpress_global_sidebar' ),
  61. 'sidebar_1' => Avada()->settings->get( 'ppbress_sidebar' ),
  62. 'sidebar_2' => Avada()->settings->get( 'ppbress_sidebar_2' ),
  63. 'position' => Avada()->settings->get( 'bbpress_sidebar_position' ),
  64. );
  65. } elseif ( class_exists( 'WooCommerce' ) && ( is_product() || is_shop() ) ) {
  66. $sidebars = array(
  67. 'global' => Avada()->settings->get( 'woo_global_sidebar' ),
  68. 'sidebar_1' => Avada()->settings->get( 'woo_sidebar' ),
  69. 'sidebar_2' => Avada()->settings->get( 'woo_sidebar_2' ),
  70. 'position' => Avada()->settings->get( 'woo_sidebar_position' ),
  71. );
  72. } elseif ( class_exists( 'WooCommerce' ) && ( is_product_category() || is_product_tag() || is_tax( 'product_brand' ) ) ) {
  73. $sidebars = array(
  74. 'global' => '1',
  75. 'sidebar_1' => Avada()->settings->get( 'woocommerce_archive_sidebar' ),
  76. 'sidebar_2' => Avada()->settings->get( 'woocommerce_archive_sidebar_2' ),
  77. 'position' => Avada()->settings->get( 'woo_sidebar_position' ),
  78. );
  79. } elseif ( is_page() ) {
  80. $sidebars = array(
  81. 'global' => Avada()->settings->get( 'pages_global_sidebar' ),
  82. 'sidebar_1' => Avada()->settings->get( 'pages_sidebar' ),
  83. 'sidebar_2' => Avada()->settings->get( 'pages_sidebar_2' ),
  84. 'position' => Avada()->settings->get( 'default_sidebar_pos' ),
  85. );
  86. } elseif ( is_single() ) {
  87. $sidebars = array(
  88. 'global' => Avada()->settings->get( 'posts_global_sidebar' ),
  89. 'sidebar_1' => Avada()->settings->get( 'posts_sidebar' ),
  90. 'sidebar_2' => Avada()->settings->get( 'posts_sidebar_2' ),
  91. 'position' => Avada()->settings->get( 'blog_sidebar_position' ),
  92. );
  93. if ( is_singular( 'avada_portfolio' ) ) {
  94. $sidebars = array(
  95. 'global' => Avada()->settings->get( 'portfolio_global_sidebar' ),
  96. 'sidebar_1' => Avada()->settings->get( 'portfolio_sidebar' ),
  97. 'sidebar_2' => Avada()->settings->get( 'portfolio_sidebar_2' ),
  98. 'position' => Avada()->settings->get( 'portfolio_sidebar_position' ),
  99. );
  100. } else if ( is_singular( 'tribe_events' ) || is_singular( 'tribe_organizer' ) || is_singular( 'tribe_venue' ) ) {
  101. $sidebars = array(
  102. 'global' => Avada()->settings->get( 'ec_global_sidebar' ),
  103. 'sidebar_1' => Avada()->settings->get( 'ec_sidebar' ),
  104. 'sidebar_2' => Avada()->settings->get( 'ec_sidebar_2' ),
  105. 'position' => Avada()->settings->get( 'ec_sidebar_pos' ),
  106. );
  107. }
  108. if( is_singular( 'tribe_organizer' ) || is_singular( 'tribe_venue' ) ) {
  109. $sidebars['global'] = 1;
  110. }
  111. } elseif ( is_archive() ) {
  112. $sidebars = array(
  113. 'global' => '1',
  114. 'sidebar_1' => Avada()->settings->get( 'blog_archive_sidebar' ),
  115. 'sidebar_2' => Avada()->settings->get( 'blog_archive_sidebar_2' ),
  116. 'position' => Avada()->settings->get( 'blog_sidebar_position' ),
  117. );
  118. if ( is_post_type_archive( 'avada_portfolio' ) || is_tax( 'portfolio_category' ) || is_tax( 'portfolio_skills' ) || is_tax( 'portfolio_tags' ) ) {
  119. $sidebars = array(
  120. 'global' => '1',
  121. 'sidebar_1' => Avada()->settings->get( 'portfolio_archive_sidebar' ),
  122. 'sidebar_2' => Avada()->settings->get( 'portfolio_archive_sidebar_2' ),
  123. 'position' => Avada()->settings->get( 'portfolio_sidebar_position' ),
  124. );
  125. }
  126. } elseif ( is_search() ) {
  127. $sidebars = array(
  128. 'global' => '1',
  129. 'sidebar_1' => Avada()->settings->get( 'search_sidebar' ),
  130. 'sidebar_2' => Avada()->settings->get( 'search_sidebar_2' ),
  131. 'position' => Avada()->settings->get( 'search_sidebar_position' ),
  132. );
  133. } else {
  134. $sidebars = array(
  135. 'global' => Avada()->settings->get( 'pages_global_sidebar' ),
  136. 'sidebar_1' => Avada()->settings->get( 'pages_sidebar' ),
  137. 'sidebar_2' => Avada()->settings->get( 'pages_sidebar_2' ),
  138. 'position' => Avada()->settings->get( 'default_sidebar_pos' ),
  139. );
  140. }
  141. if ( class_exists( 'Tribe__Events__Main' ) && is_events_archive() ) {
  142. $sidebars = array(
  143. 'global' => '1',
  144. 'sidebar_1' => Avada()->settings->get( 'ec_sidebar' ),
  145. 'sidebar_2' => Avada()->settings->get( 'ec_sidebar_2' ),
  146. 'position' => Avada()->settings->get( 'ec_sidebar_pos' ),
  147. );
  148. }
  149. // Remove sidebars from the certain woocommerce pages
  150. if ( class_exists( 'WooCommerce' ) ) {
  151. if ( is_cart() || is_checkout() || is_account_page() || ( get_option( 'woocommerce_thanks_page_id' ) && is_page( get_option( 'woocommerce_thanks_page_id' ) ) ) ) {
  152. $sidebars = array();
  153. }
  154. }
  155. return $sidebars;
  156. }
  157. /**
  158. * Get the sidebars
  159. *
  160. * @param array $sidebar_options
  161. *
  162. * @return array
  163. */
  164. public function get_sidebar_settings( $sidebar_options = array() ) {
  165. // Post options
  166. $sidebar_1 = get_post_meta( Avada::c_pageID(), 'sbg_selected_sidebar_replacement', true );
  167. $sidebar_2 = get_post_meta( Avada::c_pageID(), 'sbg_selected_sidebar_2_replacement', true );
  168. $sidebar_position_post_option = strtolower( get_post_meta( Avada::c_pageID(), 'pyre_sidebar_position', true ) );
  169. $sidebar_position_metadata = metadata_exists( 'post', Avada::c_pageID(), 'pyre_sidebar_position' );
  170. if ( is_array( $sidebar_1 ) && $sidebar_1[0] === '0' ) {
  171. $sidebar_1 = array( 'Blog Sidebar' );
  172. }
  173. if ( is_array( $sidebar_2 ) && $sidebar_2[0] === '0' ) {
  174. $sidebar_2 = array( 'Blog Sidebar' );
  175. }
  176. // Theme options
  177. $sidebar_position_theme_option = array_key_exists( 'position', $sidebar_options ) ? strtolower( $sidebar_options['position'] ) : '';
  178. // Set default sidebar position
  179. $sidebar_position = $sidebar_position_post_option;
  180. // Get sidebars and position from theme options if it's being forced globally
  181. if ( array_key_exists( 'global', $sidebar_options ) && $sidebar_options['global'] ) {
  182. $sidebar_1 = array( ( 'None' != $sidebar_options['sidebar_1'] ) ? $sidebar_options['sidebar_1'] : '' );
  183. $sidebar_2 = array( ( 'None' != $sidebar_options['sidebar_2'] ) ? $sidebar_options['sidebar_2'] : '' );
  184. $sidebar_position = $sidebar_position_theme_option;
  185. }
  186. // If sidebar position is default OR no entry in database exists
  187. if ( 'default' == $sidebar_position || ! $sidebar_position_metadata ) {
  188. $sidebar_position = $sidebar_position_theme_option;
  189. }
  190. // Reverse sidebar position if double sidebars are used and position is right
  191. if ( Avada()->template->double_sidebars() && 'right' == $sidebar_position ) {
  192. $sidebar_1_placeholder = $sidebar_1;
  193. $sidebar_2_placeholder = $sidebar_2;
  194. // Reverse the sidebars
  195. $sidebar_1 = $sidebar_2_placeholder;
  196. $sidebar_2 = $sidebar_1_placeholder;
  197. }
  198. $return = array(
  199. 'position' => $sidebar_position
  200. );
  201. if ( $sidebar_1 ) {
  202. $return['sidebar_1'] = $sidebar_1[0];
  203. }
  204. if( $sidebar_2 ) {
  205. $return['sidebar_2'] = $sidebar_2[0];
  206. }
  207. return $return;
  208. }
  209. /**
  210. * Apply inline styling and classes to the structure
  211. *
  212. * @param array $sidebars
  213. *
  214. * @return void
  215. */
  216. public function layout_structure_styling( $sidebars ) {
  217. // Add sidebar class
  218. add_filter( 'fusion_sidebar_1_class', array( $this, 'sidebar_class' ) );
  219. add_filter( 'fusion_sidebar_2_class', array( $this, 'sidebar_class' ) );
  220. // Check for sidebar location and apply styling to the content or sidebar div
  221. if ( ! Avada()->template->has_sidebar() && ! ( is_page_template( 'side-navigation.php') || is_singular( 'tribe_events' ) ) ) {
  222. add_filter( 'fusion_content_style', array( $this, 'full_width_content_style' ) );
  223. if ( is_archive() || is_home() ) {
  224. add_filter( 'fusion_content_class', array( $this, 'full_width_content_class' ) );
  225. }
  226. } elseif ( 'left' == $sidebars['position'] ) {
  227. add_filter( 'fusion_content_style', array( $this, 'float_right_style' ) );
  228. add_filter( 'fusion_sidebar_1_style', array( $this, 'float_left_style' ) );
  229. add_filter( 'fusion_sidebar_1_class', array( $this, 'side_nav_left_class' ) );
  230. } elseif ( 'right' == $sidebars['position'] ) {
  231. add_filter( 'fusion_content_style', array( $this, 'float_left_style' ) );
  232. add_filter( 'fusion_sidebar_1_style', array( $this, 'float_right_style' ) );
  233. add_filter( 'fusion_sidebar_1_class', array( $this, 'side_nav_right_class' ) );
  234. }
  235. // Page has a single sidebar
  236. if ( Avada()->template->has_sidebar() && ! Avada()->template->double_sidebars() ) {
  237. } elseif ( Avada()->template->double_sidebars() ) { // Page has double sidebars
  238. add_filter( 'fusion_content_style', array( $this, 'float_left_style' ) );
  239. add_filter( 'fusion_sidebar_1_style', array( $this, 'float_left_style' ) );
  240. add_filter( 'fusion_sidebar_2_style', array( $this, 'float_left_style' ) );
  241. if( $sidebars['position'] == 'right' ) {
  242. add_filter( 'fusion_sidebar_2_class', array( $this, 'side_nav_right_class' ) );
  243. }
  244. }
  245. }
  246. /**
  247. * Append single sidebar to a page
  248. *
  249. * @return void
  250. */
  251. public function append_sidebar_single() {
  252. get_template_part( 'templates/sidebar', '1' );
  253. }
  254. /**
  255. * Append double sidebar to a page
  256. *
  257. * @return void
  258. */
  259. public function append_sidebar_double() {
  260. get_template_part( 'templates/sidebar', '1' );
  261. get_template_part( 'templates/sidebar', '2' );
  262. }
  263. /**
  264. * Join the elements
  265. *
  266. * @param string $filter_id
  267. * @param string $sanitize
  268. * @param string $join_separator
  269. *
  270. * @return string
  271. */
  272. public function join( $filter_id = null, $sanitize = 'esc_attr', $join_separator = ' ' ) {
  273. // Get the elements using a filter
  274. $elements = apply_filters( 'fusion_' . $filter_id, array() );
  275. // Make sure each element is properly sanitized
  276. $elements = array_map( $sanitize, $elements );
  277. // Make sure there are no duplicate items
  278. $elements = array_unique( $elements );
  279. // Combine the elements of the array and return the combined string
  280. return join( $join_separator, $elements );
  281. }
  282. /**
  283. * Filter to add inline styling
  284. *
  285. * @return void
  286. */
  287. public function add_style( $filter ) {
  288. echo 'style="' . $this->join( $filter ) . '"';
  289. }
  290. /**
  291. * Filter to add class
  292. *
  293. * @return void
  294. */
  295. public function add_class( $filter ) {
  296. echo 'class="' . $this->join( $filter ) . '"';
  297. }
  298. /**
  299. * Full width page inline styling
  300. *
  301. * @return array
  302. */
  303. public function full_width_content_style( $styles ) {
  304. $styles[] = 'width: 100%;';
  305. return $styles;
  306. }
  307. /**
  308. * Full width class
  309. *
  310. * @return array
  311. */
  312. public function full_width_content_class( $classes ) {
  313. $classes[] = 'full-width';
  314. return $classes;
  315. }
  316. /**
  317. * Float right styling
  318. *
  319. * @return array
  320. */
  321. public function float_right_style( $styles ) {
  322. $styles[] = 'float: right;';
  323. return $styles;
  324. }
  325. /**
  326. * Float left styling
  327. *
  328. * @return array
  329. */
  330. public function float_left_style( $styles ) {
  331. $styles[] = 'float: left;';
  332. return $styles;
  333. }
  334. /**
  335. * Add sidebar class to the sidebars
  336. *
  337. * @return array
  338. */
  339. public function sidebar_class( $classes ) {
  340. $classes[] = 'sidebar fusion-widget-area fusion-content-widget-area';
  341. return $classes;
  342. }
  343. /**
  344. * Add side nav right class when sidebar position is right
  345. *
  346. * @return array
  347. */
  348. public function side_nav_right_class( $classes ) {
  349. if( is_page_template( 'side-navigation.php' ) ) {
  350. $classes[] = 'side-nav-right';
  351. }
  352. return $classes;
  353. }
  354. /**
  355. * Add side nav left class when sidebar position is left
  356. *
  357. * @return array
  358. */
  359. public function side_nav_left_class( $classes ) {
  360. if( is_page_template( 'side-navigation.php' ) ) {
  361. $classes[] = 'side-nav-left';
  362. }
  363. return $classes;
  364. }
  365. // WIP GIT ITEM # #746
  366. public function get_sidebars_width() {
  367. $sidebars = $this->get_sidebar_settings( $this->sidebar_options() );
  368. if ( false !== strpos( Avada()->settings->get( 'site_width' ), 'px' ) ) {
  369. $margin = '100px';
  370. $half_margin = '50px';
  371. } else {
  372. $margin = '6%';
  373. $half_margin = '3%';
  374. }
  375. if ( Avada()->template->has_sidebar() && ! Avada()->template->double_sidebars() ) {
  376. if ( get_post_type() == 'tribe_events' ) {
  377. $sidebar_width = Avada()->settings->get( 'ec_sidebar_width' );
  378. if ( false !== strpos( $sidebar_width, 'px' ) && false !== strpos( $sidebar_width, '%' ) ) {
  379. $sidebar_width = ( 100 > intval( $sidebar_width ) ) ? intval( $sidebar_width ) . '%' : intval( $sidebar_width ) . 'px';
  380. }
  381. } else {
  382. $sidebar_width = Avada()->settings->get( 'sidebar_width' );
  383. if ( false !== strpos( $sidebar_width, 'px' ) && false !== strpos( $sidebar_width, '%' ) ) {
  384. $sidebar_width = ( 100 > intval( $sidebar_width ) ) ? intval( $sidebar_width ) . '%' : intval( $sidebar_width ) . 'px';
  385. }
  386. }
  387. } elseif ( Avada()->template->double_sidebars() ) {
  388. if ( get_post_type() == 'tribe_events' ) {
  389. $sidebar_2_1_width = Avada()->settings->get( 'ec_sidebar_2_1_width' );
  390. if ( false !== strpos( $sidebar_2_1_width, 'px' ) && false !== strpos( $sidebar_2_1_width, '%' ) ) {
  391. $sidebar_2_1_width = ( 100 > intval( $sidebar_2_1_width ) ) ? intval( $sidebar_2_1_width ) . '%' : intval( $sidebar_2_1_width ) . 'px';
  392. }
  393. $sidebar_2_2_width = Avada()->settings->get( 'ec_sidebar_2_2_width' );
  394. if ( false !== strpos( $sidebar_2_2_width, 'px' ) && false !== strpos( $sidebar_2_2_width, '%' ) ) {
  395. $sidebar_2_2_width = ( 100 > intval( $sidebar_2_2_width ) ) ? intval( $sidebar_2_2_width ) . '%' : intval( $sidebar_2_2_width ) . 'px';
  396. }
  397. } else {
  398. $sidebar_2_1_width = Avada()->settings->get( 'sidebar_2_1_width' );
  399. if ( false !== strpos( $sidebar_2_1_width, 'px' ) && false !== strpos( $sidebar_2_1_width, '%' ) ) {
  400. $sidebar_2_1_width = ( 100 > intval( $sidebar_2_1_width ) ) ? intval( $sidebar_2_1_width ) . '%' : intval( $sidebar_2_1_width ) . 'px';
  401. }
  402. $sidebar_2_2_width = Avada()->settings->get( 'sidebar_2_2_width' );
  403. if ( false !== strpos( $sidebar_2_2_width, 'px' ) && false !== strpos( $sidebar_2_2_width, '%' ) ) {
  404. $sidebar_2_2_width = ( 100 > intval( $sidebar_2_2_width ) ) ? intval( $sidebar_2_2_width ) . '%' : intval( $sidebar_2_2_width ) . 'px';
  405. }
  406. }
  407. } elseif ( ! Avada()->template->has_sidebar() && ( is_page_template( 'side-navigation.php') || is_singular( 'tribe_events' ) ) ) {
  408. if ( get_post_type() == 'tribe_events' ) {
  409. $sidebar_width = Avada()->settings->get( 'ec_sidebar_width' );
  410. if ( false !== strpos( $sidebar_width, 'px' ) && false !== strpos( $sidebar_width, '%' ) ) {
  411. $sidebar_width = ( 100 > intval( $sidebar_width ) ) ? intval( $sidebar_width ) . '%' : intval( $sidebar_width ) . 'px';
  412. }
  413. } else {
  414. $sidebar_width = Avada()->settings->get( 'sidebar_width' );
  415. if ( false !== strpos( $sidebar_width, 'px' ) && false !== strpos( $sidebar_width, '%' ) ) {
  416. $sidebar_width = ( 100 > intval( $sidebar_width ) ) ? intval( $sidebar_width ) . '%' : intval( $sidebar_width ) . 'px';
  417. }
  418. }
  419. }
  420. if ( isset( $sidebar_width ) ) {
  421. } elseif ( isset( $sidebar_2_1_width ) && isset( $sidebar_2_2_width ) ) {
  422. }
  423. }
  424. /**
  425. * Get content width of the current page
  426. *
  427. * @return array
  428. */
  429. // WIP GIT ITEM # #746
  430. public function get_content_width() {
  431. $site_width = (int) Avada()->settings->get( 'site_width' );
  432. // The site width WITH units appended
  433. if ( false === strpos( Avada()->settings->get( 'site_width' ), '%' ) && false === strpos( Avada()->settings->get( 'site_width' ), 'px' ) ) {
  434. $site_width_with_units = Avada_Sanitize::size( Avada()->settings->get( 'site_width' ) . 'px' );
  435. } else {
  436. $site_width_with_units = Avada_Sanitize::size( Avada()->settings->get( 'site_width' ) );
  437. }
  438. // The site width as an integer value (WITHOUT units appended)
  439. $site_width_without_units = (int) Avada_Sanitize::size( Avada()->settings->get( 'site_width' ) );
  440. // Is the site width a percent value?
  441. $site_width_percent = ( false !== strpos( Avada()->settings->get( 'site_width' ), '%' ) ) ? true : false;
  442. $this->get_sidebars_width();
  443. //return $content_width;
  444. }
  445. }
  446. // Omit closing PHP tag to avoid "Headers already sent" issues.