! empty( $s['hide_prices'] ),
'disable_cart' => ! empty( $s['disable_cart'] ),
'price_text' => isset( $s['price_text'] ) ? trim( (string) $s['price_text'] ) : '',
'viewers' => ! empty( $s['viewers'] ),
'guest_note' => isset( $s['guest_note'] ) ? trim( (string) $s['guest_note'] ) : '',
'card_words' => ! isset( $s['card_words'] ) || ! empty( $s['card_words'] ),
'guest_line' => ! isset( $s['guest_line'] ) || ! empty( $s['guest_line'] ),
);
}
public static function update( array $changes ) {
$s = self::settings();
foreach ( array( 'hide_prices', 'disable_cart', 'viewers', 'card_words', 'guest_line' ) as $key ) {
if ( array_key_exists( $key, $changes ) ) {
$s[ $key ] = ! empty( $changes[ $key ] );
}
}
foreach ( array( 'price_text', 'guest_note' ) as $key ) {
if ( array_key_exists( $key, $changes ) ) {
$s[ $key ] = trim( (string) $changes[ $key ] );
}
}
update_option( self::OPTION, $s, true );
if ( $s['viewers'] ) {
self::ensure_role();
}
self::flush();
return $s;
}
public static function ensure_role() {
if ( null !== get_role( self::ROLE ) ) {
return false;
}
add_role( self::ROLE, self::ROLE_NAME, array( 'read' => true ) );
return true;
}
public static function role_name( $translation, $text = '', $context = '' ) {
return 'User role' === $context && self::ROLE_NAME === $text ? __( 'Price viewer', 'ai-eshop-optimizer' ) : $translation;
}
public static function let_through() {
if ( ! self::settings()['viewers'] || ! function_exists( 'wp_get_current_user' ) ) {
return false;
}
$id = (int) get_current_user_id();
if ( $id <= 0 ) {
return false;
}
if ( ! isset( self::$viewers[ $id ] ) ) {
self::$viewers[ $id ] = in_array( self::ROLE, (array) wp_get_current_user()->roles, true );
}
return self::$viewers[ $id ];
}
public static function storefront() {
if ( ( defined( 'WP_CLI' ) && WP_CLI ) || wp_doing_cron() ) {
return false;
}
if ( is_admin() ) {
if ( ! wp_doing_ajax() ) {
return false;
}
$referer = (string) wp_get_referer();
return '' === $referer || false === strpos( $referer, '/wp-admin/' );
}
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
foreach ( self::STOREFRONT_REST as $route ) {
if ( false !== strpos( $uri, $route ) ) {
return true;
}
}
return false;
}
return true;
}
public static function mark( $product ) {
$id = self::product_id( $product );
if ( $id <= 0 ) {
return '';
}
if ( isset( self::$marks[ $id ] ) ) {
return self::$marks[ $id ];
}
$own = (string) get_post_meta( $id, self::META_PRODUCT, true );
if ( 'yes' === $own || 'no' === $own ) {
return self::$marks[ $id ] = $own;
}
$catalogue = self::catalogue_terms();
if ( $catalogue ) {
$term_ids = wp_cache_get( $id, 'product_cat_relationships' );
if ( ! is_array( $term_ids ) ) {
$terms = get_the_terms( $id, 'product_cat' );
$term_ids = is_array( $terms ) ? wp_list_pluck( $terms, 'term_id' ) : array();
}
foreach ( $term_ids as $term_id ) {
if ( isset( $catalogue[ (int) ( is_object( $term_id ) ? $term_id->term_id : $term_id ) ] ) ) {
return self::$marks[ $id ] = 'yes';
}
}
}
return self::$marks[ $id ] = '';
}
public static function any_marks() {
if ( null === self::$any_marks ) {
$products = get_option( self::PRODUCTS_OPTION, null );
if ( null === $products || false === $products ) {
global $wpdb;
$products = $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM {$wpdb->postmeta} WHERE meta_key = %s LIMIT 1", self::META_PRODUCT ) ) ? 1 : 0;
update_option( self::PRODUCTS_OPTION, $products, true );
}
self::$any_marks = (bool) (int) $products || (bool) self::catalogue_terms();
}
return self::$any_marks;
}
public static function forget_products( $meta_id = 0, $object_id = 0, $meta_key = '' ) {
if ( self::META_PRODUCT === $meta_key ) {
delete_option( self::PRODUCTS_OPTION );
self::flush();
}
}
private static function catalogue_terms() {
if ( null !== self::$catalogue_terms ) {
return self::$catalogue_terms;
}
$marked = get_option( self::TERMS_OPTION, null );
if ( ! is_array( $marked ) ) {
$marked = taxonomy_exists( 'product_cat' ) ? get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'fields' => 'ids', 'meta_key' => self::META_TERM, 'meta_value' => 'yes' ) ) : array();
$marked = is_array( $marked ) ? array_values( array_map( 'intval', $marked ) ) : array();
update_option( self::TERMS_OPTION, $marked, true );
}
$all = array();
foreach ( $marked as $term_id ) {
$all[ (int) $term_id ] = true;
$children = get_term_children( (int) $term_id, 'product_cat' );
foreach ( is_array( $children ) ? $children : array() as $child ) {
$all[ (int) $child ] = true;
}
}
return self::$catalogue_terms = $all;
}
public static function forget_terms( $meta_id = 0, $object_id = 0, $meta_key = '' ) {
if ( '' === (string) $meta_key || self::META_TERM === $meta_key ) {
delete_option( self::TERMS_OPTION );
self::flush();
}
}
public static function flush() {
self::$marks = array();
self::$catalogue_terms = null;
self::$any_marks = null;
self::$viewers = array();
self::$guest_line = null;
}
public static function follow_profile( $old, $new, $choices = null ) {
if ( 'catalog' === $new && is_array( $choices ) ) {
self::update( array( 'hide_prices' => ! empty( $choices['prices'] ), 'disable_cart' => ! empty( $choices['cart'] ) ) );
if ( ! empty( $choices['requests'] ) ) {
$key = class_exists( 'AIEO_Product_Request_Settings' ) ? AIEO_Product_Request_Settings::OPTION_KEY : 'aieo_dmm_product_requests';
$pr = get_option( $key, array() );
update_option( $key, array_merge( is_array( $pr ) ? $pr : array(), array( 'enabled' => 'yes', 'frontend_enabled' => 'yes' ) ) );
}
} elseif ( 'catalog' === $old && 'catalog' !== $new ) {
self::update( array( 'hide_prices' => false, 'disable_cart' => false ) );
} else {
return false;
}
self::flush();
if ( class_exists( 'AIEO_DMM_Cache' ) ) {
AIEO_DMM_Cache::bump_version( 'prices' );
}
return true;
}
private static function product_id( $product ) {
if ( is_object( $product ) && method_exists( $product, 'get_id' ) ) {
return method_exists( $product, 'get_parent_id' ) && (int) $product->get_parent_id() > 0 ? (int) $product->get_parent_id() : (int) $product->get_id();
}
if ( $product instanceof WP_Post ) {
return 'product_variation' === $product->post_type ? (int) $product->post_parent : (int) $product->ID;
}
$id = (int) $product;
if ( $id <= 0 ) {
return 0;
}
if ( ! isset( self::$parents[ $id ] ) ) {
self::$parents[ $id ] = 'product_variation' === get_post_type( $id ) ? (int) wp_get_post_parent_id( $id ) : $id;
}
return self::$parents[ $id ];
}
public static function applies( $what, $product = null ) {
$s = self::settings();
$on = 'prices' === $what ? $s['hide_prices'] : ( 'cart' === $what ? $s['disable_cart'] : false );
if ( $on && $s['viewers'] && self::let_through() ) {
$on = false;
}
$mark = null !== $product && in_array( $what, array( 'prices', 'cart' ), true ) && self::any_marks() ? self::mark( $product ) : '';
if ( 'yes' === $mark ) {
$on = true;
} elseif ( 'no' === $mark ) {
$on = false;
}
return (bool) apply_filters( 'aieo_catalogue_mode_applies', $on && self::storefront(), (string) $what, $product );
}
public static function prices_off( $product = null ) {
return self::applies( 'prices', $product );
}
public static function cart_off( $product = null ) {
return self::applies( 'cart', $product );
}
public static function price_text_html( $product = null ) {
$s = self::settings();
if ( null !== $product && ! $s['card_words'] && ! self::own_page( $product ) ) {
return '';
}
$text = $s['price_text'];
$html = '';
if ( '' !== $text ) {
$html = '' . esc_html( self::translated( $text, self::STRING_NAME ) ) . '';
}
$line = self::guest_line_html();
if ( '' === $line || ( null !== $product && self::any_marks() && 'yes' === self::mark( $product ) ) ) {
return $html;
}
return '' === $html ? $line : '' . $html . '
' . $line . '';
}
public static function own_page( $product ) {
$id = self::product_id( $product );
$own = false;
if ( $id > 0 ) {
if ( function_exists( 'is_singular' ) && is_singular( 'product' ) ) {
$own = (int) get_queried_object_id() === $id;
} elseif ( wp_doing_ajax() || isset( $_GET['wc-ajax'] ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
$referer = (string) wp_get_referer();
$own = '' !== $referer && untrailingslashit( strtok( $referer, '?#' ) ) === untrailingslashit( (string) get_permalink( $id ) );
}
}
return (bool) apply_filters( 'aieo_catalogue_mode_own_page', $own, $product );
}
public static function price_block( $content, $block = array(), $instance = null ) {
$product = is_object( $instance ) && ! empty( $instance->context['postId'] ) ? (int) $instance->context['postId'] : 0;
if ( $product <= 0 || ! self::prices_off( $product ) ) {
return $content;
}
return '' === self::price_text_html( $product ) ? '' : $content;
}
public static function guest_line_html() {
$s = self::settings();
if ( ! $s['viewers'] || ! $s['hide_prices'] || ! $s['guest_line'] || ! function_exists( 'is_user_logged_in' ) || is_user_logged_in() ) {
return '';
}
if ( null === self::$guest_line ) {
$text = '' !== $s['guest_note'] ? self::translated( $s['guest_note'], self::NOTE_NAME ) : __( 'Log in to see your prices', 'ai-eshop-optimizer' );
$account = function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'myaccount' ) : 0;
$url = $account > 0 ? (string) get_permalink( $account ) : '';
$url = (string) apply_filters( 'aieo_catalogue_mode_signin_url', '' !== $url ? $url : wp_login_url() );
self::$guest_line = '' . esc_html( $text ) . '';
}
return self::$guest_line;
}
private static function translated( $text, $name ) {
if ( has_filter( 'wpml_translate_single_string' ) ) {
return (string) apply_filters( 'wpml_translate_single_string', $text, self::STRINGS_DOMAIN, $name );
}
return function_exists( 'pll__' ) ? (string) pll__( $text ) : (string) $text;
}
public static function boot() {
if ( self::$booted ) {
return;
}
self::$booted = true;
add_filter( 'woocommerce_get_price_html', array( __CLASS__, 'price_html' ), 9999, 2 );
add_filter( 'woocommerce_available_variation', array( __CLASS__, 'variation' ), 9999, 2 );
add_filter( 'woocommerce_structured_data_product', array( __CLASS__, 'structured_data' ), 9999, 2 );
add_filter( 'woocommerce_is_purchasable', array( __CLASS__, 'purchasable' ), 9999, 2 );
add_filter( 'woocommerce_variation_is_purchasable', array( __CLASS__, 'purchasable' ), 9999, 2 );
add_filter( 'woocommerce_loop_add_to_cart_link', array( __CLASS__, 'loop_link' ), 9999, 2 );
foreach ( array( 'woocommerce/product-button', 'woocommerce/add-to-cart-form', 'woocommerce/add-to-cart-with-options' ) as $block ) {
add_filter( 'render_block_' . $block, array( __CLASS__, 'block_control' ), 9999, 3 );
}
foreach ( self::CART_BLOCKS as $block ) {
add_filter( 'render_block_' . $block, array( __CLASS__, 'cart_door' ), 9999 );
}
add_filter( 'pre_render_block', array( __CLASS__, 'skip_cart_block' ), 11, 2 );
add_filter( 'pre_render_block', array( __CLASS__, 'skip_price_filter' ), 11, 2 );
add_filter( 'render_block_data', array( __CLASS__, 'without_price_filter' ) );
add_action( 'woocommerce_before_single_product', array( __CLASS__, 'single_product' ) );
add_filter( 'rest_request_after_callbacks', array( __CLASS__, 'store_api' ), 9999, 3 );
add_filter( 'woocommerce_hydration_request_after_callbacks', array( __CLASS__, 'store_api' ), 9999, 3 );
add_filter( 'wp_headers', array( __CLASS__, 'viewer_headers' ), 9999 );
add_filter( 'woocommerce_sale_flash', array( __CLASS__, 'sale_flash' ), 9999, 3 );
add_filter( 'render_block_woocommerce/product-sale-badge', array( __CLASS__, 'sale_badge' ), 9999, 3 );
add_filter( 'render_block_woocommerce/product-price', array( __CLASS__, 'price_block' ), 9999, 3 );
if ( class_exists( 'AIEO_Safe_Roles' ) ) {
add_filter( 'option_default_role', array( 'AIEO_Safe_Roles', 'default_role' ) );
}
if ( is_admin() ) {
add_filter( 'gettext_with_context_default', array( __CLASS__, 'role_name' ), 10, 3 );
}
self::watch();
}
public static function watch() {
foreach ( array( 'added_term_meta', 'updated_term_meta', 'deleted_term_meta' ) as $hook ) {
if ( ! has_action( $hook, array( __CLASS__, 'forget_terms' ) ) ) {
add_action( $hook, array( __CLASS__, 'forget_terms' ), 10, 3 );
}
}
foreach ( array( 'added_post_meta', 'updated_post_meta', 'deleted_post_meta' ) as $hook ) {
if ( ! has_action( $hook, array( __CLASS__, 'forget_products' ) ) ) {
add_action( $hook, array( __CLASS__, 'forget_products' ), 10, 3 );
}
}
if ( ! has_action( 'delete_product_cat', array( __CLASS__, 'forget_terms' ) ) ) {
add_action( 'delete_product_cat', array( __CLASS__, 'forget_terms' ), 10, 0 );
}
}
public static function price_html( $html, $product = null ) {
return self::prices_off( $product ) ? self::price_text_html( $product ) : $html;
}
public static function variation( $data, $product = null ) {
return is_array( $data ) && self::prices_off( $product ) ? self::variation_without_price( $data, self::price_text_html( $product ), true ) : $data;
}
private static function variation_without_price( array $row, $words, $always = false ) {
if ( $always || array_key_exists( 'price_html', $row ) ) {
$row['price_html'] = $words;
}
foreach ( array( 'display_price', 'display_regular_price' ) as $key ) {
if ( array_key_exists( $key, $row ) ) {
$row[ $key ] = 0;
}
}
return $row;
}
public static function without_variation_prices( $variations, $product ) {
if ( ! is_array( $variations ) || ! self::prices_off( $product ) ) {
return $variations;
}
$words = self::price_text_html( $product );
foreach ( $variations as $i => $row ) {
if ( is_array( $row ) ) {
$variations[ $i ] = self::variation_without_price( $row, $words );
}
}
return $variations;
}
public static function shared() {
return ! ( self::settings()['viewers'] && function_exists( 'is_user_logged_in' ) && is_user_logged_in() );
}
public static function sale_flash( $html, $post = null, $product = null ) {
return self::prices_off( $product ? $product : $post ) ? '' : $html;
}
public static function sale_badge( $content, $block = array(), $instance = null ) {
$product = is_object( $instance ) && ! empty( $instance->context['postId'] ) ? (int) $instance->context['postId'] : ( isset( $GLOBALS['product'] ) && is_object( $GLOBALS['product'] ) ? $GLOBALS['product'] : get_the_ID() );
return self::prices_off( $product ) ? '' : $content;
}
public static function store_api( $response, $handler = null, $request = null ) {
if ( ! $response instanceof WP_REST_Response || ! $request instanceof WP_REST_Request || 1 !== preg_match( '#^/wc/store(?:/v\d+)?/products(?:[/?]|$)#', (string) $request->get_route() ) ) {
return $response;
}
if ( ! self::settings()['hide_prices'] && ! self::any_marks() ) {
return $response;
}
$data = $response->get_data();
if ( ! is_array( $data ) ) {
return $response;
}
if ( isset( $data['id'], $data['prices'] ) ) {
$data = self::without_amounts( $data );
} elseif ( array_key_exists( 'price_range', $data ) ) {
if ( self::prices_off() ) {
$data['price_range'] = null;
}
} else {
foreach ( $data as $i => $item ) {
if ( is_array( $item ) && isset( $item['id'], $item['prices'] ) ) {
$data[ $i ] = self::without_amounts( $item );
}
}
}
$response->set_data( $data );
return $response;
}
private static function without_amounts( array $item ) {
if ( ! self::prices_off( (int) $item['id'] ) ) {
return $item;
}
$prices = is_object( $item['prices'] ) ? get_object_vars( $item['prices'] ) : (array) $item['prices'];
foreach ( array( 'price', 'regular_price', 'sale_price' ) as $key ) {
if ( array_key_exists( $key, $prices ) ) {
$prices[ $key ] = '';
}
}
if ( array_key_exists( 'price_range', $prices ) ) {
$prices['price_range'] = null;
}
$item['prices'] = $prices;
if ( array_key_exists( 'on_sale', $item ) ) {
$item['on_sale'] = false;
}
return $item;
}
public static function viewer_headers( $headers ) {
if ( is_array( $headers ) && self::let_through() ) {
$headers['Cache-Control'] = 'private, no-store';
$headers['X-Rooster-No-Cache'] = '1';
}
return $headers;
}
public static function structured_data( $markup, $product = null ) {
if ( is_array( $markup ) && self::prices_off( $product ) ) {
unset( $markup['offers'] );
}
return $markup;
}
public static function purchasable( $purchasable, $product = null ) {
return self::cart_off( $product ) ? false : $purchasable;
}
public static function loop_link( $html, $product = null ) {
return self::cart_off( $product ) ? self::request_button( $product, 'button aieo-catalogue-request' ) : $html;
}
public static function block_control( $content, $block = array(), $instance = null ) {
$product = is_object( $instance ) && ! empty( $instance->context['postId'] ) ? (int) $instance->context['postId'] : ( isset( $GLOBALS['product'] ) && is_object( $GLOBALS['product'] ) ? $GLOBALS['product'] : get_the_ID() );
if ( ! self::cart_off( $product ) ) {
return $content;
}
if ( 'woocommerce/product-button' === ( $block['blockName'] ?? '' ) ) {
$button = self::request_button( $product, 'wp-block-button__link wp-element-button aieo-catalogue-request' );
if ( '' === $button ) {
return '';
}
$classes = 'wp-block-button wc-block-components-product-button wp-block-woocommerce-product-button';
if ( class_exists( 'WP_HTML_Tag_Processor' ) ) {
$tags = new WP_HTML_Tag_Processor( (string) $content );
if ( $tags->next_tag() && is_string( $tags->get_attribute( 'class' ) ) && false !== strpos( (string) $tags->get_attribute( 'class' ), 'wp-block-woocommerce-product-button' ) ) {
$classes = (string) $tags->get_attribute( 'class' );
}
}
return '