Sidebar.php
61 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package SureCartAppCore |
| 4 | * @author SureCart <support@surecart.com> |
| 5 | * @copyright SureCart |
| 6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 | * @link https://surecart.com |
| 8 | */ |
| 9 | |
| 10 | namespace SureCartAppCore\Sidebar; |
| 11 | |
| 12 | class Sidebar { |
| 13 | /** |
| 14 | * Check if the current page is part of the blog structure. |
| 15 | * |
| 16 | * @return boolean |
| 17 | */ |
| 18 | protected function isBlog() { |
| 19 | return ( is_home() || is_archive() || is_search() || ( is_single() && get_post_type() === 'post' ) ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Get the post id that should be checked for a custom sidebar for the current request. |
| 24 | * |
| 25 | * @return int |
| 26 | */ |
| 27 | protected function getSidebarPostId() { |
| 28 | $post_id = intval( get_the_ID() ); |
| 29 | |
| 30 | if ( $this->isBlog() ) { |
| 31 | $post_id = intval( get_option( 'page_for_posts' ) ); |
| 32 | } |
| 33 | |
| 34 | $post_id = intval( apply_filters( 'surecart_app_core_sidebar_context_post_id', $post_id ) ); |
| 35 | |
| 36 | return $post_id; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the current sidebar id. |
| 41 | * |
| 42 | * @param string $default Default sidebar to use if a custom one is not specified. |
| 43 | * @param string $meta_key Meta key to check for a custom sidebar id. |
| 44 | * @return string |
| 45 | */ |
| 46 | public function getCurrentSidebarId( $default = 'default-sidebar', $meta_key = '_custom_sidebar' ) { |
| 47 | $post_id = $this->getSidebarPostId(); |
| 48 | $sidebar = $default; |
| 49 | |
| 50 | if ( $post_id ) { |
| 51 | $sidebar = get_post_meta( $post_id, $meta_key, true ); |
| 52 | } |
| 53 | |
| 54 | if ( empty( $sidebar ) ) { |
| 55 | $sidebar = $default; |
| 56 | } |
| 57 | |
| 58 | return $sidebar; |
| 59 | } |
| 60 | } |
| 61 |