'Count where the shop\'s product attributes stand (read only): attributes with no value, on one product, values in no filter, names stored twice.',
'synopsis' => array(
array( 'type' => 'flag', 'name' => 'fresh', 'optional' => true, 'description' => 'Count again instead of reading the stored report.' ),
array( 'type' => 'assoc', 'name' => 'format', 'optional' => true, 'default' => 'table', 'options' => array( 'table', 'json' ) ),
),
)
);
}
}
public static function cli( $args, $assoc ) {
self::load();
AIEO_Attribute_Audit::cli( $args, $assoc );
}
private static function load() {
if ( ! class_exists( 'AIEO_Attribute_Audit', false ) ) {
require_once AIEO_DIR . 'includes/class-aieo-attribute-audit.php';
}
}
private function report() {
self::load();
$fresh = isset( $_GET['aieo_audit_recount'], $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( (string) $_GET['_wpnonce'] ) ), self::RECOUNT );
return AIEO_Attribute_Audit::report( (bool) $fresh );
}
private static function share( $part, $whole ) {
return $whole > 0 ? (int) round( 100 * $part / $whole ) : 0;
}
private static function source_name( $source ) {
if ( 'fibofilters' === $source ) {
return 'FiboFilters';
}
return 'theme' === $source ? __( 'Theme', 'ai-eshop-optimizer' ) : (string) $source;
}
public function render_subtab() {
$report = $this->report();
$s = $report['summary'];
if ( 0 === (int) $s['attributes'] && 0 === (int) $s['unregistered'] ) {
list( $open, $close ) = AIEO_DMM_UI::card(
array(
'anchor' => 'attribute-audit-summary',
'icon' => 'list-checks',
'title' => __( 'Where the attributes stand', 'ai-eshop-optimizer' ),
)
);
echo $open;
echo \Rooster\Admin\States::state( 'first-use', array( 'compact' => true, 'title' => __( 'This shop has no product attributes yet.', 'ai-eshop-optimizer' ), 'text' => __( 'Attributes are made under Products, Attributes. Once products carry them, this screen counts where they stand.', 'ai-eshop-optimizer' ) ) );
echo $close;
return;
}
$this->render_summary( $report );
$this->render_attributes( $report );
$this->render_categories( $report );
$this->render_names( $report );
}
private function render_summary( array $report ) {
$s = $report['summary'];
$n = static fn ( $v ): string => number_format_i18n( (int) $v );
$when = wp_date( (string) get_option( 'date_format', 'j M Y' ) . ' ' . (string) get_option( 'time_format', 'H:i' ), (int) $report['at'] );
$url = wp_nonce_url( add_query_arg( 'aieo_audit_recount', '1', $this->get_url() ), self::RECOUNT );
list( $open, $close ) = AIEO_DMM_UI::card(
array(
'anchor' => 'attribute-audit-summary',
'icon' => 'list-checks',
'title' => __( 'Where the attributes stand', 'ai-eshop-optimizer' ),
'description' => __( 'A count of the shop\'s product attributes and their values as they are: how many products carry each attribute, which values a filter can find today, and which no product carries. It only reads. Nothing here changes an attribute, a value or a swatch.', 'ai-eshop-optimizer' ),
'footnote' => sprintf( __( 'Counted on %s.', 'ai-eshop-optimizer' ), $when ),
'tools' => \Rooster\Admin\Ui::button( __( 'Count again', 'ai-eshop-optimizer' ), array( 'size' => 'sm', 'icon' => 'refresh-cw', 'href' => $url ) ),
)
);
echo $open;
$in_use = sprintf( __( 'of the %s in use', 'ai-eshop-optimizer' ), $n( $s['in_use'] ) );
$share = static fn ( $part ): string => sprintf( __( '%d%% of the values in use', 'ai-eshop-optimizer' ), self::share( $part, $s['values_used'] ) );
$tiles = array(
array( __( 'Attributes', 'ai-eshop-optimizer' ), $n( $s['attributes'] ), '', __( 'The product attributes of the shop (Products, Attributes), whether a product carries them or not.', 'ai-eshop-optimizer' ) ),
array( __( 'Carried by a product', 'ai-eshop-optimizer' ), $n( $s['in_use'] ), '', __( 'Attributes with at least one value on at least one product. Only these can filter anything.', 'ai-eshop-optimizer' ) ),
array( __( 'On one product only', 'ai-eshop-optimizer' ), $n( $s['one_product'] ), $in_use, __( 'Attributes a single product carries: usually one product line\'s own shades or sizes. As a filter, each would list one product\'s values.', 'ai-eshop-optimizer' ) ),
array( __( 'Values in use', 'ai-eshop-optimizer' ), $n( $s['values_used'] ), '', __( 'Attribute values that at least one product carries.', 'ai-eshop-optimizer' ) ),
array( __( 'Values in no filter', 'ai-eshop-optimizer' ), $n( $s['values_not_in_filters'] ), $share( $s['values_not_in_filters'] ), __( 'Values in use whose attribute is not a filter on the shop today. A visitor cannot filter by them; they show only on their product.', 'ai-eshop-optimizer' ) ),
array( __( 'Values without a group', 'ai-eshop-optimizer' ), $n( $s['ungrouped'] ), $share( $s['ungrouped'] ), __( 'Values in use that belong to no group yet. A group connects values of many attributes, such as every line\'s blondes, into one filter choice.', 'ai-eshop-optimizer' ) ),
);
echo '
';
foreach ( $tiles as $tile ) {
echo \Rooster\Admin\Ui::stat( $tile[0], $tile[1], array_filter( array( 'hint' => $tile[2], 'tip' => $tile[3] ) ) );
}
echo '
';
$by = array();
foreach ( array_count_values( wp_list_pluck( $report['attributes'], 'filter' ) ) as $source => $count ) {
if ( '' !== (string) $source ) {
$by[] = self::source_name( (string) $source ) . ': ' . $n( $count );
}
}
$facts = array(
array( 'label' => __( 'Published products', 'ai-eshop-optimizer' ), 'value' => $n( $s['products'] ) ),
array( 'label' => __( 'Published variations', 'ai-eshop-optimizer' ), 'value' => $n( $s['variations'] ) ),
array( 'label' => __( 'Attributes with no value', 'ai-eshop-optimizer' ), 'value' => $n( $s['no_value'] ), 'tip' => __( 'Attributes that hold no value at all. They do nothing, and can be removed under Products, Attributes.', 'ai-eshop-optimizer' ) ),
array( 'label' => __( 'Attributes no product carries', 'ai-eshop-optimizer' ), 'value' => $n( $s['unused'] ), 'tip' => __( 'Attributes that hold values, none of them on a product.', 'ai-eshop-optimizer' ) ),
array( 'label' => __( 'Values no product carries', 'ai-eshop-optimizer' ), 'value' => $n( $s['values_unused'] ) ),
array( 'label' => __( 'Values on one product only', 'ai-eshop-optimizer' ), 'value' => sprintf( __( '%1$s, in %2$s attributes', 'ai-eshop-optimizer' ), $n( $s['one_product_values'] ), $n( $s['one_product'] ) ) ),
array( 'label' => __( 'Attributes that are a filter today', 'ai-eshop-optimizer' ), 'value' => $n( $s['filters'] ) . ( $by ? ' (' . implode( ', ', $by ) . ')' : '' ), 'tip' => __( 'The attributes FiboFilters or the theme\'s shop filters offer as a filter.', 'ai-eshop-optimizer' ) ),
array( 'label' => __( 'Filters whose attribute no product carries', 'ai-eshop-optimizer' ), 'value' => $n( $s['filters_unused'] ), 'tone' => $s['filters_unused'] > 0 ? 'warning' : '' ),
array( 'label' => __( 'Attributes marked as a colour', 'ai-eshop-optimizer' ), 'value' => $n( $s['colour_flagged'] ), 'tip' => __( 'Attributes with Rooster One\'s colour mark (Swatches). A variation takes its colour from such an attribute.', 'ai-eshop-optimizer' ) ),
array( 'label' => __( 'Values with a swatch', 'ai-eshop-optimizer' ), 'value' => $n( $s['swatches'] ) ),
);
if ( $s['unregistered'] > 0 ) {
$facts[] = array( 'label' => __( 'Attribute taxonomies in no list', 'ai-eshop-optimizer' ), 'value' => $n( $s['unregistered'] ), 'tone' => 'warning', 'tip' => __( 'Values left in the database under an attribute that is no longer in Products, Attributes.', 'ai-eshop-optimizer' ) );
}
echo \Rooster\Admin\Ui::facts( $facts, array( 'columns' => 2 ) );
echo $close;
}
private function render_attributes( array $report ) {
list( $open, $close ) = AIEO_DMM_UI::card(
array(
'anchor' => 'attribute-audit-attributes',
'icon' => 'tags',
'title' => __( 'The attributes', 'ai-eshop-optimizer' ),
'description' => __( 'Each attribute with its values, how many of them a product carries, how many products carry the attribute, and whether it is a filter on the shop today.', 'ai-eshop-optimizer' ),
'flush' => true,
)
);
echo $open;
$attributes = $report['attributes'];
uasort(
$attributes,
static function ( $a, $b ) {
return array( $b['products'], $b['values_used'], $a['taxonomy'] ) <=> array( $a['products'], $a['values_used'], $b['taxonomy'] );
}
);
$rows = array();
foreach ( $attributes as $a ) {
$words = array();
if ( ! $a['registered'] ) {
$words[] = array( __( 'In no list', 'ai-eshop-optimizer' ), 'warning' );
}
if ( 0 === $a['values'] ) {
$words[] = array( __( 'No value', 'ai-eshop-optimizer' ), 'warning' );
} elseif ( 0 === $a['products'] ) {
$words[] = array( __( 'On no product', 'ai-eshop-optimizer' ), 'warning' );
} elseif ( 1 === $a['products'] ) {
$words[] = array( __( 'One product', 'ai-eshop-optimizer' ), 'mono' );
}
if ( $a['colour_flag'] ) {
$words[] = array( __( 'Colour', 'ai-eshop-optimizer' ), 'info' );
}
$notes = '';
foreach ( $words as $word ) {
$notes .= \Rooster\Admin\Ui::badge( $word[0], $word[1], array( 'sm' => true ) ) . ' ';
}
$plain = implode( ', ', wp_list_pluck( $words, 0 ) );
$source = '' !== $a['filter'] ? self::source_name( $a['filter'] ) : '';
$rows[] = array(
'_id' => $a['taxonomy'],
'_name' => $a['label'],
'_text' => $a['label'] . ' ' . $a['taxonomy'] . ' ' . $a['type'] . ' ' . $source . ' ' . $plain,
'_s' => array( 'filter' => $source, 'notes' => $plain ),
'label' => $a['label'],
'taxonomy' => $a['taxonomy'],
'type' => $a['type'],
'values' => (int) $a['values'],
'used' => (int) $a['values_used'],
'products' => (int) $a['products'],
'swatches' => (int) $a['swatches'],
'filter' => '' !== $source ? \Rooster\Admin\Ui::badge( $source, 'success', array( 'sm' => true ) ) : '',
'notes' => trim( $notes ),
);
}
echo AIEO_Table::local(
'aieo-attribute-audit-attributes',
array(
array( 'key' => 'label', 'label' => __( 'Attribute', 'ai-eshop-optimizer' ) ),
array( 'key' => 'taxonomy', 'label' => __( 'Taxonomy', 'ai-eshop-optimizer' ), 'type' => 'code', 'priority' => 'low' ),
array( 'key' => 'type', 'label' => __( 'Type', 'ai-eshop-optimizer' ), 'priority' => 'low' ),
array( 'key' => 'values', 'label' => __( 'Values', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'used', 'label' => __( 'In use', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'products', 'label' => __( 'Products', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'swatches', 'label' => __( 'Swatches', 'ai-eshop-optimizer' ), 'type' => 'num', 'priority' => 'low' ),
array( 'key' => 'filter', 'label' => __( 'Filter', 'ai-eshop-optimizer' ), 'type' => 'html' ),
array( 'key' => 'notes', 'label' => __( 'Notes', 'ai-eshop-optimizer' ), 'type' => 'html' ),
),
$rows,
array(
'prefix' => 'aaa_',
'sortable' => true,
'sort' => array( 'key' => 'products', 'dir' => 'desc' ),
'dense' => true,
'caption' => __( 'The attributes', 'ai-eshop-optimizer' ),
'label' => __( 'Attributes', 'ai-eshop-optimizer' ),
'search' => array( 'label' => __( 'Search the attributes', 'ai-eshop-optimizer' ) ),
)
);
echo $close;
}
private function render_categories( array $report ) {
if ( ! $report['categories'] ) {
return;
}
list( $open, $close ) = AIEO_DMM_UI::card(
array(
'anchor' => 'attribute-audit-categories',
'icon' => 'folder-tree',
'title' => __( 'By category', 'ai-eshop-optimizer' ),
'description' => __( 'The largest product categories, and the attributes their products carry. A category\'s filters can offer every attribute counted here; the ones on one product only would each list a single product\'s values.', 'ai-eshop-optimizer' ),
'flush' => true,
)
);
echo $open;
$rows = array();
foreach ( $report['categories'] as $c ) {
$rows[] = array(
'_id' => (string) $c['term_id'],
'_name' => $c['name'],
'name' => $c['name'],
'products' => (int) $c['products'],
'attributes' => (int) $c['attributes'],
'one_product' => (int) $c['one_product'],
'filters' => (int) $c['filters'],
);
}
echo AIEO_Table::local(
'aieo-attribute-audit-categories',
array(
array( 'key' => 'name', 'label' => __( 'Category', 'ai-eshop-optimizer' ) ),
array( 'key' => 'products', 'label' => __( 'Products', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'attributes', 'label' => __( 'Attributes in use', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'one_product', 'label' => __( 'On one product only', 'ai-eshop-optimizer' ), 'type' => 'num' ),
array( 'key' => 'filters', 'label' => __( 'Filters today', 'ai-eshop-optimizer' ), 'type' => 'num' ),
),
$rows,
array(
'prefix' => 'aac_',
'size' => 0,
'sortable' => true,
'sort' => array( 'key' => 'products', 'dir' => 'desc' ),
'dense' => true,
'caption' => __( 'By category', 'ai-eshop-optimizer' ),
)
);
echo $close;
}
private function render_names( array $report ) {
$f = $report['findings'];
$label = static function ( string $tax ) use ( $report ): string {
return isset( $report['attributes'][ $tax ] ) ? (string) $report['attributes'][ $tax ]['label'] : $tax;
};
list( $open, $close ) = AIEO_DMM_UI::card(
array(
'anchor' => 'attribute-audit-names',
'icon' => 'a-large-small',
'title' => __( 'Names to look at', 'ai-eshop-optimizer' ),
'description' => __( 'Values a person reads as one, while the shop stores them apart: the same name twice in one attribute, two spellings of one word, a name typed with look-alike letters of another alphabet, and a name that lives in several attributes. The first examples of each are listed.', 'ai-eshop-optimizer' ),
)
);
echo $open;
$n = static fn ( $v ): string => number_format_i18n( (int) $v );
$kinds = array(
'stored_twice' => array( __( 'Stored twice', 'ai-eshop-optimizer' ), __( 'One attribute holds the same name more than once.', 'ai-eshop-optimizer' ), 'warning' ),
'spellings' => array( __( 'Two spellings', 'ai-eshop-optimizer' ), __( 'One attribute holds one word in more than one spelling: an accent, a hyphen, a look-alike letter.', 'ai-eshop-optimizer' ), 'warning' ),
'lookalikes' => array( __( 'Look-alike letters', 'ai-eshop-optimizer' ), __( 'A name mixes Greek and Latin letters in one word, or equals another name only once its Greek letters are read as their Latin twins.', 'ai-eshop-optimizer' ), 'warning' ),
'equal_names' => array( __( 'In several attributes', 'ai-eshop-optimizer' ), __( 'A name that more than one attribute holds. It may be one value, as a colour word is, or as many as the attributes, as a shade number is.', 'ai-eshop-optimizer' ), 'mono' ),
);
$facts = array();
foreach ( $kinds as $kind => $words ) {
$facts[] = array( 'label' => $words[0], 'value' => sprintf( __( '%1$s names, %2$s values', 'ai-eshop-optimizer' ), $n( $f[ $kind ]['count'] ), $n( $f[ $kind ]['values'] ) ), 'tip' => $words[1], 'tone' => 'warning' === $words[2] && $f[ $kind ]['count'] > 0 ? 'warning' : '' );
}
echo \Rooster\Admin\Ui::facts( $facts, array( 'columns' => 2 ) );
$rows = array();
foreach ( $kinds as $kind => $words ) {
foreach ( $f[ $kind ]['examples'] as $i => $e ) {
if ( 'equal_names' === $kind ) {
$where = sprintf( __( '%s attributes', 'ai-eshop-optimizer' ), $n( $e['attributes'] ) );
$names = (string) $e['name'];
} else {
$where = $label( (string) $e['taxonomy'] );
$names = implode( ' ยท ', array_map( 'strval', (array) $e['names'] ) );
}
$rows[] = array(
'_id' => $kind . '-' . $i,
'_name' => $names,
'_text' => $words[0] . ' ' . $where . ' ' . $names,
'_s' => array( 'kind' => $words[0] ),
'kind' => \Rooster\Admin\Ui::badge( $words[0], $words[2], array( 'sm' => true ) ),
'where' => $where,
'names' => $names,
);
}
}
echo AIEO_Table::local(
'aieo-attribute-audit-names',
array(
array( 'key' => 'kind', 'label' => __( 'What', 'ai-eshop-optimizer' ), 'type' => 'html' ),
array( 'key' => 'where', 'label' => __( 'Where', 'ai-eshop-optimizer' ) ),
array( 'key' => 'names', 'label' => __( 'Names', 'ai-eshop-optimizer' ) ),
),
$rows,
array(
'prefix' => 'aan_',
'sortable' => true,
'dense' => true,
'caption' => __( 'Names to look at', 'ai-eshop-optimizer' ),
'label' => __( 'Names', 'ai-eshop-optimizer' ),
'empty' => \Rooster\Admin\States::state( 'all-done', array( 'compact' => true, 'title' => __( 'No such name was found.', 'ai-eshop-optimizer' ), 'text' => __( 'No value is stored twice, spelt two ways or typed with look-alike letters, and no name lives in two attributes.', 'ai-eshop-optimizer' ) ) ),
'search' => array( 'label' => __( 'Search the names', 'ai-eshop-optimizer' ) ),
)
);
echo $close;
}
}