Skip to content

Commit

Permalink
235: moved Alternative_Addon_Version_Manager functionality to Base_Co…
Browse files Browse the repository at this point in the history
…okiebot_Addon and removed Addon_With_Alternative_Versions_Interface
  • Loading branch information
DavidSingh3 committed Dec 3, 2021
1 parent c255f11 commit 0beb41e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 159 deletions.
7 changes: 3 additions & 4 deletions documentation/how-to-add-new-addon.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ Every addon is contained in its own class.
- Addon classes for third party themes should extend the `Base_Cookiebot_Theme_Addon` abstract class.
- There is also a miscellaneous `Base_Cookiebot_Other_Addon` abstract class, which is used for WordPress core features like embedded videos.

Addon interfaces
Addons with alternative versions
---
Depending on the addon, you can implement several interfaces:
- [Addon_With_Alternative_Versions_Interface](../src/lib/Addon_With_Alternative_Versions_Interface.php) should be implemented by addons that have one or more incompatible previous versions.
- The `get_alternative_addon_versions` method should return an array of strings.
Addons can return a different addon class for each incompatible version.
- The `ALTERNATIVE_ADDON_VERSIONS` class constant should contain an array of strings.
- Each array key should correspond to a valid semver version number of the plugin or theme.
- Each array value should point to the classname of the addon for that previous plugin/theme version.
- One example is the [Custom_Facebook_Feed](../src/addons/controller/addons/custom_facebook_feed/Custom_Facebook_Feed.php) addon, which had to block its cookies in a different manner for [an older version](../src/addons/controller/addons/custom_facebook_feed/Custom_Facebook_Feed_Version_2_17_1.php)
Expand Down
66 changes: 53 additions & 13 deletions src/addons/controller/addons/Base_Cookiebot_Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace cybot\cookiebot\addons\controller\addons;

use cybot\cookiebot\lib\Addon_With_Alternative_Versions_Interface;
use cybot\cookiebot\lib\Alternative_Addon_Version_Manager;
use cybot\cookiebot\lib\buffer\Buffer_Output_Interface;
use cybot\cookiebot\lib\Cookie_Consent_Interface;
use cybot\cookiebot\lib\script_loader_tag\Script_Loader_Tag_Interface;
use cybot\cookiebot\lib\Settings_Service_Interface;
use cybot\cookiebot\lib\traits\Class_Constant_Override_Validator_Trait;
use cybot\cookiebot\lib\traits\Extra_Information_Trait;
use Exception;
use InvalidArgumentException;
use function cybot\cookiebot\lib\cookiebot_addons_output_cookie_types;

