Skip to content

Commit

Permalink
Merge pull request #1 from dealer4dealer/release-1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
WillieHuizing authored Apr 10, 2024
2 parents 89e510f + 46b8296 commit 1dca5d7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tags: woocommerce, connect, exact, exact globe, xcore, d4d, dealer4dealer, globe
Requires at least: 4.7.5
Tested up to: 4.9.8
Stable tag: 1.0.3
Stable tag: 1.1.0
License: The MIT License (MIT)

This module extends the api of Woocommerce and is needed for our Globe connector. For more information or support see http://www.dealer4dealer.nl.
Expand Down
11 changes: 10 additions & 1 deletion globe.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin Name: globe Rest API extension
Plugin URI: http://www.dealer4dealer.nl
description: Extend WC Rest API to support globe requests
@Version: 1.0.3
@Version: 1.1.0
@Author: Dealer4Dealer
Author URI: http://www.dealer4dealer.nl
Requires at least: 4.7.5
Expand Down Expand Up @@ -39,6 +39,15 @@ function run_globe()
}
}

add_action(
'before_woocommerce_init',
function() {
if (class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
);

function globe_check_woocommerce() {
?>
<div class="error notice">
Expand Down
121 changes: 41 additions & 80 deletions includes/class-globe-customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,96 +72,57 @@ public function init()
});
}

/**
* @param WP_REST_Request $request
* @return array|object|WP_Error|WP_REST_Response|null
* @throws Exception
*/
public function get_items($request)
{
$date_modified = $request['date_modified'] ?: 0;
global $wpdb;

/**
* If e-mail is given with the request, let woocommerce handle it.
*/
if ($request['email']) {
return parent::get_items($request);
}

$prepared_args = array();
$prepared_args['exclude'] = $request['exclude'];
$prepared_args['include'] = $request['include'];
$prepared_args['order'] = $request['order'];
$prepared_args['number'] = $request['per_page'];
if ( ! empty( $request['offset'] ) ) {
$prepared_args['offset'] = $request['offset'];
} else {
$prepared_args['offset'] = ( $request['page'] - 1 ) * $prepared_args['number'];
}
$orderby_possibles = array(
'id' => 'ID',
'include' => 'include',
'name' => 'display_name',
'registered_date' => 'registered',
);
$prepared_args['orderby'] = $orderby_possibles[ $request['orderby'] ];
$prepared_args['search'] = $request['search'];

if ( '' !== $prepared_args['search'] ) {
$prepared_args['search'] = '*' . $prepared_args['search'] . '*';
}

// Filter by email.
if ( ! empty( $request['email'] ) ) {
$prepared_args['search'] = $request['email'];
$prepared_args['search_columns'] = array( 'user_email' );
}
$limit = 50;
$timezoneOffset = wc_timezone_offset();
$filter_date_modified = '2001-01-01 00:00:00';

// Filter by role.
if ( 'all' !== $request['role'] ) {
$prepared_args['role'] = $request['role'];
if (isset($request['limit']) && $request['limit']) {
$limit = (int)$request['limit'];
}

$prepared_args = apply_filters( 'woocommerce_rest_customer_query', $prepared_args, $request );


$query = new Globe_User_Query( $prepared_args );
$query->setDateModified($date_modified);
$query->query();

$users = array();
foreach ( $query->results as $user ) {
$data = $this->prepare_item_for_response( $user, $request );
$users[] = $this->prepare_response_for_collection( $data );
if (isset($request['date_modified']) && $request['date_modified']) {
$filter_date_modified = $request['date_modified'];
}

$response = rest_ensure_response( $users );

// Store pagination values for headers then unset for count query.
$per_page = (int) $prepared_args['number'];
$page = ceil( ( ( (int) $prepared_args['offset'] ) / $per_page ) + 1 );

$prepared_args['fields'] = 'ID';

$total_users = $query->get_total();
if ( $total_users < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $prepared_args['number'] );
unset( $prepared_args['offset'] );
$count_query = new WP_User_Query( $prepared_args );
$total_users = $count_query->get_total();
}
$response->header( 'X-WP-Total', (int) $total_users );
$max_pages = ceil( $total_users / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages );

$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}

return $response;
$value = str_ireplace('T', ' ', $filter_date_modified);

$wp_users_table = $wpdb->users;
$wp_user_meta = $wpdb->usermeta;

$q = "
SELECT ID as id, user_registered as date_created,
CASE
WHEN meta_value IS NOT NULL THEN DATE_SUB(FROM_UNIXTIME(meta_value), INTERVAL %s SECOND)
ELSE user_registered
END AS date_modified
FROM {$wp_users_table} AS users
LEFT JOIN (
SELECT user_id, meta_key, meta1.meta_value
FROM {$wp_user_meta} AS meta1
WHERE meta1.meta_key = 'last_update'
) AS meta ON (users.ID = meta.user_id)
HAVING date_modified > %s
ORDER BY date_modified ASC LIMIT %d
";

$sql = $wpdb->prepare($q, array($timezoneOffset, $value, $limit));
$results = $wpdb->get_results($sql, ARRAY_A);

return $results;
}
}
43 changes: 11 additions & 32 deletions includes/class-globe-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,15 @@ public function init()
});
}

