Skip to content

Commit

Permalink
Merge pull request #18 from rtCamp/update/php-8.2
Browse files Browse the repository at this point in the history
GH-19: Plugin Update
  • Loading branch information
SH4LIN authored Dec 14, 2023
2 parents fcaaaee + 8fb06ba commit 4b8e8f2
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Replace WordPress default search with Google Custom Search results.

**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)

**Requires PHP:** 5.4+
**Requires PHP:** 8.0+

## 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).
Expand Down
2 changes: 1 addition & 1 deletion inc/classes/class-notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace RT\Search_With_Google\Inc;

use \RT\Search_With_Google\Inc\Traits\Singleton;
use RT\Search_With_Google\Inc\Traits\Singleton;

/**
* Class Notice
Expand Down
4 changes: 2 additions & 2 deletions inc/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/**
* Plugin manifest class.
*
* @package search-with-google
* @package search-with-google
*/

namespace RT\Search_With_Google\Inc;

use \RT\Search_With_Google\Inc\Traits\Singleton;
use RT\Search_With_Google\Inc\Traits\Singleton;

/**
* Class Plugin
Expand Down
56 changes: 35 additions & 21 deletions inc/classes/class-search-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

namespace RT\Search_With_Google\Inc;

use \RT\Search_With_Google\Inc\Traits\Singleton;
use RT\Search_With_Google\Inc\Traits\Singleton;

/**
* Class Search_Engine.
*
* @package search-with-google
*/
class Search_Engine {

Expand All @@ -30,40 +28,46 @@ class Search_Engine {
*
* @var string
*/
private $api_key = '';
private string $api_key = '';

/**
* Custom Search Engine ID.
*
* @var string
*/
private $cse_id = '';
private string $cse_id = '';

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

$this->init();

}

/**
* Action / Filters to be declare here.
*
* @return void
*/
protected function init() {
protected function init(): void {

$this->api_key = get_option( 'gcs_api_key' );
$this->cse_id = get_option( 'gcs_cse_id' );

}

/**
* Get search results from the Custom Search Engine.
*
* @param string $search_query Search Query.
* @param int $page Page number.
* @param string $search_query Search Query.
* @param int $page Page number.
* @param int $posts_per_page Items per request.
*
* @return array|\WP_Error Posts or false if empty or error.
*/
public function get_search_results( $search_query, $page = 1, $posts_per_page = 10 ) {
public function get_search_results( string $search_query, int $page = 1, int $posts_per_page = 10 ): \WP_Error|array {

$item_details = array();
$total_results = 0;
Expand All @@ -84,12 +88,21 @@ public function get_search_results( $search_query, $page = 1, $posts_per_page =
$request_url = add_query_arg( array( 'start' => $start ), $request_url );
}

$response = wp_remote_get(
$request_url,
array(
'timeout' => 120,
)
);
if ( function_exists( 'vip_safe_wp_remote_get') ) {
$response = vip_safe_wp_remote_get(
$request_url, // URL.
new \WP_Error( 'google_api_error', __( 'Unknown error occurred', 'search-with-google' ) ), // Fallback value.
3, // Threshold.
120 // Timeout.
);
} else {
$response = wp_remote_get( // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get -- Using this function if vip_safe_wp_remote_get function not available.
$request_url,
array(
'timeout' => 120, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- External API request.
)
);
}

// Check the response code.
$response_code = wp_remote_retrieve_response_code( $response );
Expand All @@ -108,9 +121,9 @@ public function get_search_results( $search_query, $page = 1, $posts_per_page =

if ( ! is_wp_error( $result ) ) {

if ( isset( $result->searchInformation->totalResults ) && isset( $result->items ) ) {
if ( isset( $result->searchInformation->totalResults ) && isset( $result->items ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

$total_results = (int) $result->searchInformation->totalResults;
$total_results = (int) $result->searchInformation->totalResults; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

// If no results found and pagination request then try another request.
if ( 0 === $total_results && $page > 1 ) {
Expand All @@ -120,8 +133,8 @@ public function get_search_results( $search_query, $page = 1, $posts_per_page =
if ( ! empty( $result->items ) ) {
foreach ( $result->items as $item ) {

$item_detail['title'] = $item->title;
$item_detail['link'] = $item->link;
$item_detail['title'] = $item->title;
$item_detail['link'] = $item->link;
$item_detail['snippet'] = $item->snippet;

$item_details[] = $item_detail;
Expand All @@ -143,13 +156,14 @@ public function get_search_results( $search_query, $page = 1, $posts_per_page =
/**
* Calculate start index for Custom Search Engine.
*
* @param int $page Page number of results.
* @param int $page Page number of results.
* @param int $posts_per_page Results per page.
*
* @return float|int
*/
public function get_start_index( $page, $posts_per_page ) {
public function get_start_index( int $page, int $posts_per_page ): float|int {

return ( $page * $posts_per_page ) - ( $posts_per_page - 1 );

}
}
81 changes: 45 additions & 36 deletions inc/classes/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@

namespace RT\Search_With_Google\Inc;

use \RT\Search_With_Google\Inc\Traits\Singleton;
use \RT\Search_With_Google\Inc\Search_Engine;
use RT\Search_With_Google\Inc\Traits\Singleton;

/**
* Class Search.
*
* @package search-with-google
*/
class Search {

Expand All @@ -23,30 +20,35 @@ class Search {
* Construct method.
*/
protected function __construct() {

$this->setup_hooks();

}

/**
* Action / Filters to be declare here.
*
* @return void
*/
protected function setup_hooks() {
protected function setup_hooks(): void {

/**
* Filters.
*/
add_filter( 'posts_pre_query', array( $this, 'filter_search_query' ), 10, 2 );
add_filter( 'page_link', array( $this, 'update_permalink' ), 10, 2 );

}

/**
* Filter the WP Search query to get results from Custom Search Engine.
*
* @param array $posts Posts array.
* @param \WP_Query $query WP query.
* @param \WP_Post[]|int[]|null $posts Posts array.
* @param \WP_Query $query WP query.
*
* @return mixed Modified posts.
* @return array|null Modified posts.
*/
public function filter_search_query( $posts, $query ) {
public function filter_search_query( array|null $posts, \WP_Query $query ): array|null {

if ( ! $query->is_search || is_admin() ) {
return $posts;
Expand Down Expand Up @@ -83,19 +85,22 @@ public function filter_search_query( $posts, $query ) {
$query->max_num_pages = intval( floor( $query->found_posts / $posts_per_page ) );

return $posts;

}

/**
* Get transients key based on search query and page.
*
* @param string $search_query Search query.
* @param int $page Page.
* @param string $search_query Search query.
* @param int $page Page.
* @param int $posts_per_page Posts per page.
*
* @return string
*/
public function get_transient_key( $search_query, $page, $posts_per_page ) {
public function get_transient_key( string $search_query, int $page, int $posts_per_page ): string {

return 'gcs_results_' . sanitize_title( $search_query ) . '_' . $page . '_' . $posts_per_page;

}

/**
Expand All @@ -105,7 +110,8 @@ public function get_transient_key( $search_query, $page, $posts_per_page ) {
*
* @return array
*/
public function get_posts( $items ) {
public function get_posts( array $items ): array {

$posts = array();

if ( ! empty( $items ) ) {
Expand All @@ -115,6 +121,7 @@ public function get_posts( $items ) {
}

return $posts;

}

/**
Expand All @@ -124,28 +131,27 @@ public function get_posts( $items ) {
*
* @return \WP_Post
*/
public function get_post( $item ) {

$post_id = - wp_rand( 1, 99999 ); // Negative ID, to avoid clash with a valid post.
$post = new \stdClass();
$post->ID = $post_id;
$post->post_author = 1;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$post->post_title = $item['title'];
$post->post_content = $item['snippet'];
$post->post_status = 'publish';
$post->comment_status = 'closed';
$post->ping_status = 'closed';
$post->post_name = $this->get_post_name( $item['link'] ); // Get post slug from URL.
public function get_post( array $item ): \WP_Post {

$post_id = - wp_rand( 1, 99999 ); // Negative ID, to avoid clash with a valid post.
$post = new \stdClass();
$post->ID = $post_id;
$post->post_author = 1;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$post->post_title = $item['title'];
$post->post_content = $item['snippet'];
$post->post_status = 'publish';
$post->comment_status = 'closed';
$post->ping_status = 'closed';
$post->post_name = $this->get_post_name( $item['link'] ); // Get post slug from URL.
$post->search_permalink = $item['link']; // Get post permalink from URL. This will replace the WP default permalink.
$post->post_type = 'page';
$post->filter = 'raw'; // Important!
$post->post_type = 'page';
$post->filter = 'raw'; // Important!

// Convert to WP_Post object.
$wp_post = new \WP_Post( $post );
return new \WP_Post( $post );

return $wp_post;
}

/**
Expand All @@ -155,28 +161,31 @@ public function get_post( $item ) {
*
* @return string
*/
public function get_post_name( $url ) {
public function get_post_name( string $url ): string {

$url_parse = wp_parse_url( $url );

return ltrim( $url_parse['path'], '/' );

}

/**
* Set Permalink of search result from Custom search results.
*
* @param string $permalink Page URL.
* @param string $post Post ID.
* @param int $post_id Post ID.
*
* @return string Updated permalink.
*/
public function update_permalink( $permalink, $post ) {
$post = get_post( $post );
public function update_permalink( string $permalink, int $post_id ): string {

$post = get_post( $post_id );

if ( ! empty( $post->search_permalink ) ) {
if ( ! empty( $post->search_permalink ) ) {
return $post->search_permalink;
}

return $permalink;

}
}
Loading

0 comments on commit 4b8e8f2

Please sign in to comment.