Skip to content

Commit 467e52f

Browse files
authored
Merge pull request #41 from smartcatdev/zack-dev
Single Sidebars Work Checkpoint
2 parents 4c6ef1d + 814062d commit 467e52f

File tree

9 files changed

+311
-7
lines changed

9 files changed

+311
-7
lines changed

assets/css/buildr.css

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,3 +1821,64 @@ div#author-meta-wrap .author-job-location-social .author-social a.social-link:ho
18211821
text-align: center;
18221822
}
18231823
}
1824+
1825+
/* -----------------------------------------------------------------------------
1826+
* Left and Right Sidebars
1827+
* -------------------------------------------------------------------------- */
1828+
1829+
.buildr-landr-sidebar-wrap aside.widget table#wp-calendar th,
1830+
.buildr-landr-sidebar-wrap aside.widget table#wp-calendar td {
1831+
color: #373737;
1832+
}
1833+
1834+
@media (min-width:768px) {
1835+
.buildr-landr-sidebar-wrap {
1836+
margin-top: 100px;
1837+
}
1838+
body.blog .buildr-landr-sidebar-wrap,
1839+
body.archive .buildr-landr-sidebar-wrap,
1840+
body.search .buildr-landr-sidebar-wrap {
1841+
margin-top: 0;
1842+
}
1843+
}
1844+
1845+
.buildr-landr-sidebar-wrap > aside.widget:first-child h2.widget-title {
1846+
margin-top: 0;
1847+
}
1848+
1849+
.buildr-landr-sidebar-wrap h2.widget-title {
1850+
color: #373737;
1851+
border-bottom-color: #eee;
1852+
}
1853+
1854+
@media (max-width:1199px) and (min-width:768px) {
1855+
.buildr-landr-sidebar-wrap ul.product_list_widget .product-wrap {
1856+
display: block;
1857+
}
1858+
.buildr-landr-sidebar-wrap ul.product_list_widget .product-wrap > a img {
1859+
max-width: 100%;
1860+
float: none;
1861+
}
1862+
1863+
.buildr-landr-sidebar-wrap ul.product_list_widget .product-wrap > .product-title,
1864+
.buildr-landr-sidebar-wrap ul.product_list_widget .product-wrap > .quantity {
1865+
font-size: 14px;
1866+
padding: 0;
1867+
margin-top: 15px;
1868+
}
1869+
}
1870+
1871+
@media (max-width:767px) {
1872+
.has-side-sidebar.sidebar-right > article {
1873+
padding-bottom: 50px !important;
1874+
}
1875+
.has-side-sidebar.sidebar-left > article {
1876+
padding-top: 15px !important;
1877+
}
1878+
.buildr-landr-sidebar-wrap.single.right {
1879+
margin-bottom: 100px;
1880+
}
1881+
.buildr-landr-sidebar-wrap.single.left {
1882+
margin-top: 100px;
1883+
}
1884+
}

functions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require get_template_directory() . '/inc/constants.php';
44

5+
require get_template_directory() . '/inc/meta_constants.php';
6+
57
require get_template_directory() . '/inc/functions-custom-header.php';
68

79
require get_template_directory() . '/inc/functions-fonts.php';
@@ -14,6 +16,8 @@
1416

1517
require get_template_directory() . '/inc/functions-helpers.php';
1618

19+
require get_template_directory() . '/inc/functions-metabox.php';
20+
1721
require get_template_directory() . '/inc/functions-template-tags.php';
1822

1923
require get_template_directory() . '/inc/functions-widgets.php';

inc/functions-helpers.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,31 @@ function buildr_dismiss_companion() {
306306

307307
exit();
308308
}
309-
add_action( 'wp_ajax_buildr_dismiss_companion', 'buildr_dismiss_companion' );
309+
add_action( 'wp_ajax_buildr_dismiss_companion', 'buildr_dismiss_companion' );
310+
311+
312+
function buildr_is_single_sidebar_active( $template = 'page' ) {
313+
314+
if ( $template != 'post' && $template != 'page' && $template != 'blog' ) { return false; }
315+
316+
// Which template is this check for?
317+
if ( $template == 'post' || $template == 'page' ) :
318+
319+
// Page & Post Sidebar
320+
321+
if ( get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_TEMPLATE, true ) != 'none' && is_active_sidebar( get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_TEMPLATE, true ) ) ) :
322+
return true;
323+
else :
324+
return false;
325+
endif;
326+
327+
else :
328+
329+
// Blog Sidebar
330+
331+
return is_active_sidebar( 'sidebar-blog-side' );
332+
333+
endif;
334+
335+
}
336+

