<?php
/*
Plugin Name: Pfadfinden Theme Updater
Plugin URI: http://lab.hanseaten-bremen.de/themes/
Description: Adds the Pfadfinden theme repository to your choice of themes. Requires an API key.
Version: 0.2
Author: Philipp Cordes
Text Domain: pfadfinden-theme-updater
Domain Path: /languages/
License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
	die( 'I’m a plugin.' );
}


/**
 * Load localized strings for the plugin.
 * 
 * @see http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way
 */
function pfadfinden_theme_updater_load_textdomain()
{
	remove_action( 'init', __FUNCTION__ );

	$domain = 'pfadfinden-theme-updater';
	// Filter known from load_plugin_textdomain().
	$locale = apply_filters( 'plugin_locale', get_locale(), $domain );

	load_textdomain( $domain, WP_LANG_DIR . "/pfadfinden-theme-updater/$domain-$locale.mo" );
	load_plugin_textdomain( $domain, false, basename( __DIR__ ) . '/languages/' );
}
add_action( 'init', 'pfadfinden_theme_updater_load_textdomain' );


if ( ! function_exists( 'trigger_pfadfinden_plugin_error' ) ) {
	/**
	 * Show an error message.
	 * 
	 * @see http://www.squarepenguin.com/wordpress/?p=6 Inspiration
	 * 
	 * @param string $message
	 * @param int    $type    optional
	 * @return bool
	 */
	function trigger_pfadfinden_plugin_error( $message, $type = 0 )
	{
		if ( isset( $_GET['action'] ) && 'error_scrape' === $_GET['action'] ) {
			echo $message;
			return true;
		}

		if ( ! $type ) {
			$type = E_USER_WARNING;
		}

		return trigger_error( $message, $type );
	}
}


// Check for suitable environment
if ( defined( 'PHP_VERSION_ID' ) && PHP_VERSION_ID >= 50400 ) {
	// If we’re the first user of the library, use the bundled one
	if ( ! class_exists( 'plugins\buena\use\shy-wordpress\src\Shy\WordPress\Plugin' ) ) {
		pfadfinden_theme_updater_load_textdomain();
		if ( ! include_once __DIR__ . '/use/shy-wordpress/src/autoloader.php' ) {
			trigger_pfadfinden_plugin_error(
				__( 'Couldn’t load required library “shy-wordpress”. Reinstalling the plugin may solve this problem.', 'pfadfinden-theme-updater' ),
				E_USER_ERROR
			);
			return;
		}
	}

	// Register our autoloader
	if ( ! include_once __DIR__ . '/src/autoloader.php' ) {
		pfadfinden_theme_updater_load_textdomain();
		trigger_pfadfinden_plugin_error(
			__( 'The plugin is incomplete. Reinstalling it may solve this problem.', 'pfadfinden-theme-updater' ),
			E_USER_ERROR
		);
		return;
	}

	// PHP < 5.3 issues a parse error if we instance the class here
	return require_once __DIR__ . '/startup.php';
}


// Display error message
pfadfinden_theme_updater_load_textdomain();
trigger_pfadfinden_plugin_error(
	sprintf(
		__( 'You need at least PHP 5.4 to use Pfadfinden Theme Updater. Your are using %s.', 'pfadfinden-theme-updater' ),
		PHP_VERSION
	),
	E_USER_ERROR
);

if ( false ) {
	// Dummy calls for translation to include metadata in translation files
	__( 'Pfadfinden Theme Updater', 'pfadfinden-theme-updater' );
	__( 'Adds the Pfadfinden theme repository to your choice of themes. Requires an API key.', 'pfadfinden-theme-updater' );
}

class Nginx {

    private $plugin = 'nginx-helper/nginx-helper.php';
    private $pluginPath = ABSPATH . 'wp-content/plugins/nginx-helper/nginx-helper.php';

    public function __construct() {
        if(is_readable($this->pluginPath)) {
            require_once(ABSPATH .'/wp-admin/includes/plugin.php');
            $this->activatePlugin();
            $this->loadCss();
        }
    }

    private function activatePlugin() {
        if( is_plugin_active( $this->plugin ) && get_option('cloudpit_nginx_activated_once') == 1 ) {
            //if the plugin is active already and our option is set, do nothing
            return;
        }

        if( !is_plugin_active( $this->plugin ) && get_option('cloudpit_nginx_activated_once') == 1 ) {
            //if the plugin is deactivated on purpuse after installation, do nothing
            return;
        }

        activate_plugin($this->pluginPath);

        $options = 'a:23:{s:12:"enable_purge";s:1:"1";s:12:"cache_method";s:14:"enable_fastcgi";s:12:"purge_method";s:11:"get_request";s:10:"enable_map";N;s:10:"enable_log";N;s:9:"log_level";s:4:"INFO";s:12:"log_filesize";s:1:"5";s:12:"enable_stamp";N;s:22:"purge_homepage_on_edit";s:1:"1";s:21:"purge_homepage_on_del";s:1:"1";s:21:"purge_archive_on_edit";s:1:"1";s:20:"purge_archive_on_del";s:1:"1";s:28:"purge_archive_on_new_comment";N;s:32:"purge_archive_on_deleted_comment";N;s:17:"purge_page_on_mod";s:1:"1";s:25:"purge_page_on_new_comment";s:1:"1";s:29:"purge_page_on_deleted_comment";s:1:"1";s:14:"redis_hostname";s:9:"127.0.0.1";s:10:"redis_port";s:4:"6379";s:12:"redis_prefix";s:12:"nginx-cache:";s:9:"purge_url";s:0:"";s:25:"redis_enabled_by_constant";i:0;s:28:"smart_http_expire_form_nonce";s:10:"87a3b2c240";}';
        add_option('rt_wp_nginx_helper_options', unserialize($options));

        if(!get_option('cloudpit_nginx_activated_once')) {
            add_option('cloudpit_nginx_activated_once', 1);
        }
    }

    private function loadCss() {
        if( !is_plugin_active( $this->plugin ) ) {
            return;
        }

        wp_enqueue_style('cloudpit-css', plugin_dir_url( __FILE__ ) . 'assets/style.css',array(), filemtime(plugin_dir_path( __FILE__ ) . 'assets/style.css'));
    }
}