abstract class Base_Cookiebot_Addon {
Expand All @@ -25,6 +24,7 @@ abstract class Base_Cookiebot_Addon {
const ENABLE_ADDON_BY_DEFAULT = false;
const SVN_URL_BASE_PATH = '';
const SVN_URL_DEFAULT_SUB_PATH = '';
const ALTERNATIVE_ADDON_VERSIONS = array();

/**
* @var Settings_Service_Interface
Expand Down Expand Up @@ -63,7 +63,7 @@ abstract class Base_Cookiebot_Addon {
* @throws Exception
* @since 1.3.0
*/
public function __construct(
protected function __construct(
Settings_Service_Interface $settings,
Script_Loader_Tag_Interface $script_loader_tag,
Cookie_Consent_Interface $cookie_consent,
Expand All @@ -86,6 +86,7 @@ public function __construct(
'DEFAULT_COOKIE_TYPES',
array( 'necessary', 'marketing', 'statistics', 'preferences' )
);
$this->validate_alternative_addon_versions();
}

/**
Expand All @@ -103,19 +104,58 @@ final public static function get_instance(
Cookie_Consent_Interface $cookie_consent,
Buffer_Output_Interface $buffer_output
) {
$addon_class = static::class;
$addon = new $addon_class( ...func_get_args() );

if ( is_a( $addon, Addon_With_Alternative_Versions_Interface::class ) ) {
$alternative_addon_version_manager = new Alternative_Addon_Version_Manager( ...func_get_args() );
$alternative_addon_version_manager->add_versions( $addon->get_alternative_addon_versions() );
$installed_addon_version = $alternative_addon_version_manager->get_installed_version();
if ( is_a( $installed_addon_version, self::class ) ) {
return $installed_addon_version;
$addon_class = static::class;
$addon_instance = new $addon_class( ...func_get_args() );

$installed_addon_version = $addon_instance->get_installed_version();
if ( is_a( $installed_addon_version, self::class ) ) {
return $installed_addon_version;
}

return $addon_instance;
}

/**
* @throws InvalidArgumentException
*/
final private function validate_alternative_addon_versions() {
foreach ( static::ALTERNATIVE_ADDON_VERSIONS as $version_string => $alternative_version_addon_class ) {
if ( ! version_compare( $version_string, '0.0.1', '>=' ) ) {
throw new InvalidArgumentException( 'Invalid version number "' . $version_string . '"' );
}
if ( ! class_exists( $alternative_version_addon_class ) ) {
throw new InvalidArgumentException( 'Class not found at "' . $alternative_version_addon_class . '"' );
}
if ( ! is_subclass_of( $alternative_version_addon_class, self::class ) ) {
throw new InvalidArgumentException( 'Class "' . $alternative_version_addon_class . '" is not a subclass of "' . self::class . '"' );
}
}
}

return $addon;
/**
* @return Base_Cookiebot_Addon|null
* @throws Exception
*/
final public function get_installed_version() {
$sorted_alternative_addon_versions = static::ALTERNATIVE_ADDON_VERSIONS;
uksort( $sorted_alternative_addon_versions, 'version_compare' );

foreach ( $sorted_alternative_addon_versions as $version_string => $alternative_version_addon_class ) {
/** @var Base_Cookiebot_Addon $alternative_version_addon_instance */
$alternative_version_addon_instance = new $alternative_version_addon_class(
$this->settings,
$this->script_loader_tag,
$this->cookie_consent,
$this->buffer_output
);

if ( $alternative_version_addon_instance->is_addon_installed() ) {
if ( version_compare( $alternative_version_addon_instance->get_version(), $version_string, '<=' ) ) {
return $alternative_version_addon_instance;
}
}
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ abstract class Base_Cookiebot_Plugin_Addon extends Base_Cookiebot_Addon {
* @throws Exception
* @since 1.3.0
*/
public function __construct(
protected function __construct(
Settings_Service_Interface $settings,
Script_Loader_Tag_Interface $script_loader_tag,
Cookie_Consent_Interface $cookie_consent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace cybot\cookiebot\addons\controller\addons\caos_host_analyticsjs_local;

use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Plugin_Addon;
use cybot\cookiebot\lib\Addon_With_Alternative_Versions_Interface;

class CAOS_Host_Analyticsjs_Local extends Base_Cookiebot_Plugin_Addon implements Addon_With_Alternative_Versions_Interface {
class CAOS_Host_Analyticsjs_Local extends Base_Cookiebot_Plugin_Addon {

const ADDON_NAME = 'Complete Analytics Optimization Suite (CAOS)';
const OPTION_NAME = 'caos_host_analyticsjs_local';
Expand All @@ -14,6 +13,10 @@ class CAOS_Host_Analyticsjs_Local extends Base_Cookiebot_Plugin_Addon implements
const PLUGIN_FILE_PATH = 'host-analyticsjs-local/host-analyticsjs-local.php';
const SVN_URL_BASE_PATH = 'https://plugins.svn.wordpress.org/host-analyticsjs-local/trunk/';
const SVN_URL_DEFAULT_SUB_PATH = 'host-analyticsjs-local.php';
const ALTERNATIVE_ADDON_VERSIONS = array(
'1.97' => CAOS_Host_Analyticsjs_Local_Version_1_97::class,
);

/**
* Check for Host Analyticsjs Local action hooks
*
Expand Down Expand Up @@ -84,13 +87,4 @@ public function load_addon_configuration() {
public function cookiebot_addon_host_analyticsjs_local_priority() {
return ( esc_attr( get_option( 'sgal_enqueue_order' ) ) ) ? esc_attr( get_option( 'sgal_enqueue_order' ) ) : 0;
}

/**
* @return array
*/
public function get_alternative_addon_versions() {
return array(
'1.97' => CAOS_Host_Analyticsjs_Local_Version_1_97::class,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
namespace cybot\cookiebot\addons\controller\addons\custom_facebook_feed;

use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Plugin_Addon;
use cybot\cookiebot\lib\Addon_With_Alternative_Versions_Interface;

class Custom_Facebook_Feed extends Base_Cookiebot_Plugin_Addon implements Addon_With_Alternative_Versions_Interface {
class Custom_Facebook_Feed extends Base_Cookiebot_Plugin_Addon {

const ADDON_NAME = 'Custom Facebook Feed';
const OPTION_NAME = 'custom_facebook_feed';
Expand All @@ -14,6 +13,9 @@ class Custom_Facebook_Feed extends Base_Cookiebot_Plugin_Addon implements Addon_
const DEFAULT_PLACEHOLDER_CONTENT = 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.';
const SVN_URL_BASE_PATH = 'https://plugins.svn.wordpress.org/custom-facebook-feed/trunk/';
const SVN_URL_DEFAULT_SUB_PATH = 'custom-facebook-feed.php';
const ALTERNATIVE_ADDON_VERSIONS = array(
'2.17.1' => Custom_Facebook_Feed_Version_2_17_1::class,
);

public function load_addon_configuration() {
if ( class_exists( '\CustomFacebookFeed\Custom_Facebook_Feed' ) ) {
Expand All @@ -35,13 +37,4 @@ public function load_addon_configuration() {
}
}
}

/**
* @return array
*/
public function get_alternative_addon_versions() {
return array(
'2.17.1' => Custom_Facebook_Feed_Version_2_17_1::class,
);
}
}
10 changes: 0 additions & 10 deletions src/lib/Addon_With_Alternative_Versions_Interface.php

This file was deleted.

109 changes: 0 additions & 109 deletions src/lib/Alternative_Addon_Version_Manager.php

This file was deleted.

0 comments on commit 0beb41e

Please sign in to comment.