increment_site_count(); // Increment per-popup count. $this->increment_popup_count( $popup_id ); /** * Fires after a link click is tracked. * * @since 1.22.0 * * @param int $popup_id Popup ID. * @param array $event_data Link click event data (url, linkType, etc.). */ do_action( 'popup_maker/link_click_tracked', $popup_id, $event_data ); } /** * Increment site-wide link click count. * * Uses atomic SQL update to prevent race conditions. * * @since 1.22.0 * * @return int New count after increment. */ protected function increment_site_count() { global $wpdb; // Check if option exists; if not, create it with autoload disabled. $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT option_id FROM %i WHERE option_name = %s LIMIT 1', $wpdb->options, self::SITE_COUNT_KEY ) ); if ( ! $exists ) { add_option( self::SITE_COUNT_KEY, 0, '', false ); } // Atomic increment (prevents race condition). $wpdb->query( $wpdb->prepare( 'UPDATE %i SET option_value = option_value + 1 WHERE option_name = %s', $wpdb->options, self::SITE_COUNT_KEY ) ); wp_cache_delete( self::SITE_COUNT_KEY, 'options' ); return (int) get_option( self::SITE_COUNT_KEY, 0 ); } /** * Increment per-popup link click count. * * Uses atomic SQL update to prevent race conditions. * * @since 1.22.0 * * @param int $popup_id Popup post ID. * @return int New count after increment. */ protected function increment_popup_count( $popup_id ) { global $wpdb; $exists = $wpdb->get_var( $wpdb->prepare( 'SELECT meta_id FROM %i WHERE post_id = %d AND meta_key = %s LIMIT 1', $wpdb->postmeta, $popup_id, self::POPUP_META_KEY ) ); if ( ! $exists ) { add_post_meta( $popup_id, self::POPUP_META_KEY, 0, true ); } // Atomic increment. $wpdb->query( $wpdb->prepare( 'UPDATE %i SET meta_value = meta_value + 1 WHERE post_id = %d AND meta_key = %s', $wpdb->postmeta, $popup_id, self::POPUP_META_KEY ) ); wp_cache_delete( $popup_id, 'post_meta' ); return (int) get_post_meta( $popup_id, self::POPUP_META_KEY, true ); } /** * Get site-wide link click count. * * @since 1.22.0 * * @return int Total link clicks across all popups. */ public function get_site_count() { return (int) get_option( self::SITE_COUNT_KEY, 0 ); } /** * Get link click count for a specific popup. * * @since 1.22.0 * * @param int $popup_id Popup post ID. * @return int Link clicks for this popup. */ public function get_popup_count( $popup_id ) { return (int) get_post_meta( $popup_id, self::POPUP_META_KEY, true ); } /** * Reset site-wide link click count. * * @since 1.22.0 */ public function reset_site_count() { delete_option( self::SITE_COUNT_KEY ); } /** * Reset link click count for a specific popup. * * @since 1.22.0 * * @param int $popup_id Popup post ID. */ public function reset_popup_count( $popup_id ) { delete_post_meta( $popup_id, self::POPUP_META_KEY ); } }