From b06321a87b3ca189608c426e6d52efede27298c4 Mon Sep 17 00:00:00 2001 From: Raymond Rutjes Date: Mon, 12 Sep 2016 16:32:28 +0200 Subject: [PATCH] Add an option to disable Algolia powered by link Resolves: #198 --- assets/css/algolia-instantsearch.css | 8 ++++-- .../class-algolia-admin-page-settings.php | 28 +++++++++++++++++++ includes/class-algolia-settings.php | 18 ++++++++++++ includes/class-algolia-template-loader.php | 1 + templates/autocomplete.php | 26 +++++++++-------- templates/instantsearch.php | 2 +- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/assets/css/algolia-instantsearch.css b/assets/css/algolia-instantsearch.css index 55928c24..77382ef1 100644 --- a/assets/css/algolia-instantsearch.css +++ b/assets/css/algolia-instantsearch.css @@ -24,7 +24,7 @@ #algolia-search-box { position: relative; - margin-bottom: 2rem; + margin-bottom: 3rem; } #algolia-search-box input { @@ -49,9 +49,11 @@ } .ais-search-box--powered-by { + position: absolute; + top: 54px; + right: 0; font-size: 14px; text-align: right; - margin-top: 2px; } .ais-search-box--powered-by-link { @@ -68,7 +70,7 @@ } .ais-stats { position: absolute; - bottom: 0; + top: 54px; font-size: 14px; } diff --git a/includes/admin/class-algolia-admin-page-settings.php b/includes/admin/class-algolia-admin-page-settings.php index 5a2c5d7f..e37d9163 100644 --- a/includes/admin/class-algolia-admin-page-settings.php +++ b/includes/admin/class-algolia-admin-page-settings.php @@ -109,9 +109,18 @@ public function add_settings() { $this->section ); + add_settings_field( + 'algolia_powered_by_enabled', + __( 'Remove Algolia powered by logo', 'algolia' ), + array( $this, 'powered_by_enabled_callback' ), + $this->slug, + $this->section + ); + register_setting( $this->option_group, 'algolia_application_id', array( $this, 'sanitize_application_id' ) ); register_setting( $this->option_group, 'algolia_search_api_key', array( $this, 'sanitize_search_api_key' ) ); register_setting( $this->option_group, 'algolia_api_key', array( $this, 'sanitize_api_key' ) ); + register_setting( $this->option_group, 'algolia_powered_by_enabled', array( $this, 'sanitize_powered_by_enabled' ) ); } public function application_id_callback() { @@ -132,6 +141,16 @@ public function api_key_callback() { '

' . __( 'Your Algolia ADMIN API key (kept private).', 'algolia' ) . '

'; } + public function powered_by_enabled_callback() { + $powered_by_enabled = $this->plugin->get_settings()->is_powered_by_enabled(); + $checked = ''; + if( ! $powered_by_enabled) { + $checked = ' checked'; + } + echo "" . + '

' . __( 'This will remove the Algolia logo from the autocomplete and the search page. We require that you keep the Algolia logo if you are using a free Hacker plan.', 'algolia' ) . '

'; + } + public function sanitize_application_id( $value ) { $value = sanitize_text_field( $value ); @@ -222,6 +241,15 @@ public function sanitize_api_key( $value ) { return $value; } + + /** + * @param $value + * + * @return string + */ + public function sanitize_powered_by_enabled( $value ) { + return $value === 'no' ? 'no' : 'yes'; + } /** * Display the page. diff --git a/includes/class-algolia-settings.php b/includes/class-algolia-settings.php index 90e9d921..69faed87 100644 --- a/includes/class-algolia-settings.php +++ b/includes/class-algolia-settings.php @@ -18,6 +18,7 @@ public function __construct() add_option( 'algolia_index_name_prefix', 'wp_' ); add_option( 'algolia_logging_enabled', 'no' ); add_option( 'algolia_api_is_reachable', 'no' ); + add_option( 'algolia_powered_by_enabled', 'yes' ); } /** @@ -168,4 +169,21 @@ public function set_api_is_reachable( $flag ) { $value = (bool) $flag === true ? 'yes' : 'no'; update_option( 'algolia_api_is_reachable', $value ); } + + /** + * @return bool + */ + public function is_powered_by_enabled() { + $enabled = get_option( 'algolia_powered_by_enabled', 'yes' ); + + return $enabled === 'yes'; + } + + public function enable_powered_by() { + update_option( 'algolia_powered_by_enabled', 'yes' ); + } + + public function disable_powered_by() { + update_option( 'algolia_powered_by_enabled', 'no' ); + } } diff --git a/includes/class-algolia-template-loader.php b/includes/class-algolia-template-loader.php index 203274ab..32e97b55 100644 --- a/includes/class-algolia-template-loader.php +++ b/includes/class-algolia-template-loader.php @@ -36,6 +36,7 @@ public function load_algolia_config() { 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, 'application_id' => $settings->get_application_id(), 'search_api_key' => $settings->get_search_api_key(), + 'powered_by_enabled' => $settings->is_powered_by_enabled(), 'autocomplete' => array( 'sources' => $autocomplete_config->get_config(), ), diff --git a/templates/autocomplete.php b/templates/autocomplete.php index d93fd2a9..40ebb02d 100644 --- a/templates/autocomplete.php +++ b/templates/autocomplete.php @@ -104,19 +104,21 @@ jQuery("input[name='s']").each(function(i) { var $searchInput = jQuery(this); + var config = { + debug: algolia.debug, + hint: false, + openOnFocus: true, + templates: {} + }; + //Todo: Add empty template when we fixed https://github.com/algolia/autocomplete.js/issues/109 + + if(algolia.powered_by_enabled) { + config.templates.footer = wp.template('autocomplete-footer'); + } + // Instantiate autocomplete.js - autocomplete($searchInput[0], - { - debug: algolia.debug, - hint: false, - openOnFocus: true, - templates: { - //empty: wp.template('autocomplete-empty'), // Waiting for https://github.com/algolia/autocomplete.js/issues/109 - footer: wp.template('autocomplete-footer') - } - }, - sources - ).on('autocomplete:selected', function(e, suggestion, datasetName) { + autocomplete($searchInput[0], config, sources) + .on('autocomplete:selected', function(e, suggestion, datasetName) { // Redirect the user when we detect a suggestion selection. window.location.href = suggestion.permalink; }); diff --git a/templates/instantsearch.php b/templates/instantsearch.php index b9ab16e8..e5d2fe55 100644 --- a/templates/instantsearch.php +++ b/templates/instantsearch.php @@ -97,7 +97,7 @@ container: '#algolia-search-box', placeholder: 'Search for...', wrapInput: false, - poweredBy: true + poweredBy: algolia.powered_by_enabled }) );