junaidbhura / auto-cloudinary

Super simple Cloudinary auto-upload implementation for WordPress.
https://wordpress.org/plugins/auto-cloudinary/
MIT License
41 stars 9 forks source link

If ajax request, the plug-in cannot initialize #13

Closed bnldev closed 5 years ago

bnldev commented 5 years ago

if ( is_admin() ) { // if use admin-ajax.php, run this // Admin stuff Admin::get_instance()->setup(); } else { // Front-end stuff. Core::get_instance()->setup(); Frontend::get_instance()->setup(); }

junaidbhura commented 5 years ago

Hey @baronleung thanks for the issue. Can you provide more details about what kind of AJAX call are you trying to make? Is it an authenticated or unauthenticated AJAX call?

What are you trying to achieve? Can you add some code samples so I can better understand this issue?

bnldev commented 5 years ago

@junaidbhura sorry, my english is poor. hope you can understand what I mean.

// test plugin |auto-cloudinary-ajax-test |- auto-cloudinary-ajax-test.php |- js/test.js

auto-cloudinary-ajax-test.php

<?php / Plugin Name: Auto Cloudinary Ajax Test Description: test ajax Version: 1.0 Author: Baron Leung Author URI: http://localhost Text Domain: cloudinary ajax test /

add_action('wp_ajax_nopriv_cloudinary_test', 'cloudinary_ajax_test'); add_action('wp_ajax_cloudinary_test', 'cloudinary_ajax_test'); add_action('init', 'cloudinary_ajax_test_load_script');

if (!function_exists('cloudinary_ajax_test_load_script')) { function cloudinary_ajax_test_load_script() { wp_enqueue_script( 'cloudinary_ajax_test', plugin_dir_url(FILE). 'js/test.js', array('jquery') );

    wp_localize_script(
        'cloudinary_ajax_test',
        'cloudinary_ajax_test',
        array('ajax_url' => admin_url('admin-ajax.php'))
    );
}

}

if(!function_exists('cloudinary_ajax_test')) { function cloudinary_ajax_test() {

    // load template code
    // use a simple alternative here
    wp_send_json(array(
        // check issue
        // try modify the return value of

cloudinary_update_content_images() // return JB\Cloudinary\Core::get_instance();

        // @see namespace.php
        // It does not initialize

JB\Cloudinary\Core::get_instance()->setup(), // but initializes JB\Cloudinary\Admin::get_instance()->setup()

        'html' => cloudinary_update_content_images('<img

class="wp-image-123" src="http:://localhost/test.jpg">') ));

    wp_die();
}

}

js/test.js

(function ($) { var test_url;

if(typeof ajaxurl === "undefined") {
    test_url = cloudinary_ajax_test.ajax_url;
} else {
    test_url = ajaxurl;
}

$.get(test_url, {action: "cloudinary_test"}, function (res) {
    console.log(res.html);
});

})(jQuery);

On Wed, Jan 9, 2019 at 6:36 AM Junaid Bhura notifications@github.com wrote:

Hey @baronleung https://github.com/baronleung thanks for the issue. Can you provide more details about what kind of AJAX call are you trying to make? Is it an authenticated or unauthenticated AJAX call?

What are you trying to achieve? Can you add some code samples so I can better understand this issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/junaidbhura/auto-cloudinary/issues/13#issuecomment-452476986, or mute the thread https://github.com/notifications/unsubscribe-auth/AI4pDpmASO0WJGoT1xLE4B3bqKPqeh64ks5vBR1WgaJpZM4Z1a7X .

junaidbhura commented 5 years ago

@baronleung Okay I understand the problem now, thank you. Let me see if I can find an elegant solution for this.

junaidbhura commented 5 years ago

@baronleung I've created a fix on another branch: better-ajax.

Could you please download the plugin from this branch to see if it fixes your issue?

If it does, I can merge it into master and release an update to the plugin.

bnldev commented 5 years ago

sorry, I'm late checking my email. check DOING is ok, that's how I handled it

On Wed, Jan 23, 2019 at 6:26 PM Junaid Bhura notifications@github.com wrote:

@baronleung https://github.com/baronleung I've created a fix on another branch: better-ajax https://github.com/junaidbhura/auto-cloudinary/tree/better-ajax.

Could you please download the plugin from this branch to see if it fixes your issue?

If it does, I can merge it into master and release an update to the plugin.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/junaidbhura/auto-cloudinary/issues/13#issuecomment-456749459, or mute the thread https://github.com/notifications/unsubscribe-auth/AI4pDnOtrWo0wh-nhp8zF323enuI_iCTks5vGDjrgaJpZM4Z1a7X .