Skip to content

Commit

Permalink
feat(ATDS): Added Attribute Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
seebeen committed Feb 5, 2024
1 parent a1808c7 commit 2a5a1fe
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 13 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"\\": "src/Interfaces/Shared"
},
"files": [
"src/Utils/wc-utils-att-functions.php",
"src/Utils/woocommerce-utils-functions.php"
]
},
Expand Down
107 changes: 107 additions & 0 deletions src/Data/Attribute_Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Attribute_Factory class file.
*
* @package WooCommerce Utils
* @subpackage Data
*/

namespace Oblak\WooCommerce\Data;

use Oblak\WP\Traits\Singleton;
use WC_Data_Store;
use WC_Product_Attribute;

/**
* Standardized methods for attribute taxonomy data.
*/
class Attribute_Factory {
use Singleton;

/**
* Get an Attribute Taxonomy object
*
* @param string|int|WC_Product_Attribute|false $attribute_id Attribute ID, Attribute object or Attribute name / slug.
* @return Attribute_Taxonomy|false
*/
public function get_attribute( $attribute_id = false ): Attribute_Taxonomy|false {
$attribute_id = $this->get_attribute_id( $attribute_id );

if ( ! $attribute_id ) {
return false;
}

$classname = self::get_attribute_classname( $attribute_id );

try {
return new $classname( $attribute_id );
} catch ( \Exception ) {
return false;
}
}

/**
* Get the attribute class name
*
* @param int $attribute_id Attribute ID.
* @return class-string Class name
*/
public static function get_attribute_classname( int $attribute_id ): string {
/**
* Filters the attribute class name
*
* @param class-string $classname Class name.
* @param int $attribute_id Attribute ID.
* @return class-string
*
* @since 1.30.0
*/
$classname = \apply_filters( 'woocommerce_attribute_class', Attribute_Taxonomy::class, $attribute_id );

if ( ! $classname || ! \class_exists( $classname ) ) {
$classname = Attribute_Taxonomy::class;
}

return $classname;
}

/**
* Determines the attribute ID
*
* @param string|int|WC_Product_Attribute|false $attribute_id Attribute ID, Attribute object or Attribute name / slug.
* @return int|false
*/
protected function get_attribute_id( string|int|WC_Product_Attribute|false $attribute_id ): int|false {
return match ( true ) {
default => false,
\is_numeric( $attribute_id ) => (int) $attribute_id,
$attribute_id instanceof WC_Product_Attribute => $attribute_id->get_id(),
$attribute_id instanceof Attribute_Taxonomy => $attribute_id->get_id(),
\is_string( $attribute_id ) => $this->get_attribute_by_string( $attribute_id ),

};
}

/**
* Get the attribute ID by name or label
*
* @param string $ident Attribute name / slug.
* @return int
*/
public function get_attribute_by_string( string $ident ): int {
$id = \wc_attribute_taxonomy_id_by_name( $ident );

if ( $id ) {
return $id;
}

return (int) WC_Data_Store::load( 'attribute_taxonomy' )->get_entities(
array(
'label' => $ident,
'per_page' => 1,
'return' => 'ids',
),
'OR',
);
}
}
17 changes: 4 additions & 13 deletions src/Data/Attribute_Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,10 @@ class Attribute_Taxonomy extends Extended_Data {
* @param string $label Label.
*/
public static function from_label( string $label ): static {
$ds = \WC_Data_Store::load( 'attribute_taxonomy' );

// phpcs:ignore SlevomatCodingStandard.Functions.RequireSingleLineCall.RequiredSingleLineCall
$id = (int) $ds->get_entities(
array(
'label' => $label,
'per_page' => 1,
'return' => 'ids',
),
);
$att = new static( $id );

if ( 0 === $att->get_id() ) {
$att = \wc_get_attribute_taxonomy( $label );

if ( ! $att ) {
$att = \wc_get_attribute_taxonomy_object( 0 );
$att->set_label( $label );
$att->save();
}
Expand Down
54 changes: 54 additions & 0 deletions src/Utils/wc-utils-att-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Attribute Taxonomy functions
*
* @package WooCommerce Utils
*/

use Oblak\WooCommerce\Data\Attribute_Factory;
use Oblak\WooCommerce\Data\Attribute_Taxonomy;

/**
* Get an attribute taxonomy object
*
* @param int|WC_Product_Attribute|string|false $attribute_id Attribute ID.
* @return Attribute_Taxonomy|false
*/
function wc_get_attribute_taxonomy( $attribute_id ): Attribute_Taxonomy|false {
if (
! did_action( 'woocommerce_init' ) ||
! did_action( 'woocommerce_after_register_taxonomy' ) ||
! did_action( 'woocommerce_after_register_post_type' )
) {

wc_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: wc_get_product 2: woocommerce_init 3: woocommerce_after_register_taxonomy 4: woocommerce_after_register_post_type */
__(
'%1$s should not be called before the %2$s, %3$s and %4$s actions have finished.',
'woocommerce',
),
'wc_get_attribute_taxonomy',
'woocommerce_init',
'woocommerce_after_register_taxonomy',
'woocommerce_after_register_post_type',
),
'3.9',
);
return false;
}

return Attribute_Factory::instance()->get_attribute( $attribute_id );
}

/**
* Get an attribute taxonomy object
*
* @param int $attribute_id Attribute ID.
*/
function wc_get_attribute_taxonomy_object( int $attribute_id ) {
$classname = Attribute_Factory::get_attribute_classname( $attribute_id );

return new $classname( $attribute_id );
}

0 comments on commit 2a5a1fe

Please sign in to comment.