NGG_Manager

Class NGG_Manager.

Defined (1)

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

/admin/manage/class-ngg-manager.php  
  1. abstract class NGG_Manager implements NGG_Displayable { 
  2.  
  3. const BASE = 'admin.php?page=nggallery-manage'; 
  4.  
  5. /** 
  6. * Subclasses should override this method, but must call the parent function. 
  7. */ 
  8. public function display() { 
  9. /** 
  10. * Do a bulk action. 
  11. */ 
  12. if ( ( isset( $_POST['action'] ) || isset( $_POST['action2'] ) ) && isset ( $_POST['doaction'] ) ) { 
  13. $this->handle_bulk_actions(); 
  14.  
  15. /** 
  16. * Do the operations with a dialog. 
  17. */ 
  18. if ( isset( $_POST['TB_bulkaction'] ) && isset( $_POST['TB_action'] ) ) { 
  19. $this->handle_dialog_actions(); 
  20.  
  21. /** 
  22. * Print the HTML for the dialogs. 
  23. */ 
  24. protected function print_dialogs() { 
  25.  
  26. $options = get_option( 'ngg_options' ); 
  27.  
  28. ?> 
  29. <style> 
  30. .ngg-dialog-container { 
  31. display: none; 
  32. </style> 
  33. <div class="ngg-dialog-container"> 
  34. <!-- #resize_images --> 
  35. <form id="resize_images_dialog" method="POST" accept-charset="utf-8"> 
  36. <?php wp_nonce_field( 'ngg_thickbox_form' ) ?> 
  37. <input type="hidden" name="TB_type" class="TB_type" value=""> 
  38. <input type="hidden" id="resize_images_imagelist" name="TB_imagelist" value=""> 
  39. <input type="hidden" id="resize_images_bulkaction" name="TB_bulkaction" value=""> 
  40. <input type="hidden" name="TB_action" value="resize_images"> 
  41. <table width="100%"> 
  42. <tr valign="top"> 
  43. <td> 
  44. <strong><?php _e( 'Resize Images to', 'nggallery' ); ?>:</strong> 
  45. </td> 
  46. <td> 
  47. <label for="imgWidth"><?php _e( 'Width', 'nggallery' ) ?></label> 
  48. <input type="number" min="0" class="small-text" id="imgWidth" name="imgWidth" value="<?php echo $options['imgWidth']; ?>"> 
  49. <label for="imgHeight"><?php _e( 'Height', 'nggallery' ) ?></label> 
  50. <input type="number" min="0" size="5" name="imgHeight" id="imgHeight" class="small-text" value="<?php echo $options['imgHeight']; ?>"> 
  51.  
  52. <p class="description"><?php _e( 'Width and height (in pixels). NextCellent Gallery will keep the ratio size.',  
  53. 'nggallery' ) ?></p> 
  54. </td> 
  55. </tr> 
  56. </table> 
  57. </form> 
  58. <!-- /#resize_images --> 
  59. <!-- #new_thumbnail --> 
  60. <form id="new_thumbnail_dialog" method="POST" accept-charset="utf-8"> 
  61. <?php wp_nonce_field( 'ngg_thickbox_form' ) ?> 
  62. <input type="hidden" name="TB_type" class="TB_type" value=""> 
  63. <input type="hidden" id="new_thumbnail_imagelist" name="TB_imagelist" value=""> 
  64. <input type="hidden" id="new_thumbnail_bulkaction" name="TB_bulkaction" value=""> 
  65. <input type="hidden" name="TB_action" value="new_thumbnails"> 
  66. <table width="100%"> 
  67. <tr valign="top"> 
  68. <th align="left"><?php _e( 'Size', 'nggallery' ) ?></th> 
  69. <td> 
  70. <label for="thumbwidth"><?php _e( 'Width', 'nggallery' ) ?></label> 
  71. <input id="thumbwidth" class="small-text" type="number" min="0" name="thumbwidth" value="<?php echo $options['thumbwidth']; ?>"> 
  72. <label for="thumbheight"><?php _e( 'Height', 'nggallery' ) ?></label> 
  73. <input id="thumbheight" class="small-text" type="number" step="1" min="0" name="thumbheight" value="<?php echo $options['thumbheight']; ?>"> 
  74.  
  75. <p class="description"><?php _e( 'These values are maximum values ', 'nggallery' ) ?></p> 
  76. </td> 
  77. </tr> 
  78. <tr valign="top"> 
  79. <th align="left"> 
  80. <label for="thumb_fix"> 
  81. <?php _e( 'Fixed size', 'nggallery' ); ?> 
  82. </label> 
  83. </th> 
  84. <td> 
  85. <input id="thumb_fix" type="checkbox" name="thumb_fix" value="true" <?php checked( '1',  
  86. $options['thumbfix'] ); ?>> 
  87. <?php _e( 'This will ignore the aspect ratio, so no portrait thumbnails', 'nggallery' ) ?> 
  88. </td> 
  89. </tr> 
  90. </table> 
  91. </form> 
  92. <!-- /#new_thumbnail --> 
  93. <!-- #entertags --> 
  94. <form id="tags_dialog" method="POST" accept-charset="utf-8"> 
  95. <?php wp_nonce_field( 'ngg_thickbox_form' ) ?> 
  96. <input type="hidden" class="TB_type" name="TB_type" value=""> 
  97. <input type="hidden" id="tags_imagelist" name="TB_imagelist" value=""> 
  98. <input type="hidden" id="tags_bulkaction" name="TB_bulkaction" value=""> 
  99. <input type="hidden" name="TB_action" value=""> 
  100.  
  101. <div style="text-align: center"> 
  102. <label> 
  103. <?php _e( "Enter the tags", 'nggallery' ); ?><br> 
  104. <input name="taglist" type="text" value="" style="width: 90%"> 
  105. </label> 
  106. </div> 
  107. </form> 
  108. <!-- /#entertags --> 
  109. <!-- #selectgallery --> 
  110. <form id="select_gallery_dialog" method="POST" accept-charset="utf-8"> 
  111. <?php wp_nonce_field( 'ngg_thickbox_form' ) ?> 
  112. <input type="hidden" name="TB_type" class="TB_type" value=""> 
  113. <input type="hidden" id="select_gallery_imagelist" name="TB_imagelist" value=""> 
  114. <input type="hidden" id="select_gallery_bulkaction" name="TB_bulkaction" value=""> 
  115. <input type="hidden" name="TB_action" value=""> 
  116.  
  117. <div style="text-align: center"> 
  118. <label> 
  119. <?php _e( 'Select the destination gallery:', 'nggallery' ); ?><br> 
  120. <select id="dest_gid" name="dest_gid" style="width: 300px"> 
  121. <option value="0" selected="selected"><?php _e( "Select or search for a gallery",  
  122. 'nggallery' ); ?></option> 
  123. </select> 
  124. </label> 
  125. </div> 
  126. </form> 
  127. <!-- /#selectgallery --> 
  128. </div> 
  129. <?php 
  130.  
  131. /** 
  132. * Print the Javascript. 
  133. * @todo Maybe move this whole stuff to a seperate file and register it instead of inline? 
  134. */ 
  135. protected function print_scripts() { 
  136. ?> 
  137. <script type="text/javascript"> 
  138. jQuery(function() { 
  139. jQuery("[id^=doaction]").click(function(event) { 
  140. return handleBulkActions(event); 
  141. }); 
  142. jQuery("#dest_gid").nggAutocomplete({ 
  143. type: 'gallery',  
  144. domain: "<?php echo home_url('index.php', is_ssl() ? 'https' : 'http'); ?>" 
  145. }); 
  146. if (jQuery("#page_type").val() === 'gallery') { 
  147. jQuery('.TB_type').val('gallery'); 
  148. }); 
  149.  
  150. var doActionToSelector = { 
  151. doaction: "#bulk-action-selector-top",  
  152. doaction2: "#bulk-action-selector-bottom" 
  153. }; 
  154.  
  155. function handleBulkActions(event) { 
  156. var caller = event.target; 
  157. var $selector = jQuery(doActionToSelector[caller.id]); 
  158. var $selected = jQuery("input[name^=doaction]:checkbox:checked"); 
  159.  
  160. if ($selected.length < 1) { 
  161. alert('<?php echo esc_js(__('No images selected', 'nggallery')); ?>'); 
  162. return false; 
  163.  
  164. var cp = ''; 
  165. if (jQuery("#page_type").val() === 'gallery') { 
  166. cp = 'gallery_'; 
  167. console.log($selector.val()); 
  168. /** 
  169. * The options prepended with 'g' are the ones for the gallery page, the normal ones work with images. 
  170. */ 
  171. switch ($selector.val()) { 
  172. case "-1": 
  173. alert('<?php echo esc_js(__('No action selected.', 'nggallery')); ?>'); 
  174. break; 
  175. case 'resize_images': 
  176. bulkDialog('resize_images', '<?php echo esc_js(__('Resize images', 'nggallery')); ?>', $selected); 
  177. break; 
  178. case 'new_thumbnail': 
  179. bulkDialog('new_thumbnail', '<?php echo esc_js(__('Create new thumbnails', 'nggallery')); ?>', $selected); 
  180. break; 
  181. case 'import_meta': 
  182. ajaxOperation(cp + 'import_metadata', '<?php echo esc_js(__('Import metadata', 'nggallery')); ?>', $selected, true); 
  183. break; 
  184. case 'recover_images': 
  185. ajaxOperation(cp + 'recover_image', '<?php echo esc_js(__('Recover from backup', 'nggallery')); ?>', $selected, true); 
  186. break; 
  187. case 'set_watermark': 
  188. ajaxOperation(cp + 'set_watermark', '<?php echo esc_js(__('Set watermark', 'nggallery')); ?>', $selected, true); 
  189. break; 
  190. case "copy_to": 
  191. set_TB_command('select_gallery', 'copy_to'); 
  192. bulkDialog('select_gallery', '<?php echo esc_js(__('Copy image to...', 'nggallery')); ?>', $selected); 
  193. break; 
  194. case "move_to": 
  195. set_TB_command('select_gallery', 'move_to'); 
  196. bulkDialog('select_gallery', '<?php echo esc_js(__('Move image to...', 'nggallery')); ?>', $selected); 
  197. break; 
  198. case "add_tags": 
  199. set_TB_command('tags', 'add_tags'); 
  200. bulkDialog('tags', '<?php echo esc_js(__('Add new tags', 'nggallery')); ?>', $selected); 
  201. break; 
  202. case "delete_tags": 
  203. set_TB_command('tags', 'delete_tags'); 
  204. bulkDialog('tags', '<?php echo esc_js(__('Delete tags', 'nggallery')); ?>', $selected); 
  205. break; 
  206. case "overwrite_tags": 
  207. set_TB_command('tags', 'overwrite_tags'); 
  208. bulkDialog('tags', '<?php echo esc_js(__('Overwrite', 'nggallery')); ?>', $selected); 
  209. break; 
  210. case 'rotate_cw': 
  211. ajaxOperation('rotate_cw', '<?php echo esc_js(__('Rotate images', 'nggallery')); ?>', $selected); 
  212. break; 
  213. case 'rotate_ccw': 
  214. ajaxOperation('rotate_ccw', '<?php echo esc_js(__('Rotate images', 'nggallery')); ?>', $selected); 
  215. break; 
  216. default: 
  217. console.log($selected); 
  218. var images = $selected.map(function() { 
  219. return this.value; 
  220. }).get(); 
  221. var message = '<?php echo sprintf(esc_js(__("You are about to start bulk edits for %s galleries\n\n 'Cancel' to stop, 'OK' to proceed.", 'nggallery' )), "' + images.length + '") ?>'; 
  222. return confirm(message); 
  223. return false; 
  224.  
  225. function set_TB_command(id, command) { 
  226. jQuery('#' + id + "_dialog #TB_action").val(command); 
  227.  
  228. function ajaxOperation(command, title, $selected, warning) { 
  229.  
  230. var images = $selected.map(function() { 
  231. return this.value; 
  232. }).get(); 
  233.  
  234. if (warning) { 
  235. var message = '<?php echo sprintf(esc_js(__("You are about to start bulk edits for %s galleries\n\n 'Cancel' to stop, 'OK' to proceed.", 'nggallery' )), "' + images.length + '") ?>'; 
  236.  
  237. if (!confirm(message)) { 
  238. return false; 
  239.  
  240. var ajaxOptions = { 
  241. operation: command,  
  242. ids: images,  
  243. header: title,  
  244. maxStep: images.length 
  245. }; 
  246.  
  247. nggProgressBar.init(ajaxOptions); 
  248. nggAjax.init(ajaxOptions); 
  249.  
  250. function bulkDialog(id, title, $selected) { 
  251. jQuery('#' + id + "_bulkaction").val(id); 
  252. jQuery('#' + id + "_imagelist").val($selected.map(function() { 
  253. return this.value; 
  254. }).get().join(', ')); 
  255. showDialog('#' + id + "_dialog", title); 
  256.  
  257. function showDialog(id, title, onSubmit) { 
  258.  
  259. if (typeof onSubmit === 'undefined' || onSubmit === null) { 
  260. onSubmit = function(dialog) { 
  261. jQuery(dialog).submit(); 
  262.  
  263. jQuery(id).dialog({ 
  264. width: '50%',  
  265. resizable: true,  
  266. modal: true,  
  267. title: title,  
  268. close: function() { 
  269. jQuery(this).dialog('destroy').remove(); 
  270. },  
  271. buttons: [ 
  272. text: "<?php echo esc_js(__('Cancel', 'nggallery')); ?>",  
  273. 'class': "button dialog-cancel",  
  274. 'type': "reset",  
  275. click: function() { 
  276. jQuery(this).dialog('close'); 
  277. },  
  278. text: "<?php echo esc_js(__('OK', 'nggallery')); ?>",  
  279. 'class': "button-primary",  
  280. 'type': "submit",  
  281. click: function() { 
  282. onSubmit(this); 
  283. }); 
  284. </script> 
  285. <?php 
  286.  
  287. /** 
  288. * Handle the actions that require a dialog. 
  289. */ 
  290. protected function handle_dialog_actions() { 
  291.  
  292. $ngg_options = get_option( 'ngg_options' ); 
  293.  
  294. /** 
  295. * If the post type is 
  296. */ 
  297. if ( $_POST['TB_type'] === 'gallery' ) { 
  298. $mode = 'gallery'; 
  299. } else { 
  300. $mode = 'image'; 
  301.  
  302. check_admin_referer( 'ngg_thickbox_form' ); 
  303.  
  304. $list = explode( ', ', $_POST['TB_imagelist'] ); 
  305.  
  306. switch ( $_POST['TB_action'] ) { 
  307. case 'resize_images': 
  308. $data = array( 
  309. 'width' => (int) $_POST['imgWidth'],  
  310. 'height' => (int) $_POST['imgHeight'] 
  311. ); 
  312. $command = 'resize_image'; 
  313. $title = __( 'Resize images', 'nggallery' ); 
  314. nggAdmin::do_ajax_operation( $command, $list, $title, $mode, $data ); 
  315. return; 
  316. case 'new_thumbnails': 
  317. $data = array( 
  318. 'width' => (int) $_POST['thumbwidth'],  
  319. 'height' => (int) $_POST['thumbheight'],  
  320. 'fix' => isset( $_POST['thumb_fix'] ) ? true : false 
  321. ); 
  322. $command = 'create_thumbnail'; 
  323. $title = __( 'Create new thumbnails', 'nggallery' ); 
  324. nggAdmin::do_ajax_operation( $command, $list, $title, $mode, $data ); 
  325. return; 
  326. case 'copy_to': 
  327. $dest_gid = (int) $_POST['dest_gid']; 
  328. nggAdmin::copy_images( $list, $dest_gid ); 
  329.  
  330. return; 
  331. case 'move_to': 
  332. $dest_gid = (int) $_POST['dest_gid']; 
  333. nggAdmin::move_images( $list, $dest_gid ); 
  334.  
  335. return; 
  336. case 'add_tags': 
  337. $tag_list = explode( ', ', $_POST['taglist'] ); 
  338. $tag_list = array_map( 'trim', $tag_list ); 
  339. if ( is_array( $list ) ) { 
  340. foreach ( $list as $pic_id ) { 
  341. wp_set_object_terms( $pic_id, $tag_list, 'ngg_tag', true ); 
  342. nggGallery::show_message( __( 'Tags changed', 'nggallery' ) ); 
  343.  
  344. return; 
  345. case 'delete_tags': 
  346. $tag_list = explode( ', ', $_POST['taglist'] ); 
  347. $tag_list = array_map( 'trim', $tag_list ); 
  348. if ( is_array( $list ) ) { 
  349. foreach ( $list as $pic_id ) { 
  350. $old_tags = wp_get_object_terms( $pic_id, 'ngg_tag', 'fields=names' ); 
  351. // get the slugs, to vaoid case sensitive problems 
  352. $slug_array = array_map( 'sanitize_title', $tag_list ); 
  353. $old_tags = array_map( 'sanitize_title', $old_tags ); 
  354. // compare them and return the diff 
  355. $new_tags = array_diff( $old_tags, $slug_array ); 
  356. wp_set_object_terms( $pic_id, $new_tags, 'ngg_tag' ); 
  357. nggGallery::show_message( __( 'Tags changed', 'nggallery' ) ); 
  358.  
  359. return; 
  360. case 'overwrite_tags': 
  361. $tag_list = explode( ', ', $_POST['taglist'] ); 
  362. $tag_list = array_map( 'trim', $tag_list ); 
  363. if ( is_array( $list ) ) { 
  364. foreach ( $list as $pic_id ) { 
  365. wp_set_object_terms( $pic_id, $tag_list, 'ngg_tag' ); 
  366. nggGallery::show_message( __( 'Tags changed', 'nggallery' ) ); 
  367.  
  368. return; 
  369. default: 
  370. return; 
  371.  
  372. /** 
  373. * Handle the bulk actions. 
  374. */ 
  375. protected function handle_bulk_actions() { 
  376. //Check the nonce. 
  377. if ( wp_verify_nonce( $_POST['_wpnonce'], 'bulk-ngg-manager' ) === false ) { 
  378. nggGallery::show_error( __( 'You waited too long, or you cheated.', 'nggallery' ) ); 
  379.  
  380. return; 
  381.  
  382. global $wpdb, $ngg; 
  383.  
  384. if ( $_POST['action'] !== "-1" && $_POST['action2'] !== "-1" ) { 
  385. return; 
  386.  
  387. $a1 = $_POST['action']; 
  388. $a2 = $_POST['action2']; 
  389.  
  390. if ( $a1 === "delete_gallery" || $a2 === "delete_gallery" ) { 
  391. // Delete gallery 
  392. if ( is_array( $_POST['doaction'] ) ) { 
  393. $deleted = false; 
  394. foreach ( $_POST['doaction'] as $id ) { 
  395. // get the path to the gallery 
  396. $gallery = nggdb::find_gallery( $id ); 
  397. if ( $gallery ) { 
  398. //TODO:Remove also Tag reference, look here for ids instead filename 
  399. $imagelist = $wpdb->get_col( "SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery->gid' " ); 
  400. if ( $ngg->options['deleteImg'] ) { 
  401. if ( is_array( $imagelist ) ) { 
  402. foreach ( $imagelist as $filename ) { 
  403. @unlink( WINABSPATH . $gallery->path . '/thumbs/thumbs_' . $filename ); 
  404. @unlink( WINABSPATH . $gallery->path . '/' . $filename ); 
  405. @unlink( WINABSPATH . $gallery->path . '/' . $filename . '_backup' ); 
  406. // delete folder 
  407. @rmdir( WINABSPATH . $gallery->path . '/thumbs' ); 
  408. @rmdir( WINABSPATH . $gallery->path ); 
  409. do_action( 'ngg_delete_gallery', $id ); 
  410. $deleted = nggdb::delete_gallery( $id ); 
  411.  
  412. if ( $deleted ) { 
  413. nggGallery::show_message( __( 'Gallery deleted successfully.', 'nggallery' ) ); 
  414. } else { 
  415. nggGallery::show_error( __( 'Something went wrong.', 'nggallery' ) ); 
  416.  
  417. } elseif ( $a1 === "delete_images" || $a2 === "delete_images" ) { 
  418. global $nggdb; 
  419. if ( is_array( $_POST['doaction'] ) ) { 
  420. foreach ( $_POST['doaction'] as $imageID ) { 
  421. $image = $nggdb->find_image( $imageID ); 
  422. if ( $image ) { 
  423. if ( $ngg->options['deleteImg'] ) { 
  424. @unlink( $image->imagePath ); 
  425. @unlink( $image->thumbPath ); 
  426. @unlink( $image->imagePath . "_backup" ); 
  427. do_action( 'ngg_delete_picture', $image->pid ); 
  428. $delete_pic = nggdb::delete_image( $image->pid ); 
  429. if ( $delete_pic ) { 
  430. nggGallery::show_message( __( 'Pictures deleted successfully ', 'nggallery' ) );