PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/suppamenu/standard/include/class-suppa_walkers.php

https://gitlab.com/thisishayat/itv-2016
PHP | 517 lines | 339 code | 77 blank | 101 comment | 24 complexity | bce4dae9d1bf781dda69c1f8818acf09 MD5 | raw file
  1. <?php
  2. /**
  3. * This file holds various classes and methods necessary to edit the wordpress menu.
  4. *
  5. * @package CTFramework
  6. * @author Sabri Taieb ( codezag )
  7. * @copyright Copyright (c) Sabri Taieb
  8. * @link http://vamospace.com
  9. * @since Version 1.0
  10. *
  11. */
  12. /**
  13. * This class contains various methods necessary to create mega menus in the backend
  14. * @package CTFramework
  15. *
  16. */
  17. if( !class_exists( 'suppa_walkers' ) ){
  18. class suppa_walkers{
  19. static $project_settings;
  20. static $offline_db;
  21. /**
  22. * Actions/Filters
  23. * @package CTFramework
  24. */
  25. public function init( $project_settings , $offline_db ){
  26. /** Variables **/
  27. self::$project_settings = $project_settings;
  28. self::$offline_db = $offline_db;
  29. if( count( self::$offline_db ) == 0 ){
  30. self::$offline_db['settings-responsive_text'] = 'Menu';
  31. self::$offline_db['logo_enable'] = 'off';
  32. self::$offline_db['rwd_logo_enable'] = 'off';
  33. self::$offline_db['submenu-megaposts-post_width'] = '200px';
  34. self::$offline_db['submenu-megaposts-post_height'] = '160px';
  35. self::$offline_db['posts_img_effect'] = 'none';
  36. self::$offline_db['submenu-posts-post_width'] = '200px';
  37. self::$offline_db['submenu-posts-post_height'] = '160px';
  38. self::$offline_db['latest_posts_view_all'] = 'View All...';
  39. }
  40. /** Load style & javascript to admin : nav-menus.php only **/
  41. add_action('admin_menu', array($this,'load_menus_css') , 9);
  42. add_action( 'admin_print_styles-nav-menus.php', array( $this , 'load_menus_js' ) , 3000 );
  43. /** Replace the selected menu args **/
  44. add_filter( 'wp_nav_menu_args', array( $this,'replace_args'), 3000);
  45. /** add new options to the walker **/
  46. add_filter( 'wp_edit_nav_menu_walker', array( $this,'replace_backend_walker') , 3000 );
  47. /** save suppa menu new options **/
  48. add_action( 'wp_update_nav_menu_item', array( $this,'update_menu'), 101, 3);
  49. /** Add WP Edior & Font Awesome Widgets on the Footer **/
  50. add_action( 'admin_footer', array( $this , 'add_widgets' ) );
  51. /** Ajax : Save Menu Location **/
  52. add_action( 'wp_ajax_save_locations_skins' , array( $this , 'save_locations_skins' ) );
  53. add_action( 'admin_head', array( $this , 'add_accordion_metabox' ) );
  54. /** Swith To Suppa Walker Axtion **/
  55. add_action( 'wp_ajax_suppa_switch_menu_walker' , array( $this , 'switch_menu_walker' ) );
  56. }
  57. /**
  58. *
  59. * Add JS & CSS only on nav-menus.php
  60. * @package CTFramework
  61. *
  62. */
  63. function load_menus_css( )
  64. {
  65. if( basename( $_SERVER['PHP_SELF'] ) == "nav-menus.php" )
  66. {
  67. wp_enqueue_style ( 'suppa_menu_admin_menu_css', plugins_url('../css/' , __FILE__ ). 'suppa_admin_menus.css');
  68. wp_enqueue_style ( 'suppa_menu_admin_fontAwesome', plugins_url('../css/fontAwesome/' , __FILE__ ). 'style.css');
  69. }
  70. }
  71. function load_menus_js( )
  72. {
  73. if( basename( $_SERVER['PHP_SELF'] ) == "nav-menus.php" )
  74. {
  75. // WP 3.5+
  76. // Enqueue Media uploader scripts and environment [ wp_enqueue_media() ].
  77. // Strongly suggest to use this function on the admin_enqueue_scripts action hook. Using it on admin_init hook breaks it
  78. // How To : http://stackoverflow.com/questions/13847714/wordpress-3-5-custom-media-upload-for-your-theme-options
  79. // Don't Foooooooooooooooooorget to array('jquery' , 'media-upload' , 'thickbox') to the enqueue
  80. wp_enqueue_media();
  81. wp_enqueue_script( 'suppa_menu_admin_js' , plugins_url('../js/' , __FILE__ ).'suppa_admin.js', array('jquery' , 'media-upload' , 'thickbox' , 'jquery-ui-core' , 'jquery-ui-draggable' , 'jquery-ui-droppable' , 'jquery-ui-sortable' ), '1.0.0', true );
  82. }
  83. }
  84. /**
  85. *
  86. * Add Select Location Meta Box
  87. * @package CTFramework
  88. *
  89. */
  90. function add_accordion_metabox()
  91. {
  92. add_meta_box( 'nav-menu-theme-suppa-location', __( 'SuppaMenu Locations & Skins' , 'suppa_menu' ), array( $this , 'display_select_location_meta_box' ) , 'nav-menus', 'side', 'high' );
  93. }
  94. /**
  95. *
  96. * Select Location Meta Box Render
  97. * @package CTFramework
  98. *
  99. */
  100. function display_select_location_meta_box()
  101. {
  102. // Get Settings
  103. $settings = array();
  104. // Add Accordion Menu Locations
  105. $menu_locations = get_registered_nav_menus();
  106. $menus = get_terms('nav_menu');
  107. $all_skins = get_option('suppa_all_skins');
  108. if( get_option('suppa_locations_skins') )
  109. {
  110. $saved_locations = get_option('suppa_locations_skins');
  111. echo '
  112. <p>
  113. <div id="suppa_menu_location_selected">
  114. ';
  115. foreach ($menu_locations as $key => $value)
  116. {
  117. if( array_key_exists($key, $saved_locations) )
  118. {
  119. echo '
  120. <div>
  121. <input type="checkbox" value="'.$key.'" checked >&nbsp;&nbsp;&nbsp;'.$value.'
  122. <br/>
  123. '.__('Select skin : ').' <select class="suppa_menu_location_skin">';
  124. foreach ( $all_skins as $skin => $skin_n )
  125. {
  126. if( $skin == $saved_locations[$key] )
  127. echo '<option selected="selected">'.$skin.'</option>';
  128. else
  129. echo '<option >'.$skin.'</option>';
  130. }
  131. echo '
  132. </select>
  133. </div>';
  134. }
  135. else
  136. {
  137. echo '<div>
  138. <input type="checkbox" value="'.$key.'" >&nbsp;&nbsp;&nbsp;'.$value.
  139. '<br/>
  140. '.__('Select skin : ').' <select class="suppa_menu_location_skin">';
  141. foreach ( $all_skins as $skin )
  142. {
  143. if( $skin == $saved_locations[$key] )
  144. echo '<option selected="selected">'.$skin.'</option>';
  145. else
  146. echo '<option >'.$skin.'</option>';
  147. }
  148. echo '
  149. </select>
  150. </div>';
  151. }
  152. echo '<br/>';
  153. }
  154. echo '
  155. </div><!--suppa_menu_location_selected-->
  156. </p>';
  157. }
  158. else
  159. {
  160. echo '
  161. <p>
  162. <div id="suppa_menu_location_selected">
  163. ';
  164. foreach ($menu_locations as $key => $value)
  165. {
  166. echo '
  167. <div>
  168. <input type="checkbox" value="'.$key.'" >&nbsp;&nbsp;&nbsp;'.$value;
  169. echo
  170. '<br/>
  171. '.__('Select skin : ').'<select class="suppa_menu_location_skin">';
  172. foreach ( $all_skins as $skin )
  173. {
  174. echo '<option >'.$skin.'</option>';
  175. }
  176. echo '
  177. </select>
  178. </div><br/>';
  179. }
  180. echo '
  181. </div><!--suppa_menu_location_selected-->
  182. </p>';
  183. }
  184. echo '
  185. <p>
  186. <span>
  187. <input type="submit" class="button-primary" value="Save" id="admin_suppa_save_menu_location">
  188. <input type="hidden" value="'.wp_create_nonce("suppa_menu_location_nonce").'" id="admin_suppa_save_menu_location_nonce">
  189. </span>
  190. </p>
  191. <br/><br/>
  192. ';
  193. }
  194. /**
  195. *
  196. * Add WP Edior & Font Awesome Widgets on the Footer
  197. * @package CTFramework
  198. *
  199. */
  200. function add_widgets()
  201. {
  202. global $fontAwesome;
  203. if( basename( $_SERVER['PHP_SELF'] ) == "nav-menus.php" ){
  204. // Add Widgets
  205. echo '
  206. <input type="hidden" id="admin_suppa_plugin_url" value="'.plugins_url( '../js/tinymce/' , __FILE__ ).'" />
  207. <div class="era_admin_widgets_container" >
  208. <div class="era_admin_widget_box suppa_wp_editor_container" >
  209. <div class="era_admin_widget_box_header">
  210. <span>WP Editor</span>
  211. <a>x</a>
  212. </div>
  213. <div class="era_admin_widget_box_inside" >
  214. ';
  215. wp_editor( '', 'suppa_wp_editor_the_editor', $settings = array() );
  216. echo '
  217. <div class="admin_suppa_clearfix"></div>
  218. </div>
  219. <div class="era_admin_widget_box_footer">
  220. <button class="era_admin_widgets_container_button admin_suppa_getContent_button">Add</button>
  221. </div>
  222. </div>
  223. ';
  224. echo '
  225. <div class="era_admin_widget_box suppa_fontAwesome_container" >
  226. <div class="era_admin_widget_box_header">
  227. <span>Select an Icon</span>
  228. <a>x</a>
  229. </div>
  230. <div class="era_admin_widget_box_inside" >';
  231. foreach ( $fontAwesome as $icon )
  232. {
  233. echo '
  234. <span class="admin_suppa_fontAwesome_icon_box">
  235. <span aria-hidden="true" class="'.$icon.'"></span>
  236. </span>
  237. ';
  238. }
  239. echo '
  240. <div class="admin_suppa_clearfix"></div>
  241. </div>
  242. <div class="era_admin_widget_box_footer">
  243. <button class="era_admin_widgets_container_button admin_suppa_addIcon_button">Add</button>
  244. </div>
  245. </div>
  246. </div>';
  247. }
  248. }
  249. /**
  250. *
  251. * After plugin install create css js files
  252. * @package CTFramework
  253. *
  254. */
  255. function after_plugin_install_create_css_js_files(){
  256. // Save Locations & Create Skins
  257. $locations = get_option( 'suppa_locations_skins' );
  258. foreach ( $locations as $loc => $skin ){
  259. do_action( 'CTF_suppa_menu_after_db_save', array( $loc , $skin ) );
  260. }
  261. // Save Thumbnail sizes
  262. do_action( 'CTF_suppa_menu_save_thumb_sizes' );
  263. }
  264. /**
  265. *
  266. * Ajax : Save Menu Location
  267. * @package CTFramework
  268. *
  269. */
  270. function save_locations_skins()
  271. {
  272. check_ajax_referer( 'suppa_menu_location_nonce', 'nonce' );
  273. // Save Locations & Create Skins
  274. $locations = $_POST['location'];
  275. $locations = explode(",",$locations);
  276. $loc_arr = array();
  277. foreach ( $locations as $loc )
  278. {
  279. $loc_inf = explode("=",$loc);
  280. $loc_arr[ $loc_inf[0] ] = $loc_inf[1];
  281. do_action( 'CTF_suppa_menu_after_db_save', array( $loc_inf[0] , $loc_inf[1] ) );
  282. }
  283. update_option('suppa_locations_skins', $loc_arr );
  284. // Save Thumbnail sizes
  285. do_action( 'CTF_suppa_menu_save_thumb_sizes' );
  286. die( __("Menus & Skins Ready !","suppa_menu") );
  287. }
  288. /**
  289. *
  290. * Replace the selected menu args
  291. * @package CTFramework
  292. *
  293. */
  294. function replace_args($args){
  295. if( get_option('suppa_locations_skins') ){
  296. /** RWD **/
  297. $rwd_menu_text = self::$offline_db['settings-responsive_text'];
  298. if( function_exists('icl_t') ){
  299. $rwd_menu_text = icl_t('admin_texts_plugin_CTF_suppa_menu', '[CTF_suppa_menu__group__settings]settings-responsive_text', 'Menu');
  300. }
  301. $rwd_wrap = '
  302. <div class="suppaMenu_rwd_wrap" >
  303. <div class="suppa_rwd_top_button_container">
  304. <span class="suppa_rwd_button"><span aria-hidden="true" class="suppa-reorder"></span></span>
  305. <span class="suppa_rwd_text">' . $rwd_menu_text . '</span>
  306. </div>
  307. <div class="suppa_rwd_menus_container" ></div>
  308. </div>
  309. ';
  310. $saved_locations = get_option('suppa_locations_skins');
  311. foreach ( $saved_locations as $location => $skin){
  312. if( $args['theme_location'] == $location ){
  313. // Get Skin Options
  314. $skin_options = get_option('suppamenu_skin_'.$skin);
  315. // Get Thumbs Sizes
  316. $thumbs = get_option('suppa_thumbs_sizes');
  317. $thumbs = $thumbs[$skin];
  318. $args['walker'] = new suppa_menu_walker( $skin_options, $thumbs );
  319. $args['container_class'] = $args['theme_location'].' suppaMenu_wrap';
  320. $args['menu_class'] = 'suppaMenu';
  321. $args['items_wrap'] = '<div id="%1$s" class="%2$s">%3$s</div>' . $rwd_wrap;
  322. $args['depth'] = 4;
  323. $args['container'] = 'div';
  324. }// End If
  325. }// End Foreach
  326. }// End If
  327. return $args;
  328. }
  329. /**
  330. *
  331. * Tells wordpress to use our backend walker instead of the default one
  332. * @package CTFramework
  333. *
  334. */
  335. function replace_backend_walker($name)
  336. {
  337. return 'suppa_menu_backend_walker';
  338. }
  339. /*
  340. * Save and Update the Custom Navigation Menu Item Properties by checking all $_POST vars with the name of $check
  341. * @param int $menu_id
  342. * @param int $menu_item_db
  343. */
  344. function update_menu($menu_id, $menu_item_db)
  345. {
  346. $all_keys = array(
  347. 'menu_type' ,
  348. 'dropdown_width',
  349. 'logo_image',
  350. 'logo_image_retina',
  351. 'links_fullwidth',
  352. 'links_width',
  353. 'links_align',
  354. 'links_column_width',
  355. 'link_icon_only',
  356. 'html_fullwidth',
  357. 'html_width',
  358. 'html_align',
  359. 'html_content',
  360. 'link_position',
  361. 'link_icon_type',
  362. 'link_icon_image',
  363. 'link_icon_image_hover',
  364. 'link_icon_image',
  365. 'link_icon_fontawesome',
  366. 'link_icon_fontawesome_size',
  367. 'posts_taxonomy',
  368. 'posts_category',
  369. 'posts_number',
  370. 'search_text',
  371. 'logo_image_height',
  372. 'logo_image_width',
  373. 'link_icon_image_height',
  374. 'link_icon_image_width',
  375. 'link_user_logged',
  376. 'mega_posts_category',
  377. 'mega_posts_taxonomy',
  378. 'mega_posts_number',
  379. 'linksTwoSubmenuWidth',
  380. 'linksTwoSubmenuAlign',
  381. 'linksTwo_categoriesWidth',
  382. 'dropdown_open_pos'
  383. );
  384. foreach ( $all_keys as $key )
  385. {
  386. if(!isset($_POST['menu-item-suppa-'.$key][$menu_item_db]))
  387. {
  388. $_POST['menu-item-suppa-'.$key][$menu_item_db] = "";
  389. }
  390. $value = $_POST['menu-item-suppa-'.$key][$menu_item_db];
  391. update_post_meta( $menu_item_db, '_menu-item-suppa-'.$key, $value );
  392. }
  393. }
  394. /**
  395. * Very Important .
  396. * Replace The WP add menu item by AJAX
  397. * This Will be remove when they add an action in the future
  398. *
  399. */
  400. public function switch_menu_walker()
  401. {
  402. if ( ! current_user_can( 'edit_theme_options' ) )
  403. die('-1');
  404. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' );
  405. require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
  406. $item_ids = wp_save_nav_menu_items( 0, $_POST['menu-item'] );
  407. if ( is_wp_error( $item_ids ) )
  408. die('-1');
  409. foreach ( (array) $item_ids as $menu_item_id ) {
  410. $menu_obj = get_post( $menu_item_id );
  411. if ( ! empty( $menu_obj->ID ) ) {
  412. $menu_obj = wp_setup_nav_menu_item( $menu_obj );
  413. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items
  414. $menu_items[] = $menu_obj;
  415. }
  416. }
  417. if ( ! empty( $menu_items ) ) {
  418. $args = array(
  419. 'after' => '',
  420. 'before' => '',
  421. 'link_after' => '',
  422. 'link_before' => '',
  423. 'walker' => new suppa_menu_backend_walker,
  424. );
  425. echo walk_nav_menu_tree( $menu_items, 0, (object) $args );
  426. }
  427. die('end');
  428. }
  429. }
  430. }