C:\xampp\htdocs\landing\wp-content\plugins\instant-images\api\download.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * Custom /resize route
 *
 * @since 3.0
 * @author dcooney
 * @package instant-images
 */

add_action'rest_api_init', function () {
    
$my_namespace 'instant-images';
    
$my_endpoint  '/download';
    
register_rest_route(
        
$my_namespace,
        
$my_endpoint,
        array(
            
'methods'             => 'POST',
            
'callback'            => 'instant_images_download',
            
'permission_callback' => function () {
                return 
InstantImages::instant_img_has_access();
            },
        )
    );
});


/**
 * Resize Image and run thru media uploader
 *
 * @param WP_REST_Request $request Rest request object.
 * @return $response
 * @since 3.0
 * @author dcooney
 * @package instant-images
 */
function instant_images_downloadWP_REST_Request $request ) {

    if ( 
InstantImages::instant_img_has_access() ) {

        require_once 
ABSPATH 'wp-admin/includes/file.php';
        require_once 
ABSPATH 'wp-admin/includes/image.php';

        
// Get JSON Data.
        
$data json_decode$request->get_body(), true ); // Get contents of request body.

        
if ( $data ) {

            
$id        $data['id']; // Image ID.
            
$image_url $data['image_url']; // Image URL.
            
$filename  sanitize_text_field$data['filename'] ); // The filename.
            
$title     sanitize_text_field$data['title'] ); // Title.
            
$alt       sanitize_text_field$data['alt'] ); // Alt text.
            
$caption   sanitize_text_field$data['caption'] ); // Caption text.
            
$cfilename sanitize_title$data['custom_filename'] ); // Custom filename.
            
$parent_id = ( $data['parent_id'] ) ? sanitize_title$data['parent_id'] ) : 0// Parent post ID.
            
$name      = ( ! empty( $cfilename ) ) ? $cfilename '.jpg' $filename// Actual filename.

            // Check if remote file exists.
            
if ( ! instant_images_remote_file_exists$image_url ) ) {
                
// Errorhandling.
                
$response = array(
                    
'success'    => false,
                    
'msg'        => __'Image does not exist or there was an error accessing the remote file.''instant-images' ),
                    
'id'         => $id,
                    
'attachment' => '',
                    
'admin_url'  => admin_url(),
                );
                
wp_send_json$response );
            }

            
// Send request to `wp_remote_get`.
            
$response wp_remote_get$image_url );
            if ( 
is_wp_error$response ) ) {
                return new 
WP_Error100__'Image download failed, please try again. Errors:''instant-images' ) . PHP_EOL $response->get_error_message() );
            }

            
// Get Headers.
            
$type wp_remote_retrieve_header$response'content-type' );
            if ( ! 
$type ) {
                return new 
WP_Error100__'Image type could not be determined''instant-images' ) );
            }

            
// Upload remote file.
            
$mirror wp_upload_bits$namenullwp_remote_retrieve_body$response ) );

            
// Build Attachment Data Array.
            
$attachment = array(
                
'post_title'     => $title,
                
'post_excerpt'   => $caption,
                
'post_content'   => '',
                
'post_status'    => 'inherit',
                
'post_mime_type' => $type,
            );

            
// Insert as attachment.
            
$image_id wp_insert_attachment$attachment$mirror['file'], $parent_id );

            
// Add Alt Text as Post Meta.
            
update_post_meta$image_id'_wp_attachment_image_alt'$alt );

            
// Generate Metadata.
            
$attach_data wp_generate_attachment_metadata$image_id$mirror['file'] );
            
wp_update_attachment_metadata$image_id$attach_data );

            
// Resize original image to max size (set in Instant Images settings).
            
instant_images_resize_download$name );

            
// Success.
            
$response = array(
                
'success'    => true,
                
'msg'        => __'Image successfully uploaded to the media library!''instant-images' ),
                
'id'         => $id,
                
'admin_url'  => admin_url(),
                
'attachment' => array(
                    
'id'      => $image_id,
                    
'url'     => wp_get_attachment_url$image_id ),
                    
'alt'     => $alt,
                    
'caption' => $caption,
                ),
            );

            
wp_send_json$response );

        } else {

            
$response = array(
                
'success'    => false,
                
'msg'        => __'There was an error getting image details from the request, please try again.''instant-images' ),
                
'id'         => '',
                
'attachment' => '',
                
'url'        => '',
            );

            
wp_send_json$response );

        }
    }
}


/**
 * Check if a remote image file exists.
 *
 * @param string $url The url to the remote image.
 * @return bool Whether the remote image exists.
 * @since 3.0
 * @author dcooney
 * @package instant-images
 */
function instant_images_remote_file_exists$url ) {
    
$response wp_remote_head$url );
    return 
200 === wp_remote_retrieve_response_code$response );
}



/**
 * Resize original image to max size (set in Instant Images settings)
 *
 * @param string $filename the image filename.
 * @since 3.0
 * @author dcooney
 * @package instant-images
 */
function instant_images_resize_download$filename ) {

    
$options    get_option'instant_img_settings' );
    
$download_w = isset( $options['unsplash_download_w'] ) ? $options['unsplash_download_w'] : 1600// width.
    
$download_h = isset( $options['unsplash_download_h'] ) ? $options['unsplash_download_h'] : 1200// height.

    
require_once ABSPATH 'wp-admin/includes/file.php';
    require_once 
ABSPATH 'wp-admin/includes/image.php';

    
$uploads_dir    wp_upload_dir();
    
$original_image wp_get_image_editor$uploads_dir['path'] . '/' $filename );
    if ( ! 
is_wp_error$original_image ) ) {
        
$original_image->resize$download_w$download_hfalse );
        
$original_image->save$uploads_dir['path'] . '/' $filename );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586