Skip to content

Commit

Permalink
Merge branch 'feature/page_builder_class' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzsequence committed Jul 21, 2015
2 parents c7184f2 + 2879b1c commit 303459a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
File renamed without changes.
10 changes: 10 additions & 0 deletions assets/js/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jQuery(document).ready(function($) {

var pageBuilderClass = builder_l10n.builder_class;
var pageBuilderParts = builder_l10n.parts;
var partsCount = Number( builder_l10n.parts_count );

$( 'div.'+pageBuilderClass ).each( function( i ) {
$(this).addClass(pageBuilderParts[i % partsCount]);
});
});
46 changes: 44 additions & 2 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class WDS_Page_Builder {

public $part_slug;

/**
* Construct function to get things started.
*/
Expand All @@ -21,12 +23,24 @@ public function __construct() {
$this->basename = wds_page_builder()->basename;
$this->directory_path = wds_page_builder()->directory_path;
$this->directory_url = wds_page_builder()->directory_url;
$this->part_slug = '';

add_action( 'cmb2_init', array( $this, 'do_meta_boxes' ) );
add_action( 'wds_page_builder_load_parts', array( $this, 'add_template_parts' ), 10, 1 );
add_action( 'wds_page_builder_after_load_parts', array( $this, 'load_scripts' ) );
}


public function load_scripts() {
wp_register_script( 'public', wds_page_builder()->directory_url . '/assets/js/public.js', array( 'jquery' ), '20150720', true );
wp_localize_script( 'public', 'builder_l10n', array(
'builder_class' => 'pagebuilder-part',
'parts' => $this->page_builder_parts(),
'parts_count' => count( $this->page_builder_parts() ),
) );
wp_enqueue_script( 'public' );
}

/**
* Build our meta boxes
*/
Expand Down Expand Up @@ -164,17 +178,45 @@ public function load_template_part( $part = array() ) {
return;
}

$this->set_part( $part['template_group'] );

// bail if the file doesn't exist
if ( ! file_exists( trailingslashit( get_template_directory() ) . trailingslashit( wds_page_builder_template_parts_dir() ) . wds_page_builder_template_part_prefix() . '-' . $part['template_group'] . '.php' ) ) {
if ( ! file_exists( trailingslashit( get_template_directory() ) . trailingslashit( wds_page_builder_template_parts_dir() ) . wds_page_builder_template_part_prefix() . '-' . $this->part_slug . '.php' ) ) {
return;
}

do_action( 'wds_page_builder_before_load_template' );
load_template( get_template_directory() . '/' . wds_page_builder_template_parts_dir() . '/' . wds_page_builder_template_part_prefix() . '-' . $part['template_group'] . '.php' );
load_template( get_template_directory() . '/' . wds_page_builder_template_parts_dir() . '/' . wds_page_builder_template_part_prefix() . '-' . $this->part_slug . '.php' );
do_action( 'wds_page_builder_after_load_template' );

}

public function get_part() {
return $this->part_slug;
}

public function set_part( $part ) {
$this->part_slug = $part;
}

public function page_builder_parts() {
$some_files = array_filter(get_included_files(), array( $this, 'match_parts' ) );
foreach ( $some_files as $file ) {
$the_files[] = stripslashes( str_replace( array(
get_template_directory(),
'.php',
wds_page_builder_template_parts_dir(),
wds_page_builder_template_part_prefix() . '-',
'//'
), '', $file ) );
}
return $the_files;
}

private function match_parts($var) {
return strpos($var, 'part-');
}

}

$_GLOBALS['WDS_Page_Builder'] = new WDS_Page_Builder;
Expand Down
4 changes: 3 additions & 1 deletion inc/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct() {

public function load_scripts( $hook ) {
if ( 'settings_page_wds_page_builder_options' == $hook ) {
wp_enqueue_script( 'page-builder', wds_page_builder()->directory_url . '/assets/js/page-builder.js', array( 'jquery' ), '1.4.1', true );
wp_enqueue_script( 'admin', wds_page_builder()->directory_url . '/assets/js/admin.js', array( 'jquery' ), '20150721', true );
}
}

Expand Down Expand Up @@ -266,6 +266,7 @@ function wds_page_builder_get_option( $key = '' ) {

/**
* Helper function to get the template part prefix
* @return string The template part prefix (without the hyphen)
*/
function wds_page_builder_template_part_prefix() {
$prefix = ( wds_page_builder_get_option( 'parts_prefix' ) ) ? wds_page_builder_get_option( 'parts_prefix' ) : 'part';
Expand All @@ -274,6 +275,7 @@ function wds_page_builder_template_part_prefix() {

/**
* Helper function to return the template parts directory
* @return string The template part directory name
*/
function wds_page_builder_template_parts_dir() {
$directory = ( wds_page_builder_get_option( 'parts_dir' ) ) ? wds_page_builder_get_option( 'parts_dir' ) : 'parts';
Expand Down
59 changes: 59 additions & 0 deletions inc/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,63 @@ function wds_page_builder_load_part( $part = '' ) {

$page_builder = new WDS_Page_Builder;
$page_builder->load_template_part( array( 'template_group' => $part ) );
}

/**
* Display the classes for the template part wrapper
* @since 1.5
* @param string|array $class One or more classes to add to the class list
* @return null
*/
function page_builder_class( $class = '' ) {
// Separates classes with a single space, collates classes for template part wrapper DIV
echo 'class="' . join( ' ', get_page_builder_class( $class ) ) . '"';
}

/**
* Retrieve the class names for the template part as an array
*
* Based on post_class, but we're not getting as much information as post_class.
* We just want to return a generic class, the current template part slug, and any
* custom class names that were passed to the function.
*
* @param string|array $class One or more classes to add to the class list
* @param string $part_slug Optional. The template part slug.
* @return array Array of classes.
*/
function get_page_builder_class( $class = '', $part_slug = '' ) {

if ( $class ) {
if ( ! is_array( $class ) ) {
$class = preg_split( '#\s+#', $class );
}
$classes = array_map( 'esc_attr', $class );
}

$classes[] = 'pagebuilder-part';

/**
* Filter the list of CSS classes for the current part
* @since 1.5
* @param array $classes An array of pagebuilder part classes
* @param string $class A comma-separated list of additional classes added to the post
* @param string $part_slug The template part slug to add the filtered classes to
*/
$classes = apply_filters( 'page_builder_class', $classes, $class, $part_slug );

return array_unique( $classes );

}

/**
* Gets an array of page builder parts.
*
* Note, this function ONLY returns values AFTER the parts have been loaded, so hook into
* wds_page_builder_after_load_parts or later for this to be populated
* @since 1.5
* @return array An array of template parts in use on the page
*/
function get_page_builder_parts() {
$page_builder = new WDS_Page_Builder;
return $page_builder->page_builder_parts();
}

0 comments on commit 303459a

Please sign in to comment.