Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New secure endpoints to obtain products and custom items and dooplugins update on save #224

Merged
merged 49 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
55ff65a
init endpoints
vferrero Oct 24, 2023
29d41a6
Merge remote-tracking branch 'origin/master' into vferrero/pluginrang…
vferrero Oct 24, 2023
2e06374
new endpoints
vferrero Nov 3, 2023
28d5225
Merge remote-tracking branch 'origin/master' into vferrero/pluginrang…
vferrero Nov 3, 2023
f399246
new endpoints
vferrero Nov 3, 2023
bcfc87f
- New endpoints
vferrero Nov 3, 2023
861fa40
Upgrade version 2.1.0
vferrero Nov 3, 2023
1802d64
image link field
vferrero Nov 8, 2023
a810985
one element in custom attributes
vferrero Nov 8, 2023
de25c76
refactor get_custom_attributes
vferrero Nov 8, 2023
1d4b6a1
reset_token_auth migration
vferrero Nov 8, 2023
0ffe860
Merge remote-tracking branch 'origin/master' into vferrero/pluginrang…
vferrero Nov 10, 2023
49b164c
Review code split functions
vferrero Nov 10, 2023
33a37e1
TODO remove api credentials
vferrero Nov 10, 2023
0f62e08
optimize get_variations
vferrero Nov 10, 2023
d325b12
refactor get_variants function
vferrero Nov 10, 2023
7106e9e
change endpoints names
vferrero Nov 11, 2023
9018ae9
clean unused fields
vferrero Nov 13, 2023
d191702
clear stock status
vferrero Nov 14, 2023
0d6ecf2
clear attributes
vferrero Nov 14, 2023
e806a8a
ids parameter filter
vferrero Nov 15, 2023
e998361
remove decimal precission
vferrero Nov 15, 2023
e51d28a
All fields request and clean empty fields
vferrero Nov 15, 2023
0f5275e
clean empty fields
vferrero Nov 15, 2023
28b0168
comment fix
vferrero Nov 15, 2023
a9e0c8b
refactor custom attributes var
vferrero Nov 15, 2023
33f5241
change to self class
vferrero Nov 15, 2023
88768f8
clean empty fields
vferrero Nov 16, 2023
9a62f7d
- Handler only in old requests
vferrero Nov 16, 2023
9f9b774
add $config_request param
vferrero Nov 17, 2023
fbedb90
- Update on save with dooplugins
vferrero Nov 22, 2023
effd610
doc param return
vferrero Nov 22, 2023
aefac96
remove debug
vferrero Nov 22, 2023
fd86bca
Refactor Thumb class generate
vferrero Nov 22, 2023
b994989
Comments Thumb class
vferrero Nov 22, 2023
f917eee
Check metadata image
vferrero Nov 22, 2023
e9c150b
Refactor thumb class
vferrero Nov 22, 2023
5f0bff5
change function name
vferrero Nov 22, 2023
f1594a3
remove debug
vferrero Nov 22, 2023
66c47f3
setting dooplugins var in migration version
vferrero Nov 22, 2023
286cf21
order by id asc
vferrero Nov 23, 2023
0872cd7
change per_page default var
vferrero Nov 23, 2023
01fdf99
order by and order
vferrero Nov 23, 2023
7d9f0af
default params
vferrero Nov 23, 2023
24acbcd
upgrade readme
vferrero Nov 23, 2023
57441d2
upgrade version
vferrero Nov 23, 2023
1a19664
Fix infinite loop in categories when parent_id is empty
vferrero Nov 25, 2023
098c189
empty condition
vferrero Nov 27, 2023
e010f89
Merge remote-tracking branch 'origin/master' into vferrero/pluginrang…
vferrero Nov 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions doofinder-for-woocommerce/doofinder-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Doofinder WP & WooCommerce Search
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Version: 2.0.32
* Version: 2.1.0
* Requires at least: 5.6
* Requires PHP: 7.0
* Author: Doofinder
Expand Down Expand Up @@ -35,7 +35,7 @@ class Doofinder_For_WordPress
*
* @var string
*/
public static $version = '2.0.32';
public static $version = '2.1.0';

