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

/wp-content/plugins/LayerSlider/wp/actions.php

https://gitlab.com/webkod3r/tripolis
PHP | 666 lines | 471 code | 116 blank | 79 comment | 132 complexity | 07cda28699d57242e3a5a15f3bb60941 MD5 | raw file
  1. <?php
  2. add_action('init', 'ls_register_form_actions');
  3. function ls_register_form_actions() {
  4. if(current_user_can(get_option('layerslider_custom_capability', 'manage_options'))) {
  5. // Remove slider
  6. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'remove') {
  7. if(check_admin_referer('remove_'.$_GET['id'])) {
  8. add_action('admin_init', 'layerslider_removeslider');
  9. }
  10. }
  11. // Restore slider
  12. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'restore') {
  13. if(check_admin_referer('restore_'.$_GET['id'])) {
  14. LS_Sliders::restore( (int) $_GET['id'] );
  15. header('Location: admin.php?page=layerslider'); die();
  16. }
  17. }
  18. // Duplicate slider
  19. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'duplicate') {
  20. if(check_admin_referer('duplicate_'.$_GET['id'])) {
  21. add_action('admin_init', 'layerslider_duplicateslider');
  22. }
  23. }
  24. // Import sample sliders
  25. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'import_sample') {
  26. if(check_admin_referer('import-sample-sliders')) {
  27. add_action('admin_init', 'layerslider_import_sample_slider');
  28. }
  29. }
  30. // Slider list bulk actions
  31. if(isset($_POST['ls-bulk-action'])) {
  32. if(check_admin_referer('bulk-action')) {
  33. add_action('admin_init', 'ls_sliders_bulk_action');
  34. }
  35. }
  36. // Add new slider
  37. if(isset($_POST['ls-add-new-slider'])) {
  38. if(check_admin_referer('add-slider')) {
  39. add_action('admin_init', 'ls_add_new_slider');
  40. }
  41. }
  42. // Google Fonts
  43. if(isset($_POST['ls-save-google-fonts'])) {
  44. if(check_admin_referer('save-google-fonts')) {
  45. add_action('admin_init', 'ls_save_google_fonts');
  46. }
  47. }
  48. // Advanced settings
  49. if(isset($_POST['ls-save-advanced-settings'])) {
  50. if(check_admin_referer('save-advanced-settings')) {
  51. add_action('admin_init', 'ls_save_advanced_settings');
  52. }
  53. }
  54. // Access permission
  55. if(isset($_POST['ls-access-permission'])) {
  56. if(check_admin_referer('save-access-permissions')) {
  57. add_action('admin_init', 'ls_save_access_permissions');
  58. }
  59. }
  60. // Import sliders
  61. if(isset($_POST['ls-import'])) {
  62. if(check_admin_referer('import-sliders')) {
  63. add_action('admin_init', 'ls_import_sliders');
  64. }
  65. }
  66. // Export sliders
  67. if(isset($_POST['ls-export'])) {
  68. if(check_admin_referer('export-sliders')) {
  69. add_action('admin_init', 'ls_export_sliders');
  70. }
  71. }
  72. // Custom CSS editor
  73. if(isset($_POST['ls-user-css'])) {
  74. if(check_admin_referer('save-user-css')) {
  75. add_action('admin_init', 'ls_save_user_css');
  76. }
  77. }
  78. // Skin editor
  79. if(isset($_POST['ls-user-skins'])) {
  80. if(check_admin_referer('save-user-skin')) {
  81. add_action('admin_init', 'ls_save_user_skin');
  82. }
  83. }
  84. // Transition builder
  85. if(isset($_POST['ls-user-transitions'])) {
  86. if(check_admin_referer('save-user-transitions')) {
  87. add_action('admin_init', 'ls_save_user_transitions');
  88. }
  89. }
  90. // Compatibility: convert old sliders to new data storage since 3.6
  91. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'convert') {
  92. if(check_admin_referer('convertoldsliders')) {
  93. add_action('admin_init', 'layerslider_convert');
  94. }
  95. }
  96. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'hide-support-notice') {
  97. if(check_admin_referer('hide-support-notice')) {
  98. update_option('ls-show-support-notice', 0);
  99. header('Location: admin.php?page=layerslider');
  100. die();
  101. }
  102. }
  103. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'hide-update-notice') {
  104. if(check_admin_referer('hide-update-notice')) {
  105. $latest = get_option('ls-latest-version', LS_PLUGIN_VERSION);
  106. update_option('ls-last-update-notification', $latest);
  107. header('Location: admin.php?page=layerslider');
  108. die();
  109. }
  110. }
  111. if(isset($_GET['page']) && $_GET['page'] == 'layerslider' && isset($_GET['action']) && $_GET['action'] == 'hide-revalidation-notice') {
  112. if(check_admin_referer('hide-revalidation-notice')) {
  113. update_option('ls-show-revalidation-notice', 0);
  114. header('Location: admin.php?page=layerslider');
  115. die();
  116. }
  117. }
  118. // AJAX functions
  119. add_action('wp_ajax_ls_save_slider', 'ls_save_slider');
  120. add_action('wp_ajax_ls_save_screen_options', 'ls_save_screen_options');
  121. add_action('wp_ajax_ls_get_mce_sliders', 'ls_get_mce_sliders');
  122. add_action('wp_ajax_ls_get_post_details', 'ls_get_post_details');
  123. add_action('wp_ajax_ls_get_taxonomies', 'ls_get_taxonomies');
  124. }
  125. }
  126. function ls_add_new_slider() {
  127. $id = LS_Sliders::add($_POST['title']);
  128. header('Location: admin.php?page=layerslider&action=edit&id='.$id.'&showsettings=1');
  129. die();
  130. }
  131. function ls_sliders_bulk_action() {
  132. // Remove
  133. if($_POST['action'] === 'remove') {
  134. if(!empty($_POST['sliders']) && is_array($_POST['sliders'])) {
  135. foreach($_POST['sliders'] as $item) {
  136. LS_Sliders::remove( intval($item) );
  137. delete_transient('ls-slider-data-'.intval($item));
  138. }
  139. header('Location: admin.php?page=layerslider&message=removeSuccess'); die();
  140. } else {
  141. header('Location: admin.php?page=layerslider&message=removeSelectError&error=1'); die();
  142. }
  143. }
  144. // Delete
  145. if($_POST['action'] === 'delete') {
  146. if(!empty($_POST['sliders']) && is_array($_POST['sliders'])) {
  147. foreach($_POST['sliders'] as $item) {
  148. LS_Sliders::delete( intval($item));
  149. delete_transient('ls-slider-data-'.intval($item));
  150. }
  151. header('Location: admin.php?page=layerslider&message=deleteSuccess'); die();
  152. } else {
  153. header('Location: admin.php?page=layerslider&message=deleteSelectError&error=1'); die();
  154. }
  155. }
  156. // Restore
  157. if($_POST['action'] === 'restore') {
  158. if(!empty($_POST['sliders']) && is_array($_POST['sliders'])) {
  159. foreach($_POST['sliders'] as $item) { LS_Sliders::restore( intval($item)); }
  160. header('Location: admin.php?page=layerslider&message=restoreSuccess'); die();
  161. } else {
  162. header('Location: admin.php?page=layerslider&message=restoreSelectError&error=1'); die();
  163. }
  164. }
  165. // Merge
  166. if($_POST['action'] === 'merge') {
  167. // Error check
  168. if(!isset($_POST['sliders'][1]) || !is_array($_POST['sliders'])) {
  169. header('Location: admin.php?page=layerslider&error=1&message=mergeSelectError');
  170. die();
  171. }
  172. if($sliders = LS_Sliders::find($_POST['sliders'])) {
  173. foreach($sliders as $key => $item) {
  174. // Get IDs
  175. $ids[] = '#' . $item['id'];
  176. // Merge slides
  177. if($key === 0) { $data = $item['data']; }
  178. else { $data['layers'] = array_merge($data['layers'], $item['data']['layers']); }
  179. }
  180. // Save as new
  181. $name = 'Merged sliders of ' . implode(', ', $ids);
  182. $data['properties']['title'] = $name;
  183. LS_Sliders::add($name, $data);
  184. }
  185. header('Location: admin.php?page=layerslider&message=mergeSuccess');
  186. die();
  187. }
  188. }
  189. function ls_save_google_fonts() {
  190. // Build object to save
  191. $fonts = array();
  192. if(isset($_POST['urlParams'])) {
  193. foreach($_POST['urlParams'] as $key => $val) {
  194. if(!empty($val)) {
  195. $fonts[] = array(
  196. 'param' => $val,
  197. 'admin' => isset($_POST['onlyOnAdmin'][$key]) ? true : false
  198. );
  199. }
  200. }
  201. }
  202. // Google Fonts character sets
  203. array_shift($_POST['scripts']);
  204. update_option('ls-google-font-scripts', $_POST['scripts']);
  205. // Save & redirect back
  206. update_option('ls-google-fonts', $fonts);
  207. header('Location: admin.php?page=layerslider&message=googleFontsUpdated');
  208. die();
  209. }
  210. function ls_save_advanced_settings() {
  211. $options = array('use_cache', 'include_at_footer', 'conditional_script_loading', 'concatenate_output', 'use_custom_jquery', 'put_js_to_body');
  212. foreach($options as $item) {
  213. update_option('ls_'.$item, array_key_exists($item, $_POST));
  214. }
  215. header('Location: admin.php?page=layerslider&message=generalUpdated');
  216. die();
  217. }
  218. function ls_save_screen_options() {
  219. $_POST['options'] = !empty($_POST['options']) ? $_POST['options'] : array();
  220. update_option('ls-screen-options', $_POST['options']);
  221. die();
  222. }
  223. function ls_get_mce_sliders() {
  224. $sliders = LS_Sliders::find(array('limit' => 50));
  225. foreach($sliders as $key => $item) {
  226. $sliders[$key]['preview'] = apply_filters('ls_get_preview_for_slider', $item );
  227. }
  228. die(json_encode($sliders));
  229. }
  230. function ls_save_slider() {
  231. // Vars
  232. $id = (int) $_POST['id'];
  233. $data = $_POST['sliderData'];
  234. // Parse slider settings
  235. $data['properties'] = json_decode(stripslashes(html_entity_decode($data['properties'])), true);
  236. // Parse slide data
  237. if(!empty($data['layers']) && is_array($data['layers'])) {
  238. foreach($data['layers'] as $slideKey => $slideData) {
  239. $data['layers'][$slideKey] = json_decode(stripslashes(html_entity_decode($slideData)), true);
  240. }
  241. }
  242. $title = esc_sql($data['properties']['title']);
  243. $slug = !empty($data['properties']['slug']) ? esc_sql($data['properties']['slug']) : '';
  244. // Relative URL
  245. if(isset($data['properties']['relativeurls'])) {
  246. $data = layerslider_convert_urls($data);
  247. }
  248. // WPML
  249. if(function_exists('icl_register_string')) {
  250. layerslider_register_wpml_strings($id, $data);
  251. }
  252. // Delete transient (if any) to
  253. // invalidate outdated data
  254. delete_transient('ls-slider-data-'.$id);
  255. // Update the slider
  256. if(empty($id)) {
  257. LS_Sliders::add($title, $data);
  258. } else {
  259. LS_Sliders::update($id, $title, $data, $slug);
  260. }
  261. die(json_encode(array('status' => 'ok')));
  262. }
  263. /********************************************************/
  264. /* Action to duplicate slider */
  265. /********************************************************/
  266. function layerslider_duplicateslider() {
  267. // Check and get the ID
  268. $id = (int) $_GET['id'];
  269. if(!isset($_GET['id'])) {
  270. return;
  271. }
  272. // Get the original slider
  273. $slider = LS_Sliders::find( (int)$_GET['id'] );
  274. $data = $slider['data'];
  275. // Name check
  276. if(empty($data['properties']['title'])) {
  277. $data['properties']['title'] = 'Unnamed';
  278. }
  279. // Insert the duplicate
  280. $data['properties']['title'] .= ' copy';
  281. LS_Sliders::add($data['properties']['title'], $data);
  282. // Success
  283. header('Location: admin.php?page=layerslider');
  284. die();
  285. }
  286. /********************************************************/
  287. /* Action to remove slider */
  288. /********************************************************/
  289. function layerslider_removeslider() {
  290. // Check received data
  291. if(empty($_GET['id'])) { return false; }
  292. // Remove the slider
  293. LS_Sliders::remove( intval($_GET['id']) );
  294. // Delete transient cache
  295. delete_transient('ls-slider-data-'.intval($_GET['id']));
  296. // Reload page
  297. header('Location: admin.php?page=layerslider');
  298. die();
  299. }
  300. /********************************************************/
  301. /* Action to import sample slider */
  302. /********************************************************/
  303. function layerslider_import_sample_slider() {
  304. // Get samples and importUtil
  305. $sliders = LS_Sources::getDemoSliders();
  306. include LS_ROOT_PATH.'/classes/class.ls.importutil.php';
  307. // Check reference
  308. if(!empty($_GET['slider']) && $_GET['slider'] == 'all') {
  309. foreach($sliders as $item) {
  310. if(file_exists($item['file'])) {
  311. $import = new LS_ImportUtil($item['file']);
  312. }
  313. }
  314. } elseif(!empty($_GET['slider']) && is_string($_GET['slider'])) {
  315. if($item = LS_Sources::getDemoSlider($_GET['slider'])) {
  316. if(file_exists($item['file'])) {
  317. $import = new LS_ImportUtil($item['file']);
  318. }
  319. }
  320. }
  321. header('Location: '.menu_page_url('layerslider', 0));
  322. die();
  323. }
  324. // PLUGIN USER PERMISSIONS
  325. //-------------------------------------------------------
  326. function ls_save_access_permissions() {
  327. // Get capability
  328. $capability = ($_POST['custom_role'] == 'custom') ? $_POST['custom_capability'] : $_POST['custom_role'];
  329. // Test value
  330. if(empty($capability) || !current_user_can($capability)) {
  331. header('Location: admin.php?page=layerslider&error=1&message=permissionError');
  332. die();
  333. } else {
  334. update_option('layerslider_custom_capability', $capability);
  335. header('Location: admin.php?page=layerslider&message=permissionSuccess');
  336. die();
  337. }
  338. }
  339. // IMPORT SLIDERS
  340. //-------------------------------------------------------
  341. function ls_import_sliders() {
  342. // Check export file if any
  343. if(!is_uploaded_file($_FILES['import_file']['tmp_name'])) {
  344. header('Location: '.$_SERVER['REQUEST_URI'].'&error=1&message=importSelectError');
  345. die('No data received.');
  346. }
  347. include LS_ROOT_PATH.'/classes/class.ls.importutil.php';
  348. $import = new LS_ImportUtil($_FILES['import_file']['tmp_name'], $_FILES['import_file']['name']);
  349. header('Location: '.menu_page_url('layerslider', 0));
  350. die();
  351. }
  352. // EXPORT SLIDERS
  353. //-------------------------------------------------------
  354. function ls_export_sliders() {
  355. // Get sliders
  356. if(isset($_POST['sliders'][0]) && $_POST['sliders'][0] == -1) {
  357. $sliders = LS_Sliders::find(array('limit' => 500));
  358. } elseif(!empty($_POST['sliders'])) {
  359. $sliders = LS_Sliders::find($_POST['sliders']);
  360. } else {
  361. header('Location: admin.php?page=layerslider&error=1&message=exportSelectError');
  362. die('Invalid data received.');
  363. }
  364. // Check results
  365. if(empty($sliders)) {
  366. header('Location: admin.php?page=layerslider&error=1&message=exportNotFound');
  367. die('Invalid data received.');
  368. }
  369. if(class_exists('ZipArchive')) {
  370. include LS_ROOT_PATH.'/classes/class.ls.exportutil.php';
  371. $zip = new LS_ExportUtil;
  372. }
  373. // Gather slider data
  374. foreach($sliders as $item) {
  375. // Slider settings array for fallback mode
  376. $data[] = $item['data'];
  377. // If ZipArchive is available
  378. if(class_exists('ZipArchive')) {
  379. // Add slider folder and settings.json
  380. $name = empty($item['name']) ? 'slider_' . $item['id'] : $item['name'];
  381. $name = sanitize_file_name($name);
  382. $zip->addSettings(json_encode($item['data']), $name);
  383. // Add images?
  384. if(!isset($_POST['skip_images'])) {
  385. $images = $zip->getImagesForSlider($item['data']);
  386. $images = $zip->getFSPaths($images);
  387. $zip->addImage($images, $name);
  388. }
  389. }
  390. }
  391. if(class_exists('ZipArchive')) {
  392. $zip->download();
  393. } else {
  394. $name = 'LayerSlider Export '.date('Y-m-d').' at '.date('H.i.s').'.json';
  395. header('Content-type: application/force-download');
  396. header('Content-Disposition: attachment; filename="'.str_replace(' ', '_', $name).'"');
  397. die(base64_encode(json_encode($data)));
  398. }
  399. }
  400. // TRANSITION BUILDER
  401. //-------------------------------------------------------
  402. function ls_save_user_css() {
  403. // Get target file and content
  404. $upload_dir = wp_upload_dir();
  405. $file = $upload_dir['basedir'].'/layerslider.custom.css';
  406. // Attempt to save changes
  407. if(is_writable($upload_dir['basedir'])) {
  408. file_put_contents($file, stripslashes($_POST['contents']));
  409. header('Location: admin.php?page=ls-style-editor&edited=1');
  410. die();
  411. // File isn't writable
  412. } else {
  413. wp_die(__("It looks like your files isn't writable, so PHP couldn't make any changes (CHMOD).", "LayerSlider"), __('Cannot write to file', 'LayerSlider'), array('back_link' => true) );
  414. }
  415. }
  416. // SKIN EDITOR
  417. //-------------------------------------------------------
  418. function ls_save_user_skin() {
  419. // Error checking
  420. if(empty($_POST['skin']) || strpos($_POST['skin'], '..') !== false) {
  421. wp_die(__("It looks like you haven't selected any skin to edit.", "LayerSlider"), __('No skin selected.', 'LayerSlider'), array('back_link' => true) );
  422. }
  423. // Get skin file and contents
  424. $skin = LS_Sources::getSkin($_POST['skin']);
  425. $file = $skin['file'];
  426. // Attempt to write the file
  427. if(is_writable($file)) {
  428. file_put_contents($file, stripslashes($_POST['contents']));
  429. header('Location: admin.php?page=ls-skin-editor&skin='.$skin['handle'].'&edited=1');
  430. } else {
  431. wp_die(__("It looks like your files isn't writable, so PHP couldn't make any changes (CHMOD).", "LayerSlider"), __('Cannot write to file', 'LayerSlider'), array('back_link' => true) );
  432. }
  433. }
  434. // TRANSITION BUILDER
  435. //-------------------------------------------------------
  436. function ls_save_user_transitions() {
  437. // Array to hold transitions
  438. $transitions = array();
  439. // Get transitions
  440. $transitions['t2d'] = isset($_POST['t2d']) ? $_POST['t2d'] : array();
  441. $transitions['t3d'] = isset($_POST['t3d']) ? $_POST['t3d'] : array();
  442. array_walk_recursive($transitions['t2d'], 'layerslider_builder_convert_numbers');
  443. array_walk_recursive($transitions['t3d'], 'layerslider_builder_convert_numbers');
  444. // Iterate over the sections
  445. foreach($transitions['t3d'] as $key => $val) {
  446. // Rows
  447. if(strstr($val['rows'], ',')) { $tmp = explode(',', $val['rows']); $tmp[0] = (int) trim($tmp[0]); $tmp[1] = (int) trim($tmp[1]); $transitions['t3d'][$key]['rows'] = $tmp; }
  448. else { $transitions['t3d'][$key]['rows'] = (int) $val['rows']; }
  449. // Cols
  450. if(strstr($val['cols'], ',')) { $tmp = explode(',', $val['cols']); $tmp[0] = (int) trim($tmp[0]); $tmp[1] = (int) trim($tmp[1]); $transitions['t3d'][$key]['cols'] = $tmp; }
  451. else { $transitions['t3d'][$key]['cols'] = (int) $val['cols']; }
  452. // Depth
  453. if(isset($val['tile']['depth'])) {
  454. $transitions['t3d'][$key]['tile']['depth'] = 'large'; }
  455. // Before
  456. if(!isset($val['before']['enabled'])) {
  457. unset($transitions['t3d'][$key]['before']['transition']); }
  458. // After
  459. if(!isset($val['after']['enabled'])) {
  460. unset($transitions['t3d'][$key]['after']['transition']); }
  461. }
  462. // Iterate over the sections
  463. foreach($transitions['t2d'] as $key => $val) {
  464. if(strstr($val['rows'], ',')) { $tmp = explode(',', $val['rows']); $tmp[0] = (int) trim($tmp[0]); $tmp[1] = (int) trim($tmp[1]); $transitions['t2d'][$key]['rows'] = $tmp; }
  465. else { $transitions['t2d'][$key]['rows'] = (int) $val['rows']; }
  466. if(strstr($val['cols'], ',')) { $tmp = explode(',', $val['cols']); $tmp[0] = (int) trim($tmp[0]); $tmp[1] = (int) trim($tmp[1]); $transitions['t2d'][$key]['cols'] = $tmp; }
  467. else { $transitions['t2d'][$key]['cols'] = (int) $val['cols']; }
  468. if(empty($val['transition']['rotateX'])) {
  469. unset($transitions['t2d'][$key]['transition']['rotateX']); }
  470. if(empty($val['transition']['rotateY'])) {
  471. unset($transitions['t2d'][$key]['transition']['rotateY']); }
  472. if(empty($val['transition']['rotate'])) {
  473. unset($transitions['t2d'][$key]['transition']['rotate']); }
  474. if(empty($val['transition']['scale']) || $val['transition']['scale'] == '1.0' || $val['transition']['scale'] == '1') {
  475. unset($transitions['t2d'][$key]['transition']['scale']); }
  476. }
  477. // Save transitions
  478. $upload_dir = wp_upload_dir();
  479. $custom_trs = $upload_dir['basedir'] . '/layerslider.custom.transitions.js';
  480. $data = 'var layerSliderCustomTransitions = '.json_encode($transitions).';';
  481. file_put_contents($custom_trs, $data);
  482. die('SUCCESS');
  483. }
  484. // --
  485. function ls_get_post_details() {
  486. $params = $_POST['params'];
  487. $queryArgs = array(
  488. 'post_status' => 'publish',
  489. 'limit' => 30,
  490. 'posts_per_page' => 30,
  491. 'post_type' => $params['post_type']
  492. );
  493. if(!empty($params['post_orderby'])) {
  494. $queryArgs['orderby'] = $params['post_orderby']; }
  495. if(!empty($params['post_order'])) {
  496. $queryArgs['order'] = $params['post_order']; }
  497. if(!empty($params['post_categories'][0])) {
  498. $queryArgs['category__in'] = $params['post_categories']; }
  499. if(!empty($params['post_tags'][0])) {
  500. $queryArgs['tag__in'] = $params['post_tags']; }
  501. if(!empty($params['post_taxonomy']) && !empty($params['post_tax_terms'])) {
  502. $queryArgs['tax_query'][] = array(
  503. 'taxonomy' => $params['post_taxonomy'],
  504. 'field' => 'id',
  505. 'terms' => $params['post_tax_terms']
  506. );
  507. }
  508. $posts = LS_Posts::find($queryArgs)->getParsedObject();
  509. die(json_encode($posts));
  510. }
  511. function ls_get_taxonomies() {
  512. die(json_encode(array_values(get_terms($_POST['taxonomy']))));
  513. }