post_content, 'aieo_survey' ) ) {
$wanted = true;
}
}
if ( ! $wanted && isset( $_GET[ AIEO_Survey_Placement::ARG_SURVEY ] ) ) {
$wanted = true;
}
if ( $wanted ) {
self::assets();
}
}
public static function assets() {
if ( ! defined( 'AIEO_DIR' ) || ! defined( 'AIEO_URI' ) ) {
return;
}
$ver = static function ( $rel ) {
$path = AIEO_DIR . $rel;
return file_exists( $path ) ? (string) filemtime( $path ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1' );
};
wp_enqueue_style( 'aieo-tokens', AIEO_URI . 'assets/css/aieo-tokens.css', array(), $ver( 'assets/css/aieo-tokens.css' ) );
wp_enqueue_style( 'aieo-controls', AIEO_URI . 'assets/css/aieo-controls.css', array( 'aieo-tokens' ), $ver( 'assets/css/aieo-controls.css' ) );
wp_enqueue_script( 'aieo-controls', AIEO_URI . 'assets/js/aieo-controls.js', array(), $ver( 'assets/js/aieo-controls.js' ), true );
wp_enqueue_script( 'aieo-survey', AIEO_URI . 'assets/js/aieo-survey.js', array(), $ver( 'assets/js/aieo-survey.js' ), true );
}
public static function shortcode( $atts ) {
$atts = shortcode_atts( array( 'blueprint' => '' ), (array) $atts, 'aieo_survey' );
$args = wp_unslash( $_GET );
$verified = AIEO_Survey_Placement::verify( is_array( $args ) ? $args : array() );
if ( ! $verified ) {
$blueprint = AIEO_Survey_Blueprint::code( $atts['blueprint'] );
if ( '' === $blueprint || ! AIEO_Survey_Blueprint::get( $blueprint ) ) {
return self::notice( __( 'This survey link is not valid, or it has expired.', 'ai-eshop-optimizer' ) );
}
$verified = array( 'blueprint' => $blueprint, 'order_id' => 0, 'email' => '', 'run_id' => 0 );
}
if ( isset( $_GET['aieo_survey_done'] ) ) {
return self::notice( self::thanks_for( $verified['blueprint'] ) );
}
if ( $verified['order_id'] > 0 && AIEO_Survey_Placement::answered( $verified['blueprint'], $verified['order_id'] ) ) {
return self::notice( __( 'You have already answered this one. Thank you.', 'ai-eshop-optimizer' ) );
}
return self::form( $verified['blueprint'], $verified );
}
public static function render_for_order( $blueprint, $order_id ) {
if ( ! empty( $_GET['aieo_survey_done'] )
&& isset( $_GET[ AIEO_Survey_Placement::ARG_SURVEY ] )
&& AIEO_Survey_Blueprint::code( wp_unslash( $_GET[ AIEO_Survey_Placement::ARG_SURVEY ] ) ) === AIEO_Survey_Blueprint::code( $blueprint ) ) {
echo self::notice( self::thanks_for( $blueprint ) );
return;
}
if ( ! AIEO_Survey_Placement::should_render( $blueprint, $order_id ) ) {
return;
}
$order = wc_get_order( (int) $order_id );
if ( ! $order ) {
return;
}
echo self::form(
$blueprint,
array(
'blueprint' => $blueprint,
'order_id' => (int) $order_id,
'email' => (string) $order->get_billing_email(),
'run_id' => 0,
)
);
}
public static function form( $blueprint, array $ctx ) {
$bp = AIEO_Survey_Blueprint::get( $blueprint );
if ( ! $bp || ! class_exists( 'AIEO_Controls' ) ) {
return '';
}
self::assets();
$placement = AIEO_Survey_Placement::get( $blueprint );
$title = ( isset( $placement['title'] ) && '' !== trim( (string) $placement['title'] ) )
? (string) $placement['title'] : (string) $bp['title'];
$intro = ( isset( $placement['intro'] ) && '' !== trim( (string) $placement['intro'] ) )
? (string) $placement['intro'] : (string) $bp['intro'];
$out = '';
if ( '' !== $title || '' !== $intro ) {
$out .= '
';
if ( '' !== $title ) {
$out .= '
' . esc_html( $title ) . '
';
}
if ( '' !== $intro ) {
$out .= '
' . esc_html( $intro ) . '
';
}
$out .= '
';
}
$vars = '';
if ( ! empty( $placement['section_size'] ) ) {
$vars .= '--aieo-survey-section:' . (int) $placement['section_size'] . 'px;';
}
if ( ! empty( $placement['question_size'] ) ) {
$vars .= '--aieo-survey-question:' . (int) $placement['question_size'] . 'px;';
}
if ( ! empty( $placement['option_size'] ) ) {
$vars .= '--aieo-survey-option:' . (int) $placement['option_size'] . 'px;';
}
if ( ! empty( $placement['help_size'] ) ) {
$vars .= '--aieo-survey-help:' . (int) $placement['help_size'] . 'px;';
}
$out .= '';
}
public static function handle_submit() {
$ajax = ! empty( $_POST['aieo_ajax'] );
$fail = static function () use ( $ajax ) {
if ( $ajax ) {
wp_send_json_success( array( 'message' => '' ) );
}
wp_safe_redirect( home_url( '/' ) );
exit;
};
if ( ! isset( $_POST[ self::NONCE ] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ self::NONCE ] ) ), self::ACTION ) ) {
$fail();
}
if ( ! empty( $_POST[ self::HONEYPOT ] ) ) {
$fail();
}
$posted = wp_unslash( $_POST );
$verified = AIEO_Survey_Placement::verify( is_array( $posted ) ? $posted : array() );
if ( ! $verified ) {
$fail();
}
$source = new AIEO_Survey_Source_Native( $verified['blueprint'] );
if ( ! $source->available() ) {
$fail();
}
if ( $verified['order_id'] > 0 && AIEO_Survey_Placement::answered( $verified['blueprint'], $verified['order_id'] ) ) {
self::redirect_done( $verified );
}
$answers = array();
$raw = isset( $posted['aieo_answer'] ) && is_array( $posted['aieo_answer'] ) ? $posted['aieo_answer'] : array();
foreach ( AIEO_Survey_Blueprint::question_codes( $verified['blueprint'] ) as $code ) {
if ( array_key_exists( $code, $raw ) ) {
$answers[ $code ] = $raw[ $code ];
}
}
$entry = $source->record(
$answers,
array(
'order_id' => (int) $verified['order_id'],
'run_id' => (int) $verified['run_id'],
'user_id' => get_current_user_id(),
'eponymous_id' => self::resolve_customer( $verified['email'], (int) $verified['order_id'] ),
'source' => 'aieo',
'status' => 'new',
)
);
if ( ! is_wp_error( $entry ) && $verified['order_id'] > 0 ) {
AIEO_Survey_Placement::stamp( $verified['blueprint'], $verified['order_id'] );
}
if ( ! is_wp_error( $entry ) ) {
do_action( 'aieo_submission', array(
'provider' => 'aieo_survey',
'goal_key' => 'survey_response',
'entry_id' => (int) $entry,
'surface' => $source->surface(),
'email' => (string) $verified['email'],
) );
}
if ( ! is_wp_error( $entry ) ) {
do_action( 'aieo_survey_answered', (int) $entry, $verified['blueprint'], $answers, $verified );
}
if ( $ajax ) {
wp_send_json_success( array( 'message' => self::thanks_for( $verified['blueprint'] ) ) );
}
self::redirect_done( $verified );
}
private static function redirect_done( array $verified ) {
$placement = AIEO_Survey_Placement::get( $verified['blueprint'] );
$base = $placement['page_id'] > 0 ? get_permalink( (int) $placement['page_id'] ) : '';
if ( ! $base ) {
$ref = wp_get_referer();
$base = $ref ? $ref : home_url( '/' );
}
wp_safe_redirect( add_query_arg(
array(
AIEO_Survey_Placement::ARG_SURVEY => $verified['blueprint'],
'aieo_survey_done' => 1,
),
remove_query_arg( array( AIEO_Survey_Placement::ARG_TOKEN, AIEO_Survey_Placement::ARG_EMAIL ), $base )
) );
exit;
}
private static function resolve_customer( $email, $order_id ) {
$resolved = (int) apply_filters( 'aieo_survey_resolve_customer', 0, $email, $order_id );
if ( $resolved > 0 ) {
return $resolved;
}
global $wpdb;
$ci = $wpdb->prefix . 'aieo_customer_intelligence';
if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $ci ) ) !== $ci ) {
return 0;
}
$email = strtolower( trim( (string) $email ) );
if ( '' === $email ) {
return 0;
}
return (int) $wpdb->get_var( $wpdb->prepare(
"SELECT eponymous_id FROM `{$ci}` WHERE email = %s LIMIT 1",
$email
) );
}
private static function thanks_for( $blueprint ) {
$placement = AIEO_Survey_Placement::get( $blueprint );
if ( isset( $placement['thanks'] ) && '' !== trim( (string) $placement['thanks'] ) ) {
return (string) $placement['thanks'];
}
$bp = AIEO_Survey_Blueprint::get( $blueprint );
return ( $bp && '' !== $bp['thanks'] ) ? (string) $bp['thanks'] : __( 'Thank you.', 'ai-eshop-optimizer' );
}
private static function notice( $text ) {
return '' . esc_html( $text ) . '
';
}
}