Plugin Directory

Changeset 3222670


Ignore:
Timestamp:
01/15/2025 08:13:47 AM (14 months ago)
Author:
litexten
Message:

fixes

Location:
litcommerce/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • litcommerce/trunk/changelog.txt

    r3135220 r3222670  
    1414
    1515*Add: "Allows retaining the order number in WooCommerce."
     16
     17= 1.2.0 2024-12-12 =
     18
     19*Update: "Menu Page"
     20
     21= 1.2.1 2024-12-25 =
     22
     23*Fixed: "Menu Page"
     24
     25= 1.2.2 2025-01-13 =
     26
     27*Fixed: "Menu Page"
     28
     29= 1.2.3 2025-01-15 =
     30
     31*Add: "Api Add/Delete Image For Product"
  • litcommerce/trunk/litcommerce.php

    r3211347 r3222670  
    33Plugin Name: LitCommerce
    44Description: Helps you easily integrate your WooCommerce store with LitCommerce.
    5 Version: 1.2.0
     5Version: 1.2.3
    66Author: LitCommerce
    77Author URI: https://litcommerce.com
    88License: GPL2
    99Text Domain: litcommerce
     10Requires Plugins: woocommerce
    1011*/
    1112
     
    5152
    5253    function is_multy_app(){
    53         if (is_plugin_active('litcommerce_feed/litcommerce_feed.php')) {
     54        if (is_plugin_active('litcommerce-feed/litcommerce-feed.php')) {
    5455            return true;
    5556        }
     
    197198add_action('admin_menu', [$litcommercePlugin, 'registerPluginHooks']);
    198199add_filter('woocommerce_rest_product_object_query', function ( array $args, \WP_REST_Request $request ) {
    199     $modified_after = $request->get_param('modified_after');
     200    $modified_after = get_litc_params('modified_after');
    200201
    201202    if (!$modified_after) {
     
    213214    ];
    214215    foreach ($fields as $field => $param) {
    215         if ($request->get_param($param)) {
    216             $args[$field] = $request->get_param($param);
     216        if (get_litc_params($param)) {
     217            $args[$field] = get_litc_params($param);
    217218        }
    218219    }
     
    224225}, 10, 2);
    225226add_filter('woocommerce_rest_shop_order_object_query', function ( array $args, \WP_REST_Request $request ) {
    226     $modified_after = $request->get_param('modified_after');
     227    $modified_after = get_litc_params('modified_after');
    227228
    228229    if (!$modified_after) {
     
    240241    ];
    241242    foreach ($fields as $field => $param) {
    242         if ($request->get_param($param)) {
    243             $args[$field] = $request->get_param($param);
     243        if (get_litc_params($param)) {
     244            $args[$field] = get_litc_params($param);
    244245        }
    245246    }
     
    370371add_filter('woocommerce_shop_order_search_fields', 'litc_shop_order_meta_search_fields', 10, 1);
    371372function litc_woocommerce_rest_prepare_product_object( $response, $object, $request ) {
    372     if ($request->get_param("custom_currency") == 1) {
     373    if (get_litc_params("custom_currency") == 1) {
    373374        $meta = get_post_meta($object->get_id());
    374375        foreach ($meta as $key => $value) {
     
    379380    }
    380381
    381     if ($request->get_param("get_terms")) {
    382         $terms = explode(',', $request->get_param("get_terms"));
     382    if (get_litc_params("get_terms")) {
     383        $terms = explode(',', get_litc_params("get_terms"));
    383384        foreach ($terms as $term) {
    384385            $terms_data = wp_get_post_terms($object->get_id(), $term);
     
    453454function get_litc_params($key)
    454455{
    455     if(isset($_GET[$key])) {
    456         return $_GET[$key];
    457     }
    458     return null;
     456    $value = filter_input( INPUT_GET, $key, FILTER_SANITIZE_STRING );
     457    if(!$value){
     458        $value = null;
     459    }
     460    return $value;
    459461}
    460462
     
    518520
    519521add_filter('woocommerce_rest_pre_insert_shop_order_object', 'litc_woocommerce_rest_pre_insert_shop_order_object', 10);
     522
     523
     524add_action('rest_api_init', function () {
     525    register_rest_route('wc/v3/litc', '/products/(?P<id>\d+)/images', array(
     526        'methods' => 'POST',
     527        'callback' => 'litc_add_product_images',
     528        'permission_callback' => function () {
     529            return current_user_can('edit_products');
     530        },
     531    ));
     532    register_rest_route('wc/v3/litc', '/products/(?P<product_id>\d+)/images/(?P<image_id>\d+)', array(
     533        'methods' => 'DELETE',
     534        'callback' => 'litc_delete_product_image',
     535        'permission_callback' => function () {
     536            return current_user_can('edit_products');
     537        },
     538    ));
     539});
     540
     541function litc_add_product_images($request) {
     542    $product_id = $request['id'];
     543    $images = $request->get_param('images');
     544
     545    if (!is_array($images) || empty($images)) {
     546        return new WP_Error('invalid_images', 'Invalid images array', array('status' => 400));
     547    }
     548
     549    if (!get_post($product_id) || get_post_type($product_id) !== 'product') {
     550        return new WP_Error('invalid_product', 'Product not found', array('status' => 404));
     551    }
     552
     553    $uploaded_images = [];
     554
     555    foreach ($images as $image_url) {
     556        $image_id = litc_upload_image_from_url($image_url);
     557
     558        if (is_wp_error($image_id)) {
     559            return new WP_Error('image_upload_failed', 'Failed to upload image: ' . $image_url, array('status' => 500));
     560        }
     561
     562        $uploaded_images[] = $image_id;
     563    }
     564
     565    $product = wc_get_product($product_id);
     566    $existing_gallery = $product->get_gallery_image_ids();
     567    $updated_gallery = array_merge($existing_gallery, $uploaded_images);
     568    $product->set_gallery_image_ids($updated_gallery);
     569    $product->save();
     570
     571    return rest_ensure_response([
     572        'success' => true,
     573        'uploaded_images' => $uploaded_images,
     574    ]);
     575}
     576
     577function litc_upload_image_from_url($image_url) {
     578    $upload_dir = wp_upload_dir();
     579    $image_data = file_get_contents($image_url);
     580
     581    if (!$image_data) {
     582        return new WP_Error('image_fetch_failed', 'Could not fetch image from URL.');
     583    }
     584
     585    $filename = basename($image_url);
     586    $file_path = $upload_dir['path'] . '/' . $filename;
     587
     588    file_put_contents($file_path, $image_data);
     589
     590    $filetype = wp_check_filetype($filename, null);
     591
     592    if (!$filetype['type']) {
     593        return new WP_Error('invalid_image_type', 'Invalid image type.');
     594    }
     595
     596    $attachment_id = wp_insert_attachment(
     597        [
     598            'guid' => $upload_dir['url'] . '/' . $filename,
     599            'post_mime_type' => $filetype['type'],
     600            'post_title' => sanitize_file_name($filename),
     601            'post_content' => '',
     602            'post_status' => 'inherit',
     603        ],
     604        $file_path
     605    );
     606
     607    require_once(ABSPATH . 'wp-admin/includes/image.php');
     608    $attach_data = wp_generate_attachment_metadata($attachment_id, $file_path);
     609    wp_update_attachment_metadata($attachment_id, $attach_data);
     610
     611    return $attachment_id;
     612}
     613
     614
     615function litc_delete_product_image($request) {
     616    $product_id = $request['product_id'];
     617    $image_id = $request['image_id'];
     618    $force = $request->get_param('force');
     619    if (!get_post($product_id) || get_post_type($product_id) !== 'product') {
     620        return new WP_Error('invalid_product', 'Product not found', array('status' => 404));
     621    }
     622
     623    if (!get_post($image_id)) {
     624        return new WP_Error('invalid_image', 'Image not found', array('status' => 404));
     625    }
     626
     627    $product = wc_get_product($product_id);
     628    $gallery = $product->get_gallery_image_ids();
     629
     630    if (!in_array($image_id, $gallery)) {
     631        return new WP_Error('image_not_in_product', 'Image is not associated with this product.', array('status' => 400));
     632    }
     633
     634    $updated_gallery = array_diff($gallery, [$image_id]);
     635    $product->set_gallery_image_ids($updated_gallery);
     636    $product->save();
     637
     638    if ($force === true || $force === 'true') {
     639        wp_delete_attachment($image_id, true);
     640    }
     641
     642
     643    return rest_ensure_response([
     644        'success' => true,
     645        'message' => 'Image removed from product successfully.',
     646        'remaining_images' => $updated_gallery,
     647    ]);
     648}
  • litcommerce/trunk/readme.txt

    r3194630 r3222670  
    33Contributors: LitCommerce
    44Tags: WooCommerce, Amazon, eBay, Etsy, TikTok
    5 Requires at least: 5.0
    65Tested up to: 6.7.1
    7 Requires PHP: 5.6
    8 Stable tag: 1.0.5
     6Stable tag: 1.2.3
    97License: GPL-2.0
    108License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • litcommerce/trunk/steps/SendWooCommerceKeys.php

    r3211347 r3222670  
    2727        $url     .= '&channel_url=' . urlencode( site_url() );
    2828        $url     .= '&from_app=marketplace';
     29        $url     .= '&version_plugin=1.2.3';
    2930
    3031        if(@$_GET['reconnect'] == 1){
Note: See TracChangeset for help on using the changeset viewer.