PageRenderTime 426ms CodeModel.GetById 139ms app.highlight 172ms RepoModel.GetById 99ms app.codeStats 1ms

/wp-content/plugins/dynamic-widgets/dynamic-widgets.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 939 lines | 573 code | 121 blank | 245 comment | 139 complexity | 9e4543bce88108bf1ed30f64f9dd7b3f MD5 | raw file
  1<?php
  2/**
  3 * Plugin Name: Dynamic Widgets
  4 * Plugin URI: http://dynamic-widgets.com/
  5 * Description: Dynamic Widgets gives you full control on which pages your widgets will appear. It lets you dynamicly show or hide widgets on WordPress pages.
  6 * Author: Qurl
  7 * Version: 1.5.9
  8 * Author URI: http://www.qurl.nl/
  9 * Tags: widget, widgets, dynamic, sidebar, custom, rules, logic, admin, condition, conditional tags, hide, show, wpml, qtranslate, wpec, buddypress, pods
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * Released under the GPL v.2, http://www.gnu.org/copyleft/gpl.html
 17 *
 18 * @version $Id: dynamic-widgets.php 949624 2014-07-16 12:56:39Z qurl $
 19 * @copyright 2014 Jacco Drabbe
 20 *
 21 * Thanks to Alexis Nomine for the contribution of the French (fr_FR) language files, several L10N fixes and change of the edit options UI.
 22 * Thanks to Daniel Bihler for the contribution of the German (de_DE) language files.
 23 * Thanks to Eduardo Larequi for the contribution of the Spanish (es_ES) language files and several L10N fixes.
 24 * Thanks to Hanolex for the contribution of the Chinese (Simplified) (zh_CN) language files.
 25 * Thanks to Liudas Ališauskas for the contribution of the Lithuanian (lt_LT) language files.
 26 * Thanks to Pedro Nave for the contribution of the Portuguese (pt_PT) language files.
 27 * Thanks to Renato Tavares for the contribution of the Brazil Portuguese (pt_BR) language files.
 28 * Thanks to Pavel Bilek for the contribution of the Chech (cs_CZ) language files.
 29 * Thanks to Morten Nalholm for the contribution of the Danish (da_DK) language files.
 30 * Thanks to Scott Kingsley Clark for the help to get the Pods module upgraded to support Pods v2.
 31 * Thanks to Sébastien Christy for the help finding a PHP bug preventing the exposure of class properties while in the right scope.
 32 * Thanks to Rick Anderson from Build Your Own Business Website (http://www.byobwebsite.com/) for the financial contribution to implement the AJAX lazy load taxonomy tree and the modules filter
 33 * Thanks to Advancis (http://advancis.net/) for the help and financial contribution to find and fix a WPML category bug.
 34 * Thanks to Borisa Djuraskovic for the contribution of the Serbo-Croatian (sr_RS) languages files.
 35 * Thanks to Leon Juranic from DefenseCode to run it's scanner over the source code and finding a few vulnerabilities.
 36 *
 37 *
 38 * WPML Plugin support via API
 39 * Using constants	ICL_PLUGIN_PATH > mods/wpml_module.php
 40 * Using functions  wpml_get_default_language() > mods/wpml_module.php
 41 *                  wpml_get_current_language() > mods/wpml_module.php
 42 *                  wpml_get_content_translation() > mods/wpml_module.php
 43 * 									wpml_get_active_languages() > mods/wpml_module.php
 44 *
 45 * QTranslate Plugin support via API
 46 * Using constants 	QTRANS_INIT > mods/qt_module.php
 47 * Using functions	qtrans_getLanguage() > mods/qt_module.php
 48 * Using WPOptions	qtranslate_default_language > mods/qt_module.php
 49 * 									qtranslate_enabled_languages > mods/qt_module.php
 50 *
 51 * WPSC/WPEC Plugin support
 52 * Using constants	WPSC_TABLE_PRODUCT_CATEGORIES	> dynwid_admin_overview.php
 53 * 									WPSC_VERSION > mods/wpsc_module.php
 54 * Using vars 			$wpsc_query > mods/wpsc_module.php
 55 *
 56 * BP Plugin support
 57 * Using constants	BP_VERSION > mods/bp_module.php
 58 * Using vars				$bp > mods/bp_module.php
 59 *
 60 * Pods Plugin support
 61 * Using constants 	PODS_VERSION_FULL > mods/pods_module.php
 62 * Using vars				$pod_page_exists > mods/pods_module.php, dynwid_worker.php
 63**/
 64
 65  // Constants
 66  define('DW_CLASSES', dirname(__FILE__) . '/' . 'classes/');
 67  define('DW_DEBUG', FALSE);
 68  define('DW_DB_TABLE', 'dynamic_widgets');
 69  define('DW_L10N_DOMAIN', 'dynamic-widgets');
 70  define('DW_LIST_LIMIT', 20);
 71  define('DW_LIST_STYLE', 'style="overflow:auto;height:240px;"');
 72  define('DW_OLD_METHOD', get_option('dynwid_old_method'));
 73  define('DW_PAGE_LIMIT', get_option('dynwid_page_limit', 500));
 74  define('DW_MINIMUM_PHP', '5.2.7');
 75  define('DW_MINIMUM_WP', '3.0');
 76  define('DW_MODULES', dirname(__FILE__) . '/' . 'mods/');
 77  define('DW_PLUGIN', dirname(__FILE__) . '/' . 'plugin/');
 78  define('DW_TIME_LIMIT', 86400);				// 1 day
 79  define('DW_URL_AUTHOR', 'http://www.qurl.nl');
 80  define('DW_VERSION', '1.5.9');
 81	define('DW_WPML_API', '/inc/wpml-api.php');			// WPML Plugin support - API file relative to ICL_PLUGIN_PATH
 82	define('DW_WPML_ICON', 'img/wpml_icon.png');	// WPML Plugin support - WPML icon
 83
 84	// Classes - only PHP5
 85  if ( version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=') ) {
 86  	 require_once(dirname(__FILE__) . '/dynwid_class.php');
 87  }
 88
 89  // Functions
 90	/**
 91	 * dynwid_activate() Activate the plugin
 92	 * @since 1.3.3
 93	 */
 94	function dynwid_activate() {
 95		$wpdb = $GLOBALS['wpdb'];
 96		$dbtable = $wpdb->prefix . DW_DB_TABLE;
 97
 98		$query = "CREATE TABLE IF NOT EXISTS " . $dbtable . " (
 99                id int(11) NOT NULL auto_increment,
100                widget_id varchar(60) NOT NULL,
101                maintype varchar(50) NOT NULL,
102                `name` varchar(40) NOT NULL,
103                `value` longtext NOT NULL,
104              PRIMARY KEY  (id),
105              KEY widget_id (widget_id,maintype)
106            );";
107		$wpdb->query($query);
108
109		// Version check
110		$version = get_option('dynwid_version');
111		if ( $version !== FALSE ) {
112/*    1.2 > Added support for widget display setting options for Author Pages.
113   		Need to apply archive rule to author also to keep same behavior. */
114			if ( version_compare($version, '1.2', '<') ) {
115				$query = "SELECT widget_id FROM " . $dbtable . " WHERE maintype = 'archive'";
116				$results = $wpdb->get_results($query);
117				foreach ( $results as $myrow ) {
118					$query = "INSERT INTO " . $dbtable . "(widget_id, maintype, value) VALUES ('" . $myrow->widget_id . "', 'author', '0')";
119					$wpdb->query($query);
120				}
121			}
122
123/*    1.3 > Added Date (range) support.
124   		Need to change DB `value` to a LONGTEXT type
125   		(not for the date of course, but for supporting next features which might need a lot of space) */
126			if ( version_compare($version, '1.3', '<') ) {
127				$query = "ALTER TABLE " . $dbtable . " CHANGE `value` `value` LONGTEXT NOT NULL";
128				$wpdb->query($query);
129			}
130
131/*		1.4.0.5 > Enlarged the maintype field because of addition of CTs 	*/
132			if ( version_compare($version, '1.4.0.5', '<') ) {
133				$query = "ALTER TABLE " . $dbtable . " CHANGE `maintype` `maintype` VARCHAR(50) NOT NULL";
134				$wpdb->query($query);
135			}
136
137/*		1.4.0.12 > Added MSIE 6 support in browser module
138			Need to apply MSIE rule to MSIE6 to keep same behavior. */
139			if ( version_compare($version, '1.4.0.12', '<') ) {
140				$query = "SELECT widget_id, value FROM " . $dbtable . " WHERE maintype = 'browser' AND name = 'msie'";
141				$results = $wpdb->get_results($query);
142				foreach ( $results as $myrow ) {
143					$query = "INSERT INTO " . $dbtable . "(widget_id, maintype, name, value) VALUES ('" . $myrow->widget_id . "', 'browser', 'msie6', '" . $myrow->value . "')";
144					$wpdb->query($query);
145				}
146			}
147
148/*    1.5b3 > Added support for widget display setting options for Tag Pages.
149   		Need to apply archive rule to tag also to keep same behavior. */
150			if ( version_compare($version, '1.5b3', '<') ) {
151				$query = "SELECT widget_id FROM " . $dbtable . " WHERE maintype = 'archive'";
152				$results = $wpdb->get_results($query);
153				foreach ( $results as $myrow ) {
154					$query = "INSERT INTO " . $dbtable . "(widget_id, maintype, value) VALUES ('" . $myrow->widget_id . "', 'tag', '0')";
155					$wpdb->query($query);
156				}
157			}
158
159			/*
160			1.5.3.1 > Widgets seems to be started using longer classnames to avoid clashing.
161			Widend up the width for widget_id from 40 to 60.
162			*/
163			if ( version_compare($version, '1.5.3.1', '<') ) {
164				$query = "ALTER TABLE " . $dbtable . " CHANGE `widget_id` `widget_id` VARCHAR(60) NOT NULL";
165				$wpdb->query($query);
166			}
167
168		}
169		update_option('dynwid_version', DW_VERSION);
170	}
171
172	/**
173   * dynwid_add_admin_custom_box Adds meta boxes to Custom Post Types
174   * @since 1.5.2.5
175   */
176	function dynwid_add_admin_custom_box() {
177		$args = array(
178							'public'   => TRUE,
179							'_builtin' => FALSE
180						);
181
182		$post_types = get_post_types($args, 'objects', 'and');
183		foreach ( array_keys($post_types) as $type ) {
184			add_meta_box('dynwid', __('Dynamic Widgets', DW_L10N_DOMAIN), 'dynwid_add_post_control', $type, 'side', 'low');
185		}
186  }
187
188	/**
189   * dynwid_add_admin_help_tab() Add help tab for WP >= 3.3
190   * @since 1.5.0
191   */
192	function dynwid_add_admin_help_tab() {
193		$dw_admin_screen = $GLOBALS['dw_admin_screen'];
194		$screen = get_current_screen();
195
196		if ( $screen->id == $dw_admin_screen ) {
197			// Contextual help
198  		if ( isset($_GET['action']) && $_GET['action'] == 'edit' ) {
199  			$dw_help = dynwid_contextual_help_text('edit');
200  		} else {
201				$dw_help = dynwid_contextual_help_text('overview');
202  		}
203
204			$args = array(	'id'	=> 'dw_help_tab',
205											'title'	=> 'Dynamic Widgets',
206											'content'	=> $dw_help
207										);
208			$screen->add_help_tab($args);
209		}
210	}
211
212  /**
213   * dynwid_add_admin_menu() Add plugin link to admin menu
214   * @since 1.0
215   */
216  function dynwid_add_admin_menu() {
217  	global $dw_admin_screen;
218
219    $DW = &$GLOBALS['DW'];
220
221    $dw_admin_screen = add_submenu_page('themes.php', __('Dynamic Widgets', DW_L10N_DOMAIN), __('Dynamic Widgets', DW_L10N_DOMAIN), 'edit_theme_options', 'dynwid-config', 'dynwid_admin_page');
222
223  	if ( $DW->enabled ) {
224  		add_action('admin_print_styles-' . $dw_admin_screen, 'dynwid_add_admin_styles');
225  		add_action('admin_print_scripts-' . $dw_admin_screen, 'dynwid_add_admin_scripts');
226
227  		// Contextual help
228  		if ( isset($_GET['action']) && $_GET['action'] == 'edit' ) {
229  			$dw_help = dynwid_contextual_help_text('edit');
230  		} else {
231				$dw_help = dynwid_contextual_help_text('overview');
232  		}
233
234  		// Since WP 3.3 contextual help is handled different
235  		if ( version_compare($GLOBALS['wp_version'], '3.3', '>=') ) {
236  			add_action('load-' . $dw_admin_screen, 'dynwid_add_admin_help_tab');
237  		} else {
238  			add_contextual_help($dw_admin_screen, $dw_help);
239  		}
240
241  		// Only show meta box in posts panel when there are widgets enabled.
242  		$opt = $DW->getOpt('%','individual');
243  		if ( count($opt) > 0 ) {
244  			add_meta_box('dynwid', __('Dynamic Widgets', DW_L10N_DOMAIN), 'dynwid_add_post_control', 'post', 'side', 'low');
245  		}
246  	}
247  }
248
249  /**
250   * dynwid_add_admin_scripts() Enqueue jQuery UI scripts to admin page
251   * @since 1.3
252   */
253  function dynwid_add_admin_scripts() {
254  	$DW = &$GLOBALS['DW'];
255
256  	/*
257  		BuddyPress doing an overall JS enqueue (BAD!)
258  		Workaround fixing a js error with ui.accordion freezing the screen
259  			- dtheme-ajax-js is used in BP default theme
260  			- bp-js is used in BP Compatibility Plugin
261  	*/
262  	if ( wp_script_is('dtheme-ajax-js') ) {
263  		wp_deregister_script('dtheme-ajax-js');
264  	}
265  	if ( wp_script_is('bp-js') ) {
266  		wp_deregister_script('bp-js');
267  	}
268
269    wp_enqueue_script('jquery');
270  	wp_enqueue_script('jquery-ui-core');
271  	if ( version_compare(substr($GLOBALS['wp_version'], 0, 3), '3.1', '>=') ) {
272  		wp_enqueue_script('jquery-ui-widget');
273  		// wp_enqueue_script('jquery-ui-accordion', $DW->plugin_url . 'ui.accordion.1.8.7.js', array('jquery-ui-widget'));
274  		wp_enqueue_script('jquery-ui-datepicker', $DW->plugin_url . 'ui.datepicker.1.8.7.js', array('jquery-ui-widget'));
275  	} else {
276  		//  wp_enqueue_script('jquery-ui-accordion', $DW->plugin_url . 'ui.accordion.1.7.3.js', array('jquery-ui-core'));
277  		wp_enqueue_script('jquery-ui-datepicker', $DW->plugin_url . 'ui.datepicker.1.7.3.js', array('jquery-ui-core'));
278  	}
279	}
280
281  /**
282   * dynwid_add_admin_styles() Enqueue CSS to admin page
283   * @since 1.3
284   */
285  function dynwid_add_admin_styles() {
286    $DW = &$GLOBALS['DW'];
287
288  	if ( version_compare(substr($GLOBALS['wp_version'], 0, 3), '3.1', '>=') ) {
289    	wp_enqueue_style('jquery-ui-custom', $DW->plugin_url . 'jquery-ui-1.8.7.custom.css');
290  	} else {
291  		wp_enqueue_style('jquery-ui-custom', $DW->plugin_url . 'jquery-ui-1.7.3.custom.css');
292  	}
293  }
294
295  /**
296   * dynwid_add_plugin_actions() Add settings link in WP plugin overview
297   * @param array $all
298   * @return array
299   * @since 1.0
300   */
301  function dynwid_add_plugin_actions($all) {
302    $links = array();
303	  $links[ ] = '<a href="themes.php?page=dynwid-config">' . __('Settings') . '</a>';
304
305    return array_merge($links, $all);
306  }
307
308  /**
309   * dynwid_add_post_control() Add control widget to post screen
310   * @since 1.2
311   */
312  function dynwid_add_post_control() {
313    $post = $GLOBALS['post'];
314    $DW = &$GLOBALS['DW'];
315
316    $post_type = get_post_type($post->ID);
317    if ( $post_type == 'post') {
318    	$post_type = 'single';
319    	$maintype = 'single-post';
320    } else {
321    	$maintype = $post_type . '-post';
322  	}
323
324    $opt = $DW->getOpt('%','individual');
325    echo '<strong>' . __('Apply exception rule to widgets:', DW_L10N_DOMAIN) . '</strong><br /><br />';
326    foreach ( $opt as $widget ) {
327      $single_condition = '1';
328      $checked = '';
329      $opt_single = $DW->getOpt($widget->widget_id, $post_type);
330
331      // loop through the opts to see if we have a match
332      foreach ( $opt_single as $widget_opt ) {
333        if ( $widget_opt->maintype == 'single' ) {
334          $single_condition = $widget_opt->value;
335        }
336        if ( $widget_opt->maintype == $maintype && $widget_opt->name == $post->ID ) {
337          $checked = ' checked="checked"';
338        }
339      }
340
341      $default = ( $single_condition == '0' ) ? __('Off', DW_L10N_DOMAIN) : __('On', DW_L10N_DOMAIN);
342      echo '<input type="checkbox" id="dw_' . $widget->widget_id . '" name="dw-single-post[]" value="' . $widget->widget_id . '"' . $checked . ' /> <label for="dw_' . $widget->widget_id . '">' . $DW->getName($widget->widget_id) . __(' (Default: ', DW_L10N_DOMAIN) . $default . ')</label><br />';
343    }
344  }
345
346  /**
347   * dynwid_add_tag_page() Add row to WP tags admin
348   * @since 1.2
349   */
350  function dynwid_add_tag_page() {
351    $DW = &$GLOBALS['DW'];
352
353    // Only show dynwid row when there are widgets enabled
354    $opt = $DW->getOpt('%','individual');
355    if ( count($opt) > 0 ) {
356
357      echo '<tr class="form-field">';
358      echo '<th scope="row" valign="top"><label for="dynamic-widgets">' . __('Dynamic Widgets', DW_L10N_DOMAIN) . '</label></th>';
359      echo '<td>';
360      foreach ( $opt as $widget ) {
361        $single_condition = '1';
362        $checked = '';
363        $opt_single = $DW->getOpt($widget->widget_id, 'single');
364
365        // loop through the opts to see if we have a match
366        foreach ( $opt_single as $widget_opt ) {
367          if ( $widget_opt->maintype == 'single' ) {
368            $single_condition = $widget_opt->value;
369          }
370          if ( $widget_opt->maintype == 'single-tag' && $widget_opt->name == $_GET['tag_ID'] ) {
371            $checked = ' checked="checked"';
372          }
373        }
374
375        $default = ( $single_condition == '0' ) ? __('Off', DW_L10N_DOMAIN) : __('On', DW_L10N_DOMAIN);
376        echo '<input type="checkbox" style="width:10pt;border:none;" id="dw_' . $widget->widget_id . '" name="dw-single-tag[]" value="' . $widget->widget_id . '"' . $checked . ' /> <label for="dw_' . $widget->widget_id . '">' . $DW->getName($widget->widget_id) . ' (' . __('Default', DW_L10N_DOMAIN) . ': ' . $default . ')</label><br />';
377
378      } // END foreach opt
379      echo '</td>';
380      echo '</tr>';
381    }
382  }
383
384  /**
385   * dynwid_add_widget_control() Preparation for callback hook into WP widgets admin
386   * @since 1.2
387   */
388  function dynwid_add_widget_control() {
389    $DW = &$GLOBALS['DW'];
390
391    /*
392      Hooking into the callback of the widgets by moving the existing callback to wp_callback
393      and setting callback with own callback function.
394      We need the widget_id registered in params also for calling own callback.
395    */
396    foreach ( $DW->registered_widgets as $widget_id => $widget ) {
397      if ( array_key_exists($widget_id, $DW->registered_widget_controls) ) {
398        $DW->registered_widget_controls[$widget_id]['wp_callback'] = $DW->registered_widget_controls[$widget_id]['callback'];
399        $DW->registered_widget_controls[$widget_id]['callback'] = 'dynwid_widget_callback';
400
401        /*
402          In odd cases params and/or params[0] seems not to be an array. Bugfix for:
403          Warning: Cannot use a scalar value as an array in ./wp-content/plugins/dynamic-widgets/dynamic-widgets.php
404        */
405
406        /* Fixing params */
407        if (! is_array($DW->registered_widget_controls[$widget_id]['params']) ) {
408          $DW->registered_widget_controls[$widget_id]['params'] = array();
409        }
410
411        if ( count($DW->registered_widget_controls[$widget_id]['params']) == 0 ) {
412          $DW->registered_widget_controls[$widget_id]['params'][ ] = array('widget_id' => $widget_id);
413        // Fixing params[0]
414        } else if (! is_array($DW->registered_widget_controls[$widget_id]['params'][0]) ) {
415          $DW->registered_widget_controls[$widget_id]['params'][0] = array('widget_id' => $widget_id);
416        } else {
417          $DW->registered_widget_controls[$widget_id]['params'][0]['widget_id'] = $widget_id;
418        }
419      }
420    }
421
422    // Notifying user when options are saved and returned to ./wp-admin/widgets.php
423    if ( isset($_GET['dynwid_save']) && $_GET['dynwid_save'] == 'yes' ) {
424      add_action('sidebar_admin_page', 'dynwid_add_widget_page');
425    }
426  }
427
428  /**
429   * dynwid_add_widget_page() Save success message for WP widgets admin
430	 * @since 1.2
431   */
432  function dynwid_add_widget_page() {
433    $DW = &$GLOBALS['DW'];
434
435    $name = strip_tags($DW->getName($_GET['widget_id']));
436		$lead = __('Dynamic Widgets Options saved', DW_L10N_DOMAIN);
437  	$msg = __('for', DW_L10N_DOMAIN) . ' ' .  $name;
438
439  	DWMessageBox::create($lead, $msg);
440  }
441
442  /**
443   * dynwid_admin_dump() Dump function
444   * @since 1.0
445   */
446  function dynwid_admin_dump() {
447    $DW = &$GLOBALS['DW'];
448
449    header('Content-Description: File Transfer');
450    header('Content-Disposition: attachment; filename=dynwid_dump_' . date('Ymd') . '.txt' );
451    header('Content-Type: text/plain');
452
453    $DW->dump();
454    die();
455  }
456
457  /**
458   * dynwid_admin_page() Admin pages
459   * @since 1.0
460   */
461  function dynwid_admin_page() {
462    $DW = &$GLOBALS['DW'];
463    require_once(dirname(__FILE__) . '/dynwid_admin.php');
464  }
465
466	/**
467	 * dynwid_admin_wpec_dump() Dump WPEC rules function for upgrade to 3.8
468	 * @since 1.4.0
469	 */
470  function dynwid_admin_wpec_dump() {
471  	$DW = &$GLOBALS['DW'];
472  	$wpdb = &$GLOBALS['wpdb'];
473  	$dump = array();
474
475		$opt = $DW->getOpt('%', 'wpsc');
476
477  	$categories = array();
478  	$table = WPSC_TABLE_PRODUCT_CATEGORIES;
479  	$fields = array('id', 'name');
480  	$query = "SELECT " . implode(', ', $fields) . " FROM " . $table . " WHERE active = '1' ORDER BY name";
481  	$results = $wpdb->get_results($query);
482  	foreach ( $results as $myrow ) {
483  		$categories[$myrow->id] = $myrow->name;
484  	}
485
486  	foreach ( $opt as $widget ) {
487  		$id = $widget->widget_id;
488  		if (! array_key_exists($id, $dump) ) {
489  			$dump[$id] = array( 'name' => strip_tags($DW->getName($widget->widget_id)) );
490  		}
491
492  		if ( $widget->name == 'default' ) {
493  			$dump[$id]['default'] = ( $widget->value == '0' ? 'No' : 'Yes' );
494  		} else {
495  			$v = $widget->name;
496  			$dump[$id][ ] = $categories[$v];
497  		}
498  	}
499
500  	header('Content-Description: File Transfer');
501  	header('Content-Disposition: attachment; filename=dynwid_wpec_dump_' . date('Ymd') . '.txt' );
502  	header('Content-Type: text/plain');
503
504  	foreach ( $dump as $widget ) {
505  		echo 'Widget: ' . $widget['name'] . "\r\n";
506  		echo 'Default set to ' . $widget['default'] . "\r\n";
507  		if ( count($widget) > 2 ) {
508  			echo 'Categories ticked: ' . "\r\n";
509  			foreach ( $widget as $k => $v ) {
510  				if ( is_int($k) ) {
511  					echo "\t" . $v . "\r\n";
512  				}
513	  		}
514  		}
515  		echo "\r\n";
516  	}
517
518  	die();
519  }
520
521  /**
522   * dynwid_contextual_help_text() Actual text to place into the contextual help screen
523   * @param string $screen
524   * @return string
525   * @since 1.5.0
526   *
527   */
528  function dynwid_contextual_help_text($screen) {
529  	$DW = &$GLOBALS['DW'];
530
531  	// Contextual help
532		if ( $screen == 'edit' ) {
533			$dw_help  = __('Widgets are always displayed by default', DW_L10N_DOMAIN) . ' (' . __('The \'<em>Yes</em>\' selection', DW_L10N_DOMAIN) . ')'  . '<br />';
534			$dw_help .= __('Click on the', DW_L10N_DOMAIN) . ' <img src="' . $DW->plugin_url . 'img/info.gif" alt="info" /> ' . __('next to the options for more info', DW_L10N_DOMAIN) . '.<br />';
535			$dw_help .= __('The') . ' <img src="' . $DW->plugin_url . 'img/checkmark.gif" alt="Checkmark" /> ' . __('next to a section means it has options set.', DW_L10N_DOMAIN);
536		} else {
537			$dw_help  = '<p><strong>' . __('Static', DW_L10N_DOMAIN) . ' / ' . __('Dynamic', DW_L10N_DOMAIN) . '</strong><br />';
538			$dw_help .= __('When a widget is', DW_L10N_DOMAIN) . ' <em>' . __('Static', DW_L10N_DOMAIN) . '</em>, ' . __('the widget uses the WordPress default. In other words, it\'s shown everywhere', DW_L10N_DOMAIN) . '.<br />';
539			$dw_help .=  __('A widget is', DW_L10N_DOMAIN) . ' <em>' . __('Dynamic', DW_L10N_DOMAIN) . '</em> ' . __('when there are options set, i.e. not showing on the front page.', DW_L10N_DOMAIN) . '</p>';
540			$dw_help .= '<p><strong>' . __('Reset', DW_L10N_DOMAIN) . '</strong><br />';
541			$dw_help .= __('Reset makes the widget return to', DW_L10N_DOMAIN) . ' <em>' . __('Static', DW_L10N_DOMAIN) . '</em>.</p>';
542		}
543
544		return $dw_help;
545  }
546
547  /**
548   * dynwid_disabled_add_admin_menu() Menu entry for disabled page.
549   * @since 1.5.6.1
550   *
551   */
552	function dynwid_disabled_add_admin_menu() {
553		add_submenu_page('themes.php', __('Dynamic Widgets', DW_L10N_DOMAIN), __('Dynamic Widgets', DW_L10N_DOMAIN), 'edit_theme_options', 'dynwid-config', 'dynwid_disabled_page');
554	}
555
556  /**
557   * dynwid_disabled_page() Error boxes to show in admin when DW can not be initialised due to not meeting sysreq.
558   * @since 1.5b1
559   *
560   */
561  function dynwid_disabled_page() {
562  	// As the DWMessagebox class is not loaded, we can not use it
563  	$php = version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=');
564  	$wp = version_compare($GLOBALS['wp_version'], DW_MINIMUM_WP, '>=');
565
566  	if (! $php ) {
567  		echo '<div class="error" id="message"><p>';
568  		_e('<b>ERROR</b> Your host is running a too low version of PHP. Dynamic Widgets needs at least version', DW_L10N_DOMAIN);
569  		echo ' ' . DW_MINIMUM_PHP . '.';
570  		echo '</p></div>';
571  	}
572
573  	if (! $wp ) {
574  		echo '<div class="error" id="message"><p>';
575  		_e('<b>ERROR</b> Your host is running a too low version of WordPress. Dynamic Widgets needs at least version', DW_L10N_DOMAIN);
576  		echo ' ' . DW_MINIMUM_WP . '.';
577  		echo '</p></div>';
578  	}
579  }
580
581  /**
582   * dynwid_filter_init() Init of the worker
583   * @since 1.3.5
584   */
585  function dynwid_filter_init() {
586  	$DW = &$GLOBALS['DW'];
587  	require(dirname(__FILE__) . '/dynwid_init_worker.php');
588  }
589
590  /**
591   * dynwid_filter_widgets() Worker
592   * @since 1.3.5
593   */
594  function dynwid_filter_widgets() {
595  	$DW = &$GLOBALS['DW'];
596
597  	dynwid_filter_init();
598  	if ( DW_OLD_METHOD ) {
599  		dynwid_worker($DW->sidebars);
600  	} else {
601  		add_filter('sidebars_widgets', 'dynwid_worker');
602 		}
603  }
604
605  /**
606   * dynwid_init() Init of the plugin
607   * @since 1.0
608   */
609  function dynwid_init() {
610  	$php = version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=');
611  	$wp = version_compare($GLOBALS['wp_version'], DW_MINIMUM_WP, '>=');
612
613  	if ( $php && $wp ) {
614  		$GLOBALS['DW'] = new dynWid();
615  		$DW = &$GLOBALS['DW'];
616  		$DW->plugin_url = WP_PLUGIN_URL . '/' . str_replace( basename(__FILE__), '', plugin_basename(__FILE__) );
617
618  		if ( is_admin() ) {
619  			if ( isset($_POST['dynwid_save']) && $_POST['dynwid_save'] == 'yes' ) {
620  				require_once(dirname(__FILE__) . '/dynwid_admin_save.php');
621  			}
622
623  			load_plugin_textdomain(DW_L10N_DOMAIN, FALSE, dirname(plugin_basename(__FILE__)) . '/locale');
624  			add_action('admin_menu', 'dynwid_add_admin_menu');
625
626  			if ( $DW->enabled ) {
627  				add_action('add_meta_boxes', 'dynwid_add_admin_custom_box');
628  				add_action('edit_tag_form_fields', 'dynwid_add_tag_page');
629  				add_action('edited_term', 'dynwid_save_tagdata');
630  				add_action('plugin_action_links_' . plugin_basename(__FILE__), 'dynwid_add_plugin_actions');
631  				add_action('save_post', 'dynwid_save_postdata');
632  				add_action('sidebar_admin_setup', 'dynwid_add_widget_control');
633
634  				// AJAX calls
635  				add_action('wp_ajax_term_tree', 'dynwid_term_tree');
636  			}
637  		} else {
638  			if ( $DW->enabled ) {
639  				add_action('wp_head', 'dynwid_filter_widgets');
640  			}
641  		}
642  	} else {
643  		if ( is_admin() ) {
644  			// Show errors in the admin page
645  			add_action('admin_menu', 'dynwid_disabled_add_admin_menu');
646  		}
647  	}
648  }
649
650	/**
651	 * dynwid_install() Installation
652	 * @since 1.3.1
653	 */
654	function dynwid_install() {
655		if ( function_exists('is_multisite') ) {
656			if ( is_multisite() && isset($_GET['networkwide']) && $_GET['networkwide'] == '1' ) {
657				$plugin = plugin_basename(__FILE__);
658				deactivate_plugins($plugin);
659			} else {
660				dynwid_activate();
661			}
662		} else {
663			dynwid_activate();
664		}
665	}
666
667	/**
668	 * dynwid_save_postdata() Save of options via post screen
669	 * @param int $post_id
670	 * @since 1.2
671	 */
672	function dynwid_save_postdata($post_id) {
673	  $DW = &$GLOBALS['DW'];
674
675	  if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] != 'autosave' ) {
676	  	$post_id = ( isset($_POST['post_ID']) && ! empty($_POST['post_ID']) ) ? intval($_POST['post_ID']) : 0;
677
678	  	if ( $parent_id = wp_is_post_revision($post_id) ) {
679				$post_id = $parent_id;
680			}
681
682			if ( $post_id > 0 ) {
683				$post_type = get_post_type($post_id);
684				if ( $post_type == 'post') {
685					$post_type = 'single';
686					$maintype = 'single-post';
687				} else {
688					$maintype = $post_type . '-post';
689				}
690
691			  // Housekeeping
692			  $opt = $DW->getOpt('%','individual');
693			  foreach ( $opt as $widget ) {
694			    $DW->deleteOption($widget->widget_id, $maintype, $post_id);
695			  }
696
697			  if ( array_key_exists('dw-single-post', $_POST) ) {
698			    $opt = $_POST['dw-single-post'];
699			    $default = 'yes';
700			    $default_single = '1';
701
702			    foreach ( $opt as $widget_id ) {
703			      $opt_single = $DW->getOpt($widget_id, $post_type);
704			      if ( count($opt_single) > 0 ) {
705			        foreach ( $opt_single as $widget ) {
706			          if ( $widget->maintype == $post_type ) {
707			            $default_single = $widget->value;
708			          }
709			        }
710
711			        if ( $default_single == '0' ) {
712			          $default = 'no';
713			        }
714			      }
715
716			      $DW->addMultiOption($widget_id, $maintype, $default, array($post_id));
717			    }
718			  } // END if array_key_exists
719			} // END if $post_id > 0
720		} // END if ! autosave AND ! quick edit
721	}
722
723	/**
724	 * dynwid_save_tagdata() Save of tagdata
725	 * @param int $term_id
726	 * @since 1.2
727	 */
728	function dynwid_save_tagdata($term_id) {
729	  // Only act when tag is updated via 'edit', NOT via 'quick edit'
730	  if ( $_POST['action'] == 'editedtag' ) {
731	    $DW = &$GLOBALS['DW'];
732
733	    if ( array_key_exists('tag_ID', $_POST) ) {
734	      $term_id = $_POST['tag_ID'];
735	    }
736
737	    // Housekeeping
738	    $opt = $DW->getOpt('%', 'individual');
739	    foreach ( $opt as $widget ) {
740	      $DW->deleteOption($widget->widget_id, 'single-tag', $term_id);
741	    }
742
743	    if ( array_key_exists('dw-single-tag', $_POST) ) {
744	      $opt = $_POST['dw-single-tag'];
745	      $default = 'yes';
746	      $default_single = '1';
747
748	      foreach ( $opt as $widget_id ) {
749	        $opt_single = $DW->getOpt($widget_id, 'single');
750	        if ( count($opt_single) > 0 ) {
751	          foreach ( $opt_single as $widget ) {
752	            if ( $widget->maintype == 'single' ) {
753	              $default_single = $widget->value;
754	            }
755	          }
756	        }
757	        $DW->addMultiOption($widget_id, 'single-tag', $default, array($term_id));
758	      }
759	    } // END if array_key_exists
760	  } // END if action
761	}
762
763	/**
764	 * dynwid_sql_mode() Internal check for STRICT sql mode
765	 * @since 1.3.6
766	 */
767	function dynwid_sql_mode() {
768		$wpdb = $GLOBALS['wpdb'];
769		$strict_mode = array('STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES');
770
771		$query = "SELECT @@GLOBAL.sql_mode";
772		$result = $wpdb->get_var($query);
773		$sql_global = explode(',', $result);
774
775		$query = "SELECT @@SESSION.sql_mode";
776		$result =  $wpdb->get_var($query);
777		$sql_session = explode(',', $result);
778
779		$sqlmode = array_merge($sql_global, $sql_session);
780		if ( (bool) array_intersect($sql_session, $strict_mode) ) {
781			return TRUE;
782		} else {
783			return FALSE;
784		}
785	}
786
787	/**
788	 * dynwid_term_tree() AJAX lazy loader for Taxonomy terms tree
789	 * @since 1.5.4.2
790	 *
791	 * @return void
792	 */
793	function dynwid_term_tree() {
794		include_once(DW_MODULES . 'custompost_module.php');
795
796		$DW = &$GLOBALS['DW'];
797
798		$id = ( isset($_POST['id']) && ! empty($_POST['id']) ) ? sanitize_text_field( $_POST['id'] ) : 0;
799		$name = ( isset($_POST['name']) && ! empty($_POST['name']) ) ? sanitize_text_field( $_POST['name'] ) : '';
800		$prefix = ( isset($_POST['prefix']) && ! empty($_POST['prefix']) ) ? sanitize_text_field( $_POST['prefix'] ) : '';
801		$widget_id = ( isset($_POST['widget_id']) && ! empty($_POST['widget_id']) ) ? sanitize_text_field( $_POST['widget_id'] ) : '';
802
803		if ( intval($id) > 0 && ! empty($name) && ! empty($widget_id) ) {
804			$opt_tax = $DW->getDWOpt($widget_id, $prefix);
805			$opt_tax_childs = $DW->getDWOpt($widget_id, $prefix . '-childs');
806
807			$tree = DW_CustomPost::getTaxChilds($name, array(), $id, array());
808			if ( count($tree) > 0 ) {
809				DW_CustomPost::prtTax($widget_id, $name, $tree, $opt_tax->act, $opt_tax_childs->act, $prefix);
810			}
811		}
812
813		die();
814	}
815
816	/**
817	 * dynwid_uninstall() Uninstall
818	 * @since 1.0
819	 */
820	function dynwid_uninstall() {
821		$wpdb = $GLOBALS['wpdb'];
822	  $dbtable = $wpdb->prefix . DW_DB_TABLE;
823
824    // Housekeeping
825		delete_option('dynwid_housekeeping_lastrun');
826		delete_option('dynwid_old_method');
827		delete_option('dynwid_version');
828
829		$query = "DROP TABLE IF EXISTS " . $dbtable;
830		$wpdb->query($query);
831
832	  $plugin = plugin_basename(__FILE__);
833
834	  /* Shamelessly ripped from /wp-admin/plugins.php */
835    deactivate_plugins($plugin);
836	  update_option('recently_activated', array($plugin => time()) + (array) get_option('recently_activated'));
837	  wp_redirect('plugins.php?deactivate=true&plugin_status=' . $status . '&paged=' . $page);
838
839    die();
840	}
841
842	/**
843	 * dynwid_widget_callback() Callback function for hooking into WP widgets admin
844	 * @since 1.2
845	 */
846	function dynwid_widget_callback() {
847	  $DW = &$GLOBALS['DW'];
848
849 		$DW->loadModules();
850  	$DW->getModuleName();
851
852	  $args = func_get_args();
853	  $widget_id = $args[0]['widget_id'];
854	  $wp_callback = $DW->registered_widget_controls[$widget_id]['wp_callback'];
855
856	  // Calling original callback first
857    call_user_func_array($wp_callback, $args);
858
859	  // Now adding the dynwid text & link
860	  echo '<p>' . __('Dynamic Widgets', DW_L10N_DOMAIN) . ': ';
861
862		if ( array_key_exists($widget_id, $DW->registered_widgets) ) {
863			echo '<a style="text-decoration:none;" title="' . __('Edit Dynamic Widgets Options', DW_L10N_DOMAIN) . '" href="themes.php?page=dynwid-config&action=edit&id=' . $widget_id . '&returnurl=widgets.php' . '">';
864			echo ( $DW->hasOptions($widget_id) ) ? __('Dynamic', DW_L10N_DOMAIN) : __('Static', DW_L10N_DOMAIN);
865			echo '</a>';
866		  if ( $DW->hasOptions($widget_id) ) {
867		    $s = array();
868		    $opt = $DW->getOpt($widget_id, NULL);
869		    foreach ( $opt as $widget ) {
870		      $type = $widget->maintype;
871		      if ( $type != 'individual' && substr($type, -6) != 'childs' && ! preg_match('/.*-tax_.*/', $type) ) {
872		        $single = array('single-author', 'single-category', 'single-tag', 'single-post');
873		        if ( in_array($type, $single) ) {
874		          $type = 'single';
875		        }
876		        if (! in_array($type, $s) ) {
877		          $s[ ] = $type;
878		        }
879		      }
880		    }
881
882		    $last = count($s) - 1;
883		    $string = '';
884		    for ( $i = 0; $i < $last; $i++ ) {
885		      $type = $s[$i];
886		      if (! empty($DW->dwoptions[$type]) ) {
887		        $string .= $DW->dwoptions[$type];
888					}
889		    	$string .= ( ($last - 1) == $i ) ? ' ' . __('and', DW_L10N_DOMAIN) . ' ' : ', ';
890		    }
891		    $type = $s[$last];
892		    if ( isset($DW->dwoptions[$type]) ) {
893		    	$string .= $DW->dwoptions[$type];
894		    }
895
896		    $output  = '<br /><small>';
897		    $output .= ( count($opt) > 1 ) ? __('Options set for', DW_L10N_DOMAIN) : __('Option set for', DW_L10N_DOMAIN);
898	      $output .= ' ' . $string . '.</small>';
899		    echo $output;
900		  }
901	  } else {
902		  echo '<em>' . __('Save the widget first', DW_L10N_DOMAIN) . '...</em>';
903		}
904	  echo '</p>';
905	}
906
907	/**
908	 * dynwid_worker() Worker process
909	 *
910	 * @param array $sidebars
911	 * @return array
912	 * @since 1.0
913	 */
914	function dynwid_worker($sidebars) {
915	  $DW = &$GLOBALS['DW'];
916
917		if ( $DW->listmade ) {
918			$DW->message('Dynamic Widgets removelist already created');
919			if ( count($DW->removelist) > 0 ) {
920				foreach ( $DW->removelist as $sidebar_id => $widgets ) {
921					foreach ( $widgets as $widget_key ){
922						unset($sidebars[$sidebar_id][$widget_key]);
923					}
924				}
925			}
926		} else {
927			require(dirname(__FILE__) . '/dynwid_worker.php');
928		}
929
930		return $sidebars;
931	}
932
933  // Hooks
934  add_action('admin_action_dynwid_dump', 'dynwid_admin_dump');
935  add_action('admin_action_wpec_dump', 'dynwid_admin_wpec_dump');
936  add_action('admin_action_dynwid_uninstall', 'dynwid_uninstall');
937  add_action('init', 'dynwid_init');
938  register_activation_hook(__FILE__, 'dynwid_install');
939?>