| 1 |
<?php |
| 2 |
|
| 3 |
namespace SideMenuLite\Maker; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Script { |
| 8 |
const NAME = 'SideMenuLite_'; |
| 9 |
private $object_data; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->object_data = array(); |
| 13 |
} |
| 14 |
|
| 15 |
public function add( $property_name, $property_value ) { |
| 16 |
$this->object_data[ $property_name ] = $property_value; |
| 17 |
} |
| 18 |
|
| 19 |
public function generate( $id, $param ) { |
| 20 |
$this->add_element( $id ); |
| 21 |
$this->add_settings( $param ); |
| 22 |
$this->add_style( $param ); |
| 23 |
$this->add_item( $param ); |
| 24 |
if ( ! empty( $param['mobile'] ) ) { |
| 25 |
$this->add_mobile_style( $param ); |
| 26 |
} |
| 27 |
|
| 28 |
return 'var ' . self::NAME . $id . ' = ' . wp_json_encode( $this->object_data ) . ';'; |
| 29 |
} |
| 30 |
|
| 31 |
public function add_element( $id ) { |
| 32 |
$this->add( 'element', 'side-menu-' . absint( $id ) ); |
| 33 |
} |
| 34 |
|
| 35 |
public function add_settings( $param ) { |
| 36 |
$settings = array( |
| 37 |
'showAfterPosition' => 'showAfterPosition', |
| 38 |
'hideAfterPosition' => 'hideAfterPosition', |
| 39 |
'showAfterTimer' => 'showAfterTimer', |
| 40 |
'hideAfterTimer' => 'hideAfterTimer', |
| 41 |
'touch' => 'touch', |
| 42 |
'scrollSpyOffset' => 'scrollSpyOffset', |
| 43 |
); |
| 44 |
|
| 45 |
|
| 46 |
foreach ( $settings as $param_key => $setting_key ) { |
| 47 |
if ( ! empty( $param[ $param_key ] ) ) { |
| 48 |
$this->add( $setting_key, (int) $param[ $param_key ] ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! empty( $param['include_mobile'] ) ) { |
| 53 |
$mobileHide = ! empty( $param['screen'] ) ? $param['screen'] : 480; |
| 54 |
$this->add( 'mobileHide', (int) $mobileHide ); |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! empty( $param['include_more_screen'] ) ) { |
| 58 |
$desktopHide = ! empty( $param['screen_more'] ) ? $param['screen_more'] : 1200; |
| 59 |
$this->add( 'desktopHide', (int) $desktopHide ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function add_style( $param ) { |
| 64 |
$defaults = array( |
| 65 |
'zindex' => '9', |
| 66 |
'margin' => '0', |
| 67 |
'height' => '40', |
| 68 |
'iconsize' => '24', |
| 69 |
'fontsize' => '24', |
| 70 |
'fontstyle' => 'normal', |
| 71 |
'fontweight' => 'normal', |
| 72 |
'bwidth' => '0', |
| 73 |
'bradiustop' => '0', |
| 74 |
'bradiusbottom' => '0', |
| 75 |
'bcolor' => 'rgba(0,0,0,0.75)', |
| 76 |
'gap' => '0', |
| 77 |
); |
| 78 |
|
| 79 |
$param = array_merge( $defaults, $param ); |
| 80 |
|
| 81 |
$style = array( |
| 82 |
'--sm-z-index' => ( $param['zindex'] === '9' ) ? '9999' : $param['zindex'], |
| 83 |
'--sm-offset' => $param['margin'] . 'px', |
| 84 |
'--sm-item-height' => $param['height'] . 'px', |
| 85 |
'--sm-icon-width' => $param['height'] . 'px', |
| 86 |
'--sm-icon-size' => $param['iconsize'] . 'px', |
| 87 |
'--sm-label-size' => $param['fontsize'] . 'px', |
| 88 |
'--sm-label-font' => 'inherit', |
| 89 |
'--sm-label-font-style' => $param['fontstyle'], |
| 90 |
'--sm-label-font-weight' => $param['fontweight'], |
| 91 |
'--sm-border-width' => $param['bwidth'] . 'px', |
| 92 |
'--sm-border-color' => $param['bcolor'], |
| 93 |
'--sm-radius-top' => $param['bradiustop'] . 'px', |
| 94 |
'--sm-radius-bottom' => $param['bradiusbottom'] . 'px', |
| 95 |
'--sm-button-space' => $param['gap'] . 'px', |
| 96 |
); |
| 97 |
|
| 98 |
$this->add( 'style', $style ); |
| 99 |
} |
| 100 |
|
| 101 |
public function add_item( $param ) { |
| 102 |
$item = []; |
| 103 |
|
| 104 |
if ( !empty($param['menu_1']['item_type']) && is_array( $param['menu_1']['item_type'] ) ) { |
| 105 |
$itemTypeCount = count( $param['menu_1']['item_type'] ); |
| 106 |
|
| 107 |
for ( $i = 0; $i < $itemTypeCount; $i ++ ) { |
| 108 |
$item[ $i ] = [ |
| 109 |
'--sm-color' => $param['menu_1']['color'][ $i ], |
| 110 |
'--sm-icon-color' => $param['menu_1']['iconcolor'][ $i ], |
| 111 |
'--sm-extra-text-color' => '#ffffff', |
| 112 |
'--sm-background' => $param['menu_1']['bcolor'][ $i ], |
| 113 |
'--sm-hover-background' => $param['menu_1']['hbcolor'][ $i ], |
| 114 |
'--sm-extra-text-width' => ( ! empty( $param['menu_1']['item_text_width'][ $i ] ) ? $param['menu_1']['item_text_width'][ $i ] : '270' ) . 'px', |
| 115 |
'--sm-extra-fontsize' => ( ! empty( $param['menu_1']['item_text_size'][ $i ] ) ? $param['menu_1']['item_text_size'][ $i ] : '16' ) . 'px', |
| 116 |
]; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$this->add( 'items', $item ); |
| 121 |
} |
| 122 |
|
| 123 |
public function add_mobile_style( $param ) { |
| 124 |
$m_screen = isset( $param['m_screen'] ) ? $param['m_screen'] : 768; |
| 125 |
$this->add( 'mobile', absint( $m_screen ) ); |
| 126 |
|
| 127 |
$m_height = isset( $param['m_height'] ) ? $param['m_height'] : '40'; |
| 128 |
$m_iconsize = isset( $param['m_iconsize'] ) ? $param['m_iconsize'] : '24'; |
| 129 |
$m_fontsize = isset( $param['m_fontsize'] ) ? $param['m_fontsize'] : '24'; |
| 130 |
|
| 131 |
$mobileStyle = [ |
| 132 |
'--sm-item-height' => $m_height . 'px', |
| 133 |
'--sm-icon-width' => $m_height . 'px', |
| 134 |
'--sm-icon-size' => $m_iconsize . 'px', |
| 135 |
'--sm-label-size' => $m_fontsize . 'px', |
| 136 |
]; |
| 137 |
$this->add( 'mobileStyle', $mobileStyle ); |
| 138 |
} |
| 139 |
|
| 140 |
} |