@@ -17,6 +17,12 @@ public function __construct() {
1717 add_action ( 'admin_notices ' , array ( $ this , 'admin_notice_missing_wallet_address ' ) );
1818 // Process form submissions early
1919 add_action ( 'admin_init ' , array ( $ this , 'handle_save_settings ' ) );
20+
21+ // New: Posts page PayButton Unlocks column
22+ add_filter ( 'manage_edit-post_columns ' , [ $ this , 'register_paybutton_unlocks_column ' ] );
23+ add_action ( 'manage_post_posts_custom_column ' , [ $ this , 'render_paybutton_unlocks_column ' ], 10 , 2 );
24+ add_filter ( 'manage_edit-post_sortable_columns ' , [ $ this , 'register_paybutton_unlocks_sortable ' ] );
25+ add_filter ( 'posts_clauses ' , [ $ this , 'apply_paybutton_unlocks_sorting ' ], 10 , 2 );
2026 }
2127
2228 /**
@@ -499,4 +505,75 @@ public function content_page() {
499505 );
500506 $ this ->load_admin_template ( 'content ' , $ args );
501507 }
502- }
508+
509+ //New methods to show unlock counts for each post on the core WP Posts page
510+ /**
511+ * 1) Register a new “PayButton Unlocks” column on the Posts page.
512+ */
513+ public function register_paybutton_unlocks_column ( $ columns ) {
514+ $ new = [];
515+ foreach ( $ columns as $ key => $ label ) {
516+ $ new [ $ key ] = $ label ;
517+ if ( 'title ' === $ key ) {
518+ $ new ['unlock_count ' ] = __ ( 'PayButton Unlocks ' , 'paybutton ' );
519+ }
520+ }
521+ return $ new ;
522+ }
523+
524+ /**
525+ * 2) Populate our “PayButton Unlocks” column with the total unlock count.
526+ */
527+ public function render_paybutton_unlocks_column ( $ column , $ post_id ) {
528+ if ( 'unlock_count ' !== $ column ) {
529+ return ;
530+ }
531+ global $ wpdb ;
532+ $ table = $ wpdb ->prefix . 'paybutton_paywall_unlocked ' ;
533+ $ count = (int ) $ wpdb ->get_var ( $ wpdb ->prepare (
534+ "SELECT COUNT(*) FROM {$ table } WHERE post_id = %d " ,
535+ $ post_id
536+ ) );
537+ echo esc_html ( $ count );
538+ }
539+
540+ /**
541+ * 3) Tell WP that our “unlock_count” column is sortable.
542+ */
543+ public function register_paybutton_unlocks_sortable ( $ columns ) {
544+ $ columns ['unlock_count ' ] = 'unlock_count ' ;
545+ return $ columns ;
546+ }
547+
548+ /**
549+ * 4) When sorting by unlock_count, JOIN our table and ORDER BY its COUNT(*).
550+ */
551+ public function apply_paybutton_unlocks_sorting ( $ clauses , $ query ) {
552+ if ( ! is_admin () || ! $ query ->is_main_query () ) {
553+ return $ clauses ;
554+ }
555+ // Only when ordering by our column
556+ if ( 'unlock_count ' !== $ query ->get ( 'orderby ' ) ) {
557+ return $ clauses ;
558+ }
559+
560+ global $ wpdb ;
561+ $ unlock_table = $ wpdb ->prefix . 'paybutton_paywall_unlocked ' ;
562+
563+ // LEFT JOIN: grab per-post unlock counts
564+ $ clauses ['join ' ] .= "
565+ LEFT JOIN (
566+ SELECT post_id, COUNT(*) AS unlock_count
567+ FROM {$ unlock_table }
568+ GROUP BY post_id
569+ ) AS pb_unlocks
570+ ON {$ wpdb ->posts }.ID = pb_unlocks.post_id
571+ " ;
572+
573+ // Use the requested direction, default DESC
574+ $ dir = strtoupper ( $ query ->get ( 'order ' ) ) === 'ASC ' ? 'ASC ' : 'DESC ' ;
575+ $ clauses ['orderby ' ] = " pb_unlocks.unlock_count {$ dir } " ;
576+
577+ return $ clauses ;
578+ }
579+ }
0 commit comments