Skip to content

Commit 70258b1

Browse files
committed
1 parent 9be6191 commit 70258b1

File tree

7 files changed

+180
-5
lines changed

7 files changed

+180
-5
lines changed

assets/js/customizer-builder.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function( $ ) {// jscs:ignore validateLineBreaks
2+
3+
'use strict';
4+
5+
var api = wp.customize;
6+
7+
api.bind( 'ready', function(){
8+
var currentURL = api.settings.url.preview,
9+
urlBase,
10+
urlParts,
11+
pageSidebarID,
12+
pageSidebarSection;
13+
if ( currentURL !== ShapelyBuilder.siteURL ) {
14+
urlParts = currentURL.split( '/' );
15+
urlParts.pop();
16+
urlBase = urlParts[ urlParts.length - 1 ];
17+
if ( undefined !== ShapelyBuilder.pages[ urlBase ] ) {
18+
pageSidebarID = 'sidebar-widgets-shapely-' + urlBase;
19+
/*
20+
* Defer focus until:
21+
* 1. The section exist.
22+
* 2. The instance is embedded in the document (and so is focusable).
23+
* 3. The preview has finished loading so that the active states have been set.
24+
*/
25+
pageSidebarSection = api.section( pageSidebarID, function( instance ) {
26+
instance.deferred.embedded.done( function() {
27+
api.previewer.deferred.active.done( function() {
28+
instance.focus();
29+
});
30+
});
31+
});
32+
}
33+
}
34+
});
35+
36+
})( jQuery );

functions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ function shapely_scripts() {
333333
$args = array(
334334
'controls' => array( 'slider', 'toggle' ), // array of controls to load
335335
'sections' => array( 'recommended-actions', 'pro' ), // array of sections to load
336+
'backup' => false,
336337
);
337338

338339
new Epsilon_Framework( $args );
@@ -377,3 +378,8 @@ function shapely_scripts() {
377378
*/
378379
require get_template_directory() . '/inc/admin/welcome-screen/class-shapely-notify-system.php';
379380

381+
/**
382+
* Load the shapely page builder class
383+
*/
384+
require get_template_directory() . '/inc/class-shapely-builder.php';
385+
Shapely_Builder::get_instance();

header.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@
5959
</div>
6060
</header><!-- #masthead -->
6161
<div id="content" class="main-container">
62-
<?php if ( ! is_page_template( 'page-templates/template-home.php' ) && ! is_404() ) : ?>
62+
<?php if ( ! is_page_template( 'page-templates/template-home.php' ) && ! is_404() && ! is_page_template( 'page-templates/template-widget.php' ) ) : ?>
6363
<div class="header-callout">
6464
<?php shapely_top_callout(); ?>
6565
</div>
6666
<?php endif; ?>
6767

6868
<section class="content-area <?php echo ( get_theme_mod( 'top_callout', true ) ) ? '' : ' pt0 '; ?>">
69-
<div id="main" class="<?php echo ( ! is_page_template( 'page-templates/template-home.php' ) ) ? 'container' : ''; ?>"
70-
role="main">
69+
<div id="main" class="<?php echo ( ! is_page_template( 'page-templates/template-home.php' ) && ! is_page_template( 'page-templates/template-widget.php' ) ) ? 'container' : ''; ?>" role="main">

inc/admin/welcome-screen/class-shapely-welcome.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function create_action_link( $state, $slug ) {
337337
'plugin_status' => 'all',
338338
'paged' => '1',
339339
'_wpnonce' => wp_create_nonce( 'deactivate-plugin_' . $slug2 ),
340-
), network_admin_url( 'plugins.php' )
340+
), admin_url( 'plugins.php' )
341341
);
342342
break;
343343
case 'activate':
@@ -348,7 +348,7 @@ public function create_action_link( $state, $slug ) {
348348
'plugin_status' => 'all',
349349
'paged' => '1',
350350
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $slug2 ),
351-
), network_admin_url( 'plugins.php' )
351+
), admin_url( 'plugins.php' )
352352
);
353353
break;
354354
}

