['p' => [term_id => n], 't' => [term_id => n]]] const SWAPFEED_STATS_POSTS = 'swapfeed_stats_posts'; // [post_id => n] const SWAPFEED_STATS_KEEP = 180; add_action( 'rest_api_init', function () { register_rest_route( 'swapfeed/v1', '/stat', array( 'methods' => 'POST', 'permission_callback' => '__return_true', 'callback' => 'swapfeed_stat_record', ) ); } ); function swapfeed_stat_record( WP_REST_Request $r ) { $s = swapfeed_settings(); if ( empty( $s['stats'] ) ) return new WP_REST_Response( array( 'ok' => false ), 403 ); $a = (string) $r->get_param( 'a' ); $id = absint( $r->get_param( 'id' ) ); $term = absint( $r->get_param( 'term' ) ); if ( ! in_array( $a, array( 'post', 'unpost', 'term', 'unterm' ), true ) ) { return new WP_REST_Response( array( 'ok' => false ), 400 ); } /* Rate limit: 120 events per hour per visitor. Only a salted hash is kept, for one hour. */ $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $key = 'swapfeed_rl_' . substr( md5( $ip . wp_salt( 'nonce' ) ), 0, 16 ); $n = (int) get_transient( $key ); if ( $n >= 120 ) return new WP_REST_Response( array( 'ok' => false ), 429 ); set_transient( $key, $n + 1, HOUR_IN_SECONDS ); $day = wp_date( 'Ymd' ); $days = get_option( SWAPFEED_STATS_DAYS, array() ); if ( ! is_array( $days ) ) $days = array(); if ( ! isset( $days[ $day ] ) ) $days[ $day ] = array( 'p' => array(), 't' => array() ); $delta = ( 'post' === $a || 'term' === $a ) ? 1 : -1; if ( 'post' === $a || 'unpost' === $a ) { $p = get_post( $id ); if ( ! $p || 'publish' !== $p->post_status || ! is_post_type_viewable( $p->post_type ) ) { return new WP_REST_Response( array( 'ok' => false ), 404 ); } $t = swapfeed_primary_term( $id, swapfeed_taxonomy( $p->post_type ) ); $tid = $t ? (int) $t->term_id : 0; $days[ $day ]['p'][ $tid ] = max( 0, ( isset( $days[ $day ]['p'][ $tid ] ) ? $days[ $day ]['p'][ $tid ] : 0 ) + $delta ); $posts = get_option( SWAPFEED_STATS_POSTS, array() ); if ( ! is_array( $posts ) ) $posts = array(); $posts[ $id ] = max( 0, ( isset( $posts[ $id ] ) ? $posts[ $id ] : 0 ) + $delta ); if ( ! $posts[ $id ] ) unset( $posts[ $id ] ); if ( count( $posts ) > 1500 ) { arsort( $posts ); $posts = array_slice( $posts, 0, 1000, true ); } update_option( SWAPFEED_STATS_POSTS, $posts, false ); } else { $t = $term ? get_term( $term ) : null; if ( ! $t || is_wp_error( $t ) ) return new WP_REST_Response( array( 'ok' => false ), 404 ); $days[ $day ]['t'][ $term ] = max( 0, ( isset( $days[ $day ]['t'][ $term ] ) ? $days[ $day ]['t'][ $term ] : 0 ) + $delta ); } $min = wp_date( 'Ymd', time() - SWAPFEED_STATS_KEEP * DAY_IN_SECONDS ); foreach ( array_keys( $days ) as $k ) { if ( (string) $k < $min ) unset( $days[ $k ] ); } update_option( SWAPFEED_STATS_DAYS, $days, false ); $res = new WP_REST_Response( array( 'ok' => true ) ); $res->header( 'Cache-Control', 'no-store' ); return $res; } /** Aggregate for the report. */ function swapfeed_stats_summary( $range ) { $min = wp_date( 'Ymd', time() - ( $range - 1 ) * DAY_IN_SECONDS ); $days = get_option( SWAPFEED_STATS_DAYS, array() ); $P = $T = array(); foreach ( (array) $days as $k => $d ) { if ( (string) $k < $min ) continue; foreach ( (array) ( isset( $d['p'] ) ? $d['p'] : array() ) as $t => $n ) $P[ $t ] = ( isset( $P[ $t ] ) ? $P[ $t ] : 0 ) + $n; foreach ( (array) ( isset( $d['t'] ) ? $d['t'] : array() ) as $t => $n ) $T[ $t ] = ( isset( $T[ $t ] ) ? $T[ $t ] : 0 ) + $n; } return array( $P, $T ); }