Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Blocks/Integrat...
File: IntegrationRegistry.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\Blocks\Integrations;
[1] Fix | Delete
[2] Fix | Delete
/**
[3] Fix | Delete
* Class used for tracking registered integrations with various Block types.
[4] Fix | Delete
*/
[5] Fix | Delete
class IntegrationRegistry {
[6] Fix | Delete
/**
[7] Fix | Delete
* Integration identifier is used to construct hook names and is given when the integration registry is initialized.
[8] Fix | Delete
*
[9] Fix | Delete
* @var string
[10] Fix | Delete
*/
[11] Fix | Delete
protected $registry_identifier = '';
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Registered integrations, as `$name => $instance` pairs.
[15] Fix | Delete
*
[16] Fix | Delete
* @var IntegrationInterface[]
[17] Fix | Delete
*/
[18] Fix | Delete
protected $registered_integrations = [];
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Initializes all registered integrations.
[22] Fix | Delete
*
[23] Fix | Delete
* Integration identifier is used to construct hook names and is given when the integration registry is initialized.
[24] Fix | Delete
*
[25] Fix | Delete
* @param string $registry_identifier Identifier for this registry.
[26] Fix | Delete
*/
[27] Fix | Delete
public function initialize( $registry_identifier = '' ) {
[28] Fix | Delete
if ( $registry_identifier ) {
[29] Fix | Delete
$this->registry_identifier = $registry_identifier;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
if ( empty( $this->registry_identifier ) ) {
[33] Fix | Delete
_doing_it_wrong( __METHOD__, esc_html__( 'Integration registry requires an identifier.', 'woocommerce' ), '4.6.0' );
[34] Fix | Delete
return false;
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* Fires when the IntegrationRegistry is initialized.
[39] Fix | Delete
*
[40] Fix | Delete
* Runs before integrations are initialized allowing new integration to be registered for use. This should be
[41] Fix | Delete
* used as the primary hook for integrations to include their scripts, styles, and other code extending the
[42] Fix | Delete
* blocks.
[43] Fix | Delete
*
[44] Fix | Delete
* @since 4.6.0
[45] Fix | Delete
*
[46] Fix | Delete
* @param IntegrationRegistry $this Instance of the IntegrationRegistry class which exposes the IntegrationRegistry::register() method.
[47] Fix | Delete
*/
[48] Fix | Delete
do_action( 'woocommerce_blocks_' . $this->registry_identifier . '_registration', $this );
[49] Fix | Delete
[50] Fix | Delete
foreach ( $this->get_all_registered() as $registered_integration ) {
[51] Fix | Delete
$registered_integration->initialize();
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Registers an integration.
[57] Fix | Delete
*
[58] Fix | Delete
* @param IntegrationInterface $integration An instance of IntegrationInterface.
[59] Fix | Delete
*
[60] Fix | Delete
* @return boolean True means registered successfully.
[61] Fix | Delete
*/
[62] Fix | Delete
public function register( IntegrationInterface $integration ) {
[63] Fix | Delete
$name = $integration->get_name();
[64] Fix | Delete
[65] Fix | Delete
if ( $this->is_registered( $name ) ) {
[66] Fix | Delete
/* translators: %s: Integration name. */
[67] Fix | Delete
_doing_it_wrong( __METHOD__, esc_html( sprintf( __( '"%s" is already registered.', 'woocommerce' ), $name ) ), '4.6.0' );
[68] Fix | Delete
return false;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
$this->registered_integrations[ $name ] = $integration;
[72] Fix | Delete
return true;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Checks if an integration is already registered.
[77] Fix | Delete
*
[78] Fix | Delete
* @param string $name Integration name.
[79] Fix | Delete
* @return bool True if the integration is registered, false otherwise.
[80] Fix | Delete
*/
[81] Fix | Delete
public function is_registered( $name ) {
[82] Fix | Delete
return isset( $this->registered_integrations[ $name ] );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Un-register an integration.
[87] Fix | Delete
*
[88] Fix | Delete
* @param string|IntegrationInterface $name Integration name, or alternatively a IntegrationInterface instance.
[89] Fix | Delete
* @return boolean|IntegrationInterface Returns the unregistered integration instance if unregistered successfully.
[90] Fix | Delete
*/
[91] Fix | Delete
public function unregister( $name ) {
[92] Fix | Delete
if ( $name instanceof IntegrationInterface ) {
[93] Fix | Delete
$name = $name->get_name();
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
if ( ! $this->is_registered( $name ) ) {
[97] Fix | Delete
/* translators: %s: Integration name. */
[98] Fix | Delete
_doing_it_wrong( __METHOD__, esc_html( sprintf( __( 'Integration "%s" is not registered.', 'woocommerce' ), $name ) ), '4.6.0' );
[99] Fix | Delete
return false;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
$unregistered = $this->registered_integrations[ $name ];
[103] Fix | Delete
unset( $this->registered_integrations[ $name ] );
[104] Fix | Delete
return $unregistered;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Retrieves a registered Integration by name.
[109] Fix | Delete
*
[110] Fix | Delete
* @param string $name Integration name.
[111] Fix | Delete
* @return IntegrationInterface|null The registered integration, or null if it is not registered.
[112] Fix | Delete
*/
[113] Fix | Delete
public function get_registered( $name ) {
[114] Fix | Delete
return $this->is_registered( $name ) ? $this->registered_integrations[ $name ] : null;
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
/**
[118] Fix | Delete
* Retrieves all registered integrations.
[119] Fix | Delete
*
[120] Fix | Delete
* @return IntegrationInterface[]
[121] Fix | Delete
*/
[122] Fix | Delete
public function get_all_registered() {
[123] Fix | Delete
return $this->registered_integrations;
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Gets an array of all registered integration's script handles for the editor.
[128] Fix | Delete
*
[129] Fix | Delete
* @return string[]
[130] Fix | Delete
*/
[131] Fix | Delete
public function get_all_registered_editor_script_handles() {
[132] Fix | Delete
$script_handles = [];
[133] Fix | Delete
$registered_integrations = $this->get_all_registered();
[134] Fix | Delete
[135] Fix | Delete
foreach ( $registered_integrations as $registered_integration ) {
[136] Fix | Delete
$script_handles = array_merge(
[137] Fix | Delete
$script_handles,
[138] Fix | Delete
$registered_integration->get_editor_script_handles()
[139] Fix | Delete
);
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
return array_unique( array_filter( $script_handles ) );
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
/**
[146] Fix | Delete
* Gets an array of all registered integration's script handles.
[147] Fix | Delete
*
[148] Fix | Delete
* @return string[]
[149] Fix | Delete
*/
[150] Fix | Delete
public function get_all_registered_script_handles() {
[151] Fix | Delete
$script_handles = [];
[152] Fix | Delete
$registered_integrations = $this->get_all_registered();
[153] Fix | Delete
[154] Fix | Delete
foreach ( $registered_integrations as $registered_integration ) {
[155] Fix | Delete
$script_handles = array_merge(
[156] Fix | Delete
$script_handles,
[157] Fix | Delete
$registered_integration->get_script_handles()
[158] Fix | Delete
);
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
return array_unique( array_filter( $script_handles ) );
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/**
[165] Fix | Delete
* Gets an array of all registered integration's script data.
[166] Fix | Delete
*
[167] Fix | Delete
* @return array
[168] Fix | Delete
*/
[169] Fix | Delete
public function get_all_registered_script_data() {
[170] Fix | Delete
$script_data = [];
[171] Fix | Delete
$registered_integrations = $this->get_all_registered();
[172] Fix | Delete
[173] Fix | Delete
foreach ( $registered_integrations as $registered_integration ) {
[174] Fix | Delete
$script_data[ $registered_integration->get_name() . '_data' ] = $registered_integration->get_script_data();
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
return array_filter( $script_data );
[178] Fix | Delete
}
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function