Skip to content

Commit

Permalink
adding new files
Browse files Browse the repository at this point in the history
  • Loading branch information
futtta committed Feb 17, 2018
1 parent 6c04caa commit 9a0c57a
Show file tree
Hide file tree
Showing 68 changed files with 17,451 additions and 0 deletions.
140 changes: 140 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}

tmpdir=${TMPDIR-/tmp}
tmpdir=${tmpdir%/}

WP_TESTS_DIR=${WP_TESTS_DIR-$tmpdir/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$tmpdir/wordpress/}


download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}

if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
WP_TESTS_TAG="tags/$WP_VERSION"
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
exit 1
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {

if [ -d $WP_CORE_DIR ]; then
return;
fi

mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p /tmp/wordpress-nightly
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i .bak'
else
local ioption='-i'
fi

# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
fi

cd $WP_TESTS_DIR

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# TODO/FIXME:
# In msys/mingw case $WP_TESTS_DIR has the "wrong" style
# of path written in the config file, i.e.:
#
# ```php
# define( 'ABSPATH', '/c/Windows/Temp/wordpress/' );
# ```
#
# If that's happening to you, edit that file manually once after
# running this script and change it so it becomes:
#
# ```php
# define( 'ABSPATH', 'C:/Windows/Temp/wordpress/' );
# ```
#
# Running `php phpunit.phar` within your plugin directory should
# work as expected after that.
sed $ioption "s|dirname( __FILE__ ) . '/src/'|'$WP_CORE_DIR'|" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
fi
}

install_db() {
# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if ! [ -z $DB_HOSTNAME ] ; then
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_wp
install_test_suite
install_db
23 changes: 23 additions & 0 deletions bin/readme-windows.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
C:\github\autoptimize>C:\PortableGit\bin\bash.exe bin/install-wp-tests.sh wordpress_test webodjel webber localhost 4.3.1

C:\PortableGit\bin\bash.exe install-wp-tests.sh

Open `C:\Windows\Temp\wordpress-tests-lib\wp-tests-config.php` file and:

# TODO/FIXME:
# In msys/mingw case $WP_TESTS_DIR has the "wrong" style
# of path written in the config file, i.e.:
#
# ```php
# define( 'ABSPATH', '/c/Windows/Temp/wordpress/' );
# ```
#
# If that's happening to you, edit that file manually once after
# running this script and change it so it becomes:
#
# ```php
# define( 'ABSPATH', 'C:/Windows/Temp/wordpress/' );
# ```
#
# Running `php phpunit.phar` within your plugin directory should
# work as expected after that.
57 changes: 57 additions & 0 deletions classes/autoptimizeCSSmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Thin wrapper around css minifiers to avoid rewriting a bunch of existing code.
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class autoptimizeCSSmin
{
/**
* Minifier instance.
*
* @var Autoptimize\tubalmartin\CssMin\Minifier|null
*/
protected $minifier = null;

/**
* Construtor.
*
* @param bool $raise_limits Whether to raise memory limits or not. Default true.
*/
public function __construct( $raise_limits = true )
{
$this->minifier = new Autoptimize\tubalmartin\CssMin\Minifier( $raise_limits );
}

/**
* Runs the minifier on given string of $css.
* Returns the minified css.
*
* @param string $css CSS to minify.
*
* @return string
*/
public function run( $css )
{
$result = $this->minifier->run( $css );

return $result;
}

/**
* Static helper.
*
* @param string $css CSS to minify.
*
* @return string
*/
public static function minify( $css )
{
$minifier = new self();

return $minifier->run( $css );
}
}
84 changes: 84 additions & 0 deletions classes/autoptimizeCacheChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* CacheChecker - new in AO 2.0
*
* Daily cronned job (filter to change freq. + filter to disable).
* Checks if cachesize is > 0.5GB (size is filterable), if so, an option is set which controls showing an admin notice.
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class autoptimizeCacheChecker
{
const SCHEDULE_HOOK = 'ao_cachechecker';

public function __construct()
{
}

public function run()
{
$this->add_hooks();
}

public function add_hooks()
{
if ( is_admin() ) {
add_action( 'plugins_loaded', array( $this, 'setup' ) );
}
add_action( self::SCHEDULE_HOOK, array( $this, 'cronjob' ) );
add_action( 'admin_notices', array( $this, 'show_admin_notice' ) );
}

public function setup()
{
$do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true );
$schedule = wp_get_schedule( self::SCHEDULE_HOOK );
$frequency = apply_filters( 'autoptimize_filter_cachecheck_frequency', 'daily' );
if ( ! in_array( $frequency, array( 'hourly', 'daily', 'monthly' ) ) ) {
$frequency = 'daily';
}
if ( $do_cache_check && ( ! $schedule || $schedule !== $frequency ) ) {
wp_schedule_event( time(), $frequency, self::SCHEDULE_HOOK );
} elseif ( $schedule && ! $do_cache_check ) {
wp_clear_scheduled_hook( self::SCHEDULE_HOOK );
}
}

public function cronjob()
{
$max_size = (int) apply_filters( 'autoptimize_filter_cachecheck_maxsize', 536870912 );
$do_cache_check = (bool) apply_filters( 'autoptimize_filter_cachecheck_do', true );
$stat_array = autoptimizeCache::stats();
$cache_size = round( $stat_array[1] );
if ( ( $cache_size > $max_size ) && ( $do_cache_check ) ) {
update_option( 'autoptimize_cachesize_notice', true );
if ( apply_filters( 'autoptimize_filter_cachecheck_sendmail', true ) ) {
$site_url = esc_url( site_url() );
$ao_mailto = apply_filters( 'autoptimize_filter_cachecheck_mailto', get_option( 'admin_email', '' ) );

$ao_mailsubject = __( 'Autoptimize cache size warning', 'autoptimize' ) . ' (' . $site_url . ')';
$ao_mailbody = __( 'Autoptimize\'s cache size is getting big, consider purging the cache. Have a look at https://wordpress.org/plugins/autoptimize/faq/ to see how you can keep the cache size under control.', 'autoptimize' ) . ' (site: ' . $site_url . ')';

if ( ! empty( $ao_mailto ) ) {
$ao_mailresult = wp_mail( $ao_mailto, $ao_mailsubject, $ao_mailbody );
if ( ! $ao_mailresult ) {
error_log( 'Autoptimize could not send cache size warning mail.' );
}
}
}
}
}

public function show_admin_notice()
{
if ( (bool) get_option( 'autoptimize_cachesize_notice', false ) ) {
echo '<div class="notice notice-warning"><p>';
_e( '<strong>Autoptimize\'s cache size is getting big</strong>, consider purging the cache. Have a look at <a href="https://wordpress.org/plugins/autoptimize/faq/" target="_blank" rel="noopener noreferrer">the Autoptimize FAQ</a> to see how you can keep the cache size under control.', 'autoptimize' );
echo '</p></div>';
update_option( 'autoptimize_cachesize_notice', false );
}
}
}
Loading

0 comments on commit 9a0c57a

Please sign in to comment.