90 ), is_array( $atts ) ? $atts : array(), self::SHORTCODE ); return self::render( (int) $atts['days'] ); } public static function register_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } register_block_type( self::BLOCK, array( 'api_version' => 2, 'title' => __( 'My contributions', 'ai-eshop-optimizer' ), 'description' => __( 'Shows the logged-in contributor their own leads, orders and revenue. Shows a login prompt to everyone else.', 'ai-eshop-optimizer' ), 'category' => 'widgets', 'icon' => 'chart-line', 'attributes' => array( 'days' => array( 'type' => 'number', 'default' => 90 ), ), 'render_callback' => static function ( $attrs ) { return self::render( isset( $attrs['days'] ) ? (int) $attrs['days'] : 90 ); }, ) ); } public static function editor_asset() { if ( ! wp_script_is( 'wp-blocks', 'registered' ) ) { return; } wp_register_script( 'aieo-affiliate-block', '', array( 'wp-blocks', 'wp-element', 'wp-server-side-render', 'wp-i18n' ), AIEO_VERSION, true ); wp_enqueue_script( 'aieo-affiliate-block' ); wp_add_inline_script( 'aieo-affiliate-block', "(function(b,e,s){if(!b||!s)return;b.registerBlockType('" . self::BLOCK . "',{title:'" . esc_js( __( 'My contributions', 'ai-eshop-optimizer' ) ) . "',icon:'chart-line',category:'widgets',attributes:{days:{type:'number',default:90}},edit:function(p){return e.createElement(s,{block:'" . self::BLOCK . "',attributes:p.attributes});},save:function(){return null;}});})(window.wp&&wp.blocks,window.wp&&wp.element,window.wp&&wp.serverSideRender);" ); } public static function is_portal_only_user( $user_id = 0 ) { $user_id = $user_id ? (int) $user_id : get_current_user_id(); if ( $user_id <= 0 ) { return false; } foreach ( array( 'edit_posts', 'edit_pages', 'manage_options', 'manage_woocommerce', 'upload_files', 'moderate_comments' ) as $cap ) { if ( user_can( $user_id, $cap ) ) { return false; } } return self::is_contributor( $user_id ); } public static function maybe_hide_admin_bar( $show ) { return self::is_portal_only_user() ? false : $show; } public static function block_admin_access() { if ( wp_doing_ajax() ) { return; } $script = isset( $_SERVER['PHP_SELF'] ) ? basename( (string) $_SERVER['PHP_SELF'] ) : ''; if ( in_array( $script, array( 'admin-ajax.php', 'admin-post.php' ), true ) ) { return; } if ( ! self::is_portal_only_user() ) { return; } wp_safe_redirect( self::portal_url() ); exit; } public static function portal_url() { $id = (int) get_option( 'aieo_contributor_page_id', 0 ); $url = $id > 0 ? get_permalink( $id ) : home_url( '/' ); return apply_filters( 'aieo_contributor_portal_url', $url ? $url : home_url( '/' ) ); } public static function is_contributor( $user_id ) { global $wpdb; if ( ! self::boot() ) { return false; } $t = AIEO_DMM_Contributors::table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) { return false; } return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM `{$t}` WHERE user_id = %d AND enabled = 1 LIMIT 1", (int) $user_id ) ); } private static function boot() { foreach ( array( 'class-aieo-dmm-conversions-registry.php', 'class-aieo-dmm-contributors.php', ) as $f ) { $p = AIEO_DIR . 'includes/dmm/' . $f; if ( file_exists( $p ) ) { require_once $p; } } return class_exists( 'AIEO_DMM_Contributors' ); } private static function login_form() { $html = (string) wp_login_form( array( 'echo' => false, 'redirect' => self::current_url() ) ); if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) { return $html; } $tags = new WP_HTML_Tag_Processor( $html ); $remember = false; while ( $tags->next_tag() ) { $tag = $tags->get_tag(); if ( 'FORM' === $tag ) { $tags->add_class( 'aieo-cp-login' ); } elseif ( 'P' === $tag ) { $remember = false !== strpos( ' ' . (string) $tags->get_attribute( 'class' ) . ' ', ' login-remember ' ); } elseif ( 'LABEL' === $tag ) { $tags->add_class( $remember ? 'aieo-cp-check-label' : 'aieo-cp-label' ); } elseif ( 'INPUT' === $tag ) { $type = strtolower( (string) $tags->get_attribute( 'type' ) ); if ( 'checkbox' === $type ) { $tags->add_class( 'aieo-cp-checkbox' ); } elseif ( 'submit' === $type ) { $tags->add_class( 'aieo-cp-submit' ); } elseif ( 'hidden' !== $type ) { $tags->add_class( 'aieo-cp-input' ); } } } return $tags->get_updated_html(); } public static function render( $days = 90 ) { if ( ! is_user_logged_in() ) { return self::wrap( '
' . esc_html__( 'Please log in to see your contributions.', 'ai-eshop-optimizer' ) . '
' . '' . esc_html__( 'Log in to your account', 'ai-eshop-optimizer' ) . '
' . self::login_form() ); } if ( ! self::boot() ) { return self::wrap( '' . esc_html__( 'Reporting is not available right now.', 'ai-eshop-optimizer' ) . '
' ); } $user_id = get_current_user_id(); $user = wp_get_current_user(); if ( ! self::is_contributor( $user_id ) ) { return self::wrap( '' . esc_html__( 'Your account is not linked to a campaign yet, so there is nothing to show.', 'ai-eshop-optimizer' ) . '
' . '' . esc_html__( 'Once the shop links your campaign to your account, your full history appears here immediately, including everything before the link was made.', 'ai-eshop-optimizer' ) . '
' . '' . '' . esc_html__( 'Go to my account', 'ai-eshop-optimizer' ) . ' ' . '' . esc_html__( 'Back to the shop', 'ai-eshop-optimizer' ) . '' . '
' ); } $days = max( 0, (int) $days ); $until = gmdate( 'Y-m-d H:i:s' ); $since = $days > 0 ? gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS ) : '1970-01-01 00:00:00'; $rows = AIEO_DMM_Contributors::leaderboard( $since, $until ); $mine = null; foreach ( $rows as $r ) { if ( (int) $r['user_id'] === $user_id ) { $mine = $r; break; } } ob_start(); echo '' . esc_html__( 'Nothing recorded in this period yet.', 'ai-eshop-optimizer' ) . '
'; return self::wrap( ob_get_clean() ); } $leads = (int) $mine['leads']; $orders = (int) $mine['orders']; $revenue = (float) $mine['revenue']; echo '' . esc_html( $days > 0 ? sprintf( __( 'Last %d days.', 'ai-eshop-optimizer' ), $days ) : __( 'All time.', 'ai-eshop-optimizer' ) ) . ' ' . esc_html__( 'Dates are GMT.', 'ai-eshop-optimizer' ) . '
'; self::render_campaigns( $user_id, $since, $until ); echo '' . esc_html__( 'These are the conversions that arrived through your campaigns. A visitor who arrives through your link and buys later, from somewhere else, is not counted here.', 'ai-eshop-optimizer' ) . '
'; return self::wrap( ob_get_clean() ); } private static function render_campaigns( $user_id, $since, $until ) { global $wpdb; $conv = AIEO_DMM_Conversions_Registry::table(); $rt = AIEO_DMM_Contributors::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT c.canon_campaign AS campaign, SUM(c.conversion_type = 'goal') AS leads, SUM(c.conversion_type = 'order') AS orders, ROUND(SUM(CASE WHEN c.conversion_type = 'order' THEN c.conversion_value ELSE 0 END), 2) AS revenue, MAX(c.occurred_at) AS last_seen FROM `{$conv}` c JOIN `{$rt}` r ON r.user_id = %d AND r.enabled = 1 AND ( ( r.match_field = 'campaign' AND ( ( r.match_type = 'exact' AND c.canon_campaign = r.match_canonical ) OR ( r.match_type = 'starts_with' AND c.canon_campaign LIKE CONCAT(r.match_canonical, '%%') ) OR ( r.match_type = 'contains' AND c.canon_campaign LIKE CONCAT('%%', r.match_canonical, '%%') ) ) ) OR ( r.match_field = 'source' AND ( ( r.match_type = 'exact' AND c.canon_source = r.match_canonical ) OR ( r.match_type = 'starts_with' AND c.canon_source LIKE CONCAT(r.match_canonical, '%%') ) OR ( r.match_type = 'contains' AND c.canon_source LIKE CONCAT('%%', r.match_canonical, '%%') ) ) ) ) WHERE c.occurred_at BETWEEN %s AND %s GROUP BY c.canon_campaign ORDER BY revenue DESC, leads DESC LIMIT 50", $user_id, $since, $until ), ARRAY_A ); if ( ! $rows ) { return; } if ( ! class_exists( 'AIEO_Public_Tables' ) && is_readable( AIEO_DIR . 'includes/dmm/reports/class-aieo-public-tables.php' ) ) { require_once AIEO_DIR . 'includes/dmm/reports/class-aieo-public-tables.php'; } if ( ! class_exists( 'AIEO_Public_Tables' ) || ! AIEO_Public_Tables::front_engine() ) { return; } if ( ! class_exists( 'AIEO_Table' ) ) { require_once AIEO_DIR . 'includes/dmm/reports/class-aieo-table.php'; } $list = array(); foreach ( $rows as $i => $r ) { $campaign = (string) $r['campaign']; $day = substr( (string) $r['last_seen'], 0, 10 ); $list[] = array( '_id' => $i + 1, 'campaign' => $campaign, 'leads' => number_format_i18n( (int) $r['leads'] ), 'orders' => number_format_i18n( (int) $r['orders'] ), 'revenue' => AIEO_DMM_Conversions_Registry::money( $r['revenue'] ), 'last_activity' => $day, '_s' => array( 'campaign' => $campaign, 'leads' => (int) $r['leads'], 'orders' => (int) $r['orders'], 'revenue' => (float) $r['revenue'], 'last_activity' => $day, ), '_text' => $campaign, ); } $cap = 50; echo AIEO_Public_Tables::front_css( '.aieo-cp' ); echo AIEO_Table::local( 'aieo-cp-campaigns', array( array( 'key' => 'campaign', 'label' => __( 'Campaign', 'ai-eshop-optimizer' ), 'sort_type' => 'text' ), array( 'key' => 'leads', 'label' => __( 'Leads', 'ai-eshop-optimizer' ), 'type' => 'num', 'sort_type' => 'number' ), array( 'key' => 'orders', 'label' => __( 'Orders', 'ai-eshop-optimizer' ), 'type' => 'num', 'sort_type' => 'number' ), array( 'key' => 'revenue', 'label' => __( 'Revenue', 'ai-eshop-optimizer' ), 'type' => 'num', 'sort_type' => 'number' ), array( 'key' => 'last_activity', 'label' => __( 'Last activity', 'ai-eshop-optimizer' ), 'sort_type' => 'date' ), ), $list, array( 'prefix' => 'cp_', 'size' => 25, 'sizes' => array( 25, 50 ), 'caption' => __( 'Your campaigns', 'ai-eshop-optimizer' ), 'search' => array( 'label' => __( 'Search', 'ai-eshop-optimizer' ), 'placeholder' => __( 'Campaign', 'ai-eshop-optimizer' ), ), 'capped' => count( $list ) >= $cap ? array( 'shown' => count( $list ), 'text' => sprintf( __( 'The %d campaigns with the most revenue.', 'ai-eshop-optimizer' ), $cap ), ) : null, ) ); } private static function tile( $label, $value ) { echo '