id ) { $hidden[] = self::COLUMN; } return $hidden; } private static function types() { $types = array_keys( get_post_types( array( 'public' => true, 'show_ui' => true ) ) ); return array_values( array_diff( $types, array( 'attachment' ) ) ); } public static function add_column( $cols ) { $label = '' . esc_html__( 'Contacts', 'ai-eshop-optimizer' ) . ''; $out = array(); foreach ( $cols as $key => $value ) { $out[ $key ] = $value; if ( 'aieo_views' === $key ) { $out[ self::COLUMN ] = $label; } } if ( ! isset( $out[ self::COLUMN ] ) ) { $out[ self::COLUMN ] = $label; } return $out; } public static function sortable_column( $cols ) { $cols[ self::COLUMN ] = array( self::COLUMN, true ); return $cols; } public static function prime( $posts, $query ) { if ( ! is_admin() || empty( $posts ) || ! is_object( $query ) || ! $query->is_main_query() ) { return $posts; } $ids = array(); foreach ( $posts as $p ) { if ( is_object( $p ) && isset( $p->ID ) ) { $ids[] = (int) $p->ID; } } self::load( $ids ); return $posts; } public static function order_clauses( $clauses, $query ) { if ( ! is_admin() || ! is_object( $query ) || ! $query->is_main_query() || self::COLUMN !== $query->get( 'orderby' ) ) { return $clauses; } if ( ! AIEO_Page_Actions::ensure_schema() ) { return $clauses; } global $wpdb; $t = AIEO_Page_Actions::table(); $in = "'" . implode( "','", array_map( 'esc_sql', AIEO_Page_Actions::CONTACTS ) ) . "'"; $order = 'ASC' === strtoupper( (string) $query->get( 'order' ) ) ? 'ASC' : 'DESC'; $clauses['join'] .= " LEFT JOIN (SELECT object_id, SUM(n) AS contacts FROM `{$t}` WHERE kind = 'p' AND action IN ({$in}) GROUP BY object_id) aieo_pa ON aieo_pa.object_id = {$wpdb->posts}.ID"; $clauses['orderby'] = "COALESCE(aieo_pa.contacts, 0) {$order}, {$wpdb->posts}.post_date DESC"; return $clauses; } public static function render_column( $col, $post_id ) { if ( self::COLUMN !== $col ) { return; } $post_id = (int) $post_id; if ( ! isset( self::$read[ $post_id ] ) ) { $listed = isset( $GLOBALS['wp_query'] ) && is_object( $GLOBALS['wp_query'] ) ? (array) $GLOBALS['wp_query']->posts : array(); $ids = array( $post_id ); foreach ( $listed as $p ) { $ids[] = is_object( $p ) ? (int) ( $p->ID ?? 0 ) : (int) $p; } self::load( $ids ); } $c = self::$rows[ $post_id ] ?? array(); $all = 0; $recent = 0; foreach ( AIEO_Page_Actions::CONTACTS as $a ) { $all += (int) ( $c[ $a ]['all'] ?? 0 ); $recent += (int) ( $c[ $a ]['recent'] ?? 0 ); } $parts = self::parts( $c ); if ( 0 === $all && ! $parts ) { echo '' . esc_html__( 'No contacts counted yet', 'ai-eshop-optimizer' ) . ''; return; } $lines = array(); if ( $all > 0 ) { $lines[] = '' . esc_html( number_format_i18n( $all ) ) . ''; $lines[] = esc_html( sprintf( __( '30 days: %s', 'ai-eshop-optimizer' ), number_format_i18n( $recent ) ) ); } $kinds = array_map( static function ( $part ) { return str_replace( array( ' ', '-' ), array( "\u{00A0}", "\u{2011}" ), $part ); }, $parts ); $lines[] = '' . esc_html( implode( ' ยท ', $kinds ) ) . ''; echo implode( '
', $lines ); } public static function parts( array $c ) { $n = static function ( $a ) use ( $c ) { return (int) ( $c[ $a ]['all'] ?? 0 ); }; $f = 'number_format_i18n'; $parts = array(); if ( $n( 'call' ) ) { $parts[] = sprintf( _n( '%s call', '%s calls', $n( 'call' ), 'ai-eshop-optimizer' ), $f( $n( 'call' ) ) ); } foreach ( array( 'whatsapp' => 'WhatsApp', 'viber' => 'Viber', 'messenger' => 'Messenger' ) as $a => $name ) { if ( $n( $a ) ) { $parts[] = sprintf( _x( '%1$s %2$s', 'taps on a chat app', 'ai-eshop-optimizer' ), $f( $n( $a ) ), $name ); } } if ( $n( 'email' ) ) { $parts[] = sprintf( _n( '%s e-mail', '%s e-mails', $n( 'email' ), 'ai-eshop-optimizer' ), $f( $n( 'email' ) ) ); } if ( $n( 'form_submit' ) ) { $parts[] = sprintf( _n( '%s form sent', '%s forms sent', $n( 'form_submit' ), 'ai-eshop-optimizer' ), $f( $n( 'form_submit' ) ) ); } if ( $n( 'quote_start' ) ) { $parts[] = $n( 'quote_sent' ) ? sprintf( _n( '%1$s quote, %2$s sent', '%1$s quotes, %2$s sent', $n( 'quote_start' ), 'ai-eshop-optimizer' ), $f( $n( 'quote_start' ) ), $f( $n( 'quote_sent' ) ) ) : sprintf( _n( '%s quote', '%s quotes', $n( 'quote_start' ), 'ai-eshop-optimizer' ), $f( $n( 'quote_start' ) ) ); } if ( $n( 'quote_sent_here' ) && $n( 'quote_sent_here' ) !== $n( 'quote_sent' ) ) { $parts[] = sprintf( _n( '%s quote sent from here', '%s quotes sent from here', $n( 'quote_sent_here' ), 'ai-eshop-optimizer' ), $f( $n( 'quote_sent_here' ) ) ); } return $parts; } private static function load( array $ids ) { $ids = array_values( array_diff( array_filter( array_unique( array_map( 'intval', $ids ) ) ), array_keys( self::$read ) ) ); if ( ! $ids ) { return; } foreach ( $ids as $id ) { self::$read[ $id ] = true; } self::$rows = AIEO_Page_Actions::for_posts( $ids, 30 ) + self::$rows; } }