inc/class-shapely-builder.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
*
5+
*/
6+
class Shapely_Builder {
7+
8+
private static $instance = null;
9+
10+
private $pages = array();
11+
private $sidebars = array();
12+
13+
function __construct() {
14+
15+
$this->get_all_pages();
16+
17+
// Hooks
18+
if ( ! empty( $this->pages ) ) {
19+
20+
add_action( 'widgets_init', array( $this, 'register_sidebars' ) );
21+
add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_builder_js' ) );
22+
add_filter( 'sidebars_widgets', array( $this, 'remove_specific_widget' ) );
23+
24+
}
25+
26+
}
27+
28+
public function get_all_pages() {
29+
30+
$args = array(
31+
'post_type' => 'page',
32+
'posts_per_page' => -1,
33+
'meta_query' => array(
34+
array(
35+
'key' => '_wp_page_template',
36+
'value' => 'page-templates/template-widget.php'
37+
)
38+
)
39+
);
40+
41+
$the_pages = new WP_Query( $args );
42+
43+
if ( $the_pages->have_posts() ) {
44+
foreach ( $the_pages->posts as $post ) {
45+
$this->pages[ $post->post_name ] = array(
46+
'id' => $post->ID,
47+
'title' => $post->post_title,
48+
);
49+
$this->sidebars[] = 'shapely-' . $post->post_name;
50+
}
51+
}
52+
53+
}
54+
55+
public static function get_instance() {
56+
if ( is_null( self::$instance ) ) {
57+
self::$instance = new self();
58+
}
59+
return self::$instance;
60+
}
61+
62+
public function register_sidebars() {
63+
64+
foreach ( $this->pages as $slug => $page ) {
65+
register_sidebar( array(
66+
'name' => sprintf( esc_html__( 'Page: %s', 'shapely' ), $page['title'] ),
67+
'id' => 'shapely-' . $slug,
68+
'description' => sprintf( esc_html__( 'This widgets will appear in %s page', 'shapely' ), $page['title'] ),
69+
'before_widget' => '<div id="%1$s" class="widget %2$s">',
70+
'after_widget' => '</div>',
71+
'before_title' => '<h2 class="widget-title">',
72+
'after_title' => '</h2>',
73+
) );
74+
}
75+
76+
}
77+
78+
public function enqueue_builder_js() {
79+
$builder_settings = array(
80+
'siteURL' => site_url(),
81+
'pages' => $this->pages,
82+
);
83+
wp_enqueue_script( 'shapely_builder_customizer', get_template_directory_uri() . '/assets/js/customizer-builder.js', array(), '20140317', true );
84+
85+
wp_localize_script( 'shapely_builder_customizer', 'ShapelyBuilder', $builder_settings );
86+
}
87+
88+
public function remove_specific_widget( $sidebars_widgets ) {
89+
90+
foreach ( $sidebars_widgets as $widget_area => $widget_list ) {
91+
if ( ! in_array( $widget_area, $this->sidebars ) && 'sidebar-home' != $widget_area ) {
92+
foreach ( $widget_list as $pos => $widget_id ) {
93+
if ( strpos( $widget_id, 'shapely-page-content' ) !== false || strpos( $widget_id, 'shapely-page-title' ) !== false ) {
94+
unset( $sidebars_widgets[ $widget_area ][ $pos ] );;
95+
}
96+
}
97+
}
98+
}
99+
100+
return $sidebars_widgets;
101+
102+
}
103+
104+
}

page-templates/template-widget.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
*
4+
* Template Name: Builder Page
5+
*
6+
*/
7+
?>
8+
<?php
9+
10+
get_header();
11+
12+
while ( have_posts() ) {
13+
the_post();
14+
global $post;
15+
$sidebar_id = 'shapely-' . $post->post_name;
16+
17+
if ( ! function_exists( 'dynamic_sidebar' ) || ! dynamic_sidebar( $sidebar_id ) ) {
18+
echo '<div class="container p24 wp-caption-text">';
19+
echo '<h5>' . sprintf( esc_html__( 'This is the %s sidebar, add some widgets to it to change it.', 'shapely' ), get_the_title() ) . '</h5>';
20+
echo '</div>';
21+
}
22+
23+
}
24+
25+
get_footer();
26+
27+
?>

style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,4 +4195,7 @@ footer.bg-dark a {
41954195
}
41964196
span.wpcf7-not-valid-tip {
41974197
margin-top: -24px;
4198+
}
4199+
.page-template-template-widget section.content-area {
4200+
padding-top: 0;
41984201
}

0 commit comments

Comments
 (0)