awesomemotive / one-click-demo-import

One Click Demo Import plugin
https://www.awesomemotive.com/
195 stars 84 forks source link

Clear WP Cache (deleted "cache" folder) #96

Closed JoomFX closed 7 years ago

JoomFX commented 7 years ago

Hey guys,

First of all, thank you very much for the awesome plugin!

I'm trying to clear the WP cache (delete the cache folder) on pt-ocdi/after_import. Here's my code:

function ocdi_after_import_setup() {

    // Assign front page and posts page (blog page).
    $front_page_id = get_page_by_title( 'Home' );
    $blog_page_id  = get_page_by_title( 'Blog' );

    update_option( 'show_on_front', 'page' );
    update_option( 'page_on_front', $front_page_id->ID );
    update_option( 'page_for_posts', $blog_page_id->ID );

    // Clear Gantry 5 Cache (also parent directory but only if it's empty).
    global $wp_filesystem;

    $wp_filesystem->rmdir(WP_CONTENT_DIR . '/cache/gantry5', true);
    $wp_filesystem->rmdir(WP_CONTENT_DIR . '/cache', false);

}
add_action( 'pt-ocdi/after_import', 'ocdi_after_import_setup' );

But when I try to Import the Demo Data, I get Error: Internal Server Error (500) (see this screenshot).

If I remove the following code, then everything works fine - the Demo Data is imported but the cache is not cleared:

    // Clear Gantry 5 Cache (also parent directory but only if it's empty).
    global $wp_filesystem;

    $wp_filesystem->rmdir(WP_CONTENT_DIR . '/cache/gantry5', true);
    $wp_filesystem->rmdir(WP_CONTENT_DIR . '/cache', false);

I also tried with wp_cache_flush() but it doesn't clear the content of the wp-content/cache folder:

function ocdi_after_import_setup() {

    // Assign front page and posts page (blog page).
    $front_page_id = get_page_by_title( 'Home' );
    $blog_page_id  = get_page_by_title( 'Blog' );

    update_option( 'show_on_front', 'page' );
    update_option( 'page_on_front', $front_page_id->ID );
    update_option( 'page_for_posts', $blog_page_id->ID );

    // Clear WP Cache
    wp_cache_flush();

}
add_action( 'pt-ocdi/after_import', 'ocdi_after_import_setup' );

I'm really not good with PHP so I hope that I'm missing something simple. Thanks a lot for your help 😉

capuderg commented 7 years ago

Hi,

thanks for the kind words :)

About your code, I think the issue is in the global $wp_filesystem;. It's not defined by default (it's null), so you have to initialize the WP_Filesystem class first. More info here https://codex.wordpress.org/Filesystem_API.

Take care!

JoomFX commented 7 years ago

Hey @capuderg ,

You were right! After initializing WP_Filesystem everything works just fine!

Thanks a lot mate and have a wonderful weekend 😉

capuderg commented 7 years ago

Hi @JoomFX,

no problem :)

If you haven't yet, please write a review here: https://wordpress.org/support/plugin/one-click-demo-import/reviews/ We would really appreciate it!

Have a nice weekend as well.

Take care!