/**
* The only instance of Doofinder_For_WordPress
Expand Down Expand Up @@ -99,6 +99,8 @@ public function __construct()
// Load classes on demand
self::autoload(self::plugin_path() . 'includes/');

Endpoints::init();

add_action('init', function () use ($class) {
//Initialize update on save
Update_On_Save::init();
Expand Down Expand Up @@ -338,7 +340,6 @@ public static function initialize_rest_endpoints()
{
add_action('rest_api_init', function () {
Config::register();
REST_API_Handler::initialize();
Index_Status_Handler::initialize();
});
}
Expand Down
5 changes: 2 additions & 3 deletions doofinder-for-woocommerce/includes/api/class-store-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ private function get_product_datatype($language)
"type" => "wordpress",
"options" => [
"feed_type" => "product",
"url" => $this->language->get_home_url($language),
"custom_attributes" => Settings::get_custom_attributes()
"lang" => $language
]
]
]
Expand All @@ -333,7 +332,7 @@ private function get_post_datatype($language)
"type" => "wordpress",
"options" => [
"feed_type" => "posts",
"url" => $this->language->get_home_url($language)
"lang" => $language
]
]
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

use Doofinder\WP\Endpoints;

/**
* Class Endpoint_Custom
*
* This class defines various methods for handling item wordpress endpoints.
*/
class Endpoint_Custom
{
const PER_PAGE = 100;
const CONTEXT = "doofinder/v1";
const ENDPOINT = "/custom";

/**
* Initialize the custom item endpoint.
*
* @return void
*/
public static function initialize() {
add_action('rest_api_init', function () {
register_rest_route(self::CONTEXT, self::ENDPOINT, array(
'methods' => 'GET',
'callback' => array(Endpoint_Custom::class, 'custom_endpoint')
));
});
}

/**
* Custom item endpoint callback.
*
* @param WP_REST_Request $request The REST request object.
* @return WP_REST_Response Response containing modified data.
*/
public static function custom_endpoint($request) {

Endpoints::CheckSecureToken();

// Get the 'fields' parameter from the request
$fields_param = $request->get_param('fields');
$fields = !empty($fields_param) ? explode(',', $fields_param) : array();

$config_request["per_page"] = $request->get_param('per_page') ?? self::PER_PAGE;
$config_request["page"] = $request->get_param('page') ?? 1;
$config_request["lang"] = $request->get_param('lang') ?? "";
$config_request["type"] = $request->get_param('type');

// Retrieve the original items data
$items = self::get_items($config_request);
$modified_items = array();

// Process and filter items data
if (!empty($items)) {
foreach ($items as $item_data) {
// Filter fields
$filtered_data = !empty($fields) ? array_intersect_key($item_data, array_flip($fields)) : $item_data;
pedromcp90 marked this conversation as resolved.
Show resolved Hide resolved

if(isset($filtered_data["title"]["rendered"]) && in_array('title', $fields)){
$filtered_data["title"] = $filtered_data["title"]["rendered"];
}
if(isset($filtered_data["content"]["rendered"]) && in_array('content', $fields)){
$filtered_data["content"] = self::process_content($filtered_data["content"]["rendered"]);
}
if(isset($filtered_data["excerpt"]["rendered"]) && in_array('excerpt', $fields)){
$filtered_data["description"] = self::process_content($filtered_data["excerpt"]["rendered"]);
unset($filtered_data["excerpt"]);
}
unset($filtered_data["author"]);
if(in_array('author', $fields) && $config_request["type"] != "posts"){
$filtered_data["author"] = isset($filtered_data["_embedded"]["author"][0]["name"]) ? $filtered_data["_embedded"]["author"][0]["name"] : "Default";
}
if(isset($filtered_data["_embedded"]["wp:featuredmedia"][0]["media_details"]["sizes"]["medium"]["source_url"]) && in_array('image_link', $fields)){
$filtered_data["image_link"] = $filtered_data["_embedded"]["wp:featuredmedia"][0]["media_details"]["sizes"]["medium"]["source_url"];
}
if(isset($filtered_data["_embedded"]["wp:term"][0]) && in_array('post_tags', $fields)){
$filtered_data["post_tags"] = self::get_terms("post_tag", $filtered_data["_embedded"]["wp:term"]);
}
if(isset($filtered_data["_embedded"]["wp:term"][0]) && in_array('categories', $fields)){
$filtered_data["categories"] = self::get_terms("category", $filtered_data["_embedded"]["wp:term"]);
}
unset($filtered_data["_embedded"]);
$modified_items[] = $filtered_data;
}
}

// Return the modified items data as a response
return new WP_REST_Response($modified_items);
}

/**
* Process content by decoding HTML entities, stripping HTML tags, and replacing sequences of whitespace characters.
*
* @param string $content The content to process, including HTML markup.
*
* @return string The processed content with HTML entities decoded, HTML tags removed, and whitespace sequences replaced with a single space.
*/
private static function process_content($content) {
$content = html_entity_decode(strip_tags($content));
$content = preg_replace('/[ \t\r\n]+/', ' ', $content);

return trim($content);
}

/**
* Retrieves the names of taxonomies of a specific type within an array of items.
*
* @param string $type The taxonomy type to search for (e.g., "category" or "post_tag").
* @param array $array_items The array of items containing taxonomy information.
* @return array An array of taxonomy names that match the specified type.
*/
private static function get_terms($type, $array_items){
$names = array();
foreach($array_items as $array_item){
foreach($array_item as $item){
if(isset($item["taxonomy"]) && $item["taxonomy"] == $type){
$names[] = self::process_content($item["name"]);
}
}
}
return $names;
}

/**
* Retrieve a list of items with pagination.
*
* @param array $config_request Config request params (page, per_page, type)
* @return array|null An array of items data or null on failure.
*/
private static function get_items($config_request){
// Retrieve the original items data
$request = new WP_REST_Request('GET', "/wp/v2/".$config_request["type"]);
$request->set_query_params(array(
'page' => $config_request["page"],
'per_page' => $config_request["per_page"],
'lang' => $config_request["lang"]
));
$response = rest_do_request($request);
$data = rest_get_server()->response_to_data($response, true);

if(!empty($data["data"]["status"]) && $data["data"]["status"] != 200){
$data = [];
}

return $data;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use Doofinder\WP\Endpoints;
use Doofinder\WP\Thumbnail;

/**
* Class Endpoint_Post_Category
*
* This class defines various methods for handling item wordpress endpoints.
*/
class Endpoint_Post_Category
{
const PER_PAGE = 100;
const CONTEXT = "doofinder/v1";
const ENDPOINT = "/post-cat";

/**
* Initialize the custom item endpoint.
*
* @return void
*/
public static function initialize() {
add_action('rest_api_init', function () {
register_rest_route(self::CONTEXT, self::ENDPOINT, array(
'methods' => 'GET',
'callback' => array(Endpoint_Post_Category::class, 'post_category_endpoint')
));
});
}

/**
* Custom item endpoint callback.
*
* @param WP_REST_Request $request The REST request object.
* @return WP_REST_Response Response containing modified data.
*/
public static function post_category_endpoint($request) {

Endpoints::CheckSecureToken();

$config_request["per_page"] = $request->get_param('per_page') ?? self::PER_PAGE;
$config_request["page"] = $request->get_param('page') ?? 1;
$config_request["lang"] = $request->get_param('lang') ?? "";
$config_request["fields"] = $request->get_param('fields') ?? "";

// Get the 'fields' parameter from the request
$fields = !empty($config_request["fields"]) ? explode(',', $config_request["fields"]) : array();

// Retrieve the original items data
$items = self::get_items($config_request);

// Return the modified items data as a response
return new WP_REST_Response($items);
}

/**
* Retrieve a list of items with pagination.
*
* @param array $config_request Config request params (page, per_page, type)
* @return array|null An array of items data or null on failure.
*/
private static function get_items($config_request){
// Retrieve the original items data
$request = new WP_REST_Request('GET', "/wp/v2/categories");
$request->set_query_params(array(
'page' => $config_request["page"],
'per_page' => $config_request["per_page"],
'lang' => $config_request["lang"],
'_fields' => $config_request["fields"]
));
$response = rest_do_request($request);
$data = rest_get_server()->response_to_data($response, true);

if(!empty($data["data"]["status"]) && $data["data"]["status"] != 200){
$data = [];
}

return $data;
}
}
?>
Loading