function pte_ajax()
{
    // Move all adjuntant functions to a separate file and include that here
    require_once PTE_PLUGINPATH . 'php/functions.php';
    PteLogger::debug("PARAMETERS: " . print_r($_REQUEST, true));
    switch ($_GET['pte-action']) {
        case "iframe":
            pte_init_iframe();
            break;
        case "resize-images":
            pte_resize_images();
            break;
        case "confirm-images":
            pte_confirm_images();
            break;
        case "delete-images":
            pte_delete_images();
            break;
        case "get-thumbnail-info":
            $id = (int) $_GET['id'];
            if (pte_check_id($id)) {
                print json_encode(pte_get_all_alternate_size_information($id));
            }
            break;
        case "change-options":
            pte_update_user_options();
            break;
    }
    die;
}
Example #2
0
function pte_resize_images()
{
    $logger = PteLogger::singleton();
    global $pte_sizes;
    // Require JSON output
    pte_require_json();
    $id = intval($_GET['id']);
    $w = pte_check_int($_GET['w']);
    $h = pte_check_int($_GET['h']);
    $x = pte_check_int($_GET['x']);
    $y = pte_check_int($_GET['y']);
    $save = isset($_GET['save']) && strtolower($_GET['save']) === "true";
    if (pte_check_id($id) === false || $w === false || $h === false || $x === false || $y === false) {
        return pte_json_error("ResizeImages initialization failed: '{$id}-{$w}-{$h}-{$x}-{$y}'");
    }
    // Check nonce
    if (!check_ajax_referer("pte-resize-{$id}", 'pte-nonce', false)) {
        return pte_json_error("CSRF Check failed");
    }
    // Get the sizes to process
    $pte_sizes = $_GET['pte-sizes'];
    if (!is_array($pte_sizes)) {
        $logger->debug("Converting pte_sizes to array");
        $pte_sizes = explode(",", $pte_sizes);
    }
    $sizes = pte_get_all_alternate_size_information($id);
    // The following information is common to all sizes
    // *** common-info
    $original_file = _load_image_to_edit_path($id);
    $original_size = @getimagesize($original_file);
    // SETS $PTE_TMP_DIR and $PTE_TMP_URL
    extract(pte_tmp_dir());
    $thumbnails = array();
    if (!$original_size) {
        return pte_json_error("Could not read image size");
    }
    $logger->debug("BASE FILE DIMENSIONS/INFO: " . print_r($original_size, true));
    list($orig_w, $orig_h, $orig_type) = $original_size;
    // *** End common-info
    // So this never interrupts the jpeg_quality anywhere else
    add_filter('jpeg_quality', 'pte_get_jpeg_quality');
    add_filter('wp_editor_set_quality', 'pte_get_jpeg_quality');
    foreach ($sizes as $size => $data) {
        // Get all the data needed to run image_create
        //
        //	$dst_w, $dst_h
        extract(pte_get_width_height($data, $w, $h));
        $logger->debug("WIDTHxHEIGHT: {$dst_w} x {$dst_h}");
        // Set the cropped filename
        $transparent = pte_is_crop_border_enabled($w, $h, $dst_w, $dst_h) && !pte_is_crop_border_opaque();
        $basename = pte_generate_filename($original_file, $dst_w, $dst_h, $transparent);
        $tmpfile = "{$PTE_TMP_DIR}{$id}" . DIRECTORY_SEPARATOR . "{$basename}";
        // === CREATE IMAGE ===================
        // This function is in wp-includes/media.php
        // We've added a filter to return our own editor which extends the wordpress one.
        add_filter('wp_image_editors', 'pte_image_editors');
        $editor = wp_get_image_editor($original_file);
        if (is_a($editor, "WP_Image_Editor_Imagick")) {
            $logger->debug("EDITOR: ImageMagick");
        }
        if (is_a($editor, "WP_Image_Editor_GD")) {
            $logger->debug("EDITOR: GD");
        }
        $crop_results = $editor->crop($x, $y, $w, $h, $dst_w, $dst_h);
        if (is_wp_error($crop_results)) {
            $logger->error("Error creating image: {$size}");
            continue;
        }
        // The directory containing the original file may no longer exist when
        // using a replication plugin.
        wp_mkdir_p(dirname($tmpfile));
        $tmpfile = dirname($tmpfile) . '/' . wp_unique_filename(dirname($tmpfile), basename($tmpfile));
        $tmpurl = "{$PTE_TMP_URL}{$id}/" . basename($tmpfile);
        if (is_wp_error($editor->save($tmpfile))) {
            $logger->error("Error writing image: {$size} to '{$tmpfile}'");
            continue;
        }
        // === END CREATE IMAGE ===============
        // URL: wp_upload_dir => base_url/subdir + /basename of $tmpfile
        // This is for the output
        $thumbnails[$size]['url'] = $tmpurl;
        $thumbnails[$size]['file'] = basename($tmpfile);
    }
    // Did you process anything?
    if (count($thumbnails) < 1) {
        return pte_json_error("No images processed");
    }
    $ptenonce = wp_create_nonce("pte-{$id}");
    // If save -- return pte_confirm_images
    if ($save) {
        function create_pte_confirm($thumbnail)
        {
            return $thumbnail['file'];
        }
        $_REQUEST['pte-nonce'] = $ptenonce;
        $_GET['pte-confirm'] = array_map('create_pte_confirm', $thumbnails);
        $logger->debug("CONFIRM:");
        $logger->debug(print_r($_GET, true));
        return pte_confirm_images(true);
    }
    return pte_json_encode(array('thumbnails' => $thumbnails, 'pte-nonce' => $ptenonce, 'pte-delete-nonce' => wp_create_nonce("pte-delete-{$id}")));
}
function pte_ajax()
{
    // Move all adjuntant functions to a separate file and include that here
    require_once PTE_PLUGINPATH . 'php/functions.php';
    $logger = PteLogger::singleton();
    $logger->debug("PARAMETERS: " . print_r($_REQUEST, true));
    switch ($_GET['pte-action']) {
        case "test":
            pte_test();
            break;
        case "launch":
            pte_launch();
            break;
        case "resize-images":
            pte_resize_images();
            break;
        case "confirm-images":
            pte_confirm_images();
            break;
        case "delete-images":
            pte_delete_images();
            break;
    }
    die;
}