Skip to content

Commit

Permalink
Allow customizing the order for grid-parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Feb 25, 2019
1 parent e5875a3 commit 09d8821
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
3 changes: 3 additions & 0 deletions assets/css/customizer/customizer.css
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,6 @@ h3.gridd-customizer-section-title {

#accordion-panel-gridd_hidden_panel {
display: none !important; }

#customize-control-gridd_grid_load_order .visibility {
display: none; }
2 changes: 1 addition & 1 deletion assets/css/customizer/customizer.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions assets/css/customizer/customizer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,10 @@ h3.gridd-customizer-section-title {

#accordion-panel-gridd_hidden_panel {
display: none !important;
}

#customize-control-gridd_grid_load_order {
.visibility {
display: none;
}
}
11 changes: 5 additions & 6 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@
*/
$active_parts = Grid_Parts::get_instance()->get_active();
$content_position = array_search( 'content', $active_parts, true );
if ( false === $content_position ) {
return;
}
foreach ( $active_parts as $key => $val ) {
if ( $key < $content_position ) {
do_action( 'gridd_the_grid_part', $val );
if ( false !== $content_position ) {
foreach ( $active_parts as $key => $val ) {
if ( $key < $content_position ) {
do_action( 'gridd_the_grid_part', $val );
}
}
}
?>
Expand Down
20 changes: 20 additions & 0 deletions inc/customizer/section/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,23 @@
'css_vars' => '--gridd-grid-max-width',
]
);

$parts = Grid_Parts::get_instance()->get_parts();
$sortable_parts = [];
foreach ( $parts as $part ) {
$sortable_parts[ $part['id'] ] = $part['label'];
}

gridd_add_customizer_field(
[
'type' => 'sortable',
'settings' => 'gridd_grid_load_order',
'label' => esc_html__( 'Grid Parts Load Order', 'gridd' ),
'description' => __( 'Changes the order in which parts get loaded. This only affects the mobile views and SEO. <strong>IMPORTANT: Please note that this setting does not live-update the preview. After making your changes you will have to save the options and refresh your page.</strong>', 'gridd' ),
'tooltip' => esc_html__( 'Your content should always be near the top. You can place secondary items lower in the load order', 'gridd' ),
'section' => 'gridd_grid',
'default' => array_keys( $sortable_parts ),
'priority' => 900,
'choices' => $sortable_parts,
]
);
59 changes: 58 additions & 1 deletion inc/grid-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function include_grid_part_files() {
public function set_parts() {
$this->parts = apply_filters( 'gridd_get_template_parts', [] );

// Reorder.
// Reorder using the default priorities.
usort(
$this->parts,
function( $a, $b ) {
Expand All @@ -150,6 +150,9 @@ function( $a, $b ) {
return ( isset( $a['priority'] ) ) ? 1 : -1;
}
);

// Apply cusstom order.
$this->apply_custom_order();
}

/**
Expand Down Expand Up @@ -230,4 +233,58 @@ public static function is_grid_part_active( $grid_part, $grid = 'gridd_grid' ) {
}
return false;
}

/**
* Applies custom order to grid-parts.
*
* @access protected
* @since 1.0.3
* @return void
*/
protected function apply_custom_order() {
$saved_order = get_theme_mod( 'gridd_grid_load_order', array() );
$all_part_ids = [];

// Get an array of all part IDs.
foreach ( $this->parts as $part ) {
$all_part_ids[] = $part['id'];
}

// Add any missing part-IDs to the saved order.
// Necessary if there is no saved order, or if a new part-ID was added.
foreach ( $all_part_ids as $part_id ) {
if ( ! in_array( $part_id, $saved_order ) ) {
$saved_order[] = $part_id;
}
}

// Build the ordered array.
$ordered = [];
foreach ( $saved_order as $part_id ) {
$part = $this->get_part_definition( $part_id );
if ( $part ) {
$ordered[] = $part;
}
}

// Update the array of parts with the custom-ordered one.
$this->parts = $ordered;
}

/**
* Get the definition of a grid-part.
*
* @access protected
* @since 1.0.3
* @param string $id The grid-part ID.
* @return array|false
*/
protected function get_part_definition( $id ) {
foreach ( $this->parts as $part ) {
if ( $id === $part['id'] ) {
return $part;
}
}
return false;
}
}

0 comments on commit 09d8821

Please sign in to comment.