get_w3tc_defaults(); if (!class_exists('W3_ConfigData')) { $this->enable_w3_total_cache(); } $config_writer = new W3_ConfigWriter(0, false); foreach ($vars as $name => $val) { $config_writer->set($name, $val); } $config_writer->set('common.instance_id', mt_rand()); $config_writer->save(); $this->refresh_w3tc(); } public function get_w3tc_defaults() { return array( 'pgcache.check.domain' => true, 'pgcache.prime.post.enabled' => true, 'pgcache.reject.logged' => true, 'pgcache.reject.request_head' => true, 'pgcache.purge.front_page' => true, 'pgcache.purge.home' => true, 'pgcache.purge.post' => true, 'pgcache.purge.comments' => true, 'pgcache.purge.author' => true, 'pgcache.purge.terms' => true, 'pgcache.purge.archive.daily' => true, 'pgcache.purge.archive.monthly' => true, 'pgcache.purge.archive.yearly' => true, 'pgcache.purge.feed.blog' => true, 'pgcache.purge.feed.comments' => true, 'pgcache.purge.feed.author' => true, 'pgcache.purge.feed.terms' => true, 'pgcache.cache.feed' => true, 'pgcache.debug' => false, 'pgcache.purge.postpages_limit' => 0,//purge all pages that list posts 'pgcache.purge.feed.types' => array( 0 => 'rdf', 1 => 'rss', 2 => 'rss2', 3 => 'atom' ), 'minify.debug' => false, 'dbcache.debug' => false, 'objectcache.debug' => false, 'mobile.enabled' => true, 'minify.auto' => false, 'minify.html.engine' => 'html', 'minify.html.inline.css' => true, 'minify.html.inline.js' => true, 'minify.js.engine' => 'js', 'minify.css.engine' => 'css', 'minify.js.header.embed_type' => 'nb-js', 'minify.js.body.embed_type' => 'nb-js', 'minify.js.footer.embed_type' => 'nb-js', 'minify.lifetime' => 14400, 'minify.file.gc' => 144000, 'dbcache.lifetime' => 3600, 'dbcache.file.gc' => 7200, 'objectcache.lifetime' => 3600, 'objectcache.file.gc' => 7200, 'browsercache.cssjs.last_modified' => true, 'browsercache.cssjs.compression' => true, 'browsercache.cssjs.expires' => true, 'browsercache.cssjs.lifetime' => 31536000, 'browsercache.cssjs.nocookies' => false, 'browsercache.cssjs.cache.control' => true, 'browsercache.cssjs.cache.policy' => 'cache_maxage', 'browsercache.cssjs.etag' => true, 'browsercache.cssjs.w3tc' => true, 'browsercache.cssjs.replace' => true, 'browsercache.html.compression' => true, 'browsercache.html.last_modified' => true, 'browsercache.html.expires' => true, 'browsercache.html.lifetime' => 30, 'browsercache.html.cache.control' => true, 'browsercache.html.cache.policy' => 'cache_maxage', 'browsercache.html.etag' => true, 'browsercache.html.w3tc' => true, 'browsercache.html.replace' => true, 'browsercache.other.last_modified' => true, 'browsercache.other.compression' => true, 'browsercache.other.expires' => true, 'browsercache.other.lifetime' => 31536000, 'browsercache.other.nocookies' => false, 'browsercache.other.cache.control' => true, 'browsercache.other.cache.policy' => 'cache_maxage', 'browsercache.other.etag' => true, 'browsercache.other.w3tc' => true, 'browsercache.other.replace' => true, 'config.check' => true, 'varnish.enabled' => false ); } public function enable_w3_total_cache() { $file = 'w3-total-cache/w3-total-cache.php'; $slug = 'w3-total-cache'; if (!class_exists('W3_ConfigWriter')) { $plugins = $this->get_plugins(); if (isset($plugins[$file])) { activate_plugin($file); } else { $this->install_plugin($slug); $this->activate_plugin($file); $this->hit_the_w3tc_page(); } } } public function get_plugins() { if (isset($this->plugin_list)) { return $this->plugin_list; } else { return get_plugins(); } } public function install_plugin($slug, $activate = false) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $api = plugins_api('plugin_information', array('slug' => $slug)); $found = false; $plugins = $this->get_plugins(); foreach ($plugins as $file => $plugin) { if ($plugin['Name'] == $api->name) { $found = true; } } if (!$found) { ob_start(); $upgrader = new Plugin_Upgrader(new A2_Plugin_Installer_Skin(compact('title', 'url', 'nonce', 'plugin', 'api'))); $upgrader->install($api->download_link); ob_end_clean(); $this->plugin_list = get_plugins(); } if ($activate) { $plugins = $this->get_plugins(); foreach ($plugins as $file => $plugin) { if ($plugin['Name'] == $api->name) { $this->activate_plugin($file); } } } $this->clear_w3_total_cache(); } public function activate_plugin($file) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; activate_plugin($file); $this->clear_w3_total_cache(); } public function clear_w3_total_cache() { if (is_plugin_active('w3-total-cache/w3-total-cache.php')) { //TODO: add clear cache } } public function hit_the_w3tc_page() { $cookie = ""; foreach ($_COOKIE as $name => $val) { $cookie .= "{$name}={$val};"; } rtrim($cookie, ';'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, get_admin_url() . 'admin.php?page=w3tc_general&nonce=' . wp_create_nonce('w3tc')); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_REFERER, get_admin_url()); $result = curl_exec($ch); curl_close($ch); } public function refresh_w3tc() { $this->hit_the_w3tc_page(); } public function get_w3tc_config(){ if(class_exists('W3_ConfigData')){ $config_writer = new W3_ConfigWriter(0,false); return W3_ConfigData::get_array_from_file($config_writer->get_config_filename()); } else{ return false; } } public function enable_w3tc_cache(){ $permalink_structure = get_option('permalink_structure'); $vars = array(); if($permalink_structure == ''){ $vars['pgcache.engine']='file'; } else{ $vars['pgcache.engine']='file_generic'; } $vars['dbcache.engine'] = 'file'; $vars['objectcache.engine'] = 'file'; $vars['objectcache.enabled'] = true; $vars['dbcache.enabled'] = true; $vars['pgcache.enabled'] = true; $vars['browsercache.enabled'] = true; $this->update_w3tc($vars); } public function enable_w3tc_page_cache(){ $permalink_structure = get_option('permalink_structure'); $vars = array(); if($permalink_structure == ''){ $vars['pgcache.engine']='file'; } else{ $vars['pgcache.engine']='file_generic'; } $vars['pgcache.enabled'] = true; $this->update_w3tc($vars); } public function enable_w3tc_db_cache(){ $permalink_structure = get_option('permalink_structure'); $vars = array(); $vars['dbcache.engine'] = 'file'; $vars['dbcache.enabled'] = true; $this->update_w3tc($vars); } public function enable_w3tc_object_cache(){ $permalink_structure = get_option('permalink_structure'); $vars = array(); $vars['objectcache.engine'] = 'file'; $vars['objectcache.enabled'] = true; $this->update_w3tc($vars); } public function enable_w3tc_browser_cache(){ $permalink_structure = get_option('permalink_structure'); $vars = array(); $vars['browsercache.enabled'] = true; $this->update_w3tc($vars); } public function update_w3tc($vars) { $vars = array_merge($this->get_w3tc_defaults(), $vars); if (!class_exists('W3_ConfigData')) { $this->enable_w3_total_cache(); } $config_writer = new W3_ConfigWriter(0, false); foreach ($vars as $name => $val) { $config_writer->set($name, $val); } $config_writer->set('common.instance_id', mt_rand()); $config_writer->save(); $this->refresh_w3tc(); } public function disable_w3tc_cache() { $this->update_w3tc(array( 'pgcache.enabled' => false, 'dbcache.enabled' => false, 'objectcache.enabled' => false, 'browsercache.enabled' => false, )); } public function disable_w3tc_page_cache(){ $vars = array(); $vars['pgcache.enabled'] = false; $this->update_w3tc($vars); } public function disable_w3tc_db_cache(){ $vars = array(); $vars['dbcache.enabled'] = false; $this->update_w3tc($vars); } public function disable_w3tc_object_cache(){ $vars = array(); $vars['objectcache.enabled'] = false; $this->update_w3tc($vars); } public function disable_w3tc_browser_cache(){ $vars = array(); $vars['browsercache.enabled'] = false; $this->update_w3tc($vars); } public function disable_html_minify() { $this->update_w3tc(array( 'minify.html.enable' => false, 'minify.html.enabled' => false, 'minify.auto' => false )); } public function enable_html_minify() { $this->update_w3tc(array( 'minify.html.enable' => true, 'minify.enabled' => true, 'minify.auto' => false, 'minify.engine' => 'file' )); } public function curl_save_w3tc($cookie, $url) { $post = "w3tc_save_options=Save all settings&_wpnonce=" . wp_create_nonce('w3tc') . "&_wp_http_referer=%2Fwp-admin%2Fadmin.php%3Fpage%3Dw3tc_general%26&w3tc_note%3Dconfig_save"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, get_admin_url() . $url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_REFERER, get_admin_url() . $url); //curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $head = curl_exec($ch); curl_close($ch); } public function get_optimizations() { return $this->optimizations; } /** * Creates HTML for the Administration page to set options for this plugin. * Override this method to create a customized page. * @return void */ public function settingsPage() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access A2 Optimized.', 'a2-optimized')); } $thisclass = $this; $optimization_count = 0; $this->get_plugin_status(); $thisdir = rtrim(__DIR__, "/"); wp_enqueue_style('bootstrap', plugins_url('/assets/bootstrap/css/bootstrap.css',__FILE__)); wp_enqueue_style('bootstrap-theme', plugins_url('/assets/bootstrap/css/bootstrap-theme.css',__FILE__)); wp_enqueue_script('bootstrap-theme', plugins_url('/assets/bootstrap/js/bootstrap.js',__FILE__), array('jquery')); $image_dir = plugins_url('/assets/images',__FILE__); do_action('a2_notices'); $ini_error_reporting = ini_get('error_reporting'); //ini_set('error_reporting',0); $this->optimization_status = ""; $optionMetaData = $this->getOptionMetaData(); $csrf_token = 0; /*$kbpage = $this->curl('https://www.a2hosting.com/kb'); if (preg_match('/name="csrf_token" value="([a-z0-9]{40})"/', $kbpage, $csrf_match)) { $csrf_token = $csrf_match[1]; }*/ $optimization_status = ""; foreach ($this->advanced_optimizations as $shortname => &$item) { $this->advanced_optimization_status .= $this->get_optimization_status($item); if ($item['configured']) { $this->advanced_optimization_count++; } } $this->optimization_count = 0; foreach ($this->optimizations as $shortname => &$item) { $this->optimization_status .= $this->get_optimization_status($item); if ($item['configured']) { $this->optimization_count++; } } if ($this->optimization_count == count($this->optimizations)) { $optimization_alert = '
Your site has been fully optimized!
'; } elseif (!$this->optimizations['page_cache']['configured']) { $optimization_alert = '
Your site is NOT optimized!
'; } elseif ($this->optimization_count > 5) { $optimization_alert = '
Your site has been partially optimized!
'; } elseif ($this->optimization_count > 2) { $optimization_alert = '
Your site is barely optimized!
'; } else { $optimization_alert = '
Your site is NOT optimized!
'; } $optimization_number = count($this->optimizations); $optimization_circle = ""; if ($optimization_number > 0) { $optimization_circle = <<{$this->optimization_count}/{$optimization_number} HTML; } $kb_search_box = << HTML; list($warnings, $num_warnings) = $this->warnings(); $advanced_circle = ""; $warning_circle = ""; if ($num_warnings > 0) { $warning_circle = <<{$num_warnings} HTML; } $settingsGroup = get_class($this) . '-settings-group'; $description = $this->get_plugin_description(); if($this->is_a2()) { $feedback = << We want to hear from you! Please share your thoughts and feedback in our Suggestion Box! HTML; } else { $feedback = << We want to hear from you! Please share your thoughts and feedback in our wordpress.org support forum! HTML; } echo <<
{$kb_search_box}
{$optimization_alert}

Optimization Status

{$this->optimization_status}

Warnings

{$warnings}

Advanced Optimizations

{$this->advanced_optimization_status}

About A2 Optimized

A2 Optimized was developed by A2 Hosting to make it faster and easier to configure the caching of all aspects of a WordPress site.

This free plugin comes with many of the popular Optimizations that come with WordPress hosted at A2 Hosting.

To get the full advantage of A2 Optimized, host your site at A2 Hosting

Free Optimizations

Page Caching with W3 Total Cache
  • Page Caching stores full copies of pages on the disk so that PHP code and database queries can be skipped by the web server.
DB Caching with W3 Total Cache
  • Database cache stores copies of common database queries on disk or in memory to speed up page rendering.
Object Caching with W3 Total Cache
  • Object Caching stores commonly used elements such as menus, widgets and forms on disk or in memory to speed up page rendering.
Browser Caching with W3 Total Cache
  • Add Rules to the web server to tell the visitor's browser to store a copy of static files to reduce the load time for pages requested after the first page is loaded.
Minify HTML Pages
  • Auto Configure W3 Total Cache to remove excess white space and comments from HTML pages to compress their size.
  • Smaller html pages download faster.
Minify CSS Files
  • Auto Configure W3 Total Cache to condense CSS files.
  • Combines multiple css files into a single download.
  • Can provide significant speed imporvements for page loads.
Minify JS Files
  • Auto Configure W3 Total Cache to condense JavaScript files into non human-readable compressed files.
  • Combines multiple js files into a single download.
  • Can provide significant speed improvements for page loads.
Gzip Compression Enabled
  • Turns on gzip compression using W3 Total Cache.
  • Ensures that files are compressed before sending them to the visitor's browser.
  • Can provide significant speed improvements for page loads.
  • Reduces bandwidth required to serve web pages.
Deny Direct Access to Configuration Files and Comment Form
  • Enables WordPress hardening rules in .htaccess to prevent browser access to certain files.
  • Prevents bots from submitting to comment forms.
  • Turn this off if you use systems that post to the comment form without visiting the page.
Lock Editing of Plugins and Themes from the WP Admin
  • Turns off the file editor in the wp-admin.
  • Prevents plugins and themes from being tampered with from the wp-admin.

A2 Hosting Exclusive Optimizations

These one-click optimizations are only available while hosted at A2 Hosting.

Login URL Change
  • Move the login page from the default wp-login.php to a random URL.
  • Prevents bots from automatically brute-force attacking wp-login.php
reCAPTCHA on comments and login
  • Provides google reCAPTCHA on both the Login form and comments.
  • Prevents bots from automatically brute-force attacking wp-login.php
  • Prevents bots from automatically spamming comments.
Compress Images on Upload
  • Enables and configures EWWW Image Optimizer.
  • Compresses images that are uploaded to save bandwidth.
  • Improves page load times: especially on sites with many images.
Turbo Web Hosting
  • Take advantage of A2 Hosting's Turbo Web Hosting platform.
  • Faster serving of static files.
  • Pre-compiled .htaccess files on the web server for imporved performance.
  • PHP OpCode cache enabled by default
  • Custom PHP engine that is faster than Fast-CGI and FPM
Memcached Database and Object Cache
  • Database and Object cache in memory instead of on disk.
  • More secure and faster Memcached using Unix socket files.
  • Significant improvement in page load times, especially on pages that can not use full page cache such as wp-admin
$feedback
HTML; ini_set('error_reporting', $ini_error_reporting); } public function get_plugin_status() { $thisclass = $this; $opts = new A2_Optimized_Optimizations($thisclass); $this->advanced_optimizations = $opts->get_advanced(); $this->optimizations = $opts->get_optimizations(); $this->plugin_list = get_plugins(); if (isset($_GET['activate'])) { foreach ($this->plugin_list as $file => $plugin) { if ($_GET['activate'] == $plugin['Name']) { $this->activate_plugin($file); } } } if (isset($_GET['hide_login_url'])) { $this->addOption('hide_login_url', true); } if (isset($_GET['deactivate'])) { foreach ($this->plugin_list as $file => $plugin) { if ($_GET['deactivate'] == $plugin['Name']) { $this->deactivate_plugin($file); } } } if (isset($_GET['delete'])) { foreach ($this->plugin_list as $file => $plugin) { if ($_GET['delete'] == $plugin['Name']) { $this->uninstall_plugin($file); } } } if (isset($_GET['disable_optimization'])) { $hash = ""; if (isset($this->optimizations[$_GET['disable_optimization']])) { $this->optimizations[$_GET['disable_optimization']]['disable']($_GET['disable_optimization']); } if (isset($this->advanced_optimizations[$_GET['disable_optimization']])) { $this->advanced_optimizations[$_GET['disable_optimization']]['disable']($_GET['disable_optimization']); $hash = "#optimization-advanced-tab"; } echo << window.location = 'admin.php?page=A2_Optimized_Plugin_admin{$hash}'; JAVASCRIPT; exit(); } if (isset($_GET['enable_optimization'])) { $hash = ""; if (isset($this->optimizations[$_GET['enable_optimization']])) { $this->optimizations[$_GET['enable_optimization']]['enable']($_GET['enable_optimization']); } if (isset($this->advanced_optimizations[$_GET['enable_optimization']])) { $this->advanced_optimizations[$_GET['enable_optimization']]['enable']($_GET['enable_optimization']); $hash = "#optimization-advanced-tab"; } echo << window.location = 'admin.php?page=A2_Optimized_Plugin_admin{$hash}'; JAVASCRIPT; exit(); } ini_set('disable_functions', ''); require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $plugins_url = plugins_url(); $plugins_url = explode('/', $plugins_url); array_shift($plugins_url); array_shift($plugins_url); array_shift($plugins_url); $this->plugin_dir = ABSPATH . implode('/', $plugins_url); $this->plugins_url = plugins_url(); validate_active_plugins(); $this->set_install_status('plugins', $this->plugin_list); } /** * A wrapper function delegating to WP add_option() but it prefixes the input $optionName * to enforce "scoping" the options in the WP options table thereby avoiding name conflicts * @param $optionName string defined in settings.php and set as keys of $this->optionMetaData * @param $value mixed the new value * @return null from delegated call to delete_option() */ public function addOption($optionName, $value) { $prefixedOptionName = $this->prefix($optionName); // how it is stored in DB return add_option($prefixedOptionName, $value); } /** * Get the prefixed version input $name suitable for storing in WP options * Idempotent: if $optionName is already prefixed, it is not prefixed again, it is returned without change * @param $name string option name to prefix. Defined in settings.php and set as keys of $this->optionMetaData * @return string */ public function prefix($name) { $optionNamePrefix = $this->getOptionNamePrefix(); if (strpos($name, $optionNamePrefix) === 0) { // 0 but not false return $name; // already prefixed } return $optionNamePrefix . $name; } public function getOptionNamePrefix() { return get_class($this) . '_'; } public function deactivate_plugin($file) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; if (is_plugin_active($file)) { deactivate_plugins($file); $this->clear_w3_total_cache(); } } public function uninstall_plugin($file, $delete = true) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; $this->deactivate_plugin($file); uninstall_plugin($file); if ($delete) { delete_plugins(array($file)); } unset($this->plugin_list[$file]); $this->clear_w3_total_cache(); } public function set_install_status($name, $value) { if (!isset($this->install_status)) { $this->install_status = new StdClass; } $this->install_status->{$name} = $value; } /** * Define your options meta data here as an array, where each element in the array * @return array of key=>display-name and/or key=>array(display-name, choice1, choice2, ...) * key: an option name for the key (this name will be given a prefix when stored in * the database to ensure it does not conflict with other plugin options) * value: can be one of two things: * (1) string display name for displaying the name of the option to the user on a web page * (2) array where the first element is a display name (as above) and the rest of * the elements are choices of values that the user can select * e.g. * array( * 'item' => 'Item:', // key => display-name * 'rating' => array( // key => array ( display-name, choice1, choice2, ...) * 'CanDoOperationX' => array('Can do Operation X', 'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber'), * 'Rating:', 'Excellent', 'Good', 'Fair', 'Poor') */ public function getOptionMetaData() { return array(); } private function curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); curl_close($ch); return $content; } /*public function get_litespeed(){ return get_option('a2_optimized_litespeed'); }*/ /*public function set_litespeed($litespeed = true){ update_option('a2_optimized_litespeed',$litespeed); }*/ function get_optimization_status(&$item) { if ($item != null) { $settings_slug = $this->getSettingsSlug(); if (isset($item['is_configured'])) { $item['is_configured']($item); } $active_color = 'danger'; $active_text = 'Not Activated'; $glyph = 'exclamation-sign'; $links = array(); if ($item['configured']) { $active_color = 'success'; $active_text = 'Configured'; $glyph = 'ok'; if (isset($item['disable'])) { $links[] = array("?page=$settings_slug&disable_optimization={$item['slug']}", "Disable", "_self"); } if (isset($item['settings'])) { $links[] = array("{$item['settings']}", "Configure", "_self"); } if (isset($item['configured_links'])) { foreach ($item['configured_links'] as $name => $link) { if (gettype($link) == 'array') { $links[] = array($link[0], $name, $link[1]); } else { $links[] = array($link, $name, "_self"); } } } } elseif (isset($item['partially_configured']) && $item['partially_configured']) { $active_color = 'warning'; $active_text = "Partially Configured. {$item['partially_configured_message']}"; $glyph = 'warning-sign'; if (isset($item['disable'])) { $links[] = array("?page=$settings_slug&disable_optimization={$item['slug']}", "Disable", "_self"); } if (isset($item['settings'])) { $links[] = array("{$item['settings']}", "Configure", "_self"); } if (isset($item['partially_configured_links'])) { foreach ($item['partially_configured_links'] as $name => $link) { if (gettype($link) == 'array') { $links[] = array($link[0], $name, $link[1]); } else { $links[] = array($link, $name, "_self"); } } } } else { if (isset($item['enable'])) { $links[] = array("?page=$settings_slug&enable_optimization={$item['slug']}", "Enable", "_self"); } if (isset($item['not_configured_links'])) { foreach ($item['not_configured_links'] as $name => $link) { if (gettype($link) == 'array') { $links[] = array($link[0], $name, $link[1]); } else { $links[] = array($link, $name, "_self"); } } } } if (isset($item['kb'])) { $links[] = array($item['kb'], "Learn More", "_blank"); } $link_html = ''; foreach ($links as $i => $link) { if (isset($link[0]) && isset($link[1]) && isset($link[2])) { $link_html .= <<{$link[1]} | HTML; } } $premium = ""; if (isset($item['premium'])) { $premium = '
'; } $link_html = rtrim($link_html, "|"); return <<
{$item['name']}
{$active_text}
{$premium}

{$item['description']}

{$link_html}
HTML; } return true; } private function warnings() { $num_warnings = 0; $opts = new A2_Optimized_Optimizations($this); $warnings = $opts->get_warnings(); $warning_html = ""; foreach ($warnings as $type => $warning_set) { switch ($type) { case 'Bad WP Options': foreach ($warning_set as $option_name => $warning) { $warn = false; $value = get_option($option_name); switch ($warning['type']) { case 'numeric': switch ($warning['threshold_type']) { case '>': if ($value > $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; case '<': if ($value < $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; case '=': if ($value == $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; } break; case 'text': switch ($warning['threshold_type']) { case '=': if ($value == $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; case '!=': if ($value != $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; } break; case 'array_count': switch ($warning['threshold_type']) { case '>': if (is_array($value) && count($value) > $warning['threshold']) { $warning_html .= $this->warning_display($warning); $num_warnings++; } break; } break; } } break; case 'Advanced Warnings': foreach ($warning_set as $name => $warning) { if ($warning['is_warning']()) { $warning_html .= $this->warning_display($warning); $num_warnings++; } } break; case 'Bad Plugins': foreach ($warning_set as $plugin_folder => $warning) { $warn = false; } } } $warn = false; $plugins = $this->get_plugins(); foreach ($plugins as $file => $plugin) { if (!is_plugin_active($file)) { $plugin['file'] = $file; $warning_html .= $this->plugin_not_active_warning($plugin); $num_warnings++; } } return array($warning_html, $num_warnings); } private function warning_display($warning) { return <<
{$warning['title']}

{$warning['description']}

Configure
HTML; } /* public function plugin_list(){ //Name,PluginURI,Version,Description,Author,AuthorURI,TextDomain,DomainPath,Network,Title,AuthorName $string = ""; include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); $plugins = $this->get_plugins(); foreach($plugins as $filename=>$plugin){ $name = $plugin['Name']; $title = $plugin['Title']; $checked = ""; if(is_plugin_active($filename)){ $checked = "checked='checked'"; } ob_start(); $dump = ob_get_contents(); ob_end_clean(); $string .=<< $title delete {$dump} HTML; } return $string; }*/ private function plugin_not_active_warning($plugin) { $manage = "plugins.php?plugin_status=inactive"; return <<
Inactive Plugin: {$plugin['Name']}

Deactivated plugins should be deleted. Deactivating a plugin does not remove the plugin and its files from your website. Plugins with security flaws may still affect your site even when not active.

{$plugin['Description']}

Manage deactivated plugins
HTML; } public function get_advanced_optimizations() { return $this->advanced_optimizations; } public function set_lockdown($lockdown = true) { update_option('a2_optimized_lockdown', $lockdown); } public function set_nomods($lockdown = true) { update_option('a2_optimized_nomods', $lockdown); } public function set_deny_direct($deny = true) { update_option('a2_optimized_deny_direct', $deny); } public function write_wp_config() { $lockdown = $this->get_lockdown(); $nomods = $this->get_nomods(); touch(ABSPATH . 'wp-config.php'); copy(ABSPATH . 'wp-config.php', ABSPATH . 'wp-config.php.bak.a2'); $a2_config = ""; if ($lockdown) { $a2_config = <<get_deny_direct()) { //Append the new rules to .htaccess //get the path to the WordPress install - nvm //$rewrite_base = "/".trim(explode('/',str_replace(array('https://','http://'),'',site_url()),2)[1],"/")."/"; $a2hardening = << Order deny,allow Deny from all RewriteBase {$home_path} RewriteRule ^wp-admin/includes/ - [F,L] RewriteRule !^wp-includes/ - [S=3] RewriteRule ^wp-includes/[^/]+\.php$ - [F,L] RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L] RewriteRule ^wp-includes/theme-compat/ - [F,L] RewriteRule ^wp-config\.php - [F,L] RewriteRule ^php\.ini - [F,L] RewriteRule \.htaccess - [F,L] RewriteCond %{REQUEST_METHOD} POST RewriteCond %{REQUEST_URI} .wp-comments-post.php* RewriteCond %{HTTP_REFERER} !.*{$_SERVER['HTTP_HOST']}.* [OR] RewriteCond %{HTTP_USER_AGENT} ^$ RewriteRule (.*) - [F,L] # END WordPress Hardening APACHE; } $litespeed = ""; $htaccess = file_get_contents(ABSPATH . '.htaccess'); $pattern = "/[\r\n]*# BEGIN WordPress Hardening.*# END WordPress Hardening[\r\n]*/msiU"; $htaccess = preg_replace($pattern, '', $htaccess); $htaccess = <<optionMetaData * @return bool from delegated call to delete_option() */ public function deleteOption($optionName) { $prefixedOptionName = $this->prefix($optionName); // how it is stored in DB return delete_option($prefixedOptionName); } /** * A wrapper function delegating to WP add_option() but it prefixes the input $optionName * to enforce "scoping" the options in the WP options table thereby avoiding name conflicts * @param $optionName string defined in settings.php and set as keys of $this->optionMetaData * @param $value mixed the new value * @return null from delegated call to delete_option() */ public function updateOption($optionName, $value) { $prefixedOptionName = $this->prefix($optionName); // how it is stored in DB return update_option($prefixedOptionName, $value); } /** * Checks if a particular user has a role. * Returns true if a match was found. * * @param string $role Role name. * @param int $user_id (Optional) The ID of a user. Defaults to the current user. * @return bool */ function checkUserRole($role, $user_id = null) { if (is_numeric($user_id)) { $user = get_userdata($user_id); } else { $user = wp_get_current_user(); } return empty($user) ? false : in_array($role, (array)$user->roles); } /** * A wrapper function delegating to WP get_option() but it prefixes the input $optionName * to enforce "scoping" the options in the WP options table thereby avoiding name conflicts * @param $optionName string defined in settings.php and set as keys of $this->optionMetaData * @param $default string default value to return if the option is not set * @return string the value from delegated call to get_option(), or optional default value * if option is not set. */ public function getOption($optionName, $default = null) { $prefixedOptionName = $this->prefix($optionName); // how it is stored in DB $retVal = get_option($prefixedOptionName); if (!$retVal && $default) { $retVal = $default; } return $retVal; } /** * @param $roleName string a standard WP role name like 'Administrator' * @return bool */ public function isUserRoleEqualOrBetterThan($roleName) { if ('Anyone' == $roleName) { return true; } $capability = $this->roleToCapability($roleName); return $this->checkUserCapability($capability); } /** * Given a WP role name, return a WP capability which only that role and roles above it have * http://codex.wordpress.org/Roles_and_Capabilities * @param $roleName * @return string a WP capability or '' if unknown input role */ protected function roleToCapability($roleName) { switch ($roleName) { case 'Super Admin': return 'manage_options'; case 'Administrator': return 'manage_options'; case 'Editor': return 'publish_pages'; case 'Author': return 'publish_posts'; case 'Contributor': return 'edit_posts'; case 'Subscriber': return 'read'; case 'Anyone': return 'read'; } return ''; } /** * Checks if a particular user has a given capability without calling current_user_can. * Returns true if a match was found. * * @param string $capability Capability name. * @param int $user_id (Optional) The ID of a user. Defaults to the current user. * @return bool */ function checkUserCapability($capability, $user_id = null) { if (!is_numeric($user_id)) { $user_id = wp_get_current_user(); } if (is_numeric($user_id)) { $user = get_userdata($user_id); } else { return false; } $capabilities = (array)$user->allcaps; return empty($user) ? false : isset($capabilities["{$capability}"]) ? $capabilities["{$capability}"] : false; } private function plugin_display($plugin) { $links['Delete'] = admin_url() . "admin.php?page=" . $this->getSettingsSlug() . "&delete={$plugin['Name']}"; $glyph = 'warning-sign'; if (!$plugin['active']) { $glyph = 'exclamation-sign'; $links['Activate'] = admin_url() . "admin.php?page=" . $this->getSettingsSlug() . "&activate={$plugin['Name']}"; } else { $glyph = 'ok'; $links['Deactivate'] = admin_url() . "admin.php?page=" . $this->getSettingsSlug() . "&deactivate={$plugin['Name']}"; if (isset($plugin['config_url'])) { $links['Configure'] = $plugin['config_url']; } } $link_html = ""; foreach ($links as $name => $href) { $link_html .= <<$name | HTML; } $link_html = trim($link_html, " |"); return <<
{$plugin['Name']}

{$plugin['Description']}

{$link_html}
HTML; } protected function is_a2(){ if( is_dir("/opt/a2-optimized") ){ return true; } return false; } function get_plugin_description() { $description = <<