inc/functions-metabox.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
new Buildr_Left_Right_Sidebar_Meta_Box;
4+
class Buildr_Left_Right_Sidebar_Meta_Box {
5+
6+
public function __construct() {
7+
8+
if ( is_admin() ) {
9+
add_action( 'load-post.php', array ( $this, 'init_metabox' ) );
10+
add_action( 'load-post-new.php', array ( $this, 'init_metabox' ) );
11+
}
12+
13+
}
14+
15+
public function init_metabox() {
16+
17+
add_action( 'add_meta_boxes', array ( $this, 'add_metabox' ) );
18+
add_action( 'save_post', array ( $this, 'save_metabox' ), 10, 2 );
19+
20+
}
21+
22+
public function add_metabox() {
23+
24+
add_meta_box( 'buildr_left_right_sidebar_meta', __( 'Sidebar Options', 'buildr' ), array ( $this, 'render_left_right_sidebar_metabox' ), array( 'post', 'page' ), 'normal', 'high' );
25+
26+
}
27+
28+
public function render_left_right_sidebar_metabox( $post ) {
29+
30+
// Add nonce for security and authentication.
31+
wp_nonce_field( 'left_right_sidebar_meta_box_nonce_action', 'left_right_sidebar_meta_box_nonce' );
32+
33+
// Retrieve an existing value from the database.
34+
$sidebar_template = get_post_meta( $post->ID, BUILDR_META::SIDEBAR_TEMPLATE, true );
35+
$sidebar_location = get_post_meta( $post->ID, BUILDR_META::SIDEBAR_LOCATION, true );
36+
37+
// Set default values.
38+
if ( empty( $sidebar_template ) ) { $sidebar_template = 'none'; }
39+
if ( empty( $sidebar_location ) ) { $sidebar_location = 'sidebar-right'; }
40+
41+
// Form fields.
42+
echo '<table class="form-table">';
43+
44+
echo ' <tr>';
45+
echo ' <th><label for="' . esc_attr( BUILDR_META::SIDEBAR_TEMPLATE ) . '" class="' . esc_attr( BUILDR_META::SIDEBAR_TEMPLATE ) . '_label">' . __( 'Sidebar - Template', 'buildr' ) . '</label></th>';
46+
echo ' <td>';
47+
echo ' <select id="' . esc_attr( BUILDR_META::SIDEBAR_TEMPLATE ) . '" name="' . esc_attr( BUILDR_META::SIDEBAR_TEMPLATE ) . '" class="' . esc_attr( BUILDR_META::SIDEBAR_TEMPLATE ) . '_field">';
48+
echo ' <option value="none" ' . selected( $sidebar_template, 'none', false ) . ' >' . __( 'None', 'buildr' ) . '</option>';
49+
echo ' <option value="sidebar-side-a" ' . selected( $sidebar_template, 'sidebar-side-a', false ) . ' >' . __( 'Sidebar A', 'buildr' ) . '</option>';
50+
echo ' <option value="sidebar-side-b" ' . selected( $sidebar_template, 'sidebar-side-b', false ) . ' >' . __( 'Sidebar B', 'buildr' ) . '</option>';
51+
echo ' <option value="sidebar-side-c" ' . selected( $sidebar_template, 'sidebar-side-c', false ) . ' >' . __( 'Sidebar C', 'buildr' ) . '</option>';
52+
echo ' </select>';
53+
echo ' </td>';
54+
echo ' </tr>';
55+
56+
echo ' <tr>';
57+
echo ' <th><label for="' . esc_attr( BUILDR_META::SIDEBAR_LOCATION ) . '" class="' . esc_attr( BUILDR_META::SIDEBAR_LOCATION ) . '_label">' . __( 'Sidebar - Location', 'buildr' ) . '</label></th>';
58+
echo ' <td>';
59+
echo ' <select id="' . esc_attr( BUILDR_META::SIDEBAR_LOCATION ) . '" name="' . esc_attr( BUILDR_META::SIDEBAR_LOCATION ) . '" class="' . esc_attr( BUILDR_META::SIDEBAR_LOCATION ) . '_field">';
60+
echo ' <option value="sidebar-left" ' . selected( $sidebar_location, 'sidebar-left', false ) . ' >' . __( 'Left', 'buildr' ) . '</option>';
61+
echo ' <option value="sidebar-right" ' . selected( $sidebar_location, 'sidebar-right', false ) . ' >' . __( 'Right', 'buildr' ) . '</option>';
62+
echo ' </select>';
63+
echo ' </td>';
64+
echo ' </tr>';
65+
66+
echo '</table>';
67+
68+
}
69+
70+
public function save_metabox( $post_id, $post ) {
71+
72+
// Add nonce for security and authentication.
73+
$nonce_name = isset( $_POST[ 'left_right_sidebar_meta_box_nonce' ] ) ? $_POST[ 'left_right_sidebar_meta_box_nonce' ] : '';
74+
$nonce_action = 'left_right_sidebar_meta_box_nonce_action';
75+
76+
// Check if a nonce is set and valid
77+
if ( !isset( $nonce_name ) ) { return; }
78+
if ( !wp_verify_nonce( $nonce_name, $nonce_action ) ) { return; }
79+
80+
// Sanitize user input.
81+
$sidebar_template = isset( $_POST[ BUILDR_META::SIDEBAR_TEMPLATE ] ) ? sanitize_text_field( $_POST[ BUILDR_META::SIDEBAR_TEMPLATE ] ) : '';
82+
$sidebar_location = isset( $_POST[ BUILDR_META::SIDEBAR_LOCATION ] ) ? sanitize_text_field( $_POST[ BUILDR_META::SIDEBAR_LOCATION ] ) : '';
83+
84+
// Update the meta field in the database.
85+
update_post_meta( $post_id, BUILDR_META::SIDEBAR_TEMPLATE, $sidebar_template );
86+
update_post_meta( $post_id, BUILDR_META::SIDEBAR_LOCATION, $sidebar_location );
87+
88+
}
89+
90+
}

