diff --git a/assets/js/page-builder.js b/assets/js/admin.js similarity index 100% rename from assets/js/page-builder.js rename to assets/js/admin.js diff --git a/assets/js/public.js b/assets/js/public.js new file mode 100644 index 0000000..e8ada20 --- /dev/null +++ b/assets/js/public.js @@ -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]); + }); +}); \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php index 1a6e48d..0bdb4b5 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -13,6 +13,8 @@ class WDS_Page_Builder { + public $part_slug; + /** * Construct function to get things started. */ @@ -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 */ @@ -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; diff --git a/inc/options.php b/inc/options.php index b360304..e897208 100644 --- a/inc/options.php +++ b/inc/options.php @@ -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 ); } } @@ -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'; @@ -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'; diff --git a/inc/template-tags.php b/inc/template-tags.php index cfcd0b8..2cfaea8 100644 --- a/inc/template-tags.php +++ b/inc/template-tags.php @@ -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(); } \ No newline at end of file