toggle-blocks.class.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // disable direct access
  4. }
  5. if ( ! class_exists('Mega_Menu_Toggle_Blocks') ) :
  6. /**
  7. * Mobile Toggle Blocks
  8. */
  9. class Mega_Menu_Toggle_Blocks {
  10. /**
  11. * Constructor
  12. *
  13. * @since 2.1
  14. */
  15. public function __construct() {
  16. add_filter( 'megamenu_scss_variables', array( $this, 'add_menu_toggle_block_vars_to_scss'), 10, 5 );
  17. add_filter( 'megamenu_scss_variables', array( $this, 'add_spacer_block_vars_to_scss'), 10, 5 );
  18. add_filter( 'megamenu_load_scss_file_contents', array( $this, 'append_scss'), 10 );
  19. add_filter( 'megamenu_toggle_bar_content', array( $this, 'output_public_toggle_blocks' ), 10, 4 );
  20. add_action( 'wp_ajax_mm_get_toggle_block_menu_toggle', array( $this, 'output_menu_toggle_block_html' ) );
  21. add_action( 'megamenu_output_admin_toggle_block_menu_toggle', array( $this, 'output_menu_toggle_block_html'), 10, 2 );
  22. add_action( 'megamenu_output_public_toggle_block_menu_toggle', array( $this, 'output_menu_public_toggle_block_html'), 10, 2 );
  23. add_action( 'wp_ajax_mm_get_toggle_block_spacer', array( $this, 'output_spacer_block_html' ) );
  24. add_action( 'megamenu_output_admin_toggle_block_spacer', array( $this, 'output_spacer_block_html'), 10, 2 );
  25. add_action( 'megamenu_after_theme_revert', array($this, 'revert_toggle_blocks') );
  26. add_action( 'megamenu_after_theme_save', array( $this, 'save_toggle_blocks' ) );
  27. add_action( 'megamenu_admin_scripts', array( $this, 'enqueue_scripts' ) );
  28. add_action( 'megamenu_print_theme_option_toggle_blocks', array( $this, 'print_theme_toggle_bar_designer_option'), 10, 2);
  29. add_filter( 'megamenu_theme_editor_settings', array( $this, 'add_toggle_designer_to_theme_editor'), 10 );
  30. }
  31. /**
  32. * Output the menu toggle block (front end)
  33. *
  34. * @since 2.4.1
  35. * @param string $html
  36. * @param array $settings
  37. * @return string
  38. */
  39. public function output_menu_public_toggle_block_html( $html, $settings ) {
  40. $css_version = get_transient("megamenu_css_version");
  41. // only use HTML version of toggle block if CSS version is above 2.4.0.2
  42. // if transient is missing, assume the latest version of the CSS is present and use Flex layout
  43. if ( ! $css_version || version_compare($css_version, '2.4.0.2') >= 0 ) {
  44. $closed_text = isset( $settings['closed_text'] ) ? do_shortcode( stripslashes( $settings['closed_text'] ) ) : "MENU";
  45. $open_text = isset( $settings['open_text'] ) ? do_shortcode( stripslashes( $settings['open_text'] ) ) : "MENU";
  46. $html = "<span class='mega-toggle-label'><span class='mega-toggle-label-closed'>{$closed_text}</span><span class='mega-toggle-label-open'>{$open_text}</span></span>";
  47. } else {
  48. $html = "";
  49. }
  50. return apply_filters("megamenu_toggle_menu_toggle_html", $html);
  51. }
  52. /**
  53. * Return the saved toggle blocks for a specified theme
  54. *
  55. * @param string $theme_id
  56. * @since 2.1
  57. * @return array
  58. */
  59. private function get_toggle_blocks_for_theme( $theme_id ) {
  60. $blocks = max_mega_menu_get_toggle_blocks();
  61. if ( isset( $blocks[ $theme_id ] ) ) {
  62. return $blocks[ $theme_id ];
  63. }
  64. // backwards compatibility
  65. // default to right aligned menu toggle using existing theme settings
  66. $default_blocks = array(
  67. 1 => $this->get_default_menu_toggle_block( $theme_id )
  68. );
  69. return $default_blocks;
  70. }
  71. /**
  72. * Return default menu toggle block settings
  73. *
  74. * @since 2.1
  75. * @return array
  76. */
  77. private function get_default_menu_toggle_block( $theme_id = 'default' ) {
  78. $style_manager = new Mega_Menu_Style_Manager();
  79. $themes = $style_manager->get_themes();
  80. $menu_theme = isset( $themes[ $theme_id ] ) ? $themes[ $theme_id ] : $themes['default'];
  81. $defaults = array(
  82. 'type' => 'menu_toggle',
  83. 'align' => 'right',
  84. 'closed_text' => isset($menu_theme['responsive_text']) ? $menu_theme['responsive_text'] : "MENU",
  85. 'open_text' => isset($menu_theme['responsive_text']) ? $menu_theme['responsive_text'] : "MENU",
  86. 'closed_icon' => 'dash-f333',
  87. 'open_icon' => 'dash-f153',
  88. 'icon_position' => 'after',
  89. 'text_color' => isset($menu_theme['toggle_font_color']) ? $menu_theme['toggle_font_color'] : 'rgb(221, 221, 221)',
  90. 'icon_color' => isset($menu_theme['toggle_font_color']) ? $menu_theme['toggle_font_color'] : 'rgb(221, 221, 221)',
  91. 'text_size' => '14px',
  92. 'icon_size' => '24px'
  93. );
  94. return $defaults;
  95. }
  96. /**
  97. * Get the HTML output for the toggle blocks
  98. *
  99. * @since 2.1
  100. * @param string $content
  101. * @param string $nav_menu
  102. * @param array $args
  103. * @param string $theme_id
  104. * @return string
  105. */
  106. public function output_public_toggle_blocks( $content, $nav_menu, $args, $theme_id ) {
  107. $toggle_blocks = $this->get_toggle_blocks_for_theme( $theme_id );
  108. $blocks_html = "";
  109. if ( is_array( $toggle_blocks ) ) {
  110. $css_version = get_transient("megamenu_css_version");
  111. // only use Flex layout version of toggle blocks if CSS version is above 2.4.0.2
  112. // if transient is missing, assume the latest version of the CSS is present and use Flex layout
  113. if ( ! $css_version || version_compare($css_version, '2.4.0.2') >= 0 ) {
  114. $blocks_html = $this->get_flex_blocks_html($toggle_blocks, $content, $nav_menu, $args, $theme_id);
  115. } else {
  116. $blocks_html = $this->get_backwards_compatibility_blocks_html($toggle_blocks, $content, $nav_menu, $args, $theme_id);
  117. }
  118. }
  119. $content .= $blocks_html;
  120. return $content;
  121. }
  122. /**
  123. * Sort the toggle blocks into 3 divs (left, center, right) to be aligned using flex CSS.
  124. *
  125. * @param array $toggle_blocks
  126. * @since 2.4.1
  127. * @return string html
  128. */
  129. private function get_flex_blocks_html( $toggle_blocks, $content, $nav_menu, $args, $theme_id ) {
  130. $sorted_blocks = array();
  131. /** Sort blocks into left, center, right array **/
  132. foreach ( $toggle_blocks as $block_id => $block ) {
  133. if ( isset( $block['align'] ) ) {
  134. $sorted_blocks[$block['align']][$block_id] = $block;
  135. } else {
  136. $sorted_blocks['left'][$block_id] = $block;
  137. }
  138. }
  139. $blocks_html = '<div class="mega-toggle-blocks-left">';
  140. if ( isset( $sorted_blocks['left'] ) ) {
  141. foreach ( $sorted_blocks['left'] as $block_id => $block ) {
  142. $blocks_html .= $this->get_toggle_block_html($block_id, $block, $content, $nav_menu, $args, $theme_id);
  143. }
  144. }
  145. $blocks_html .= "</div>";
  146. $blocks_html .= '<div class="mega-toggle-blocks-center">';
  147. if ( isset( $sorted_blocks['center'] ) ) {
  148. foreach ( $sorted_blocks['center'] as $block_id => $block ) {
  149. $blocks_html .= $this->get_toggle_block_html($block_id, $block, $content, $nav_menu, $args, $theme_id);
  150. }
  151. }
  152. $blocks_html .= "</div>";
  153. $blocks_html .= '<div class="mega-toggle-blocks-right">';
  154. if ( isset( $sorted_blocks['right'] ) ) {
  155. foreach ( $sorted_blocks['right'] as $block_id => $block ) {
  156. $blocks_html .= $this->get_toggle_block_html($block_id, $block, $content, $nav_menu, $args, $theme_id);
  157. }
  158. }
  159. $blocks_html .= "</div>";
  160. return $blocks_html;
  161. }
  162. /**
  163. * Generate the HTML for a single toggle block
  164. *
  165. * @since 2.4.1
  166. * @param string block_id
  167. * @param array $block
  168. * @return string
  169. */
  170. private function get_toggle_block_html($block_id, $block, $content, $nav_menu, $args, $theme_id) {
  171. $block_html = "";
  172. if ( isset( $block['type'] ) ) {
  173. $class = "mega-" . str_replace("_", "-", $block['type']) . "-block";
  174. } else {
  175. $class = "";
  176. }
  177. $id = apply_filters('megamenu_toggle_block_id', 'mega-toggle-block-' . $block_id);
  178. $attributes = apply_filters('megamenu_toggle_block_attributes', array(
  179. "class" => "mega-toggle-block {$class} mega-toggle-block-{$block_id}",
  180. "id" => "mega-toggle-block-{$block_id}"
  181. ), $block, $content, $nav_menu, $args, $theme_id);
  182. $block_html .= "<div";
  183. foreach ( $attributes as $attribute => $val ) {
  184. $block_html .= " " . $attribute . "='" . esc_attr( $val ) . "'";
  185. }
  186. $block_html .= ">";
  187. $block_html .= apply_filters("megamenu_output_public_toggle_block_{$block['type']}", "", $block);
  188. $block_html .= "</div>";
  189. return $block_html;
  190. }
  191. /**
  192. * Return a flat HTML list of menu toggle blocks. Only used when CSS version has not been updated to 2.4.1+
  193. *
  194. * @param array $toggle_blocks
  195. * @since 2.4.1
  196. * @return string html
  197. */
  198. private function get_backwards_compatibility_blocks_html( $toggle_blocks, $content, $nav_menu, $args, $theme_id ) {
  199. $blocks_html = "";
  200. foreach ( $toggle_blocks as $block_id => $block ) {
  201. if ( isset( $block['type'] ) ) {
  202. $class = "mega-" . str_replace("_", "-", $block['type']) . "-block";
  203. } else {
  204. $class = "";
  205. }
  206. if ( isset( $block['align'] ) ) {
  207. $align = "mega-toggle-block-" . $block['align'];
  208. } else {
  209. $align = "mega-toggle-block-left";
  210. }
  211. // @todo remove ID once MMM Pro has been updated to use classes
  212. $id = apply_filters('megamenu_toggle_block_id', 'mega-toggle-block-' . $block_id);
  213. $attributes = apply_filters('megamenu_toggle_block_attributes', array(
  214. "class" => "mega-toggle-block {$class} {$align} mega-toggle-block-{$block_id}",
  215. "id" => "mega-toggle-block-{$block_id}"
  216. ), $block, $content, $nav_menu, $args, $theme_id);
  217. /**
  218. *
  219. * function remove_ids_from_toggle_blocks($attributes, $block, $content, $nav_menu, $args, $theme_id) {
  220. * if (isset($attributes['id'])) {
  221. * unset($attributes['id']);
  222. * }
  223. * return $attributes;
  224. * }
  225. * add_filter('megamenu_toggle_block_attributes', 'remove_ids_from_toggle_blocks');
  226. *
  227. */
  228. $blocks_html .= "<div";
  229. foreach ( $attributes as $attribute => $val ) {
  230. $blocks_html .= " " . $attribute . "='" . esc_attr( $val ) . "'";
  231. }
  232. $blocks_html .= ">";
  233. $blocks_html .= apply_filters("megamenu_output_public_toggle_block_{$block['type']}", "", $block);
  234. $blocks_html .= "</div>";
  235. }
  236. return $blocks_html;
  237. }
  238. /**
  239. * Save the toggle blocks when the theme is saved
  240. *
  241. * @since 2.1
  242. */
  243. public function save_toggle_blocks() {
  244. $theme = esc_attr( $_POST['theme_id'] );
  245. $saved_blocks = max_mega_menu_get_toggle_blocks();
  246. if ( isset( $saved_blocks[ $theme ] ) ) {
  247. unset( $saved_blocks[ $theme ] );
  248. }
  249. $submitted_settings = $_POST['toggle_blocks'];
  250. $saved_blocks[ $theme ] = $submitted_settings;
  251. max_mega_menu_save_toggle_blocks( $saved_blocks );
  252. }
  253. /**
  254. * Revert the toggle blocks when a theme is reverted
  255. *
  256. * @since 2.1
  257. */
  258. public function revert_toggle_blocks() {
  259. $theme = esc_attr( $_GET['theme_id'] );
  260. $saved_toggle_blocks = max_mega_menu_get_toggle_blocks();
  261. if ( isset( $saved_toggle_blocks[$theme] ) ) {
  262. unset( $saved_toggle_blocks[$theme] );
  263. }
  264. max_mega_menu_save_toggle_blocks( $saved_toggle_blocks );
  265. }
  266. /**
  267. * Add the toggle bar designer to the theme editor
  268. *
  269. * @since 2.1
  270. * @return array
  271. */
  272. public function add_toggle_designer_to_theme_editor( $settings ) {
  273. $settings['mobile_menu']['settings']['toggle_blocks'] = array(
  274. 'priority' => 6,
  275. 'title' => __( "Toggle Bar Designer", "megamenu" ),
  276. 'description' => __( "Configure the contents of the mobile toggle bar", "megamenu" ),
  277. 'settings' => array(
  278. array(
  279. 'title' => "",
  280. 'type' => 'toggle_blocks',
  281. 'key' => 'toggle_blocks'
  282. )
  283. ),
  284. );
  285. return $settings;
  286. }
  287. /**
  288. * Enqueue nav-menus.php scripts
  289. *
  290. * @since 2.1
  291. */
  292. public function enqueue_scripts() {
  293. wp_enqueue_script( 'mega-menu-toggle-bar-designer', MEGAMENU_BASE_URL . 'js/toggledesigner.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-sortable' ), MEGAMENU_VERSION );
  294. wp_localize_script( 'mega-menu-toggle-bar-designer', 'megamenu',
  295. array(
  296. 'nonce' => wp_create_nonce('megamenu_edit')
  297. )
  298. );
  299. }
  300. /**
  301. * Append the logo SCSS to the main SCSS file
  302. *
  303. * @since 2.1
  304. * @param string $scss
  305. * @param string
  306. */
  307. public function append_scss( $scss ) {
  308. $path = MEGAMENU_PATH . 'css/toggle-blocks.scss';
  309. $contents = file_get_contents( $path );
  310. return $scss . $contents;
  311. }
  312. /**
  313. * Create a new variable containing the toggle blocks to be used by the SCSS file
  314. *
  315. * @param array $vars
  316. * @param string $location
  317. * @param string $theme
  318. * @param int $menu_id
  319. * @param string $theme_id
  320. * @return array - all custom SCSS vars
  321. * @since 2.1
  322. */
  323. public function add_menu_toggle_block_vars_to_scss( $vars, $location, $theme, $menu_id, $theme_id ) {
  324. $toggle_blocks = $this->get_toggle_blocks_for_theme( $theme_id );
  325. $menu_toggle_blocks = array();
  326. if ( is_array( $toggle_blocks ) ) {
  327. foreach( $toggle_blocks as $index => $settings ) {
  328. if ( isset( $settings['type'] ) && $settings['type'] == 'menu_toggle' ) {
  329. if ( isset( $settings['closed_icon'] ) ) {
  330. $closed_icon_parts = explode( '-', $settings['closed_icon'] );
  331. $closed_icon = end( $closed_icon_parts );
  332. } else {
  333. $closed_icon = 'disabled';
  334. }
  335. if ( isset( $settings['open_icon'] ) ) {
  336. $open_icon_parts = explode( '-', $settings['open_icon'] );
  337. $open_icon = end( $open_icon_parts );
  338. } else {
  339. $open_icon = 'disabled';
  340. }
  341. if ( isset( $settings['closed_text'] ) ) {
  342. $closed_text = "'" . do_shortcode( stripslashes( html_entity_decode( $settings['closed_text'], ENT_QUOTES ) ) ) . "'";
  343. } else {
  344. $closed_text = "'MENU'";
  345. }
  346. if ( isset( $settings['open_text'] ) ) {
  347. $open_text = "'" . do_shortcode( stripslashes( html_entity_decode( $settings['open_text'], ENT_QUOTES ) ) ) . "'";
  348. } else {
  349. $open_text = "''";
  350. }
  351. $styles = array(
  352. 'id' => $index,
  353. 'align' => isset($settings['align']) ? "'" . $settings['align'] . "'" : "'right'",
  354. 'closed_text' => $closed_text,
  355. 'open_text' => $open_text,
  356. 'closed_icon' => $closed_icon != 'disabled' ? "'\\" . $closed_icon . "'" : "''",
  357. 'open_icon' => $open_icon != 'disabled' ? "'\\" . $open_icon . "'" : "''",
  358. 'text_color' => isset($settings['text_color']) ? $settings['text_color'] : '#fff',
  359. 'icon_color' => isset($settings['icon_color']) ? $settings['icon_color'] : '#fff',
  360. 'icon_position' => isset($settings['icon_position']) ? "'" . $settings['icon_position'] . "'" : 'after',
  361. 'text_size' => isset($settings['text_size']) && strlen($settings['text_size']) ? $settings['text_size'] : '14px',
  362. 'icon_size' => isset($settings['icon_size']) && strlen($settings['icon_size']) ? $settings['icon_size'] : '24px'
  363. );
  364. $menu_toggle_blocks[ $index ] = $styles;
  365. }
  366. }
  367. }
  368. //$menu_toggle_blocks(
  369. // (123, red, 150px),
  370. // (456, green, null),
  371. // (789, blue, 90%),());
  372. if ( count( $menu_toggle_blocks ) ) {
  373. $list = "(";
  374. foreach ( $menu_toggle_blocks as $id => $vals ) {
  375. $list .= "(" . implode( ",", $vals ) . "),";
  376. }
  377. // Always add an empty list item to meke sure there are always at least 2 items in the list
  378. // Lists with a single item are not treated the same way by SASS
  379. $list .= "());";
  380. $vars['menu_toggle_blocks'] = $list;
  381. } else {
  382. $vars['menu_toggle_blocks'] = "()";
  383. }
  384. return $vars;
  385. }
  386. /**
  387. * Create a new variable containing the spacer blocks to be used by the SCSS file
  388. *
  389. * @param array $vars
  390. * @param string $location
  391. * @param string $theme
  392. * @param int $menu_id
  393. * @param string $theme_id
  394. * @return array - all custom SCSS vars
  395. * @since 2.1
  396. */
  397. public function add_spacer_block_vars_to_scss( $vars, $location, $theme, $menu_id, $theme_id ) {
  398. $toggle_blocks = $this->get_toggle_blocks_for_theme( $theme_id );
  399. $spacer_blocks = array();
  400. if ( is_array( $toggle_blocks ) ) {
  401. foreach( $toggle_blocks as $index => $settings ) {
  402. if ( isset( $settings['type'] ) && $settings['type'] == 'spacer' ) {
  403. $styles = array(
  404. 'id' => $index,
  405. 'align' => isset($settings['align']) ? "'" . $settings['align'] . "'" : "'right'",
  406. 'width' => isset($settings['width']) ? $settings['width'] : '0px',
  407. );
  408. $spacer_blocks[ $index ] = $styles;
  409. }
  410. }
  411. }
  412. //$menu_toggle_blocks(
  413. // (123, red, 150px),
  414. // (456, green, null),
  415. // (789, blue, 90%),());
  416. if ( count( $spacer_blocks ) ) {
  417. $list = "(";
  418. foreach ( $spacer_blocks as $id => $vals ) {
  419. $list .= "(" . implode( ",", $vals ) . "),";
  420. }
  421. // Always add an empty list item to meke sure there are always at least 2 items in the list
  422. // Lists with a single item are not treated the same way by SASS
  423. $list .= "());";
  424. $vars['spacer_toggle_blocks'] = $list;
  425. } else {
  426. $vars['spacer_toggle_blocks'] = "()";
  427. }
  428. return $vars;
  429. }
  430. /**
  431. * Print the toggle bar designer option
  432. *
  433. * @since 2.1
  434. * @param string $key
  435. * @param string $theme_id
  436. */
  437. public function print_theme_toggle_bar_designer_option( $key, $theme_id ) {
  438. $toggle_blocks = $this->get_toggle_blocks_for_theme( $theme_id );
  439. $block_types = apply_filters("megamenu_registered_toggle_blocks", array(
  440. 'title' => __("Add block to toggle bar", "megamenu"),
  441. 'menu_toggle' => __("Menu Toggle", "megamenu"),
  442. 'spacer' => __("Spacer", "megamenu")
  443. ));
  444. ?>
  445. <select id='toggle-block-selector'>
  446. <?php foreach( $block_types as $block_id => $block_name ) : ?>
  447. <option value='<?php echo $block_id; ?>'><?php echo $block_name ?></option>
  448. <?php endforeach; ?>
  449. <?php if ( ! is_plugin_active('megamenu-pro/megamenu-pro.php') ): ?>
  450. <option disabled="disabled">Search (Pro)</option>
  451. <option disabled="disabled">Logo (Pro)</option>
  452. <option disabled="disabled">Icon (Pro)</option>
  453. <option disabled="disabled">HTML (Pro)</option>
  454. <?php endif; ?>
  455. </select>
  456. <div class='toggle-bar-designer'>
  457. <div class='mega-blocks'>
  458. <div class='mega-left'>
  459. <?php
  460. if ( is_array( $toggle_blocks ) ) {
  461. foreach( $toggle_blocks as $block_id => $settings ) {
  462. if ( is_int( $block_id ) && is_array( $settings ) && isset( $settings['align'] ) && $settings['align'] == 'left' || ! isset( $settings['align'] ) ) {
  463. if ( isset( $settings['type'] ) ) {
  464. do_action( "megamenu_output_admin_toggle_block_{$settings['type']}", $block_id, $settings );
  465. }
  466. }
  467. }
  468. }
  469. ?>
  470. </div>
  471. <div class='mega-center'>
  472. <?php
  473. if ( is_array( $toggle_blocks ) ) {
  474. foreach( $toggle_blocks as $block_id => $settings ) {
  475. if ( is_int( $block_id ) && is_array( $settings ) && isset( $settings['align'] ) && $settings['align'] == 'center' ) {
  476. if ( isset( $settings['type'] ) ) {
  477. do_action( "megamenu_output_admin_toggle_block_{$settings['type']}", $block_id, $settings );
  478. }
  479. }
  480. }
  481. }
  482. ?>
  483. </div>
  484. <div class='mega-right'>
  485. <?php
  486. if ( is_array( $toggle_blocks ) ) {
  487. foreach( $toggle_blocks as $block_id => $settings ) {
  488. if ( is_int( $block_id ) && is_array( $settings ) && isset( $settings['align'] ) && $settings['align'] == 'right' ) {
  489. if ( isset( $settings['type'] ) ) {
  490. do_action( "megamenu_output_admin_toggle_block_{$settings['type']}", $block_id, $settings );
  491. }
  492. }
  493. }
  494. }
  495. ?>
  496. </div>
  497. </div>
  498. </div>
  499. <p class='mega-info'><?php _e("Click on a block to edit it, or drag and drop it to resposition the block within the toggle bar", "megamenu"); ?></p>
  500. <?php
  501. }
  502. /**
  503. * Output the HTML for the "Spacer" toggle block settings
  504. *
  505. * @since 2.1
  506. * @param int $block_id
  507. * @param array $settings
  508. */
  509. public function output_spacer_block_html( $block_id, $settings = array() ) {
  510. if ( empty( $settings ) ) {
  511. $block_id = "0";
  512. }
  513. $defaults = array(
  514. 'align' => 'right',
  515. 'width' => '0px'
  516. );
  517. $settings = array_merge( $defaults, $settings );
  518. ?>
  519. <div class='block'>
  520. <div class='block-title'><span title='<?php _e("Spacer", "megamenu"); ?>' class="dashicons dashicons-leftright"></span></div>
  521. <div class='block-settings'>
  522. <h3><?php _e("Spacer Settings", "megamenu") ?></h3>
  523. <input type='hidden' class='type' name='toggle_blocks[<?php echo $block_id; ?>][type]' value='spacer' />
  524. <input type='hidden' class='align' name='toggle_blocks[<?php echo $block_id; ?>][align]' value='<?php echo $settings['align'] ?>'>
  525. <label>
  526. <?php _e("Width", "megamenu") ?><input type='text' class='closed_text' name='toggle_blocks[<?php echo $block_id; ?>][width]' value='<?php echo $settings['width'] ?>' />
  527. </label>
  528. <a class='mega-delete'><?php _e("Delete", "megamenu"); ?></a>
  529. </div>
  530. </div>
  531. <?php
  532. }
  533. /**
  534. * Output the HTML for the "Menu Toggle" block settings
  535. *
  536. * @since 2.1
  537. * @param int $block_id
  538. * @param array $settings
  539. */
  540. public function output_menu_toggle_block_html( $block_id, $settings = array() ) {
  541. if ( empty( $settings ) ) {
  542. $block_id = "0";
  543. }
  544. $theme_id = 'default';
  545. if ( isset( $_GET['theme'] ) ) {
  546. $theme_id = esc_attr( $_GET['theme'] );
  547. }
  548. $defaults = $this->get_default_menu_toggle_block( $theme_id );
  549. $settings = array_merge( $defaults, $settings );
  550. ?>
  551. <div class='block'>
  552. <div class='block-title'><?php _e("MENU", "megamenu"); ?> <span title='<?php _e("Menu Toggle", "megamenu"); ?>' class="dashicons dashicons-menu"></span></div>
  553. <div class='block-settings'>
  554. <h3><?php _e("Menu Toggle Settings", "megamenu") ?></h3>
  555. <input type='hidden' class='type' name='toggle_blocks[<?php echo $block_id; ?>][type]' value='menu_toggle' />
  556. <input type='hidden' class='align' name='toggle_blocks[<?php echo $block_id; ?>][align]' value='<?php echo $settings['align'] ?>'>
  557. <label>
  558. <?php _e("Closed Text", "megamenu") ?><input type='text' class='closed_text' name='toggle_blocks[<?php echo $block_id; ?>][closed_text]' value='<?php echo stripslashes( esc_attr( $settings['closed_text'] ) ) ?>' />
  559. </label>
  560. <label>
  561. <?php _e("Open Text", "megamenu") ?><input type='text' class='open_text' name='toggle_blocks[<?php echo $block_id; ?>][open_text]' value='<?php echo stripslashes( esc_attr( $settings['open_text'] ) ) ?>' />
  562. </label>
  563. <label>
  564. <?php _e("Closed Icon", "megamenu") ?>
  565. <?php $this->print_icon_option( 'closed_icon', $block_id, $settings['closed_icon'], $this->toggle_icons() ); ?>
  566. </label>
  567. <label>
  568. <?php _e("Open Icon", "megamenu") ?>
  569. <?php $this->print_icon_option( 'open_icon', $block_id, $settings['open_icon'], $this->toggle_icons() ); ?>
  570. </label>
  571. <label>
  572. <?php _e("Text Color", "megamenu") ?>
  573. <?php $this->print_toggle_color_option( 'text_color', $block_id, $settings['text_color'] ); ?>
  574. </label>
  575. <label>
  576. <?php _e("Text Size", "megamenu") ?><input type='text' class='text_size' name='toggle_blocks[<?php echo $block_id; ?>][text_size]' value='<?php echo stripslashes( esc_attr( $settings['text_size'] ) ) ?>' />
  577. </label>
  578. <label>
  579. <?php _e("Icon Color", "megamenu") ?>
  580. <?php $this->print_toggle_color_option( 'icon_color', $block_id, $settings['icon_color'] ); ?>
  581. </label>
  582. <label>
  583. <?php _e("Icon Size", "megamenu") ?><input type='text' class='icon_size' name='toggle_blocks[<?php echo $block_id; ?>][icon_size]' value='<?php echo stripslashes( esc_attr( $settings['icon_size'] ) ) ?>' />
  584. </label>
  585. <label>
  586. <?php _e("Icon Position", "megamenu") ?><select name='toggle_blocks[<?php echo $block_id; ?>][icon_position]'>
  587. <option value='before' <?php selected( $settings['icon_position'], "before" ) ?> ><?php _e("Before", "megamenu") ?></option>
  588. <option value='after' <?php selected( $settings['icon_position'], "after" ) ?> ><?php _e("After", "megamenu") ?></option>
  589. </select>
  590. </label>
  591. <a class='mega-delete'><?php _e("Delete", "megamenu"); ?></a>
  592. </div>
  593. </div>
  594. <?php
  595. }
  596. /**
  597. * Print an icon selection box
  598. *
  599. * @since 2.1
  600. * @param string $key
  601. * @param int $block_id
  602. * @param string $value
  603. */
  604. public function print_icon_option( $key, $block_id, $value, $icons ) {
  605. ?>
  606. <select class='icon_dropdown' name='toggle_blocks[<?php echo $block_id ?>][<?php echo $key ?>]'>
  607. <?php
  608. echo "<option value='disabled'>" . __("Disabled", "megamenu") . "</option>";
  609. foreach ($icons as $code => $class) {
  610. $name = str_replace('dashicons-', '', $class);
  611. $name = ucwords(str_replace(array('-','arrow'), ' ', $name));
  612. echo "<option data-class='{$class}' value='{$code}'" . selected( $value, $code, false ) . ">" . $name . "</option>";
  613. }
  614. ?>
  615. </select>
  616. <?php
  617. }
  618. /**
  619. * Print a color picker
  620. *
  621. * @since 2.1
  622. * @param string $key
  623. * @param int $block_id
  624. * @param string $value
  625. */
  626. public function print_toggle_color_option( $key, $block_id, $value ) {
  627. if ( $value == 'transparent' ) {
  628. $value = 'rgba(0,0,0,0)';
  629. }
  630. if ( $value == 'rgba(0,0,0,0)' ) {
  631. $value_text = 'transparent';
  632. } else {
  633. $value_text = $value;
  634. }
  635. echo "<div class='mm-picker-container'>";
  636. echo " <input type='text' class='mm_colorpicker' name='toggle_blocks[{$block_id}][{$key}]' value='{$value}' />";
  637. echo " <div class='chosen-color'>{$value_text}</div>";
  638. echo "</div>";
  639. }
  640. /**
  641. * List of all available toggle DashIcon classes.
  642. *
  643. * @since 2.1
  644. * @return array - Sorted list of toggle classes
  645. */
  646. public function toggle_icons() {
  647. $icons = array(
  648. 'dash-f333' => 'dashicons-menu',
  649. 'dash-f214' => 'dashicons-editor-justify',
  650. 'dash-f158' => 'dashicons-no',
  651. 'dash-f335' => 'dashicons-no-alt',
  652. 'dash-f132' => 'dashicons-plus',
  653. 'dash-f502' => 'dashicons-plus-alt',
  654. 'dash-f460' => 'dashicons-minus',
  655. 'dash-f153' => 'dashicons-dismiss',
  656. 'dash-f142' => 'dashicons-arrow-up',
  657. 'dash-f140' => 'dashicons-arrow-down',
  658. 'dash-f342' => 'dashicons-arrow-up-alt',
  659. 'dash-f346' => 'dashicons-arrow-down-alt',
  660. 'dash-f343' => 'dashicons-arrow-up-alt2',
  661. 'dash-f347' => 'dashicons-arrow-down-alt2',
  662. );
  663. $icons = apply_filters( "megamenu_toggle_icons", $icons );
  664. return $icons;
  665. }
  666. }
  667. endif;