/** * admin init hook. Setup settings according. */ function admin_init() { $has_api_key = WP_reCaptcha::instance()->has_api_key(); if (!$has_api_key && current_user_can('manage_options')) { add_action('admin_notices', array(&$this, 'api_key_notice')); } $this->enter_api_key = !$has_api_key || isset($_REQUEST['recaptcha-action']) && $_REQUEST['recaptcha-action'] == 'recaptcha-set-api-key'; if (!$this->enter_api_key) { add_settings_section('recaptcha_protection', __('Protect', 'wp-recaptcha-integration'), array(&$this, 'explain_protection'), 'recaptcha'); add_settings_section('recaptcha_styling', __('Style', 'wp-recaptcha-integration'), array(&$this, 'explain_styling'), 'recaptcha'); } add_settings_section('recaptcha_apikey', __('Connect', 'wp-recaptcha-integration'), array(&$this, 'explain_apikey'), 'recaptcha'); if (!$this->enter_api_key) { add_settings_section('recaptcha_advanced', __('Advanced', 'wp-recaptcha-integration'), array(&$this, 'explain_advanced'), 'recaptcha'); } if ($this->enter_api_key) { // no API Key. Let the user enter it. register_setting('recaptcha_options', 'recaptcha_publickey', 'trim'); register_setting('recaptcha_options', 'recaptcha_privatekey', 'trim'); add_settings_field('recaptcha_publickey', __('Site key', 'wp-recaptcha-integration'), array(&$this, 'secret_input_text'), 'recaptcha', 'recaptcha_apikey', array('name' => 'recaptcha_publickey')); add_settings_field('recaptcha_privatekey', __('Secret key', 'wp-recaptcha-integration'), array(&$this, 'secret_input_text'), 'recaptcha', 'recaptcha_apikey', array('name' => 'recaptcha_privatekey')); if ($has_api_key) { add_settings_field('cancel', '', array(&$this, 'cancel_enter_api_key'), 'recaptcha', 'recaptcha_apikey'); } } else { // API Key. Add test tool. add_action('wp_ajax_recaptcha-init-test-api-key', array(&$this, 'ajax_init_test_api_key')); add_action('wp_ajax_recaptcha-test-api-key', array(&$this, 'ajax_test_api_key')); add_action('wp_ajax_recaptcha-test-verification', array(&$this, 'ajax_test_api_key_verification')); } if ($has_api_key) { if (!WP_reCaptcha::is_network_activated() || !is_network_admin()) { // local options register_setting('recaptcha_options', 'recaptcha_language', array(&$this, 'sanitize_language')); register_setting('recaptcha_options', 'recaptcha_flavor', array(&$this, 'sanitize_flavor')); register_setting('recaptcha_options', 'recaptcha_theme', array(&$this, 'sanitize_theme')); register_setting('recaptcha_options', 'recaptcha_disable_submit', 'intval'); register_setting('recaptcha_options', 'recaptcha_noscript', 'intval'); add_settings_field('recaptcha_flavor', __('Flavor', 'wp-recaptcha-integration'), array(&$this, 'input_radio'), 'recaptcha', 'recaptcha_styling', array('name' => 'recaptcha_flavor', 'items' => array(array('value' => 'grecaptcha', 'label' => __('No Captcha where you just click a button', 'wp-recaptcha-integration')), array('value' => 'recaptcha', 'label' => __('Old style reCAPTCHA where you type some cryptic text', 'wp-recaptcha-integration'))))); add_settings_field('recaptcha_language', __('Language Settings'), array(&$this, 'select_language'), 'recaptcha', 'recaptcha_styling'); add_settings_field('recaptcha_theme', __('Theme', 'wp-recaptcha-integration'), array(&$this, 'select_theme'), 'recaptcha', 'recaptcha_styling'); add_settings_field('recaptcha_disable_submit', __('Disable Submit Button', 'wp-recaptcha-integration'), array(&$this, 'input_checkbox'), 'recaptcha', 'recaptcha_styling', array('name' => 'recaptcha_disable_submit', 'label' => __('Disable Form Submit Button until no-captcha is entered.', 'wp-recaptcha-integration'))); add_settings_field('recaptcha_noscript', __('Noscript Fallback', 'wp-recaptcha-integration'), array(&$this, 'input_checkbox'), 'recaptcha', 'recaptcha_advanced', array('name' => 'recaptcha_noscript', 'label' => __('Provide a fallback for non javascript capable browsers.', 'wp-recaptcha-integration'), 'description' => __('Leave this unchecked when your site requires JavaScript anyway.', 'wp-recaptcha-integration'), 'class' => 'flavor-grecaptcha')); global $wp_version; if (version_compare($wp_version, '4.2') >= 0) { register_setting('recaptcha_options', 'recaptcha_comment_use_42_filter', 'intval'); add_settings_field('recaptcha_comment_use_42_filter', __('Comment Form rendering', 'wp-recaptcha-integration'), array(&$this, 'input_checkbox'), 'recaptcha', 'recaptcha_advanced', array('name' => 'recaptcha_comment_use_42_filter', 'label' => __('My Comment Form is WordPress 4.2 compatible.', 'wp-recaptcha-integration'), 'description' => __('Enable this when your comment form uses the <code>comment_form_submit_button</code> filter. (Or just try if it works.)', 'wp-recaptcha-integration'), 'class' => 'flavor-grecaptcha')); } } if (!WP_reCaptcha::is_network_activated() || is_network_admin()) { // network options register_setting('recaptcha_options', 'recaptcha_enable_comments', 'intval'); register_setting('recaptcha_options', 'recaptcha_enable_signup', 'intval'); register_setting('recaptcha_options', 'recaptcha_enable_login', 'intval'); register_setting('recaptcha_options', 'recaptcha_enable_lostpw', 'intval'); register_setting('recaptcha_options', 'recaptcha_lockout', 'intval'); if (function_exists('WC')) { register_setting('recaptcha_options', 'recaptcha_enable_wc_order', 'intval'); } register_setting('recaptcha_options', 'recaptcha_disable_for_known_users', 'intval'); $protect_settings = array(); $protect_settings[] = array('name' => 'recaptcha_enable_comments', 'label' => __('Comment Form', 'wp-recaptcha-integration'), 'class' => ''); $protect_settings[] = array('name' => 'recaptcha_enable_signup', 'label' => __('Signup Form', 'wp-recaptcha-integration'), 'class' => ''); $protect_settings[] = array('name' => 'recaptcha_enable_login', 'label' => __('Login Form', 'wp-recaptcha-integration'), 'class' => ''); $protect_settings[] = array('name' => 'recaptcha_enable_lostpw', 'label' => __('Lost Password Form', 'wp-recaptcha-integration'), 'class' => ''); if (function_exists('WC')) { $wc_warn = WP_reCaptcha::instance()->get_option('recaptcha_enable_wc_order') && WP_reCaptcha::instance()->get_option('recaptcha_flavor') !== 'grecaptcha'; $protect_settings[] = array('name' => 'recaptcha_enable_wc_order', 'label' => __('woocommerce Checkout', 'wp-recaptcha-integration'), 'class' => 'flavor-grecaptcha'); } add_settings_field('recaptcha_protection', __('Forms to protect', 'wp-recaptcha-integration'), array(&$this, 'input_protect'), 'recaptcha', 'recaptcha_protection', $protect_settings); add_settings_field('recaptcha_disable_for_known_users', __('Disable for known users', 'wp-recaptcha-integration'), array(&$this, 'input_checkbox'), 'recaptcha', 'recaptcha_protection', array('name' => 'recaptcha_disable_for_known_users', 'label' => __('Disable reCaptcha verification for logged in users.', 'wp-recaptcha-integration'))); add_settings_field('recaptcha_lockout', __('Prevent lockout', 'wp-recaptcha-integration'), array(&$this, 'input_checkbox'), 'recaptcha', 'recaptcha_protection', array('name' => 'recaptcha_lockout', 'label' => __('Allow administrator to log in if API keys do not work.', 'wp-recaptcha-integration'), 'description' => __('When the captcha verification fails, and the private or public API key does not work the plugin will let you in anyway. Please note that with this option checked plus a broken keypair your site will be open to brute force attacks, that try to guess an administrator password.', 'wp-recaptcha-integration'))); } if (!WP_reCaptcha::instance()->get_option('recaptcha_publickey') || !WP_reCaptcha::instance()->get_option('recaptcha_privatekey')) { add_settings_error('recaptcha', 1, __('Please configure the public and private key. <a href="http://www.google.com/recaptcha/whyrecaptcha">What are you trying to tell me?</a>', 'wp-recaptcha-integration'), 'updated'); } } }
/** * Get plugin option by name. * * @param $option_name string * @return bool false if check does not validate */ public function get_option($option_name) { switch ($option_name) { case 'recaptcha_publickey': // first try local, then global // first try local, then global case 'recaptcha_privatekey': $option_value = get_option($option_name); if (!$option_value && WP_reCaptcha::is_network_activated()) { $option_value = get_site_option($option_name); } return $option_value; case 'recaptcha_enable_comments': // global on network. else local // global on network. else local case 'recaptcha_enable_signup': case 'recaptcha_enable_login': case 'recaptcha_enable_lostpw': case 'recaptcha_disable_for_known_users': case 'recaptcha_enable_wc_order': if (WP_reCaptcha::is_network_activated()) { return get_site_option($option_name); } return get_option($option_name); default: // always local return get_option($option_name); } }