PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  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. // Constants
  65. define('DW_CLASSES', dirname(__FILE__) . '/' . 'classes/');
  66. define('DW_DEBUG', FALSE);
  67. define('DW_DB_TABLE', 'dynamic_widgets');
  68. define('DW_L10N_DOMAIN', 'dynamic-widgets');
  69. define('DW_LIST_LIMIT', 20);
  70. define('DW_LIST_STYLE', 'style="overflow:auto;height:240px;"');
  71. define('DW_OLD_METHOD', get_option('dynwid_old_method'));
  72. define('DW_PAGE_LIMIT', get_option('dynwid_page_limit', 500));
  73. define('DW_MINIMUM_PHP', '5.2.7');
  74. define('DW_MINIMUM_WP', '3.0');
  75. define('DW_MODULES', dirname(__FILE__) . '/' . 'mods/');
  76. define('DW_PLUGIN', dirname(__FILE__) . '/' . 'plugin/');
  77. define('DW_TIME_LIMIT', 86400); // 1 day
  78. define('DW_URL_AUTHOR', 'http://www.qurl.nl');
  79. define('DW_VERSION', '1.5.9');
  80. define('DW_WPML_API', '/inc/wpml-api.php'); // WPML Plugin support - API file relative to ICL_PLUGIN_PATH
  81. define('DW_WPML_ICON', 'img/wpml_icon.png'); // WPML Plugin support - WPML icon
  82. // Classes - only PHP5
  83. if ( version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=') ) {
  84. require_once(dirname(__FILE__) . '/dynwid_class.php');
  85. }
  86. // Functions
  87. /**
  88. * dynwid_activate() Activate the plugin
  89. * @since 1.3.3
  90. */
  91. function dynwid_activate() {
  92. $wpdb = $GLOBALS['wpdb'];
  93. $dbtable = $wpdb->prefix . DW_DB_TABLE;
  94. $query = "CREATE TABLE IF NOT EXISTS " . $dbtable . " (
  95. id int(11) NOT NULL auto_increment,
  96. widget_id varchar(60) NOT NULL,
  97. maintype varchar(50) NOT NULL,
  98. `name` varchar(40) NOT NULL,
  99. `value` longtext NOT NULL,
  100. PRIMARY KEY (id),
  101. KEY widget_id (widget_id,maintype)
  102. );";
  103. $wpdb->query($query);
  104. // Version check
  105. $version = get_option('dynwid_version');
  106. if ( $version !== FALSE ) {
  107. /* 1.2 > Added support for widget display setting options for Author Pages.
  108. Need to apply archive rule to author also to keep same behavior. */
  109. if ( version_compare($version, '1.2', '<') ) {
  110. $query = "SELECT widget_id FROM " . $dbtable . " WHERE maintype = 'archive'";
  111. $results = $wpdb->get_results($query);
  112. foreach ( $results as $myrow ) {
  113. $query = "INSERT INTO " . $dbtable . "(widget_id, maintype, value) VALUES ('" . $myrow->widget_id . "', 'author', '0')";
  114. $wpdb->query($query);
  115. }
  116. }
  117. /* 1.3 > Added Date (range) support.
  118. Need to change DB `value` to a LONGTEXT type
  119. (not for the date of course, but for supporting next features which might need a lot of space) */
  120. if ( version_compare($version, '1.3', '<') ) {
  121. $query = "ALTER TABLE " . $dbtable . " CHANGE `value` `value` LONGTEXT NOT NULL";
  122. $wpdb->query($query);
  123. }
  124. /* 1.4.0.5 > Enlarged the maintype field because of addition of CTs */
  125. if ( version_compare($version, '1.4.0.5', '<') ) {
  126. $query = "ALTER TABLE " . $dbtable . " CHANGE `maintype` `maintype` VARCHAR(50) NOT NULL";
  127. $wpdb->query($query);
  128. }
  129. /* 1.4.0.12 > Added MSIE 6 support in browser module
  130. Need to apply MSIE rule to MSIE6 to keep same behavior. */
  131. if ( version_compare($version, '1.4.0.12', '<') ) {
  132. $query = "SELECT widget_id, value FROM " . $dbtable . " WHERE maintype = 'browser' AND name = 'msie'";
  133. $results = $wpdb->get_results($query);
  134. foreach ( $results as $myrow ) {
  135. $query = "INSERT INTO " . $dbtable . "(widget_id, maintype, name, value) VALUES ('" . $myrow->widget_id . "', 'browser', 'msie6', '" . $myrow->value . "')";
  136. $wpdb->query($query);
  137. }
  138. }
  139. /* 1.5b3 > Added support for widget display setting options for Tag Pages.
  140. Need to apply archive rule to tag also to keep same behavior. */
  141. if ( version_compare($version, '1.5b3', '<') ) {
  142. $query = "SELECT widget_id FROM " . $dbtable . " WHERE maintype = 'archive'";
  143. $results = $wpdb->get_results($query);
  144. foreach ( $results as $myrow ) {
  145. $query = "INSERT INTO " . $dbtable . "(widget_id, maintype, value) VALUES ('" . $myrow->widget_id . "', 'tag', '0')";
  146. $wpdb->query($query);
  147. }
  148. }
  149. /*
  150. 1.5.3.1 > Widgets seems to be started using longer classnames to avoid clashing.
  151. Widend up the width for widget_id from 40 to 60.
  152. */
  153. if ( version_compare($version, '1.5.3.1', '<') ) {
  154. $query = "ALTER TABLE " . $dbtable . " CHANGE `widget_id` `widget_id` VARCHAR(60) NOT NULL";
  155. $wpdb->query($query);
  156. }
  157. }
  158. update_option('dynwid_version', DW_VERSION);
  159. }
  160. /**
  161. * dynwid_add_admin_custom_box Adds meta boxes to Custom Post Types
  162. * @since 1.5.2.5
  163. */
  164. function dynwid_add_admin_custom_box() {
  165. $args = array(
  166. 'public' => TRUE,
  167. '_builtin' => FALSE
  168. );
  169. $post_types = get_post_types($args, 'objects', 'and');
  170. foreach ( array_keys($post_types) as $type ) {
  171. add_meta_box('dynwid', __('Dynamic Widgets', DW_L10N_DOMAIN), 'dynwid_add_post_control', $type, 'side', 'low');
  172. }
  173. }
  174. /**
  175. * dynwid_add_admin_help_tab() Add help tab for WP >= 3.3
  176. * @since 1.5.0
  177. */
  178. function dynwid_add_admin_help_tab() {
  179. $dw_admin_screen = $GLOBALS['dw_admin_screen'];
  180. $screen = get_current_screen();
  181. if ( $screen->id == $dw_admin_screen ) {
  182. // Contextual help
  183. if ( isset($_GET['action']) && $_GET['action'] == 'edit' ) {
  184. $dw_help = dynwid_contextual_help_text('edit');
  185. } else {
  186. $dw_help = dynwid_contextual_help_text('overview');
  187. }
  188. $args = array( 'id' => 'dw_help_tab',
  189. 'title' => 'Dynamic Widgets',
  190. 'content' => $dw_help
  191. );
  192. $screen->add_help_tab($args);
  193. }
  194. }
  195. /**
  196. * dynwid_add_admin_menu() Add plugin link to admin menu
  197. * @since 1.0
  198. */
  199. function dynwid_add_admin_menu() {
  200. global $dw_admin_screen;
  201. $DW = &$GLOBALS['DW'];
  202. $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');
  203. if ( $DW->enabled ) {
  204. add_action('admin_print_styles-' . $dw_admin_screen, 'dynwid_add_admin_styles');
  205. add_action('admin_print_scripts-' . $dw_admin_screen, 'dynwid_add_admin_scripts');
  206. // Contextual help
  207. if ( isset($_GET['action']) && $_GET['action'] == 'edit' ) {
  208. $dw_help = dynwid_contextual_help_text('edit');
  209. } else {
  210. $dw_help = dynwid_contextual_help_text('overview');
  211. }
  212. // Since WP 3.3 contextual help is handled different
  213. if ( version_compare($GLOBALS['wp_version'], '3.3', '>=') ) {
  214. add_action('load-' . $dw_admin_screen, 'dynwid_add_admin_help_tab');
  215. } else {
  216. add_contextual_help($dw_admin_screen, $dw_help);
  217. }
  218. // Only show meta box in posts panel when there are widgets enabled.
  219. $opt = $DW->getOpt('%','individual');
  220. if ( count($opt) > 0 ) {
  221. add_meta_box('dynwid', __('Dynamic Widgets', DW_L10N_DOMAIN), 'dynwid_add_post_control', 'post', 'side', 'low');
  222. }
  223. }
  224. }
  225. /**
  226. * dynwid_add_admin_scripts() Enqueue jQuery UI scripts to admin page
  227. * @since 1.3
  228. */
  229. function dynwid_add_admin_scripts() {
  230. $DW = &$GLOBALS['DW'];
  231. /*
  232. BuddyPress doing an overall JS enqueue (BAD!)
  233. Workaround fixing a js error with ui.accordion freezing the screen
  234. - dtheme-ajax-js is used in BP default theme
  235. - bp-js is used in BP Compatibility Plugin
  236. */
  237. if ( wp_script_is('dtheme-ajax-js') ) {
  238. wp_deregister_script('dtheme-ajax-js');
  239. }
  240. if ( wp_script_is('bp-js') ) {
  241. wp_deregister_script('bp-js');
  242. }
  243. wp_enqueue_script('jquery');
  244. wp_enqueue_script('jquery-ui-core');
  245. if ( version_compare(substr($GLOBALS['wp_version'], 0, 3), '3.1', '>=') ) {
  246. wp_enqueue_script('jquery-ui-widget');
  247. // wp_enqueue_script('jquery-ui-accordion', $DW->plugin_url . 'ui.accordion.1.8.7.js', array('jquery-ui-widget'));
  248. wp_enqueue_script('jquery-ui-datepicker', $DW->plugin_url . 'ui.datepicker.1.8.7.js', array('jquery-ui-widget'));
  249. } else {
  250. // wp_enqueue_script('jquery-ui-accordion', $DW->plugin_url . 'ui.accordion.1.7.3.js', array('jquery-ui-core'));
  251. wp_enqueue_script('jquery-ui-datepicker', $DW->plugin_url . 'ui.datepicker.1.7.3.js', array('jquery-ui-core'));
  252. }
  253. }
  254. /**
  255. * dynwid_add_admin_styles() Enqueue CSS to admin page
  256. * @since 1.3
  257. */
  258. function dynwid_add_admin_styles() {
  259. $DW = &$GLOBALS['DW'];
  260. if ( version_compare(substr($GLOBALS['wp_version'], 0, 3), '3.1', '>=') ) {
  261. wp_enqueue_style('jquery-ui-custom', $DW->plugin_url . 'jquery-ui-1.8.7.custom.css');
  262. } else {
  263. wp_enqueue_style('jquery-ui-custom', $DW->plugin_url . 'jquery-ui-1.7.3.custom.css');
  264. }
  265. }
  266. /**
  267. * dynwid_add_plugin_actions() Add settings link in WP plugin overview
  268. * @param array $all
  269. * @return array
  270. * @since 1.0
  271. */
  272. function dynwid_add_plugin_actions($all) {
  273. $links = array();
  274. $links[ ] = '<a href="themes.php?page=dynwid-config">' . __('Settings') . '</a>';
  275. return array_merge($links, $all);
  276. }
  277. /**
  278. * dynwid_add_post_control() Add control widget to post screen
  279. * @since 1.2
  280. */
  281. function dynwid_add_post_control() {
  282. $post = $GLOBALS['post'];
  283. $DW = &$GLOBALS['DW'];
  284. $post_type = get_post_type($post->ID);
  285. if ( $post_type == 'post') {
  286. $post_type = 'single';
  287. $maintype = 'single-post';
  288. } else {
  289. $maintype = $post_type . '-post';
  290. }
  291. $opt = $DW->getOpt('%','individual');
  292. echo '<strong>' . __('Apply exception rule to widgets:', DW_L10N_DOMAIN) . '</strong><br /><br />';
  293. foreach ( $opt as $widget ) {
  294. $single_condition = '1';
  295. $checked = '';
  296. $opt_single = $DW->getOpt($widget->widget_id, $post_type);
  297. // loop through the opts to see if we have a match
  298. foreach ( $opt_single as $widget_opt ) {
  299. if ( $widget_opt->maintype == 'single' ) {
  300. $single_condition = $widget_opt->value;
  301. }
  302. if ( $widget_opt->maintype == $maintype && $widget_opt->name == $post->ID ) {
  303. $checked = ' checked="checked"';
  304. }
  305. }
  306. $default = ( $single_condition == '0' ) ? __('Off', DW_L10N_DOMAIN) : __('On', DW_L10N_DOMAIN);
  307. 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 />';
  308. }
  309. }
  310. /**
  311. * dynwid_add_tag_page() Add row to WP tags admin
  312. * @since 1.2
  313. */
  314. function dynwid_add_tag_page() {
  315. $DW = &$GLOBALS['DW'];
  316. // Only show dynwid row when there are widgets enabled
  317. $opt = $DW->getOpt('%','individual');
  318. if ( count($opt) > 0 ) {
  319. echo '<tr class="form-field">';
  320. echo '<th scope="row" valign="top"><label for="dynamic-widgets">' . __('Dynamic Widgets', DW_L10N_DOMAIN) . '</label></th>';
  321. echo '<td>';
  322. foreach ( $opt as $widget ) {
  323. $single_condition = '1';
  324. $checked = '';
  325. $opt_single = $DW->getOpt($widget->widget_id, 'single');
  326. // loop through the opts to see if we have a match
  327. foreach ( $opt_single as $widget_opt ) {
  328. if ( $widget_opt->maintype == 'single' ) {
  329. $single_condition = $widget_opt->value;
  330. }
  331. if ( $widget_opt->maintype == 'single-tag' && $widget_opt->name == $_GET['tag_ID'] ) {
  332. $checked = ' checked="checked"';
  333. }
  334. }
  335. $default = ( $single_condition == '0' ) ? __('Off', DW_L10N_DOMAIN) : __('On', DW_L10N_DOMAIN);
  336. 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 />';
  337. } // END foreach opt
  338. echo '</td>';
  339. echo '</tr>';
  340. }
  341. }
  342. /**
  343. * dynwid_add_widget_control() Preparation for callback hook into WP widgets admin
  344. * @since 1.2
  345. */
  346. function dynwid_add_widget_control() {
  347. $DW = &$GLOBALS['DW'];
  348. /*
  349. Hooking into the callback of the widgets by moving the existing callback to wp_callback
  350. and setting callback with own callback function.
  351. We need the widget_id registered in params also for calling own callback.
  352. */
  353. foreach ( $DW->registered_widgets as $widget_id => $widget ) {
  354. if ( array_key_exists($widget_id, $DW->registered_widget_controls) ) {
  355. $DW->registered_widget_controls[$widget_id]['wp_callback'] = $DW->registered_widget_controls[$widget_id]['callback'];
  356. $DW->registered_widget_controls[$widget_id]['callback'] = 'dynwid_widget_callback';
  357. /*
  358. In odd cases params and/or params[0] seems not to be an array. Bugfix for:
  359. Warning: Cannot use a scalar value as an array in ./wp-content/plugins/dynamic-widgets/dynamic-widgets.php
  360. */
  361. /* Fixing params */
  362. if (! is_array($DW->registered_widget_controls[$widget_id]['params']) ) {
  363. $DW->registered_widget_controls[$widget_id]['params'] = array();
  364. }
  365. if ( count($DW->registered_widget_controls[$widget_id]['params']) == 0 ) {
  366. $DW->registered_widget_controls[$widget_id]['params'][ ] = array('widget_id' => $widget_id);
  367. // Fixing params[0]
  368. } else if (! is_array($DW->registered_widget_controls[$widget_id]['params'][0]) ) {
  369. $DW->registered_widget_controls[$widget_id]['params'][0] = array('widget_id' => $widget_id);
  370. } else {
  371. $DW->registered_widget_controls[$widget_id]['params'][0]['widget_id'] = $widget_id;
  372. }
  373. }
  374. }
  375. // Notifying user when options are saved and returned to ./wp-admin/widgets.php
  376. if ( isset($_GET['dynwid_save']) && $_GET['dynwid_save'] == 'yes' ) {
  377. add_action('sidebar_admin_page', 'dynwid_add_widget_page');
  378. }
  379. }
  380. /**
  381. * dynwid_add_widget_page() Save success message for WP widgets admin
  382. * @since 1.2
  383. */
  384. function dynwid_add_widget_page() {
  385. $DW = &$GLOBALS['DW'];
  386. $name = strip_tags($DW->getName($_GET['widget_id']));
  387. $lead = __('Dynamic Widgets Options saved', DW_L10N_DOMAIN);
  388. $msg = __('for', DW_L10N_DOMAIN) . ' ' . $name;
  389. DWMessageBox::create($lead, $msg);
  390. }
  391. /**
  392. * dynwid_admin_dump() Dump function
  393. * @since 1.0
  394. */
  395. function dynwid_admin_dump() {
  396. $DW = &$GLOBALS['DW'];
  397. header('Content-Description: File Transfer');
  398. header('Content-Disposition: attachment; filename=dynwid_dump_' . date('Ymd') . '.txt' );
  399. header('Content-Type: text/plain');
  400. $DW->dump();
  401. die();
  402. }
  403. /**
  404. * dynwid_admin_page() Admin pages
  405. * @since 1.0
  406. */
  407. function dynwid_admin_page() {
  408. $DW = &$GLOBALS['DW'];
  409. require_once(dirname(__FILE__) . '/dynwid_admin.php');
  410. }
  411. /**
  412. * dynwid_admin_wpec_dump() Dump WPEC rules function for upgrade to 3.8
  413. * @since 1.4.0
  414. */
  415. function dynwid_admin_wpec_dump() {
  416. $DW = &$GLOBALS['DW'];
  417. $wpdb = &$GLOBALS['wpdb'];
  418. $dump = array();
  419. $opt = $DW->getOpt('%', 'wpsc');
  420. $categories = array();
  421. $table = WPSC_TABLE_PRODUCT_CATEGORIES;
  422. $fields = array('id', 'name');
  423. $query = "SELECT " . implode(', ', $fields) . " FROM " . $table . " WHERE active = '1' ORDER BY name";
  424. $results = $wpdb->get_results($query);
  425. foreach ( $results as $myrow ) {
  426. $categories[$myrow->id] = $myrow->name;
  427. }
  428. foreach ( $opt as $widget ) {
  429. $id = $widget->widget_id;
  430. if (! array_key_exists($id, $dump) ) {
  431. $dump[$id] = array( 'name' => strip_tags($DW->getName($widget->widget_id)) );
  432. }
  433. if ( $widget->name == 'default' ) {
  434. $dump[$id]['default'] = ( $widget->value == '0' ? 'No' : 'Yes' );
  435. } else {
  436. $v = $widget->name;
  437. $dump[$id][ ] = $categories[$v];
  438. }
  439. }
  440. header('Content-Description: File Transfer');
  441. header('Content-Disposition: attachment; filename=dynwid_wpec_dump_' . date('Ymd') . '.txt' );
  442. header('Content-Type: text/plain');
  443. foreach ( $dump as $widget ) {
  444. echo 'Widget: ' . $widget['name'] . "\r\n";
  445. echo 'Default set to ' . $widget['default'] . "\r\n";
  446. if ( count($widget) > 2 ) {
  447. echo 'Categories ticked: ' . "\r\n";
  448. foreach ( $widget as $k => $v ) {
  449. if ( is_int($k) ) {
  450. echo "\t" . $v . "\r\n";
  451. }
  452. }
  453. }
  454. echo "\r\n";
  455. }
  456. die();
  457. }
  458. /**
  459. * dynwid_contextual_help_text() Actual text to place into the contextual help screen
  460. * @param string $screen
  461. * @return string
  462. * @since 1.5.0
  463. *
  464. */
  465. function dynwid_contextual_help_text($screen) {
  466. $DW = &$GLOBALS['DW'];
  467. // Contextual help
  468. if ( $screen == 'edit' ) {
  469. $dw_help = __('Widgets are always displayed by default', DW_L10N_DOMAIN) . ' (' . __('The \'<em>Yes</em>\' selection', DW_L10N_DOMAIN) . ')' . '<br />';
  470. $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 />';
  471. $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);
  472. } else {
  473. $dw_help = '<p><strong>' . __('Static', DW_L10N_DOMAIN) . ' / ' . __('Dynamic', DW_L10N_DOMAIN) . '</strong><br />';
  474. $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 />';
  475. $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>';
  476. $dw_help .= '<p><strong>' . __('Reset', DW_L10N_DOMAIN) . '</strong><br />';
  477. $dw_help .= __('Reset makes the widget return to', DW_L10N_DOMAIN) . ' <em>' . __('Static', DW_L10N_DOMAIN) . '</em>.</p>';
  478. }
  479. return $dw_help;
  480. }
  481. /**
  482. * dynwid_disabled_add_admin_menu() Menu entry for disabled page.
  483. * @since 1.5.6.1
  484. *
  485. */
  486. function dynwid_disabled_add_admin_menu() {
  487. add_submenu_page('themes.php', __('Dynamic Widgets', DW_L10N_DOMAIN), __('Dynamic Widgets', DW_L10N_DOMAIN), 'edit_theme_options', 'dynwid-config', 'dynwid_disabled_page');
  488. }
  489. /**
  490. * dynwid_disabled_page() Error boxes to show in admin when DW can not be initialised due to not meeting sysreq.
  491. * @since 1.5b1
  492. *
  493. */
  494. function dynwid_disabled_page() {
  495. // As the DWMessagebox class is not loaded, we can not use it
  496. $php = version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=');
  497. $wp = version_compare($GLOBALS['wp_version'], DW_MINIMUM_WP, '>=');
  498. if (! $php ) {
  499. echo '<div class="error" id="message"><p>';
  500. _e('<b>ERROR</b> Your host is running a too low version of PHP. Dynamic Widgets needs at least version', DW_L10N_DOMAIN);
  501. echo ' ' . DW_MINIMUM_PHP . '.';
  502. echo '</p></div>';
  503. }
  504. if (! $wp ) {
  505. echo '<div class="error" id="message"><p>';
  506. _e('<b>ERROR</b> Your host is running a too low version of WordPress. Dynamic Widgets needs at least version', DW_L10N_DOMAIN);
  507. echo ' ' . DW_MINIMUM_WP . '.';
  508. echo '</p></div>';
  509. }
  510. }
  511. /**
  512. * dynwid_filter_init() Init of the worker
  513. * @since 1.3.5
  514. */
  515. function dynwid_filter_init() {
  516. $DW = &$GLOBALS['DW'];
  517. require(dirname(__FILE__) . '/dynwid_init_worker.php');
  518. }
  519. /**
  520. * dynwid_filter_widgets() Worker
  521. * @since 1.3.5
  522. */
  523. function dynwid_filter_widgets() {
  524. $DW = &$GLOBALS['DW'];
  525. dynwid_filter_init();
  526. if ( DW_OLD_METHOD ) {
  527. dynwid_worker($DW->sidebars);
  528. } else {
  529. add_filter('sidebars_widgets', 'dynwid_worker');
  530. }
  531. }
  532. /**
  533. * dynwid_init() Init of the plugin
  534. * @since 1.0
  535. */
  536. function dynwid_init() {
  537. $php = version_compare(PHP_VERSION, DW_MINIMUM_PHP, '>=');
  538. $wp = version_compare($GLOBALS['wp_version'], DW_MINIMUM_WP, '>=');
  539. if ( $php && $wp ) {
  540. $GLOBALS['DW'] = new dynWid();
  541. $DW = &$GLOBALS['DW'];
  542. $DW->plugin_url = WP_PLUGIN_URL . '/' . str_replace( basename(__FILE__), '', plugin_basename(__FILE__) );
  543. if ( is_admin() ) {
  544. if ( isset($_POST['dynwid_save']) && $_POST['dynwid_save'] == 'yes' ) {
  545. require_once(dirname(__FILE__) . '/dynwid_admin_save.php');
  546. }
  547. load_plugin_textdomain(DW_L10N_DOMAIN, FALSE, dirname(plugin_basename(__FILE__)) . '/locale');
  548. add_action('admin_menu', 'dynwid_add_admin_menu');
  549. if ( $DW->enabled ) {
  550. add_action('add_meta_boxes', 'dynwid_add_admin_custom_box');
  551. add_action('edit_tag_form_fields', 'dynwid_add_tag_page');
  552. add_action('edited_term', 'dynwid_save_tagdata');
  553. add_action('plugin_action_links_' . plugin_basename(__FILE__), 'dynwid_add_plugin_actions');
  554. add_action('save_post', 'dynwid_save_postdata');
  555. add_action('sidebar_admin_setup', 'dynwid_add_widget_control');
  556. // AJAX calls
  557. add_action('wp_ajax_term_tree', 'dynwid_term_tree');
  558. }
  559. } else {
  560. if ( $DW->enabled ) {
  561. add_action('wp_head', 'dynwid_filter_widgets');
  562. }
  563. }
  564. } else {
  565. if ( is_admin() ) {
  566. // Show errors in the admin page
  567. add_action('admin_menu', 'dynwid_disabled_add_admin_menu');
  568. }
  569. }
  570. }
  571. /**
  572. * dynwid_install() Installation
  573. * @since 1.3.1
  574. */
  575. function dynwid_install() {
  576. if ( function_exists('is_multisite') ) {
  577. if ( is_multisite() && isset($_GET['networkwide']) && $_GET['networkwide'] == '1' ) {
  578. $plugin = plugin_basename(__FILE__);
  579. deactivate_plugins($plugin);
  580. } else {
  581. dynwid_activate();
  582. }
  583. } else {
  584. dynwid_activate();
  585. }
  586. }
  587. /**
  588. * dynwid_save_postdata() Save of options via post screen
  589. * @param int $post_id
  590. * @since 1.2
  591. */
  592. function dynwid_save_postdata($post_id) {
  593. $DW = &$GLOBALS['DW'];
  594. if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && $_POST['action'] != 'autosave' ) {
  595. $post_id = ( isset($_POST['post_ID']) && ! empty($_POST['post_ID']) ) ? intval($_POST['post_ID']) : 0;
  596. if ( $parent_id = wp_is_post_revision($post_id) ) {
  597. $post_id = $parent_id;
  598. }
  599. if ( $post_id > 0 ) {
  600. $post_type = get_post_type($post_id);
  601. if ( $post_type == 'post') {
  602. $post_type = 'single';
  603. $maintype = 'single-post';
  604. } else {
  605. $maintype = $post_type . '-post';
  606. }
  607. // Housekeeping
  608. $opt = $DW->getOpt('%','individual');
  609. foreach ( $opt as $widget ) {
  610. $DW->deleteOption($widget->widget_id, $maintype, $post_id);
  611. }
  612. if ( array_key_exists('dw-single-post', $_POST) ) {
  613. $opt = $_POST['dw-single-post'];
  614. $default = 'yes';
  615. $default_single = '1';
  616. foreach ( $opt as $widget_id ) {
  617. $opt_single = $DW->getOpt($widget_id, $post_type);
  618. if ( count($opt_single) > 0 ) {
  619. foreach ( $opt_single as $widget ) {
  620. if ( $widget->maintype == $post_type ) {
  621. $default_single = $widget->value;
  622. }
  623. }
  624. if ( $default_single == '0' ) {
  625. $default = 'no';
  626. }
  627. }
  628. $DW->addMultiOption($widget_id, $maintype, $default, array($post_id));
  629. }
  630. } // END if array_key_exists
  631. } // END if $post_id > 0
  632. } // END if ! autosave AND ! quick edit
  633. }
  634. /**
  635. * dynwid_save_tagdata() Save of tagdata
  636. * @param int $term_id
  637. * @since 1.2
  638. */
  639. function dynwid_save_tagdata($term_id) {
  640. // Only act when tag is updated via 'edit', NOT via 'quick edit'
  641. if ( $_POST['action'] == 'editedtag' ) {
  642. $DW = &$GLOBALS['DW'];
  643. if ( array_key_exists('tag_ID', $_POST) ) {
  644. $term_id = $_POST['tag_ID'];
  645. }
  646. // Housekeeping
  647. $opt = $DW->getOpt('%', 'individual');
  648. foreach ( $opt as $widget ) {
  649. $DW->deleteOption($widget->widget_id, 'single-tag', $term_id);
  650. }
  651. if ( array_key_exists('dw-single-tag', $_POST) ) {
  652. $opt = $_POST['dw-single-tag'];
  653. $default = 'yes';
  654. $default_single = '1';
  655. foreach ( $opt as $widget_id ) {
  656. $opt_single = $DW->getOpt($widget_id, 'single');
  657. if ( count($opt_single) > 0 ) {
  658. foreach ( $opt_single as $widget ) {
  659. if ( $widget->maintype == 'single' ) {
  660. $default_single = $widget->value;
  661. }
  662. }
  663. }
  664. $DW->addMultiOption($widget_id, 'single-tag', $default, array($term_id));
  665. }
  666. } // END if array_key_exists
  667. } // END if action
  668. }
  669. /**
  670. * dynwid_sql_mode() Internal check for STRICT sql mode
  671. * @since 1.3.6
  672. */
  673. function dynwid_sql_mode() {
  674. $wpdb = $GLOBALS['wpdb'];
  675. $strict_mode = array('STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES');
  676. $query = "SELECT @@GLOBAL.sql_mode";
  677. $result = $wpdb->get_var($query);
  678. $sql_global = explode(',', $result);
  679. $query = "SELECT @@SESSION.sql_mode";
  680. $result = $wpdb->get_var($query);
  681. $sql_session = explode(',', $result);
  682. $sqlmode = array_merge($sql_global, $sql_session);
  683. if ( (bool) array_intersect($sql_session, $strict_mode) ) {
  684. return TRUE;
  685. } else {
  686. return FALSE;
  687. }
  688. }
  689. /**
  690. * dynwid_term_tree() AJAX lazy loader for Taxonomy terms tree
  691. * @since 1.5.4.2
  692. *
  693. * @return void
  694. */
  695. function dynwid_term_tree() {
  696. include_once(DW_MODULES . 'custompost_module.php');
  697. $DW = &$GLOBALS['DW'];
  698. $id = ( isset($_POST['id']) && ! empty($_POST['id']) ) ? sanitize_text_field( $_POST['id'] ) : 0;
  699. $name = ( isset($_POST['name']) && ! empty($_POST['name']) ) ? sanitize_text_field( $_POST['name'] ) : '';
  700. $prefix = ( isset($_POST['prefix']) && ! empty($_POST['prefix']) ) ? sanitize_text_field( $_POST['prefix'] ) : '';
  701. $widget_id = ( isset($_POST['widget_id']) && ! empty($_POST['widget_id']) ) ? sanitize_text_field( $_POST['widget_id'] ) : '';
  702. if ( intval($id) > 0 && ! empty($name) && ! empty($widget_id) ) {
  703. $opt_tax = $DW->getDWOpt($widget_id, $prefix);
  704. $opt_tax_childs = $DW->getDWOpt($widget_id, $prefix . '-childs');
  705. $tree = DW_CustomPost::getTaxChilds($name, array(), $id, array());
  706. if ( count($tree) > 0 ) {
  707. DW_CustomPost::prtTax($widget_id, $name, $tree, $opt_tax->act, $opt_tax_childs->act, $prefix);
  708. }
  709. }
  710. die();
  711. }
  712. /**
  713. * dynwid_uninstall() Uninstall
  714. * @since 1.0
  715. */
  716. function dynwid_uninstall() {
  717. $wpdb = $GLOBALS['wpdb'];
  718. $dbtable = $wpdb->prefix . DW_DB_TABLE;
  719. // Housekeeping
  720. delete_option('dynwid_housekeeping_lastrun');
  721. delete_option('dynwid_old_method');
  722. delete_option('dynwid_version');
  723. $query = "DROP TABLE IF EXISTS " . $dbtable;
  724. $wpdb->query($query);
  725. $plugin = plugin_basename(__FILE__);
  726. /* Shamelessly ripped from /wp-admin/plugins.php */
  727. deactivate_plugins($plugin);
  728. update_option('recently_activated', array($plugin => time()) + (array) get_option('recently_activated'));
  729. wp_redirect('plugins.php?deactivate=true&plugin_status=' . $status . '&paged=' . $page);
  730. die();
  731. }
  732. /**
  733. * dynwid_widget_callback() Callback function for hooking into WP widgets admin
  734. * @since 1.2
  735. */
  736. function dynwid_widget_callback() {
  737. $DW = &$GLOBALS['DW'];
  738. $DW->loadModules();
  739. $DW->getModuleName();
  740. $args = func_get_args();
  741. $widget_id = $args[0]['widget_id'];
  742. $wp_callback = $DW->registered_widget_controls[$widget_id]['wp_callback'];
  743. // Calling original callback first
  744. call_user_func_array($wp_callback, $args);
  745. // Now adding the dynwid text & link
  746. echo '<p>' . __('Dynamic Widgets', DW_L10N_DOMAIN) . ': ';
  747. if ( array_key_exists($widget_id, $DW->registered_widgets) ) {
  748. 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' . '">';
  749. echo ( $DW->hasOptions($widget_id) ) ? __('Dynamic', DW_L10N_DOMAIN) : __('Static', DW_L10N_DOMAIN);
  750. echo '</a>';
  751. if ( $DW->hasOptions($widget_id) ) {
  752. $s = array();
  753. $opt = $DW->getOpt($widget_id, NULL);
  754. foreach ( $opt as $widget ) {
  755. $type = $widget->maintype;
  756. if ( $type != 'individual' && substr($type, -6) != 'childs' && ! preg_match('/.*-tax_.*/', $type) ) {
  757. $single = array('single-author', 'single-category', 'single-tag', 'single-post');
  758. if ( in_array($type, $single) ) {
  759. $type = 'single';
  760. }
  761. if (! in_array($type, $s) ) {
  762. $s[ ] = $type;
  763. }
  764. }
  765. }
  766. $last = count($s) - 1;
  767. $string = '';
  768. for ( $i = 0; $i < $last; $i++ ) {
  769. $type = $s[$i];
  770. if (! empty($DW->dwoptions[$type]) ) {
  771. $string .= $DW->dwoptions[$type];
  772. }
  773. $string .= ( ($last - 1) == $i ) ? ' ' . __('and', DW_L10N_DOMAIN) . ' ' : ', ';
  774. }
  775. $type = $s[$last];
  776. if ( isset($DW->dwoptions[$type]) ) {
  777. $string .= $DW->dwoptions[$type];
  778. }
  779. $output = '<br /><small>';
  780. $output .= ( count($opt) > 1 ) ? __('Options set for', DW_L10N_DOMAIN) : __('Option set for', DW_L10N_DOMAIN);
  781. $output .= ' ' . $string . '.</small>';
  782. echo $output;
  783. }
  784. } else {
  785. echo '<em>' . __('Save the widget first', DW_L10N_DOMAIN) . '...</em>';
  786. }
  787. echo '</p>';
  788. }
  789. /**
  790. * dynwid_worker() Worker process
  791. *
  792. * @param array $sidebars
  793. * @return array
  794. * @since 1.0
  795. */
  796. function dynwid_worker($sidebars) {
  797. $DW = &$GLOBALS['DW'];
  798. if ( $DW->listmade ) {
  799. $DW->message('Dynamic Widgets removelist already created');
  800. if ( count($DW->removelist) > 0 ) {
  801. foreach ( $DW->removelist as $sidebar_id => $widgets ) {
  802. foreach ( $widgets as $widget_key ){
  803. unset($sidebars[$sidebar_id][$widget_key]);
  804. }
  805. }
  806. }
  807. } else {
  808. require(dirname(__FILE__) . '/dynwid_worker.php');
  809. }
  810. return $sidebars;
  811. }
  812. // Hooks
  813. add_action('admin_action_dynwid_dump', 'dynwid_admin_dump');
  814. add_action('admin_action_wpec_dump', 'dynwid_admin_wpec_dump');
  815. add_action('admin_action_dynwid_uninstall', 'dynwid_uninstall');
  816. add_action('init', 'dynwid_init');
  817. register_activation_hook(__FILE__, 'dynwid_install');
  818. ?>