style-manager.class.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // disable direct access
  4. }
  5. if ( ! class_exists( 'Mega_Menu_Style_Manager' ) ) :
  6. /**
  7. *
  8. */
  9. final class Mega_Menu_Style_Manager {
  10. /**
  11. *
  12. */
  13. var $settings = array();
  14. /**
  15. * Constructor
  16. *
  17. * @since 1.0
  18. */
  19. public function __construct() {
  20. $this->settings = get_option( "megamenu_settings" );
  21. }
  22. /**
  23. * Setup actions
  24. *
  25. * @since 1.0
  26. */
  27. public function setup_actions() {
  28. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
  29. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 );
  30. add_action( 'wp_head', array( $this, 'head_css' ), 9999 );
  31. add_action( 'megamenu_delete_cache', array( $this, 'delete_cache' ) );
  32. add_action( 'megamenu_delete_cache', array( $this, 'clear_external_caches' ) );
  33. add_action( 'after_switch_theme', array( $this, 'delete_cache') );
  34. // PolyLang
  35. if ( function_exists( 'pll_current_language' ) ) {
  36. add_filter( 'megamenu_css_transient_key', array( $this, 'polylang_transient_key') );
  37. add_filter( 'megamenu_css_filename', array( $this, 'polylang_css_filename') );
  38. add_action( 'megamenu_after_delete_cache', array( $this, 'polylang_delete_cache') );
  39. } else if ( defined( 'ICL_LANGUAGE_CODE' ) ) { // WPML
  40. add_filter( 'megamenu_css_transient_key', array( $this, 'wpml_transient_key') );
  41. add_filter( 'megamenu_css_filename', array( $this, 'wpml_css_filename') );
  42. add_action( 'megamenu_after_delete_cache', array( $this, 'wpml_delete_cache') );
  43. }
  44. }
  45. /**
  46. * Clear plugin caches when CSS is updated or menu settings are changed
  47. */
  48. public function clear_external_caches() {
  49. // Breeze: https://wordpress.org/plugins/breeze/
  50. do_action('breeze_clear_all_cache');
  51. }
  52. /**
  53. * Return the default menu theme
  54. */
  55. public function get_default_theme() {
  56. return apply_filters("megamenu_default_theme", array(
  57. 'title' => __("Default", "megamenu"),
  58. 'container_background_from' => '#222',
  59. 'container_background_to' => '#222',
  60. 'container_padding_left' => '0px',
  61. 'container_padding_right' => '0px',
  62. 'container_padding_top' => '0px',
  63. 'container_padding_bottom' => '0px',
  64. 'container_border_radius_top_left' => '0px',
  65. 'container_border_radius_top_right' => '0px',
  66. 'container_border_radius_bottom_left' => '0px',
  67. 'container_border_radius_bottom_right' => '0px',
  68. 'arrow_up' => 'dash-f142',
  69. 'arrow_down' => 'dash-f140',
  70. 'arrow_left' => 'dash-f141',
  71. 'arrow_right' => 'dash-f139',
  72. 'font_size' => '14px', // deprecated
  73. 'font_color' => '#666', // deprecated
  74. 'font_family' => 'inherit', // deprecated
  75. 'menu_item_align' => 'left',
  76. 'menu_item_background_from' => 'rgba(0,0,0,0)',
  77. 'menu_item_background_to' => 'rgba(0,0,0,0)',
  78. 'menu_item_background_hover_from' => '#333',
  79. 'menu_item_background_hover_to' => '#333',
  80. 'menu_item_spacing' => '0px',
  81. 'menu_item_link_font' => 'inherit',
  82. 'menu_item_link_font_size' => '14px',
  83. 'menu_item_link_height' => '40px',
  84. 'menu_item_link_color' => '#ffffff',
  85. 'menu_item_link_weight' => 'normal',
  86. 'menu_item_link_text_transform' => 'none',
  87. 'menu_item_link_text_decoration' => 'none',
  88. 'menu_item_link_text_align' => 'left',
  89. 'menu_item_link_color_hover' => '#ffffff',
  90. 'menu_item_link_weight_hover' => 'normal',
  91. 'menu_item_link_text_decoration_hover' => 'none',
  92. 'menu_item_link_padding_left' => '10px',
  93. 'menu_item_link_padding_right' => '10px',
  94. 'menu_item_link_padding_top' => '0px',
  95. 'menu_item_link_padding_bottom' => '0px',
  96. 'menu_item_link_border_radius_top_left' => '0px',
  97. 'menu_item_link_border_radius_top_right' => '0px',
  98. 'menu_item_link_border_radius_bottom_left' => '0px',
  99. 'menu_item_link_border_radius_bottom_right' => '0px',
  100. 'menu_item_border_color' => '#fff',
  101. 'menu_item_border_left' => '0px',
  102. 'menu_item_border_right' => '0px',
  103. 'menu_item_border_top' => '0px',
  104. 'menu_item_border_bottom' => '0px',
  105. 'menu_item_border_color_hover' => '#fff',
  106. 'menu_item_highlight_current' => 'on',
  107. 'menu_item_divider' => 'off',
  108. 'menu_item_divider_color' => 'rgba(255, 255, 255, 0.1)',
  109. 'menu_item_divider_glow_opacity' => '0.1',
  110. 'panel_background_from' => '#f1f1f1',
  111. 'panel_background_to' => '#f1f1f1',
  112. 'panel_width' => '100%',
  113. 'panel_inner_width' => '100%',
  114. 'panel_border_color' => '#fff',
  115. 'panel_border_left' => '0px',
  116. 'panel_border_right' => '0px',
  117. 'panel_border_top' => '0px',
  118. 'panel_border_bottom' => '0px',
  119. 'panel_border_radius_top_left' => '0px',
  120. 'panel_border_radius_top_right' => '0px',
  121. 'panel_border_radius_bottom_left' => '0px',
  122. 'panel_border_radius_bottom_right' => '0px',
  123. 'panel_header_color' => '#555',
  124. 'panel_header_text_transform' => 'uppercase',
  125. 'panel_header_font' => 'inherit',
  126. 'panel_header_font_size' => '16px',
  127. 'panel_header_font_weight' => 'bold',
  128. 'panel_header_text_decoration' => 'none',
  129. 'panel_header_padding_top' => '0px',
  130. 'panel_header_padding_right' => '0px',
  131. 'panel_header_padding_bottom' => '5px',
  132. 'panel_header_padding_left' => '0px',
  133. 'panel_header_margin_top' => '0px',
  134. 'panel_header_margin_right' => '0px',
  135. 'panel_header_margin_bottom' => '0px',
  136. 'panel_header_margin_left' => '0px',
  137. 'panel_header_border_color' => 'panel_header_color',
  138. 'panel_header_border_left' => '0px',
  139. 'panel_header_border_right' => '0px',
  140. 'panel_header_border_top' => '0px',
  141. 'panel_header_border_bottom' => '0px',
  142. 'panel_padding_left' => '0px',
  143. 'panel_padding_right' => '0px',
  144. 'panel_padding_top' => '0px',
  145. 'panel_padding_bottom' => '0px',
  146. 'panel_widget_padding_left' => '15px',
  147. 'panel_widget_padding_right' => '15px',
  148. 'panel_widget_padding_top' => '15px',
  149. 'panel_widget_padding_bottom' => '15px',
  150. 'panel_font_size' => 'font_size',
  151. 'panel_font_color' => 'font_color',
  152. 'panel_font_family' => 'font_family',
  153. 'panel_second_level_font_color' => 'panel_header_color',
  154. 'panel_second_level_font_color_hover' => 'panel_header_color',
  155. 'panel_second_level_text_transform' => 'panel_header_text_transform',
  156. 'panel_second_level_font' => 'panel_header_font',
  157. 'panel_second_level_font_size' => 'panel_header_font_size',
  158. 'panel_second_level_font_weight' => 'panel_header_font_weight',
  159. 'panel_second_level_font_weight_hover' => 'panel_header_font_weight',
  160. 'panel_second_level_text_decoration' => 'panel_header_text_decoration',
  161. 'panel_second_level_text_decoration_hover' => 'panel_header_text_decoration',
  162. 'panel_second_level_background_hover_from' => 'rgba(0,0,0,0)',
  163. 'panel_second_level_background_hover_to' => 'rgba(0,0,0,0)',
  164. 'panel_second_level_padding_left' => '0px',
  165. 'panel_second_level_padding_right' => '0px',
  166. 'panel_second_level_padding_top' => '0px',
  167. 'panel_second_level_padding_bottom' => '0px',
  168. 'panel_second_level_margin_left' => '0px',
  169. 'panel_second_level_margin_right' => '0px',
  170. 'panel_second_level_margin_top' => '0px',
  171. 'panel_second_level_margin_bottom' => '0px',
  172. 'panel_second_level_border_color' => 'panel_header_color',
  173. 'panel_second_level_border_left' => '0px',
  174. 'panel_second_level_border_right' => '0px',
  175. 'panel_second_level_border_top' => '0px',
  176. 'panel_second_level_border_bottom' => '0px',
  177. 'panel_third_level_font_color' => 'panel_font_color',
  178. 'panel_third_level_font_color_hover' => 'panel_font_color',
  179. 'panel_third_level_text_transform' => 'none',
  180. 'panel_third_level_font' => 'panel_font_family',
  181. 'panel_third_level_font_size' => 'panel_font_size',
  182. 'panel_third_level_font_weight' => 'normal',
  183. 'panel_third_level_font_weight_hover' => 'normal',
  184. 'panel_third_level_text_decoration' => 'none',
  185. 'panel_third_level_text_decoration_hover' => 'none',
  186. 'panel_third_level_background_hover_from' => 'rgba(0,0,0,0)',
  187. 'panel_third_level_background_hover_to' => 'rgba(0,0,0,0)',
  188. 'panel_third_level_padding_left' => '0px',
  189. 'panel_third_level_padding_right' => '0px',
  190. 'panel_third_level_padding_top' => '0px',
  191. 'panel_third_level_padding_bottom' => '0px',
  192. 'flyout_width' => '150px',
  193. 'flyout_menu_background_from' => '#f1f1f1',
  194. 'flyout_menu_background_to' => '#f1f1f1',
  195. 'flyout_border_color' => '#ffffff',
  196. 'flyout_border_left' => '0px',
  197. 'flyout_border_right' => '0px',
  198. 'flyout_border_top' => '0px',
  199. 'flyout_border_bottom' => '0px',
  200. 'flyout_border_radius_top_left' => '0px',
  201. 'flyout_border_radius_top_right' => '0px',
  202. 'flyout_border_radius_bottom_left' => '0px',
  203. 'flyout_border_radius_bottom_right' => '0px',
  204. 'flyout_menu_item_divider' => 'off',
  205. 'flyout_menu_item_divider_color' => 'rgba(255, 255, 255, 0.1)',
  206. 'flyout_padding_top' => '0px',
  207. 'flyout_padding_right' => '0px',
  208. 'flyout_padding_bottom' => '0px',
  209. 'flyout_padding_left' => '0px',
  210. 'flyout_link_padding_left' => '10px',
  211. 'flyout_link_padding_right' => '10px',
  212. 'flyout_link_padding_top' => '0px',
  213. 'flyout_link_padding_bottom' => '0px',
  214. 'flyout_link_weight' => 'normal',
  215. 'flyout_link_weight_hover' => 'normal',
  216. 'flyout_link_height' => '35px',
  217. 'flyout_link_text_decoration' => 'none',
  218. 'flyout_link_text_decoration_hover' => 'none',
  219. 'flyout_background_from' => '#f1f1f1',
  220. 'flyout_background_to' => '#f1f1f1',
  221. 'flyout_background_hover_from' => '#dddddd',
  222. 'flyout_background_hover_to' => '#dddddd',
  223. 'flyout_link_size' => 'font_size',
  224. 'flyout_link_color' => 'font_color',
  225. 'flyout_link_color_hover' => 'font_color',
  226. 'flyout_link_family' => 'font_family',
  227. 'flyout_link_text_transform' => 'none',
  228. 'responsive_breakpoint' => '600px',
  229. 'responsive_text' => 'MENU', // deprecated
  230. 'line_height' => '1.7',
  231. 'z_index' => '999',
  232. 'shadow' => 'off',
  233. 'shadow_horizontal' => '0px',
  234. 'shadow_vertical' => '0px',
  235. 'shadow_blur' => '5px',
  236. 'shadow_spread' => '0px',
  237. 'shadow_color' => 'rgba(0, 0, 0, 0.1)',
  238. 'transitions' => 'off',
  239. 'resets' => 'off',
  240. 'mobile_columns' => '2',
  241. 'toggle_background_from' => 'container_background_from',
  242. 'toggle_background_to' => 'container_background_to',
  243. 'toggle_font_color' => 'rgb(221, 221, 221)', // deprecated
  244. 'toggle_bar_height' => '40px',
  245. 'mobile_menu_item_height' => '40px',
  246. 'mobile_menu_overlay' => 'off',
  247. 'mobile_menu_force_width' => 'off',
  248. 'mobile_menu_force_width_selector' => 'body',
  249. 'mobile_background_from' => 'container_background_from',
  250. 'mobile_background_to' => 'container_background_to',
  251. 'mobile_menu_item_link_font_size' => 'menu_item_link_font_size',
  252. 'mobile_menu_item_link_color' => 'menu_item_link_color',
  253. 'mobile_menu_item_link_text_align' => 'menu_item_link_text_align',
  254. 'mobile_menu_item_link_color_hover' => 'menu_item_link_color_hover',
  255. 'mobile_menu_item_background_hover_from' => 'menu_item_background_hover_from',
  256. 'mobile_menu_item_background_hover_to' => 'menu_item_background_hover_to',
  257. 'disable_mobile_toggle' => 'off',
  258. 'custom_css' => '/** Push menu onto new line **/
  259. #{$wrap} {
  260. clear: both;
  261. }'
  262. ) );
  263. }
  264. /**
  265. *
  266. * @since 1.0
  267. */
  268. public function default_themes() {
  269. $themes['default'] = $this->get_default_theme();
  270. return apply_filters( "megamenu_themes", $themes );
  271. }
  272. /**
  273. * Merge the saved themes (from options table) into array of complete themes
  274. *
  275. * @since 2.1
  276. */
  277. private function merge_in_saved_themes( $all_themes ) {
  278. if ( $saved_themes = max_mega_menu_get_themes() ) {
  279. foreach ( $saved_themes as $key => $settings ) {
  280. if ( isset( $all_themes[ $key ] ) ) {
  281. // merge modifications to default themes
  282. $all_themes[ $key ] = array_merge( $all_themes[ $key ], $saved_themes[ $key ] );
  283. } else {
  284. // add in new themes
  285. $all_themes[ $key ] = $settings;
  286. }
  287. }
  288. }
  289. return $all_themes;
  290. }
  291. /**
  292. * Populate all themes with all keys from the default theme
  293. *
  294. * @since 2.1
  295. */
  296. private function ensure_all_themes_have_all_default_theme_settings( $all_themes ) {
  297. $default_theme = $this->get_default_theme();
  298. $themes = array();
  299. foreach ( $all_themes as $theme_id => $theme ) {
  300. $themes[ $theme_id ] = array_merge( $default_theme, $theme );
  301. }
  302. return $themes;
  303. }
  304. /**
  305. * For backwards compatibility, copy old settings into new values
  306. *
  307. * @since 2.1
  308. */
  309. private function process_theme_replacements( $all_themes ) {
  310. foreach ( $all_themes as $key => $settings ) {
  311. // process replacements
  312. foreach ( $settings as $var => $val ) {
  313. if ( ! is_array( $val ) && isset( $all_themes[$key][$val] ) ) {
  314. $all_themes[$key][$var] = $all_themes[$key][$val];
  315. }
  316. }
  317. }
  318. return $all_themes;
  319. }
  320. /**
  321. * Return a filtered list of themes
  322. *
  323. * @since 1.0
  324. * @return array
  325. */
  326. public function get_themes() {
  327. $default_themes = $this->default_themes();
  328. $all_themes = $this->merge_in_saved_themes( $default_themes );
  329. $all_themes = $this->ensure_all_themes_have_all_default_theme_settings( $all_themes );
  330. $all_themes = $this->process_theme_replacements( $all_themes );
  331. uasort( $all_themes, array( $this, 'sort_by_title' ) );
  332. return $all_themes;
  333. }
  334. /**
  335. * Sorts a 2d array by the 'title' key
  336. *
  337. * @since 1.0
  338. * @param array $a
  339. * @param array $b
  340. */
  341. private function sort_by_title( $a, $b ) {
  342. return strcmp( $a['title'], $b['title'] );
  343. }
  344. /**
  345. *
  346. *
  347. * @since 1.3.1
  348. */
  349. private function is_debug_mode() {
  350. return ( defined( 'MEGAMENU_DEBUG' ) && MEGAMENU_DEBUG === true ) || ( current_user_can('edit_theme_options') && isset( $_GET['nocache'] ) );
  351. }
  352. /**
  353. * Return the menu CSS for use in inline CSS block. Use the cache if possible.
  354. *
  355. * @since 1.3.1
  356. */
  357. public function get_css() {
  358. if ( ( $css = $this->get_cached_css() ) && ! $this->is_debug_mode() ) {
  359. return $css;
  360. } else {
  361. return $this->generate_css();
  362. }
  363. }
  364. /**
  365. * Generate and cache the CSS for our menus.
  366. * The CSS is compiled by scssphp using the file located in /css/megamenu.scss
  367. *
  368. * @since 1.0
  369. * @return string
  370. * @param boolean $debug_mode (prints error messages to the CSS when enabled)
  371. */
  372. public function generate_css() {
  373. if ( function_exists('wp_raise_memory_limit') ) {
  374. wp_raise_memory_limit(); // attempt to raise memory limit to 256MB
  375. }
  376. // the settings may have changed since the class was instantiated,
  377. // reset them here
  378. $this->settings = get_option( "megamenu_settings" );
  379. if ( ! $this->settings ) {
  380. return "/** CSS Generation Failed. No menu settings found **/";
  381. }
  382. $css = "";
  383. foreach ( $this->settings as $location => $settings ) {
  384. if ( isset( $settings['enabled'] ) && has_nav_menu( $location ) ) {
  385. $theme = $this->get_theme_settings_for_location( $location );
  386. $menu_id = $this->get_menu_id_for_location( $location );
  387. $compiled_css = $this->generate_css_for_location( $location, $theme, $menu_id );
  388. if ( ! is_wp_error( $compiled_css ) ) {
  389. $css .= $compiled_css;
  390. }
  391. }
  392. }
  393. if ( strlen( $css ) ) {
  394. $scss_location = 'core';
  395. foreach ( $this->get_possible_scss_file_locations() as $path ) {
  396. if ( file_exists($path) && $path !== $this->get_default_scss_file_location() ) {
  397. $scss_location = 'custom';
  398. }
  399. }
  400. $css = "/** " . date('l jS F Y H:i:s e') . " ({$scss_location}) **/\n/** THIS FILE IS AUTOMATICALLY GENERATED - DO NOT MAKE MANUAL EDITS! **/\n/** Custom CSS should be added to Mega Menu > Menu Themes > Custom Styling **/\n\n" . $css;
  401. $css = apply_filters( "megamenu_compiled_css", $css );
  402. $this->set_cached_css( $css );
  403. if ( $this->get_css_output_method() == 'fs' ) {
  404. $this->save_to_filesystem( $css );
  405. }
  406. }
  407. return $css;
  408. }
  409. /**
  410. *
  411. * @since 1.6.1
  412. */
  413. private function save_to_filesystem( $css ) {
  414. global $wp_filesystem;
  415. if ( ! $wp_filesystem ) {
  416. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  417. }
  418. $upload_dir = wp_upload_dir();
  419. $filename = $this->get_css_filename();
  420. $dir = trailingslashit( $upload_dir['basedir'] ) . 'maxmegamenu/';
  421. WP_Filesystem( false, $upload_dir['basedir'], true );
  422. if( ! $wp_filesystem->is_dir( $dir ) ) {
  423. $wp_filesystem->mkdir( $dir );
  424. }
  425. if ( ! $wp_filesystem->put_contents( $dir . $filename, $css ) ) {
  426. // File write failed.
  427. // Update CSS output option to 'head' to stop us from attempting to regenerate the CSS on every request.
  428. $settings = get_option( 'megamenu_settings' );
  429. $settings['css'] = 'head';
  430. update_option( 'megamenu_settings', $settings );
  431. $this->settings = get_option( "megamenu_settings" );
  432. set_transient( 'megamenu_failed_to_write_css_to_filesystem', 'true', 0 );
  433. }
  434. }
  435. /**
  436. * Return an array of all the possible file path locations for the SCSS file
  437. * @since 2.2.3
  438. * @return array
  439. */
  440. private function get_possible_scss_file_locations() {
  441. return apply_filters( "megamenu_scss_locations", array(
  442. trailingslashit( get_stylesheet_directory() ) . trailingslashit("megamenu") . 'megamenu.scss', // child theme
  443. trailingslashit( get_template_directory() ) . trailingslashit("megamenu") . 'megamenu.scss', // parent theme
  444. $this->get_default_scss_file_location()
  445. ));
  446. }
  447. /**
  448. * Return the default SCSS file path
  449. *
  450. * @since 2.2.3
  451. * @return string
  452. */
  453. private function get_default_scss_file_location() {
  454. return MEGAMENU_PATH . trailingslashit('css') . 'megamenu.scss';
  455. }
  456. /**
  457. * Return the path to the megamenu.scss file, look for custom files before
  458. * loading the core version.
  459. *
  460. * @since 1.0
  461. * @return string
  462. */
  463. private function load_scss_file() {
  464. /**
  465. * *** IMPORTANT NOTICE ***
  466. *
  467. * Allowing users to create their own versions of megamenu.scss was a poor design decision.
  468. *
  469. * The bundled SCSS file and the plugin code work in perfect harmony.
  470. *
  471. * When a user (or theme developer) creates their own copy of megamenu.scss it
  472. * _will_ become outdated as the plugin is updated and the menu HTML changes.
  473. *
  474. * Instead of using a custom SCSS file, override only the absolute minimum CSS in the
  475. * Menu Theme > Custom Styling section.
  476. */
  477. $scss = file_get_contents( MEGAMENU_PATH . trailingslashit('css') . 'mixin.scss' );
  478. $scss .= file_get_contents( MEGAMENU_PATH . trailingslashit('css') . 'reset.scss' );
  479. $locations = $this->get_possible_scss_file_locations();
  480. foreach ( $locations as $path ) {
  481. if ( file_exists( $path ) ) {
  482. $scss .= file_get_contents( $path );
  483. //break;
  484. //
  485. // @todo: add a break here. This is a known bug but some users may be relying on it.
  486. // Add warning message to plugin to alert users about not using custom megamenu.scss files
  487. // then fix the bug in a later release.
  488. }
  489. }
  490. return apply_filters( "megamenu_load_scss_file_contents", $scss);
  491. }
  492. public function test_theme_compilation($theme) {
  493. $menu_id = 0;
  494. $menus = get_registered_nav_menus();
  495. if ( count( $menus ) ) {
  496. $locations = get_nav_menu_locations();
  497. foreach ($menus as $location => $description ) {
  498. if ( isset( $locations[ $location ] ) ) {
  499. $menu_id = $locations[ $location ];
  500. continue;
  501. }
  502. }
  503. }
  504. return $this->generate_css_for_location( 'test', $theme, $menu_id );
  505. }
  506. /**
  507. * Compiles raw SCSS into CSS for a particular menu location.
  508. *
  509. * @since 1.3
  510. * @return mixed
  511. * @param array $settings
  512. * @param string $location
  513. */
  514. public function generate_css_for_location( $location, $theme, $menu_id ) {
  515. $scssc = new scssc();
  516. $scssc->setFormatter( 'scss_formatter' );
  517. $import_paths = apply_filters('megamenu_scss_import_paths', array(
  518. trailingslashit( get_stylesheet_directory() ) . trailingslashit("megamenu"),
  519. trailingslashit( get_stylesheet_directory() ),
  520. trailingslashit( get_template_directory() ) . trailingslashit("megamenu"),
  521. trailingslashit( get_template_directory() ),
  522. trailingslashit( WP_PLUGIN_DIR )
  523. ));
  524. foreach ( $import_paths as $path ) {
  525. $scssc->addImportPath( $path );
  526. }
  527. try {
  528. return $scssc->compile( $this->get_complete_scss_for_location( $location, $theme, $menu_id ) );
  529. }
  530. catch ( Exception $e ) {
  531. $message = __("Warning: CSS compilation failed. Please check your changes or revert the theme.", "megamenu");
  532. return new WP_Error( 'scss_compile_fail', $message . "<br /><br />" . $e->getMessage() );
  533. }
  534. }
  535. /**
  536. * Generates a SCSS string which includes the variables for a menu theme,
  537. * for a particular menu location.
  538. *
  539. * @since 1.3
  540. * @return string
  541. * @param string $theme
  542. * @param string $location
  543. * @param int $menu_id
  544. */
  545. private function get_complete_scss_for_location( $location, $theme, $menu_id ) {
  546. $sanitized_location = str_replace( apply_filters("megamenu_location_replacements", array("-", " ") ), "-", $location );
  547. $wrap_selector = apply_filters( "megamenu_scss_wrap_selector", "#mega-menu-wrap-{$sanitized_location}", $menu_id, $location );
  548. $menu_selector = apply_filters( "megamenu_scss_menu_selector", "#mega-menu-{$sanitized_location}", $menu_id, $location );
  549. $vars['wrap'] = "'$wrap_selector'";
  550. $vars['menu'] = "'$menu_selector'";
  551. $vars['location'] = "'$sanitized_location'";
  552. $vars['menu_id'] = "'$menu_id'";
  553. $settings = $this->get_menu_settings_for_location( $location );
  554. if ( isset( $settings['effect_speed'] ) && intval( $settings['effect_speed'] ) > 0 ) {
  555. $effect_speed = intval( $settings['effect_speed'] ) . 'ms';
  556. } else {
  557. $effect_speed = '200ms';
  558. }
  559. $vars['effect_speed'] = $effect_speed;
  560. foreach( $theme as $name => $value ) {
  561. if ( in_array( $name, array( 'arrow_up', 'arrow_down', 'arrow_left', 'arrow_right' ) ) ) {
  562. $parts = explode( '-', $value );
  563. $code = end( $parts );
  564. $arrow_icon = $code == 'disabled' ? "''" : "'\\" . $code . "'";
  565. $vars[$name] = $arrow_icon;
  566. continue;
  567. }
  568. if ( in_array( $name, array( 'responsive_text' ) ) ) {
  569. if ( strlen( $value ) ) {
  570. $vars[$name] = "'" . do_shortcode( $value ) . "'";
  571. } else {
  572. $vars[$name] = "''";
  573. }
  574. continue;
  575. }
  576. if ( in_array( $name, array( 'panel_width', 'panel_inner_width', 'mobile_menu_force_width_selector' ) ) ) {
  577. if ( preg_match('/^\d/', $value) !== 1 ) { // doesn't start with number (jQuery selector)
  578. $vars[$name] = '100%';
  579. continue;
  580. }
  581. }
  582. if ( $name != 'custom_css' ) {
  583. $vars[$name] = $value;
  584. }
  585. }
  586. // Non-standard characters in the title will break CSS compilation, unset it here as it's not needed.
  587. if ( isset( $vars['title'] ) ) {
  588. unset( $vars['title'] );
  589. }
  590. $vars = apply_filters( "megamenu_scss_variables", $vars, $location, $theme, $menu_id, $this->get_theme_id_for_location($location) );
  591. $scss = "";
  592. foreach ($vars as $name => $value) {
  593. $scss .= "$" . $name . ": " . $value . ";\n";
  594. }
  595. $scss .= $this->load_scss_file();
  596. $scss .= stripslashes( html_entity_decode( $theme['custom_css'], ENT_QUOTES ) );
  597. return apply_filters( "megamenu_scss", $scss, $location, $theme, $menu_id );
  598. }
  599. /**
  600. * Returns the menu ID for a specified menu location, defaults to 0
  601. *
  602. * @since 1.3
  603. */
  604. private function get_menu_id_for_location( $location ) {
  605. $locations = get_nav_menu_locations();
  606. $menu_id = isset( $locations[ $location ] ) ? $locations[ $location ] : 0;
  607. return $menu_id;
  608. }
  609. /**
  610. * Returns the theme ID for a specified menu location, defaults to 'default'
  611. *
  612. * @since 2.1
  613. */
  614. private function get_theme_id_for_location( $location ) {
  615. $settings = $this->settings;
  616. $theme_id = isset( $settings[ $location ]['theme'] ) ? $settings[ $location ]['theme'] : 'default';
  617. return $theme_id;
  618. }
  619. /**
  620. * Returns the theme settings for a specified location. Defaults to the default theme.
  621. *
  622. * @since 1.3
  623. */
  624. private function get_theme_settings_for_location( $location ) {
  625. $theme_id = $this->get_theme_id_for_location( $location );
  626. $all_themes = $this->get_themes();
  627. $theme_settings = isset( $all_themes[ $theme_id ] ) ? $all_themes[ $theme_id ] : $all_themes[ 'default' ];
  628. return $theme_settings;
  629. }
  630. /**
  631. * Enqueue public CSS and JS files required by Mega Menu
  632. *
  633. * @since 1.0
  634. */
  635. public function enqueue_styles() {
  636. if ( $this->get_css_output_method() == 'fs' ) {
  637. $this->enqueue_fs_style();
  638. }
  639. wp_enqueue_style( 'dashicons' );
  640. do_action( 'megamenu_enqueue_public_scripts' );
  641. }
  642. /**
  643. * Enqueue public CSS and JS files required by Mega Menu
  644. *
  645. * @since 1.0
  646. */
  647. public function enqueue_scripts() {
  648. wp_enqueue_script( 'hoverIntent' );
  649. $js_path = MEGAMENU_BASE_URL . "js/maxmegamenu.js";
  650. $dependencies = apply_filters("megamenu_javascript_dependencies", array('jquery', 'hoverIntent'));
  651. wp_enqueue_script( 'megamenu', $js_path, $dependencies, MEGAMENU_VERSION, true );
  652. $params = apply_filters("megamenu_javascript_localisation",
  653. array(
  654. "timeout" => 300,
  655. "interval" => 100
  656. )
  657. );
  658. wp_localize_script( 'megamenu', 'megamenu', $params );
  659. }
  660. /**
  661. * Returns the menu settings for a specified location.
  662. *
  663. * @since 2.2
  664. */
  665. private function get_menu_settings_for_location( $location ) {
  666. $settings = $this->settings;
  667. $location_settings = isset( $settings[ $location ] ) ? $settings[ $location ] : array();
  668. return $location_settings;
  669. }
  670. /**
  671. * Enqueue the stylesheet held on the filesystem.
  672. *
  673. * @since 1.6.1
  674. */
  675. private function enqueue_fs_style() {
  676. $upload_dir = wp_upload_dir();
  677. $filename = $this->get_css_filename();
  678. $filepath = trailingslashit( $upload_dir['basedir'] ) . 'maxmegamenu/' . $filename;
  679. if ( ! is_file( $filepath ) || $this->is_debug_mode()) {
  680. // regenerate the CSS and save to filesystem
  681. $this->generate_css();
  682. }
  683. // file should now exist
  684. if ( is_file( $filepath ) ) {
  685. $css_url = trailingslashit( $upload_dir['baseurl'] ) . 'maxmegamenu/' . $filename;
  686. $protocol = is_ssl() ? 'https://' : 'http://';
  687. // ensure we're using the correct protocol
  688. $css_url = str_replace( array( "http://", "https://" ), $protocol, $css_url );
  689. wp_enqueue_style( 'megamenu', $css_url, false, substr( md5( filemtime( $filepath ) ), 0, 6 ) );
  690. }
  691. }
  692. /**
  693. *
  694. * @since 1.6.1
  695. */
  696. private function set_cached_css( $css ) {
  697. set_transient( $this->get_transient_key(), $css, 0 );
  698. set_transient( 'megamenu_css_version', MEGAMENU_VERSION, 0 );
  699. }
  700. /**
  701. * Return the cached css if it exists
  702. *
  703. * @since 1.9
  704. * @return mixed
  705. */
  706. private function get_cached_css() {
  707. return get_transient( $this->get_transient_key() );
  708. }
  709. /**
  710. * Delete the cached CSS
  711. *
  712. * @since 1.9
  713. * @return mixed
  714. */
  715. public function delete_cache() {
  716. global $wp_filesystem;
  717. if ( ! $wp_filesystem ) {
  718. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  719. }
  720. $upload_dir = wp_upload_dir();
  721. $filename = $this->get_css_filename();
  722. $dir = trailingslashit( $upload_dir['basedir'] ) . 'maxmegamenu/';
  723. WP_Filesystem( false, $upload_dir['basedir'], true );
  724. $wp_filesystem->rmdir( $dir, true );
  725. delete_transient( $this->get_transient_key() );
  726. $this->generate_css();
  727. do_action( "megamenu_after_delete_cache" );
  728. return true;
  729. }
  730. /**
  731. * Return the key to use for the CSS transient
  732. *
  733. * @since 1.9
  734. * @return string
  735. */
  736. private function get_transient_key() {
  737. return apply_filters( 'megamenu_css_transient_key', 'megamenu_css' );
  738. }
  739. /**
  740. * Return the filename to use for the stylesheet, ensuring the filename is unique
  741. * for multi site setups
  742. *
  743. * @since 1.6.1
  744. */
  745. private function get_css_filename() {
  746. return apply_filters( "megamenu_css_filename", 'style' ) . '.css';
  747. }
  748. /**
  749. * Return the CSS output method, default to filesystem
  750. *
  751. * @return string
  752. */
  753. private function get_css_output_method() {
  754. return isset( $this->settings['css'] ) ? $this->settings['css'] : 'fs';
  755. }
  756. /**
  757. * Print CSS to <head>
  758. *
  759. * @since 1.3.1
  760. */
  761. public function head_css() {
  762. $method = $this->get_css_output_method();
  763. if ( in_array( $method, array( 'disabled', 'fs' ) ) ) {
  764. echo "<style type=\"text/css\">/** Mega Menu CSS: {$method} **/</style>\n";
  765. return;
  766. }
  767. $css = $this->get_css();
  768. echo '<style type="text/css">' . str_replace( array( " ", "\n" ), '', $css ) . "</style>\n";
  769. }
  770. /**
  771. * Delete language specific transients created when PolyLang is installed
  772. *
  773. * @since 1.9
  774. */
  775. public function polylang_delete_cache() {
  776. global $polylang;
  777. foreach ( $polylang->model->get_languages_list() as $term ) {
  778. delete_transient( 'megamenu_css_' . $term->locale );
  779. }
  780. }
  781. /**
  782. * Modify the CSS transient key to make it unique to the current language
  783. *
  784. * @since 1.9
  785. * @return string
  786. */
  787. public function polylang_transient_key( $key ) {
  788. $locale = strtolower( pll_current_language('locale') );
  789. if ( strlen( $locale ) ) {
  790. $key = $key . "_" . $locale;
  791. }
  792. return $key;
  793. }
  794. /**
  795. * Modify the CSS filename to make it unique to the current language
  796. *
  797. * @since 1.9
  798. * @return string
  799. */
  800. public function polylang_css_filename( $filename ) {
  801. $locale = strtolower( pll_current_language('locale') );
  802. if ( strlen( $locale ) ) {
  803. $filename .= "_" . $locale;
  804. }
  805. return $filename;
  806. }
  807. /**
  808. * Delete language specific transients created when WPML is installed
  809. *
  810. * @since 1.9
  811. */
  812. public function wpml_delete_cache() {
  813. $languages = icl_get_languages('skip_missing=N');
  814. foreach ( $languages as $language ) {
  815. delete_transient( 'megamenu_css_' . $language['language_code'] );
  816. }
  817. }
  818. /**
  819. * Modify the CSS transient key to make it unique to the current language
  820. *
  821. * @since 1.9
  822. * @return string
  823. */
  824. public function wpml_transient_key( $key ) {
  825. $key .= "_" . ICL_LANGUAGE_CODE;
  826. return $key;
  827. }
  828. /**
  829. * Modify the CSS filename to make it unique to the current language
  830. *
  831. * @since 1.9
  832. * @return string
  833. */
  834. public function wpml_css_filename( $filename ) {
  835. $filename .= "_" . ICL_LANGUAGE_CODE;
  836. return $filename;
  837. }
  838. }
  839. endif;