true, ); const RATE_PER_MIN = 60; const ACTION_ROWS = 'aieo_public_table_rows'; const ACTION_CHOICES = 'aieo_public_table_choices'; const SIZES = array( 10, 25, 50 ); public static function allowed() { return array_keys( array_filter( self::ALLOWED ) ); } private static function per_page_max() { return ( class_exists( 'AIEO_Table_Registry' ) && defined( 'AIEO_Table_Registry::PER_PAGE_MAX' ) ) ? (int) AIEO_Table_Registry::PER_PAGE_MAX : 200; } public static function register() { static $done = false; if ( $done ) { return; } $done = true; add_action( 'init', array( __CLASS__, 'register_block' ) ); add_action( 'wp_ajax_nopriv_' . self::ACTION_ROWS, array( __CLASS__, 'serve_rows' ) ); add_action( 'wp_ajax_' . self::ACTION_ROWS, array( __CLASS__, 'serve_rows' ) ); add_action( 'wp_ajax_nopriv_' . self::ACTION_CHOICES, array( __CLASS__, 'serve_choices' ) ); add_action( 'wp_ajax_' . self::ACTION_CHOICES, array( __CLASS__, 'serve_choices' ) ); } public static function is_public( $report_id ) { return ! empty( self::ALLOWED[ (string) $report_id ] ); } private static function builders() { if ( class_exists( '\Rooster\Admin\Ui' ) ) { return true; } if ( ! defined( 'AIEO_DIR' ) || ! defined( 'AIEO_URI' ) ) { return false; } $dir = AIEO_DIR . 'lib/rooster-admin'; if ( ! function_exists( 'rooster_admin_builders' ) ) { if ( ! is_readable( $dir . '/rooster-admin.php' ) ) { return false; } require_once $dir . '/rooster-admin.php'; } rooster_admin_register( $dir, '', AIEO_URI . 'lib/rooster-admin' ); return rooster_admin_builders(); } public static function front_engine() { if ( ! self::builders() ) { return false; } $dir = AIEO_DIR . 'lib/rooster-admin'; if ( ! function_exists( 'rooster_table_engine' ) ) { if ( ! is_readable( $dir . '/table-engine.php' ) ) { return false; } require_once $dir . '/table-engine.php'; } rooster_table_engine( $dir, AIEO_URI . 'lib/rooster-admin', array( 'local' => true ) ); return true; } private static function front_select() { if ( ! function_exists( 'rooster_table_engine' ) ) { return; } rooster_table_engine( AIEO_DIR . 'lib/rooster-admin', AIEO_URI . 'lib/rooster-admin', array( 'local' => true, 'select' => true ) ); static $kept = false; if ( ! $kept ) { $kept = true; add_filter( 'rooster/performance/delay_keep', static function ( $handles ) { return array_merge( (array) $handles, array( 'rooster-table-local', 'rooster-table-totals', 'rooster-table-select' ) ); } ); } } public static function front_css( $scope ) { static $done = array(); $scope = preg_replace( '/[^A-Za-z0-9_.#\- ]/', '', (string) $scope ); if ( '' === $scope || isset( $done[ $scope ] ) ) { return ''; } $done[ $scope ] = true; $s = $scope . ' '; $css = $s . '.rooster-admin-table{margin:0 0 1em}' . $s . '.rooster-admin-table__scroll{overflow-x:auto;-webkit-overflow-scrolling:touch}' . $s . '.rooster-admin-table table{display:table;width:100%;border-collapse:collapse;margin:0}' . $s . '.rooster-admin-table th,' . $s . '.rooster-admin-table td{padding:9px 10px;border-bottom:1px solid rgba(128,128,128,.22);text-align:start;vertical-align:middle}' . $s . '.rooster-admin-table th{font-weight:600;white-space:nowrap}' . $s . '.rooster-admin-table .is-num{text-align:end;font-variant-numeric:tabular-nums}' . $s . '.rooster-admin-table th a{display:inline-flex;align-items:center;gap:4px;color:inherit;text-decoration:none}' . $s . '.rooster-admin-table__thumb{position:relative;display:inline-flex;flex-shrink:0;align-items:center;justify-content:center;overflow:hidden;border-radius:6px;background:rgba(128,128,128,.12);vertical-align:middle;width:40px;height:40px}' . $s . '.rooster-admin-table__thumb.is-sm{width:32px;height:32px}' . $s . '.rooster-admin-table__thumb.is-lg{width:56px;height:56px}' . $s . '.rooster-admin-table__thumb.is-xl{width:80px;height:80px}' . $s . '.rooster-admin-table__img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover}' . $s . '.rooster-admin-table__img.is-failed{visibility:hidden}' . $s . '.rooster-admin-filters__field{display:flex;flex-direction:column;gap:2px}' . $s . '.rooster-admin-inputwrap{position:relative;display:inline-flex;align-items:center;max-width:100%}' . $s . '.rooster-admin-inputwrap__icon{position:absolute;inset-inline-start:10px;pointer-events:none;opacity:.6}' . $s . '.rooster-admin-inputwrap .rooster-admin-input{padding-inline-start:32px}' . $s . '.rooster-admin-table caption,' . $s . '.screen-reader-text{position:absolute!important;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}' . $s . '.rooster-admin-table svg{width:1em;height:1em;fill:none;stroke:currentColor;stroke-width:2;vertical-align:-.125em}' . $s . '.rooster-admin-filters,' . $s . '.rooster-admin-table__toolbar{display:flex;flex-wrap:wrap;gap:8px 12px;align-items:flex-end;margin:0 0 .75em}' . $s . '.rooster-admin-table__footer{display:flex;flex-wrap:wrap;gap:8px 16px;align-items:center;justify-content:space-between;margin:.5em 0 0}' . $s . '.rooster-admin-table__size{display:inline-flex;align-items:center;gap:10px;font-size:var(--wp--preset--font-size--sm,.875rem)}' . $s . '.rooster-admin-pager{display:flex;flex-wrap:wrap;gap:8px 16px;align-items:center}' . $s . '.rooster-admin-pager__count{color:var(--wp--preset--color--muted,inherit);font-size:var(--wp--preset--font-size--sm,.875rem);font-variant-numeric:tabular-nums}' . $s . '.rooster-admin-pager__list{display:flex;flex-wrap:wrap;gap:6px;align-items:center;list-style:none;margin:0;padding:0}' . $s . '.rooster-admin-pager__list li{margin:0}' . $s . '.rooster-admin-pager__list a,' . $s . '.rooster-admin-pager__list span:not(.screen-reader-text):not(.rooster-icon){display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box;min-width:var(--wp--custom--rooster--field--height,2.5rem);height:var(--wp--custom--rooster--field--height,2.5rem);padding:0 10px;border:0;border-radius:var(--wp--custom--rooster--shape--control,0);background:var(--wp--preset--color--subtle,color-mix(in srgb,currentColor 8%,transparent));color:inherit;font-size:var(--wp--preset--font-size--sm,.875rem);font-variant-numeric:tabular-nums;line-height:1;text-decoration:none;transition:box-shadow .16s ease}' . $s . '.rooster-admin-pager__list a:hover{box-shadow:inset 0 0 0 1px var(--wp--preset--color--fg,currentColor)}' . $s . '.rooster-admin-pager__list [aria-current="page"],' . $s . '.rooster-admin-pager__list [aria-current="page"]:hover{box-shadow:inset 0 0 0 2px var(--wp--preset--color--fg,currentColor);font-weight:600}' . $s . '.rooster-admin-pager__gap{display:inline-flex;align-items:center;min-width:1.25rem;justify-content:center;color:var(--wp--preset--color--muted,inherit)}' . $s . '.rooster-admin-table__chips{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin:0 0 .75em}' . $s . '.rooster-admin-chip{display:inline-flex;align-items:center;gap:2px;min-height:2rem;padding:0 2px 0 10px;border:1px solid var(--wp--preset--color--line,color-mix(in srgb,currentColor 30%,transparent));border-radius:var(--wp--custom--rooster--shape--control,6px);background:var(--wp--preset--color--bg,transparent);font-size:var(--wp--preset--font-size--sm,.875rem);line-height:1.3}' . $s . '.rooster-admin-chip__remove{display:inline-flex;align-items:center;justify-content:center;width:24px;height:24px;border-radius:calc(var(--wp--custom--rooster--shape--control,6px) * .75);color:inherit;text-decoration:none}' . $s . '.rooster-admin-chip__remove:hover{background:color-mix(in srgb,currentColor 10%,transparent)}' . $s . '.rooster-admin-table__clear{margin-inline-start:4px;font-size:var(--wp--preset--font-size--sm,.875rem)}' . $s . '[hidden]{display:none!important}'; return ''; } private static function public_def() { $id = isset( $_GET['report'] ) ? sanitize_key( wp_unslash( $_GET['report'] ) ) : ''; if ( '' === $id || ! self::is_public( $id ) || ! self::load_reports() || ! AIEO_Report_Registry::get( $id ) ) { wp_send_json( array( 'message' => __( 'No such table.', 'ai-eshop-optimizer' ) ), 404 ); } if ( determine_locale() !== get_locale() ) { switch_to_locale( get_locale() ); } if ( ! self::rate_ok() ) { wp_send_json( array( 'message' => __( 'Too many requests. Try again in a moment.', 'ai-eshop-optimizer' ) ), 429 ); } return AIEO_Report_Registry::get( $id ); } public static function serve_rows() { $def = self::public_def(); if ( ! self::builders() ) { wp_send_json( array( 'message' => __( 'This table is not available right now.', 'ai-eshop-optimizer' ) ), 503 ); } $answer = AIEO_Report_Render::answer( $def->id(), self::request_from_get( $def ) ); if ( isset( $answer['error'] ) ) { wp_send_json( array( 'message' => __( 'That table could not be loaded.', 'ai-eshop-optimizer' ) ), 500 ); } wp_send_json( $answer ); } public static function serve_choices() { $def = self::public_def(); $request = self::request_from_get( $def ); $out = array(); foreach ( self::filter_options_for( $def, $request ) as $key => $values ) { $out[ $key ] = array_map( static function ( $value ) { return array( $value, $value ); }, $values ); } wp_send_json( (object) $out ); } private static function filter_options_for( $def, $request ) { $out = array(); $query = new AIEO_Report_Query( $def, array( 'params' => isset( $request['params'] ) ? $request['params'] : array() ) ); foreach ( (array) $def->filterable() as $key => $kind ) { if ( ! in_array( (string) $kind, array( 'select', 'multiselect' ), true ) ) { continue; } $res = $query->filter_options( $key ); $out[ $key ] = isset( $res['options'] ) ? array_map( 'strval', (array) $res['options'] ) : array(); } return $out; } private static function request_from_get( $def ) { $params = array(); if ( isset( $_GET['params'] ) && is_array( $_GET['params'] ) ) { foreach ( wp_unslash( $_GET['params'] ) as $k => $v ) { $key = preg_replace( '/[^a-zA-Z0-9_]/', '', (string) $k ); if ( '' === $key ) { continue; } if ( is_array( $v ) ) { $inner = array(); foreach ( $v as $k2 => $v2 ) { if ( ! is_array( $v2 ) ) { $inner[ preg_replace( '/[^a-zA-Z0-9_]/', '', (string) $k2 ) ] = sanitize_text_field( (string) $v2 ); } } $params[ $key ] = $inner; } else { $params[ $key ] = sanitize_text_field( (string) $v ); } } } $req = array( 'page' => isset( $_GET['page'] ) ? max( 1, absint( $_GET['page'] ) ) : 1, 'per_page' => isset( $_GET['per_page'] ) ? min( self::per_page_max(), max( 1, absint( $_GET['per_page'] ) ) ) : 10, 'sort' => isset( $_GET['orderby'] ) ? preg_replace( '/[^a-zA-Z0-9_]/', '', (string) wp_unslash( $_GET['orderby'] ) ) : '', 'dir' => isset( $_GET['order'] ) && 'desc' === strtolower( (string) wp_unslash( $_GET['order'] ) ) ? 'desc' : 'asc', 'search' => isset( $_GET['search'] ) ? sanitize_text_field( wp_unslash( $_GET['search'] ) ) : '', 'params' => AIEO_Report_Render::query_params( $def, $params ), ); return $req; } private static function rate_ok() { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $key = 'aieo_pt_' . md5( $ip . gmdate( 'YmdHi' ) ); $n = (int) get_transient( $key ); if ( $n >= self::RATE_PER_MIN ) { return false; } set_transient( $key, $n + 1, MINUTE_IN_SECONDS * 2 ); return true; } public static function register_block() { $dir = AIEO_DIR . 'includes/dmm/blocks/data-table'; if ( ! function_exists( 'register_block_type' ) || ! is_readable( $dir . '/block.json' ) ) { return; } if ( class_exists( 'WP_Block_Type_Registry' ) && WP_Block_Type_Registry::get_instance()->is_registered( 'aieo/data-table' ) ) { return; } register_block_type( $dir, array( 'render_callback' => array( __CLASS__, 'render_block' ) ) ); } private static function load_reports() { if ( class_exists( 'AIEO_Report_Render' ) && class_exists( 'AIEO_Report_Registry' ) ) { return true; } if ( ! defined( 'AIEO_DIR' ) ) { return false; } $base = AIEO_DIR . 'includes/dmm/reports/'; foreach ( array( 'class-aieo-table.php', 'class-aieo-table-registry.php', 'class-aieo-report-registry.php', 'class-aieo-report-query.php', 'class-aieo-report-render.php', ) as $file ) { if ( is_readable( $base . $file ) ) { require_once $base . $file; } } return class_exists( 'AIEO_Report_Render' ) && class_exists( 'AIEO_Report_Registry' ) && class_exists( 'AIEO_Table' ); } private static function library_overrides( $def, $request, $size ) { $prefix = AIEO_Report_Render::prefix( $def->id() ); $options = self::filter_options_for( $def, $request ); $labels = array(); foreach ( (array) $def->columns() as $col ) { $labels[ (string) $col['key'] ] = (string) ( $col['label'] ?? $col['key'] ); } $decls = array(); $kinds = (array) $def->filterable(); foreach ( $options as $key => $values ) { $chosen = isset( $request['params'][ $key ] ) ? (array) $request['params'][ $key ] : array(); $values = array_values( array_unique( array_merge( $values, array_map( 'strval', $chosen ) ) ) ); $decls[] = array( 'key' => $key, 'label' => $labels[ $key ] ?? $key, 'kind' => 'values', 'control' => 'multiselect' === (string) ( $kinds[ $key ] ?? '' ) ? 'chips' : 'select', 'options' => array_combine( $values, $values ), ); } $sort = $def->default_sort(); $sizes = self::SIZES; if ( ! in_array( $size, $sizes, true ) ) { $sizes[] = $size; sort( $sizes ); } $ajax = admin_url( 'admin-ajax.php' ); return array( 'context' => 'storefront', 'remote' => array( 'url' => add_query_arg( 'action', self::ACTION_ROWS, $ajax ), 'nonce' => '', 'scope' => array( 'report' => $def->id() ), 'options' => add_query_arg( 'action', self::ACTION_CHOICES, $ajax ), 'options_current' => true, 'prefix' => $prefix, 'sort' => $sort ? array( 'key' => $sort[0], 'dir' => strtolower( $sort[1] ), ) : null, 'sizes' => $sizes, ), 'filters' => $decls, 'filter_values' => $decls ? \Rooster\Admin\Filters::values( $decls, $prefix, wp_slash( AIEO_Report_Render::address_values( array_map( static fn ( $d ) => $prefix . $d['key'], $decls ) ) ) ) : null, 'caption' => (string) $def->get( 'label', '' ), 'actions' => array(), 'bulk' => array(), 'edit' => array(), 'export' => array(), ); } public static function render_block( $attrs = array() ) { $id = isset( $attrs['table'] ) ? sanitize_key( (string) $attrs['table'] ) : ''; if ( '' === $id || ! self::is_public( $id ) ) { return ''; } if ( ! self::load_reports() || ! self::front_engine() ) { return ''; } $def = AIEO_Report_Registry::get( $id ); if ( ! $def ) { return ''; } if ( in_array( 'multiselect', array_map( 'strval', (array) $def->filterable() ), true ) ) { self::front_select(); } $per_page = isset( $attrs['perPage'] ) ? min( self::per_page_max(), max( 1, (int) $attrs['perPage'] ) ) : (int) $def->get( 'per_page', 10 ); $request = array( 'page' => 1, 'per_page' => $per_page, 'sort' => '', 'dir' => 'asc', 'search' => '', 'params' => array() ); $request = AIEO_Report_Render::view_request( $def, $request, false ); $request['per_page'] = min( self::per_page_max(), max( 1, (int) $request['per_page'] ) ); $overrides = self::library_overrides( $def, $request, $per_page ); $env = AIEO_Report_Render::run( $id, $request, $overrides ); $html = isset( $env['html'] ) ? (string) $env['html'] : AIEO_Report_Render::html( $def, $env, $request, $overrides ); $boot = ''; $buy = ''; return '
' . self::front_css( '.aieo-public-table' ) . $buy . $boot . $html . '
'; } } }