Plugin Directory

Changeset 3306838


Ignore:
Timestamp:
06/05/2025 06:42:07 AM (10 months ago)
Author:
levelsdev
Message:

add image library support

Location:
ready-made-oxygen-integration/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ready-made-oxygen-integration/trunk/includes/js/addPasteButton.js

    r3287833 r3306838  
    2929
    3030// :white_check_mark: Paste logic
    31 function triggerPaste(scope) {
     31async function triggerPaste(scope) {
    3232  if (!scope) {
    3333    return console.warn(
     
    3636  }
    3737
    38   navigator.clipboard
    39     .readText()
    40     .then(function (clipboardText) {
    41       window.localStorage.setItem("oxygen-componentbuffer", clipboardText);
    42       scope.$apply(() => {
    43         scope.$broadcast("pasteElement");
    44         console.log(":clipboard: pasteElement broadcasted");
    45       });
    46     })
    47     .catch(function (error) {
    48       console.error(":x: Error reading clipboard: ", error);
     38  try {
     39    console.log("READOXIN: Starting paste operation");
     40    const clipboardText = await navigator.clipboard.readText();
     41    console.log("READOXIN: Successfully read clipboard text");
     42   
     43    // Process the JSON to handle temp images
     44    console.log("READOXIN: Processing JSON for temp images");
     45    const processedJson = await processOxygenJson(clipboardText);
     46    console.log("READOXIN: JSON processing completed");
     47   
     48    window.localStorage.setItem("oxygen-componentbuffer", processedJson);
     49    scope.$apply(() => {
     50      scope.$broadcast("pasteElement");
     51      console.log("READOXIN: pasteElement broadcasted with processed images");
    4952    });
     53  } catch (error) {
     54    console.error("READOXIN: Error processing paste:", error);
     55  }
    5056}
    5157
     
    100106  document.head.appendChild(style);
    101107}
     108// Image processing functions
     109async function processOxygenJson(jsonString) {
     110    console.log("READOXIN: Starting JSON processing");
     111    let jsonData;
     112   
     113    try {
     114        jsonData = JSON.parse(jsonString);
     115        console.log("READOXIN: Successfully parsed JSON");
     116    } catch (e) {
     117        console.error('READOXIN: Invalid JSON, returning original string:', e);
     118        return jsonString;
     119    }
     120   
     121    // Process the JSON recursively
     122    console.log("READOXIN: Starting recursive element processing");
     123    await processJsonElement(jsonData);
     124    console.log("READOXIN: Completed recursive element processing");
     125   
     126    return JSON.stringify(jsonData);
     127}
     128
     129async function processJsonElement(element) {
     130    // Check if this element is a ct_image with temp URL
     131    if (element.name === 'ct_image' &&
     132        element.options &&
     133        element.options.original &&
     134        element.options.original.attachment_url &&
     135        element.options.original.attachment_url.includes('api.levels.dev/api/uploads/temp')) {
     136       
     137        console.log('READOXIN: Found temp image, processing:', element.options.original.attachment_url);
     138       
     139        try {
     140            console.log('READOXIN: Making AJAX request to process image');
     141            const response = await fetch(readoxinAjax.ajaxurl, {
     142                method: 'POST',
     143                headers: {
     144                    'Content-Type': 'application/x-www-form-urlencoded',
     145                },
     146                body: new URLSearchParams({
     147                    action: 'process_temp_image',
     148                    image_url: element.options.original.attachment_url,
     149                    nonce: readoxinAjax.nonce
     150                })
     151            });
     152           
     153            console.log('READOXIN: Received response from server');
     154            const result = await response.json();
     155           
     156            if (result.success) {
     157                console.log('READOXIN: Image processed successfully:', result.data);
     158               
     159                // Update the element with new attachment info
     160                element.options.original.image_type = "2";
     161                element.options.original.attachment_url = result.data.attachment_url;
     162                element.options.original.src = result.data.attachment_id.toString();
     163               
     164                console.log('READOXIN: Updated element with new image data - ID:', result.data.attachment_id, 'URL:', result.data.attachment_url);
     165            } else {
     166                console.error('READOXIN: Failed to process image:', result.data);
     167            }
     168        } catch (error) {
     169            console.error('READOXIN: Error processing image:', error);
     170        }
     171    }
     172   
     173    // Process children recursively
     174    if (element.children && Array.isArray(element.children)) {
     175        console.log('READOXIN: Processing', element.children.length, 'child elements');
     176        for (let child of element.children) {
     177            await processJsonElement(child);
     178        }
     179    }
     180}
     181
    102182console.log("add paste button js!");
    103183// This will work regardless of when your script runs
  • ready-made-oxygen-integration/trunk/ready-made-oxygen-integration.php

    r3287833 r3306838  
    4646    true
    4747  );
     48 
     49  // Pass nonce to JavaScript
     50  wp_localize_script('paste-button-js', 'readoxinAjax', array(
     51    'nonce' => wp_create_nonce('readoxin_process_images'),
     52    'ajaxurl' => admin_url('admin-ajax.php')
     53  ));
    4854}
    4955
     
    94100}
    95101
     102/**
     103 * Handle image processing for temp URLs
     104 */
     105function handle_temp_image_processing() {
     106    error_log('READOXIN: Starting temp image processing');
     107   
     108    // Verify nonce for security
     109    if (!wp_verify_nonce($_POST['nonce'], 'readoxin_process_images')) {
     110        error_log('READOXIN: Security check failed - invalid nonce');
     111        wp_die('Security check failed');
     112    }
     113   
     114    $image_url = sanitize_url($_POST['image_url']);
     115    error_log('READOXIN: Processing image URL: ' . $image_url);
     116   
     117    if (strpos($image_url, 'api.levels.dev/api/uploads/temp') === false) {
     118        error_log('READOXIN: Invalid temp image URL - does not contain expected domain');
     119        wp_send_json_error('Invalid temp image URL');
     120        return;
     121    }
     122   
     123    $attachment_id = process_temp_image($image_url);
     124   
     125    if ($attachment_id) {
     126        $attachment_url = wp_get_attachment_url($attachment_id);
     127        error_log('READOXIN: Image processed successfully - Attachment ID: ' . $attachment_id . ', URL: ' . $attachment_url);
     128        wp_send_json_success(array(
     129            'attachment_id' => $attachment_id,
     130            'attachment_url' => $attachment_url
     131        ));
     132    } else {
     133        error_log('READOXIN: Failed to process image');
     134        wp_send_json_error('Failed to process image');
     135    }
     136}
     137
     138/**
     139 * Download temp image and add to media library
     140 */
     141function process_temp_image($image_url) {
     142    error_log('READOXIN: Starting download of temp image: ' . $image_url);
     143   
     144    require_once(ABSPATH . 'wp-admin/includes/media.php');
     145    require_once(ABSPATH . 'wp-admin/includes/file.php');
     146    require_once(ABSPATH . 'wp-admin/includes/image.php');
     147   
     148    // Download the image
     149    error_log('READOXIN: Attempting to download image from URL');
     150    $temp_file = download_url($image_url);
     151   
     152    if (is_wp_error($temp_file)) {
     153        error_log('READOXIN: Failed to download image - ' . $temp_file->get_error_message());
     154        return false;
     155    }
     156   
     157    error_log('READOXIN: Image downloaded successfully to temp file: ' . $temp_file);
     158   
     159    // Get file info
     160    $file_array = array(
     161        'name' => basename($image_url),
     162        'tmp_name' => $temp_file
     163    );
     164   
     165    error_log('READOXIN: Attempting to add image to media library with filename: ' . basename($image_url));
     166   
     167    // Upload to media library
     168    $attachment_id = media_handle_sideload($file_array, 0);
     169   
     170    // Clean up temp file
     171    @unlink($temp_file);
     172    error_log('READOXIN: Cleaned up temp file');
     173   
     174    if (is_wp_error($attachment_id)) {
     175        error_log('READOXIN: Failed to add image to media library - ' . $attachment_id->get_error_message());
     176        return false;
     177    }
     178   
     179    error_log('READOXIN: Successfully added image to media library with ID: ' . $attachment_id);
     180    return $attachment_id;
     181}
     182
     183// Hook for AJAX requests
     184add_action('wp_ajax_process_temp_image', __NAMESPACE__ . '\\handle_temp_image_processing');
     185add_action('wp_ajax_nopriv_process_temp_image', __NAMESPACE__ . '\\handle_temp_image_processing');
     186
    96187init();
Note: See TracChangeset for help on using the changeset viewer.