__( 'Order confirmation, above the order details', 'ai-eshop-optimizer' ), 'woocommerce_thankyou' => __( 'Order confirmation, below the order details', 'ai-eshop-optimizer' ), 'woocommerce_order_details_after_order_table' => __( 'Order confirmation, after the item table', 'ai-eshop-optimizer' ), 'woocommerce_account_view_order' => __( 'My account, when viewing an order', 'ai-eshop-optimizer' ), ); } public static function all() { $raw = get_option( self::OPTION, array() ); if ( ! is_array( $raw ) ) { $raw = array(); } $out = array(); foreach ( $raw as $blueprint => $p ) { $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); if ( '' === $blueprint || ! is_array( $p ) ) { continue; } $hook = isset( $p['hook'] ) ? (string) $p['hook'] : ''; $out[ $blueprint ] = array( 'enabled' => ! empty( $p['enabled'] ), 'hook' => isset( self::hooks()[ $hook ] ) ? $hook : '', 'priority' => isset( $p['priority'] ) ? (int) $p['priority'] : 20, 'statuses' => array_values( array_filter( array_map( 'sanitize_key', (array) ( isset( $p['statuses'] ) ? $p['statuses'] : array() ) ) ) ), 'once_per_order' => ! isset( $p['once_per_order'] ) || ! empty( $p['once_per_order'] ), 'page_id' => isset( $p['page_id'] ) ? (int) $p['page_id'] : 0, 'expires_days' => isset( $p['expires_days'] ) ? max( 0, (int) $p['expires_days'] ) : 30, 'title' => isset( $p['title'] ) ? (string) $p['title'] : '', 'intro' => isset( $p['intro'] ) ? (string) $p['intro'] : '', 'thanks' => isset( $p['thanks'] ) ? (string) $p['thanks'] : '', 'section_size' => self::size( isset( $p['section_size'] ) ? $p['section_size'] : 0 ), 'question_size' => self::size( isset( $p['question_size'] ) ? $p['question_size'] : 0 ), 'option_size' => self::size( isset( $p['option_size'] ) ? $p['option_size'] : 0 ), 'help_size' => self::size( isset( $p['help_size'] ) ? $p['help_size'] : 0 ), ); } return $out; } public static function get( $blueprint ) { $all = self::all(); $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); return isset( $all[ $blueprint ] ) ? $all[ $blueprint ] : array( 'enabled' => false, 'hook' => '', 'priority' => 20, 'statuses' => array(), 'once_per_order' => true, 'page_id' => 0, 'expires_days' => 30, 'title' => '', 'intro' => '', 'thanks' => '', 'section_size' => 0, 'question_size' => 0, 'option_size' => 0, 'help_size' => 0, ); } public static function size( $v ) { $v = (int) $v; if ( $v <= 0 ) { return 0; } return max( 10, min( 48, $v ) ); } public static function save( $blueprint, array $patch ) { $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); if ( '' === $blueprint ) { return false; } $all = self::all(); $all[ $blueprint ] = array_merge( self::get( $blueprint ), $patch ); return update_option( self::OPTION, $all, false ); } public static function token( $blueprint, $order_id, $email, $expires = 0 ) { return substr( hash_hmac( 'sha256', 'aieo_survey|' . AIEO_Survey_Blueprint::code( $blueprint ) . '|' . (int) $order_id . '|' . strtolower( trim( (string) $email ) ) . '|' . (int) $expires, wp_salt( 'auth' ) ), 0, 24 ); } public static function url( $blueprint, $order_id = 0, $email = '', $run_id = 0 ) { $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); $placement = self::get( $blueprint ); $page = (int) $placement['page_id']; if ( $page <= 0 ) { return ''; } $base = get_permalink( $page ); if ( ! $base ) { return ''; } $expires = $placement['expires_days'] > 0 ? time() + ( $placement['expires_days'] * DAY_IN_SECONDS ) : 0; return add_query_arg( array( self::ARG_SURVEY => $blueprint, self::ARG_ORDER => (int) $order_id, self::ARG_EMAIL => rawurlencode( strtolower( trim( (string) $email ) ) ), self::ARG_RUN => (int) $run_id, 'aieo_exp' => (int) $expires, self::ARG_TOKEN => self::token( $blueprint, $order_id, $email, $expires ), ), $base ); } public static function verify( array $args ) { $blueprint = AIEO_Survey_Blueprint::code( isset( $args[ self::ARG_SURVEY ] ) ? $args[ self::ARG_SURVEY ] : '' ); if ( '' === $blueprint || ! AIEO_Survey_Blueprint::get( $blueprint ) ) { return null; } $order_id = isset( $args[ self::ARG_ORDER ] ) ? (int) $args[ self::ARG_ORDER ] : 0; $email = isset( $args[ self::ARG_EMAIL ] ) ? strtolower( trim( rawurldecode( (string) $args[ self::ARG_EMAIL ] ) ) ) : ''; $run_id = isset( $args[ self::ARG_RUN ] ) ? (int) $args[ self::ARG_RUN ] : 0; $expires = isset( $args['aieo_exp'] ) ? (int) $args['aieo_exp'] : 0; $token = isset( $args[ self::ARG_TOKEN ] ) ? (string) $args[ self::ARG_TOKEN ] : ''; if ( '' === $token || ! hash_equals( self::token( $blueprint, $order_id, $email, $expires ), $token ) ) { return null; } if ( $expires > 0 && time() > $expires ) { return null; } return array( 'blueprint' => $blueprint, 'order_id' => $order_id, 'email' => $email, 'run_id' => $run_id ); } public static function answered( $blueprint, $order_id ) { $order_id = (int) $order_id; $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); if ( $order_id <= 0 || '' === $blueprint ) { return false; } $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : null; if ( $order ) { $stamped = (array) json_decode( (string) $order->get_meta( self::ORDER_META ), true ); if ( ! empty( $stamped[ $blueprint ] ) ) { return true; } } global $wpdb; $entries = AIEO_Form_CPT::entries_table(); $found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$entries} WHERE order_id = %d AND surface = %s", $order_id, 'aieo_' . $blueprint ) ); if ( $found > 0 && $order ) { self::stamp( $blueprint, $order_id ); } return $found > 0; } public static function stamp( $blueprint, $order_id ) { if ( ! function_exists( 'wc_get_order' ) ) { return; } $order = wc_get_order( (int) $order_id ); if ( ! $order ) { return; } $blueprint = AIEO_Survey_Blueprint::code( $blueprint ); $stamped = (array) json_decode( (string) $order->get_meta( self::ORDER_META ), true ); if ( ! empty( $stamped[ $blueprint ] ) ) { return; } $stamped[ $blueprint ] = 1; $order->update_meta_data( self::ORDER_META, wp_json_encode( $stamped ) ); $order->save(); } public static function should_render( $blueprint, $order_id ) { $placement = self::get( $blueprint ); if ( empty( $placement['enabled'] ) || '' === $placement['hook'] ) { return false; } $order = function_exists( 'wc_get_order' ) ? wc_get_order( (int) $order_id ) : null; if ( ! $order ) { return false; } if ( $placement['statuses'] && ! in_array( $order->get_status(), $placement['statuses'], true ) ) { return false; } if ( ! empty( $placement['once_per_order'] ) && self::answered( $blueprint, $order_id ) ) { return false; } return true; } public static function register() { add_action( 'wp', array( __CLASS__, 'attach' ) ); } public static function attach() { if ( is_admin() ) { return; } foreach ( self::all() as $blueprint => $placement ) { if ( empty( $placement['enabled'] ) || '' === $placement['hook'] ) { continue; } add_action( $placement['hook'], static function ( $order_id ) use ( $blueprint ) { if ( is_object( $order_id ) && method_exists( $order_id, 'get_id' ) ) { $order_id = $order_id->get_id(); } AIEO_Survey_Renderer::render_for_order( $blueprint, (int) $order_id ); }, (int) $placement['priority'], 1 ); } } }