-
Notifications
You must be signed in to change notification settings - Fork 2
/
gallery-zip.php
89 lines (73 loc) · 2.25 KB
/
gallery-zip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Plugin Name: Gallery Zip
* Plugin URI: http://yoda.neun12.de
* Description: Allows the user to zip all images in a gallery
* Version: 0.3
* Author: Ralf Albert
* Author URI: http://yoda.neun12.de
* Network: true
* License: GPLv3
*/
namespace GalleryZip;
add_action( 'plugins_loaded', __NAMESPACE__ . '\gallery_zip_start', 10, 0 );
/**
* Invoke the plugin and load the needed classes
*/
function gallery_zip_start() {
// simple autoloader
$classes = glob( dirname( __FILE__ ) . '/classes/*.php' );
if ( ! empty( $classes ) ) {
foreach ( $classes as $class )
require_once $class;
add_action( 'init', __NAMESPACE__ . '\add_hooks', 10, 0 );
add_action( 'init', __NAMESPACE__ . '\enqueue_scripts', 10, 0 );
if ( is_admin() )
return;
// this is only needed on the frontend
GalleryZip::get_instance( new GalleryZip_DataContainer() );
}
}
/**
* Adding the needed hooks
*/
function add_hooks() {
add_action( 'wp_ajax_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0 );
add_action( 'wp_ajax_nopriv_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0 );
}
/**
* Enqueu the JavaScript
*/
function enqueue_scripts() {
// load minified version if SCRIPT_DEBUG is true
$min = ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script(
'gallery-zip',
plugins_url(
sprintf( 'js/gallery_zip%s.js', $min ),
__FILE__
),
array( 'jquery' ),
false,
true
);
// set JS object with params
wp_localize_script( 'gallery-zip', 'GalleryZip', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
/**
* Ajax callback for creating the zip-file and sending the url to zip-file
*/
function get_gallery_zip() {
$send_result = function( $result = '' ) {
if ( is_array( $result ) )
$result = var_export( $result, true );
header( 'Content-type: application/json' );
die( json_encode( array( 'result' => $result ) ) );
};
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$gallery_id = (int) filter_input( INPUT_POST, 'gallery_id', FILTER_SANITIZE_NUMBER_INT );
if ( 0 >= $post_id )
$send_result( var_export( $_POST, true ) );
$images = GalleryZip::get_images_ajax_callback( $post_id, $gallery_id );
$send_result( $images );
}