'recommended',
'index' => $transactional ? 0 : 1,
'follow' => $private_completion ? 0 : 1,
'noarchive' => $transactional ? 1 : 0,
'nosnippet' => $private_completion ? 1 : 0,
'noimageindex' => 0,
'include_sitemap' => $transactional ? 0 : 1,
'canonical_url' => '',
'social_title' => '',
'social_description' => '',
'social_image' => '',
'analytics_enabled'=> 1,
);
}
/** Return saved controls merged with recommended defaults. */
public static function search_settings( $page_id, $step_type = '' ) {
$page_id = absint( $page_id );
if ( ! $step_type ) {
$step_type = (string) get_post_meta( $page_id, '_divi_powers_funnel_step_type', true );
}
$saved = get_post_meta( $page_id, self::SEARCH_META, true );
$saved = is_array( $saved ) ? $saved : array();
return wp_parse_args( $saved, self::default_search_settings( $step_type ) );
}
/** Save sanitized search and sharing settings. */
public static function save_search_settings( $page_id, $input, $step_type = '' ) {
$page_id = absint( $page_id );
if ( ! $page_id || 'page' !== get_post_type( $page_id ) ) {
return false;
}
$defaults = self::default_search_settings( $step_type );
$input = is_array( $input ) ? $input : array();
$visibility = sanitize_key( $input['visibility'] ?? $defaults['visibility'] );
if ( ! in_array( $visibility, array( 'recommended', 'public', 'hidden' ), true ) ) {
$visibility = 'recommended';
}
$clean = array(
'visibility' => $visibility,
'index' => empty( $input['index'] ) ? 0 : 1,
'follow' => empty( $input['follow'] ) ? 0 : 1,
'noarchive' => empty( $input['noarchive'] ) ? 0 : 1,
'nosnippet' => empty( $input['nosnippet'] ) ? 0 : 1,
'noimageindex' => empty( $input['noimageindex'] ) ? 0 : 1,
'include_sitemap' => empty( $input['include_sitemap'] ) ? 0 : 1,
'canonical_url' => esc_url_raw( $input['canonical_url'] ?? '' ),
'social_title' => sanitize_text_field( $input['social_title'] ?? '' ),
'social_description' => sanitize_textarea_field( $input['social_description'] ?? '' ),
'social_image' => esc_url_raw( $input['social_image'] ?? '' ),
'analytics_enabled' => empty( $input['analytics_enabled'] ) ? 0 : 1,
);
if ( 'recommended' === $visibility ) {
$clean = array_merge( $clean, array_intersect_key( $defaults, array_flip( array( 'index', 'follow', 'noarchive', 'nosnippet', 'noimageindex', 'include_sitemap' ) ) ) );
} elseif ( 'public' === $visibility ) {
$clean['index'] = 1;
$clean['follow'] = 1;
$clean['include_sitemap'] = 1;
} elseif ( 'hidden' === $visibility ) {
$clean['index'] = 0;
$clean['include_sitemap'] = 0;
}
update_post_meta( $page_id, self::SEARCH_META, $clean );
update_post_meta( $page_id, self::SITEMAP_EXCLUDE_META, empty( $clean['include_sitemap'] ) || empty( $clean['index'] ) ? 1 : 0 );
return $clean;
}
/** Find the active funnel context for a page, including legacy Pro maps. */
public static function context_for_page( $page_id ) {
$page_id = absint( $page_id );
if ( ! $page_id ) {
return array();
}
$saved = get_post_meta( $page_id, self::CONTEXT_META, true );
if ( is_array( $saved ) ) {
$saved = self::sanitize_context( $saved );
if ( $saved['funnel_id'] && 'trash' !== get_post_status( $saved['funnel_id'] ) ) {
return $saved;
}
}
$funnels = get_posts(
array(
'post_type' => 'fivedp_funnel',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'modified',
'order' => 'DESC',
'meta_key' => '_divi_powers_funnel_status', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Legacy fallback; generated pages normally use their direct saved funnel context.
'meta_value' => 'active', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Legacy fallback; generated pages normally use their direct saved funnel context.
)
);
foreach ( $funnels as $funnel ) {
$definition = get_post_meta( $funnel->ID, '_divi_powers_funnel_definition', true );
$nodes = is_array( $definition ) && ! empty( $definition['nodes'] ) && is_array( $definition['nodes'] ) ? array_values( $definition['nodes'] ) : array();
foreach ( $nodes as $position => $node ) {
if ( ! is_array( $node ) ) {
continue;
}
$node_page = self::node_page_id( $node );
if ( $page_id !== $node_page ) {
continue;
}
$step_type = self::infer_step_type( $node );
return self::sanitize_context(
array(
'funnel_id' => $funnel->ID,
'funnel_name' => $funnel->post_title,
'step_id' => sanitize_key( $node['id'] ?? 'step-' . ( $position + 1 ) ),
'step_type' => $step_type,
'step_label' => sanitize_text_field( $node['label'] ?? ucfirst( str_replace( '_', ' ', $step_type ) ) ),
'step_position' => $position + 1,
'is_entry' => ! empty( $node['is_entry'] ) || 0 === $position,
'is_goal' => ! empty( $node['is_goal'] ),
'analytics_enabled'=> self::search_settings( $page_id, $step_type )['analytics_enabled'],
)
);
}
}
return array();
}
/** Build a stable next-step URL that survives page replacement and reordering. */
public static function next_url( $funnel_id, $step_id ) {
return add_query_arg(
array(
self::ROUTE_QUERY => absint( $funnel_id ),
'dp_step' => sanitize_key( $step_id ),
),
home_url( '/' )
);
}
/** Read basic local funnel metrics. */
public static function metrics( $funnel_id ) {
$metrics = get_option( self::METRICS_PREFIX . absint( $funnel_id ), array() );
return wp_parse_args(
is_array( $metrics ) ? $metrics : array(),
array(
'entries' => 0,
'step_views' => 0,
'step_counts' => array(),
'last_event' => '',
)
);
}
/** Lightweight Site Kit presence check without coupling to private internals. */
public static function site_kit_status() {
if ( is_admin() && ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$active = function_exists( 'is_plugin_active' ) && is_plugin_active( 'google-site-kit/google-site-kit.php' );
$active = $active || defined( 'GOOGLESITEKIT_VERSION' ) || class_exists( 'Google\\Site_Kit\\Plugin' );
return array(
'active' => (bool) $active,
'dashboard_url'=> admin_url( 'admin.php?page=googlesitekit-dashboard' ),
'settings_url' => admin_url( 'admin.php?page=googlesitekit-settings' ),
);
}
public function filter_robots( $robots ) {
if ( ! is_singular( 'page' ) ) {
return $robots;
}
$page_id = get_queried_object_id();
$context = self::context_for_page( $page_id );
if ( ! $context ) {
return $robots;
}
$settings = self::search_settings( $page_id, $context['step_type'] );
foreach ( array( 'noindex', 'nofollow', 'noarchive', 'nosnippet', 'noimageindex' ) as $directive ) {
unset( $robots[ $directive ] );
}
if ( empty( $settings['index'] ) ) {
$robots['noindex'] = true;
}
if ( empty( $settings['follow'] ) ) {
$robots['nofollow'] = true;
}
foreach ( array( 'noarchive', 'nosnippet', 'noimageindex' ) as $directive ) {
if ( ! empty( $settings[ $directive ] ) ) {
$robots[ $directive ] = true;
}
}
return $robots;
}
public function filter_canonical( $canonical, $post ) {
if ( ! $post instanceof WP_Post || 'page' !== $post->post_type ) {
return $canonical;
}
$context = self::context_for_page( $post->ID );
if ( ! $context ) {
return $canonical;
}
$settings = self::search_settings( $post->ID, $context['step_type'] );
return ! empty( $settings['canonical_url'] ) ? $settings['canonical_url'] : $canonical;
}
public function filter_sitemap_query( $args, $post_type ) {
if ( 'page' !== $post_type ) {
return $args;
}
$exclude = array(
'relation' => 'OR',
array( 'key' => self::SITEMAP_EXCLUDE_META, 'compare' => 'NOT EXISTS' ),
array( 'key' => self::SITEMAP_EXCLUDE_META, 'value' => '1', 'compare' => '!=' ),
);
if ( ! empty( $args['meta_query'] ) ) {
$args['meta_query'] = array( 'relation' => 'AND', $args['meta_query'], $exclude ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Core sitemap query must exclude funnel steps explicitly marked private from discovery.
} else {
$args['meta_query'] = $exclude; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Core sitemap query must exclude funnel steps explicitly marked private from discovery.
}
return $args;
}
public function render_social_meta() {
if ( ! is_singular( 'page' ) || self::seo_plugin_controls_social() ) {
return;
}
$page_id = get_queried_object_id();
$context = self::context_for_page( $page_id );
if ( ! $context ) {
return;
}
$settings = self::search_settings( $page_id, $context['step_type'] );
$title = trim( (string) $settings['social_title'] );
$description = trim( (string) $settings['social_description'] );
$image = trim( (string) $settings['social_image'] );
if ( ! $title && ! $description && ! $image ) {
return;
}
if ( $title ) {
echo '' . "\n";
}
if ( $description ) {
echo '' . "\n";
}
if ( $image ) {
echo '' . "\n";
echo '' . "\n";
}
}
public function route_next_step() {
$funnel_id = isset( $_GET[ self::ROUTE_QUERY ] ) ? absint( $_GET[ self::ROUTE_QUERY ] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$step_id = isset( $_GET['dp_step'] ) ? sanitize_key( wp_unslash( $_GET['dp_step'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! $funnel_id || ! $step_id ) {
return;
}
$funnel = get_post( $funnel_id );
if ( ! $funnel || 'fivedp_funnel' !== $funnel->post_type || 'trash' === $funnel->post_status ) {
wp_safe_redirect( home_url( '/' ) );
exit;
}
$definition = get_post_meta( $funnel_id, '_divi_powers_funnel_definition', true );
$nodes = is_array( $definition ) && ! empty( $definition['nodes'] ) && is_array( $definition['nodes'] ) ? array_values( $definition['nodes'] ) : array();
$target = null;
foreach ( $nodes as $index => $node ) {
if ( sanitize_key( $node['id'] ?? '' ) !== $step_id ) {
continue;
}
$target = $nodes[ $index + 1 ] ?? null;
break;
}
if ( ! $target && ! empty( $definition['edges'] ) ) {
foreach ( $definition['edges'] as $edge ) {
if ( sanitize_key( $edge['source'] ?? '' ) === $step_id ) {
$target_id = sanitize_key( $edge['target'] ?? '' );
foreach ( $nodes as $node ) {
if ( sanitize_key( $node['id'] ?? '' ) === $target_id ) {
$target = $node;
break 2;
}
}
}
}
}
$page_id = is_array( $target ) ? self::node_page_id( $target ) : 0;
$url = $page_id && 'publish' === get_post_status( $page_id ) ? get_permalink( $page_id ) : '';
if ( $url && is_array( $target ) ) {
self::prepare_checkout_cart( $target );
}
wp_safe_redirect( $url ? $url : home_url( '/' ) );
exit;
}
/** Prepare an assigned-product checkout before the router redirects to it. */
private static function prepare_checkout_cart( $target ) {
$step_type = sanitize_key( $target['step_type'] ?? $target['type'] ?? '' );
$product_id = absint( $target['product_id'] ?? 0 );
if ( 'checkout' !== $step_type || ! $product_id || ! function_exists( 'WC' ) || ! function_exists( 'wc_get_product' ) ) {
return false;
}
$product = wc_get_product( $product_id );
if ( ! $product || ! $product->is_purchasable() ) {
return false;
}
if ( ( ! WC() || ! WC()->cart ) && function_exists( 'wc_load_cart' ) ) {
wc_load_cart();
}
if ( ! WC() || ! WC()->cart ) {
return false;
}
$profile_id = absint( $target['profile_id'] ?? 0 );
$definition = $profile_id ? get_post_meta( $profile_id, '_divi_powers_woo_profile_definition', true ) : array();
$definition = is_array( $definition ) ? $definition : array();
$definition = wp_parse_args(
$definition,
array(
'cart_behavior' => 'replace',
'product_ids' => array( $product_id ),
)
);
$profile = array(
'id' => $profile_id,
'definition' => $definition,
);
$definition = apply_filters( 'divi_powers_woo_profile_cart_definition', $definition, $profile );
$definition = is_array( $definition ) ? $definition : $profile['definition'];
$assigned_ids = array_values( array_unique( array_filter( array_map( 'absint', $definition['product_ids'] ?? array( $product_id ) ) ) ) );
if ( ! in_array( $product_id, $assigned_ids, true ) ) {
$assigned_ids[] = $product_id;
}
$cart_behavior = sanitize_key( $definition['cart_behavior'] ?? 'replace' );
if ( 'replace' === $cart_behavior ) {
WC()->cart->empty_cart();
} else {
foreach ( WC()->cart->get_cart() as $cart_key => $item ) {
$item_product_id = absint( $item['variation_id'] ?? 0 ) ?: absint( $item['product_id'] ?? 0 );
if ( in_array( $item_product_id, $assigned_ids, true ) ) {
WC()->cart->remove_cart_item( $cart_key );
}
}
}
$quantity = max( 1, min( 99, absint( apply_filters( 'divi_powers_woo_profile_product_quantity', 1, $product_id, $profile, $definition ) ) ) );
$added = WC()->cart->add_to_cart( $product_id, $quantity );
if ( ! $added ) {
if ( function_exists( 'wc_add_notice' ) ) {
wc_add_notice( __( 'The checkout product could not be added to the cart. Please try again.', '5d-powers-for-divi' ), 'error' );
}
return false;
}
WC()->cart->calculate_totals();
return true;
}
public function enqueue_tracking() {
if ( is_admin() || ! is_singular( 'page' ) ) {
return;
}
$page_id = get_queried_object_id();
$context = self::context_for_page( $page_id );
if ( ! $context || ! self::tracking_is_enabled( $page_id, $context ) ) {
return;
}
$context['analytics_enabled'] = 1;
$token = self::tracking_token( $page_id, $context['funnel_id'], $context['step_id'] );
wp_enqueue_script( 'divi-powers-funnel-tracking', DIVI_POWERS_URL . 'assets/js/funnel-tracking.js', array(), DIVI_POWERS_VERSION, true );
wp_localize_script(
'divi-powers-funnel-tracking',
'DiviPowersFunnelTracking',
array(
'endpoint' => rest_url( 'divi-powers/v1/funnel-event' ),
'token' => $token,
'pageId' => $page_id,
'funnelId' => $context['funnel_id'],
'funnelName'=> $context['funnel_name'],
'stepId' => $context['step_id'],
'stepType' => $context['step_type'],
'stepLabel' => $context['step_label'],
'position' => $context['step_position'],
'isEntry' => ! empty( $context['is_entry'] ),
'isGoal' => ! empty( $context['is_goal'] ),
'testEvent' => isset( $_GET['dp_test_event'] ) ? 1 : 0, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
)
);
}
public function register_routes() {
register_rest_route(
'divi-powers/v1',
'/funnel-event',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'record_event' ),
'permission_callback' => '__return_true',
)
);
}
public function record_event( WP_REST_Request $request ) {
$page_id = absint( $request->get_param( 'page_id' ) );
$funnel_id = absint( $request->get_param( 'funnel_id' ) );
$step_id = sanitize_key( (string) $request->get_param( 'step_id' ) );
$token = sanitize_text_field( (string) $request->get_param( 'token' ) );
if ( ! hash_equals( self::tracking_token( $page_id, $funnel_id, $step_id ), $token ) ) {
return new WP_Error( 'divi_powers_tracking_token', __( 'Invalid funnel tracking token.', '5d-powers-for-divi' ), array( 'status' => 403 ) );
}
$context = self::context_for_page( $page_id );
if ( ! $context || absint( $context['funnel_id'] ) !== $funnel_id || sanitize_key( $context['step_id'] ) !== $step_id ) {
return new WP_Error( 'divi_powers_tracking_context', __( 'The funnel step no longer matches this page.', '5d-powers-for-divi' ), array( 'status' => 400 ) );
}
if ( ! self::tracking_is_enabled( $page_id, $context ) ) {
return new WP_Error( 'divi_powers_tracking_disabled', __( 'Funnel tracking is not enabled for this published step.', '5d-powers-for-divi' ), array( 'status' => 403 ) );
}
$metrics = self::metrics( $funnel_id );
$metrics['step_views'] = absint( $metrics['step_views'] ) + 1;
$metrics['step_counts'][ $step_id ] = absint( $metrics['step_counts'][ $step_id ] ?? 0 ) + 1;
if ( ! empty( $context['is_entry'] ) ) {
$metrics['entries'] = absint( $metrics['entries'] ) + 1;
}
$metrics['last_event'] = current_time( 'mysql', true );
update_option( self::METRICS_PREFIX . $funnel_id, $metrics, false );
return rest_ensure_response( array( 'success' => true ) );
}
/** Require a published page, a live funnel, and the step's saved opt-in. */
private static function tracking_is_enabled( $page_id, array $context ) {
$funnel_id = absint( $context['funnel_id'] ?? 0 );
$settings = self::search_settings( $page_id, sanitize_key( $context['step_type'] ?? '' ) );
return 'publish' === get_post_status( absint( $page_id ) )
&& 'publish' === get_post_status( $funnel_id )
&& 'active' === sanitize_key( (string) get_post_meta( $funnel_id, '_divi_powers_funnel_status', true ) )
&& ! empty( $settings['analytics_enabled'] );
}
private static function sanitize_context( $context ) {
$context = is_array( $context ) ? $context : array();
return array(
'funnel_id' => absint( $context['funnel_id'] ?? 0 ),
'funnel_name' => sanitize_text_field( $context['funnel_name'] ?? '' ),
'step_id' => sanitize_key( $context['step_id'] ?? '' ),
'step_type' => sanitize_key( $context['step_type'] ?? 'content' ),
'step_label' => sanitize_text_field( $context['step_label'] ?? '' ),
'step_position' => max( 1, absint( $context['step_position'] ?? 1 ) ),
'is_entry' => empty( $context['is_entry'] ) ? 0 : 1,
'is_goal' => empty( $context['is_goal'] ) ? 0 : 1,
'analytics_enabled' => empty( $context['analytics_enabled'] ) ? 0 : 1,
);
}
private static function node_page_id( $node ) {
if ( ! empty( $node['page_id'] ) ) {
return absint( $node['page_id'] );
}
if ( 'checkout' === sanitize_key( $node['type'] ?? '' ) && ! empty( $node['profile_id'] ) ) {
$definition = get_post_meta( absint( $node['profile_id'] ), '_divi_powers_woo_profile_definition', true );
return is_array( $definition ) ? absint( $definition['checkout_page_id'] ?? 0 ) : 0;
}
return 0;
}
private static function infer_step_type( $node ) {
if ( ! empty( $node['step_type'] ) ) {
return sanitize_key( $node['step_type'] );
}
if ( 'checkout' === sanitize_key( $node['type'] ?? '' ) ) {
return 'checkout';
}
$label = strtolower( (string) ( $node['label'] ?? '' ) );
if ( ! empty( $node['is_goal'] ) || false !== strpos( $label, 'thank' ) || false !== strpos( $label, 'confirmation' ) ) {
return 'thank_you';
}
if ( false !== strpos( $label, 'landing' ) ) {
return 'landing';
}
if ( false !== strpos( $label, 'offer' ) ) {
return 'offer';
}
return 'content';
}
private static function tracking_token( $page_id, $funnel_id, $step_id ) {
return hash_hmac( 'sha256', absint( $page_id ) . '|' . absint( $funnel_id ) . '|' . sanitize_key( $step_id ), wp_salt( 'nonce' ) );
}
private static function seo_plugin_controls_social() {
return defined( 'WPSEO_VERSION' ) || defined( 'RANK_MATH_VERSION' ) || defined( 'AIOSEO_VERSION' ) || defined( 'SEOPRESS_VERSION' );
}
}