NGG_Image_Manager

Class NGG_Gallery_Manager.

Defined (1)

The class is defined in the following location(s).

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