| 1 |
<?php |
| 2 |
/** |
| 3 |
* Responsible for the grid layout. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Responsible for the grid layout. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_Grid_Layout { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get styling for the grid. |
| 24 |
* |
| 25 |
* @since 3.0.0 |
| 26 |
* @param mixed $grid Grid to output. |
| 27 |
* @param boolean $is_preview Wether or not this is styling for a grid preview. |
| 28 |
*/ |
| 29 |
public static function get_style( $grid, $is_preview = false ) { |
| 30 |
$output = ''; |
| 31 |
|
| 32 |
// Desktop. |
| 33 |
$style = self::get_item_sizing( $grid, 'desktop' ); |
| 34 |
$tablet_style = self::get_item_sizing( $grid, 'tablet' ); |
| 35 |
$mobile_style = self::get_item_sizing( $grid, 'mobile' ); |
| 36 |
|
| 37 |
$tablet_breakpoint = WPUPG_Settings::get( 'breakpoint_tablet' ); |
| 38 |
$mobile_breakpoint = WPUPG_Settings::get( 'breakpoint_mobile' ); |
| 39 |
|
| 40 |
// Add styling. |
| 41 |
if ( count( $style ) ) { |
| 42 |
$selector = '#wpupg-grid-' . $grid->slug_or_id(); |
| 43 |
|
| 44 |
$output .= '<style type="text/css">'; |
| 45 |
$output .= $selector . ' '; |
| 46 |
$output .= implode( ' ' . $selector . ' ', $style ); |
| 47 |
|
| 48 |
if ( $tablet_style ) { |
| 49 |
$tablet_selector = $is_preview ? '.wpupg-admin-modal-grid-preview-tablet ' . $selector : $selector; |
| 50 |
|
| 51 |
if ( ! $is_preview ) { |
| 52 |
$output .= ' @media (min-width: ' . $mobile_breakpoint . 'px) and (max-width: ' . $tablet_breakpoint . 'px) {'; |
| 53 |
} |
| 54 |
$output .= $tablet_selector . ' '; |
| 55 |
$output .= implode( ' ' . $tablet_selector . ' ', $tablet_style ); |
| 56 |
if ( ! $is_preview ) { |
| 57 |
$output .= '}'; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if ( $mobile_style ) { |
| 62 |
$mobile_selector = $is_preview ? '.wpupg-admin-modal-grid-preview-mobile ' . $selector : $selector; |
| 63 |
|
| 64 |
if ( ! $is_preview ) { |
| 65 |
$output .= ' @media (max-width: ' . $mobile_breakpoint . 'px) {'; |
| 66 |
} |
| 67 |
$output .= $mobile_selector . ' '; |
| 68 |
$output .= implode( ' ' . $mobile_selector . ' ', $mobile_style ); |
| 69 |
if ( ! $is_preview ) { |
| 70 |
$output .= '}'; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$output .= '</style>'; |
| 75 |
} |
| 76 |
|
| 77 |
return $output; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get item sizing styling for the grid. |
| 82 |
* |
| 83 |
* @since 3.0.0 |
| 84 |
* @param mixed $grid Grid to get the styling for. |
| 85 |
* @param mixed $device Device to get the sizing for. |
| 86 |
*/ |
| 87 |
private static function get_item_sizing( $grid, $device ) { |
| 88 |
$style = array(); |
| 89 |
$different = 'layout_' . $device . '_different'; |
| 90 |
|
| 91 |
if ( 'desktop' === $device || $grid->$different() ) { |
| 92 |
$sizing = 'layout_' . $device . '_sizing'; |
| 93 |
|
| 94 |
if ( 'ignore' !== $grid->$sizing() ) { |
| 95 |
$sizing_fixed = 'layout_' . $device . '_sizing_fixed'; |
| 96 |
$sizing_margin = 'layout_' . $device . '_sizing_margin'; |
| 97 |
$sizing_columns = 'layout_' . $device . '_sizing_columns'; |
| 98 |
|
| 99 |
if ( 'fixed' === $grid->$sizing() ) { |
| 100 |
$style[] = '.wpupg-item { width: 100%; max-width: ' . $grid->$sizing_fixed() . 'px; margin: ' . $grid->$sizing_margin() . 'px; }'; |
| 101 |
} elseif ( 'columns' === $grid->$sizing() ) { |
| 102 |
$width = floor( 10000 / $grid->$sizing_columns() ) / 100; |
| 103 |
$style[] = '.wpupg-item { width: calc(' . $width . '% - ' . 2 * $grid->$sizing_margin() . 'px); max-width: none; margin: ' . $grid->$sizing_margin() . 'px; }'; |
| 104 |
} |
| 105 |
|
| 106 |
if ( 'masonry' !== $grid->layout_mode() || ! $grid->centered() ) { |
| 107 |
$style[] = '{ margin: 0 -' . $grid->$sizing_margin() . 'px;}'; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $style; |
| 113 |
} |
| 114 |
} |
| 115 |
|