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

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

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 481 lines | 325 code | 92 blank | 64 comment | 147 complexity | 69cb22f4864e4b31d80c5c5a8fe4d14f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * dynwid_admin_save.php - Saving options to the database
  4. *
  5. * @version $Id: dynwid_admin_save.php 939272 2014-06-26 19:44:38Z qurl $
  6. * @copyright 2011 Jacco Drabbe
  7. */
  8. // Security - nonce, etc.
  9. $widget_id = ( isset($_POST['widget_id']) && ! empty($_POST['widget_id']) ) ? esc_attr($_POST['widget_id']) : '';
  10. $returnurl = ( isset($_POST['returnurl']) && ! empty($_POST['returnurl']) ) ? esc_url($_POST['returnurl']) : '';
  11. // In some cases $widget_id appears not to be global (anymore)
  12. $GLOBALS['widget_id'] = $widget_id;
  13. check_admin_referer('plugin-name-action_edit_' . $widget_id);
  14. if (! array_key_exists($widget_id, $DW->registered_widgets) ) {
  15. wp_die('WidgetID is not valid');
  16. }
  17. /* Checking basic stuff */
  18. $DW->registerOverrulers();
  19. foreach ( $DW->overrule_maintype as $o ) {
  20. if ( $o != 'date' && $o != 'url' ) {
  21. $act_field = $o . '_act';
  22. if ( isset($_POST[$act_field]) ) {
  23. if ( $_POST[$o] == 'no' && count($_POST[$act_field]) == 0 ) {
  24. wp_redirect( $_SERVER['REQUEST_URI'] . '&work=none' );
  25. die();
  26. }
  27. }
  28. }
  29. }
  30. // Date check
  31. if ( $_POST['date'] == 'no' ) {
  32. $date_start = trim(esc_attr($_POST['date_start']));
  33. $date_end = trim(esc_attr($_POST['date_end']));
  34. if (! preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/', $date_start) && ! preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/', $date_end) ) {
  35. wp_redirect( $_SERVER['REQUEST_URI'] . '&work=none' );
  36. die();
  37. }
  38. if (! empty($date_start) ) {
  39. @list($date_start_year, $date_start_month, $date_start_day ) = explode('-', $date_start);
  40. if (! checkdate($date_start_month, $date_start_day, $date_start_year) ) {
  41. unset($date_start);
  42. }
  43. }
  44. if (! empty($date_end) ) {
  45. @list($date_end_year, $date_end_month, $date_end_day ) = explode('-', $date_end);
  46. if (! checkdate($date_end_month, $date_end_day, $date_end_year) ) {
  47. unset($date_end);
  48. }
  49. }
  50. if (! empty($date_start) && ! empty($date_end) ) {
  51. if ( mktime(0, 0, 0, $date_start_month, $date_start_day, $date_start_year) > mktime(0, 0, 0, $date_end_month, $date_end_day, $date_end_year) ) {
  52. wp_redirect( $_SERVER['REQUEST_URI'] . '&work=nonedate' );
  53. die();
  54. }
  55. }
  56. }
  57. // URL
  58. if ( $_POST['url'] == 'no' && empty($_POST['url_value']) ) {
  59. wp_redirect( $_SERVER['REQUEST_URI'] . '&work=none' );
  60. die();
  61. }
  62. // IP
  63. if ( $_POST['ip'] == 'no' && empty($_POST['ip_value']) ) {
  64. wp_redirect( $_SERVER['REQUEST_URI'] . '&work=none' );
  65. die();
  66. }
  67. // Removing already set options, but keeping individual rules
  68. $dbtable = $GLOBALS['wpdb']->prefix . DW_DB_TABLE;
  69. $query = "SELECT COUNT(1) AS total FROM " . $dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = 'individual'";
  70. $count = $GLOBALS['wpdb']->get_var($query);
  71. if ( $count > 0 && isset($_POST['individual']) && $_POST['individual'] == '1' ) {
  72. $post_types = ( is_array($_POST['post_types']) ) ? $_POST['post_types'] : array();
  73. $post_types = array_merge( array('single_post', 'single_tag'), $post_types );
  74. foreach ( $post_types as $t ) {
  75. $maintype = (! preg_match('/^single/', $t) ) ? $t . '-post' : $t;
  76. $query = "SELECT name FROM " . $dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype . "'";
  77. $results = $GLOBALS['wpdb']->get_results($query);
  78. foreach ( $results as $row ) {
  79. if ( is_numeric($row->name) ) {
  80. $a = $maintype . '_act';
  81. if (! is_array($_POST[$a]) ) {
  82. $_POST[$a] = array();
  83. }
  84. $_POST[$a][ ] = $row->name;
  85. }
  86. }
  87. }
  88. }
  89. $DW->resetOptions($widget_id);
  90. // Role
  91. DWModule::save('role', 'complex');
  92. // Date
  93. if ( $_POST['date'] == 'no' ) {
  94. $dates = array();
  95. if ( preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/', $date_start) ) {
  96. $dates['date_start'] = $date_start;
  97. }
  98. if ( preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/', $date_end) ) {
  99. $dates['date_end'] = $date_end;
  100. }
  101. if ( count($dates) > 0 ) {
  102. $DW->addDate($widget_id, $dates);
  103. }
  104. }
  105. // Day
  106. DWModule::save('day', 'complex');
  107. // Week
  108. DWModule::save('week', 'complex');
  109. // Browser
  110. DWModule::save('browser', 'complex');
  111. // Device
  112. DWModule::save('device', 'complex');
  113. // Template
  114. DWModule::save('tpl', 'complex');
  115. // URL
  116. if (! empty($_POST['url_value']) ) {
  117. $urls = array();
  118. $url_values = trim($_POST['url_value']);
  119. $url_values = str_replace("\r", "", $url_values);
  120. $url_values = explode("\n", $url_values);
  121. foreach ( $url_values as $url ) {
  122. $url = trim($url);
  123. if (! empty($url) ) {
  124. $urls[ ] = $url;
  125. }
  126. }
  127. if ( count($urls) > 0 ) {
  128. $DW->addUrls($widget_id, $_POST['url'], $urls);
  129. }
  130. }
  131. // IP
  132. if (! empty($_POST['ip_value']) ) {
  133. $ips = array();
  134. $ip_values = trim($_POST['ip_value']);
  135. $ip_values = str_replace("\r", "", $ip_values);
  136. $ip_values = explode("\n", $ip_values);
  137. foreach ( $ip_values as $ip ) {
  138. $ip = trim($ip);
  139. if (! empty($ip) ) {
  140. $ips[ ] = $ip;
  141. }
  142. }
  143. if ( count($ips) > 0 ) {
  144. $DW->addIPs($widget_id, $_POST['ip'], $ips);
  145. }
  146. }
  147. // Front Page
  148. DWModule::save('front-page', 'complex');
  149. // Single Post
  150. DWModule::save('single');
  151. // -- Post Taxonomies
  152. if ( isset($_POST['single_tax_list']) && count($_POST['single_tax_list']) > 0 ) {
  153. foreach ( $_POST['single_tax_list'] as $tax ) {
  154. $act_tax_field = $tax . '_act';
  155. // if ( isset($_POST[$act_tax_field]) && count($_POST[$act_tax_field]) > 0 ) {
  156. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) ) {
  157. if ( substr($_POST[$act_tax_field], 0, 1) == ',' ) {
  158. $_POST[$act_tax_field] = substr($_POST[$act_tax_field], 1);
  159. }
  160. $t = explode(',', $_POST[$act_tax_field]);
  161. $t = array_unique($t);
  162. $DW->addMultiOption($widget_id, $tax, $_POST['single'], $t);
  163. }
  164. // ---- Childs >> Can't use DWModule::childSave() cause of $name != $tax, but $name == 'post'
  165. $act_tax_childs_field = $tax . '_childs_act';
  166. // if ( isset($_POST[$act_tax_field]) && count($_POST[$act_tax_field]) > 0 && isset($_POST[$act_tax_childs_field]) && count($_POST[$act_tax_childs_field]) > 0 ) {
  167. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) && isset($_POST[$act_tax_childs_field]) && ! empty($_POST[$act_tax_childs_field]) ) {
  168. if ( substr($_POST[$act_tax_childs_field], 0, 1) == ',' ) {
  169. $_POST[$act_tax_childs_field] = substr($_POST[$act_tax_childs_field], 1);
  170. }
  171. $t_childs = explode(',', $_POST[$act_tax_childs_field]);
  172. $t_childs = array_unique($t_childs);
  173. $DW->addChilds($widget_id, $tax . '-childs', $_POST['single'], $t, $t_childs);
  174. }
  175. }
  176. }
  177. // -- Author
  178. if ( isset($_POST['single_author_act']) && count($_POST['single_author_act']) > 0 ) {
  179. if ( $_POST['single'] == 'yes' ) {
  180. $DW->addSingleOption($widget_id, 'single', '1');
  181. }
  182. $DW->addMultiOption($widget_id, 'single-author', $_POST['single'], $_POST['single_author_act']);
  183. }
  184. // -- Category
  185. if ( isset($_POST['single_category_act']) && count($_POST['single_category_act']) > 0 ) {
  186. if ( $_POST['single'] == 'yes' && isset($_POST['single_author_act']) && count($_POST['single_author_act']) == 0 ) {
  187. $DW->addSingleOption($widget_id, 'single', '1');
  188. }
  189. $DW->addMultiOption($widget_id, 'single-category', $_POST['single'], $_POST['single_category_act']);
  190. }
  191. // ---- Childs
  192. if ( isset($_POST['single_category_act']) && count($_POST['single_category_act']) > 0 && isset($_POST['single_category_childs_act']) && count($_POST['single_category_childs_act']) > 0 ) {
  193. $DW->addChilds($widget_id, 'single-category-childs', $_POST['single'], $_POST['single_category_act'], $_POST['single_category_childs_act']);
  194. }
  195. // -- Individual / Posts / Tag
  196. if ( isset($_POST['individual']) && $_POST['individual'] == '1' ) {
  197. $DW->addSingleOption($widget_id, 'individual', '1');
  198. if ( isset($_POST['single_post_act']) && count($_POST['single_post_act']) > 0 ) {
  199. $DW->addMultiOption($widget_id, 'single-post', $_POST['single'], $_POST['single_post_act']);
  200. }
  201. if ( isset($_POST['single_tag_act']) && count($_POST['single_tag_act']) > 0 ) {
  202. $DW->addMultiOption($widget_id, 'single-tag', $_POST['single'], $_POST['single_tag_act']);
  203. }
  204. }
  205. // Attachment
  206. DWModule::save('attachment');
  207. // Pages
  208. // Go through the page_tax_list - Workaround as for some reason get_object_taxonomies() is not always filled
  209. $page_taxonomy = FALSE;
  210. $page_tax_list = array();
  211. if ( isset($_POST['page_tax_list']) && count($_POST['page_tax_list']) > 0 ) {
  212. foreach ( $_POST['page_tax_list'] as $tax ) {
  213. $act_tax_field = $tax . '_act';
  214. if ( isset($_POST[$act_tax_field]) && count($_POST[$act_tax_field]) > 0 ) {
  215. $page_taxonomy = TRUE;
  216. break;
  217. }
  218. }
  219. }
  220. if ( (isset($_POST['page_act']) && count($_POST['page_act']) > 0) || $page_taxonomy ) {
  221. if (! isset($_POST['page_act']) ) {
  222. $_POST['page_act'] = array();
  223. }
  224. $DW->addMultiOption($widget_id, 'page', $_POST['page'], $_POST['page_act']);
  225. } else if ( $_POST['page'] == 'no' ) {
  226. $DW->addSingleOption($widget_id, 'page');
  227. }
  228. // -- Childs
  229. DWModule::childSave('page');
  230. // -- Page Taxonomies
  231. if ( isset($_POST['page_tax_list']) && count($_POST['page_tax_list']) > 0 ) {
  232. foreach ( $_POST['page_tax_list'] as $tax ) {
  233. $act_tax_field = $tax . '_act';
  234. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) ) {
  235. if ( substr($_POST[$act_tax_field], 0, 1) == ',' ) {
  236. $_POST[$act_tax_field] = substr($_POST[$act_tax_field], 1);
  237. }
  238. $t = explode(',', $_POST[$act_tax_field]);
  239. $t = array_unique($t);
  240. $DW->addMultiOption($widget_id, $tax, $_POST['page'], $t);
  241. }
  242. // ---- Childs >> Can't use DWModule::childSave() cause of $name != $tax, but $name == 'page'
  243. $act_tax_childs_field = $tax . '_childs_act';
  244. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) && isset($_POST[$act_tax_childs_field]) && ! empty($_POST[$act_tax_childs_field]) ) {
  245. if ( substr($_POST[$act_tax_childs_field], 0, 1) == ',' ) {
  246. $_POST[$act_tax_childs_field] = substr($_POST[$act_tax_childs_field], 1);
  247. }
  248. $t_childs = explode(',', $_POST[$act_tax_childs_field]);
  249. $t_childs = array_unique($t_childs);
  250. $DW->addChilds($widget_id, $tax . '-childs', $_POST['page'], $t, $t_childs);
  251. }
  252. }
  253. }
  254. // Author
  255. DWModule::save('author', 'complex');
  256. // Categories
  257. DWModule::save('category', 'complex');
  258. DWModule::childSave('category'); // -- Childs
  259. // Tags
  260. DWModule::save('tag', 'complex');
  261. // Archive
  262. DWModule::save('archive');
  263. // Error 404
  264. DWModule::save('e404');
  265. // Search
  266. DWModule::save('search');
  267. // Custom Types
  268. if ( isset($_POST['post_types']) ) {
  269. foreach ( $_POST['post_types'] as $type ) {
  270. if ( isset($_POST['individual']) && $_POST['individual'] == '1' ) {
  271. if ( isset($_POST[$type . '-post_act']) && count($_POST[$type . '-post_act']) > 0 ) {
  272. $DW->addMultiOption($widget_id, $type . '-post', $_POST[$type], $_POST[$type . '-post_act']);
  273. }
  274. }
  275. // Check taxonomies
  276. $taxonomy = FALSE;
  277. // Go through the tax_list - Workaround as for some reason get_object_taxonomies() is not always filled
  278. $tax_list = array();
  279. $len = strlen($type);
  280. if ( isset($_POST['tax_list']) && count($_POST['tax_list']) > 0 ) {
  281. foreach ( $_POST['tax_list'] as $tl ) {
  282. if ( substr($tl, 0, $len) == $type ) {
  283. $tax_list[] = $tl;
  284. }
  285. }
  286. }
  287. foreach ( $tax_list as $tax ) {
  288. $act_tax_field = $tax . '_act';
  289. if ( isset($_POST[$act_tax_field]) && count($_POST[$act_tax_field]) > 0 ) {
  290. $taxonomy = TRUE;
  291. break;
  292. }
  293. }
  294. $act_field = $type . '_act';
  295. if ( (isset($_POST[$act_field]) && count($_POST[$act_field]) > 0) || $taxonomy ) {
  296. if (! isset($_POST[$act_field]) ) {
  297. $_POST[$act_field] = array();
  298. }
  299. $DW->addMultiOption($widget_id, $type, $_POST[$type], $_POST[$act_field]);
  300. } else if ( $_POST[$type] == 'no' ) {
  301. $DW->addSingleOption($widget_id, $type);
  302. }
  303. // -- Childs
  304. DWModule::childSave($type);
  305. // -- Taxonomies
  306. foreach ( $tax_list as $tax ) {
  307. $act_tax_field = $tax . '_act';
  308. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) ) {
  309. if ( substr($_POST[$act_tax_field], 0, 1) == ',' ) {
  310. $_POST[$act_tax_field] = substr($_POST[$act_tax_field], 1);
  311. }
  312. $t = explode(',', $_POST[$act_tax_field]);
  313. $t = array_unique($t);
  314. $DW->addMultiOption($widget_id, $tax, $_POST[$type], $t);
  315. }
  316. // ---- Childs >> Can't use DWModule::childSave() cause of $name != $tax, but $name == $type
  317. $act_tax_childs_field = $tax . '_childs_act';
  318. if ( isset($_POST[$act_tax_field]) && ! empty($_POST[$act_tax_field]) && isset($_POST[$act_tax_childs_field]) && ! empty($_POST[$act_tax_childs_field]) ) {
  319. if ( substr($_POST[$act_tax_childs_field], 0, 1) == ',' ) {
  320. $_POST[$act_tax_childs_field] = substr($_POST[$act_tax_childs_field], 1);
  321. }
  322. $t_childs = explode(',', $_POST[$act_tax_childs_field]);
  323. $t_childs = array_unique($t_childs);
  324. $DW->addChilds($widget_id, $tax . '-childs', $_POST[$type], $t, $t_childs);
  325. }
  326. }
  327. }
  328. DWModule::save('cp_archive', 'complex');
  329. }
  330. // Custom Taxonomies
  331. if ( isset($_POST['dw_taxonomy']) ) {
  332. foreach ( $_POST['dw_taxonomy'] as $tax ) {
  333. $type = 'tax_' . $tax;
  334. $act_field = $type . '_act';
  335. if ( isset($_POST[$act_field]) && ! empty($_POST[$act_field]) ) {
  336. /* if (! is_array($_POST[$act_field]) ) {
  337. $_POST[$act_field] = array();
  338. } */
  339. if ( substr($_POST[$act_field], 0, 1) == ',' ) {
  340. $_POST[$act_field] = substr($_POST[$act_field], 1);
  341. }
  342. $t = explode(',', $_POST[$act_field]);
  343. $t = array_unique($t);
  344. $DW->addMultiOption($widget_id, $type, $_POST[$type], $t);
  345. } else if ( $_POST[$type] == 'no' ) {
  346. $DW->addSingleOption($widget_id, $type);
  347. }
  348. DWModule::childSave($type);
  349. }
  350. }
  351. // WPML PLugin support
  352. DWModule::save('wpml', 'complex');
  353. // QTranslate Plugin support
  354. DWModule::save('qt', 'complex');
  355. // WPSC/WPEC Plugin support
  356. DWModule::save('wpsc', 'complex');
  357. // bbPress Plugin support
  358. DWModule::save('bbp_profile', 'simple');
  359. // BP Plugin support
  360. DWModule::save('bp', 'complex');
  361. // BP Plugin support (Groups)
  362. DWModule::save('bp-group', 'complex');
  363. // Pods Plugin support
  364. DWModule::save('pods', 'complex');
  365. // Redirect to ReturnURL
  366. if (! empty($returnurl) ) {
  367. $q = array();
  368. // Checking if there are arguments set
  369. $pos = strpos($returnurl, '?');
  370. if ( $pos !== FALSE ) {
  371. // evaluate the args
  372. $query_string = substr($returnurl, ($pos+1));
  373. $args = explode('&', $query_string);
  374. foreach ( $args as $arg ) {
  375. @list($name, $value) = explode('=', $arg);
  376. if ( $name != 'dynwid_save' && $name != 'widget_id' ) {
  377. $q[ ] = $name . '=' . $value;
  378. }
  379. }
  380. $script_url = substr($returnurl, 0, $pos);
  381. } else {
  382. $script_url = $returnurl;
  383. }
  384. $q[ ] = 'dynwid_save=yes';
  385. $q[ ] = 'widget_id=' . $widget_id;
  386. wp_redirect( $script_url . '?' . implode('&', $q) );
  387. die();
  388. }
  389. ?>