0 ) { $taxonomy = isset( $attributes['taxonomy'] ) ? sanitize_key( (string) $attributes['taxonomy'] ) : ''; $term = get_term( $term_id, ( '' !== $taxonomy && taxonomy_exists( $taxonomy ) ) ? $taxonomy : '' ); } else { $term = get_queried_object(); } if ( ! $term instanceof WP_Term ) { return null; } return ( null === $taxonomies || in_array( $term->taxonomy, (array) $taxonomies, true ) ) ? $term : null; } public static function rest_terms( $request ) { $taxonomy = sanitize_key( (string) $request->get_param( 'taxonomy' ) ); if ( ! taxonomy_exists( $taxonomy ) ) { return new WP_Error( 'aieo_no_taxonomy', __( 'No such taxonomy.', 'ai-eshop-optimizer' ), array( 'status' => 404 ) ); } $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => 500, 'orderby' => 'name' ) ); $out = array(); foreach ( is_array( $terms ) ? $terms : array() as $term ) { $out[] = array( 'id' => (int) $term->term_id, 'name' => html_entity_decode( (string) $term->name, ENT_QUOTES ) ); } return $out; } public static function post_id( $block, $types ) { $types = (array) $types; if ( $block instanceof WP_Block && ! empty( $block->context['postId'] ) ) { $id = (int) $block->context['postId']; } elseif ( in_the_loop() ) { $id = (int) get_the_ID(); } else { $id = is_singular() ? (int) get_queried_object_id() : 0; } return ( $id > 0 && in_array( get_post_type( $id ), $types, true ) ) ? $id : 0; } public static function with_post( $id, callable $fn ) { $saved_post = $GLOBALS['post'] ?? null; $saved_product = $GLOBALS['product'] ?? null; $GLOBALS['post'] = get_post( $id ); setup_postdata( $GLOBALS['post'] ); if ( function_exists( 'wc_get_product' ) && ! ( ( $GLOBALS['product'] ?? null ) instanceof WC_Product && $GLOBALS['product']->get_id() === (int) $id ) ) { $GLOBALS['product'] = wc_get_product( $id ); } try { return $fn(); } finally { $GLOBALS['post'] = $saved_post; if ( $saved_post instanceof WP_Post ) { setup_postdata( $saved_post ); } $GLOBALS['product'] = $saved_product; } } public static function current_product() { $product = $GLOBALS['product'] ?? null; if ( $product instanceof WC_Product ) { return $product; } if ( function_exists( 'wc_get_product' ) && function_exists( 'is_singular' ) && is_singular( 'product' ) ) { $product = wc_get_product( get_queried_object_id() ); return $product instanceof WC_Product ? $product : null; } return null; } } endif;