/admin/manage/class-ngg-image-manager.php

  1. <?php 
  2. include_once( 'class-ngg-abstract-image-manager.php' ); 
  3.  
  4. /** 
  5. * Class NGG_Gallery_Manager 
  6. * 
  7. * Display the gallery managing page. 
  8. */ 
  9. class NGG_Image_Manager extends NGG_Abstract_Image_Manager { 
  10.  
  11. private $gallery; 
  12. private $id; 
  13.  
  14. public function __construct() { 
  15. $this->id = (int) $_GET['gid']; 
  16.  
  17. /** 
  18. * Display the page. 
  19. */ 
  20. public function display() { 
  21.  
  22. parent::display(); 
  23.  
  24. if ( isset( $_POST['form'] ) && $_POST['form'] === "gallery" ) { 
  25. if (isset ($_POST['add-new-page'])) { 
  26. $this->create_page(); 
  27. } else { 
  28. $this->handle_update_gallery(); 
  29.  
  30. if ( isset( $_POST['scan_folder'] ) ) { 
  31. $this->handle_scan_folder(); 
  32.  
  33. if ( isset( $_GET['action'] ) ) { 
  34. $this->handle_row_action(); 
  35.  
  36. /** 
  37. * @global $nggdb nggdb 
  38. */ 
  39. global $nggdb; 
  40.  
  41. $this->gallery = $nggdb->find_gallery( $this->id ); 
  42.  
  43. if ( isset( $_GET['paged'] ) ) { 
  44. $page = $_GET['paged']; 
  45. } else { 
  46. $page = ''; 
  47.  
  48. /** 
  49. * Display the actual table. 
  50. */ 
  51. $table = new NGG_Image_List_Table( self::BASE ); 
  52. $table->prepare_items(); 
  53. ?> 
  54. <div class="wrap"> 
  55. <form id="update_gallery" class="nggform" method="post" action="<?php echo self::BASE . '&mode=image&gid=' . $this->id . '&paged=' . $page; ?>" accept-charset="utf-8"> 
  56. <?php wp_nonce_field( 'ngg-update-gallery', '_ngg_nonce_gallery' ); ?> 
  57. <input type="hidden" name="form" value="gallery"> 
  58. <?php $this->print_gallery_overview( $table->items ) ?> 
  59. </form> 
  60. <!-- TODO Add a search inside a gallery form --> 
  61. <form id="update_images" class="nggform" method="post" action="<?php echo self::BASE . '&mode=image&gid=' . $this->id . '&paged=' . $page; ?>" accept-charset="utf-8"> 
  62. <?php wp_nonce_field( 'ngg-update-images', '_ngg_nonce_images' ); ?> 
  63. <input type="hidden" id="page_type" name="page_type" value="image"> 
  64. <?php $table->display(); ?> 
  65. </form> 
  66. </div> 
  67. <?php 
  68. $this->print_dialogs(); 
  69. $this->print_scripts(); 
  70.  
  71. /** 
  72. * @todo Make this better. 
  73. */ 
  74. protected function print_scripts() { 
  75. parent::print_scripts(); 
  76. ?> 
  77. <script type="text/javascript"> 
  78. /** 
  79. * Confirm the scan operation. 
  80. */ 
  81. jQuery("#scan_folder").click(function() { 
  82. return confirm( 
  83. "<?php _e( 'This will change folder and file names (e.g. remove spaces, special characters, ...)', 'nggallery' ) ?>" + 
  84. "\n\n" + 
  85. "<?php _e( 'You will need to update your URLs if you link directly to the images.', 'nggallery' ) ?>" + 
  86. "\n\n" + 
  87. "<?php _e( 'Press OK to proceed, and Cancel to stop.', 'nggallery' ) ?>" 
  88. ); 
  89. }); 
  90.  
  91. /** 
  92. * For the row actions. 
  93. */ 
  94. jQuery(".confirm_recover").click(function() { 
  95. var fileName = jQuery(this).data('file'); 
  96. return confirm( '<?php _e( 'Recover "{}"?', 'nggallery' ) ?>'.replace('{}', fileName)); 
  97. }); 
  98.  
  99. jQuery(".confirm_delete").click(function() { 
  100. var fileName = jQuery(this).data('file'); 
  101. return confirm( '<?php _e( 'Delete "{}"?', 'nggallery' ) ?>'.replace('{}', fileName)); 
  102. }); 
  103.  
  104. /** 
  105. * Redirect to the sorting UI. 
  106. */ 
  107. jQuery("#sort_gallery").click(function() { 
  108. location.href = "<?php echo esc_js(self::BASE) . '&mode=sort&gid=' . $this->id ?>"; 
  109. }); 
  110. </script> 
  111.  
  112. <?php 
  113.  
  114. /** 
  115. * Handle a request to scan the folder for new images. 
  116. */ 
  117. private function handle_scan_folder() { 
  118. if ( wp_verify_nonce( $_POST['_ngg_nonce_gallery'], 'ngg-update-gallery' ) === false ) { 
  119. nggGallery::show_error( __( 'You waited too long, or you cheated.', 'nggallery' ) ); 
  120.  
  121. return; 
  122.  
  123. global $wpdb; 
  124.  
  125. $gallery_path = $wpdb->get_var( $wpdb->prepare( "SELECT path FROM $wpdb->nggallery WHERE gid = %d",  
  126. $this->id ) ); 
  127. nggAdmin::import_gallery( $gallery_path ); 
  128.  
  129. private function print_gallery_overview( $images ) { 
  130. /** 
  131. * @global $nggdb nggdb 
  132. */ 
  133. global $nggdb; 
  134.  
  135. $disabled = $title = ""; 
  136. $options = get_option( 'ngg_options' ); 
  137.  
  138. if ( $options['galSort'] != "sortorder" ) { 
  139. //Disable sort button and provide feedback why is disabled 
  140. $disabled = "disabled "; 
  141. $title = "title='" . __( 'To enable manual Sort set Custom Order Sort.See Settings->Gallery Settings->Sort Options',  
  142. 'nggallery' ) . "'"; 
  143. ?> 
  144. <h2><?php _e( 'Gallery', 'nggallery' ) ?><?php esc_html_e( $this->gallery->title ) ?></h2> 
  145. <?php if ( nggGallery::current_user_can( 'NextGEN Edit gallery options' ) ) { ?> 
  146. <div id="poststuff"> 
  147. <div id="gallerydiv" class="postbox <?php echo postbox_classes( 'gallery_div', 'ngg-manage' ); ?>"> 
  148. <h3 class="hndle"><?php _e( 'Gallery settings', 'nggallery' ) ?></h3> 
  149.  
  150. <div class="inside"> 
  151. <table class="form-table" id="gallery-properties"> 
  152. <tr> 
  153. <td align="left"><label for="title"><?php _e( 'Title' ) ?></label></td> 
  154. <td align="left"> 
  155. <input type="text" id="title" name="title" class="regular-text" value="<?php esc_attr_e( $this->gallery->title ) ?>"/> 
  156. </td> 
  157. <td align="right"><label for="page_id"><?php _e( 'Page Link', 'nggallery' ) ?></label> 
  158. </td> 
  159. <td align="left"> 
  160. <select id="page_id" name="page_id"> 
  161. <option value="0"><?php _e( 'Not linked', 'nggallery' ) ?></option> 
  162. <?php parent_dropdown( intval( $this->gallery->pageid ) ); ?> 
  163. </select> 
  164. </td> 
  165. </tr> 
  166. <tr> 
  167. <td align="left"><label for="gallery_desc"><?php _e( 'Description' ) ?></label></td> 
  168. <td align="left"> 
  169. <textarea name="gallery_desc" id="gallery_desc" cols="46" rows="3"><?php echo $this->gallery->galdesc; ?></textarea> 
  170. </td> 
  171. <td align="right"><label for="preview_pic"><?php _e( 'Preview image',  
  172. 'nggallery' ) ?></label></td> 
  173. <td align="left"> 
  174. <select name="preview_pic" id="preview_pic"> 
  175. <option value="0"><?php _e( 'No Picture', 'nggallery' ) ?></option> 
  176. <?php 
  177. // ensure that a preview pic from a other page is still shown here 
  178. if ( intval( $this->gallery->previewpic ) != 0 ) { 
  179. if ( ! array_key_exists( $this->gallery->previewpic, $images ) ) { 
  180. $previewpic = $nggdb->find_image( $this->gallery->previewpic ); 
  181. if ( $previewpic ) { 
  182. echo '<option value="' . $previewpic->pid . '" selected>' . $previewpic->pid . ' - ' . esc_attr( $previewpic->filename ) . '</option>' . "\n"; 
  183. if ( is_array( $images ) ) { 
  184. foreach ( $images as $picture ) { 
  185. if ( $picture->exclude ) { 
  186. continue; 
  187. $selected = ( $picture->pid == $this->gallery->previewpic ) ? 'selected' : ''; 
  188. echo '<option value="' . $picture->pid . '" ' . $selected . ' >' . $picture->pid . ' - ' . esc_attr( $picture->filename ) . '</option>' . "\n"; 
  189. ?> 
  190. </select> 
  191. </td> 
  192. </tr> 
  193. <tr> 
  194. <td align="left"><label for="path"><?php _e( 'Path', 'nggallery' ) ?></label></td> 
  195. <td align="left"> 
  196. <input <?php if ( is_multisite() ) { 
  197. echo 'readonly = "readonly"'; 
  198. } ?> type="text" name="path" class="regular-text code" id="path" value="<?php echo $this->gallery->path; ?>"/> 
  199. </td> 
  200. <td align="right"><label for="author"><?php _e( 'Author', 'nggallery' ); ?></label></td> 
  201. <td align="left"><?php echo get_userdata( (int) $this->gallery->author )->display_name ?></td> 
  202. </tr> 
  203. <tr> 
  204. <td align="left"><?php _e( 'Gallery ID', 'nggallery' ) ?>:</td> 
  205. <td align="right"><?php echo $this->gallery->gid; ?></td> 
  206. <?php if ( current_user_can( 'publish_pages' ) ) { ?> 
  207. <td align="right"><label for="parent_id"><?php _e( 'Create new page',  
  208. 'nggallery' ) ?></label></td> 
  209. <td align="left"> 
  210. <select name="parent_id" id="parent_id"> 
  211. <option value="0"><?php _e( 'Main page (No parent)',  
  212. 'nggallery' ); ?></option> 
  213. <?php if ( get_post() ) { 
  214. parent_dropdown(); 
  215. } ?> 
  216. </select> 
  217. <input class="button-secondary action" type="submit" name="add-new-page" value="<?php _e( 'Add page',  
  218. 'nggallery' ); ?>" id="group"/> 
  219. </td> 
  220. <?php } ?> 
  221. </tr> 
  222. <?php do_action( 'ngg_manage_gallery_settings', $this->id ); ?> 
  223. </table> 
  224. <div class="submit"> 
  225. <button class='button-secondary' type='button' <?php echo $disabled, $title ?> id='sort_gallery'> 
  226. <?php _e( 'Sort gallery', 'nggallery' ) ?> 
  227. </button> 
  228. <button type="submit" class="button-secondary" name="scan_folder" id="scan_folder"> 
  229. <?php _e( "Scan folder for new images", 'nggallery' ); ?> 
  230. </button> 
  231. <button type="submit" class="button-primary action"> 
  232. <?php _e( "Save Changes", 'nggallery' ); ?> 
  233. </button> 
  234. </div> 
  235. </div> 
  236. </div> 
  237. </div> <!-- poststuff --> 
  238. <?php 
  239.  
  240. private function handle_update_gallery() { 
  241.  
  242. if ( wp_verify_nonce( $_POST['_ngg_nonce_gallery'], 'ngg-update-gallery' ) === false ) { 
  243. nggGallery::show_error( __( 'You waited too long, or you cheated.', 'nggallery' ) ); 
  244.  
  245. return; 
  246.  
  247. global $wpdb; 
  248.  
  249. if ( nggGallery::current_user_can( 'NextGEN Edit gallery options' ) ) { 
  250.  
  251. if ( nggGallery::current_user_can( 'NextGEN Edit gallery title' ) ) { 
  252. // don't forget to update the slug 
  253. $slug = nggdb::get_unique_slug( sanitize_title( $_POST['title'] ), 'gallery', $this->id ); 
  254. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET title= '%s', slug= '%s' WHERE gid = %d",  
  255. esc_attr( $_POST['title'] ), $slug, $this->id ) ); 
  256. if ( nggGallery::current_user_can( 'NextGEN Edit gallery path' ) ) { 
  257. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET path= '%s' WHERE gid = %d",  
  258. untrailingslashit( str_replace( '\\', '/', trim( stripslashes( $_POST['path'] ) ) ) ),  
  259. $this->id ) ); 
  260. if ( nggGallery::current_user_can( 'NextGEN Edit gallery description' ) ) { 
  261. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET galdesc= '%s' WHERE gid = %d",  
  262. esc_attr( $_POST['gallery_desc'] ), $this->id ) ); 
  263. if ( nggGallery::current_user_can( 'NextGEN Edit gallery page id' ) ) { 
  264. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET pageid= '%d' WHERE gid = %d",  
  265. (int) $_POST['page_id'], $this->id ) ); 
  266. if ( nggGallery::current_user_can( 'NextGEN Edit gallery preview pic' ) ) { 
  267. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET previewpic= '%d' WHERE gid = %d",  
  268. (int) $_POST['preview_pic'], $this->id ) ); 
  269. if ( isset ( $_POST['author'] ) && nggGallery::current_user_can( 'NextGEN Edit gallery author' ) ) { 
  270. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET author= '%d' WHERE gid = %d",  
  271. (int) $_POST['author'], $this->id ) ); 
  272.  
  273. wp_cache_delete( $this->id, 'ngg_gallery' ); 
  274.  
  275.  
  276. do_action( 'ngg_update_gallery', $this->id, $_POST ); 
  277.  
  278. nggGallery::show_message( __( 'Update successful', "nggallery" ) ); 
  279.  
  280.  
  281. private function handle_row_action() { 
  282.  
  283. check_admin_referer('ngg_row_action'); 
  284.  
  285. /** 
  286. * @global nggdb $nggdb 
  287. */ 
  288. global $nggdb; 
  289.  
  290. // Delete a picture 
  291. if ( $_GET['action'] == 'delete' ) { 
  292.  
  293. $pid = (int) $_GET['pid']; 
  294. $options = get_option( 'ngg_options' ); 
  295.  
  296. //TODO:Remove also Tag reference 
  297. $image = $nggdb->find_image( $pid ); 
  298. if ($image) { 
  299. if ($options['deleteImg']) { 
  300. @unlink($image->imagePath); 
  301. @unlink($image->thumbPath); 
  302. @unlink($image->imagePath . '_backup' ); 
  303. do_action('ngg_delete_picture', $pid); 
  304. $result = nggdb::delete_image ( $pid ); 
  305. } else { 
  306. $result = false; 
  307.  
  308. if ($result) { 
  309. nggGallery::show_message( 
  310. sprintf( __( 'Picture %d deleted successfully.', 'nggallery' ), $pid ) 
  311. ); 
  312. } else { 
  313. nggGallery::show_error( 
  314. sprintf( __( 'Picture %d could not be deleted.', 'nggallery' ), $pid ) 
  315. ); 
  316.  
  317. return; 
  318.  
  319. // Recover picture from backup 
  320. if ( $_GET['action'] == 'recover' ) { 
  321.  
  322. $image = $nggdb->find_image( (int) $_GET['pid'] ); 
  323. // bring back the old image 
  324. nggAdmin::recover_image( $image ); 
  325. nggAdmin::create_thumbnail( $image ); 
  326.  
  327. nggGallery::show_message( __( 'Operation successful. Please clear your browser cache.', "nggallery" ) ); 
  328.  
  329. return; 
  330.  
  331. /** 
  332. * Create a page with the same title as the current gallery, and include a shortcode to this 
  333. * gallery. 
  334. */ 
  335. private function create_page() 
  336. if ( wp_verify_nonce( $_POST['_ngg_nonce_gallery'], 'ngg-update-gallery' ) === false ) { 
  337. nggGallery::show_error( __( 'You waited too long, or you cheated.', 'nggallery' ) ); 
  338.  
  339. return; 
  340.  
  341. global $wpdb; 
  342.  
  343. $parent_id = esc_attr($_POST['parent_id']); 
  344. $gallery_title = esc_attr($_POST['title']); 
  345. $gallery_name = $wpdb->get_var("SELECT name FROM $wpdb->nggallery WHERE gid = '$this->gid' "); 
  346.  
  347. // Create a WP page 
  348. global $user_ID; 
  349.  
  350. $page['post_type'] = 'page'; 
  351. $page['post_content'] = '[nggallery id=' . $this->gid . ']'; 
  352. $page['post_parent'] = $parent_id; 
  353. $page['post_author'] = $user_ID; 
  354. $page['post_status'] = 'publish'; 
  355. $page['post_title'] = $gallery_title == '' ? $gallery_name : $gallery_title; 
  356. $page = apply_filters('ngg_add_new_page', $page, $this->gid); 
  357.  
  358. $gallery_pageid = wp_insert_post ($page); 
  359. if ($gallery_pageid != 0) { 
  360. $result = $wpdb->query("UPDATE $wpdb->nggallery SET title= '$gallery_title', pageid = '$gallery_pageid' WHERE gid = '$this->gid'"); 
  361. wp_cache_delete($this->gid, 'ngg_gallery'); 
  362. nggGallery::show_message( sprintf( __( 'New page <strong>%s</strong> (ID: %s) created.', 'nggallery'), $gallery_title, $gallery_pageid )); 
  363.  
  364. do_action('ngg_gallery_addnewpage', $this->gid); 
.