public function get_items($request)
{
$request['after'] = $request['date_modified'];

add_filter('posts_where', function ($query) {
$query = str_replace('post_date', 'post_modified', $query);
return $query;
}, 10, 1);

return parent::get_items($request);
}

public function get_objects($query_args)
{
$query = new WP_Query();
$result = $query->query( $query_args );

$total_posts = $query->found_posts;
if ( $total_posts < 1 ) {
// Out-of-bounds, run the query again without LIMIT for total count.
unset( $query_args['paged'] );
$count_query = new WP_Query();
$count_query->query( $query_args );
$total_posts = $count_query->found_posts;
}

return array(
'objects' => array_map( array( $this, 'get_object' ), $result ),
'total' => (int) $total_posts,
'pages' => (int) ceil( $total_posts / (int) $query->query_vars['posts_per_page'] ),
);
}
/**
* Set alternate default values
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['per_page']['default'] = 50;
$params['order']['default'] = 'asc';
$params['orderby']['default'] = 'modified';
$params['dates_are_gmt']['default'] = true;
return $params;
}
}
58 changes: 55 additions & 3 deletions includes/class-globe.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Globe
{
private $_version = '1.0.3';
private $_version = '1.1.0';
protected static $_instance = null;
protected static $_productInstance = null;

Expand All @@ -18,6 +18,10 @@ public static function instance()

public function __construct()
{
if ( ! $this->isXcoreRequest() ) {
return;
}

$this->includes();
$this->customers();
$this->orders();
Expand All @@ -28,12 +32,18 @@ public function init()
{
add_action('rest_api_init', function () {
register_rest_route('wc-globe/v1', 'version', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'globe_api_version'),
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'globe_api_version'),
'permission_callback' => '__return_true',
));
});
}

public function initHooks()
{
add_filter('woocommerce_rest_shop_order_object_query', [$this, 'xcoreFilterByDateModified'], 10, 2);
}

public function globe_api_version($data)
{
return $this->_version;
Expand All @@ -55,4 +65,46 @@ public function orders()
{
Globe_Orders::instance();
}

/**
* We rely heavily on the ability to retrieve data by its modification date. This
* adds the functionality to do so for both orders and products.
* Since Woocommerce 5.8.0 the option has been added to filter products by modified
* date using modified_after
*
* @param $args
* @param $request
*
* @return array
*/
public function xcoreFilterByDateModified( $args, $request )
{
$args['date_query'][0]['inclusive'] = true;

if ($request->get_param('modified_after')) {
return $args;
}

$objectId = $request->get_param( 'id' );
$date_modified = $request->get_param( 'date_modified' ) ?: '2001-01-01 00:00:00';

if ( $objectId ) {
$args['post__in'][] = $objectId;
}

$args['date_query'][0]['column'] = 'post_modified_gmt';
$args['date_query'][0]['after'] = $date_modified;

return $args;
}

private function isXcoreRequest()
{
if (empty($_SERVER['REQUEST_URI'])) {
return false;
}

$restPrefix = trailingslashit(rest_get_url_prefix());
return (false !== strpos($_SERVER['REQUEST_URI'], $restPrefix . 'wc-globe/'));
}
}

0 comments on commit 1dca5d7

Please sign in to comment.