array( 'X-OM-API-Key' => (string) get_option( 'aieo_rooster_api_key', '' ), 'Accept' => 'application/json', ), 'timeout' => 8, 'sslverify' => (bool) apply_filters('aieo_portal_sslverify', true), ) ); $snap = null; if ( ! is_wp_error( $resp ) && 200 === (int) wp_remote_retrieve_response_code( $resp ) ) { $body = json_decode( (string) wp_remote_retrieve_body( $resp ), true ); if ( is_array( $body ) && self::SCHEMA === (int) ( $body['schema'] ?? 0 ) && ! empty( $body['rows_assoc'] ) && is_array( $body['rows_assoc'] ) ) { $snap = $body; } } if ( null === $snap ) { set_transient( self::MISS_TRANSIENT . $page_id, 1, 15 * MINUTE_IN_SECONDS ); return null; } set_transient( self::SNAP_TRANSIENT . $page_id, $snap, DAY_IN_SECONDS ); return $snap; } public static function render_demo_card( $page, array $snap ) { if ( ! class_exists( 'AIEO_Report_Registry' ) || ! class_exists( 'AIEO_Report_Render' ) ) { return false; } $def = AIEO_Report_Registry::get( $page->report_id() ); if ( ! $def ) { return false; } $rows = array_values( (array) $snap['rows_assoc'] ); $envelope = array( 'rows_assoc' => $rows, 'totals' => isset( $snap['totals'] ) ? (array) $snap['totals'] : array(), 'meta' => isset( $snap['meta'] ) ? (array) $snap['meta'] : array(), ); $html = AIEO_Report_Render::html( $def, $envelope, array(), array( 'report' => '', 'actions' => array(), 'bulk' => array(), 'page_id' => $page->get_id(), 'cap' => count( $rows ), 'total' => (int) ( $envelope['meta']['total'] ?? count( $rows ) ), ) ); if ( '' === trim( (string) $html ) ) { return false; } $anchor = AIEO_Report_Page::anchor_for( $page->get_id() ); echo '
' . esc_html( $description ) . '
'; } echo $html; echo '' . '' . esc_html__( 'Upgrade to Pro', 'ai-eshop-optimizer' ) . ' → ' . '' . esc_html__( 'Pro runs every Insights report on your own live data.', 'ai-eshop-optimizer' ) . '
'; echo '' . esc_html__( 'Want to see this report working? Create your free Rooster account — every new site gets a 2-week Pro trial, and registered sites can preview the Insights reports with demo data.', 'ai-eshop-optimizer' ) . '
' . '' . esc_html__( 'Create your free account', 'ai-eshop-optimizer' ) . ''; echo '' . esc_html__( 'This site is registered with rooster.systems, so you can preview the Insights reports with demo data from the Rooster One demo shop. One switch for all reports; turn it off any time.', 'ai-eshop-optimizer' ) . '
' . ''; echo ''; self::print_toggle_script( 'on' ); } private static function print_toggle_script( $which ) { static $printed = array(); if ( isset( $printed[ $which ] ) ) { return; } $printed[ $which ] = true; $btn_id = 'on' === $which ? 'aieo-demo-on' : 'aieo-demo-off'; $value = 'on' === $which ? '1' : '0'; echo ''; } public static function ajax_toggle() { if ( ! class_exists( 'AIEO_DMM' ) ) { wp_send_json_error(); } AIEO_DMM::verify_ajax_request(); $on = isset( $_POST['on'] ) && '1' === (string) wp_unslash( $_POST['on'] ); update_option( self::OPTION, $on ? 'on' : 'off', false ); if ( ! $on ) { global $wpdb; $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient%aieo_demo_%'" ); } wp_send_json_success( array( 'on' => $on ? 1 : 0 ) ); } public static function build_snapshot( $page_id, $per_page = 50 ) { $page_id = sanitize_key( $page_id ); $report = $page_id; $params = array(); if ( class_exists( 'AIEO_Table_Registry' ) ) { $aliases = AIEO_Table_Registry::superseded_aliases(); if ( isset( $aliases[ $page_id ] ) ) { $report = (string) $aliases[ $page_id ]['definition']; $params = (array) $aliases[ $page_id ]['preset']; } } $def = AIEO_Report_Registry::get( $report ); if ( ! $def ) { return new WP_Error( 'aieo_demo_no_def', 'No definition for ' . $page_id ); } $envelope = AIEO_Report_Render::run( $report, array( 'page' => 1, 'per_page' => max( 1, (int) $per_page ), 'params' => $params, ) ); if ( isset( $envelope['error'] ) ) { return new WP_Error( 'aieo_demo_run', $page_id . ': ' . $envelope['error'] ); } $rows = array_values( (array) ( $envelope['rows_assoc'] ?? array() ) ); foreach ( $rows as &$r ) { foreach ( $r as $k => $v ) { if ( false !== strpos( (string) $k, 'image' ) ) { $r[ $k ] = ''; } } } unset( $r ); return array( 'schema' => self::SCHEMA, 'page' => $page_id, 'report' => $report, 'definition_version' => (string) $def->get( 'version', '' ), 'params' => $params, 'columns' => array_map( static function ( $c ) { return (string) $c['key']; }, (array) $def->columns() ), 'generated_at' => gmdate( 'c' ), 'rows_assoc' => $rows, 'totals' => isset( $envelope['totals'] ) ? (array) $envelope['totals'] : array(), 'meta' => isset( $envelope['meta'] ) ? (array) $envelope['meta'] : array(), ); } } add_action( 'wp_ajax_aieo_dmm_demo_previews_toggle', array( 'AIEO_Report_Demo', 'ajax_toggle' ) );