get_setting( '_tcacc_analytics', 'off' ); if ( ! in_array( $mode, array( 'ga', 'count', 'both' ), true ) || empty( $context['post_id'] ) ) { return $attributes; } return $attributes . ' data-tcacc-analytics="' . esc_attr( $mode ) . '" data-tcacc-id="' . absint( $context['post_id'] ) . '"'; } /** * Register the endpoint that receives open events. * * @return void */ public function register_route() { register_rest_route( 'tcaccordion/v1', '/track', array( 'methods' => 'POST', 'callback' => array( $this, 'handle_track' ), 'permission_callback' => '__return_true', 'args' => array( 'id' => array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), 'title' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), ), ) ); } /** * Count one opened item. * * @param WP_REST_Request $request Request. * @return WP_REST_Response */ public function handle_track( $request ) { $id = absint( $request->get_param( 'id' ) ); $title = mb_substr( trim( (string) $request->get_param( 'title' ) ), 0, 120 ); if ( ! $id || '' === $title ) { return new WP_REST_Response( array( 'ok' => false ), 400 ); } $post = get_post( $id ); if ( ! $post || 'accordion_tp' !== $post->post_type || 'publish' !== $post->post_status ) { return new WP_REST_Response( array( 'ok' => false ), 404 ); } if ( ! in_array( get_post_meta( $id, '_tcacc_analytics', true ), array( 'count', 'both' ), true ) ) { return new WP_REST_Response( array( 'ok' => false ), 403 ); } // Simple rate limit per visitor address (not stored, only hashed for the counter key). $address = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $key = 'tcacc_rl_' . md5( $address ); $hits = (int) get_transient( $key ); if ( $hits >= 60 ) { return new WP_REST_Response( array( 'ok' => false ), 429 ); } set_transient( $key, $hits + 1, MINUTE_IN_SECONDS ); $stats = get_post_meta( $id, self::META_STATS, true ); if ( ! is_array( $stats ) || ! isset( $stats['items'] ) || ! is_array( $stats['items'] ) ) { $stats = array( 'total' => 0, 'items' => array(), ); } $hash = substr( md5( mb_strtolower( $title ) ), 0, 12 ); if ( ! isset( $stats['items'][ $hash ] ) ) { if ( count( $stats['items'] ) >= self::MAX_ITEMS ) { return new WP_REST_Response( array( 'ok' => true ), 200 ); } $stats['items'][ $hash ] = array( 't' => $title, 'n' => 0, ); } $stats['items'][ $hash ]['n']++; $stats['total'] = isset( $stats['total'] ) ? (int) $stats['total'] + 1 : 1; update_post_meta( $id, self::META_STATS, $stats ); return new WP_REST_Response( array( 'ok' => true ), 200 ); } /** * Settings rows and the statistics table. * * @param WP_Post $post Accordion post. * @return void */ public function render_settings( $post ) { $mode = get_post_meta( $post->ID, '_tcacc_analytics', true ); $mode = in_array( $mode, $this->modes, true ) ? $mode : 'off'; $stats = get_post_meta( $post->ID, self::META_STATS, true ); $stats = ( is_array( $stats ) && ! empty( $stats['items'] ) ) ? $stats : array(); ?>

'tcaccordion_reset_stats', 'post' => $post->ID, ), admin_url( 'admin-post.php' ) ), 'tcaccordion_reset_stats_' . $post->ID ); ?>

 | 

modes, true ) ? $mode : 'off' ); } // phpcs:enable WordPress.Security.NonceVerification.Missing } /** * Delete the counters of one accordion. * * @return void */ public function handle_reset() { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; check_admin_referer( 'tcaccordion_reset_stats_' . $post_id ); if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) || 'accordion_tp' !== get_post_type( $post_id ) ) { wp_die( esc_html__( 'You are not allowed to reset these statistics.', 'accordions-wp' ), '', array( 'response' => 403 ) ); } delete_post_meta( $post_id, self::META_STATS ); wp_safe_redirect( get_edit_post_link( $post_id, 'raw' ) ); exit; } } new TCAccordion_Pro_Analytics();