PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-homepage-slideshow/homepageslideshow.php

https://bitbucket.org/lgorence/quickpress
PHP | 827 lines | 709 code | 23 blank | 95 comment | 104 complexity | 3d43260f74e2f763a46906c43cec487b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /*
  3. Plugin Name: Homepage Slideshow
  4. Version: 2.2
  5. Plugin URI: http://xmlswf.com
  6. Description: A Gallery Management Plugin
  7. Author: XML / SWF
  8. Author URI: http://xmlswf.com
  9. */
  10. define('HSS_PLUGIN_DIR', dirname(__FILE__));
  11. define('HSS_PLUGIN_URL', WP_PLUGIN_URL . '/' . basename(HSS_PLUGIN_DIR));
  12. define('HSS_PLUGIN_UPLOADS_DIR', WP_CONTENT_DIR . '/uploads/homepageslideshow');
  13. define('HSS_PLUGIN_UPLOADS_URL', WP_CONTENT_URL . '/uploads/homepageslideshow');
  14. define('HSS_PLUGIN_XML_DIR', HSS_PLUGIN_DIR . '/xml');
  15. define('HSS_PLUGIN_XML_URL', HSS_PLUGIN_URL . '/xml');
  16. require_once HSS_PLUGIN_DIR . '/functions.php';
  17. class HomepageSlideshow
  18. {
  19. private $errors = array();
  20. public $messages = array();
  21. public $def_settings = null;
  22. public function __construct()
  23. {
  24. $this->def_settings = hss_get_def_settings();
  25. register_activation_hook(__FILE__, array($this, 'activate'));
  26. register_deactivation_hook(__FILE__, array($this, 'deactivate'));
  27. $this->add_actions();
  28. $this->add_filters();
  29. $this->add_shortcodes();
  30. $this->add_scripts();
  31. if( !is_dir(HSS_PLUGIN_UPLOADS_DIR) )
  32. $this->errors[] = __('Homepage Slideshow: The uploads dir does not exists, please create it and set write permissions');
  33. if( is_dir(HSS_PLUGIN_UPLOADS_DIR) && !is_writable(HSS_PLUGIN_UPLOADS_DIR) )
  34. $this->errors[] = sprintf(__('Homepage Slideshow: The upload dir "%s" is not writable, please set writte permissions'), HSS_PLUGIN_UPLOADS_DIR);
  35. if( !is_dir(HSS_PLUGIN_XML_DIR) )
  36. $this->errors[] = __('Homepage Slideshow: The folder to store xml files does not exits, please create it and set write permissions');
  37. if( is_dir(HSS_PLUGIN_XML_DIR) && !is_writable(HSS_PLUGIN_XML_DIR) )
  38. $this->errors[] = sprintf(__('Homepage Slideshow: The folder to store xml files "%s" is not writable, please set writte permissions'), HSS_PLUGIN_XML_DIR);
  39. }
  40. public function add_actions()
  41. {
  42. if( is_admin() )
  43. {
  44. add_action('admin_menu', array($this, 'add_menus'));
  45. add_action('admin_notices', array($this, 'admin_notices'));
  46. add_action('init', array($this, 'handle_admin_request'));
  47. add_action('admin_head', array($this, 'hss_admin_head'));
  48. }
  49. else
  50. {
  51. }
  52. }
  53. public function add_filters()
  54. {
  55. }
  56. public function add_scripts()
  57. {
  58. global $pagenow, $typenow;
  59. $plugin_page = $_GET['page'];
  60. if( is_admin() && ($plugin_page == 'homepageslideshow_manage')):
  61. wp_enqueue_style('hss_admin_css', HSS_PLUGIN_URL . '/css/admin.css');
  62. wp_enqueue_style('hss_default_css', HSS_PLUGIN_URL . '/css/default.css');
  63. wp_enqueue_style('hss_dd_css', HSS_PLUGIN_URL . '/css/data_tables.css');
  64. wp_enqueue_script('hss_swfupload', HSS_PLUGIN_URL . '/js/swfupload/js/swfupload.js');
  65. wp_enqueue_script('hss_swfuploadqueue', HSS_PLUGIN_URL . '/js/swfupload/js/swfupload.queue.js');
  66. wp_enqueue_script('hss_fileprogress', HSS_PLUGIN_URL . '/js/swfupload/js/fileprogress.js');
  67. wp_enqueue_script('hss_handlers', HSS_PLUGIN_URL . '/js/swfupload/js/handlers.js');
  68. wp_enqueue_style( 'farbtastic' );
  69. wp_enqueue_script( 'farbtastic' );
  70. //wp_enqueue_script('hss_swfobject', HSS_PLUGIN_URL . '/js/uploadify-2.1.4/swfobject.js');
  71. //wp_enqueue_script('hss_uploadify', HSS_PLUGIN_URL . '/js/uploadify-2.1.4/jquery.uploadify.v2.1.4.min.js', array('jquery', 'hss_swfobject'));
  72. wp_enqueue_script('hss_tp_js', HSS_PLUGIN_URL . '/js/paging.js');
  73. wp_enqueue_script('hss_admin_js', HSS_PLUGIN_URL . '/js/admin.js');
  74. //wp_enqueue_script('thickbox');//, home_url() . '/wp-includes/js/thickbox/thickbox.js', array('jquery'));
  75. add_thickbox();
  76. endif;
  77. if( is_admin() && ($plugin_page == 'homepageslideshow_settings')):
  78. //wp_enqueue_script( HSS_PLUGIN_URL . '/js/jscolor/jscolor.js' );
  79. wp_enqueue_script('hss_setting_js', HSS_PLUGIN_URL . '/js/jscolor/jscolor.js');
  80. //wp_enqueue_script('hss_setting_js', HSS_PLUGIN_URL . '/js/setting.js');
  81. endif;
  82. }
  83. public function add_shortcodes()
  84. {
  85. add_shortcode('hss', 'shortcode_display_hss_gallery');
  86. }
  87. public function add_menus()
  88. {
  89. add_menu_page(__('HSS'), __('HSS'), 8, 'homepageslideshow_menu', create_function('', 'require_once HSS_PLUGIN_DIR . "/html/about.php";'));
  90. add_submenu_page('homepageslideshow_menu', __('Homepage Slideshow Setting'), __('About'), 8, 'homepageslideshow_menu',
  91. create_function('', 'require_once HSS_PLUGIN_DIR . "/html/about.php";'));
  92. add_submenu_page('homepageslideshow_menu', __('Homepage Slideshow Setting'), __('Settings'), 8, 'homepageslideshow_settings',
  93. create_function('', 'require_once HSS_PLUGIN_DIR . "/html/settings.php";'));
  94. add_submenu_page('homepageslideshow_menu', __('Homepage Slideshow Management'), __('Category Management'), 8, 'homepageslideshow_manage',
  95. create_function('', 'require_once HSS_PLUGIN_DIR . "/html/manage.php";'));
  96. add_submenu_page('homepageslideshow_menu', __('Homepage Slideshow Cache'), __('Delete Cache'), 8, 'homepageslideshow_cache',
  97. create_function('', 'require_once HSS_PLUGIN_DIR . "/html/cache.php";'));
  98. }
  99. public function hss_admin_head()
  100. {
  101. print '<script type="text/javascript">
  102. var hss_url = "'.HSS_PLUGIN_URL.'";
  103. </script>';
  104. }
  105. public function admin_notices()
  106. {
  107. foreach($this->errors as $e)
  108. {
  109. print '<div class="error"><p>'.$e.'</p></div>';
  110. }
  111. }
  112. public function handle_admin_request()
  113. {
  114. $task = isset($_REQUEST['task']) ? $_REQUEST['task'] : null;
  115. if($task == null) return false;
  116. if( method_exists($this, $task) )
  117. $this->$task();
  118. }
  119. /**
  120. * Here start all tasks methods
  121. */
  122. public function hss_add_new_album()
  123. {
  124. global $wpdb;
  125. $album_id = isset($_POST['album_id']) ? (int)$_POST['album_id'] : null;
  126. $album_name = trim($_POST['album_name']);
  127. $album_desc = trim($_POST['album_desc']);
  128. if (!function_exists('get_magic_quotes_gpc') || get_magic_quotes_gpc() != 1) {
  129. //$album_name = addslashes($album_name);
  130. //$album_desc = addslashes($album_desc);
  131. }
  132. $album = null;
  133. $album_dir = null;
  134. //edit album
  135. if( $album_id != null )
  136. {
  137. //get album
  138. $query = "SELECT album_id, name, description, image, thumb, status, `order`, creation_date
  139. FROM {$wpdb->prefix}hss_albums
  140. WHERE album_id = $album_id";
  141. $album = $wpdb->get_row($query);
  142. if( empty($album) )
  143. {
  144. //album does not exists
  145. die('album not found'. $query);
  146. }
  147. $album_dir = hss_get_album_dir($album->album_id);
  148. //delete album images if new one will be uploaded
  149. if( isset($_FILES) && isset($_FILES['album_img']) && $_FILES['album_img']['size'] > 0 )
  150. {
  151. if( file_exists($album_dir . '/big/' . $album->image) )
  152. unlink($album_dir . '/big/' . $album->image);
  153. if( $album_dir . '/thumb/' . $album->thumb )
  154. unlink($album_dir . '/thumb/' . $album->thumb);
  155. }
  156. $album = array('name' => $album_name, 'description' => $album_desc);
  157. }
  158. //create a new album
  159. else
  160. {
  161. $album = array('name' => $album_name, 'description' => $album_desc, 'order' => 0, 'image' => '', 'thumb' => '', 'status' => 1);
  162. $wpdb->insert($wpdb->prefix.'hss_albums', $album);
  163. //get album id
  164. $album_id = $wpdb->insert_id;
  165. $album_dir = hss_get_album_dir($album_id);
  166. if( !is_dir( $album_dir ) )
  167. mkdir($album_dir);
  168. if( !is_dir($album_dir . '/big') )
  169. mkdir($album_dir . '/big');
  170. if( !is_dir($album_dir . '/thumb') )
  171. mkdir($album_dir . '/thumb');
  172. }
  173. //upload images
  174. if( isset($_FILES) && isset($_FILES['album_img']) && $_FILES['album_img']['size'] > 0 )
  175. {
  176. //die(HSS_PLUGIN_UPLOADS_DIR . '/' . $album_dir);
  177. if( !is_dir( $album_dir ) )
  178. mkdir($album_dir);
  179. if( !is_dir($album_dir . '/big') )
  180. mkdir($album_dir . '/big');
  181. if( !is_dir($album_dir . '/thumb') )
  182. mkdir($album_dir . '/thumb');
  183. $unique_name = wp_unique_filename($album_dir . '/big', $_FILES['album_img']['name']);
  184. //move uploaded file (big file)
  185. move_uploaded_file($_FILES['album_img']['tmp_name'], $album_dir . '/big/' . $unique_name);
  186. //set album image
  187. $album['image'] = $unique_name;
  188. //resize for thumbnail
  189. $thumb = image_resize($album_dir . '/big/' .$unique_name,
  190. //(int)get_option('large_size_w'),
  191. //(int)get_option('large_size_h'),
  192. 80,
  193. 80,
  194. 0, 'resized');
  195. copy($thumb, $album_dir . '/thumb/' . basename($thumb));
  196. //delete temp thumb
  197. unlink($thumb);
  198. if( is_wp_error($thumb) )
  199. {
  200. print_r($thumb);die('Error');
  201. }
  202. $album['thumb'] = basename($thumb);
  203. }
  204. $wpdb->update($wpdb->prefix.'hss_albums', $album, array('album_id' => $album_id));
  205. if( isset($_REQUEST['TB_iframe']))
  206. {
  207. $js = '<script type="text/javascript">self.parent.tb_remove();self.parent.hss_refresh_albums_table();</script>';
  208. die($js);
  209. }
  210. }
  211. /**
  212. * Delete album
  213. * @param $json
  214. * @return unknown_type
  215. */
  216. public function hss_delete_album($json = true)
  217. {
  218. global $wpdb;
  219. $key = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : null;
  220. if( $key == null ) return false;
  221. //check if album exists
  222. if( ($album = $this->hss_get_album($key)) == null )
  223. {
  224. //album not found or not exists
  225. $this->json_response(array('status' => 'error', 'message' => 'The album id does not exists'));
  226. }
  227. //check for images
  228. $query = "SELECT image_id FROM {$wpdb->prefix}hss_images WHERE album_id = $key";
  229. if( $wpdb->query($query) > 0 )
  230. {
  231. //error: the album contains images
  232. $this->json_response(array('status' => 'error', 'message' => 'The album is not empty, please delete all images first'));
  233. }
  234. $album_dir = hss_get_album_dir($key);
  235. if( file_exists($album_dir .'/big/'.$album['image']) )
  236. unlink($album_dir .'/big/'.$album['image']);
  237. if( file_exists($album_dir .'/thumb/'.$album['thumb']) )
  238. unlink($album_dir .'/thumb/'.$album['thumb']);
  239. if( is_dir($album_dir.'/thumb') )
  240. rmdir($album_dir.'/thumb');
  241. if( is_dir($album_dir.'/big') )
  242. rmdir($album_dir.'/big');
  243. @rmdir($album_dir);
  244. //delete album
  245. $query = "DELETE FROM {$wpdb->prefix}hss_albums WHERE album_id = $key LIMIT 1";
  246. $wpdb->query($query);
  247. if( $json )
  248. $this->json_response(array('status' => 'ok', 'message' => 'The album has been deleted.'));
  249. }
  250. public function reset_albums(){update_option('hss_albums', array());delete_option('hss_albums');}
  251. /**
  252. * Save Homepage Slideshow setttings
  253. *
  254. * @return unknown_type
  255. */
  256. public function save_hss_settings()
  257. {
  258. $ops = array();
  259. foreach($_POST['settings'] as $key => $value)
  260. {
  261. $ops[$key] = trim($value);
  262. }
  263. update_option('hss_settings', $ops);
  264. /*
  265. insert xml code part
  266. */
  267. }
  268. public function hss_single_image_upload()
  269. {
  270. global $wpdb;
  271. $album_id = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : null;
  272. $title = trim($_REQUEST['image_title']);
  273. $desc = isset($_REQUEST['image_description']) ? trim($_REQUEST['image_description']) : '';
  274. $price = isset($_REQUEST['image_price']) ? trim($_REQUEST['image_price']) : 0;
  275. $price = (is_numeric($price)) ? $price : 0;
  276. $thumb = isset($_REQUEST['image_thumb']) ? trim($_REQUEST['image_thumb']) : 'generate';
  277. $link = isset($_REQUEST['image_link']) ? trim($_REQUEST['image_link']) : '';
  278. //for update
  279. $image_id = isset($_REQUEST['image_id']) ? (int)$_REQUEST['image_id'] : null;
  280. //init messages
  281. $this->messages['upload'] = array();
  282. //get albums
  283. $album = null;
  284. //die("thumb: $thumb");
  285. //for update image
  286. if( $image_id !== null )
  287. {
  288. //die('editing image'. $image_id);
  289. if( !($album = $this->hss_get_album($album_id)) )
  290. {
  291. die(__('Incorrect album or does not exists'));
  292. return false;
  293. }
  294. $_image = $this->get_image($image_id);
  295. //check if new image will be uploaded
  296. if( isset($_FILES) && $_FILES['image_file']['size'] > 0 )
  297. {
  298. //delete images becuase new one will be uploaded
  299. if( file_exists(hss_get_album_dir($album_id) . '/big/' . $_image->image) )
  300. {
  301. unlink(hss_get_album_dir($album_id) . '/big/' . $_image->image);
  302. }
  303. }
  304. if( $thumb == 'upload' && $_FILES['image_file_thumb']['size'] > 0 )
  305. {
  306. //delete thumb images
  307. if( file_exists(hss_get_album_dir($album_id) . '/thumb/' . $_image->thumb) )
  308. {
  309. unlink(hss_get_album_dir($album_id) . '/thumb/' . $_image->thumb);
  310. }
  311. }
  312. }
  313. //add new image
  314. else
  315. {
  316. if( !($album = $this->hss_get_album($album_id)) )
  317. {
  318. $this->messages['upload'][] = __('Incorrect album');
  319. return false;
  320. }
  321. if( empty($title))
  322. {
  323. $this->messages['upload'][] = __('Please enter a valid title for image');
  324. return false;
  325. }
  326. /*
  327. if( empty($price))
  328. {
  329. $this->messages['upload'][] = __('Please enter a price');
  330. return false;
  331. }
  332. */
  333. if( !isset($_FILES['image_file']) || $_FILES['image_file']['size'] <= 0 )
  334. {
  335. $this->messages['upload'][] = __('Please select an image to upload');
  336. return false;
  337. }
  338. if( $thumb == 'upload' )
  339. {
  340. if( !isset($_FILES['image_file_thumb']) || $_FILES['image_file_thumb']['size'] <= 0)
  341. {
  342. $this->messages['upload'][] = __('Please select a thumb for the image');
  343. return false;
  344. }
  345. }
  346. }
  347. //print_r($_REQUEST);die();
  348. //get album dir
  349. $album_dir = hss_get_album_dir($album_id);
  350. $image_file = $thumb_file = null;
  351. if( isset($_FILES) && $_FILES['image_file']['size'] > 0 )
  352. {
  353. //build new image
  354. $image_file = wp_unique_filename($album_dir . '/big', $_FILES['image_file']['name']);
  355. //save uploaded image (big image)
  356. move_uploaded_file($_FILES['image_file']['tmp_name'], $album_dir . '/big/' . $image_file);
  357. if( $thumb == 'generate' )
  358. {
  359. //generate thumb image
  360. $thumb_file = image_resize($album_dir . '/big/' . $image_file, 80, 80, false, 'resized');
  361. copy($thumb_file, $album_dir . '/thumb/' . basename($thumb_file));
  362. unlink($thumb_file);
  363. }
  364. elseif( $thumb == 'upload' )
  365. {
  366. $tmp_thumb = wp_unique_filename($album_dir . '/thumb', $thumb_file);
  367. move_uploaded_file($_FILES['image_file_thumb']['tmp_name'], $album_dir . '/thumb/' . $tmp_thumb);
  368. $thumb_file = wp_unique_filename($album_dir . '/thumb', $_FILES['image_file_thumb']['name']);
  369. $thumb_file = image_resize($album_dir . '/thumb/' . $tmp_thumb, 80, 80, false, 'resized');
  370. unlink($album_dir . '/' . $tmp_thumb);
  371. }
  372. }
  373. //for edit image
  374. if( $image_id !== null )
  375. {
  376. $image = $this->get_image($image_id);
  377. if( !$image )
  378. {
  379. die('Image id does not exists');
  380. }
  381. $data = array('title' => $title, 'description' => $desc, 'price' => $price,
  382. 'image' => ($image_file != null) ? $image_file : $image['image'],
  383. 'thumb' => ($thumb_file != null ) ? basename($thumb_file) : $image['thumb'],
  384. 'link' => $link);
  385. $wpdb->update($wpdb->prefix.'hss_images', $data, array('image_id' => $image_id));
  386. }
  387. //add new image
  388. else
  389. {
  390. $image = array('category_id' => $album_id, 'title' => $title, 'description' => $desc,
  391. 'price' => $price,
  392. 'thumb' => basename($thumb_file),
  393. 'image' => $image_file,
  394. 'status' => 1,
  395. 'order' => 0,
  396. 'link' => '');
  397. if(!$wpdb->insert($wpdb->prefix.'hss_images', $image))
  398. {
  399. $this->messages['upload'][] = __('Error ocurred while trying to insert a new image');
  400. }
  401. }
  402. if( isset($_REQUEST['TB_iframe']))
  403. {
  404. $js = '<script type="text/javascript">self.parent.tb_remove();self.parent.hss_refresh_images_table("'.$album_id.'");</script>';
  405. die($js);
  406. }
  407. }
  408. /**
  409. *
  410. * @return unknown_type
  411. */
  412. public function hss_resize_image_and_add()
  413. {
  414. global $wpdb;
  415. $price = trim($_REQUEST['image_price']);
  416. $price = (is_numeric($price)) ? $price : 0;
  417. $album_id = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : null;
  418. $full_filename = $_REQUEST['filename'];
  419. $cpage = $_REQUEST['cpage'];
  420. $view = $_REQUEST['view'];
  421. if( !file_exists($full_filename) )
  422. {
  423. header('Content-type: application/json');
  424. $res = json_encode(array('status' => 'error', 'message' => 'Image file does not exists or error moving file'));
  425. die($res);
  426. }
  427. //$title = substr(basename($full_filename), 0, (strrpos(basename($full_filename), '.') - 1));
  428. $title = substr(basename($full_filename), 0, (strrpos(basename($full_filename), '.')));
  429. $album_dir = hss_get_album_dir($album_id);
  430. $image_file = basename($full_filename);
  431. $album = $this->hss_get_album($album_id);
  432. if( !$album )
  433. {
  434. $this->json_response(array('status' => 'error', 'message' => 'The album image does not exists'));
  435. }
  436. $big_image = wp_unique_filename($album_dir . '/big', basename($full_filename));
  437. copy($full_filename, $album_dir.'/big/'.$big_image);
  438. //create thumb
  439. $tmp_thumb = image_resize($full_filename, 80, 80, false, 'resized');
  440. $thumb = wp_unique_filename($album_dir . '/thumb/', basename($tmp_thumb));
  441. //copy thumb to folder
  442. copy($tmp_thumb, $album_dir.'/thumb/'.$thumb);
  443. //delete big and generated thumb
  444. unlink($full_filename);
  445. unlink($tmp_thumb);
  446. //die(basename($thumb));
  447. $image = array('category_id' => $album_id, 'title' => $title, 'thumb' => basename($thumb), 'description' => '',
  448. 'price' => number_format($price, 2), 'order' => 0, 'image' => $big_image, 'status' => 1);
  449. $wpdb->insert($wpdb->prefix.'hss_images', $image);
  450. $image_id = $wpdb->insert_id;
  451. //generate new row
  452. $row = '<tr>
  453. <td><input type="checkbox" name="image_'.$image_id.'" value="'.$image_id.'" /></td>
  454. <td>'.$image_id.'</td>
  455. <td>'.$image['title'].'</td>
  456. <td><img src="'.hss_get_album_url($album_id). '/' . $image['thumb'].'" alt="" /></td>
  457. <td>'.$image['description'].'</td>
  458. <td>'.$image['price'].'</td>
  459. <td>
  460. <form action="" method="post" class="order_form">
  461. <input type="hidden" name="task" value="hss_reorder_image" />
  462. <input type="hidden" name="album_id" value="'.$album['album_id'].'" />
  463. <input type="hidden" name="image_id" value="'.$image_id.'" />
  464. <input type="text" name="img_order" value="0" class="image_order" />
  465. </form>
  466. </td>
  467. <td>
  468. <a href="'.$cpage.'&view='.$view.'&task=hss_disable_image&album_id='.$album_id.'&hss_image_id='.$image_id.'">
  469. '.__('Disable').'
  470. </a>&nbsp;
  471. <a class="thickbox"
  472. href="'.HSS_PLUGIN_URL .'/html/edit_image.php?album_id='.$album_id.'&hss_image_id='.$image_id.'&KeepThis=true&TB_iframe=true&height=400&width=600">
  473. '.__('Edit').'
  474. </a>
  475. <a href="'.$cpage.'&view='.$view.'&task=hss_delete_image&album_key='.$album_id.'&hss_image_id='.$image_id.'">
  476. '.__('Delete').'</a>&nbsp;
  477. </td>
  478. </tr>';
  479. $res = array('status' => 'ok', 'row' => $row);
  480. $this->json_response($res);
  481. }
  482. public function hss_delete_image()
  483. {
  484. $album_id = (int)trim($_REQUEST['album_id']);
  485. $image_id = (int)$_REQUEST['hss_image_id'];
  486. $album = $this->hss_get_album($album_id);
  487. if( !$album )
  488. {
  489. $this->messages['upload'][] = __('Incorrect album');
  490. return false;
  491. }
  492. $image = $this->get_image($image_id);
  493. if( !$image )
  494. {
  495. $this->messages['upload'][] = __('Image id does not exists');
  496. return false;
  497. }
  498. $this->delete_image($image_id);
  499. $this->messages['upload'][] = __('Album image deleted');
  500. }
  501. public function hss_get_albums_table()
  502. {
  503. global $wpdb;
  504. $query = "SELECT * FROM {$wpdb->prefix}hss_albums ORDER BY `order` ASC";
  505. $albums = $wpdb->get_results($query, ARRAY_A);
  506. ob_start();
  507. require_once HSS_PLUGIN_DIR . '/html/albums_rows.php';
  508. $rows = ob_get_clean();
  509. $json = json_encode(array('status' => 'ok', 'rows' => $rows));
  510. header('Content-type: application/json');
  511. die($json);
  512. }
  513. public function hss_get_albums_images_table()
  514. {
  515. $album_id = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : null;
  516. if( $album_id == null )
  517. {
  518. $this->json_response(array('status' => 'error', 'message' => 'Invalid image id'));
  519. }
  520. $album = $this->hss_get_album($album_id);
  521. if( !$album )
  522. {
  523. $this->json_response(array('status' => 'error', 'message' => 'The album does not exists'));
  524. }
  525. $images = $this->hss_get_album_images($album_id);
  526. $cpage = 'admin.php?page=homepageslideshow_manage';
  527. $_REQUEST['view'] = 'manage_album';
  528. ob_start();
  529. require_once HSS_PLUGIN_DIR . '/html/images_rows.php';
  530. $rows = ob_get_clean();
  531. $this->json_response(array('status' => 'ok', 'rows' => $rows));
  532. }
  533. public function json_response($res)
  534. {
  535. if( is_array($res) || is_object($res) )
  536. $res = json_encode($res);
  537. header('Content-type: application/json');
  538. die($res);
  539. }
  540. public function activate()
  541. {
  542. global $wpdb;
  543. $query = array();
  544. $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}hss_albums(album_id bigint not null auto_increment,
  545. name varchar(150),
  546. description varchar(500),
  547. image varchar(500),
  548. thumb varchar(500),
  549. status tinyint(1),
  550. `order` bigint default 0,
  551. creation_date datetime,
  552. primary key(album_id)
  553. )";
  554. $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}hss_images(image_id bigint not null auto_increment,
  555. category_id bigint not null,
  556. title varchar(150),
  557. description varchar(500),
  558. price decimal(10,2),
  559. thumb varchar(500),
  560. image varchar(500),
  561. status tinyint(1),
  562. `order` bigint default 0,
  563. link text,
  564. creation_date datetime,
  565. primary key(image_id)
  566. )";
  567. $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}hss_misc(id int not null auto_increment,
  568. ione int,
  569. itwo int,
  570. ithree int,
  571. txt text,
  572. primary key(id)
  573. )";
  574. $sec_word = md5('r'.rand().'r2'.rand().'h'.time().'r3'.rand().'r4'.rand());
  575. $query[] = "INSERT INTO {$wpdb->prefix}hss_misc (id, ione, itwo, ithree, txt) VALUES (1,1,1,1,'".$sec_word."')";
  576. foreach($query as $q)
  577. {
  578. $wpdb->query($q);
  579. }
  580. //create folders
  581. if( !is_dir(HSS_PLUGIN_UPLOADS_DIR) )
  582. {
  583. mkdir(HSS_PLUGIN_UPLOADS_DIR);
  584. chmod(HSS_PLUGIN_UPLOADS_DIR, 0777);
  585. }
  586. if( !is_dir(HSS_PLUGIN_XML_DIR) )
  587. {
  588. mkdir(HSS_PLUGIN_XML_DIR);
  589. chmod(HSS_PLUGIN_XML_DIR, 0777);
  590. }
  591. $this->def_settings = hss_get_def_settings();
  592. //store default settings
  593. add_option('hss_settings', $this->def_settings);
  594. }
  595. public function deactivate()
  596. {
  597. global $wpdb;
  598. //$query = "DROP TABLE {$wpdb->prefix}hss_albums";
  599. //$wpdb->query($query);
  600. //$query = "DROP TABLE {$wpdb->prefix}hss_images";
  601. //$wpdb->query($query);
  602. $query = "DROP TABLE {$wpdb->prefix}hss_misc";
  603. $wpdb->query($query);
  604. delete_option('hss_settings');
  605. }
  606. /**
  607. * Get album from id
  608. * @param $album_id
  609. * @return null on album not found or associative array on album found
  610. */
  611. public function hss_get_album($album_id)
  612. {
  613. global $wpdb;
  614. $album_id = (int)$album_id;
  615. $query = "SELECT album_id, name, description, image, thumb, `order`, status, creation_date
  616. FROM {$wpdb->prefix}hss_albums
  617. WHERE album_id = $album_id
  618. LIMIT 1";
  619. $album = $wpdb->get_row($query, ARRAY_A);
  620. if( empty($album) )
  621. return null;
  622. return $album;
  623. }
  624. public function hss_get_album_images($album_id)
  625. {
  626. global $wpdb;
  627. $album_id = (int)$album_id;
  628. $query = "SELECT image_id, category_id, title, description, price, thumb, image, status, `order`, link, creation_date
  629. FROM {$wpdb->prefix}hss_images
  630. WHERE category_id = $album_id
  631. ORDER BY `order` ASC";
  632. $images = $wpdb->get_results($query, ARRAY_A);
  633. if( empty($images) )
  634. return null;
  635. return $images;
  636. }
  637. public function get_image($image_id)
  638. {
  639. global $wpdb;
  640. $image_id = (int)$image_id;
  641. $query = "SELECT image_id, category_id, title, description, price, thumb, image, status, `order`, creation_date
  642. FROM {$wpdb->prefix}hss_images
  643. WHERE image_id = $image_id
  644. LIMIT 1";
  645. $image = $wpdb->get_row($query, ARRAY_A);
  646. if( empty($image) )
  647. return null;
  648. return $image;
  649. }
  650. public function hss_delete_cache()
  651. {
  652. if( !isset($_REQUEST['delete_cache']) ) return null;
  653. $dh = opendir(HSS_PLUGIN_XML_DIR);
  654. while(($file = readdir($dh)) !== false)
  655. {
  656. if( $file{0} == '.' ) continue;
  657. unlink(HSS_PLUGIN_XML_DIR . '/' . $file);
  658. }
  659. closedir($dh);
  660. $this->messages['cache'] = array('The cache has benn deleted!!!');
  661. }
  662. public function hss_reorder_image()
  663. {
  664. global $wpdb;
  665. $album_id = (int)$_REQUEST['album_id'];
  666. $image_id = (int)$_REQUEST['image_id'];
  667. $img_order = (int)$_REQUEST['img_order'];
  668. $wpdb->update($wpdb->prefix.'hss_images', array('order' => $img_order), array('image_id' => $image_id));
  669. }
  670. public function hss_reorder_album()
  671. {
  672. global $wpdb;
  673. $album_id = (int)$_REQUEST['album_id'];
  674. $album_order = (int)$_REQUEST['album_order'];
  675. $wpdb->update($wpdb->prefix.'hss_albums', array('order' => $album_order), array('album_id' => $album_id));
  676. }
  677. public function hss_bulk_delete_albums()
  678. {
  679. global $wpdb;
  680. $ids = json_decode($_REQUEST['ids']);
  681. if( empty($ids) )
  682. {
  683. $this->json_response(array('status' => 'errpr', 'message' => 'No Categories selected'));
  684. }
  685. $error = '';
  686. foreach($ids as $id)
  687. {
  688. if( $this->hss_get_album_images($id) != null )
  689. {
  690. $error .= 'The category '. $id . ' is not empty, please delete the images first';
  691. continue;
  692. }
  693. $_REQUEST['album_id'] = $id;
  694. $this->hss_delete_album(false);
  695. }
  696. $res = array('status' => 'ok', 'message' => 'Categories deleted');
  697. if( !empty($error) )
  698. {
  699. $res['status'] = 'error';
  700. $res['message'] = $error;
  701. }
  702. $this->json_response($res);
  703. }
  704. public function hss_bulk_disable_albums()
  705. {
  706. $ids = json_decode($_REQUEST['ids']);
  707. foreach($ids as $id)
  708. {
  709. $this->hss_disable_album($id);
  710. }
  711. $this->json_response(array('status' => 'ok'));
  712. }
  713. public function hss_bulk_enable_albums()
  714. {
  715. $ids = json_decode($_REQUEST['ids']);
  716. foreach($ids as $id)
  717. {
  718. $this->hss_enable_album($id);
  719. }
  720. $this->json_response(array('status' => 'ok'));
  721. }
  722. public function delete_image($image_id)
  723. {
  724. global $wpdb;
  725. $image = $this->get_image($image_id);
  726. if( !$image ) return null;
  727. $album_dir = hss_get_album_dir($image['category_id']);
  728. @unlink($album_dir .'/big/'. $image['image']);
  729. @unlink($album_dir .'/thumb/'. $image['thumb']);
  730. $wpdb->query("DELETE FROM {$wpdb->prefix}hss_images WHERE image_id = $image_id LIMIT 1");
  731. return true;
  732. }
  733. public function hss_bulk_delete_images()
  734. {
  735. $ids = json_decode($_REQUEST['ids']);
  736. $msg = '';
  737. foreach($ids as $id)
  738. {
  739. if( !$this->delete_image($id) )
  740. $msg .= 'Error delete image with id ' . $id. ' ';
  741. }
  742. $res = array('status' => 'ok', 'message' => 'Images deleted');
  743. if( !empty($msg) )
  744. {
  745. $res['status'] = 'error';
  746. $res['message'] = $msg;
  747. }
  748. $this->json_response($res);
  749. }
  750. public function hss_bulk_disable_images()
  751. {
  752. $ids = json_decode($_REQUEST['ids']);
  753. foreach($ids as $id)
  754. {
  755. $this->hss_disable_image($id);
  756. }
  757. $this->json_response(array('status' => 'ok'));
  758. }
  759. public function hss_bulk_enable_images()
  760. {
  761. $ids = json_decode($_REQUEST['ids']);
  762. foreach($ids as $id)
  763. {
  764. $this->hss_enable_image($id);
  765. }
  766. $this->json_response(array('status' => 'ok'));
  767. }
  768. public function hss_disable_album($album_id = null)
  769. {
  770. global $wpdb;
  771. $album_id = (int)$album_id;
  772. $album_id = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : $album_id;
  773. if( $album_id )
  774. {
  775. $wpdb->update($wpdb->prefix.'hss_albums', array('status' => 0), array('album_id' => $album_id));
  776. }
  777. }
  778. public function hss_enable_album($album_id = null)
  779. {
  780. global $wpdb;
  781. $album_id = (int)$album_id;
  782. $album_id = isset($_REQUEST['album_id']) ? (int)$_REQUEST['album_id'] : $album_id;
  783. if( $album_id )
  784. $wpdb->update($wpdb->prefix.'hss_albums', array('status' => 1), array('album_id' => $album_id));
  785. }
  786. public function hss_disable_image($image_id = null)
  787. {
  788. global $wpdb;
  789. $image_id = (int)$image_id;
  790. $image_id = isset( $_REQUEST['hss_image_id'] ) ? (int)$_REQUEST['hss_image_id'] : $image_id;
  791. if( $image_id)
  792. $wpdb->update($wpdb->prefix.'hss_images', array('status' => 0), array('image_id' => $image_id));
  793. }
  794. public function hss_enable_image($image_id = null)
  795. {
  796. global $wpdb;
  797. $image_id = (int)$image_id;
  798. $image_id = isset($_REQUEST['hss_image_id']) ? (int)$_REQUEST['hss_image_id'] : $image_id;
  799. if( $image_id )
  800. $wpdb->update($wpdb->prefix.'hss_images', array('status' => 1), array('image_id' => $image_id));
  801. }
  802. }
  803. $ghss = new HomepageSlideshow();
  804. ?>