inc/functions-template-tags.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function buildr_render_masonry_wrap_open() { ?>
178178

179179
<div class="row">
180180

181-
<div class="col-sm-12">
181+
<div class="<?php echo buildr_is_single_sidebar_active( 'blog' ) ? 'col-sm-9 col-md-9 col-lg-9' : 'col-sm-12'; ?>">
182182

183183
<div class="masonry_card_blog">
184184

@@ -203,6 +203,8 @@ function buildr_render_masonry_wrap_close() { ?>
203203
</div>
204204

205205
</div>
206+
207+
<?php buildr_output_side_sidebar( 'blog', 'right' ) ?>
206208

207209
</div>
208210

@@ -227,7 +229,7 @@ function buildr_render_mosaic_wrap_open() { ?>
227229

228230
<div class="row">
229231

230-
<div class="col-sm-12">
232+
<div class="<?php echo buildr_is_single_sidebar_active( 'blog' ) ? 'col-sm-9 col-md-9 col-lg-9' : 'col-sm-12'; ?>">
231233

232234
<div class="mosaic-grid">
233235

@@ -249,6 +251,8 @@ function buildr_render_mosaic_wrap_close() { ?>
249251
</div>
250252

251253
</div>
254+
255+
<?php buildr_output_side_sidebar( 'blog', 'right' ) ?>
252256

253257
</div>
254258

@@ -273,7 +277,7 @@ function buildr_render_standard_wrap_open() { ?>
273277

274278
<div class="row">
275279

276-
<div class="col-sm-12">
280+
<div class="<?php echo buildr_is_single_sidebar_active( 'blog' ) ? 'col-sm-9 col-md-9 col-lg-9' : 'col-sm-12'; ?>">
277281

278282
<div class="standard-blog">
279283

@@ -295,6 +299,8 @@ function buildr_render_standard_wrap_close() { ?>
295299
</div>
296300

297301
</div>
302+
303+
<?php buildr_output_side_sidebar( 'blog', 'right' ) ?>
298304

299305
</div>
300306

@@ -304,3 +310,62 @@ function buildr_render_standard_wrap_close() { ?>
304310

305311
<?php }
306312
add_action( 'buildr_blog_standard_wrap_close', 'buildr_render_standard_wrap_close');
313+
314+
/**
315+
*
316+
* Outputs a side-located sidebar if conditionally appropriate
317+
*
318+
* @since 1.1.0
319+
* @param type $template
320+
* @param type $location
321+
* @return HTML and sidebar content
322+
*/
323+
function buildr_output_side_sidebar( $template = 'single', $location = 'right' ) {
324+
325+
// Return if the template argument is not for Single (Post/Page) or Blog (includes Archive pages)
326+
if ( $template != 'single' && $template != 'blog' ) { return; }
327+
328+
// Which template is this check for?
329+
if ( $template == 'single' ) :
330+
331+
// Page & Post Sidebar
332+
333+
if ( get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_TEMPLATE, true ) != 'none' && get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_LOCATION, true ) == 'sidebar-' . $location ) :
334+
335+
if ( is_active_sidebar( get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_TEMPLATE, true ) ) ) : ?>
336+
337+
<div class="col-sm-3 col-md-3 col-lg-3">
338+
339+
<div class="buildr-landr-sidebar-wrap single <?php echo esc_attr( $location ); ?>">
340+
341+
<?php dynamic_sidebar( get_post_meta( get_the_ID(), BUILDR_META::SIDEBAR_TEMPLATE, true ) ); ?>
342+
343+
</div>
344+
345+
</div>
346+
347+
<?php endif;
348+
349+
endif;
350+
351+
else :
352+
353+
// Blog Sidebar
354+
355+
if ( is_active_sidebar( 'sidebar-blog-side' ) ) : ?>
356+
357+
<div class="col-sm-3 col-md-3 col-lg-3">
358+
359+
<div class="buildr-landr-sidebar-wrap archive <?php echo esc_attr( $location ); ?>">
360+
361+
<?php dynamic_sidebar( 'sidebar-blog-side' ); ?>
362+
363+
</div>
364+
365+
</div>
366+
367+
<?php endif;
368+
369+
endif;
370+
371+
}

inc/functions-widgets.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,45 @@ function buildr_widgets_init() {
150150
'before_title' => '<h2 class="widget-title">',
151151
'after_title' => '</h2>',
152152
));
153+
154+
register_sidebar( array (
155+
'name' => esc_html__( 'Sidebar A', 'buildr' ),
156+
'id' => 'sidebar-side-a',
157+
'description' => esc_html__( 'Add widgets here.', 'buildr' ),
158+
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
159+
'after_widget' => '</aside>',
160+
'before_title' => '<h2 class="widget-title">',
161+
'after_title' => '</h2>',
162+
));
163+
164+
register_sidebar( array (
165+
'name' => esc_html__( 'Sidebar B', 'buildr' ),
166+
'id' => 'sidebar-side-b',
167+
'description' => esc_html__( 'Add widgets here.', 'buildr' ),
168+
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
169+
'after_widget' => '</aside>',
170+
'before_title' => '<h2 class="widget-title">',
171+
'after_title' => '</h2>',
172+
));
173+
174+
register_sidebar( array (
175+
'name' => esc_html__( 'Sidebar C', 'buildr' ),
176+
'id' => 'sidebar-side-c',
177+
'description' => esc_html__( 'Add widgets here.', 'buildr' ),
178+
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
179+
'after_widget' => '</aside>',
180+
'before_title' => '<h2 class="widget-title">',
181+
'after_title' => '</h2>',
182+
));
183+
184+
register_sidebar( array (
185+
'name' => esc_html__( 'Blog & Archive', 'buildr' ),
186+
'id' => 'sidebar-blog-side',
187+
'description' => esc_html__( 'Add widgets here.', 'buildr' ),
188+
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
189+
'after_widget' => '</aside>',
190+
'before_title' => '<h2 class="widget-title">',
191+
'after_title' => '</h2>',
192+
));
153193

154-
}
194+
}

inc/meta_constants.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
interface BUILDR_META {
4+
5+
const SIDEBAR_TEMPLATE = 'buildr_left_right_sidebar_template',
6+
SIDEBAR_LOCATION = 'buildr_left_right_sidebar_location';
7+
8+
}
9+

0 commit comments

Comments
 (0)