initOptions(); // Initialize DB Tables used by the plugin $this->installDatabaseTables(); // Other Plugin initialization - for the plugin writer to override as needed //$this->otherInstall(); // Record the installed version $this->saveInstalledVersion(); // To avoid running install() more then once $this->markAsInstalled(); } protected function initOptions() { $options = $this->getOptionMetaData(); if (!empty($options)) { foreach ($options as $key => $arr) { if (is_array($arr) && count($arr) > 1) { $this->addOption($key, $arr[1]); } } } } public function getOptionMetaData() { // http://plugin.michael-simpson.com/?page_id=31 return array( //'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades. 'recaptcha' => array('reCaptcha'), //'ATextInput' => array(__('Enter in some text', 'my-awesome-plugin')), //'CanSeeSubmitData' => array(__('Can See Submission data', 'my-awesome-plugin'), // 'Administrator', 'Editor', 'Author', 'Contributor', 'Subscriber', 'Anyone') ); } protected function installDatabaseTables() { // global $wpdb; // $tableName = $this->prefixTableName('mytable'); // $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` ( // `id` INTEGER NOT NULL"); } protected function saveInstalledVersion() { $this->setVersionSaved($this->getVersion()); } protected function setVersionSaved($version) { return $this->updateOption(self::optionVersion, $version); } public function getVersion() { return $this->getPluginHeaderValue('Version'); } public function getPluginHeaderValue($key) { // Read the string from the comment header of the main plugin file $data = file_get_contents($this->getPluginDir() . DIRECTORY_SEPARATOR . $this->getMainPluginFileName()); $match = array(); preg_match('/' . $key . ':\s*(\S+)/', $data, $match); if (count($match) >= 1) { return $match[1]; } return null; } protected function getPluginDir() { return dirname(__FILE__); } protected function getMainPluginFileName() { return 'a2-optimized.php'; } protected function markAsInstalled() { return $this->updateOption(self::optionInstalled, true); } public function uninstall() { $this->markAsUnInstalled(); } protected function markAsUnInstalled() { return $this->deleteOption(self::optionInstalled); } public function activate(){ touch(ABSPATH.'403.shtml'); $this->write_htaccess(); } public function deactivate(){ //remove lines from .htaccess $htaccess = file_get_contents(ABSPATH.'.htaccess'); $pattern = "/[\r\n]*# BEGIN WordPress Hardening.*# END WordPress Hardening[\r\n]*/msiU"; $htaccess = preg_replace($pattern,'',$htaccess); //Write the rules to .htaccess $fp = fopen(ABSPATH.'.htaccess', "c"); if (flock($fp, LOCK_EX)) { ftruncate($fp, 0); // truncate file fwrite($fp, $htaccess); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // release the lock } else{ //no file lock :( } } public function upgrade() { } function update_notice() { global $code_version, $saved_version; echo<<
HTML;
_e( "A2 Optimized has been Updated from {$saved_version} to {$code_version} !", 'a2-text-domain' );
echo<<
HTML;
}
public function login_captcha(){
if (file_exists("/opt/a2-optimized/wordpress/recaptchalib.php")) {
include_once("/opt/a2-optimized/wordpress/recaptchalib.php");
$a2_recaptcha = $this->getOption('recaptcha');
if ($a2_recaptcha == 1) {
$key = $this->get_public_key();
if (!is_null($key)) {
$captcha = a2recaptcha_get_html($key, null, true);
echo <<
#recaptcha_area, #recaptcha_table{
margin-left: -12px !important;
}
{$captcha}
HTML;
}
}
}
}
protected function get_public_key()
{
if ($key = get_option("a2_recaptcha_pubkey", false)) {
return $key;
}
if (file_exists("/opt/a2-optimized/wordpress/pubkey")) {
return file_get_contents("/opt/a2-optimized/wordpress/pubkey");
}
return null;
}
public function comment_captcha(){
if(!$this->checkUserCapability('moderate_comments', get_current_user_id() )){
if (file_exists("/opt/a2-optimized/wordpress/recaptchalib.php")) {
include_once("/opt/a2-optimized/wordpress/recaptchalib.php");
$a2_recaptcha = $this->getOption('recaptcha');
if ($a2_recaptcha == 1) {
$key = $this->get_public_key();
if (!is_null($key)) {
$captcha = a2recaptcha_get_html($key, null, true);
echo <<
#recaptcha_area{
margin: 10px auto !important;
}
{$captcha}
HTML;
}
}
}
}
}
public function captcha_authenticate($user, $username, $password)
{
if ($username != '' && !(defined('XMLRPC_REQUEST') && XMLRPC_REQUEST)) {
$a2_recaptcha = $this->getOption('recaptcha');
if ($a2_recaptcha == 1) {
if (file_exists("/opt/a2-optimized/wordpress/recaptchalib.php")) {
include_once("/opt/a2-optimized/wordpress/recaptchalib.php");
$privatekey = $this->get_private_key();
if (!is_null($privatekey)) {
$resp = a2recaptcha_check_answer($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!empty($username)) {
if (!$resp->is_valid) {
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
//wp_die("The reCAPTCHA wasn't entered correctly. Go back and try it again.: (reCAPTCHA said: {$resp->error})");
return new WP_Error('recaptcha_error', "The reCAPTCHA wasn't entered correctly. Please try it again.");
}
}
}
}
}
}
}
protected function get_private_key()
{
if (file_exists("/opt/a2-optimized/wordpress/privkey")) {
return file_get_contents("/opt/a2-optimized/wordpress/privkey");
}
if ($key = get_option("a2_recaptcha_privkey", false)) {
return $key;
}
return null;
}
public function captcha_comment_authenticate($commentdata)
{
if (!$this->checkUserCapability('moderate_comments', get_current_user_id()) && !(defined('XMLRPC_REQUEST') && XMLRPC_REQUEST)) {
if (file_exists("/opt/a2-optimized/wordpress/recaptchalib.php")) {
include_once("/opt/a2-optimized/wordpress/recaptchalib.php");
$a2_recaptcha = $this->getOption('recaptcha');
if ($a2_recaptcha == 1) {
$privatekey = $this->get_private_key();
if (!is_null($privatekey)) {
$resp = a2recaptcha_check_answer($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!empty($commentdata)) {
if (!$resp->is_valid) {
wp_die("The reCAPTCHA wasn't entered correctly. Please use your browsers back button and try again.");
}
} else {
wp_die("There was an error. Please try again.");
}
}
}
}
}
return $commentdata;
}
public function permalink_changed()
{
$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=A2_Optimized_Plugin_admin');
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 addActionsAndFilters()
{
add_action('permalink_structure_changed', array(&$this, 'permalink_changed'));
$date = date("Y-m-d");
if (strpos($_SERVER['REQUEST_URI'], "login-{$date}") > 0) {
add_action('template_redirect', array(&$this, 'get_moved_login'));
}
add_filter( 'allow_minor_auto_core_updates', '__return_true' );
add_filter('auto_update_translation', '__return_true');
/*add_filter( 'allow_major_auto_core_updates', '__return_true' );
add_filter( 'allow_minor_auto_core_updates', '__return_true' );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
add_filter( 'auto_update_translation', '__return_true' );
*/
if (is_admin()) {
add_filter('admin_init', array(&$this, 'admin_init'));
add_action('admin_bar_menu', array(&$this, 'addAdminBar'), 8374);
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
if (defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT) {
add_action('admin_menu', array(&$this, 'addLockedEditor'), 100, 100);
}
add_action('admin_print_styles', array(&$this, 'myStyleSheet'));
add_action('wp_dashboard_setup', array(&$this, 'dashboard_widget'));
$a2_plugin_basename = plugin_basename($GLOBALS['A2_Plugin_Dir'] . '/a2-optimized.php');
add_filter("plugin_action_links_{$a2_plugin_basename}", array(&$this, 'plugin_settings_link'));
}
if (get_option('A2_Optimized_Plugin_recaptcha', 0) == 1 && !is_admin()) {
add_action('woocommerce_login_form', array(&$this, 'login_captcha'));
add_action('login_form', array(&$this, 'login_captcha'));
add_filter('authenticate', array(&$this, 'captcha_authenticate'), 1, 3);
add_action('comment_form_after_fields', array(&$this, 'comment_captcha'));
add_filter('preprocess_comment', array(&$this, 'captcha_comment_authenticate'), 1, 3);
}
}
public function plugin_settings_link($links)
{
$settings_link = 'Settings';
array_unshift($links, $settings_link);
return $links;
}
public function get_moved_login()
{
wp_redirect(wp_login_url(), 302);
exit();
}
public function myStyleSheet()
{
wp_enqueue_style('a2-optimized-css', plugins_url('/assets/css/style.css',__FILE__));
}
/**
* Add a widget to the dashboard.
*
* This function is hooked into the 'wp_dashboard_setup' action below.
*/
public function dashboard_widget()
{
$logo_url = plugins_url() . "/a2-optimized-wp/assets/images/a2optimized.png";
wp_add_dashboard_widget(
'a2_optimized', // Widget slug.
"", // Title.
array(&$this, 'a2_dashboard_widget') // Display function.
);
wp_add_dashboard_widget(
'a2_optimized_kb', // Widget slug.
"Have any questions? Search the A2 Hosting Knowledge Base for answers.", // Title.
array(&$this, 'kb_dashboard_widget') // Display function.
);
//force the widget to the top of the dashboard
global $wp_meta_boxes;
// Get the regular dashboard widgets array
// (which has our new widget already but at the end)
unset($wp_meta_boxes['dashboard']['normal']['core']['wp_welcome_widget']);
$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
// Backup and delete our new dashboard widget from the end of the array
$example_widget_backup = array('a2_optimized' => $normal_dashboard['a2_optimized'], 'a2_optimized_kb' => $normal_dashboard['a2_optimized_kb']);
// Merge the two arrays together so our widget is at the beginning
$sorted_dashboard = array_merge($example_widget_backup, $normal_dashboard);
// Save the sorted array back into the original metaboxes
$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
}
/**
* Create the function to output the contents of our Dashboard Widget.
*/
public function a2_dashboard_widget()
{
// Display whatever it is you want to show.
echo <<
A2 Optimized will automatically configure your WordPress site for speed and security.
A2 Optimized includes these features.
To learn more about the A2 Optimized WordPress plugin: read this Knowledge Base article
HTML; } public function kb_dashboard_widget() { echo << Search the Knowledge Base HTML; } public function locked_files_notice() { echo <<Editing of plugin and theme files in the wp-admin is disabled by A2 Optimized
This is recommended for security reasons. You can modify this setting on the A2 Optimized Configuration page
A ReCaptacha plugin is installed.
Disable and delete any plugins using reCaptcha to use the reCaptcha functionality built into A2 Optimized.
Editing of plugin and theme files in the wp-admin is enabled
This is not recommended for security reasons. You can modify this setting on the A2 Optimized Configuration page
Your login page is now here: {$link}. Bookmark this page!
HTML; } } public function admin_init() { if (!$this->checkUserCapability('manage_options', get_current_user_id())) { return false; } $active_plugins = get_option('active_plugins'); if (in_array('rename-wp-login/rename-wp-login.php', $active_plugins)) { if ($rwl_page = get_option('rwl_page')) { if ($rwl_page != '') { add_action('admin_notices', array(&$this, 'rwl_notice')); if ($a2_login_page = get_option('a2_login_page')) { if ($a2_login_page != $rwl_page) { update_option('a2_login_page', $rwl_page); } } else { update_option('a2_login_page', $rwl_page); } } } } if (isset($_GET['page']) && in_array($_GET['page'], $this->config_pages)) { add_action('admin_notices', array(&$this, 'config_page_notice')); } if (isset($_GET['action']) && $_GET['action'] == 'install-plugin') { if (isset($_GET['plugin']) && in_array($_GET['plugin'], $this->incompatible_plugins)) { add_action('admin_notices', array(&$this, 'incompatible_plugin_notice')); } } //we don't need this function anymore since the new reCaptcha is now compatible with other recaptcha plugins //if(function_exists('recaptcha_get_html')){ //add_action( 'admin_notices', array(&$this,'recaptcha_installed_notice')); //} if (!(strpos($_SERVER['SCRIPT_FILENAME'], 'plugins.php') === false) && defined('DISALLOW_FILE_EDIT') && DISALLOW_FILE_EDIT) { add_action('admin_notices', array(&$this, 'locked_files_notice')); } else if (!(strpos($_SERVER['SCRIPT_FILENAME'], 'plugins.php') === false)) { add_action('admin_notices', array(&$this, 'not_locked_files_notice')); } } /** * Puts the configuration page in the Plugins menu by default. * Override to put it elsewhere or create a set of submenus * Override with an empty implementation if you don't want a configuration page * @return void */ public function addSettingsSubMenuPage() { $this->addSettingsSubMenuPageToMenu(); } protected function addSettingsSubMenuPageToMenu() { $this->requireExtraPluginFiles(); $displayName = $this->getPluginDisplayName(); add_menu_page($displayName, $displayName, 'manage_options', $this->getSettingsSlug(), array(&$this, 'settingsPage'), null, 3.14159265359); } protected function requireExtraPluginFiles() { //require_once(ABSPATH . 'wp-includes/pluggable.php'); require_once(ABSPATH . 'wp-admin/includes/plugin.php'); } public function getPluginDisplayName() { return 'A2 Optimized'; } protected function getSettingsSlug() { return get_class($this) . '_admin'; } public function addAdminBar() { $this->requireExtraPluginFiles(); global $wp_admin_bar; if (current_user_can('manage_options')) { $wp_admin_bar->add_node(array( 'id' => 'a2-optimized-admin-bar', 'title' => 'A2 Optimized', 'href' => admin_url('admin.php?page=' . $this->getSettingsSlug()) )); } } public function addLockedEditor() { $this->requireExtraPluginFiles(); add_theme_page('Editor Locked', 'Editor Locked', 'manage_options', 'editor-locked', array(&$this, 'settingsPage')); } /** * @return bool indicating if the plugin is installed already */ public function isInstalled() { return $this->getOption(self::optionInstalled) == true; } protected function get_recaptcha_public_key() { if (file_exists("/opt/a2-optimized/wordpress_encoded/pk.php")) { return file_get_contents("/opt/a2-optimized/wordpress_encoded/pk.php"); } if ($key = get_option("a2_recaptcha_pubkey")) { return $key; } return null; } protected function addSettingsSubMenuPageToPluginsMenu() { $this->requireExtraPluginFiles(); $displayName = $this->getPluginDisplayName(); add_submenu_page('plugins.php', $displayName, $displayName, 'manage_options', $this->getSettingsSlug(), array(&$this, 'settingsPage')); } protected function addSettingsSubMenuPageToDashboard() { $this->requireExtraPluginFiles(); $displayName = $this->getPluginDisplayName(); add_dashboard_page($displayName, $displayName, 'manage_options', $this->getSettingsSlug(), array(&$this, 'settingsPage')); } protected function addSettingsSubMenuPageToSettingsMenu() { $this->requireExtraPluginFiles(); $displayName = $this->getPluginDisplayName(); add_options_page($displayName, $displayName, 'manage_options', $this->getSettingsSlug(), array(&$this, 'settingsPage')); } public function incompatible_plugin_notice() { echo <<Proceed with caution: The Plugin you just installed may be incompatible with A2 Optimized.
HTML; } public function config_page_notice() { echo <<This site has been configured using the A2 Optimized plugin. We, at A2 Hosting, have spent quite a bit of time figuring out the best set of options for this plugin; however, if you think you need to customize configuration: by all means... Continue. If you have arrived here by mistake, you may use the A2 Optimized administration page to configure this plugin.
HTML; } }