Advertisement
firoze

Simply hide widgets on specified pages

Jun 22nd, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.17 KB | None | 0 0
  1. // source linK:https://github.com/Strategy11/display-widgets/blob/master/display-widgets.php
  2.  
  3. plugin link:https://wordpress.org/plugins/display-widgets/
  4.  
  5. //Simply hide widgets on specified pages. Adds checkboxes to each widget to either show or hide it on every site page.
  6. display-widgets.php
  7.  
  8.  
  9. // call this into your functions.php
  10.  
  11.  
  12. global $dw_plugin;
  13. $dw_plugin = new DWPlugin();
  14.  
  15. class DWPlugin{
  16. var $transient_name = 'dw_details';
  17. var $checked = array();
  18. var $id_base = '';
  19. var $number = '';
  20.  
  21. // pages on site
  22. var $pages = array();
  23.  
  24. // custom post types
  25. var $cposts = array();
  26.  
  27. // taxonomies
  28. var $taxes = array();
  29.  
  30. // categories
  31. var $cats = array();
  32.  
  33. // WPML languages
  34. var $langs = array();
  35.  
  36. function __construct(){
  37. //add_filter('widget_display_callback', array(&$this, 'show_widget'));
  38.  
  39. // change the hook that triggers widget check
  40. $hook = apply_filters('dw_callback_trigger', 'wp_loaded');
  41.  
  42. add_filter($hook, array(&$this, 'trigger_widget_checks'));
  43. add_action('in_widget_form', array(&$this, 'hidden_widget_options'), 10, 3);
  44. add_filter('widget_update_callback', array(&$this, 'update_widget_options'), 10, 3);
  45. add_action('wp_ajax_dw_show_widget', array(&$this, 'show_widget_options'));
  46. add_action('admin_footer', array(&$this, 'load_js'));
  47.  
  48. // when a page is saved
  49. add_action('save_post_page', array(&$this, 'delete_transient'));
  50.  
  51. // when a new category/taxonomy is created
  52. add_action('created_term', array(&$this, 'delete_transient'));
  53.  
  54. // when a custom post type is added
  55. add_action('update_option_rewrite_rules', array(&$this, 'delete_transient'));
  56.  
  57. // reset transient after activating the plugin
  58. register_activation_hook(dirname(__FILE__) .'/display-widgets.php', array(&$this, 'delete_transient'));
  59.  
  60. add_action('plugins_loaded', array(&$this, 'load_lang'));
  61. }
  62.  
  63. function trigger_widget_checks() {
  64. add_filter('sidebars_widgets', array(&$this, 'sidebars_widgets'));
  65. }
  66.  
  67. function show_widget($instance) {
  68. $post_id = get_queried_object_id();
  69. $post_id = self::get_lang_id($post_id, 'page');
  70.  
  71. if ( is_home() ) {
  72. $show = isset($instance['page-home']) ? $instance['page-home'] : false;
  73. if ( !$show && $post_id ){
  74. $show = isset($instance['page-'. $post_id]) ? $instance['page-'. $post_id] : false;
  75. }
  76.  
  77. // check if blog page is front page too
  78. if ( !$show && is_front_page() && isset($instance['page-front']) ) {
  79. $show = $instance['page-front'];
  80. }
  81. } else if ( is_front_page() ) {
  82. $show = isset($instance['page-front']) ? $instance['page-front'] : false;
  83. if ( !$show && $post_id ) {
  84. $show = isset($instance['page-'. $post_id]) ? $instance['page-'. $post_id] : false;
  85. }
  86. } else if ( is_category() ) {
  87. $show = isset($instance['cat-'. get_query_var('cat')]) ? $instance['cat-'. get_query_var('cat')] : false;
  88. } else if ( is_tax() ) {
  89. $term = get_queried_object();
  90. $show = isset($instance['tax-'. $term->taxonomy]) ? $instance['tax-'. $term->taxonomy] : false;
  91. unset($term);
  92. } else if ( is_post_type_archive() ) {
  93. $type = get_post_type();
  94. $show = isset($instance['type-'. $type .'-archive']) ? $instance['type-'. $type .'-archive'] : false;
  95. } else if ( is_archive() ) {
  96. $show = isset($instance['page-archive']) ? $instance['page-archive'] : false;
  97. } else if ( is_single() ) {
  98. $type = get_post_type();
  99. if ( $type != 'page' && $type != 'post' ) {
  100. $show = isset($instance['type-'. $type]) ? $instance['type-'. $type] : false;
  101. }
  102.  
  103. if ( !isset($show) ) {
  104. $show = isset($instance['page-single']) ? $instance['page-single'] : false;
  105. }
  106.  
  107. if ( !$show ) {
  108. $cats = get_the_category();
  109. foreach ( $cats as $cat ) {
  110. if ($show) break;
  111. $c_id = self::get_lang_id($cat->cat_ID, 'category');
  112. if ( isset($instance['cat-'. $c_id]) ) {
  113. $show = $instance['cat-'. $c_id];
  114. }
  115. unset($c_id);
  116. unset($cat);
  117. }
  118. }
  119.  
  120. } else if ( is_404() ) {
  121. $show = isset($instance['page-404']) ? $instance['page-404'] : false;
  122. } else if ( is_search() ) {
  123. $show = isset($instance['page-search']) ? $instance['page-search'] : false;
  124. } else if ( $post_id ) {
  125. $show = isset($instance['page-'. $post_id]) ? $instance['page-'. $post_id] : false;
  126. } else {
  127. $show = false;
  128. }
  129.  
  130. if ( $post_id && !$show && isset($instance['other_ids']) && !empty($instance['other_ids']) ) {
  131. $other_ids = explode(',', $instance['other_ids']);
  132. foreach ( $other_ids as $other_id ) {
  133. if ( $post_id == (int) $other_id ) {
  134. $show = true;
  135. }
  136. }
  137. }
  138.  
  139. if ( !$show && defined('ICL_LANGUAGE_CODE') ) {
  140. // check for WPML widgets
  141. $show = isset($instance['lang-'. ICL_LANGUAGE_CODE]) ? $instance['lang-'. ICL_LANGUAGE_CODE] : false;
  142. }
  143.  
  144. if ( !isset($show) ) {
  145. $show = false;
  146. }
  147.  
  148. $instance['dw_include'] = isset($instance['dw_include']) ? $instance['dw_include'] : 0;
  149. $instance['dw_logged'] = self::show_logged($instance);
  150.  
  151. if ( ( $instance['dw_include'] && false == $show ) || ( 0 == $instance['dw_include'] && $show ) ) {
  152. return false;
  153. } else {
  154. $user_ID = is_user_logged_in();
  155. if ( ( 'out' == $instance['dw_logged'] && $user_ID ) ||
  156. ( 'in' == $instance['dw_logged'] && !$user_ID ) ) {
  157. return false;
  158. }
  159.  
  160. }
  161.  
  162. return $instance;
  163. }
  164.  
  165. function sidebars_widgets($sidebars) {
  166. if ( is_admin() ) {
  167. return $sidebars;
  168. }
  169.  
  170. global $wp_registered_widgets;
  171.  
  172. foreach ( $sidebars as $s => $sidebar ) {
  173. if ( $s == 'wp_inactive_widgets' || strpos($s, 'orphaned_widgets') === 0 || empty($sidebar) ) {
  174. continue;
  175. }
  176.  
  177. foreach ( $sidebar as $w => $widget ) {
  178. // $widget is the id of the widget
  179. if ( !isset($wp_registered_widgets[$widget]) ) {
  180. continue;
  181. }
  182.  
  183. if ( isset($this->checked[$widget]) ) {
  184. $show = $this->checked[$widget];
  185. } else {
  186. $opts = $wp_registered_widgets[$widget];
  187. $id_base = is_array($opts['callback']) ? $opts['callback'][0]->id_base : $opts['callback'];
  188.  
  189. if ( !$id_base ) {
  190. continue;
  191. }
  192.  
  193. $instance = get_option('widget_' . $id_base);
  194.  
  195. if ( !$instance || !is_array($instance) ) {
  196. continue;
  197. }
  198.  
  199. if ( isset($instance['_multiwidget']) && $instance['_multiwidget'] ) {
  200. $number = $opts['params'][0]['number'];
  201. if ( !isset($instance[$number]) ) {
  202. continue;
  203. }
  204.  
  205. $instance = $instance[$number];
  206. unset($number);
  207. }
  208.  
  209. unset($opts);
  210.  
  211. $show = self::show_widget($instance);
  212.  
  213. $this->checked[$widget] = $show ? true : false;
  214. }
  215.  
  216. if ( !$show ) {
  217. unset($sidebars[$s][$w]);
  218. }
  219.  
  220. unset($widget);
  221. }
  222. unset($sidebar);
  223. }
  224.  
  225. return $sidebars;
  226. }
  227.  
  228. function hidden_widget_options($widget, $return, $instance) {
  229. if ( $_POST && isset($_POST['id_base']) && $_POST['id_base'] == $widget->id_base ) {
  230. // widget was just saved so it's open
  231. self::show_hide_widget_options($widget, $return, $instance);
  232. return;
  233. }
  234.  
  235. self::register_globals();
  236.  
  237. $instance['dw_include'] = isset($instance['dw_include']) ? $instance['dw_include'] : 0;
  238. $instance['dw_logged'] = self::show_logged($instance);
  239. $instance['other_ids'] = isset($instance['other_ids']) ? $instance['other_ids'] : '';
  240. ?>
  241. <div class="dw_opts">
  242. <input type="hidden" name="<?php echo $widget->get_field_name('dw_include'); ?>" id="<?php echo $widget->get_field_id('dw_include'); ?>" value="<?php echo $instance['dw_include'] ?>" />
  243. <input type="hidden" id="<?php echo $widget->get_field_id('dw_logged'); ?>" name="<?php echo $widget->get_field_name('dw_logged'); ?>" value="<?php echo $instance['dw_logged'] ?>" />
  244.  
  245. <?php foreach ( $instance as $k => $v ) {
  246. if ( !$v ) {
  247. continue;
  248. }
  249.  
  250. if ( strpos($k, 'page-') === 0 || strpos($k, 'type-') === 0 || strpos($k, 'cat-') === 0 ||
  251. strpos($k, 'tax-') === 0 || strpos($k, 'lang-') === 0) {
  252. ?>
  253. <input type="hidden" id="<?php echo $widget->get_field_id($k); ?>" name="<?php echo $widget->get_field_name($k); ?>" value="<?php echo $v ?>" />
  254. <?php } ?>
  255. <?php } ?>
  256.  
  257. <input type="hidden" name="<?php echo $widget->get_field_name('other_ids'); ?>" id="<?php echo $widget->get_field_id('other_ids'); ?>" value="<?php echo esc_attr($instance['other_ids']) ?>" />
  258. </div>
  259. <?php
  260. }
  261.  
  262. function show_widget_options() {
  263. $instance = htmlspecialchars_decode(nl2br(stripslashes($_POST['opts'])));
  264. $instance = json_decode($instance, true);
  265. $this->id_base = $_POST['id_base'];
  266. $this->number = $_POST['widget_number'];
  267.  
  268. $new_instance = array();
  269. $prefix = 'widget-'. $this->id_base .'['. $this->number .'][';
  270. foreach ( $instance as $k => $v ) {
  271. $n = str_replace( array( $prefix, ']'), '', $v['name']);
  272. $new_instance[$n] = $v['value'];
  273. }
  274.  
  275. self::show_hide_widget_options($this, '', $new_instance);
  276. die();
  277. }
  278.  
  279. function show_hide_widget_options($widget, $return, $instance) {
  280. self::register_globals();
  281.  
  282. $wp_page_types = self::page_types();
  283.  
  284. $instance['dw_include'] = isset($instance['dw_include']) ? $instance['dw_include'] : 0;
  285. $instance['dw_logged'] = self::show_logged($instance);
  286. $instance['other_ids'] = isset($instance['other_ids']) ? $instance['other_ids'] : '';
  287. ?>
  288. <p>
  289. <label for="<?php echo $widget->get_field_id('dw_include'); ?>"><?php _e('Show Widget for:', 'display-widgets') ?></label>
  290. <select name="<?php echo $widget->get_field_name('dw_logged'); ?>" id="<?php echo $widget->get_field_id('dw_logged'); ?>" class="widefat">
  291. <option value=""><?php _e('Everyone', 'display-widgets') ?></option>
  292. <option value="out" <?php echo selected( $instance['dw_logged'], 'out' ) ?>><?php _e('Logged-out users', 'display-widgets') ?></option>
  293. <option value="in" <?php echo selected( $instance['dw_logged'], 'in' ) ?>><?php _e('Logged-in users', 'display-widgets') ?></option>
  294. </select>
  295. </p>
  296.  
  297. <p>
  298. <select name="<?php echo $widget->get_field_name('dw_include'); ?>" id="<?php echo $widget->get_field_id('dw_include'); ?>" class="widefat">
  299. <option value="0"><?php _e('Hide on checked pages', 'display-widgets') ?></option>
  300. <option value="1" <?php echo selected( $instance['dw_include'], 1 ) ?>><?php _e('Show on checked pages', 'display-widgets') ?></option>
  301. </select>
  302. </p>
  303.  
  304. <div style="height:150px; overflow:auto; border:1px solid #dfdfdf; padding:5px; margin-bottom:5px;">
  305. <h4 class="dw_toggle" style="cursor:pointer;margin-top:0;"><?php _e('Miscellaneous', 'display-widgets') ?> +/-</h4>
  306. <div class="dw_collapse">
  307. <?php foreach ($wp_page_types as $key => $label){
  308. $instance['page-'. $key] = isset($instance['page-'. $key]) ? $instance['page-'. $key] : false;
  309. ?>
  310. <p><input class="checkbox" type="checkbox" <?php checked($instance['page-'. $key], true) ?> id="<?php echo $widget->get_field_id('page-'. $key); ?>" name="<?php echo $widget->get_field_name('page-'. $key); ?>" />
  311. <label for="<?php echo $widget->get_field_id('page-'. $key); ?>"><?php echo $label .' '. __('Page', 'display-widgets') ?></label></p>
  312. <?php } ?>
  313. </div>
  314.  
  315. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Pages') ?> +/-</h4>
  316. <div class="dw_collapse">
  317. <?php foreach ( $this->pages as $page ) {
  318. $instance['page-'. $page->ID] = isset($instance['page-'. $page->ID]) ? $instance['page-'. $page->ID] : false;
  319. $p_title = apply_filters('the_title', $page->post_title, $page->ID);
  320. if ( $page->post_parent ) {
  321. $parent = get_post($page->post_parent);
  322. $p_title .= ' ('. apply_filters('the_title', $parent->post_title, $parent->ID);
  323. if ( $parent->post_parent ) {
  324. $grandparent = get_post($parent->post_parent);
  325. $p_title .= ' - '. apply_filters('the_title', $grandparent->post_title, $grandparent->ID);
  326. unset($grandparent);
  327. }
  328. $p_title .= ')';
  329. unset($parent);
  330. }
  331.  
  332. ?>
  333. <p><input class="checkbox" type="checkbox" <?php checked($instance['page-'. $page->ID], true) ?> id="<?php echo $widget->get_field_id('page-'. $page->ID); ?>" name="<?php echo $widget->get_field_name('page-'. $page->ID); ?>" />
  334. <label for="<?php echo $widget->get_field_id('page-'. $page->ID); ?>"><?php echo $p_title ?></label></p>
  335. <?php unset($p_title);
  336. } ?>
  337. </div>
  338.  
  339. <?php if ( !empty($this->cposts) ) { ?>
  340. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Custom Post Types', 'display-widgets') ?> +/-</h4>
  341. <div class="dw_collapse">
  342. <?php foreach ( $this->cposts as $post_key => $custom_post ) {
  343. $instance['type-'. $post_key] = isset($instance['type-'. $post_key]) ? $instance['type-'. $post_key] : false;
  344. ?>
  345. <p><input class="checkbox" type="checkbox" <?php checked($instance['type-'. $post_key], true) ?> id="<?php echo $widget->get_field_id('type-'. $post_key); ?>" name="<?php echo $widget->get_field_name('type-'. $post_key); ?>" />
  346. <label for="<?php echo $widget->get_field_id('type-'. $post_key); ?>"><?php echo stripslashes($custom_post->labels->name) ?></label></p>
  347. <?php
  348. unset($post_key);
  349. unset($custom_post);
  350. } ?>
  351. </div>
  352.  
  353. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Custom Post Type Archives', 'display-widgets') ?> +/-</h4>
  354. <div class="dw_collapse">
  355. <?php foreach ( $this->cposts as $post_key => $custom_post ) {
  356. if ( !$custom_post->has_archive ) {
  357. // don't give the option if there is no archive page
  358. continue;
  359. }
  360. $instance['type-'. $post_key .'-archive'] = isset($instance['type-'. $post_key .'-archive']) ? $instance['type-'. $post_key .'-archive'] : false;
  361. ?>
  362. <p><input class="checkbox" type="checkbox" <?php checked($instance['type-'. $post_key.'-archive'], true) ?> id="<?php echo $widget->get_field_id('type-'. $post_key .'-archive'); ?>" name="<?php echo $widget->get_field_name('type-'. $post_key .'-archive'); ?>" />
  363. <label for="<?php echo $widget->get_field_id('type-'. $post_key .'-archive'); ?>"><?php echo stripslashes($custom_post->labels->name) ?> <?php _e('Archive', 'display-widgets') ?></label></p>
  364. <?php } ?>
  365. </div>
  366. <?php } ?>
  367.  
  368. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Categories') ?> +/-</h4>
  369. <div class="dw_collapse">
  370. <?php foreach ( $this->cats as $cat ) {
  371. $instance['cat-'. $cat->cat_ID] = isset($instance['cat-'. $cat->cat_ID]) ? $instance['cat-'. $cat->cat_ID] : false;
  372. ?>
  373. <p><input class="checkbox" type="checkbox" <?php checked($instance['cat-'. $cat->cat_ID], true) ?> id="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>" name="<?php echo $widget->get_field_name('cat-'. $cat->cat_ID); ?>" />
  374. <label for="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>"><?php echo $cat->cat_name ?></label></p>
  375. <?php
  376. unset($cat);
  377. }
  378. ?>
  379. </div>
  380.  
  381. <?php if ( !empty($this->taxes) ) { ?>
  382. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Taxonomies', 'display-widgets') ?> +/-</h4>
  383. <div class="dw_collapse">
  384. <?php foreach ( $this->taxes as $tax ) {
  385. $instance['tax-'. $tax] = isset($instance['tax-'. $tax]) ? $instance['tax-'. $tax] : false;
  386. ?>
  387. <p><input class="checkbox" type="checkbox" <?php checked($instance['tax-'. $tax], true) ?> id="<?php echo $widget->get_field_id('tax-'. $tax); ?>" name="<?php echo $widget->get_field_name('tax-'. $tax); ?>" />
  388. <label for="<?php echo $widget->get_field_id('tax-'. $tax); ?>"><?php echo str_replace(array('_','-'), ' ', ucfirst($tax)) ?></label></p>
  389. <?php
  390. unset($tax);
  391. }
  392. ?>
  393. </div>
  394. <?php } ?>
  395.  
  396. <?php if ( !empty($this->langs) ) { ?>
  397. <h4 class="dw_toggle" style="cursor:pointer;"><?php _e('Languages', 'display-widgets') ?> +/-</h4>
  398. <div class="dw_collapse">
  399. <?php foreach ( $this->langs as $lang ) {
  400. $key = $lang['language_code'];
  401. $instance['lang-'. $key] = isset($instance['lang-'. $key]) ? $instance['lang-'. $key] : false;
  402. ?>
  403. <p><input class="checkbox" type="checkbox" <?php checked($instance['lang-'. $key], true) ?> id="<?php echo $widget->get_field_id('lang-'. $key); ?>" name="<?php echo $widget->get_field_name('lang-'. $key); ?>" />
  404. <label for="<?php echo $widget->get_field_id('lang-'. $key); ?>"><?php echo $lang['native_name'] ?></label></p>
  405.  
  406. <?php
  407. unset($lang);
  408. unset($key);
  409. }
  410. ?>
  411. </div>
  412. <?php } ?>
  413.  
  414. <p><label for="<?php echo $widget->get_field_id('other_ids'); ?>"><?php _e('Comma Separated list of IDs of posts not listed above', 'display-widgets') ?>:</label>
  415. <input type="text" value="<?php echo $instance['other_ids'] ?>" name="<?php echo $widget->get_field_name('other_ids'); ?>" id="<?php echo $widget->get_field_id('other_ids'); ?>" />
  416. </p>
  417. </div>
  418. <?php
  419. }
  420.  
  421. function update_widget_options($instance, $new_instance, $old_instance) {
  422. self::register_globals();
  423.  
  424. if ( !empty($this->pages) ) {
  425. foreach ($this->pages as $page) {
  426. if ( isset($new_instance['page-'. $page->ID]) ) {
  427. $instance['page-'. $page->ID] = 1;
  428. } else if ( isset($instance['page-'. $page->ID]) ) {
  429. unset($instance['page-'. $page->ID]);
  430. }
  431. unset($page);
  432. }
  433. }
  434.  
  435. foreach ( $this->cats as $cat ) {
  436. if ( isset($new_instance['cat-'. $cat->cat_ID]) ) {
  437. $instance['cat-'. $cat->cat_ID] = 1;
  438. } else if ( isset($instance['cat-'. $cat->cat_ID]) ){
  439. unset($instance['cat-'. $cat->cat_ID]);
  440. }
  441. unset($cat);
  442. }
  443.  
  444. if ( !empty($this->cposts) ) {
  445. foreach ( $this->cposts as $post_key => $custom_post ) {
  446. if ( isset($new_instance['type-'. $post_key]) ) {
  447. $instance['type-'. $post_key] = 1;
  448. } else if (isset($instance['type-'. $post_key]) ) {
  449. unset($instance['type-'. $post_key]);
  450. }
  451.  
  452. if ( isset($new_instance['type-'. $post_key .'-archive']) ) {
  453. $instance['type-'. $post_key .'-archive'] = 1;
  454. } else if ( isset($instance['type-'. $post_key .'-archive']) ) {
  455. unset($instance['type-'. $post_key .'-archive']);
  456. }
  457.  
  458. unset($custom_post);
  459. }
  460. }
  461.  
  462. if ( !empty($this->taxes) ) {
  463. foreach ( $this->taxes as $tax ) {
  464. if ( isset($new_instance['tax-'. $tax]) ) {
  465. $instance['tax-'. $tax] = 1;
  466. } else if ( isset($instance['tax-'. $tax]) ) {
  467. unset($instance['tax-'. $tax]);
  468. }
  469. unset($tax);
  470. }
  471. }
  472.  
  473. if ( !empty($this->langs) ) {
  474. foreach ( $this->langs as $lang ) {
  475. if ( isset($new_instance['lang-'. $lang['language_code'] ]) ) {
  476. $instance['lang-'. $lang['language_code']] = 1;
  477. } else if(isset($instance['lang-'. $lang['language_code']]) ) {
  478. unset($instance['lang-'. $lang['language_code']]);
  479. }
  480. unset($lang);
  481. }
  482. }
  483.  
  484. $instance['dw_include'] = ( isset($new_instance['dw_include']) && $new_instance['dw_include'] ) ? 1 : 0;
  485. $instance['dw_logged'] = ( isset($new_instance['dw_logged']) && $new_instance['dw_logged'] ) ? $new_instance['dw_logged'] : '';
  486. $instance['other_ids'] = ( isset($new_instance['other_ids']) && $new_instance['other_ids'] ) ? $new_instance['other_ids'] : '';
  487.  
  488. $page_types = self::page_types();
  489. foreach ( array_keys($page_types) as $page ) {
  490. if ( isset($new_instance['page-'. $page]) ) {
  491. $instance['page-'. $page] = 1;
  492. } else if ( isset($instance['page-'. $page]) ) {
  493. unset($instance['page-'. $page]);
  494. }
  495. }
  496. unset($page_types);
  497.  
  498. return $instance;
  499. }
  500.  
  501. function get_field_name($field_name) {
  502. return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
  503. }
  504.  
  505. function get_field_id($field_name) {
  506. return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
  507. }
  508.  
  509. function load_js() {
  510. global $pagenow;
  511.  
  512. if ( $pagenow != 'widgets.php' ) {
  513. //only load the js on the widgets page
  514. return;
  515. }
  516. ?>
  517. <script type="text/javascript">
  518. /*<![CDATA[*/
  519. jQuery(document).ready(function($){
  520. $('.widgets-holder-wrap').on('click', '.dw_toggle', dw_toggle);
  521. });
  522. jQuery(document.body).bind('click.widgets-toggle', dw_show_opts);
  523. function dw_show_opts(e){
  524. var target = jQuery(e.target);
  525. var widget = target.closest('div.widget');
  526. var inside = widget.children('.widget-inside');
  527. var opts = inside.find('.dw_opts');
  528. if(opts.length == 0){
  529. return;
  530. }
  531.  
  532. inside.find('.spinner').show();
  533.  
  534. jQuery.ajax({
  535. type:'POST',url:'<?php echo admin_url( "admin-ajax.php" ) ?>',
  536. data:{
  537. 'action':'dw_show_widget',
  538. 'opts':JSON.stringify(opts.children('input').serializeArray()),
  539. 'id_base':inside.find('input.id_base').val(),
  540. 'widget_number':(inside.find('input.multi_number').val() == '') ? inside.find('input.widget_number').val() : inside.find('input.multi_number').val()
  541. },
  542. success:function(html){ opts.replaceWith(html); inside.find('.spinner').hide(); }
  543. });
  544. }
  545. function dw_toggle(){jQuery(this).next('.dw_collapse').toggle();}
  546. /*]]>*/
  547. </script>
  548. <?php
  549. }
  550.  
  551. function show_logged($instance) {
  552. if ( isset($instance['dw_logged']) ) {
  553. return $instance['dw_logged'];
  554. }
  555.  
  556. if ( isset($instance['dw_logout']) && $instance['dw_logout'] ) {
  557. $instance['dw_logged'] = 'out';
  558. } else if ( isset($instance['dw_login']) && $instance['dw_login'] ) {
  559. $instance['dw_logged'] = 'in';
  560. } else {
  561. $instance['dw_logged'] = '';
  562. }
  563.  
  564. return $instance['dw_logged'];
  565. }
  566.  
  567. function page_types(){
  568. $page_types = array(
  569. 'front' => __('Front', 'display-widgets'),
  570. 'home' => __('Blog', 'display-widgets'),
  571. 'archive' => __('Archives'),
  572. 'single' => __('Single Post'),
  573. '404' => '404',
  574. 'search' => __('Search'),
  575. );
  576.  
  577. return $page_types;
  578. }
  579.  
  580. function register_globals(){
  581. if ( !empty($this->checked) ) {
  582. return;
  583. }
  584.  
  585. $saved_details = get_transient( $this->transient_name );
  586. if ( $saved_details ) {
  587. foreach ( $saved_details as $k => $d ) {
  588. if ( empty($this->{$k}) ) {
  589. $this->{$k} = $d;
  590. }
  591.  
  592. unset($k);
  593. unset($d);
  594. }
  595. }
  596.  
  597. if ( empty($this->pages) ) {
  598. $this->pages = get_posts( array(
  599. 'post_type' => 'page', 'post_status' => 'publish',
  600. 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC',
  601. 'fields' => array('ID', 'name'),
  602. ));
  603. }
  604.  
  605. if ( empty($this->cats) ) {
  606. $this->cats = get_categories( array(
  607. 'hide_empty' => false,
  608. //'fields' => 'id=>name', //added in 3.8
  609. ) );
  610. }
  611.  
  612. if ( empty($this->cposts) ) {
  613. $this->cposts = get_post_types( array(
  614. 'public' => true,
  615. ), 'object');
  616.  
  617. foreach ( array( 'revision', 'post', 'page', 'attachment', 'nav_menu_item' ) as $unset ) {
  618. unset($this->cposts[$unset]);
  619. }
  620.  
  621. foreach ( $this->cposts as $c => $type ) {
  622. $post_taxes = get_object_taxonomies($c);
  623. foreach ( $post_taxes as $post_tax) {
  624. $this->taxes[] = $post_tax;
  625. }
  626. }
  627. }
  628.  
  629. if ( empty($this->langs) && function_exists('icl_get_languages') ) {
  630. $this->langs = icl_get_languages('skip_missing=0&orderby=code');
  631. }
  632.  
  633. // save for one week
  634. set_transient( $this->transient_name, array(
  635. 'pages' => $this->pages,
  636. 'cats' => $this->cats,
  637. 'cposts' => $this->cposts,
  638. 'taxes' => $this->taxes,
  639. ), 60*60*24*7 );
  640.  
  641. if ( empty($this->checked) ) {
  642. $this->checked[] = true;
  643. }
  644. }
  645.  
  646. function delete_transient() {
  647. delete_transient( $this->transient_name );
  648. }
  649.  
  650. function load_lang(){
  651. load_plugin_textdomain( 'display-widgets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  652. }
  653.  
  654. /* WPML support */
  655. function get_lang_id($id, $type = 'page') {
  656. if ( function_exists('icl_object_id') ) {
  657. $id = icl_object_id($id, $type, true);
  658. }
  659.  
  660. return $id;
  661. }
  662.  
  663. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement