1

I am facing an issue of update now button not showing up even if there is an update for a plugin. The following is the method that checks the updates for the plugin

public function check_update( $_transient_data ) {
        
        global $pagenow;

        if ( ! is_object( $_transient_data ) ) {
            $_transient_data = new stdClass;
            
        }

        if ( 'plugins.php' == $pagenow && is_multisite() ) {
            return $_transient_data;
        }
        
        if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
            return $_transient_data;
        }

        $version_info = $this->get_cached_version_info();

        if ( false === $version_info ) {
            $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
            error_log('version info from api'.wp_json_encode($version_info));
            $this->set_version_info_cache( $version_info );

        }

        if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {

            if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {

                $_transient_data->response[ $this->name ] = $version_info;

            }

            $_transient_data->last_checked           = time();
            $_transient_data->checked[ $this->name ] = $this->version;

        }

        return $_transient_data;
    }

This is the method that gets the info related to version info in the cache:

public function get_cached_version_info( $cache_key = '' ) {

        if( empty( $cache_key ) ) {
            $cache_key = $this->cache_key;
        }

        $cache = get_option( $cache_key );

        if( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) {
            return false; // Cache is expired
        }

        return json_decode( $cache['value'] );

    }

And this is the method which sets the version info in the cache:

public function set_version_info_cache( $value = '', $cache_key = '' ) {

        if( empty( $cache_key ) ) {
            $cache_key = $this->cache_key;
        }

        $data = array(
            'timeout' => strtotime( '+3 hours', time() ),
            'value'   => json_encode( $value )
        );

        update_option( $cache_key, $data, 'no' );

    }

When I remove/delete the update_plugins transient the updates started to show up but again after some time the again the update now button disappears only showing that there is an update for the plugin. I tried to use the following but it is not a recommendable approach:

public function force_update_check() {
    delete_site_transient( 'update_plugins' );
}

Therefore a but help needed to fix the issue. This is the method for showing update notification

    public function show_update_notification( $file, $plugin ) {

        if ( is_network_admin() ) {
            return;
        }

        if( ! current_user_can( 'update_plugins' ) ) {
            return;
        }

        if( ! is_multisite() ) {
            return;
        }

        if ( $this->name != $file ) {
            return;
        }

        // Remove our filter on the site transient
        remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );

        $update_cache = get_site_transient( 'update_plugins' );

        $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass();

        if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {

            $version_info = $this->get_cached_version_info();

            if ( false === $version_info ) {
                $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );

                $this->set_version_info_cache( $version_info );
            }

            if ( ! is_object( $version_info ) ) {
                return;
            }

            if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {

                $update_cache->response[ $this->name ] = $version_info;

            }

            $update_cache->last_checked = time();
            $update_cache->checked[ $this->name ] = $this->version;

            set_site_transient( 'update_plugins', $update_cache );

        } else {

            $version_info = $update_cache->response[ $this->name ];

        }

        // Restore our filter
        add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );

        if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {

            // build a plugin list row, with update notification
            $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
            # <tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange">
            echo '<tr class="plugin-update-tr" id="' . $this->slug . '-update" data-slug="' . $this->slug . '" data-plugin="' . $this->slug . '/' . $file . '">';
            echo '<td colspan="3" class="plugin-update colspanchange">';
            echo '<div class="update-message notice inline notice-warning notice-alt">';

            $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );

            if ( empty( $version_info->download_link ) ) {
                printf(
                    __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'easy-digital-downloads' ),
                    esc_html( $version_info->name ),
                    '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
                    esc_html( $version_info->new_version ),
                    '</a>'
                );
            } else {
                printf(
                    __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'easy-digital-downloads' ),
                    esc_html( $version_info->name ),
                    '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
                    esc_html( $version_info->new_version ),
                    '</a>',
                    '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) .'">',
                    '</a>'
                );
            }

            do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );

            echo '</div></td></tr>';
        }
    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.