Shape the future of Social networking with WordPress: Join Project Midnight Sun! The next generation platform for community building with WordPress!

BuddyDev

Search

Replies

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #56201
    This reply has been marked as private.
  • Participant
    Level: Guru
    Posts: 922
    Tosin on #56198
    This reply has been marked as private.
  • Participant
    Level: Guru
    Posts: 922
    Tosin on #56194
    This reply has been marked as private.
  • Participant
    Level: Guru
    Posts: 922
    Tosin on #56193
    This reply has been marked as private.
  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55934

    yes Brajesh I would really appreciate the change in code

    Thanks

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55879

    Hello Brajesh,

    Your addon feature is different, it reactivates accounts after a specified number of days

    But what I want is to automatically deactivate account of users who have been inactive for 2 years

    Thanks

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55821

    Hello Brajesh

    This plugin is the cause BuddyPress Auto Friendship Pro Version 1.2.5

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55816

    Hi Brajesh

    This code seemed to work

     /**
     * Assign (Sponsored Post) category to BuddyBlog pay per post articles using purchase meta data.
     */
    function assign_sponsored_term_on_purchase( $post_id, $form_id = null ) {
        // Ensure this is a post type 'post'
        if ( 'post' !== get_post_type( $post_id ) ) {
            return;
        }
    
        // Check post status early - if not published, return immediately
        if ( 'publish' !== get_post_status( $post_id ) ) {
            return;
        }
    
        // Check if the post has any purchase-related meta data
        $has_purchase_meta = get_post_meta( $post_id, '_bblpro_ppp_purchased', true );
        $has_expiry_time = get_post_meta( $post_id, '_bblpro_ppp_expiry_time', false );
        
        // If the post has purchase-related meta, assume it was purchased
        if ( ! $has_purchase_meta && empty( $has_expiry_time ) ) {
            return;
        }
    
        // Get the taxonomy term 'Sponsored Posts' from 'content_type'
        $new_term = get_term_by( 'name', 'Sponsored Posts', 'content_type' );
        if ( ! $new_term ) {
            return;
        }
    
        // Assign the term to the post
        wp_set_post_terms( $post_id, $new_term->term_id, 'content_type', true );
    }
    add_action( 'bblpro_ppp_post_purchase_completed', 'assign_sponsored_term_on_purchase', 10, 2 ); 

    Thanks

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55804

    Those two plugin are not the issue, this are the plugins I have enabled
    (BuddyDev) BP Deactivate Account
    (BuddyDev) Stealth Mode for Site admin
    BuddyBlog Pay Per Post
    BuddyBlog Pro
    BP Confirm Actions
    BP Non Editable Profile Fields
    BuddyBlog Pay Per Post
    BuddyBlog Pro
    BuddyDev Dashboard
    BuddyPress Activity Autoloader
    BuddyPress Auto Activate Autologin Redirect To Profile On Signup
    BuddyPress Auto Friendship Pro
    BuddyPress Default Email Notification Settings Control
    BuddyPress Featured Members
    BuddyPress Member Types Pro
    BuddyPress Moderation Tools
    BuddyPress Profile Completion
    BuddyPress Profile Visibility Manager
    BuddyPress Username Changer
    BuddyPress Xprofile Custom Field Types
    Conditional Profile Fields for BuddyPress
    User Login Notifier for WordPress
    WordPress Ban Registration Domain
    WordPress Restrict Email Domains
    WordPress Username Availability Checker

    This is my error log below

    Critical Uncaught Error: Call to undefined method Press_Themes\PT_Settings\Page::set_sidebar_config() in /var/www/wptbox/wp-content/plugins/buddyblog-pro/src/admin/class-settings-admin.php:909
    Additional context
    {
    “error”: {
    “type”: 1,
    “file”: “/var/www/wptbox/wp-content/plugins/buddyblog-pro/src/admin/class-settings-admin.php”,
    “line”: 909
    },
    “remote-logging”: true,
    “backtrace”: [
    “”,
    “#0 /var/www/wptbox/wp-content/plugins/buddyblog-pro/src/admin/class-settings-admin.php(84): BuddyBlog_Pro\Admin\Settings_Admin->setup_sidebar()”,
    “#1 /var/www/wptbox/wp-includes/class-wp-hook.php(324): BuddyBlog_Pro\Admin\Settings_Admin->init()”,
    “#2 /var/www/wptbox/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()”,
    “#3 /var/www/wptbox/wp-includes/plugin.php(517): WP_Hook->do_action()”,
    “#4 /var/www/wptbox/wp-admin/admin.php(176): do_action()”,
    “#5 /var/www/wptbox/wp-admin/options.php(19): require_once(‘…’)”,
    “#6 {main}”,
    “thrown”
    ]
    }

  • Participant
    Level: Guru
    Posts: 922
    Tosin on #55635

    Gentle Reminder sir,

     <?php
    /**
     * Plugin Name: BuddyPress Notifications Management Tool
     * Description: Manually or automatically delete BuddyPress user notifications based on age or schedule.
     * Version: 2.3.2
     * License: GPLv2 or later
     * Requires at least: 5.6
     * Requires PHP: 7.4
     * Requires Plugins: buddypress
     * Text Domain: bp-notifications-cleanup
     */
    
    defined( 'ABSPATH' ) || exit;
    
    class BP_Delete_All_Notifications_Auto {
    
        private $opt_schedule = 'bp_delete_notifications_schedule';
        private $opt_days     = 'bp_delete_notifications_days';
        const CRON_HOOK = 'bp_delete_all_notifications_cron';
        const CHUNK_SIZE = 1000;
        const NONCE_ACTION = 'delete_all_bp_notifications_action';
        const NONCE_NAME = 'delete_all_bp_notifications_nonce';
    
        public function __construct() {
            add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
            add_action( 'admin_post_delete_all_bp_notifications', array( $this, 'handle_delete_notifications' ) );
            add_action( 'admin_notices', array( $this, 'admin_notices' ) );
            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_confirmation_script' ) );
            add_action( 'admin_init', array( $this, 'register_settings' ) );
            add_action( self::CRON_HOOK, array( $this, 'cron_cleanup' ) );
            add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ) );
            register_activation_hook( __FILE__, array( $this, 'activate' ) );
            register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
            register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
            $this->maybe_schedule_cron();
            
            // BuddyPress dependency notice
            add_action( 'admin_notices', array( $this, 'buddypress_missing_notice' ) );
        }
    
        public function add_admin_menu() {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
    
            // Only show if notifications component is active
            if ( ! $this->is_notifications_component_active() ) {
                return;
            }
    
            add_submenu_page(
                'tools.php',
                __( 'BP Notification Cleanup', 'bp-notifications-cleanup' ),
                __( 'BP Notification Cleanup', 'bp-notifications-cleanup' ),
                'manage_options',
                'delete-bp-notifications',
                array( $this, 'admin_page_content' )
            );
        }
    
        public function admin_page_content() {
            if ( ! current_user_can( 'manage_options' ) ) {
                return;
            }
    
            $schedule = get_option( $this->opt_schedule, 'none' );
            $days     = get_option( $this->opt_days, 30 );
            ?>
            <div class="wrap">
                <h1><?php esc_html_e( 'BuddyPress Notification Cleanup', 'bp-notifications-cleanup' ); ?></h1>
                <?php if ( ! $this->is_notifications_component_active() ) : ?>
                    <div class="notice notice-error">
                        <p><?php esc_html_e( 'BuddyPress Notifications component is not active.', 'bp-notifications-cleanup' ); ?></p>
                    </div>
                <?php else : ?>
                    <div class="card">
                        <h2><?php esc_html_e( 'Manual Delete', 'bp-notifications-cleanup' ); ?></h2>
                        <p><?php esc_html_e( 'This will permanently delete all notifications for all users.', 'bp-notifications-cleanup' ); ?></p>
                        <p><?php esc_html_e( 'Total Notifications:', 'bp-notifications-cleanup' ); ?> 
                            <strong><?php echo esc_html( number_format_i18n( $this->get_notifications_count() ) ); ?></strong>
                        </p>
                        <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" id="bp-delete-notifications-form">
                            <?php wp_nonce_field( self::NONCE_ACTION, self::NONCE_NAME ); ?>
                            <input type="hidden" name="action" value="delete_all_bp_notifications">
                            <p>
                                <input type="submit" class="button button-danger" value="<?php esc_attr_e( 'Delete All Notifications', 'bp-notifications-cleanup' ); ?>">
                            </p>
                        </form>
                    </div>
                    <hr>
                    <div class="card">
                        <h2><?php esc_html_e( 'Auto Cleanup Settings', 'bp-notifications-cleanup' ); ?></h2>
                        <form method="post" action="options.php">
                            <?php
                            settings_fields( 'bp_notifications_cleanup_settings' );
                            do_settings_sections( 'bp_notifications_cleanup' );
                            submit_button( __( 'Save Settings', 'bp-notifications-cleanup' ) );
                            ?>
                        </form>
                        <p>
                            <?php esc_html_e( 'Schedule:', 'bp-notifications-cleanup' ); ?> 
                            <strong><?php echo esc_html( ucfirst( $schedule ) ); ?></strong>
                        </p>
                        <p>
                            <?php esc_html_e( 'Retention Days:', 'bp-notifications-cleanup' ); ?> 
                            <strong><?php echo esc_html( $days ); ?></strong>
                            (<?php esc_html_e( 'Notifications older than this will be deleted.', 'bp-notifications-cleanup' ); ?>)
                        </p>
                        <?php if ( 'none' !== $schedule ) : ?>
                            <p>
                                <?php esc_html_e( 'Next scheduled cleanup:', 'bp-notifications-cleanup' ); ?>
                                <strong>
                                    <?php
                                    $next = wp_next_scheduled( self::CRON_HOOK );
                                    if ($next) {
                                        $format = get_option('date_format') . ' ' . get_option('time_format');
                                        $timestamp = $next + (get_option('gmt_offset') * HOUR_IN_SECONDS);
                                        echo esc_html(date_i18n($format, $timestamp));
                                    } else {
                                        esc_html_e('Not scheduled', 'bp-notifications-cleanup');
                                    }
                                    ?>
                                </strong>
                            </p>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            </div>
            <?php
        }
    
        public function handle_delete_notifications() {
            if ( ! current_user_can( 'manage_options' ) ) {
                wp_die( esc_html__( 'Permission denied', 'bp-notifications-cleanup' ) );
            }
            
            check_admin_referer( self::NONCE_ACTION, self::NONCE_NAME );
            
            $nonce    = wp_create_nonce( 'notifications_deleted' );
            $redirect = admin_url( 'tools.php?page=delete-bp-notifications' );
            
            try {
                $this->perform_chunked_deletion();
                $redirect = add_query_arg( array( 'deleted' => 1, '_wpnonce' => $nonce ), $redirect );
            } catch ( Exception $e ) {
                error_log( 'BP Notification Cleanup Error: ' . $e->getMessage() );
                $redirect = add_query_arg( array( 'error' => 1 ), $redirect );
            }
            
            wp_safe_redirect( $redirect );
            exit;
        }
    
        private function perform_chunked_deletion() {
            global $wpdb;
            $table = $wpdb->prefix . 'bp_notifications';
            
            if ( ! $this->table_exists( $table ) ) {
                return;
            }
    
            $continue = true;
            $chunk_size = self::CHUNK_SIZE;
    
            while ( $continue ) {
                $query = "DELETE FROM <code>&quot; . esc_sql( $table ) . &quot;</code> LIMIT {$chunk_size}";
                $rows_affected = $wpdb->query( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                
                if ( $rows_affected === false ) {
                    throw new Exception( 'Database error during chunked deletion' );
                }
                
                if ( $rows_affected < $chunk_size ) {
                    $continue = false;
                }
                
                // Prevent server overload
                if ( $rows_affected > 0 ) {
                    usleep( 100000 ); // 0.1 second delay between chunks
                }
            }
        }
    
        public function cron_cleanup() {
            // Only run if notifications component is active
            if ( ! $this->is_notifications_component_active() ) {
                return;
            }
    
            global $wpdb;
            $table = $wpdb->prefix . 'bp_notifications';
            
            if ( ! $this->table_exists( $table ) ) {
                return;
            }
            
            $days = absint( get_option( $this->opt_days, 30 ) );
            if ( $days <= 0 ) {
                return;
            }
    
            $continue = true;
            $chunk_size = self::CHUNK_SIZE;
    
            while ( $continue ) {
                $query = $wpdb->prepare(
                    "DELETE FROM <code>&quot; . esc_sql( $table ) . &quot;</code> 
                    WHERE date_notified < ( NOW() - INTERVAL %d DAY )
                    LIMIT %d",
                    $days,
                    $chunk_size
                );
                
                $rows_affected = $wpdb->query( $query ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
                
                if ( $rows_affected === false ) {
                    error_log( 'BP Notification Cron Cleanup Error: Database query failed' );
                    break;
                }
                
                if ( $rows_affected < $chunk_size ) {
                    $continue = false;
                }
                
                // Prevent server overload
                if ( $rows_affected > 0 ) {
                    usleep( 100000 ); // 0.1 second delay between chunks
                }
            }
        }
    
        public function admin_notices() {
            if ( ! isset( $_GET['page'] ) || 'delete-bp-notifications' !== $_GET['page'] ) {
                return;
            }
    
            if ( isset( $_GET['deleted'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'notifications_deleted' ) ) {
                echo '<div class="notice notice-success is-dismissible"><p>' . 
                     esc_html__( 'All BuddyPress notifications deleted.', 'bp-notifications-cleanup' ) . 
                     '</p></div>';
            }
    
            if ( isset( $_GET['error'] ) ) {
                echo '<div class="notice notice-error is-dismissible"><p>' . 
                     esc_html__( 'Error deleting notifications. Please check logs.', 'bp-notifications-cleanup' ) . 
                     '</p></div>';
            }
        }
        
        public function buddypress_missing_notice() {
            // Skip if BuddyPress is active
            if ( function_exists( 'buddypress' ) ) {
                return;
            }
            
            // Skip if we're on our own settings page
            $screen = get_current_screen();
            if ( $screen && isset( $screen->id ) && 'tools_page_delete-bp-notifications' === $screen->id ) {
                return;
            }
            
            echo '<div class="notice notice-error"><p>' . 
                 esc_html__( 'BuddyPress Delete Notifications requires BuddyPress to be installed and activated.', 'bp-notifications-cleanup' ) . 
                 '</p></div>';
        }
    
        public function enqueue_confirmation_script( $hook ) {
            if ( 'tools_page_delete-bp-notifications' !== $hook ) {
                return;
            }
    
            $message = __( 'This will permanently delete ALL notifications. Continue?', 'bp-notifications-cleanup' );
            wp_add_inline_script(
                'jquery',
                'jQuery(document).ready(function($){
                    $("#bp-delete-notifications-form").on("submit", function(e){
                        if(!confirm("' . esc_js( $message ) . '")) {
                            e.preventDefault();
                        }
                    });
                });'
            );
        }
    
        private function get_notifications_count() {
            global $wpdb;
            $table = $wpdb->prefix . 'bp_notifications';
            
            if ( ! $this->table_exists( $table ) ) {
                return 0;
            }
            
            try {
                return (int) $wpdb->get_var( "SELECT COUNT(*) FROM <code>&quot; . esc_sql( $table ) . &quot;</code>" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            } catch ( Exception $e ) {
                error_log( 'BP Notification Count Error: ' . $e->getMessage() );
                return 0;
            }
        }
    
        public function register_settings() {
            register_setting(
                'bp_notifications_cleanup_settings',
                $this->opt_schedule,
                array(
                    'type'              => 'string',
                    'sanitize_callback' => array( $this, 'sanitize_schedule' ),
                    'default'           => 'none',
                )
            );
            register_setting(
                'bp_notifications_cleanup_settings',
                $this->opt_days,
                array(
                    'type'              => 'integer',
                    'sanitize_callback' => array( $this, 'sanitize_retention_days' ),
                    'default'           => 30,
                )
            );
            
            add_settings_section(
                'bp_notifications_cleanup_section',
                __( 'Cleanup Options', 'bp-notifications-cleanup' ),
                null,
                'bp_notifications_cleanup'
            );
            
            add_settings_field(
                'schedule',
                __( 'Cleanup Schedule', 'bp-notifications-cleanup' ),
                array( $this, 'schedule_field_cb' ),
                'bp_notifications_cleanup',
                'bp_notifications_cleanup_section'
            );
            
            add_settings_field(
                'days',
                __( 'Retention Days', 'bp-notifications-cleanup' ),
                array( $this, 'days_field_cb' ),
                'bp_notifications_cleanup',
                'bp_notifications_cleanup_section'
            );
        }
    
        public function schedule_field_cb() {
            $v = get_option( $this->opt_schedule, 'none' );
            ?>
            <select name="<?php echo esc_attr( $this->opt_schedule ); ?>">
                <option value="none" <?php selected( $v, 'none' ); ?>>
                    <?php esc_html_e( 'None', 'bp-notifications-cleanup' ); ?>
                </option>
                <option value="daily" <?php selected( $v, 'daily' ); ?>>
                    <?php esc_html_e( 'Daily', 'bp-notifications-cleanup' ); ?>
                </option>
                <option value="weekly" <?php selected( $v, 'weekly' ); ?>>
                    <?php esc_html_e( 'Weekly', 'bp-notifications-cleanup' ); ?>
                </option>
                <option value="monthly" <?php selected( $v, 'monthly' ); ?>>
                    <?php esc_html_e( 'Monthly', 'bp-notifications-cleanup' ); ?>
                </option>
            </select>
            <?php
        }
    
        public function days_field_cb() {
            $v = get_option( $this->opt_days, 30 );
            ?>
            <input type="number" 
                   name="<?php echo esc_attr( $this->opt_days ); ?>" 
                   value="<?php echo esc_attr( $v ); ?>" 
                   min="1" 
                   class="small-text" />
            <p class="description">
                <?php esc_html_e( 'Notifications older than this many days will be deleted during automatic cleanup.', 'bp-notifications-cleanup' ); ?>
            </p>
            <?php
        }
    
        public function sanitize_schedule( $v ) {
            $allowed = array( 'none', 'daily', 'weekly', 'monthly' );
            $v = in_array( $v, $allowed, true ) ? $v : 'none';
            $this->reschedule_cron( $v );
            return $v;
        }
        
        public function sanitize_retention_days( $days ) {
            return max( 1, absint( $days ) );
        }
    
        private function maybe_schedule_cron() {
            $schedule = get_option( $this->opt_schedule, 'none' );
            if ( 'none' !== $schedule && ! wp_next_scheduled( self::CRON_HOOK ) ) {
                wp_schedule_event( time(), $schedule, self::CRON_HOOK );
            }
        }
    
        private function reschedule_cron( $new_schedule ) {
            wp_clear_scheduled_hook( self::CRON_HOOK );
            if ( 'none' !== $new_schedule ) {
                wp_schedule_event( time(), $new_schedule, self::CRON_HOOK );
            }
        }
    
        public function filter_cron_schedules( $schedules ) {
            if ( ! isset( $schedules['monthly'] ) ) {
                $schedules['monthly'] = array(
                    'interval' => 30 * DAY_IN_SECONDS,
                    'display'  => __( 'Once Monthly (30 days)', 'bp-notifications-cleanup' )
                );
            }
            return $schedules;
        }
    
        private function table_exists( $table ) {
            global $wpdb;
            return $wpdb->get_var( $wpdb->prepare( 
                "SHOW TABLES LIKE %s", 
                $wpdb->esc_like( $table ) 
            ) ) === $table;
        }
        
        private function is_notifications_component_active() {
            return function_exists( 'bp_is_active' ) && bp_is_active( 'notifications' );
        }
    
        public function activate() {
            global $wpdb;
            $table = $wpdb->prefix . 'bp_notifications';
            
            if ( $this->table_exists( $table ) ) {
                // Check if index exists using SHOW INDEX
                $index_exists = $wpdb->get_var( $wpdb->prepare(
                    "SHOW INDEX FROM <code>$table</code> WHERE Key_name = 'date_notified'"
                ) );
                
                if ( ! $index_exists ) {
                    $wpdb->query( "ALTER TABLE <code>$table</code> ADD INDEX <code>date_notified</code> (<code>date_notified</code>)" );
                }
            }
            
            $schedule = get_option( $this->opt_schedule, 'none' );
            $this->reschedule_cron( $schedule );
        }
    
        public function deactivate() {
            wp_clear_scheduled_hook( self::CRON_HOOK );
        }
        
        public static function uninstall() {
            delete_option( 'bp_delete_notifications_schedule' );
            delete_option( 'bp_delete_notifications_days' );
            wp_clear_scheduled_hook( self::CRON_HOOK );
        }
    }
    
    new BP_Delete_All_Notifications_Auto(); 

    Thanks