Skip to content

Commit

Permalink
Refactor remote specs structure and naming (#45547)
Browse files Browse the repository at this point in the history
* Deprecate DataSourcePoller

* Deprecate and move all rule processors and transformers

* Lint

* More deprecation

* Remove extra line

* Update deprecated class to not produce too many messages by limiting to unique messages

* Changelog

* Update all dependency uses, move TransformerService and TransformerInterface to Transformers package

* More dependency update

* Changelog

* Fix wrong file reference

* Lint markdown

* Lint markdown

* Add unsaved file

* Delete unused file and more lint

* More lint

* Ugh ignore faulty lint rule

* Rename variables for lint
  • Loading branch information
ilyasfoo committed Mar 19, 2024
1 parent 7214a1f commit 5485665
Show file tree
Hide file tree
Showing 135 changed files with 4,249 additions and 2,466 deletions.
Expand Up @@ -2,7 +2,7 @@

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;

register_woocommerce_admin_test_helper_rest_route(
'/remote-spec-validator/validate',
Expand Down
@@ -0,0 +1,4 @@
Significance: patch
Type: dev

Update import path
@@ -0,0 +1,4 @@
Significance: minor
Type: dev

Refactor remote specs structure and naming
3 changes: 2 additions & 1 deletion plugins/woocommerce/includes/admin/class-wc-admin-addons.php
Expand Up @@ -8,6 +8,7 @@

use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications as PromotionRuleEngine;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -1075,7 +1076,7 @@ public static function output() {
// Check for existence of promotions and evaluate out if we should show them.
if ( ! empty( $promotions ) ) {
foreach ( $promotions as $promo_id => $promotion ) {
$evaluator = new PromotionRuleEngine\RuleEvaluator();
$evaluator = new RuleEvaluator();
$passed = $evaluator->evaluate( $promotion->rules );
if ( ! $passed ) {
unset( $promotions[ $promo_id ] );
Expand Down
246 changes: 31 additions & 215 deletions plugins/woocommerce/src/Admin/DataSourcePoller.php
Expand Up @@ -2,55 +2,29 @@

namespace Automattic\WooCommerce\Admin;

use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller as RemoteSpecsDataSourcePoller;

/**
* Specs data source poller class.
* This handles polling specs from JSON endpoints, and
* stores the specs in to the database as an option.
*
* @deprecated since 8.8.0
*/
abstract class DataSourcePoller {

/**
* Get class instance.
*/
abstract public static function get_instance();

/**
* Name of data sources filter.
*/
const FILTER_NAME = 'data_source_poller_data_sources';

/**
* Name of data source specs filter.
*/
const FILTER_NAME_SPECS = 'data_source_poller_specs';

/**
* Id of DataSourcePoller.
*
* @var string
*/
protected $id = array();

abstract class DataSourcePoller extends RemoteSpecsDataSourcePoller {
/**
* Default data sources array.
*
* @var array
*/
protected $data_sources = array();

/**
* Default args.
*
* @var array
* Log a deprecation to the error log.
*/
protected $args = array();

/**
* The logger instance.
*
* @var WC_Logger|null
*/
protected static $logger = null;
private static function log_deprecation() {
error_log( // phpcs:ignore
sprintf(
'%1$s is deprecated since version %2$s! Use %3$s instead.',
self::class,
'8.8.0',
'Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller'
)
);
}

/**
* Constructor.
Expand All @@ -60,211 +34,53 @@ abstract public static function get_instance();
* @param array $args Options for DataSourcePoller.
*/
public function __construct( $id, $data_sources = array(), $args = array() ) {
$this->data_sources = $data_sources;
$this->id = $id;

$arg_defaults = array(
'spec_key' => 'id',
'transient_name' => 'woocommerce_admin_' . $id . '_specs',
'transient_expiry' => 7 * DAY_IN_SECONDS,
);
$this->args = wp_parse_args( $args, $arg_defaults );
}

/**
* Get the logger instance.
*
* @return WC_Logger
*/
protected static function get_logger() {
if ( is_null( self::$logger ) ) {
self::$logger = wc_get_logger();
}

return self::$logger;
}

/**
* Returns the key identifier of spec, this can easily be overwritten. Defaults to id.
*
* @param mixed $spec a JSON parsed spec coming from the JSON feed.
* @return string|boolean
*/
protected function get_spec_key( $spec ) {
$key = $this->args['spec_key'];
if ( isset( $spec->$key ) ) {
return $spec->$key;
}
return false;
self::log_deprecation();
parent::__construct( $id, $data_sources, $args );
}

/**
* Reads the data sources for specs and persists those specs.
*
* @deprecated 8.8.0
* @return array list of specs.
*/
public function get_specs_from_data_sources() {
$locale = get_user_locale();
$specs_group = get_transient( $this->args['transient_name'] ) ?? array();
$specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array();

if ( ! is_array( $specs ) || empty( $specs ) ) {
$this->read_specs_from_data_sources();
$specs_group = get_transient( $this->args['transient_name'] );
$specs = isset( $specs_group[ $locale ] ) ? $specs_group[ $locale ] : array();
}
$specs = apply_filters( self::FILTER_NAME_SPECS, $specs, $this->id );
return $specs !== false ? $specs : array();
self::log_deprecation();
return parent::get_specs_from_data_sources();
}

/**
* Reads the data sources for specs and persists those specs.
*
* @deprecated 8.8.0
* @return bool Whether any specs were read.
*/
public function read_specs_from_data_sources() {
$specs = array();
$data_sources = apply_filters( self::FILTER_NAME, $this->data_sources, $this->id );

// Note that this merges the specs from the data sources based on the
// id - last one wins.
foreach ( $data_sources as $url ) {
$specs_from_data_source = self::read_data_source( $url );
$this->merge_specs( $specs_from_data_source, $specs, $url );
}

$specs_group = get_transient( $this->args['transient_name'] );
$specs_group = is_array( $specs_group ) ? $specs_group : array();
$locale = get_user_locale();
$specs_group[ $locale ] = $specs;
// Persist the specs as a transient.
$this->set_specs_transient(
$specs_group,
$this->args['transient_expiry']
);
return count( $specs ) !== 0;
self::log_deprecation();
return parent::read_specs_from_data_sources();
}

/**
* Delete the specs transient.
*
* @deprecated 8.8.0
* @return bool success of failure of transient deletion.
*/
public function delete_specs_transient() {
return delete_transient( $this->args['transient_name'] );
self::log_deprecation();
return parent::delete_specs_transient();
}

/**
* Set the specs transient.
*
* @param array $specs The specs to set in the transient.
* @param int $expiration The expiration time for the transient.
*/
public function set_specs_transient( $specs, $expiration = 0 ) {
set_transient(
$this->args['transient_name'],
$specs,
$expiration,
);
}

/**
* Read a single data source and return the read specs
*
* @param string $url The URL to read the specs from.
*
* @return array The specs that have been read from the data source.
*/
protected static function read_data_source( $url ) {
$logger_context = array( 'source' => $url );
$logger = self::get_logger();
$response = wp_remote_get(
add_query_arg(
'locale',
get_user_locale(),
$url
),
array(
'user-agent' => 'WooCommerce/' . WC_VERSION . '; ' . home_url( '/' ),
)
);

if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
$logger->error(
'Error getting data feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $response, true ), $logger_context );

return array();
}

$body = $response['body'];
$specs = json_decode( $body );

if ( $specs === null ) {
$logger->error(
'Empty response in data feed',
$logger_context
);

return array();
}

if ( ! is_array( $specs ) ) {
$logger->error(
'Data feed is not an array',
$logger_context
);

return array();
}

return $specs;
}

/**
* Merge the specs.
*
* @param Array $specs_to_merge_in The specs to merge in to $specs.
* @param Array $specs The list of specs being merged into.
* @param string $url The url of the feed being merged in (for error reporting).
* @deprecated 8.8.0
*/
protected function merge_specs( $specs_to_merge_in, &$specs, $url ) {
foreach ( $specs_to_merge_in as $spec ) {
if ( ! $this->validate_spec( $spec, $url ) ) {
continue;
}

$id = $this->get_spec_key( $spec );
$specs[ $id ] = $spec;
}
}

/**
* Validate the spec.
*
* @param object $spec The spec to validate.
* @param string $url The url of the feed that provided the spec.
*
* @return bool The result of the validation.
*/
protected function validate_spec( $spec, $url ) {
$logger = self::get_logger();
$logger_context = array( 'source' => $url );

if ( ! $this->get_spec_key( $spec ) ) {
$logger->error(
'Spec is invalid because the id is missing in feed',
$logger_context
);
// phpcs:ignore
$logger->error( print_r( $spec, true ), $logger_context );

return false;
}

return true;
public function set_specs_transient( $specs, $expiration = 0 ) {
self::log_deprecation();
return parent::set_specs_transient( $specs, $expiration );
}
}
24 changes: 17 additions & 7 deletions plugins/woocommerce/src/Admin/DeprecatedClassFacade.php
Expand Up @@ -47,6 +47,12 @@ class DeprecatedClassFacade {
*/
protected static $deprecated_in_version = '';

/**
* Static array of logged messages.
*
* @var array
*/
private static $logged_messages = array();

/**
* Constructor.
Expand All @@ -61,14 +67,18 @@ public function __construct() {
* @param string $function The name of the deprecated function being called.
*/
private static function log_deprecation( $function ) {
error_log( // phpcs:ignore
sprintf(
'%1$s is deprecated since version %2$s! Use %3$s instead.',
static::class . '::' . $function,
static::$deprecated_in_version,
static::$facade_over_classname . '::' . $function
)
$message = sprintf(
'%1$s is deprecated since version %2$s! Use %3$s instead.',
static::class . '::' . $function,
static::$deprecated_in_version,
static::$facade_over_classname . '::' . $function
);

// Only log when the message has not been logged before.
if ( ! in_array( $message, self::$logged_messages, true ) ) {
error_log( $message ); // phpcs:ignore
self::$logged_messages[] = $message;
}
}

/**
Expand Down
Expand Up @@ -2,7 +2,7 @@

namespace Automattic\WooCommerce\Admin\Features\MarketingRecommendations;

use Automattic\WooCommerce\Admin\DataSourcePoller;
use Automattic\WooCommerce\Admin\RemoteSpecs\DataSourcePoller;

/**
* Specs data source poller class for marketing recommendations.
Expand Down
Expand Up @@ -7,7 +7,7 @@

defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;

/**
* Evaluates the spec and returns the evaluated suggestion.
Expand Down

0 comments on commit 5485665

Please sign in to comment.