Skip to content

Commit

Permalink
Merge pull request #31 from rtCamp/develop
Browse files Browse the repository at this point in the history
GH-27: Add the "Custom Search JSON API" along with "Custom Search Site Restricted JSON API"
  • Loading branch information
SH4LIN authored Dec 22, 2023
2 parents af8fa8c + 32a748f commit 8fdd772
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 34 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
54 changes: 54 additions & 0 deletions assets/css/settings.css
Original file line number Diff line number Diff line change
@@ -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));
}
57 changes: 57 additions & 0 deletions inc/classes/class-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Assets class.
*
* @package search-with-google
*/

namespace RT\Search_With_Google\Inc;

use RT\Search_With_Google\Inc\Traits\Singleton;

/**
* Class Assets
*/
class Assets {

use Singleton;

/**
* Construct method.
*/
protected function __construct() {

$this->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' )
);
}
}
}
20 changes: 17 additions & 3 deletions inc/classes/class-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
?>
<div class="notice notice-error">
<p>
<?php
esc_html_e( 'Notice : This plugin uses a Custom site-restricted Search API which has been deprecated by Google. Hence, limiting its use to only users with existing API keys. Any new API request to Google will return a 403 error, and it will return an empty posts array on the front-end.', 'search-with-google' );
esc_html_e( 'According to a notification from Google, assistance for custom site-restricted search is scheduled to cease as of December 18, 2024.', 'search-with-google' );
?>
<a href="<?php echo esc_url( $read_more_url ); ?>" target="_blank">
<a href="<?php echo esc_url( $custom_site_restricted_search_read_more ); ?>" target="_blank">
<?php esc_html_e( 'Read more', 'search-with-google' ); ?>
</a>
</p>
<p>
<?php esc_html_e( '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. You have the option to opt in for this change by ', 'search-with-google' ); ?>
<a href="<?php echo esc_url( admin_url( 'options-reading.php#search-type' ) ); ?>">
<?php esc_html_e( 'Settings > Reading', 'search-with-google' ); ?>
</a>
</p>

<p>
<?php esc_html_e( 'Note: If you utilize the Custom Search API, there are limitations on the daily volume of search queries allowed. Kindly refer ', 'search-with-google' ); ?>
<a href="<?php echo esc_url( $custom_search_read_more ); ?>" target="_blank">
<?php esc_html_e( 'Documentation', 'search-with-google' ); ?>
</a>
</p>
</div>
<?php

Expand Down
1 change: 1 addition & 0 deletions inc/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Plugin {
protected function __construct() {

// Load plugin classes.
Assets::get_instance();
Settings::get_instance();
Search::get_instance();
Notice::get_instance();
Expand Down
27 changes: 19 additions & 8 deletions inc/classes/class-search-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class Search_Engine {

use Singleton;

/**
* Custom Search Site Restricted JSON API URL.
*
* @var string
*/
const GOOGLE_API_URL = 'https://customsearch.googleapis.com/customsearch/v1/siterestrict';

/**
* API Key.
*
Expand Down Expand Up @@ -80,7 +73,7 @@ public function get_search_results( $search_query, $page = 1, $posts_per_page =
'q' => rawurlencode( $search_query ),
'num' => $posts_per_page,
),
self::GOOGLE_API_URL
$this->get_api_url()
);

if ( $page > 1 ) {
Expand Down Expand Up @@ -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;

}
}
36 changes: 36 additions & 0 deletions inc/classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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'
);

}

/**
Expand Down Expand Up @@ -112,5 +122,31 @@ public function cse_id_field_cb() {
<input type="text" name="gcs_cse_id" value="<?php echo ! empty( $setting ) ? esc_attr( $setting ) : ''; ?>">
<?php
}

/**
* Search Type field markup.
*
* @return void
*/
public function cse_search_type_field_cb() {

// Get the value of the setting we've registered with register_setting().
$setting = get_option( 'gcs_search_type' );

// Add slider toggle switch.
?>
<div class="switch-wrapper">
<span><?php esc_html_e( 'Custom Site Restricted Search API', 'search-with-google' ); ?></span>

<label class="switch">
<input id="search-type" type="checkbox" name="gcs_search_type" value="1" <?php checked( $setting, '1' ); ?>>
<span class="slider"></span>
</label>

<span><?php esc_html_e( 'Custom Search API', 'search-with-google' ); ?></span>
</div>
<?php

}
}

39 changes: 22 additions & 17 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
=== Search with Google ===
Contributors: rtCamp, kiranpotphode
Contributors: rtCamp, kiranpotphode, sh4lin
Donate link: https://rtcamp.com/
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.8
Tested up to: 5.5
Stable tag: 1.0
Requires PHP: 8.0
Tested up to: 6.4.2
Stable tag: 1.1
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Replace WordPress default search with server-side rendered Google Custom Search results.

== 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 replacement is done on the WordPress back-end, so results appear as normal within WordPress search.
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 =
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! =

Expand Down Expand Up @@ -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.
6 changes: 5 additions & 1 deletion search-with-google.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';

/**
Expand Down

0 comments on commit 8fdd772

Please sign in to comment.