editor && $elementor->editor->is_edit_mode() ) || ( $elementor->preview && $elementor->preview->is_preview_mode() ) ) {
return true;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check of the Elementor AJAX action; Elementor verifies its own nonce.
return wp_doing_ajax() && isset( $_REQUEST['action'] ) && 'elementor_ajax' === $_REQUEST['action'];
}
/**
* The Buildify document currently being edited, if any.
*
* @return \Buildify\Elementor\Documents\Theme_Document|null
*/
public static function get_editor_document() {
if ( ! self::is_editor() ) {
return null;
}
$document = \Elementor\Plugin::$instance->documents->get_current();
if ( ! $document ) {
$post_id = get_the_ID();
if ( $post_id && Post_Type::POST_TYPE === get_post_type( $post_id ) ) {
$document = \Elementor\Plugin::$instance->documents->get( $post_id );
}
}
return $document instanceof Elementor\Documents\Theme_Document ? $document : null;
}
/**
* ID of the post widgets and dynamic tags should show.
*
* In the editor this is the "Preview as" post of the template; on the front end it is the current post.
*
* @return int
*/
public static function get_post_id() {
$document = self::get_editor_document();
if ( $document ) {
$preview = $document->get_preview_post_id();
if ( $preview ) {
return $preview;
}
}
$post_id = get_the_ID();
if ( $post_id && Post_Type::POST_TYPE === get_post_type( $post_id ) ) {
return 0;
}
return (int) $post_id;
}
/**
* Post object widgets should show.
*
* @return \WP_Post|null
*/
public static function get_post() {
$post_id = self::get_post_id();
$post = $post_id ? get_post( $post_id ) : null;
return $post instanceof \WP_Post ? $post : null;
}
/**
* Product widgets should show.
*
* @return \WC_Product|null
*/
public static function get_product() {
if ( ! function_exists( 'wc_get_product' ) ) {
return null;
}
$document = self::get_editor_document();
if ( $document ) {
$preview = $document->get_preview_post_id();
if ( $preview && 'product' === get_post_type( $preview ) ) {
$product = wc_get_product( $preview );
return $product ? $product : null;
}
}
global $product;
if ( $product instanceof \WC_Product ) {
return $product;
}
$post_id = get_the_ID();
if ( $post_id && 'product' === get_post_type( $post_id ) ) {
$found = wc_get_product( $post_id );
return $found ? $found : null;
}
return null;
}
/**
* Runs a callback with the global post (and product) switched to the widget context.
*
* @param callable $callback Callback.
* @return mixed Callback result.
*/
public static function with_post_context( $callback ) {
$post = self::get_post();
if ( ! $post || ( isset( $GLOBALS['post'] ) && $GLOBALS['post'] instanceof \WP_Post && $GLOBALS['post']->ID === $post->ID ) ) {
return call_user_func( $callback, $post );
}
$original_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
$original_product = isset( $GLOBALS['product'] ) ? $GLOBALS['product'] : null;
$GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Temporarily switched and restored below.
setup_postdata( $post );
if ( 'product' === $post->post_type && function_exists( 'wc_get_product' ) ) {
$GLOBALS['product'] = wc_get_product( $post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce global. Temporarily switched and restored below.
}
$result = call_user_func( $callback, $post );
$GLOBALS['post'] = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring the original value.
$GLOBALS['product'] = $original_product; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce global. Restoring the original value.
if ( $original_post instanceof \WP_Post ) {
setup_postdata( $original_post );
} else {
wp_reset_postdata();
}
return $result;
}
/**
* Translated template or post ID for WPML and Polylang.
*
* @param int $id Post ID.
* @param string $post_type Post type.
* @return int
*/
public static function translate_id( $id, $post_type = '' ) {
$id = (int) $id;
if ( ! $id ) {
return 0;
}
if ( '' === $post_type ) {
$post_type = get_post_type( $id );
}
if ( has_filter( 'wpml_object_id' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML hook.
return (int) apply_filters( 'wpml_object_id', $id, $post_type, true );
}
if ( function_exists( 'pll_get_post' ) && function_exists( 'pll_is_translated_post_type' ) && pll_is_translated_post_type( $post_type ) ) {
$translated = pll_get_post( $id );
return $translated ? (int) $translated : $id;
}
return $id;
}
/**
* Translated term ID for WPML and Polylang.
*
* @param int $id Term ID.
* @param string $taxonomy Taxonomy.
* @return int
*/
public static function translate_term_id( $id, $taxonomy ) {
$id = (int) $id;
if ( has_filter( 'wpml_object_id' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML hook.
return (int) apply_filters( 'wpml_object_id', $id, $taxonomy, true );
}
if ( function_exists( 'pll_get_term' ) ) {
$translated = pll_get_term( $id );
return $translated ? (int) $translated : $id;
}
return $id;
}
/**
* Public post types usable in conditions and queries.
*
* @return array name => label
*/
public static function get_post_types() {
$types = array();
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) {
if ( in_array( $type->name, array( 'attachment', 'elementor_library', 'e-landing-page', 'e-floating-buttons', Post_Type::POST_TYPE ), true ) ) {
continue;
}
$types[ $type->name ] = $type->labels->singular_name;
}
/**
* Filters the post types offered in conditions and widgets.
*
* @param array $types name => label.
*/
return (array) apply_filters( 'buildify_post_types', $types );
}
/**
* Public taxonomies usable in conditions and widgets.
*
* @return array name => label
*/
public static function get_taxonomies() {
$taxonomies = array();
foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $taxonomy ) {
if ( in_array( $taxonomy->name, array( 'post_format', 'elementor_library_type', 'product_shipping_class', 'product_visibility' ), true ) ) {
continue;
}
$taxonomies[ $taxonomy->name ] = $taxonomy->labels->singular_name;
}
/**
* Filters the taxonomies offered in conditions and widgets.
*
* @param array $taxonomies name => label.
*/
return (array) apply_filters( 'buildify_taxonomies', $taxonomies );
}
/**
* Editor placeholder text shown when a widget has nothing to display.
*
* @param string $message Message.
*/
public static function editor_placeholder( $message ) {
if ( ! self::is_editor() ) {
return;
}
printf( '
%s
', esc_html( $message ) );
}
/**
* Plain-text excerpt of a post, with spaces kept between paragraphs and blocks.
*
* @param \WP_Post $post Post.
* @param int $words Maximum number of words.
* @return string
*/
public static function excerpt( $post, $words = 55 ) {
if ( post_password_required( $post ) ) {
return '';
}
$text = has_excerpt( $post ) ? $post->post_excerpt : excerpt_remove_blocks( strip_shortcodes( $post->post_content ) );
$text = preg_replace( '#<[^>]+>#', ' ', (string) $text );
return wp_trim_words( $text, max( 1, (int) $words ) );
}
/**
* Estimated reading time in minutes.
*
* @param int $post_id Post ID.
* @param int $words_per_minute Reading speed.
* @return int
*/
public static function reading_time( $post_id, $words_per_minute = 200 ) {
$content = get_post_field( 'post_content', $post_id );
$text = wp_strip_all_tags( strip_shortcodes( (string) $content ) );
if ( '' === trim( $text ) && class_exists( '\Elementor\Plugin' ) && \Elementor\Plugin::$instance->documents->get( $post_id ) && \Elementor\Plugin::$instance->documents->get( $post_id )->is_built_with_elementor() ) {
$text = wp_strip_all_tags( \Elementor\Plugin::$instance->frontend->get_builder_content( $post_id ) );
}
$words = count( preg_split( '/\s+/u', trim( $text ), -1, PREG_SPLIT_NO_EMPTY ) );
return max( 1, (int) ceil( $words / max( 50, (int) $words_per_minute ) ) );
}
/**
* Current URL without query arguments added by builders.
*
* @return string
*/
public static function current_url() {
if ( is_singular() ) {
return (string) get_permalink( self::get_post_id() );
}
global $wp;
return home_url( add_query_arg( array(), isset( $wp->request ) ? $wp->request : '' ) );
}
/**
* Allowed HTML for inline SVG icons generated by the plugin.
*
* @return array
*/
public static function svg_kses() {
return array(
'svg' => array(
'class' => true,
'xmlns' => true,
'viewbox' => true,
'width' => true,
'height' => true,
'fill' => true,
'aria-hidden' => true,
'focusable' => true,
'role' => true,
),
'path' => array(
'd' => true,
'fill' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
),
);
}
/**
* Allowed HTML for templates embedded with the shortcode, block and Section Template widget.
*
* The post allowlist of WordPress, plus the markup Elementor widgets produce: inline SVG
* icons, form fields, media, and ARIA and data attributes. Scripts, event handlers and
* unsafe URL protocols are always removed.
*
* @return array
*/
public static function embed_allowed_html() {
static $allowed = null;
if ( null !== $allowed ) {
return $allowed;
}
$global = array(
'class' => true,
'id' => true,
'style' => true,
'title' => true,
'role' => true,
'hidden' => true,
'tabindex' => true,
'lang' => true,
'dir' => true,
'data-*' => true,
'aria-label' => true,
'aria-labelledby' => true,
'aria-describedby' => true,
'aria-hidden' => true,
'aria-expanded' => true,
'aria-controls' => true,
'aria-current' => true,
'aria-selected' => true,
'aria-live' => true,
'aria-modal' => true,
'aria-haspopup' => true,
'aria-pressed' => true,
'aria-valuenow' => true,
'aria-valuemin' => true,
'aria-valuemax' => true,
'itemscope' => true,
'itemtype' => true,
'itemprop' => true,
);
$extra = array(
'svg' => array(
'xmlns' => true,
'viewbox' => true,
'width' => true,
'height' => true,
'fill' => true,
'stroke' => true,
'focusable' => true,
'preserveaspectratio' => true,
),
'g' => array(
'fill' => true,
'stroke' => true,
'transform' => true,
),
'path' => array(
'd' => true,
'fill' => true,
'fill-rule' => true,
'clip-rule' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'transform' => true,
),
'circle' => array(
'cx' => true,
'cy' => true,
'r' => true,
'fill' => true,
'stroke' => true,
'stroke-width' => true,
),
'ellipse' => array(
'cx' => true,
'cy' => true,
'rx' => true,
'ry' => true,
'fill' => true,
),
'rect' => array(
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'rx' => true,
'ry' => true,
'fill' => true,
'stroke' => true,
),
'line' => array(
'x1' => true,
'y1' => true,
'x2' => true,
'y2' => true,
'stroke' => true,
'stroke-width' => true,
),
'polyline' => array(
'points' => true,
'fill' => true,
'stroke' => true,
),
'polygon' => array(
'points' => true,
'fill' => true,
'stroke' => true,
),
'defs' => array(),
'lineargradient' => array(
'x1' => true,
'y1' => true,
'x2' => true,
'y2' => true,
'gradientunits' => true,
'gradienttransform' => true,
),
'stop' => array(
'offset' => true,
'stop-color' => true,
'stop-opacity' => true,
),
'form' => array(
'action' => true,
'method' => true,
'novalidate' => true,
'name' => true,
),
'input' => array(
'type' => true,
'name' => true,
'value' => true,
'placeholder' => true,
'required' => true,
'checked' => true,
'disabled' => true,
'readonly' => true,
'min' => true,
'max' => true,
'step' => true,
'size' => true,
'autocomplete' => true,
'inputmode' => true,
'pattern' => true,
),
'select' => array(
'name' => true,
'required' => true,
'multiple' => true,
'disabled' => true,
),
'option' => array(
'value' => true,
'selected' => true,
'disabled' => true,
),
'textarea' => array(
'name' => true,
'rows' => true,
'cols' => true,
'placeholder' => true,
'required' => true,
),
'button' => array(
'type' => true,
'name' => true,
'value' => true,
'disabled' => true,
),
'label' => array(
'for' => true,
),
'iframe' => array(
'src' => true,
'width' => true,
'height' => true,
'allow' => true,
'allowfullscreen' => true,
'frameborder' => true,
'loading' => true,
'referrerpolicy' => true,
),
'picture' => array(),
'source' => array(
'src' => true,
'srcset' => true,
'sizes' => true,
'type' => true,
'media' => true,
),
'img' => array(
'src' => true,
'srcset' => true,
'sizes' => true,
'alt' => true,
'width' => true,
'height' => true,
'loading' => true,
'decoding' => true,
'fetchpriority' => true,
),
'time' => array(
'datetime' => true,
),
'meta' => array(
'itemprop' => true,
'content' => true,
),
);
$allowed = wp_kses_allowed_html( 'post' );
foreach ( $extra as $tag => $attributes ) {
$allowed[ $tag ] = array_merge( isset( $allowed[ $tag ] ) ? $allowed[ $tag ] : array(), $attributes );
}
foreach ( $allowed as $tag => $attributes ) {
$allowed[ $tag ] = array_merge( is_array( $attributes ) ? $attributes : array(), $global );
}
/**
* Filters the HTML allowed in embedded Buildify templates.
*
* @param array $allowed Allowed tags and attributes, as used by wp_kses().
*/
$allowed = (array) apply_filters( 'buildify_embed_allowed_html', $allowed );
return $allowed;
}
/**
* Inline SVG icon from the bundled set.
*
* @param string $name Icon name.
* @return string
*/
public static function icon( $name ) {
$paths = array(
'menu' => 'M3 6h18M3 12h18M3 18h18',
'close' => 'M6 6l12 12M18 6L6 18',
'search' => 'M11 4a7 7 0 1 0 0 14a7 7 0 1 0 0-14zM20 20l-4-4',
'cart' => 'M3 4h2l2.4 11.2a2 2 0 0 0 2 1.6h7.7a2 2 0 0 0 2-1.5L21 8H6.2M10 21a1 1 0 1 0 0-2a1 1 0 0 0 0 2zM17 21a1 1 0 1 0 0-2a1 1 0 0 0 0 2z',
'chevron' => 'M6 9l6 6l6-6',
'arrow-up' => 'M12 19V5M5 12l7-7l7 7',
'left' => 'M15 18l-6-6l6-6',
'right' => 'M9 18l6-6l-6-6',
'user' => 'M12 12a4 4 0 1 0 0-8a4 4 0 0 0 0 8zM4 21a8 8 0 0 1 16 0',
'calendar' => 'M4 5h16v16H4zM4 10h16M9 3v4M15 3v4',
'clock' => 'M12 3a9 9 0 1 0 0 18a9 9 0 0 0 0-18zM12 7v5l3 3',
'folder' => 'M3 6h6l2 2h10v11H3z',
'tag' => 'M3 12V3h9l9 9l-9 9zM7.5 7.5h.01',
'comment' => 'M4 5h16v11H8l-4 4z',
'link' => 'M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1',
'check' => 'M5 12l5 5L20 7',
'list' => 'M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01',
);
if ( ! isset( $paths[ $name ] ) ) {
return '';
}
return sprintf(
'',
esc_attr( $name ),
esc_attr( $paths[ $name ] )
);
}
}