• p15h

    (@prestonwordsworth)


    Currently, although the legend say On their publication and edit, the implementation doesn’t trigger the warmer upon edit.

    public function populate_enqueue( $new_status, $old_status, $post ) {
    if ( 'publish' === $new_status ) {
    $posts_enqueue = Cache_Warmer::$options->get( 'cache-warmer-posts-enqueue' );
    if ( ! in_array( $post->ID, $posts_enqueue, true ) ) {
    $posts_enqueue[] = $post->ID;
    Cache_Warmer::$options->set( 'cache-warmer-posts-enqueue', $posts_enqueue );
    }
    }
    }

    Here’s an example of a trigger that does respond to an edit, from the CF page cache plugin:

    $purge_actions = array(
    'deleted_post', // Delete a post
    'wp_trash_post', // Before a post is sent to the Trash
    'clean_post_cache', // After a post’s cache is cleaned
    'edit_post', // Edit a post - includes leaving comments
    'delete_attachment', // Delete an attachment - includes re-uploading
    'elementor/editor/after_save', // Elementor edit
    'elementor/core/files/clear_cache', // Elementor clear cache
    );

    foreach ($purge_actions as $action) {
    add_action($action, array($this, 'purge_cache_on_post_edit'), PHP_INT_MAX, 2);
    }

    function purge_cache_on_post_edit($postId)
    {

    static $done = [];

    if (isset($done[$postId])) {
    return;
    }

    // Do not run this on the WordPress Nav Menu Pages
    global $pagenow;
    if ($pagenow === 'nav-menus.php') return;

    if (($this->main_instance->get_single_config('cf_auto_purge', 0) > 0 || $this->main_instance->get_single_config('cf_auto_purge_all', 0) > 0) &&
    $this->is_cache_enabled()) {

    $current_action = function_exists('current_action') ? current_action() : "";

    $this->modules = $this->main_instance->get_modules();

    $error = '';

    $validPostStatus = ['publish', 'trash', 'private'];
    $thisPostStatus = get_post_status($postId);

    if (get_permalink($postId) != true || !in_array($thisPostStatus, $validPostStatus)) {
    return;
    }

    if (is_int(wp_is_post_autosave($postId)) || is_int(wp_is_post_revision($postId))) {
    return;
    }

    if ($this->main_instance->get_single_config('cf_auto_purge_all', 0) > 0) {
    $this->purge_all();
    return;
    }

    $savedPost = get_post($postId);

    if (is_a($savedPost, 'WP_Post') == false) {
    return;
    }

    $urls = $this->get_post_related_links($postId);

    $this->purge_urls($urls);
    $this->modules['logs']->add_log('cache_controller::purge_cache_on_post_edit', "Purge Cloudflare cache for only post id {$postId} and related contents - Fired action: {$current_action}");

    $done[$postId] = true;
    }
    }
Viewing 1 replies (of 1 total)
  • Thread Starter p15h

    (@prestonwordsworth)

    I’ve now identified the reason why populate_enqueue doesn’t get triggered on post update: it’s because it’s using the transition_post_status hook, which only fires when you refresh the page, whereas post update is usually done via Ajax.

    So, there’s two ways to go about this. One could use the edit_post hook like in the purge action above. Or one could register a custom hook:

    add_action('wp_ajax_' . WPACU_PLUGIN_ID . '_preload',     array($this, 'ajaxPreloadGuest'), PHP_INT_MAX);
    /**
    * This is triggered when /admin/admin-ajax.php is called (default WordPress AJAX handler)
    */
    public function ajaxPreloadGuest()
    {
    // Check nonce
    if ( ! isset( $_POST['wpacu_nonce'] ) || ! wp_verify_nonce( $_POST['wpacu_nonce'], 'wpacu_ajax_preload_url_nonce' ) ) {
    echo 'Error: The security nonce is not valid.';
    exit();
    }

    $pageUrl = isset($_POST['page_url']) ? $_POST['page_url'] : false;
    $pageUrlDomain = parse_url($pageUrl, PHP_URL_HOST);
    $pageUrlPreload = add_query_arg( array( 'wpacu_preload' => 1 ), $pageUrl );

    // Check if the URL is valid
    if (! filter_var($pageUrlPreload, FILTER_VALIDATE_URL)) {
    echo 'The URL
    '.$pageUrlPreload.' is not valid.';
    exit();
    }

    // Check the domain from "page_url" parameter
    if (strpos(site_url(), $pageUrlDomain) === false) {
    echo 'Error: Possible hacking attempt! The host name of the requested URL is not the same as the one of "Site Address (URL)" from "Settings" - "General".';
    exit();
    }

    // Check privileges
    if (! Menu::userCanAccessAssetCleanUp()) {
    echo 'Error: Not enough privileges to perform this action.';
    exit();
    }

    $response = wp_remote_get($pageUrlPreload);

    if (is_wp_error($response)) {
    // Any error generated during the fetch? Print it
    echo 'Error: '.$response->get_error_code();
    } else {
    // No errors
    echo 'Status Code: '.wp_remote_retrieve_response_code($response).' / Page URL (preload): ' . $pageUrlPreload . "\n\n";
    echo isset($response['body']) ? $response['body'] : 'No "body" key found from wp_remote_get(), the preload might not have triggered';
    }

    exit();
    }
    /**
    * This is triggered when /admin/admin-ajax.php is called (default WordPress AJAX handler)
    */
    public function ajaxPreloadGuest()
    {
    // Check nonce
    if ( ! isset( $_POST['wpacu_nonce'] ) || ! wp_verify_nonce( $_POST['wpacu_nonce'], 'wpacu_ajax_preload_url_nonce' ) ) {
    echo 'Error: The security nonce is not valid.';
    exit();
    }

    $pageUrl = isset($_POST['page_url']) ? $_POST['page_url'] : false;
    $pageUrlDomain = parse_url($pageUrl, PHP_URL_HOST);
    $pageUrlPreload = add_query_arg( array( 'wpacu_preload' => 1 ), $pageUrl );

    // Check if the URL is valid
    if (! filter_var($pageUrlPreload, FILTER_VALIDATE_URL)) {
    echo 'The URL '.$pageUrlPreload.' is not valid.';
    exit();
    }

    // Check the domain from "page_url" parameter
    if (strpos(site_url(), $pageUrlDomain) === false) {
    echo 'Error: Possible hacking attempt! The host name of the requested URL is not the same as the one of "Site Address (URL)" from "Settings" - "General".';
    exit();
    }

    // Check privileges
    if (! Menu::userCanAccessAssetCleanUp()) {
    echo 'Error: Not enough privileges to perform this action.';
    exit();
    }

    $response = wp_remote_get($pageUrlPreload);

    if (is_wp_error($response)) {
    // Any error generated during the fetch? Print it
    echo 'Error: '.$response->get_error_code();
    } else {
    // No errors
    echo 'Status Code: '.wp_remote_retrieve_response_code($response).' / Page URL (preload): ' . $pageUrlPreload . "\n\n";
    echo isset($response['body']) ? $response['body'] : 'No "body" key found from wp_remote_get(), the preload might not have triggered';
    }

    exit();
    }
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.