diff --git a/README.md b/README.md index f4bf145..5230a78 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,20 @@ Replace WordPress default search with Google Custom Search results. -**Contributors:** [rtCamp](https://github.com/rtCamp/), [Kiran Potphode](https://github.com/kiranpotphode/) +**Contributors:** [rtCamp](https://github.com/rtCamp/), [Kiran Potphode](https://github.com/kiranpotphode/), [Shalin Shah](https://github.com/SH4LIN) **Tags:** google, search, cse, custom search engine, programmable search, programmable search engine, google cse, google custom search engine, google programmable search, google programmable search engine, google search **Requires at least:** 4.9 -**Tested up to:** 5.5 +**Tested up to:** 6.4.2 **License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html) -**Requires PHP:** 8.0+ +**Requires PHP:** 7.4 ## Description -This plugin will replace the WordPress default search query with server-side results from [Custom Search Site Restricted JSON API](https://developers.google.com/custom-search/v1/site_restricted_api). +This plugin will replace the WordPress default search query with server-side results from either the [Custom Search Site Restricted JSON API](https://developers.google.com/custom-search/v1/site_restricted_api) or the [Custom Search JSON API](https://developers.google.com/custom-search/v1/overview). You can make your selection within the settings > Reading > Search type. This replacement is done on the WordPress back-end, so results appear as normal within WordPress search. ## Requirements - [Google API Key](https://console.developers.google.com/apis/credentials) @@ -24,11 +24,14 @@ This plugin will replace the WordPress default search query with server-side res ## Setup - [Get Google API key](https://developers.google.com/custom-search/v1/introduction). An API key is a way to identify your client to Google. - [Get Programmable Search engine ID](https://cse.google.com/). In settings, restrict the Search engine to only search for your one site. - - On WordPress dashboard, set API Key and Custom Search Engine ID in the plugin settings. `Dashboard > Settings > Reading > Search with Google Settings`. +- On WordPress dashboard, set API Key and Custom Search Engine ID in the plugin settings. `Dashboard > Settings > Reading > Search with Google Settings`. +- Select the search type from Custom Search Site Restricted JSON API or Custom Search API. (Refer [#Notes](#notes) section for more details) ## Notes - Custom Search Site Restricted JSON API can show only 100 search results for the query. - A result page can have maximum of 10 results. +- Assistance for Custom Site Restricted Search JSON API is scheduled to cease as of December 18, 2024. [Read more](https://developers.google.com/custom-search/v1/site_restricted_api). Due to this modification, we are introducing an opt-in feature that enables the use of solely the Custom Search API, as opposed to the Custom Site-restricted Search API. This will allow you to continue using the Custom Search API after December 18, 2024. +- Custom Site Search API has a daily limit of 10,000 queries per day. [Read more](https://developers.google.com/custom-search/v1/overview#pricing). ## Contribute diff --git a/assets/css/settings.css b/assets/css/settings.css new file mode 100644 index 0000000..81566e0 --- /dev/null +++ b/assets/css/settings.css @@ -0,0 +1,54 @@ +.switch-wrapper { + display: flex; + align-items: center; + justify-content: flex-start; + gap: 14px; + flex-wrap: wrap; +} + +.switch { + position: relative; + display: inline-block; + width: 50px; + height: 24px; +} + +.switch input { + opacity: 0; + width: 0; + height: 0; + overflow: hidden; +} + +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + display: flex; + align-items: center; + background-color: #ccc; + -webkit-transition: transform .4s; + transition: transform .4s; + border-radius: 34px; +} + +.slider::before { + position: absolute; + content: ""; + height: 18px; + width: 18px; + left: 2px; + background-color: white; + -webkit-transition: .4s; + transition: .4s; + border-radius: 50%; +} + +input[name="gcs_search_type"]:checked ~ .slider:before { + -webkit-transform: translateX(calc(50px - 20px - 2px)); + -ms-transform: translateX(calc(50px - 20px - 2px)); + transform: translateX(calc(50px - 20px - 2px)); +} diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php new file mode 100644 index 0000000..ed61cd5 --- /dev/null +++ b/inc/classes/class-assets.php @@ -0,0 +1,57 @@ +setup_hooks(); + + } + + /** + * Action / Filters to be declare here. + * + * @return void + */ + protected function setup_hooks() { + + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); + + } + + /** + * Enqueue admin assets. + * + * @param string $hook_suffix Hook suffix. + * + * @return void + */ + public function enqueue_admin_assets( $hook_suffix ) { + + if ( 'options-reading.php' === $hook_suffix ) { + wp_enqueue_style( + 'search-with-google-settings-style', + SEARCH_WITH_GOOGLE_URL . '/assets/css/settings.css', + array(), + filemtime( SEARCH_WITH_GOOGLE_PATH . '/assets/css/settings.css' ) + ); + } + } +} diff --git a/inc/classes/class-notice.php b/inc/classes/class-notice.php index d97e0b7..890270c 100644 --- a/inc/classes/class-notice.php +++ b/inc/classes/class-notice.php @@ -46,17 +46,31 @@ protected function setup_hooks() { */ public function display_notice() { - $read_more_url = 'https://developers.google.com/custom-search/v1/site_restricted_api'; + $custom_site_restricted_search_read_more = 'https://developers.google.com/custom-search/v1/site_restricted_api'; + $custom_search_read_more = 'https://developers.google.com/custom-search/v1/overview#pricing'; ?>

- +

+

+ + + Reading', 'search-with-google' ); ?> + +

+ +

+ + + + +

rawurlencode( $search_query ), 'num' => $posts_per_page, ), - self::GOOGLE_API_URL + $this->get_api_url() ); if ( $page > 1 ) { @@ -166,4 +159,22 @@ public function get_start_index( $page, $posts_per_page ) { return ( $page * $posts_per_page ) - ( $posts_per_page - 1 ); } + + /** + * Get API URL. + * + * @return string + */ + private function get_api_url() { + + $search_type = get_option( 'gcs_search_type' ); + $api_url = 'https://www.googleapis.com/customsearch/v1/siterestrict'; + + if ( '1' === $search_type ) { + $api_url = 'https://www.googleapis.com/customsearch/v1'; + } + + return $api_url; + + } } diff --git a/inc/classes/class-settings.php b/inc/classes/class-settings.php index c741c92..4fdf007 100644 --- a/inc/classes/class-settings.php +++ b/inc/classes/class-settings.php @@ -50,6 +50,7 @@ public function register_settings() { register_setting( 'reading', 'gcs_api_key', $args ); register_setting( 'reading', 'gcs_cse_id', $args ); + register_setting( 'reading', 'gcs_search_type', $args ); // Register a new section in the "reading" page. add_settings_section( @@ -75,6 +76,15 @@ public function register_settings() { 'cse_settings_section' ); + // Add a new toggle setting field for selecting between custom search API and custom site-restricted search API. + add_settings_field( + 'gcs_search_type', + __( 'Search Type', 'search-with-google' ), + array( $this, 'cse_search_type_field_cb' ), + 'reading', + 'cse_settings_section' + ); + } /** @@ -112,5 +122,31 @@ public function cse_id_field_cb() { +
+ + + + + +
+ Reading > Search type. This replacement is done on the WordPress back-end, so results appear as normal within WordPress search. = Requirements = 1. [Google API Key](https://console.developers.google.com/apis/credentials) -1. [Programmable Search engine ID](https://cse.google.com/all) +2. [Programmable Search engine ID](https://cse.google.com/all) + = Setup = 1. [Get Google API key](https://developers.google.com/custom-search/v1/introduction). An API key is a way to identify your client to Google. -1. [Get Programmable Search engine ID](https://cse.google.com/). In Google settings, restrict the Search engine to only search for your one site. -1. On WordPress dashboard, set API Key and Custom Search Engine ID in the plugin settings. `Dashboard > Settings > Reading > Search with Google Settings`. +2. [Get Programmable Search engine ID](https://cse.google.com/). In Google settings, restrict the Search engine to only search for your one site. +3. On WordPress dashboard, set API Key and Custom Search Engine ID in the plugin settings. `Dashboard > Settings > Reading > Search with Google Settings`. +4. Select the search type from Custom Search Site Restricted JSON API or Custom Search API. (Refer [Notes](#notes) section for more details) = Notes = 1. Custom Search Site Restricted JSON API can show only 100 search results for the query. -1. A result page can have maximum of 10 results. +2. A result page can have maximum of 10 results. +3. Assistance for Custom Site Restricted Search JSON API is scheduled to cease as of December 18, 2024. [Read more](https://developers.google.com/custom-search/v1/site_restricted_api). Due to this modification, we are introducing an opt-in feature that enables the use of solely the Custom Search API, as opposed to the Custom Site-restricted Search API. This will allow you to continue using the Custom Search API after December 18, 2024. = BTW, We're Hiring! = @@ -66,23 +69,25 @@ Once you're ready to send a pull request, please run through the following check == Changelog == -= 1.0 = -* Initial release. - = 1.1 = -* Updated PHP code to be compatible with PHP 8.0+ +* Updated PHP code to be compatible with PHP 8.2 * Fixed WordPress coding standards issues * Used VIP compatible code for WordPress VIP compatibility +* Added support for the Custom JSON API * Added Deprecation notice for the Custom site-restricted JSON API += 1.0 = +* Initial release. + == Upgrade Notice == - -= 1.0 = -Initial release. = 1.1 = -* Updated PHP code to be compatible with PHP 8.0+ +* Updated PHP code to be compatible with PHP 8.2 * Fixed WordPress coding standards issues * Used VIP compatible code for WordPress VIP compatibility +* Added support for the Custom JSON API * Added Deprecation notice for the Custom site-restricted JSON API + += 1.0 = +Initial release. diff --git a/search-with-google.php b/search-with-google.php index 62d08ec..f8d525f 100644 --- a/search-with-google.php +++ b/search-with-google.php @@ -2,7 +2,7 @@ /** * Plugin Name: Search with Google * Description: Replace WordPress default search with Google Custom Search results. - * Version: 1.0 + * Version: 1.1 * Author: rtCamp * Author URI: https://rtCamp.com * License: GPLv2 or later @@ -20,6 +20,10 @@ define( 'SEARCH_WITH_GOOGLE_PATH', __DIR__ ); } +if ( ! defined( 'SEARCH_WITH_GOOGLE_URL' ) ) { + define( 'SEARCH_WITH_GOOGLE_URL', plugins_url( '', __FILE__ ) ); +} + require_once SEARCH_WITH_GOOGLE_PATH . '/inc/helpers/autoloader.php'; /**