Download stopped working in 4.7.4

My plugin uses the following code to prompt users to download two external database files used by the plugin to fetch the users country code. Since 4.7.4, the files fail to download. (the spinner just keeps on spinning forever) It’s also preventing users from posting new posts until the message is dismissed. Any ideas what might have caused it to break?

/* Ask user to download GeoIP database files. */
add_action( 'admin_notices', 'csmi_dl_admin_notice' );
add_action( 'network_admin_notices', 'csmi_dl_admin_notice' ); // also show message on multisite
function csmi_dl_admin_notice() {
    $upload_dir = wp_upload_dir();
    $dir = $upload_dir[ 'basedir' ] .'/geoip';
    if ( !file_exists( $dir ) ) {
        wp_mkdir_p( $dir );
    }   
    $localfilev4 = $dir . '/GeoIPv4.dat';
    $localfilev6 = $dir . '/GeoIPv6.dat';
    $ignorefile = $dir . '/ignore.txt';
    $ctx = stream_context_create( array( 'http' => array( 'timeout' => 120 ) ) ); 
    if ( !file_exists( $localfilev4 ) && !file_exists( $ignorefile ) ) {
        if ( current_user_can( 'install_plugins' ) ) {
            ?>
            <script type="text/javascript">
            jQuery(document).ready(function($) {
                $("#download").click(
                    function () {
                        $('#download-div').html("<p><img src='<?php echo plugin_dir_url( __FILE__ ) . 'assets/resources/spinner.gif';?>' alt='Please Wait...'/></p><p>Please wait while the Geolite database files download. Typically takes 10-15 seconds.</p>");
                    }
                );
            });
            </script>
            <?php echo
            '<div class="notice notice-warning is-dismissible" id="download-div"><p>Important: The CSMI plugin uses Maxmind Geolite databases for better speed and accuracy. Click "Download" to install these files now.</p>
            <p><form action="" method="get">
            <input type="submit" class="button" id="download" name="download" value="Download" />
            <input type="submit" id="ignore" name="ignore" value="ignore" style="border: 0; background-color: transparent; color: grey; font-size: x-small; vertical-align: bottom; text-align: right;"/>
            </p></div>';
            if ($_GET) {
                if ( isset( $_GET[ 'download' ] ) ) {
                    $newfilev4 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv4.dat/download", 0, $ctx );
                    file_put_contents( $dir . '/GeoIPv4.dat', $newfilev4 );
                    if ( !file_exists( $localfilev6 ) ) {
                        $newfilev6 = file_get_contents( "https://sourceforge.net/projects/geoipupdate/files/GeoIPv6.dat/download", 0, $ctx );
                        file_put_contents( $dir . '/GeoIPv6.dat', $newfilev6 );
                        echo '<meta http-equiv="refresh" content="0">';
                        ?>
                        <div class="notice notice-success is-dismissible">
                            <p><?php echo "Success!"; ?></p>
                        </div>
                        <?php
                    }
                } elseif ( isset( $_GET[ 'ignore' ] ) ) {
                    $ignorefile = fopen( $dir . "/ignore.txt", "w" );
                    fclose( $ignorefile );
                    echo '<meta http-equiv="refresh" content="0">';
                    ?>
                    <div class="notice notice-success is-dismissible">
                        <p><?php echo "Success!"; ?></p>
                    </div>
                    <?php
                }
            }
        }
    }
}

Read more here: Download stopped working in 4.7.4

Leave a Reply

Your email address will not be published. Required fields are marked *